This commit is contained in:
Cyril LEPAGNOT 2017-09-26 08:10:01 +00:00 committed by GitHub
commit 1a6c9f6e59
5 changed files with 262 additions and 34 deletions

View File

@ -1,6 +1,6 @@
class GildedRose
def initialize(items)
def initialize(items:)
@items = items
end
@ -52,17 +52,3 @@ class GildedRose
end
end
end
class Item
attr_accessor :name, :sell_in, :quality
def initialize(name, sell_in, quality)
@name = name
@sell_in = sell_in
@quality = quality
end
def to_s()
"#{@name}, #{@sell_in}, #{@quality}"
end
end

View File

@ -1,13 +1,245 @@
require File.join(File.dirname(__FILE__), 'gilded_rose')
require File.join(File.dirname(__FILE__), 'item')
describe GildedRose do
RSpec.describe GildedRose do
subject { Item.new(name: name, sell_in: sell_in, quality: quality) }
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"
let(:items) { [subject] }
describe '#update_quality' do
let(:sell_in) { 0 }
let(:quality) { 0 }
before do
GildedRose
.new(items: items)
.update_quality
end
shared_examples :does_not_change_the_name do
describe '#items' do
describe '#first' do
describe '#name' do
it { expect(subject.name).to eq(name) }
end
end
end
end
context 'when #name is any_other' do
let(:name) { 'any_other' }
it_behaves_like :does_not_change_the_name
context 'when sell_in is negative' do
let(:sell_in) { -42 }
it { expect(subject.sell_in).to eq(-43) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(48) }
end
end
context 'when sell_in is 4' do
let(:sell_in) { 4 }
it { expect(subject.sell_in).to eq(3) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(49) }
end
end
context 'when sell_in is 8' do
let(:sell_in) { 8 }
it { expect(subject.sell_in).to eq(7) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(49) }
end
end
context 'when sell_in is 12' do
let(:sell_in) { 12 }
it { expect(subject.sell_in).to eq(11) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(49) }
end
end
end
context 'when #name is Aged Brie' do
let(:name) { 'Aged Brie' }
it_behaves_like :does_not_change_the_name
context 'when sell_in is negative' do
let(:sell_in) { -42 }
it { expect(subject.sell_in).to eq(-43) }
it { expect(subject.quality).to eq(2) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
context 'when sell_in is 4' do
let(:sell_in) { 4 }
it { expect(subject.sell_in).to eq(3) }
it { expect(subject.quality).to eq(1) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
context 'when sell_in is 8' do
let(:sell_in) { 8 }
it { expect(subject.sell_in).to eq(7) }
it { expect(subject.quality).to eq(1) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
context 'when sell_in is 12' do
let(:sell_in) { 12 }
it { expect(subject.sell_in).to eq(11) }
it { expect(subject.quality).to eq(1) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
end
context 'when #name is Sulfuras, Hand of Ragnaros' do
let(:name) { 'Sulfuras, Hand of Ragnaros' }
it_behaves_like :does_not_change_the_name
context 'when sell_in is negative' do
let(:sell_in) { -42 }
it { expect(subject.sell_in).to eq(-42) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
context 'when sell_in is 4' do
let(:sell_in) { 4 }
it { expect(subject.sell_in).to eq(4) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
context 'when sell_in is 8' do
let(:sell_in) { 8 }
it { expect(subject.sell_in).to eq(8) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
context 'when sell_in is 12' do
let(:sell_in) { 12 }
it { expect(subject.sell_in).to eq(12) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
end
context 'when #name is Backstage passes to a TAFKAL80ETC concert' do
let(:name) { 'Backstage passes to a TAFKAL80ETC concert' }
it_behaves_like :does_not_change_the_name
context 'when sell_in is negative' do
let(:sell_in) { -42 }
it { expect(subject.sell_in).to eq(-43) }
it { expect(subject.quality).to eq(0) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(0) }
end
end
context 'when sell_in is 4' do
let(:sell_in) { 4 }
it { expect(subject.sell_in).to eq(3) }
it { expect(subject.quality).to eq(3) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
context 'when sell_in is 8' do
let(:sell_in) { 8 }
it { expect(subject.sell_in).to eq(7) }
it { expect(subject.quality).to eq(2) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
context 'when sell_in is 12' do
let(:sell_in) { 12 }
it { expect(subject.sell_in).to eq(11) }
it { expect(subject.quality).to eq(1) }
context 'when quality is >= 50' do
let(:quality) { 50 }
it { expect(subject.quality).to eq(50) }
end
end
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

7
ruby/item.rb Normal file
View File

@ -0,0 +1,7 @@
require 'ostruct'
class Item < OpenStruct
def to_s
[name, sell_in, quality].join ', '
end
end

15
ruby/item_spec.rb Normal file
View File

@ -0,0 +1,15 @@
require File.join(File.dirname(__FILE__), 'item')
RSpec.describe Item do
let(:item) do
described_class.new(
name: 'a',
sell_in: 0,
quality: 0,
)
end
describe '#to_s' do
it { expect(item.to_s).to eq('a, 0, 0') }
end
end