Write tests for all types of items

Without looking at the code I've written specs based on the requirements listed and the output of the texttest.
All specs are green except for the expected 2 failing 'quality' tests for the new 'Conjured' item.
With all models tested I feel more comfortable starting the refactor. The plan at the moment is to extract the code of 1 type of item at the time into it's own method.
This commit is contained in:
Maarten Parmentier 2023-06-26 22:47:38 +02:00
parent d0d6fdb93c
commit f0204a4b1e
3 changed files with 204 additions and 25 deletions

View File

@ -1,13 +0,0 @@
require File.join(File.dirname(__FILE__), 'gilded_rose')
describe GildedRose do
describe "#update_quality" do
it "does not change the name" do
items = [Item.new("foo", 0, 0)]
GildedRose.new(items).update_quality()
expect(items[0].name).to eq "fixme"
end
end
end

View File

@ -1,12 +0,0 @@
require File.join(File.dirname(__FILE__), 'gilded_rose')
require 'test/unit'
class TestUntitled < Test::Unit::TestCase
def test_foo
items = [Item.new("foo", 0, 0)]
GildedRose.new(items).update_quality()
assert_equal items[0].name, "fixme"
end
end

View File

@ -0,0 +1,204 @@
require_relative '../gilded_rose'
describe GildedRose do
let(:quality) { 3 }
let(:sell_in) { 3 }
let(:item_name) { 'Elixir of the Mongoose' }
let(:item) { Item.new(item_name, sell_in, quality) }
let(:items) { [item] }
subject { GildedRose.new(items) }
describe '#update_quality' do
context "when item is regular like e.g. 'Elixir of the Mongoose'" do
context 'when sell by date has not passed' do
it 'lowers quality by 1' do
subject.update_quality
expect(items[0].quality).to eq 2
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq 2
end
end
context 'when sell by date has passed' do
let(:sell_in) { 0 }
it 'lowers quality by 2' do
subject.update_quality
expect(items[0].quality).to eq 1
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq -1
end
end
it 'does never lower the quality below 0' do
items = [Item.new(item_name, sell_in, 0)]
subject.update_quality
expect(items[0].quality).to eq 0
end
end
context "when item name is 'Sulfuras, Hand of Ragnaros'" do
let(:item_name) { 'Sulfuras, Hand of Ragnaros' }
let(:quality) { 80 }
context 'when sell by date has not passed' do
it 'does not lower quality by 1' do
subject.update_quality
expect(items[0].quality).to eq 80
end
it 'does not lower sell_in' do
subject.update_quality
expect(items[0].sell_in).to eq 3
end
end
context 'when sell by date has passed' do
let(:sell_in) { 0 }
it 'does not lower quality by 1' do
subject.update_quality
expect(items[0].quality).to eq 80
end
it 'does not lower sell_in' do
subject.update_quality
expect(items[0].sell_in).to eq 0
end
end
end
context "when item name is 'Aged Brie'" do
let(:item_name) { 'Aged Brie' }
context 'when sell by date has not passed' do
it 'increases quality by 1' do
subject.update_quality
expect(items[0].quality).to eq 4
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq 2
end
end
context 'when sell by date has passed' do
let(:sell_in) { 0 }
it 'increases quality by 2' do
subject.update_quality
expect(items[0].quality).to eq 5
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq -1
end
end
it 'does never raise the quality above 50' do
items = [Item.new(item_name, sell_in, 50)]
subject.update_quality
expect(items[0].quality).to eq 50
end
end
context "when item name is 'Backstage passes to a TAFKAL80ETC concert'" do
let(:item_name) { 'Backstage passes to a TAFKAL80ETC concert' }
context 'when sell by date has not passed' do
context 'when 10 or less days remaining' do
let(:sell_in) { 10 }
it 'increases quality by 2' do
subject.update_quality
expect(items[0].quality).to eq 5
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq 9
end
end
context 'when 5 or less days remaining' do
let(:sell_in) { 5 }
it 'increases quality by 3' do
subject.update_quality
expect(items[0].quality).to eq 6
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq 4
end
end
end
context 'when sell by date has passed' do
let(:sell_in) { 0 }
it 'increases quality by 2' do
subject.update_quality
expect(items[0].quality).to eq 0
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq -1
end
end
it 'does never raise the quality above 50' do
items = [Item.new(item_name, sell_in, 50)]
subject.update_quality
expect(items[0].quality).to eq 50
end
end
xcontext "when item name is 'Conjured Mana Cake'" do
let(:item_name) { 'Conjured Mana Cake' }
context 'when sell by date has not passed' do
it 'lowers quality by 2' do
subject.update_quality
expect(items[0].quality).to eq 1
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq 2
end
end
context 'when sell by date has passed' do
let(:sell_in) { 0 }
let(:quality) { 6 }
it 'lowers quality by 4' do
subject.update_quality
expect(items[0].quality).to eq 2
end
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq -1
end
end
it 'does never lower the quality below 0' do
items = [Item.new(item_name, sell_in, 0)]
subject.update_quality
expect(items[0].quality).to eq 0
end
end
end
end