mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
Refactored the code to make it more readable
This commit is contained in:
parent
ab2b7cddd8
commit
a21bde290f
@ -1,5 +1,19 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
def decrease_quality(item, decrease_quality_by=None):
|
||||
if not decrease_quality_by:
|
||||
if item.sell_in <= 0:
|
||||
decrease_quality_by = 2
|
||||
else:
|
||||
decrease_quality_by = 1
|
||||
|
||||
item.quality = max(item.quality - decrease_quality_by, 0)
|
||||
|
||||
|
||||
def increase_quality(item, increase_quality_by=1):
|
||||
item.quality = min(item.quality + increase_quality_by, 50)
|
||||
|
||||
|
||||
class GildedRose(object):
|
||||
|
||||
def __init__(self, items):
|
||||
@ -7,33 +21,23 @@ class GildedRose(object):
|
||||
|
||||
def update_quality(self):
|
||||
for item in self.items:
|
||||
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert":
|
||||
if item.quality > 0:
|
||||
if item.name != "Sulfuras, Hand of Ragnaros":
|
||||
item.quality = item.quality - 1
|
||||
else:
|
||||
if item.quality < 50:
|
||||
item.quality = item.quality + 1
|
||||
if item.name == "Backstage passes to a TAFKAL80ETC concert":
|
||||
if item.sell_in < 11:
|
||||
if item.quality < 50:
|
||||
item.quality = item.quality + 1
|
||||
if item.sell_in < 6:
|
||||
if item.quality < 50:
|
||||
item.quality = item.quality + 1
|
||||
if item.name != "Sulfuras, Hand of Ragnaros":
|
||||
item.sell_in = item.sell_in - 1
|
||||
if item.sell_in < 0:
|
||||
if item.name != "Aged Brie":
|
||||
if item.name != "Backstage passes to a TAFKAL80ETC concert":
|
||||
if item.quality > 0:
|
||||
if item.name != "Sulfuras, Hand of Ragnaros":
|
||||
item.quality = item.quality - 1
|
||||
else:
|
||||
item.quality = item.quality - item.quality
|
||||
if item.name == "Sulfuras, Hand of Ragnaros":
|
||||
continue
|
||||
elif item.name == "Aged Brie":
|
||||
increase_quality(item)
|
||||
elif item.name == "Backstage passes to a TAFKAL80ETC concert":
|
||||
if item.sell_in <= 0:
|
||||
item.quality = 0
|
||||
elif item.sell_in <= 5:
|
||||
increase_quality(item, 3)
|
||||
elif item.sell_in <= 10:
|
||||
increase_quality(item, 2)
|
||||
else:
|
||||
if item.quality < 50:
|
||||
item.quality = item.quality + 1
|
||||
increase_quality(item)
|
||||
else:
|
||||
decrease_quality(item)
|
||||
|
||||
item.sell_in = item.sell_in - 1
|
||||
|
||||
|
||||
class Item:
|
||||
|
||||
@ -19,6 +19,14 @@ class GildedRoseTest(unittest.TestCase):
|
||||
# Quality should never go below 0
|
||||
self.assertEqual(0, item.quality)
|
||||
|
||||
def test_normal_item_quality_if_sell_in_is_0(self):
|
||||
item = Item(name="foo", sell_in=0, quality=4)
|
||||
gilded_rose = GildedRose([item])
|
||||
gilded_rose.update_quality()
|
||||
|
||||
# Quality will degrade by 2 once sell in date passed
|
||||
self.assertEqual(2, item.quality)
|
||||
|
||||
def test_aged_brie_quality_exceeded(self):
|
||||
item = Item(name="Aged Brie", sell_in=0, quality=50)
|
||||
gilded_rose = GildedRose([item])
|
||||
|
||||
Loading…
Reference in New Issue
Block a user