extracted feature tests to their own files

This commit is contained in:
ollie beney 2020-11-05 17:34:00 +00:00
parent 75fa4869c0
commit c74abe5e3c
6 changed files with 238 additions and 262 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'rubocop' gem 'rubocop'

View File

@ -1,10 +1,12 @@
class GildedRose # frozen_string_literal: true
# class for gildedrose item degradation
class GildedRose
def self.update_quality(items) def self.update_quality(items)
items.map do |item| items.map do |item|
case case
when !special_item?(item) when !special_item?(item)
update_normal_quality(item) update_normal_quality(item)
item.sell_in -= 1 item.sell_in -= 1
when backstage?(item) when backstage?(item)
update_backstage_quality(item) if item.quality < 50 update_backstage_quality(item) if item.quality < 50
@ -13,27 +15,27 @@ class GildedRose
update_brie_quality(item) update_brie_quality(item)
item.sell_in -= 1 item.sell_in -= 1
when conjured?(item) when conjured?(item)
2.times {update_normal_quality(item)} 2.times { update_normal_quality(item) }
end end
end end
end end
def self.update_backstage_quality(item) def self.update_backstage_quality(item)
case item.sell_in case item.sell_in
when (-(Float::INFINITY)..0) when (-Float::INFINITY..0)
item.quality = 0 item.quality = 0
when 0..5 when 0..5
3.times { item.quality += 1 if item.quality < 50 } 3.times { item.quality += 1 if item.quality < 50 }
when 6..10 when 6..10
2.times { item.quality += 1 if item.quality < 50 } 2.times { item.quality += 1 if item.quality < 50 }
when 10..Float::INFINITY when 10..Float::INFINITY
item.quality += 1 item.quality += 1
end end
end end
def self.update_normal_quality(item) def self.update_normal_quality(item)
if item.sell_in < 0 if item.sell_in.negative?
2.times { item.quality -= 1 unless item.quality.zero?} 2.times { item.quality -= 1 unless item.quality.zero? }
else else
item.quality -= 1 unless item.quality.zero? item.quality -= 1 unless item.quality.zero?
end end
@ -41,18 +43,18 @@ class GildedRose
def self.update_brie_quality(item) def self.update_brie_quality(item)
if item.sell_in < 1 && item.quality < 48 if item.sell_in < 1 && item.quality < 48
item.quality += 2 item.quality += 2
else else
item.quality += 1 if item.quality < 50 item.quality += 1 if item.quality < 50
end end
end end
def self.sulfuras?(item) def self.sulfuras?(item)
!item.name.downcase.match( /sulfuras/).nil? !item.name.downcase.match(/sulfuras/).nil?
end end
def self.brie?(item) def self.brie?(item)
!item.name.downcase.match( /aged brie/).nil? !item.name.downcase.match(/aged brie/).nil?
end end
def self.backstage?(item) def self.backstage?(item)
@ -64,34 +66,10 @@ class GildedRose
end end
def self.special_item?(item) def self.special_item?(item)
( brie?(item) || backstage?(item) || conjured?(item) || sulfuras?(item)) (brie?(item) || backstage?(item) || conjured?(item) || sulfuras?(item))
end end
end end
# class for items owned by gilded rose inn
class Item class Item
attr_accessor :name, :sell_in, :quality attr_accessor :name, :sell_in, :quality
@ -101,7 +79,7 @@ class Item
@quality = quality @quality = quality
end end
def to_s() # def to_s
"#{@name}, #{@sell_in}, #{@quality}" # "#{@name}, #{@sell_in}, #{@quality}"
end # end
end end

View File

@ -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

View File

@ -1,141 +1,23 @@
# frozen_string_literal: true
# rubocop: disable all
require 'gilded_rose' require 'gilded_rose'
describe GildedRose do 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 describe '#update_normal_quality' do
it 'updates the quality of a normal item' 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) expect(item_double).to receive(:quality=).with(2)
GildedRose.update_normal_quality(item_double) GildedRose.update_normal_quality(item_double)
end end
it 'does not update quality if quality == 0' do 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) expect(item_double).not_to receive(:quality=).with(-1)
GildedRose.update_normal_quality(item_double) GildedRose.update_normal_quality(item_double)
end end
end end
describe '#selfarus?' do describe '#selfarus?' do
@ -145,79 +27,76 @@ end
end end
it 'returns false on a non sulfuras item' do 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 expect(GildedRose.sulfuras?(item_double)).to eq false
end end
end end
describe '#special_item?' do describe '#special_item?' do
it 'returns true for aged brie' 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 expect(GildedRose.special_item?(item_double)).to eq true
end end
it 'returns true for backstage passes' do 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 expect(GildedRose.special_item?(item_double)).to eq true
end end
it 'returns true for sulfuras' do it 'returns true for sulfuras' do
sulfuras_double = double :selfarus, name: 'Sulfuras' sulfuras_double = double :selfarus, name: 'Sulfuras'
expect(GildedRose.special_item?(sulfuras_double)).to eq true expect(GildedRose.special_item?(sulfuras_double)).to eq true
end end
it 'returns false on potato' do 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 expect(GildedRose.special_item?(item_double)).to eq false
end end
end end
describe '#update_backstage_quality' do describe '#update_backstage_quality' do
it 'increases in quality by one when sellIn is > 10 days' 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 items_double = double :item, name: 'Backstage passes to a TAFKAL80ETC concert', sell_in: 15, quality: 20
expect(items_double).to receive(:quality=).with(21) expect(items_double).to receive(:quality=).with(21)
GildedRose.update_backstage_quality(items_double) GildedRose.update_backstage_quality(items_double)
end end
it 'increases in quality by 2 when sellin < 10 days' do 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 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 expect(items_double).to receive(:quality=).with(21).twice
GildedRose.update_backstage_quality(items_double) GildedRose.update_backstage_quality(items_double)
end
end
it 'increases by 3 when sellin < 5' do it 'increases by 3 when sellin < 5' do
items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 4, quality: 20 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 expect(items_double).to receive(:quality=).with(21).exactly(3).times
GildedRose.update_backstage_quality(items_double) GildedRose.update_backstage_quality(items_double)
end end
it 'decreases to 0 when sellin == 0' do it 'decreases to 0 when sellin == 0' do
items_double = double :item, name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 0, quality: 20 items_double = double :item, name: 'Backstage passes to a TAFKAL80ETC concert', sell_in: 0, quality: 20
expect(items_double).to receive(:quality=).with(0) expect(items_double).to receive(:quality=).with(0)
GildedRose.update_backstage_quality(items_double) GildedRose.update_backstage_quality(items_double)
end end
end end
describe '#update_brie_quality' do describe '#update_brie_quality' do
it 'should add one to the quality each day ' 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) expect(brie_double).to receive(:quality=).with(16)
GildedRose.update_brie_quality(brie_double) GildedRose.update_brie_quality(brie_double)
end end
it 'should not let quality go past 50' do 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) expect(brie_double).not_to receive(:quality=).with(51)
GildedRose.update_brie_quality(brie_double) GildedRose.update_brie_quality(brie_double)
end end
it 'should increase by two when sellin <= 0' do 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) expect(brie_double).to receive(:quality=).with(4)
GildedRose.update_brie_quality(brie_double) GildedRose.update_brie_quality(brie_double)
end end
end end
describe '#brie?' do describe '#brie?' do
@ -227,25 +106,21 @@ end
end end
it 'returns false on a non brie item' do 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 expect(GildedRose.sulfuras?(item_double)).to eq false
end end
end end
describe '#backstage?' do describe '#backstage?' do
it 'returns true on a backstage item' 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 expect(GildedRose.backstage?(backstage_double)).to eq true
end end
it 'returns false on a non brie item' do 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 expect(GildedRose.backstage?(item_double)).to eq false
end end
end end
end end
# rubocop:enable all

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# This file was generated by the `rspec --init` command. Conventionally, all # This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause # 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. # triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups config.shared_context_metadata_behavior = :apply_to_host_groups
# The settings below are suggested to provide a good initial experience # The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content. # 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
# This allows you to limit a spec run to individual examples or groups # # you care about by tagging them with `:focus` metadata. When nothing
# you care about by tagging them with `:focus` metadata. When nothing # # is tagged with `:focus`, all examples get run. RSpec also provides
# is tagged with `:focus`, all examples get run. RSpec also provides # # aliases for `it`, `describe`, and `context` that include `:focus`
# aliases for `it`, `describe`, and `context` that include `:focus` # # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
# metadata: `fit`, `fdescribe` and `fcontext`, respectively. # config.filter_run_when_matching :focus
config.filter_run_when_matching :focus #
# # Allows RSpec to persist some state between runs in order to support
# Allows RSpec to persist some state between runs in order to support # # the `--only-failures` and `--next-failure` CLI options. We recommend
# the `--only-failures` and `--next-failure` CLI options. We recommend # # you configure your source control system to ignore this file.
# you configure your source control system to ignore this file. # config.example_status_persistence_file_path = "spec/examples.txt"
config.example_status_persistence_file_path = "spec/examples.txt" #
# # Limits the available syntax to the non-monkey patched syntax that is
# Limits the available syntax to the non-monkey patched syntax that is # # recommended. For more details, see:
# recommended. For more details, see: # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-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
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode # config.disable_monkey_patching!
config.disable_monkey_patching! #
# # This setting enables warnings. It's recommended, but in some cases may
# This setting enables warnings. It's recommended, but in some cases may # # be too noisy due to issues in dependencies.
# be too noisy due to issues in dependencies. # config.warnings = true
config.warnings = true #
# # Many RSpec users commonly either run the entire suite or an individual
# 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
# file, and it's useful to allow more verbose output when running an # # individual spec file.
# individual spec file. # if config.files_to_run.one?
if config.files_to_run.one? # # Use the documentation formatter for detailed output,
# Use the documentation formatter for detailed output, # # unless a formatter has already been configured
# unless a formatter has already been configured # # (e.g. via a command-line flag).
# (e.g. via a command-line flag). # config.default_formatter = "doc"
config.default_formatter = "doc" # end
end #
# # Print the 10 slowest examples and example groups at the
# Print the 10 slowest examples and example groups at the # # end of the spec run, to help surface which specs are running
# end of the spec run, to help surface which specs are running # # particularly slow.
# particularly slow. # config.profile_examples = 10
config.profile_examples = 10 #
# # Run specs in random order to surface order dependencies. If you find an
# 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
# order dependency and want to debug it, you can fix the order by providing # # the seed, which is printed after each run.
# the seed, which is printed after each run. # # --seed 1234
# --seed 1234 # config.order = :random
config.order = :random #
# # Seed global randomization in this process using the `--seed` CLI option.
# Seed global randomization in this process using the `--seed` CLI option. # # Setting this allows you to use `--seed` to deterministically reproduce
# Setting this allows you to use `--seed` to deterministically reproduce # # test failures related to randomization by passing the same `--seed` value
# test failures related to randomization by passing the same `--seed` value # # as the one that triggered the failure.
# as the one that triggered the failure. # Kernel.srand config.seed
Kernel.srand config.seed
=end
end end

1
ruby/spec/texttest_fixture.rb Normal file → Executable file
View File

@ -1,4 +1,5 @@
#!/usr/bin/ruby -w #!/usr/bin/ruby -w
# frozen_string_literal: true
# rubocop:disable all # rubocop:disable all
require File.join(File.dirname(__FILE__), 'gilded_rose') require File.join(File.dirname(__FILE__), 'gilded_rose')