diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 4dd24099..393764c4 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -3,6 +3,7 @@ AGED_BRIE = "Aged Brie" BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert" SULFURAS = "Sulfuras, Hand of Ragnaros" +CONJURED_PREFIX = "Conjured" MIN_QUALITY = 0 MAX_QUALITY = 50 @@ -71,17 +72,29 @@ class SulfurasUpdater(ItemUpdater): pass +class ConjuredUpdater(ItemUpdater): + """Degrades twice as fast as an ordinary item.""" + + def _quality_delta(self, item): + return 2 * super()._quality_delta(item) + + _UPDATERS_BY_NAME = { AGED_BRIE: AgedBrieUpdater(), BACKSTAGE_PASS: BackstagePassUpdater(), SULFURAS: SulfurasUpdater(), } +_CONJURED_UPDATER = ConjuredUpdater() _DEFAULT_UPDATER = ItemUpdater() def updater_for(item): """Pick the rule set that applies to an item. Updaters are stateless.""" - return _UPDATERS_BY_NAME.get(item.name, _DEFAULT_UPDATER) + if item.name in _UPDATERS_BY_NAME: + return _UPDATERS_BY_NAME[item.name] + if item.name.startswith(CONJURED_PREFIX): + return _CONJURED_UPDATER + return _DEFAULT_UPDATER class Item: diff --git a/python/tests/approved_files/test_gilded_rose_approvals.test_gilded_rose_approvals.approved.txt b/python/tests/approved_files/test_gilded_rose_approvals.test_gilded_rose_approvals.approved.txt index a04e48ac..4e7237ff 100644 --- a/python/tests/approved_files/test_gilded_rose_approvals.test_gilded_rose_approvals.approved.txt +++ b/python/tests/approved_files/test_gilded_rose_approvals.test_gilded_rose_approvals.approved.txt @@ -21,7 +21,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 14, 21 Backstage passes to a TAFKAL80ETC concert, 9, 50 Backstage passes to a TAFKAL80ETC concert, 4, 50 -Conjured Mana Cake, 2, 5 +Conjured Mana Cake, 2, 4 -------- day 2 -------- name, sellIn, quality @@ -33,7 +33,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 13, 22 Backstage passes to a TAFKAL80ETC concert, 8, 50 Backstage passes to a TAFKAL80ETC concert, 3, 50 -Conjured Mana Cake, 1, 4 +Conjured Mana Cake, 1, 2 -------- day 3 -------- name, sellIn, quality @@ -45,7 +45,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 12, 23 Backstage passes to a TAFKAL80ETC concert, 7, 50 Backstage passes to a TAFKAL80ETC concert, 2, 50 -Conjured Mana Cake, 0, 3 +Conjured Mana Cake, 0, 0 -------- day 4 -------- name, sellIn, quality @@ -57,7 +57,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 11, 24 Backstage passes to a TAFKAL80ETC concert, 6, 50 Backstage passes to a TAFKAL80ETC concert, 1, 50 -Conjured Mana Cake, -1, 1 +Conjured Mana Cake, -1, 0 -------- day 5 -------- name, sellIn, quality diff --git a/python/tests/test_gilded_rose.py b/python/tests/test_gilded_rose.py index b1e6877e..f95c078a 100644 --- a/python/tests/test_gilded_rose.py +++ b/python/tests/test_gilded_rose.py @@ -12,6 +12,7 @@ from gilded_rose import GildedRose, Item AGED_BRIE = "Aged Brie" SULFURAS = "Sulfuras, Hand of Ragnaros" BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert" +CONJURED_CAKE = "Conjured Mana Cake" def update(name, sell_in, quality): @@ -81,12 +82,29 @@ class TestBackstagePasses: assert quality == 0 +class TestConjuredItem: + def test_quality_degrades_twice_as_fast_as_a_normal_item(self): + assert update(CONJURED_CAKE, 3, 6) == (2, 4) + + def test_quality_degrades_four_per_day_once_sell_by_date_has_passed(self): + assert update(CONJURED_CAKE, 0, 6) == (-1, 2) + + @pytest.mark.parametrize("sell_in, quality", [(3, 1), (0, 3), (-2, 0)]) + def test_quality_is_never_negative(self, sell_in, quality): + _, new_quality = update(CONJURED_CAKE, sell_in, quality) + assert new_quality == 0 + + def test_any_item_whose_name_starts_with_conjured_is_conjured(self): + assert update("Conjured +5 Dexterity Vest", 10, 20) == (9, 18) + + def test_all_items_in_the_inventory_are_updated(): items = [ Item("+5 Dexterity Vest", 10, 20), Item(AGED_BRIE, 2, 0), Item(SULFURAS, 0, 80), Item(BACKSTAGE_PASS, 5, 20), + Item(CONJURED_CAKE, 3, 6), ] GildedRose(items).update_quality() assert [(i.sell_in, i.quality) for i in items] == [ @@ -94,6 +112,7 @@ def test_all_items_in_the_inventory_are_updated(): (1, 1), (0, 80), (4, 23), + (2, 4), ]