mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
4.2: enforce quality validation with tests
- Implement quality bounds checking on initialization - Add special handling for legendary items - Include comprehensive test coverage
This commit is contained in:
parent
7c52f599a3
commit
2f9fe5c9a6
@ -16,7 +16,16 @@ class GildedRose(object):
|
||||
|
||||
def __init__(self, items):
|
||||
self.items = items
|
||||
self.items = items
|
||||
for item in items:
|
||||
self._enforce_quality_bounds(item)
|
||||
|
||||
def _enforce_quality_bounds(self, item):
|
||||
if item.name == self.LEGENDARY_ITEM:
|
||||
if item.quality != self.LEGENDARY_QUALITY:
|
||||
item.quality = self.LEGENDARY_QUALITY # Enforce legendary quality
|
||||
else:
|
||||
item.quality = max(0, min(self.MAX_QUALITY, item.quality))
|
||||
|
||||
def update_quality(self):
|
||||
for item in self.items:
|
||||
@ -32,7 +41,7 @@ class GildedRose(object):
|
||||
self.update_backstage_pass(item)
|
||||
elif ITEM_NAME.CONJURED in item.name:
|
||||
self.update_conjured_item(item)
|
||||
elif item.name != ITEM_NAME.SULFURAS:
|
||||
elif item.name != self.LEGENDARY_ITEM:
|
||||
self.update_normal_item(item)
|
||||
|
||||
def update_conjured_item(self, item):
|
||||
@ -40,7 +49,7 @@ class GildedRose(object):
|
||||
self.decrease_quality(item)
|
||||
|
||||
def update_sell_in(self, item):
|
||||
if item.name != ITEM_NAME.SULFURAS:
|
||||
if item.name != self.LEGENDARY_ITEM:
|
||||
item.sell_in -= 1
|
||||
|
||||
def handle_expired_item(self, item):
|
||||
@ -51,7 +60,7 @@ class GildedRose(object):
|
||||
elif ITEM_NAME.CONJURED in item.name:
|
||||
self.decrease_quality(item)
|
||||
self.decrease_quality(item)
|
||||
elif item.name != ITEM_NAME.SULFURAS:
|
||||
elif item.name != self.LEGENDARY_ITEM:
|
||||
self.decrease_quality(item)
|
||||
|
||||
def update_aged_brie(self, item):
|
||||
|
||||
@ -107,6 +107,7 @@ class GildedRoseTest(unittest.TestCase):
|
||||
gilded_rose.update_quality()
|
||||
self.assertEqual("foo", items[0].name)
|
||||
|
||||
# Conjured Items
|
||||
def test_conjured_item_quality_decreases_twice_as_fast(self):
|
||||
items = [Item("Conjured Mana Cake", 10, 20)]
|
||||
gilded_rose = GildedRose(items)
|
||||
@ -125,6 +126,55 @@ class GildedRoseTest(unittest.TestCase):
|
||||
gilded_rose.update_quality()
|
||||
self.assertEqual(0, items[0].quality)
|
||||
|
||||
# Add these test cases to enforce quality bounds
|
||||
|
||||
def test_legendary_item_quality_enforced_on_creation(self):
|
||||
"""Test that Sulfuras quality is automatically set to 80 if initialized with wrong value"""
|
||||
items = [Item("Sulfuras, Hand of Ragnaros", 5, 50)] # Incorrect quality
|
||||
gilded_rose = GildedRose(items)
|
||||
self.assertEqual(80, items[0].quality) # Should be corrected to 80
|
||||
|
||||
def test_normal_item_quality_clamped_on_creation(self):
|
||||
"""Test that normal items' quality is clamped to 0-50 range during initialization"""
|
||||
items = [Item("Normal Item", 5, 60)] # Above max
|
||||
gilded_rose = GildedRose(items)
|
||||
self.assertEqual(50, items[0].quality)
|
||||
|
||||
items = [Item("Normal Item", 5, -5)] # Below min
|
||||
gilded_rose = GildedRose(items)
|
||||
self.assertEqual(0, items[0].quality)
|
||||
|
||||
def test_legendary_item_quality_unchanged_by_updates(self):
|
||||
"""Test that Sulfuras quality remains 80 even after multiple updates"""
|
||||
items = [Item("Sulfuras, Hand of Ragnaros", 5, 80)]
|
||||
gilded_rose = GildedRose(items)
|
||||
for _ in range(5): # Multiple updates
|
||||
gilded_rose.update_quality()
|
||||
self.assertEqual(80, items[0].quality)
|
||||
self.assertEqual(5, items[0].sell_in) # Sell_in should not change
|
||||
|
||||
def test_backstage_pass_quality_clamped_on_creation(self):
|
||||
"""Test that backstage pass quality is clamped to 0-50 during initialization"""
|
||||
items = [Item("Backstage passes to a TAFKAL80ETC concert", 15, 60)]
|
||||
gilded_rose = GildedRose(items)
|
||||
self.assertEqual(50, items[0].quality)
|
||||
|
||||
def test_conjured_item_quality_clamped_on_creation(self):
|
||||
"""Test that conjured items' quality is clamped to 0-50 during initialization"""
|
||||
items = [Item("Conjured Mana Cake", 5, 60)]
|
||||
gilded_rose = GildedRose(items)
|
||||
self.assertEqual(50, items[0].quality)
|
||||
|
||||
items = [Item("Conjured Mana Cake", 5, -10)]
|
||||
gilded_rose = GildedRose(items)
|
||||
self.assertEqual(0, items[0].quality)
|
||||
|
||||
def test_aged_brie_quality_clamped_on_creation(self):
|
||||
"""Test that Aged Brie quality is clamped to 0-50 during initialization"""
|
||||
items = [Item("Aged Brie", 5, 60)]
|
||||
gilded_rose = GildedRose(items)
|
||||
self.assertEqual(50, items[0].quality)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user