From 8efec1b84dd242acdf80232a45fa89324ba57398 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 18 Jun 2019 17:42:02 +0000 Subject: [PATCH] Test task --- ruby/gilded_rose.rb | 46 ++++++++++++++++++++++----------------- ruby/gilded_rose_spec.rb | 33 ++++++++++++++++++++++++---- ruby/gilded_rose_tests.rb | 33 ++++++++++++++++++++++++---- ruby/texttest_fixture.rb | 38 ++++++++++++++------------------ 4 files changed, 100 insertions(+), 50 deletions(-) diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index 3c260621..01202778 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -1,51 +1,57 @@ class GildedRose - - def initialize(items) - @items = items + + def self.all_items + ObjectSpace.each_object(Item).to_a end - - def update_quality() - @items.each do |item| - if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert" + + def self.update_quality + all_items.each do |item| + if !item.name.include?("Aged Brie") and !item.name.include?("Backstage") if item.quality > 0 - if item.name != "Sulfuras, Hand of Ragnaros" - item.quality = item.quality - 1 + if !item.name.include?("Sulfuras") + item.quality -= 1 + if item.name.include?("Conjured") + item.quality -= 1 + end end end else if item.quality < 50 - item.quality = item.quality + 1 - if item.name == "Backstage passes to a TAFKAL80ETC concert" + item.quality += 1 + if item.name.include?("Backstage") if item.sell_in < 11 if item.quality < 50 - item.quality = item.quality + 1 + item.quality += 1 end end if item.sell_in < 6 if item.quality < 50 - item.quality = item.quality + 1 + item.quality += 1 end end end end end - if item.name != "Sulfuras, Hand of Ragnaros" + if !item.name.include?("Sulfuras") 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.name.include?("Aged Brie") + if !item.name.include?("Backstage") if item.quality > 0 - if item.name != "Sulfuras, Hand of Ragnaros" - item.quality = item.quality - 1 + if !item.name.include?("Sulfuras") + item.quality -= 1 + if item.name.include?("Conjured") + item.quality -= 1 + end end end else - item.quality = item.quality - item.quality + item.quality = 0 end else if item.quality < 50 - item.quality = item.quality + 1 + item.quality += 1 end end end diff --git a/ruby/gilded_rose_spec.rb b/ruby/gilded_rose_spec.rb index 269fe1b0..8e590eb2 100644 --- a/ruby/gilded_rose_spec.rb +++ b/ruby/gilded_rose_spec.rb @@ -2,11 +2,36 @@ require File.join(File.dirname(__FILE__), 'gilded_rose') describe GildedRose do - describe "#update_quality" 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" + + 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), + Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=39), + # Now it works perfectly + Item.new(name="Conjured Mana Cake", sell_in=0, quality=6), + Item.new(name="Conjured Mana Cake", sell_in=3, quality=6), + ] + + GildedRose.update_quality() + expect(items[0].quality).to eq 19 + expect(items[1].quality).to eq 1 + expect(items[2].quality).to eq 6 + expect(items[3].quality).to eq 80 + expect(items[4].quality).to eq 80 + expect(items[5].quality).to eq 21 + expect(items[6].quality).to eq 50 + expect(items[7].quality).to eq 50 + expect(items[8].quality).to eq 42 + expect(items[9].quality).to eq 2 + expect(items[10].quality).to eq 4 end end diff --git a/ruby/gilded_rose_tests.rb b/ruby/gilded_rose_tests.rb index 2e1b70d1..eb877d75 100644 --- a/ruby/gilded_rose_tests.rb +++ b/ruby/gilded_rose_tests.rb @@ -3,10 +3,35 @@ 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" + def test_gilded_rose + + 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), + Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=39), + # Now it works perfectly + Item.new(name="Conjured Mana Cake", sell_in=0, quality=6), + Item.new(name="Conjured Mana Cake", sell_in=3, quality=6), + ] + + GildedRose.update_quality() + assert_equal items[0].quality, 19 + assert_equal items[1].quality, 1 + assert_equal items[2].quality, 6 + assert_equal items[3].quality, 80 + assert_equal items[4].quality, 80 + assert_equal items[5].quality, 21 + assert_equal items[6].quality, 50 + assert_equal items[7].quality, 50 + assert_equal items[8].quality, 42 + assert_equal items[9].quality, 2 + assert_equal items[10].quality, 4 end end \ No newline at end of file diff --git a/ruby/texttest_fixture.rb b/ruby/texttest_fixture.rb index 25dd5f66..5c21e42c 100644 --- a/ruby/texttest_fixture.rb +++ b/ruby/texttest_fixture.rb @@ -2,32 +2,26 @@ 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 -] + Item.new(name="+5 Dexterity Vest", sell_in=10, quality=20), + Item.new(name="Aged Brie", sell_in=2, quality=0), + Item.new(name="Aged Brie", sell_in=2, quality=49), + 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), + # Now it works perfectly + Item.new(name="Conjured Mana Cake", sell_in=0, quality=6), + Item.new(name="Conjured Mana Cake", sell_in=3, quality=6), + ] +days = 3 -days = 2 -if ARGV.size > 0 - days = ARGV[0].to_i + 1 -end - -gilded_rose = GildedRose.new items -(0...days).each do |day| +(0..days).each do |day| puts "-------- day #{day} --------" - puts "name, sellIn, quality" items.each do |item| puts item end - puts "" - gilded_rose.update_quality + GildedRose.update_quality end