mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
extracted all product types into their own methods and tested
This commit is contained in:
parent
c0ae6e9988
commit
e2673f0734
@ -7,27 +7,14 @@ class GildedRose
|
||||
def self.update_quality(items)
|
||||
items.map do |item|
|
||||
if !special_item?(item)
|
||||
update_normal_quality(item) if !sulfuras?(item)
|
||||
else
|
||||
update_normal_quality(item)
|
||||
item.sell_in -= 1
|
||||
elsif item.name.downcase.match /backstage/
|
||||
update_backstage_quality(item) if item.quality < 50
|
||||
end
|
||||
# ______________________________________________________________________________________
|
||||
# start of block that reduces sell in
|
||||
if !sulfuras?(item)
|
||||
item.sell_in = item.sell_in - 1
|
||||
end
|
||||
# end of block that reduces sell in
|
||||
|
||||
if item.sell_in < 0
|
||||
if item.name != "Aged Brie"
|
||||
if !item.name.downcase.match /backstage/
|
||||
update_normal_quality(item)
|
||||
else
|
||||
item.quality = 0
|
||||
end
|
||||
else
|
||||
item.quality = item.quality + 1 if item.quality < 50
|
||||
end
|
||||
item.sell_in -= 1
|
||||
elsif item.name.downcase.match /aged brie/
|
||||
update_brie_quality(item)
|
||||
item.sell_in -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -43,16 +30,25 @@ class GildedRose
|
||||
when 10..Float::INFINITY
|
||||
item.quality += 1
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
def self.update_normal_quality(item)
|
||||
item.quality -= 1 unless item.quality.zero?
|
||||
if item.sell_in < 0
|
||||
item.quality -= 2 unless item.quality.zero?
|
||||
else
|
||||
item.quality -= 1 unless item.quality.zero?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def self.update_brie_quality(item)
|
||||
if item.sell_in < 1 && item.quality < 48
|
||||
item.quality += 2
|
||||
else
|
||||
item.quality += 1 if item.quality < 50
|
||||
end
|
||||
end
|
||||
|
||||
def self.sulfuras?(item)
|
||||
!item.name.downcase.match( /sulfuras/).nil?
|
||||
end
|
||||
|
||||
@ -25,7 +25,7 @@ let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) }
|
||||
end
|
||||
|
||||
it 'should decrease quality of normal items by 2 when sell_in date passes' do
|
||||
items = [Item.new("old potato", 0, 20)]
|
||||
items = [Item.new("old potato", -1, 20)]
|
||||
GildedRose.update_quality(items)
|
||||
expect(items.first.quality).to eq (18)
|
||||
end
|
||||
@ -49,6 +49,11 @@ let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) }
|
||||
GildedRose.update_quality(items)
|
||||
expect(items.first.quality).to eq 50
|
||||
end
|
||||
it 'doubles the increase when sell_in below 1' do
|
||||
items = [Item.new("Aged Brie", 0, 20)]
|
||||
GildedRose.update_quality(items)
|
||||
expect(items.first.quality).to eq 22
|
||||
end
|
||||
end
|
||||
|
||||
describe 'Sulfuras input' do
|
||||
@ -88,7 +93,8 @@ let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) }
|
||||
end
|
||||
describe '#update_normal_quality' do
|
||||
it 'updates the quality of a normal item' do
|
||||
item_double = double :item, name: "potato", sellIn: 1, quality: 3
|
||||
item_double = double :item, name: "potato", sell_in: 1, quality: 3
|
||||
|
||||
expect(item_double).to receive(:quality=).with(2)
|
||||
GildedRose.update_normal_quality(item_double)
|
||||
end
|
||||
@ -163,6 +169,23 @@ let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) }
|
||||
end
|
||||
|
||||
describe '#update_brie_quality' do
|
||||
it 'should add one to the quality each day ' do
|
||||
brie_double = double :item, name: "Aged Brie", sell_in: 30, quality:15
|
||||
expect(brie_double).to receive(:quality=).with(16)
|
||||
GildedRose.update_brie_quality(brie_double)
|
||||
end
|
||||
|
||||
it 'should not let quality go past 50' do
|
||||
brie_double = double :item, name: "Aged Brie", sell_in: 30, quality:50
|
||||
expect(brie_double).not_to receive(:quality=).with(51)
|
||||
GildedRose.update_brie_quality(brie_double)
|
||||
end
|
||||
it 'should increase by two when sellin <= 0' do
|
||||
brie_double = double :item, name: "Aged Brie", sell_in: 0, quality:2
|
||||
expect(brie_double).to receive(:quality=).with(4)
|
||||
GildedRose.update_brie_quality(brie_double)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user