From c74abe5e3c3ca5e26beb96a0ea1d9da32fc0ee4a Mon Sep 17 00:00:00 2001 From: ollie beney Date: Thu, 5 Nov 2020 17:34:00 +0000 Subject: [PATCH] extracted feature tests to their own files --- ruby/Gemfile | 2 + ruby/lib/gilded_rose.rb | 74 +++---- .../features/gilded_rose_features_spec.rb | 120 +++++++++++ ruby/spec/gilded_rose_spec.rb | 197 ++++-------------- ruby/spec/spec_helper.rb | 106 +++++----- ruby/spec/texttest_fixture.rb | 1 + 6 files changed, 238 insertions(+), 262 deletions(-) create mode 100644 ruby/spec/features/gilded_rose_features_spec.rb mode change 100644 => 100755 ruby/spec/texttest_fixture.rb diff --git a/ruby/Gemfile b/ruby/Gemfile index e076bdae..11d1436b 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gem 'rubocop' diff --git a/ruby/lib/gilded_rose.rb b/ruby/lib/gilded_rose.rb index ced8d0c2..ece4bb2d 100644 --- a/ruby/lib/gilded_rose.rb +++ b/ruby/lib/gilded_rose.rb @@ -1,10 +1,12 @@ -class GildedRose +# frozen_string_literal: true +# class for gildedrose item degradation +class GildedRose def self.update_quality(items) items.map do |item| - case + case when !special_item?(item) - update_normal_quality(item) + update_normal_quality(item) item.sell_in -= 1 when backstage?(item) update_backstage_quality(item) if item.quality < 50 @@ -13,27 +15,27 @@ class GildedRose update_brie_quality(item) item.sell_in -= 1 when conjured?(item) - 2.times {update_normal_quality(item)} + 2.times { update_normal_quality(item) } end end end def self.update_backstage_quality(item) - case item.sell_in - when (-(Float::INFINITY)..0) - item.quality = 0 - when 0..5 - 3.times { item.quality += 1 if item.quality < 50 } - when 6..10 - 2.times { item.quality += 1 if item.quality < 50 } - when 10..Float::INFINITY - item.quality += 1 - end + case item.sell_in + when (-Float::INFINITY..0) + item.quality = 0 + when 0..5 + 3.times { item.quality += 1 if item.quality < 50 } + when 6..10 + 2.times { item.quality += 1 if item.quality < 50 } + when 10..Float::INFINITY + item.quality += 1 + end end def self.update_normal_quality(item) - if item.sell_in < 0 - 2.times { item.quality -= 1 unless item.quality.zero?} + if item.sell_in.negative? + 2.times { item.quality -= 1 unless item.quality.zero? } else item.quality -= 1 unless item.quality.zero? end @@ -41,18 +43,18 @@ class GildedRose def self.update_brie_quality(item) if item.sell_in < 1 && item.quality < 48 - item.quality += 2 + item.quality += 2 else item.quality += 1 if item.quality < 50 end end def self.sulfuras?(item) - !item.name.downcase.match( /sulfuras/).nil? + !item.name.downcase.match(/sulfuras/).nil? end def self.brie?(item) - !item.name.downcase.match( /aged brie/).nil? + !item.name.downcase.match(/aged brie/).nil? end def self.backstage?(item) @@ -64,34 +66,10 @@ class GildedRose end def self.special_item?(item) - ( brie?(item) || backstage?(item) || conjured?(item) || sulfuras?(item)) + (brie?(item) || backstage?(item) || conjured?(item) || sulfuras?(item)) end - - - end - - - - - - - - - - - - - - - - - - - - - - +# class for items owned by gilded rose inn class Item attr_accessor :name, :sell_in, :quality @@ -101,7 +79,7 @@ class Item @quality = quality end - def to_s() - "#{@name}, #{@sell_in}, #{@quality}" - end + # def to_s + # "#{@name}, #{@sell_in}, #{@quality}" + # end end diff --git a/ruby/spec/features/gilded_rose_features_spec.rb b/ruby/spec/features/gilded_rose_features_spec.rb new file mode 100644 index 00000000..c5e06ab4 --- /dev/null +++ b/ruby/spec/features/gilded_rose_features_spec.rb @@ -0,0 +1,120 @@ +require 'gilded_rose' + +# rubocop:disable all +describe 'feature tests' do + let(:potato) { Item.new('potato', 15, 2) } + let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) } + + it 'does not change the name' do + items = [Item.new('foo', 0, 0)] + GildedRose.update_quality(items) + expect(items[0].name).to eq 'foo' + end + + describe 'non-special item input' do + it 'should decrese the quality of normal item' do + items = [potato] + GildedRose.update_quality(items) + expect(items.first.quality).to eq(1) + end + + it 'should decrese the sellIn of normal item' do + items = [potato] + GildedRose.update_quality(items) + expect(items.first.sell_in).to eq(14) + end + + it 'should decrease quality of normal items by 2 when sell_in date passes' do + items = [Item.new('old potato', -1, 20)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq(18) + end + + it 'should not decrease quality below 0' do + items = [Item.new('old potato', 0, 0)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq(0) + end + end + + describe 'Aged Brie input' do + it 'increases in quality as it ages' do + items = [Item.new('Aged Brie', 25, 45)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 46 + end + + it 'cannot increase in quality beyond 50' do + items = [Item.new('Aged Brie', 25, 50)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 50 + end + it 'doubles the increase when sell_in below 1' do + items = [Item.new('Aged Brie', 0, 20)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 22 + end + end + + describe 'Sulfuras input' do + it 'does not change' do + items = [sulfarus] + GildedRose.update_quality(items) + expect(items[0].quality).to eq 80 + expect(items[0].sell_in).to eq 50 + end + end + + describe 'backstage pass input' do + it 'increases in quality by one when sellIn is > 10 days' do + items = [Item.new('Backstage passes to a TAFKAL80ETC concert', 15, 20)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 21 + end + + it 'increases in quality by 2 when sellin < 10 days' do + items = [Item.new('Backstage passes to a TAFKAL80ETC concert', 9, 20)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 22 + end + + it 'increases by 3 when sellin < 5' do + items = [Item.new('Backstage passes to a TAFKAL80ETC concert', 4, 20)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 23 + end + + it 'decreases to 0 when sellin == 0' do + items = [Item.new('Backstage passes to a TAFKAL80ETC concert', 0, 20)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 0 + end + end + + describe 'conjured input' do + it 'should degrade twice as fast as normal items' do + items = [Item.new('Conjured Armour', 10, 15)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 13 + end + + it 'should not degrade past 0' do + items = [Item.new('Conjured Armour', 10, 1)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 0 + end + + it 'should degrade by 4 when sell in has passed' do + items = [Item.new('Conjured Armour', -1, 17)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 13 + end + + it 'still should not degrade past 0 when sell in has passed' do + items = [Item.new('Conjured Armour', -1, 3)] + GildedRose.update_quality(items) + expect(items.first.quality).to eq 0 + end + end +end +# rubocop:enable all \ No newline at end of file diff --git a/ruby/spec/gilded_rose_spec.rb b/ruby/spec/gilded_rose_spec.rb index 17abbb71..f912f62e 100644 --- a/ruby/spec/gilded_rose_spec.rb +++ b/ruby/spec/gilded_rose_spec.rb @@ -1,141 +1,23 @@ +# frozen_string_literal: true + +# rubocop: disable all require 'gilded_rose' describe GildedRose do -let(:potato) { Item.new('potato', 15, 2)} -let(:sulfarus) { Item.new('Sulfuras, Hand of Ragnaros', 50, 80) } - - describe "feature tests" do - it "does not change the name" do - items = [Item.new("foo", 0, 0)] - GildedRose.update_quality(items) - expect(items[0].name).to eq "foo" - end - describe 'non-special item input' do - it 'should decrese the quality of normal item' do - items = [potato] - GildedRose.update_quality(items) - expect(items.first.quality).to eq (1) - end - - it 'should decrese the sellIn of normal item' do - items = [potato] - GildedRose.update_quality(items) - expect(items.first.sell_in).to eq (14) - end - - it 'should decrease quality of normal items by 2 when sell_in date passes' do - items = [Item.new("old potato", -1, 20)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq (18) - end - - it 'should not decrease quality below 0' do - items = [Item.new("old potato", 0, 0)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq (0) - end - end - - - - describe 'Aged Brie input' do - it 'increases in quality as it ages' do - items = [Item.new("Aged Brie", 25, 45)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 46 - end - - it 'cannot increase in quality beyond 50' do - items = [Item.new("Aged Brie", 25, 50)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 50 - end - it 'doubles the increase when sell_in below 1' do - items = [Item.new("Aged Brie", 0, 20)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 22 - end - end - - describe 'Sulfuras input' do - it 'does not change' do - items = [sulfarus] - GildedRose.update_quality(items) - expect(items[0].quality).to eq 80 - expect(items[0].sell_in).to eq 50 - end - end - - describe 'backstage pass input' do - it 'increases in quality by one when sellIn is > 10 days' do - items = [Item.new("Backstage passes to a TAFKAL80ETC concert", 15, 20)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 21 - end - - it 'increases in quality by 2 when sellin < 10 days' do - items = [Item.new("Backstage passes to a TAFKAL80ETC concert", 9, 20)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 22 - end - - it 'increases by 3 when sellin < 5' do - items = [Item.new("Backstage passes to a TAFKAL80ETC concert", 4, 20)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 23 - end - - it 'decreases to 0 when sellin == 0' do - items = [Item.new("Backstage passes to a TAFKAL80ETC concert", 0, 20)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 0 - end - end - - describe 'conjured input' do - it "should degrade twice as fast as normal items" do - items = [Item.new("Conjured Armour", 10, 15)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 13 - end - - it "should not degrade past 0" do - items = [Item.new("Conjured Armour", 10, 1)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 0 - end - - it "should degrade by 4 when sell in has passed" do - items = [Item.new("Conjured Armour", -1, 17)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 13 - end - - it "still should not degrade past 0 when sell in has passed" do - items = [Item.new("Conjured Armour", -1, 3)] - GildedRose.update_quality(items) - expect(items.first.quality).to eq 0 - end - end - - - - -end + describe '#update_normal_quality' do it 'updates the quality of a normal item' do - item_double = double :item, name: "potato", sell_in: 1, quality: 3 + item_double = double :item, name: 'potato', sell_in: 1, quality: 3 expect(item_double).to receive(:quality=).with(2) GildedRose.update_normal_quality(item_double) end it 'does not update quality if quality == 0' do - item_double = double :item, name: "potato", sell_in: 1, quality: 0 + item_double = double :item, name: 'potato', sell_in: 1, quality: 0 expect(item_double).not_to receive(:quality=).with(-1) GildedRose.update_normal_quality(item_double) end - end describe '#selfarus?' do @@ -145,79 +27,76 @@ end end it 'returns false on a non sulfuras item' do - item_double = double :item, name: "potato", sell_in: 1, quality: 0 + item_double = double :item, name: 'potato', sell_in: 1, quality: 0 expect(GildedRose.sulfuras?(item_double)).to eq false end end describe '#special_item?' do it 'returns true for aged brie' do - item_double = double :item, name: "Aged Brie" + item_double = double :item, name: 'Aged Brie' expect(GildedRose.special_item?(item_double)).to eq true end it 'returns true for backstage passes' do - item_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert" + item_double = double :item, name: 'Backstage passes to a TAFKAL80ETC concert' expect(GildedRose.special_item?(item_double)).to eq true end it 'returns true for sulfuras' do sulfuras_double = double :selfarus, name: 'Sulfuras' expect(GildedRose.special_item?(sulfuras_double)).to eq true - end + end it 'returns false on potato' do - item_double = double :item, name: "potato" + item_double = double :item, name: 'potato' expect(GildedRose.special_item?(item_double)).to eq false end end describe '#update_backstage_quality' do - it 'increases in quality by one when sellIn is > 10 days' do - items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 15, quality: 20 + it 'increases in quality by one when sellIn is > 10 days' do + items_double = double :item, name: 'Backstage passes to a TAFKAL80ETC concert', sell_in: 15, quality: 20 expect(items_double).to receive(:quality=).with(21) GildedRose.update_backstage_quality(items_double) - end + end - it 'increases in quality by 2 when sellin < 10 days' do - items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 9, quality: 20 - expect(items_double).to receive(:quality=).with(21).twice - GildedRose.update_backstage_quality(items_double) - - end + it 'increases in quality by 2 when sellin < 10 days' do + items_double = double :item, name: 'Backstage passes to a TAFKAL80ETC concert', sell_in: 9, quality: 20 + expect(items_double).to receive(:quality=).with(21).twice + GildedRose.update_backstage_quality(items_double) + end - it 'increases by 3 when sellin < 5' do - items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 4, quality: 20 - expect(items_double).to receive(:quality=).with(21).exactly(3).times - GildedRose.update_backstage_quality(items_double) - end + it 'increases by 3 when sellin < 5' do + items_double = double :item, name: 'Backstage passes to a TAFKAL80ETC concert', sell_in: 4, quality: 20 + expect(items_double).to receive(:quality=).with(21).exactly(3).times + GildedRose.update_backstage_quality(items_double) + end - it 'decreases to 0 when sellin == 0' do - items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 0, quality: 20 - expect(items_double).to receive(:quality=).with(0) - GildedRose.update_backstage_quality(items_double) - end + it 'decreases to 0 when sellin == 0' do + items_double = double :item, name: 'Backstage passes to a TAFKAL80ETC concert', sell_in: 0, quality: 20 + expect(items_double).to receive(:quality=).with(0) + GildedRose.update_backstage_quality(items_double) + end end describe '#update_brie_quality' do it 'should add one to the quality each day ' do - brie_double = double :item, name: "Aged Brie", sell_in: 30, quality:15 + brie_double = double :item, name: 'Aged Brie', sell_in: 30, quality: 15 expect(brie_double).to receive(:quality=).with(16) GildedRose.update_brie_quality(brie_double) end it 'should not let quality go past 50' do - brie_double = double :item, name: "Aged Brie", sell_in: 30, quality:50 + brie_double = double :item, name: 'Aged Brie', sell_in: 30, quality: 50 expect(brie_double).not_to receive(:quality=).with(51) GildedRose.update_brie_quality(brie_double) end it 'should increase by two when sellin <= 0' do - brie_double = double :item, name: "Aged Brie", sell_in: 0, quality:2 + brie_double = double :item, name: 'Aged Brie', sell_in: 0, quality: 2 expect(brie_double).to receive(:quality=).with(4) GildedRose.update_brie_quality(brie_double) end - - end describe '#brie?' do @@ -227,25 +106,21 @@ end end it 'returns false on a non brie item' do - item_double = double :item, name: "potato", sell_in: 1, quality: 0 + item_double = double :item, name: 'potato', sell_in: 1, quality: 0 expect(GildedRose.sulfuras?(item_double)).to eq false end end describe '#backstage?' do it 'returns true on a backstage item' do - backstage_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert" + backstage_double = double :item, name: 'Backstage passes to a TAFKAL80ETC concert' expect(GildedRose.backstage?(backstage_double)).to eq true end it 'returns false on a non brie item' do - item_double = double :item, name: "potato", sell_in: 1, quality: 0 + item_double = double :item, name: 'potato', sell_in: 1, quality: 0 expect(GildedRose.backstage?(item_double)).to eq false end end - - - - - end +# rubocop:enable all \ No newline at end of file diff --git a/ruby/spec/spec_helper.rb b/ruby/spec/spec_helper.rb index 251aa510..5681bb3a 100644 --- a/ruby/spec/spec_helper.rb +++ b/ruby/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause @@ -44,57 +46,55 @@ RSpec.configure do |config| # triggering implicit auto-inclusion in groups with matching metadata. config.shared_context_metadata_behavior = :apply_to_host_groups -# The settings below are suggested to provide a good initial experience -# with RSpec, but feel free to customize to your heart's content. -=begin - # This allows you to limit a spec run to individual examples or groups - # you care about by tagging them with `:focus` metadata. When nothing - # is tagged with `:focus`, all examples get run. RSpec also provides - # aliases for `it`, `describe`, and `context` that include `:focus` - # metadata: `fit`, `fdescribe` and `fcontext`, respectively. - config.filter_run_when_matching :focus - - # Allows RSpec to persist some state between runs in order to support - # the `--only-failures` and `--next-failure` CLI options. We recommend - # you configure your source control system to ignore this file. - config.example_status_persistence_file_path = "spec/examples.txt" - - # Limits the available syntax to the non-monkey patched syntax that is - # recommended. For more details, see: - # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ - # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode - config.disable_monkey_patching! - - # This setting enables warnings. It's recommended, but in some cases may - # be too noisy due to issues in dependencies. - config.warnings = true - - # Many RSpec users commonly either run the entire suite or an individual - # file, and it's useful to allow more verbose output when running an - # individual spec file. - if config.files_to_run.one? - # Use the documentation formatter for detailed output, - # unless a formatter has already been configured - # (e.g. via a command-line flag). - config.default_formatter = "doc" - end - - # Print the 10 slowest examples and example groups at the - # end of the spec run, to help surface which specs are running - # particularly slow. - config.profile_examples = 10 - - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - config.order = :random - - # Seed global randomization in this process using the `--seed` CLI option. - # Setting this allows you to use `--seed` to deterministically reproduce - # test failures related to randomization by passing the same `--seed` value - # as the one that triggered the failure. - Kernel.srand config.seed -=end + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + # config.disable_monkey_patching! + # + # # This setting enables warnings. It's recommended, but in some cases may + # # be too noisy due to issues in dependencies. + # config.warnings = true + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = "doc" + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed end diff --git a/ruby/spec/texttest_fixture.rb b/ruby/spec/texttest_fixture.rb old mode 100644 new mode 100755 index cb698965..3897fb25 --- a/ruby/spec/texttest_fixture.rb +++ b/ruby/spec/texttest_fixture.rb @@ -1,4 +1,5 @@ #!/usr/bin/ruby -w +# frozen_string_literal: true # rubocop:disable all require File.join(File.dirname(__FILE__), 'gilded_rose')