From dc9e8be57d6ddcd613da213d40abd5f7f72f1987 Mon Sep 17 00:00:00 2001 From: Feken Baboyan Date: Thu, 25 Feb 2021 15:24:12 -0500 Subject: [PATCH] Restructure GildedRose Project --- ruby/gilded_rose.rb | 93 -------------- ruby/gilded_rose_spec.rb | 13 -- ruby/gilded_rose_tests.rb | 125 ------------------ ruby/item_factory.rb | 10 -- ruby/item_factory_tests.rb | 16 --- ruby/lib/gilded_rose.rb | 5 + ruby/lib/gilded_rose/item_factory.rb | 13 ++ ruby/lib/gilded_rose/store.rb | 83 ++++++++++++ ruby/tests/gilded_rose/item_factory_tests.rb | 19 +++ ruby/tests/gilded_rose/store_tests.rb | 128 +++++++++++++++++++ ruby/texttest_fixture.rb | 33 ----- 11 files changed, 248 insertions(+), 290 deletions(-) delete mode 100644 ruby/gilded_rose.rb delete mode 100644 ruby/gilded_rose_spec.rb delete mode 100644 ruby/gilded_rose_tests.rb delete mode 100644 ruby/item_factory.rb delete mode 100644 ruby/item_factory_tests.rb create mode 100644 ruby/lib/gilded_rose.rb create mode 100644 ruby/lib/gilded_rose/item_factory.rb create mode 100644 ruby/lib/gilded_rose/store.rb create mode 100644 ruby/tests/gilded_rose/item_factory_tests.rb create mode 100644 ruby/tests/gilded_rose/store_tests.rb delete mode 100644 ruby/texttest_fixture.rb diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb deleted file mode 100644 index 11d061b6..00000000 --- a/ruby/gilded_rose.rb +++ /dev/null @@ -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 - - diff --git a/ruby/gilded_rose_spec.rb b/ruby/gilded_rose_spec.rb deleted file mode 100644 index 269fe1b0..00000000 --- a/ruby/gilded_rose_spec.rb +++ /dev/null @@ -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 diff --git a/ruby/gilded_rose_tests.rb b/ruby/gilded_rose_tests.rb deleted file mode 100644 index 147d93a4..00000000 --- a/ruby/gilded_rose_tests.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/ruby/item_factory.rb b/ruby/item_factory.rb deleted file mode 100644 index 0396d808..00000000 --- a/ruby/item_factory.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/ruby/item_factory_tests.rb b/ruby/item_factory_tests.rb deleted file mode 100644 index 87eadbc2..00000000 --- a/ruby/item_factory_tests.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/ruby/lib/gilded_rose.rb b/ruby/lib/gilded_rose.rb new file mode 100644 index 00000000..938ce5ce --- /dev/null +++ b/ruby/lib/gilded_rose.rb @@ -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 \ No newline at end of file diff --git a/ruby/lib/gilded_rose/item_factory.rb b/ruby/lib/gilded_rose/item_factory.rb new file mode 100644 index 00000000..fa3d567e --- /dev/null +++ b/ruby/lib/gilded_rose/item_factory.rb @@ -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 + diff --git a/ruby/lib/gilded_rose/store.rb b/ruby/lib/gilded_rose/store.rb new file mode 100644 index 00000000..5919f24d --- /dev/null +++ b/ruby/lib/gilded_rose/store.rb @@ -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 + diff --git a/ruby/tests/gilded_rose/item_factory_tests.rb b/ruby/tests/gilded_rose/item_factory_tests.rb new file mode 100644 index 00000000..0437544a --- /dev/null +++ b/ruby/tests/gilded_rose/item_factory_tests.rb @@ -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 + diff --git a/ruby/tests/gilded_rose/store_tests.rb b/ruby/tests/gilded_rose/store_tests.rb new file mode 100644 index 00000000..e7dc16e6 --- /dev/null +++ b/ruby/tests/gilded_rose/store_tests.rb @@ -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 + diff --git a/ruby/texttest_fixture.rb b/ruby/texttest_fixture.rb deleted file mode 100644 index ad76aacf..00000000 --- a/ruby/texttest_fixture.rb +++ /dev/null @@ -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