Skip to content

Instantly share code, notes, and snippets.

@ltrainpr
Last active December 29, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltrainpr/7669775 to your computer and use it in GitHub Desktop.
Save ltrainpr/7669775 to your computer and use it in GitHub Desktop.
Gilded Rose Kata
class Brie
def initialize(args)
@name = args[:name]
@quality = args[:quality]
@sellin = args[:sell_in]
end
def day_passes
brie_quality_increase
end
def sellin_decrease
@sellin -= 1
end
def above_min_quality
@quality > min_quality
end
def below_max_quality
@quality < max_quality
end
def min_quality
0
end
def max_quality
50
end
def min_sellin
0
end
def brie_quality_increase
if below_max_quality
@quality += 1
else
@quality
end
end
def quality_decrease
@quality -= 1
end
def negative_sellin
brie_quality_increase if below_max_quality
@quality
end
end
class ConcertPasses
def initialize(args)
@name = args[:name]
@quality = args[:quality]
@sellin = args[:sell_in]
end
def day_passes
concert_pass_quality_increase
end
def sellin_decrease
@sellin -= 1
end
def above_min_quality
@quality > min_quality
end
def below_max_quality
@quality < max_quality
end
def min_quality
0
end
def max_quality
50
end
def min_sellin
0
end
def quality_increase
@quality += 1
end
def quality_decrease
@quality -= 1
end
def concert_passes
@name == 'Backstage passes to a TAFKAL80ETC concert'
end
def negative_sellin
@quality -= @quality
end
def concert_pass_quality_increase
if @sellin < 6
quality_increase
quality_increase
elsif @sellin < 11
quality_increase
end
quality_increase if below_max_quality
@quality
end
end
def update_quality(items)
items.each do |item|
@name = item.name
@quality = item.quality
@sellin = item.sell_in
@item = strategy_delegator(item)
@quality = @item.day_passes if below_max_quality
@sellin = @item.sellin_decrease unless sulfuras
@quality = @item.negative_sellin if @sellin < min_sellin
item.quality = @quality
item.sell_in = @sellin
end
end
def above_min_quality
@quality > min_quality
end
def below_max_quality
@quality < max_quality
end
def min_quality
0
end
def max_quality
50
end
def min_sellin
0
end
def sulfuras
@name == 'Sulfuras, Hand of Ragnaros'
end
def brie
@name == 'Aged Brie'
end
def concert_passes
@name == 'Backstage passes to a TAFKAL80ETC concert'
end
def normal_item
!brie && !concert_passes && !sulfuras
end
def strategy_delegator(item)
case
when brie then Brie.new(item)
when concert_passes then ConcertPasses.new(item)
when sulfuras then Sulfuras.new(item)
else
NormalItem.new(item)
end
end
# DO NOT CHANGE THINGS BELOW -----------------------------------------
Item = Struct.new(:name, :sell_in, :quality)
require_relative 'normal_item'
require_relative 'brie'
require_relative 'concert_passes'
require_relative 'sulfuras'
# 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),
# ]
require 'rspec/given'
require 'gilded_rose'
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 { item.sell_in.should == initial_sell_in-1 }
context "before sell date" do
Then { item.quality.should == initial_quality-1 }
end
context "on sell date" do
Given(:initial_sell_in) { 0 }
Then { item.quality.should == initial_quality-2 }
end
context "after sell date" do
Given(:initial_sell_in) { -10 }
Then { item.quality.should == initial_quality-2 }
end
context "of zero quality" do
Given(:initial_quality) { 0 }
Then { item.quality.should == 0 }
end
end
context "Aged Brie" do
Given(:name) { "Aged Brie" }
Invariant { item.sell_in.should == initial_sell_in-1 }
context "before sell date" do
Then { item.quality.should == initial_quality+1 }
context "with max quality" do
Given(:initial_quality) { 50 }
Then { item.quality.should == initial_quality }
end
end
context "on sell date" do
Given(:initial_sell_in) { 0 }
Then { item.quality.should == initial_quality+2 }
context "near max quality" do
Given(:initial_quality) { 49 }
Then { item.quality.should == 50 }
end
context "with max quality" do
Given(:initial_quality) { 50 }
Then { item.quality.should == initial_quality }
end
end
context "after sell date" do
Given(:initial_sell_in) { -10 }
Then { item.quality.should == initial_quality+2 }
context "with max quality" do
Given(:initial_quality) { 50 }
Then { item.quality.should == initial_quality }
end
end
end
context "Sulfuras" do
Given(:initial_quality) { 80 }
Given(:name) { "Sulfuras, Hand of Ragnaros" }
Invariant { item.sell_in.should == initial_sell_in }
context "before sell date" do
Then { item.quality.should == initial_quality }
end
context "on sell date" do
Given(:initial_sell_in) { 0 }
Then { item.quality.should == initial_quality }
end
context "after sell date" do
Given(:initial_sell_in) { -10 }
Then { item.quality.should == initial_quality }
end
end
context "Backstage pass" do
Given(:name) { "Backstage passes to a TAFKAL80ETC concert" }
Invariant { item.sell_in.should == initial_sell_in-1 }
context "long before sell date" do
Given(:initial_sell_in) { 11 }
Then { item.quality.should == 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 { item.quality.should == initial_quality+2 }
context "at max quality" do
Given(:initial_quality) { 50 }
Then { item.quality.should == initial_quality }
end
end
context "medium close to sell date (lower bound)" do
Given(:initial_sell_in) { 6 }
Then { item.quality.should == initial_quality+2 }
context "at max quality" do
Given(:initial_quality) { 50 }
Then { item.quality.should == initial_quality }
end
end
context "very close to sell date (upper bound)" do
Given(:initial_sell_in) { 5 }
Then { item.quality.should == initial_quality+3 }
context "at max quality" do
Given(:initial_quality) { 50 }
Then { item.quality.should == initial_quality }
end
end
context "very close to sell date (lower bound)" do
Given(:initial_sell_in) { 1 }
Then { item.quality.should == initial_quality+3 }
context "at max quality" do
Given(:initial_quality) { 50 }
Then { item.quality.should == initial_quality }
end
end
context "on sell date" do
Given(:initial_sell_in) { 0 }
Then { item.quality.should == 0 }
end
context "after sell date" do
Given(:initial_sell_in) { -10 }
Then { item.quality.should == 0 }
end
end
context "conjured item" do
before { pending }
Given(:name) { "Conjured Mana Cake" }
Invariant { item.sell_in.should == initial_sell_in-1 }
context "before the sell date" do
Given(:initial_sell_in) { 5 }
Then { item.quality.should == initial_quality-2 }
context "at zero quality" do
Given(:initial_quality) { 0 }
Then { item.quality.should == initial_quality }
end
end
context "on sell date" do
Given(:initial_sell_in) { 0 }
Then { item.quality.should == initial_quality-4 }
context "at zero quality" do
Given(:initial_quality) { 0 }
Then { item.quality.should == initial_quality }
end
end
context "after sell date" do
Given(:initial_sell_in) { -10 }
Then { item.quality.should == initial_quality-4 }
context "at zero quality" do
Given(:initial_quality) { 0 }
Then { item.quality.should == 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 { items[0].quality.should == 9 }
Then { items[0].sell_in.should == 4 }
Then { items[1].quality.should == 11 }
Then { items[1].sell_in.should == 2 }
end
end
class NormalItem
def initialize(args)
@name = args[:name]
@quality = args[:quality]
@sellin = args[:sell_in]
end
def day_passes
update_normal_item_quality
end
def sellin_decrease
@sellin -= 1
end
def update_normal_item_quality
if above_min_quality
quality_decrease
else
min_quality
end
end
def above_min_quality
@quality > min_quality
end
def below_max_quality
@quality < max_quality
end
def min_quality
0
end
def max_quality
50
end
def min_sellin
0
end
def quality_increase
@quality += 1
end
def quality_decrease
@quality -= 1
end
def negative_sellin
quality_decrease if above_min_quality
@quality
end
end
class Sulfuras
def initialize(args)
@name = args[:name]
@quality = args[:quality]
@sellin = args[:sell_in]
end
def day_passes
@quality
end
def negative_sellin
@quality
end
def sellin_decrease
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment