mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
Restructure GildedRose Project
This commit is contained in:
parent
7767e6a6f6
commit
dc9e8be57d
@ -1,93 +0,0 @@
|
||||
class GildedRose
|
||||
|
||||
def initialize(items)
|
||||
@items = items
|
||||
end
|
||||
|
||||
def update_quality()
|
||||
@items.each do |item|
|
||||
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.quality > 0
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.quality = item.quality - 1
|
||||
end
|
||||
end
|
||||
else
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
if item.name == "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.sell_in < 11
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
if item.sell_in < 6
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.sell_in = 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 = item.quality - 1
|
||||
end
|
||||
end
|
||||
else
|
||||
item.quality = item.quality - item.quality
|
||||
end
|
||||
else
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ItemFactory
|
||||
SPECIAL_ITEMS = ["Aged Brie", "Backstage passes to a TAFKAL80ETC concert", "Sulfuras, Hand of Ragnaros"]
|
||||
def self.create_item(name:, sell_in:, quality:)
|
||||
if SPECIAL_ITEMS.include?(name)
|
||||
return Item.new(name, sell_in, quality)
|
||||
end
|
||||
|
||||
GenericItem.new(name, sell_in, quality)
|
||||
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
|
||||
|
||||
class GenericItem < Item
|
||||
def update_quality
|
||||
if @quality > 0
|
||||
@quality -= 1
|
||||
end
|
||||
end
|
||||
|
||||
def update_sell_in
|
||||
@sell_in -= 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -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
|
||||
@ -1,125 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__), 'gilded_rose')
|
||||
require 'test/unit'
|
||||
|
||||
class TestUntitled < Test::Unit::TestCase
|
||||
MAX_QUALITY = 50
|
||||
|
||||
test "Once the sell by date has passed, quality degrades twice as fast" do
|
||||
initial_expired_item_quality = 2
|
||||
initial_unexpired_item_quality = 2
|
||||
|
||||
expired_item = Item.new(name = "TestItem", sell_in = 0, quality = initial_expired_item_quality)
|
||||
unexpired_item = Item.new(name = "TestItem", sell_in = 2, quality = initial_unexpired_item_quality)
|
||||
|
||||
items = [expired_item, unexpired_item]
|
||||
GildedRose.new(items).update_quality
|
||||
|
||||
assert_equal initial_expired_item_quality - 2, expired_item.quality
|
||||
assert_equal initial_unexpired_item_quality - 1, unexpired_item.quality
|
||||
end
|
||||
|
||||
test "Once the sell by date has passed, quality degrades but it does not drop below zero" do
|
||||
expired_item = Item.new(name = "TestItem", sell_in = 0, quality = 0)
|
||||
|
||||
gilded_rose = GildedRose.new([expired_item])
|
||||
|
||||
gilded_rose.update_quality
|
||||
assert_equal 0, expired_item.quality
|
||||
end
|
||||
|
||||
test "After every update_quality call, sell_in is reduced by 1" do
|
||||
number_of_updates = 10
|
||||
initial_item_sell_in = 4
|
||||
|
||||
item = Item.new(name = "TestItem", sell_in = initial_item_sell_in, quality = 0)
|
||||
gilded_rose = GildedRose.new([item])
|
||||
number_of_updates.times { gilded_rose.update_quality }
|
||||
|
||||
expected_item_sell_in = initial_item_sell_in - number_of_updates
|
||||
assert_equal expected_item_sell_in, item.sell_in
|
||||
end
|
||||
|
||||
test "Aged Brie item increases in Quality the older it gets" do
|
||||
number_of_updates = 10
|
||||
initial_item_sell_in = 10
|
||||
initial_item_quality = 0
|
||||
|
||||
item = Item.new(name = "Aged Brie", sell_in = initial_item_sell_in, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = GildedRose.new([item])
|
||||
number_of_updates.times { gilded_rose.update_quality }
|
||||
|
||||
expected_item_quality = initial_item_quality + number_of_updates
|
||||
assert_equal expected_item_quality, item.quality
|
||||
end
|
||||
|
||||
test "Aged Brie item increases in Quality the older it gets, but it does not exceed 50 in quality" do
|
||||
initial_item_sell_in = 10
|
||||
initial_item_quality = 0
|
||||
|
||||
item = Item.new(name = "Aged Brie", sell_in = initial_item_sell_in, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = GildedRose.new([item])
|
||||
(MAX_QUALITY + 1).times { gilded_rose.update_quality }
|
||||
|
||||
assert_equal MAX_QUALITY, item.quality
|
||||
end
|
||||
|
||||
test "Sulfuras sell by date does not decrease after every update_quality" do
|
||||
number_of_updates = 20
|
||||
initial_item_sell_in = 10
|
||||
|
||||
item = Item.new(name = "Sulfuras, Hand of Ragnaros", sell_in = initial_item_sell_in, quality = 10)
|
||||
|
||||
gilded_rose = GildedRose.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
assert_equal initial_item_sell_in, item.sell_in
|
||||
end
|
||||
|
||||
test "Sulfuras quality does not decrease after each update_quality" do
|
||||
initial_item_quality = 10
|
||||
|
||||
item = Item.new(name = "Sulfuras, Hand of Ragnaros", sell_in = 10, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = GildedRose.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
assert_equal initial_item_quality, item.quality
|
||||
end
|
||||
|
||||
test "Backstage passes quality increases by 2 when the item has 10 days or less to expire" do
|
||||
initial_item_quality = 10
|
||||
|
||||
item = Item.new(name = "Backstage passes to a TAFKAL80ETC concert", sell_in = 10, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = GildedRose.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
expected_item_quality = initial_item_quality + 2
|
||||
assert_equal expected_item_quality, item.quality
|
||||
end
|
||||
|
||||
test "Backstage passes quality increases by 3 when the item has 5 days or less to expire" do
|
||||
initial_item_quality = 10
|
||||
|
||||
item = Item.new(name = "Backstage passes to a TAFKAL80ETC concert", sell_in = 5, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = GildedRose.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
expected_item_quality = initial_item_quality + 3
|
||||
assert_equal expected_item_quality, item.quality
|
||||
end
|
||||
|
||||
test "Backstage passes quality drops to 0 when the item is expired" do
|
||||
initial_expired_item_quality = 10
|
||||
initial_expired_item_sell_in = 0
|
||||
item = Item.new(name = "Backstage passes to a TAFKAL80ETC concert", sell_in = initial_expired_item_sell_in, quality = initial_expired_item_quality)
|
||||
|
||||
gilded_rose = GildedRose.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
assert_equal 0, item.quality
|
||||
end
|
||||
end
|
||||
@ -1,10 +0,0 @@
|
||||
class ItemFactory
|
||||
SPECIAL_ITEMS = ["Aged Brie", "Backstage passes to a TAFKAL80ETC concert", "Sulfuras, Hand of Ragnaros"]
|
||||
def self.create_item(name:, sell_in:, quality:)
|
||||
if SPECIAL_ITEMS.include?(name)
|
||||
return Item.new(name, sell_in, quality)
|
||||
end
|
||||
|
||||
GenericItem.new(name, sell_in, quality)
|
||||
end
|
||||
end
|
||||
@ -1,16 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__), 'gilded_rose')
|
||||
require 'test/unit'
|
||||
|
||||
class ItemFactoryTests < Test::Unit::TestCase
|
||||
test ".create_item returns GenericItem instance if it is not a special item" do
|
||||
created_item = ItemFactory.create_item(name: "random_item", sell_in: 0, quality: 0)
|
||||
|
||||
assert_instance_of GenericItem, created_item
|
||||
end
|
||||
|
||||
test ".create_item returns Item instance if Aged Brie item if given" do
|
||||
created_item = ItemFactory.create_item(name: "Aged Brie", sell_in: 0, quality: 0)
|
||||
|
||||
assert_instance_of Item, created_item
|
||||
end
|
||||
end
|
||||
5
ruby/lib/gilded_rose.rb
Normal file
5
ruby/lib/gilded_rose.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require File.join(File.dirname(__FILE__), "gilded_rose/store" )
|
||||
require File.join(File.dirname(__FILE__), "gilded_rose/item_factory" )
|
||||
|
||||
module GildedRose
|
||||
end
|
||||
13
ruby/lib/gilded_rose/item_factory.rb
Normal file
13
ruby/lib/gilded_rose/item_factory.rb
Normal file
@ -0,0 +1,13 @@
|
||||
module GildedRose
|
||||
class ItemFactory
|
||||
SPECIAL_ITEMS = ["Aged Brie", "Backstage passes to a TAFKAL80ETC concert", "Sulfuras, Hand of Ragnaros"]
|
||||
def self.create_item(name:, sell_in:, quality:)
|
||||
if SPECIAL_ITEMS.include?(name)
|
||||
return Item.new(name, sell_in, quality)
|
||||
end
|
||||
|
||||
GenericItem.new(name, sell_in, quality)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
83
ruby/lib/gilded_rose/store.rb
Normal file
83
ruby/lib/gilded_rose/store.rb
Normal file
@ -0,0 +1,83 @@
|
||||
module GildedRose
|
||||
class Store
|
||||
|
||||
def initialize(items)
|
||||
@items = items
|
||||
end
|
||||
|
||||
def update_quality()
|
||||
@items.each do |item|
|
||||
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.quality > 0
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.quality = item.quality - 1
|
||||
end
|
||||
end
|
||||
else
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
if item.name == "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.sell_in < 11
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
if item.sell_in < 6
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.sell_in = 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 = item.quality - 1
|
||||
end
|
||||
end
|
||||
else
|
||||
item.quality = item.quality - item.quality
|
||||
end
|
||||
else
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
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
|
||||
|
||||
class GenericItem < Item
|
||||
def update_quality
|
||||
if @quality > 0
|
||||
@quality -= 1
|
||||
end
|
||||
end
|
||||
|
||||
def update_sell_in
|
||||
@sell_in -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
19
ruby/tests/gilded_rose/item_factory_tests.rb
Normal file
19
ruby/tests/gilded_rose/item_factory_tests.rb
Normal file
@ -0,0 +1,19 @@
|
||||
require_relative '../../lib/gilded_rose'
|
||||
require 'test/unit'
|
||||
|
||||
module GildedRose
|
||||
class ItemFactoryTests < Test::Unit::TestCase
|
||||
test ".create_item returns GenericItem instance if it is not a special item" do
|
||||
created_item = ItemFactory.create_item(name: "random_item", sell_in: 0, quality: 0)
|
||||
|
||||
assert_instance_of GenericItem, created_item
|
||||
end
|
||||
|
||||
test ".create_item returns Item instance if Aged Brie item if given" do
|
||||
created_item = ItemFactory.create_item(name: "Aged Brie", sell_in: 0, quality: 0)
|
||||
|
||||
assert_instance_of Item, created_item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
128
ruby/tests/gilded_rose/store_tests.rb
Normal file
128
ruby/tests/gilded_rose/store_tests.rb
Normal file
@ -0,0 +1,128 @@
|
||||
require_relative '../../lib/gilded_rose'
|
||||
require 'test/unit'
|
||||
|
||||
module GildedRose
|
||||
class StoreTests < Test::Unit::TestCase
|
||||
MAX_QUALITY = 50
|
||||
|
||||
test "Once the sell by date has passed, quality degrades twice as fast" do
|
||||
initial_expired_item_quality = 2
|
||||
initial_unexpired_item_quality = 2
|
||||
|
||||
expired_item = Item.new(name = "TestItem", sell_in = 0, quality = initial_expired_item_quality)
|
||||
unexpired_item = Item.new(name = "TestItem", sell_in = 2, quality = initial_unexpired_item_quality)
|
||||
|
||||
items = [expired_item, unexpired_item]
|
||||
Store.new(items).update_quality
|
||||
|
||||
assert_equal initial_expired_item_quality - 2, expired_item.quality
|
||||
assert_equal initial_unexpired_item_quality - 1, unexpired_item.quality
|
||||
end
|
||||
|
||||
test "Once the sell by date has passed, quality degrades but it does not drop below zero" do
|
||||
expired_item = Item.new(name = "TestItem", sell_in = 0, quality = 0)
|
||||
|
||||
gilded_rose = Store.new([expired_item])
|
||||
|
||||
gilded_rose.update_quality
|
||||
assert_equal 0, expired_item.quality
|
||||
end
|
||||
|
||||
test "After every update_quality call, sell_in is reduced by 1" do
|
||||
number_of_updates = 10
|
||||
initial_item_sell_in = 4
|
||||
|
||||
item = Item.new(name = "TestItem", sell_in = initial_item_sell_in, quality = 0)
|
||||
gilded_rose = Store.new([item])
|
||||
number_of_updates.times { gilded_rose.update_quality }
|
||||
|
||||
expected_item_sell_in = initial_item_sell_in - number_of_updates
|
||||
assert_equal expected_item_sell_in, item.sell_in
|
||||
end
|
||||
|
||||
test "Aged Brie item increases in Quality the older it gets" do
|
||||
number_of_updates = 10
|
||||
initial_item_sell_in = 10
|
||||
initial_item_quality = 0
|
||||
|
||||
item = Item.new(name = "Aged Brie", sell_in = initial_item_sell_in, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = Store.new([item])
|
||||
number_of_updates.times { gilded_rose.update_quality }
|
||||
|
||||
expected_item_quality = initial_item_quality + number_of_updates
|
||||
assert_equal expected_item_quality, item.quality
|
||||
end
|
||||
|
||||
test "Aged Brie item increases in Quality the older it gets, but it does not exceed 50 in quality" do
|
||||
initial_item_sell_in = 10
|
||||
initial_item_quality = 0
|
||||
|
||||
item = Item.new(name = "Aged Brie", sell_in = initial_item_sell_in, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = Store.new([item])
|
||||
(MAX_QUALITY + 1).times { gilded_rose.update_quality }
|
||||
|
||||
assert_equal MAX_QUALITY, item.quality
|
||||
end
|
||||
|
||||
test "Sulfuras sell by date does not decrease after every update_quality" do
|
||||
number_of_updates = 20
|
||||
initial_item_sell_in = 10
|
||||
|
||||
item = Item.new(name = "Sulfuras, Hand of Ragnaros", sell_in = initial_item_sell_in, quality = 10)
|
||||
|
||||
gilded_rose = Store.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
assert_equal initial_item_sell_in, item.sell_in
|
||||
end
|
||||
|
||||
test "Sulfuras quality does not decrease after each update_quality" do
|
||||
initial_item_quality = 10
|
||||
|
||||
item = Item.new(name = "Sulfuras, Hand of Ragnaros", sell_in = 10, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = Store.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
assert_equal initial_item_quality, item.quality
|
||||
end
|
||||
|
||||
test "Backstage passes quality increases by 2 when the item has 10 days or less to expire" do
|
||||
initial_item_quality = 10
|
||||
|
||||
item = Item.new(name = "Backstage passes to a TAFKAL80ETC concert", sell_in = 10, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = Store.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
expected_item_quality = initial_item_quality + 2
|
||||
assert_equal expected_item_quality, item.quality
|
||||
end
|
||||
|
||||
test "Backstage passes quality increases by 3 when the item has 5 days or less to expire" do
|
||||
initial_item_quality = 10
|
||||
|
||||
item = Item.new(name = "Backstage passes to a TAFKAL80ETC concert", sell_in = 5, quality = initial_item_quality)
|
||||
|
||||
gilded_rose = Store.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
expected_item_quality = initial_item_quality + 3
|
||||
assert_equal expected_item_quality, item.quality
|
||||
end
|
||||
|
||||
test "Backstage passes quality drops to 0 when the item is expired" do
|
||||
initial_expired_item_quality = 10
|
||||
initial_expired_item_sell_in = 0
|
||||
item = Item.new(name = "Backstage passes to a TAFKAL80ETC concert", sell_in = initial_expired_item_sell_in, quality = initial_expired_item_quality)
|
||||
|
||||
gilded_rose = Store.new([item])
|
||||
gilded_rose.update_quality
|
||||
|
||||
assert_equal 0, item.quality
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,33 +0,0 @@
|
||||
#!/usr/bin/ruby -w
|
||||
|
||||
require File.join(File.dirname(__FILE__), 'gilded_rose')
|
||||
|
||||
puts "OMGHAI!"
|
||||
items = [
|
||||
Item.new(name="+5 Dexterity Vest", sell_in=10, quality=20),
|
||||
Item.new(name="Aged Brie", sell_in=2, quality=0),
|
||||
Item.new(name="Elixir of the Mongoose", sell_in=5, quality=7),
|
||||
Item.new(name="Sulfuras, Hand of Ragnaros", sell_in=0, quality=80),
|
||||
Item.new(name="Sulfuras, Hand of Ragnaros", sell_in=-1, quality=80),
|
||||
Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20),
|
||||
Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=10, quality=49),
|
||||
Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=49),
|
||||
# This Conjured item does not work properly yet
|
||||
Item.new(name="Conjured Mana Cake", sell_in=3, quality=6), # <-- :O
|
||||
]
|
||||
|
||||
days = 2
|
||||
if ARGV.size > 0
|
||||
days = ARGV[0].to_i + 1
|
||||
end
|
||||
|
||||
gilded_rose = GildedRose.new items
|
||||
(0...days).each do |day|
|
||||
puts "-------- day #{day} --------"
|
||||
puts "name, sellIn, quality"
|
||||
items.each do |item|
|
||||
puts item
|
||||
end
|
||||
puts ""
|
||||
gilded_rose.update_quality
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user