Created
April 18, 2016 16:14
-
-
Save JoshCheek/8b5759dd0fb00a8db947a88d16dc4de4 to your computer and use it in GitHub Desktop.
Gilded Rose (based on https://github.com/jimweirich/gilded_rose_kata)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_quality(items) | |
items.each do |item| | |
if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert' | |
if item.quality > 0 | |
if item.name != 'Sulfuras, Hand of Ragnaros' | |
item.quality -= 1 | |
end | |
end | |
else | |
if item.quality < 50 | |
item.quality += 1 | |
if item.name == 'Backstage passes to a TAFKAL80ETC concert' | |
if item.sell_in < 11 | |
if item.quality < 50 | |
item.quality += 1 | |
end | |
end | |
if item.sell_in < 6 | |
if item.quality < 50 | |
item.quality += 1 | |
end | |
end | |
end | |
end | |
end | |
if item.name != 'Sulfuras, Hand of Ragnaros' | |
item.sell_in -= 1 | |
end | |
if item.sell_in < 0 | |
if item.name != "Aged Brie" | |
if item.name != 'Backstage passes to a TAFKAL80ETC concert' | |
if item.quality > 0 | |
if item.name != 'Sulfuras, Hand of Ragnaros' | |
item.quality -= 1 | |
end | |
end | |
else | |
item.quality = item.quality - item.quality | |
end | |
else | |
if item.quality < 50 | |
item.quality += 1 | |
end | |
end | |
end | |
end | |
end | |
# DO NOT CHANGE THINGS BELOW ----------------------------------------- | |
Item = Struct.new(:name, :sell_in, :quality) | |
# We use the setup in the spec rather than the following for testing. | |
# | |
# Items = [ | |
# Item.new("+5 Dexterity Vest", 10, 20), | |
# Item.new("Aged Brie", 2, 0), | |
# Item.new("Elixir of the Mongoose", 5, 7), | |
# Item.new("Sulfuras, Hand of Ragnaros", 0, 80), | |
# Item.new("Backstage passes to a TAFKAL80ETC concert", 15, 20), | |
# Item.new("Conjured Mana Cake", 3, 6), | |
# ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_quality(items) | |
items.each do |item| | |
update_item item | |
end | |
end | |
def update_item(item) | |
if item.name == 'Aged Brie' | |
item.sell_in -= 1 | |
if item.sell_in < 0 | |
item.quality += 2 | |
else | |
item.quality += 1 | |
end | |
item.quality = 50 if 50 < item.quality | |
item.quality = 0 if item.quality < 0 | |
elsif item.name == 'Backstage passes to a TAFKAL80ETC concert' | |
item.sell_in -= 1 | |
item.quality += 1 | |
if item.sell_in < 5 | |
item.quality += 2 | |
elsif item.sell_in < 10 | |
item.quality += 1 | |
end | |
item.quality = 50 if 50 < item.quality | |
item.quality = 0 if item.sell_in < 0 | |
elsif item.name == 'Sulfuras, Hand of Ragnaros' | |
elsif item.name == "NORMAL ITEM" | |
item.sell_in -= 1 | |
if item.sell_in < 0 | |
item.quality -= 2 | |
else | |
item.quality -= 1 | |
end | |
item.quality = 0 if item.quality < 0 | |
elsif item.name == "Conjured Mana Cake" | |
item.sell_in -= 1 | |
if 0 < item.sell_in | |
item.quality -= 2 | |
else | |
item.quality -= 4 | |
end | |
item.quality = 0 if item.quality < 0 | |
else | |
raise item.name | |
end | |
end | |
# DO NOT CHANGE THINGS BELOW ----------------------------------------- | |
Item = Struct.new(:name, :sell_in, :quality) | |
# We use the setup in the spec rather than the following for testing. | |
# | |
# Items = [ | |
# Item.new("+5 Dexterity Vest", 10, 20), | |
# Item.new("Aged Brie", 2, 0), | |
# Item.new("Elixir of the Mongoose", 5, 7), | |
# Item.new("Sulfuras, Hand of Ragnaros", 0, 80), | |
# Item.new("Backstage passes to a TAFKAL80ETC concert", 15, 20), | |
# Item.new("Conjured Mana Cake", 3, 6), | |
# ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_quality(items) | |
items.each { |item| AgeItem.call item } | |
end | |
class AgeItem | |
@agers = {} | |
def self.agers | |
@agers | |
end | |
def self.call(item) | |
agers[item.name].new(item).age | |
end | |
attr_reader :item | |
def initialize(item) | |
@item = item | |
end | |
def delta_sell_in | |
-1 | |
end | |
def delta_quality | |
raise 'implement me' | |
end | |
def max_quality | |
50 | |
end | |
def min_quality | |
0 | |
end | |
def age | |
item.sell_in += delta_sell_in | |
item.quality += delta_quality | |
item.quality = max_quality if max_quality < item.quality | |
item.quality = min_quality if item.quality < min_quality | |
end | |
end | |
class AgeItem::AgedBrie < AgeItem | |
def delta_quality | |
item.sell_in < 0 ? 2 : 1 | |
end | |
end | |
class AgeItem::BackstagePass < AgeItem | |
def delta_quality | |
return -item.quality if item.sell_in < 0 | |
return 3 if item.sell_in < 5 | |
return 2 if item.sell_in < 10 | |
return 1 | |
end | |
end | |
class AgeItem::Sulfuras < AgeItem | |
def delta_sell_in | |
0 | |
end | |
def delta_quality | |
0 | |
end | |
def max_quality | |
item.quality | |
end | |
def min_quality | |
item.quality | |
end | |
end | |
class AgeItem::Normal < AgeItem | |
def delta_quality | |
item.sell_in < 0 ? -2 : -1 | |
end | |
end | |
class AgeItem::ConjuredMana < AgeItem | |
def delta_quality | |
0 < item.sell_in ? -2 : -4 | |
end | |
end | |
class AgeItem | |
agers['Aged Brie'] = AgedBrie | |
agers['Backstage passes to a TAFKAL80ETC concert'] = BackstagePass | |
agers['Sulfuras, Hand of Ragnaros'] = Sulfuras | |
agers['NORMAL ITEM'] = Normal | |
agers['Conjured Mana Cake'] = ConjuredMana | |
end | |
# DO NOT CHANGE THINGS BELOW ----------------------------------------- | |
Item = Struct.new(:name, :sell_in, :quality) | |
# We use the setup in the spec rather than the following for testing. | |
# | |
# Items = [ | |
# Item.new("+5 Dexterity Vest", 10, 20), | |
# Item.new("Aged Brie", 2, 0), | |
# Item.new("Elixir of the Mongoose", 5, 7), | |
# Item.new("Sulfuras, Hand of Ragnaros", 0, 80), | |
# Item.new("Backstage passes to a TAFKAL80ETC concert", 15, 20), | |
# Item.new("Conjured Mana Cake", 3, 6), | |
# ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec/given' | |
require_relative 'gilded_rose' | |
RSpec.configure do |config| | |
config.filter_gems_from_backtrace 'given_core' | |
end | |
RSpec.describe "#update_quality" do | |
context "with a single" do | |
Given(:initial_sell_in) { 5 } | |
Given(:initial_quality) { 10 } | |
Given(:item) { Item.new(name, initial_sell_in, initial_quality) } | |
When { update_quality([item]) } | |
context "normal item" do | |
Given(:name) { "NORMAL ITEM" } | |
Invariant { expect(item.sell_in).to eq initial_sell_in-1 } | |
context "before sell date" do | |
Then { expect(item.quality).to eq initial_quality-1 } | |
end | |
context "on sell date" do | |
Given(:initial_sell_in) { 0 } | |
Then { expect(item.quality).to eq initial_quality-2 } | |
end | |
context "after sell date" do | |
Given(:initial_sell_in) { -10 } | |
Then { expect(item.quality).to eq initial_quality-2 } | |
end | |
context "of zero quality" do | |
Given(:initial_quality) { 0 } | |
Then { expect(item.quality).to eq 0 } | |
end | |
end | |
context "Aged Brie" do | |
Given(:name) { "Aged Brie" } | |
Invariant { expect(item.sell_in).to eq initial_sell_in-1 } | |
context "before sell date" do | |
Then { expect(item.quality).to eq initial_quality+1 } | |
context "with max quality" do | |
Given(:initial_quality) { 50 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "on sell date" do | |
Given(:initial_sell_in) { 0 } | |
Then { expect(item.quality).to eq initial_quality+2 } | |
context "near max quality" do | |
Given(:initial_quality) { 49 } | |
Then { expect(item.quality).to eq 50 } | |
end | |
context "with max quality" do | |
Given(:initial_quality) { 50 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "after sell date" do | |
Given(:initial_sell_in) { -10 } | |
Then { expect(item.quality).to eq initial_quality+2 } | |
context "with max quality" do | |
Given(:initial_quality) { 50 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
end | |
context "Sulfuras" do | |
Given(:initial_quality) { 80 } | |
Given(:name) { "Sulfuras, Hand of Ragnaros" } | |
Invariant { expect(item.sell_in).to eq initial_sell_in } | |
context "before sell date" do | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
context "on sell date" do | |
Given(:initial_sell_in) { 0 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
context "after sell date" do | |
Given(:initial_sell_in) { -10 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "Backstage pass" do | |
Given(:name) { "Backstage passes to a TAFKAL80ETC concert" } | |
Invariant { expect(item.sell_in).to eq initial_sell_in-1 } | |
context "long before sell date" do | |
Given(:initial_sell_in) { 11 } | |
Then { expect(item.quality).to eq initial_quality+1 } | |
context "at max quality" do | |
Given(:initial_quality) { 50 } | |
end | |
end | |
context "medium close to sell date (upper bound)" do | |
Given(:initial_sell_in) { 10 } | |
Then { expect(item.quality).to eq initial_quality+2 } | |
context "at max quality" do | |
Given(:initial_quality) { 50 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "medium close to sell date (lower bound)" do | |
Given(:initial_sell_in) { 6 } | |
Then { expect(item.quality).to eq initial_quality+2 } | |
context "at max quality" do | |
Given(:initial_quality) { 50 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "very close to sell date (upper bound)" do | |
Given(:initial_sell_in) { 5 } | |
Then { expect(item.quality).to eq initial_quality+3 } | |
context "at max quality" do | |
Given(:initial_quality) { 50 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "very close to sell date (lower bound)" do | |
Given(:initial_sell_in) { 1 } | |
Then { expect(item.quality).to eq initial_quality+3 } | |
context "at max quality" do | |
Given(:initial_quality) { 50 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "on sell date" do | |
Given(:initial_sell_in) { 0 } | |
Then { expect(item.quality).to eq 0 } | |
end | |
context "after sell date" do | |
Given(:initial_sell_in) { -10 } | |
Then { expect(item.quality).to eq 0 } | |
end | |
end | |
context "conjured item" do | |
Given(:name) { "Conjured Mana Cake" } | |
Invariant { expect(item.sell_in).to eq initial_sell_in-1 } | |
context "before the sell date" do | |
Given(:initial_sell_in) { 5 } | |
Then { expect(item.quality).to eq initial_quality-2 } | |
context "at zero quality" do | |
Given(:initial_quality) { 0 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "on sell date" do | |
Given(:initial_sell_in) { 0 } | |
Then { expect(item.quality).to eq initial_quality-4 } | |
context "at zero quality" do | |
Given(:initial_quality) { 0 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
context "after sell date" do | |
Given(:initial_sell_in) { -10 } | |
Then { expect(item.quality).to eq initial_quality-4 } | |
context "at zero quality" do | |
Given(:initial_quality) { 0 } | |
Then { expect(item.quality).to eq initial_quality } | |
end | |
end | |
end | |
end | |
context "with several objects" do | |
Given(:items) { | |
[ | |
Item.new("NORMAL ITEM", 5, 10), | |
Item.new("Aged Brie", 3, 10), | |
] | |
} | |
When { update_quality(items) } | |
Then { expect(items[0].quality).to eq 9 } | |
Then { expect(items[0].sell_in).to eq 4 } | |
Then { expect(items[1].quality).to eq 11 } | |
Then { expect(items[1].sell_in).to eq 2 } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment