found and fixed bug where quality could go above 50 and below 0 when change increment was greater than one

This commit is contained in:
ollie beney 2020-11-05 17:03:15 +00:00
parent 24ebe0b7dd
commit 75fa4869c0
2 changed files with 46 additions and 7 deletions

View File

@ -12,6 +12,8 @@ class GildedRose
when brie?(item) when brie?(item)
update_brie_quality(item) update_brie_quality(item)
item.sell_in -= 1 item.sell_in -= 1
when conjured?(item)
2.times {update_normal_quality(item)}
end end
end end
end end
@ -21,9 +23,9 @@ class GildedRose
when (-(Float::INFINITY)..0) when (-(Float::INFINITY)..0)
item.quality = 0 item.quality = 0
when 0..5 when 0..5
item.quality += 3 3.times { item.quality += 1 if item.quality < 50 }
when 6..10 when 6..10
item.quality += 2 2.times { item.quality += 1 if item.quality < 50 }
when 10..Float::INFINITY when 10..Float::INFINITY
item.quality += 1 item.quality += 1
end end
@ -31,7 +33,7 @@ class GildedRose
def self.update_normal_quality(item) def self.update_normal_quality(item)
if item.sell_in < 0 if item.sell_in < 0
item.quality -= 2 unless item.quality.zero? 2.times { item.quality -= 1 unless item.quality.zero?}
else else
item.quality -= 1 unless item.quality.zero? item.quality -= 1 unless item.quality.zero?
end end
@ -57,8 +59,12 @@ class GildedRose
!item.name.downcase.match(/backstage/).nil? !item.name.downcase.match(/backstage/).nil?
end end
def self.conjured?(item)
!item.name.downcase.match(/conjured/).nil?
end
def self.special_item?(item) def self.special_item?(item)
( brie?(item) || backstage?(item) || sulfuras?(item)) ( brie?(item) || backstage?(item) || conjured?(item) || sulfuras?(item))
end end

View File

@ -36,6 +36,8 @@ let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) }
end end
end end
describe 'Aged Brie input' do describe 'Aged Brie input' do
it 'increases in quality as it ages' do it 'increases in quality as it ages' do
items = [Item.new("Aged Brie", 25, 45)] items = [Item.new("Aged Brie", 25, 45)]
@ -89,7 +91,37 @@ let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) }
expect(items.first.quality).to eq 0 expect(items.first.quality).to eq 0
end end
end end
end
describe 'conjured input' do
it "should degrade twice as fast as normal items" do
items = [Item.new("Conjured Armour", 10, 15)]
GildedRose.update_quality(items)
expect(items.first.quality).to eq 13
end
it "should not degrade past 0" do
items = [Item.new("Conjured Armour", 10, 1)]
GildedRose.update_quality(items)
expect(items.first.quality).to eq 0
end
it "should degrade by 4 when sell in has passed" do
items = [Item.new("Conjured Armour", -1, 17)]
GildedRose.update_quality(items)
expect(items.first.quality).to eq 13
end
it "still should not degrade past 0 when sell in has passed" do
items = [Item.new("Conjured Armour", -1, 3)]
GildedRose.update_quality(items)
expect(items.first.quality).to eq 0
end
end
end
describe '#update_normal_quality' do describe '#update_normal_quality' do
it 'updates the quality of a normal item' do it 'updates the quality of a normal item' do
item_double = double :item, name: "potato", sell_in: 1, quality: 3 item_double = double :item, name: "potato", sell_in: 1, quality: 3
@ -149,14 +181,14 @@ let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) }
it 'increases in quality by 2 when sellin < 10 days' do it 'increases in quality by 2 when sellin < 10 days' do
items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 9, quality: 20 items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 9, quality: 20
expect(items_double).to receive(:quality=).with(22) expect(items_double).to receive(:quality=).with(21).twice
GildedRose.update_backstage_quality(items_double) GildedRose.update_backstage_quality(items_double)
end end
it 'increases by 3 when sellin < 5' do it 'increases by 3 when sellin < 5' do
items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 4, quality: 20 items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 4, quality: 20
expect(items_double).to receive(:quality=).with(23) expect(items_double).to receive(:quality=).with(21).exactly(3).times
GildedRose.update_backstage_quality(items_double) GildedRose.update_backstage_quality(items_double)
end end
@ -215,4 +247,5 @@ let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) }
end end