Skip to content

Instantly share code, notes, and snippets.

@GrantStannard
Created October 19, 2023 17:49
Show Gist options
  • Save GrantStannard/a2165ed7e0a5cf6d9939a085c105b53a to your computer and use it in GitHub Desktop.
Save GrantStannard/a2165ed7e0a5cf6d9939a085c105b53a to your computer and use it in GitHub Desktop.
Arcade Techinical interview
class GildedRose
def initialize(items)
@items = items
end
def update_quality()
@items.each do |item|
case item.name
when "Aged Brie"
item.sell_in -= 1
if item.quality < 50
item.quality += 1
item.quality += 1 if item.sell_in < 0
end
when "Backstage passes to a TAFKAL80ETC concert"
item.sell_in -= 1
if item.sell_in < 0
item.quality = 0
elsif item.sell_in < 5
item.quality += 3
elsif item.sell_in < 10
item.quality += 2
else
item.quality += 1
end
item.quality = 50 if item.quality > 50
when "Sulfuras, Hand of Ragnaros"
# do nothing legendary is always valuable
when "Conjured Mana Cake"
item.sell_in -= 1
item.quality -= 2
item.quality -= 2 if item.sell_in < 0
else
item.sell_in -= 1
item.quality -= 1
item.quality -= 1 if item.sell_in < 0
end
item.quality = 0 if item.quality < 0
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
require File.join(File.dirname(__FILE__), 'gilded_rose')
describe GildedRose do
describe "#update_quality" do
subject {gildedrose.update_quality}
let(:gildedrose) { GildedRose.new(items) }
context 'when there are multiple items ' do
let(:items) { [Item.new(name="Aged Brie", sell_in=2, quality=0), Item.new(name="Conjured Mana Cake", sell_in=3, quality=10)] }
it 'updates all of them' do
expect(subject.first.sell_in).to eq(1)
expect(subject.last.sell_in).to eq(2)
end
end
context 'when quality would go below 0' do
let(:items) { [Item.new(name="+5 Dexterity Vest", sell_in=2, quality=0)] }
it 'is set to 0' do
expect(subject.first.quality).to eq(0)
end
end
context 'brie' do
let(:items) { [Item.new(name="Aged Brie", sell_in=2, quality=0)] }
it 'decreases the sell in' do
expect(subject.first.sell_in).to eq(1)
end
context 'when the sell_in is 0' do
let(:items) { [Item.new(name="Aged Brie", sell_in=0, quality=0)] }
it 'increase in quality at twice as fast' do
expect(subject.first.quality).to eq(2)
end
end
context 'when quality is less than 50' do
it 'increases in quality' do
expect(subject.first.quality).to eq(1)
end
end
context 'when quality is 50' do
let(:items) { [Item.new(name="Aged Brie", sell_in=2, quality=50)] }
it 'does not increase in quality' do
expect(subject.first.quality).to eq(50)
end
end
end
context 'backstage passes' do
let(:items) { [Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20)] }
it 'increases in quality' do
expect(subject.first.quality).to eq(21)
end
it 'decreases the sell in' do
expect(subject.first.sell_in).to eq(14)
end
context 'when quality is 50 or less' do
context 'when sell in is 10 or less and greater than 5' do
let(:items) { [Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=6, quality=20)] }
it 'increases in quality at double rate'do
expect(subject.first.quality).to eq(22)
end
end
context 'when sell in is 5 or less' do
let(:items) { [Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=3, quality=20)] }
it 'increases in quality at triple rate' do
expect(subject.first.quality).to eq(23)
end
end
end
context 'when quality is 50' do
let(:items) { [Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=50)] }
it 'does not increase in quality' do
expect(subject.first.quality).to eq(50)
end
end
context 'when sell in is negative' do
let(:items) { [Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=0, quality=20)] }
it 'quailty is 0' do
expect(subject.first.quality).to eq(0)
end
end
end
context 'legendary item' do
let(:items) { [Item.new(name="Sulfuras, Hand of Ragnaros", sell_in=0, quality=80)] }
it 'does not decrease in quality' do
expect( subject.first.quality).to eq(80)
end
it 'does not decrease sell_in' do
expect(subject.first.sell_in).to eq(0)
end
end
context 'Conjured Mana Cake' do
let(:items) { [Item.new(name="Conjured Mana Cake", sell_in=3, quality=10)] }
it 'descreaseing in quality twice as fast' do
expect(subject.first.quality).to eq(8)
end
it 'decreases the sell in' do
expect(subject.first.sell_in).to eq(2)
end
context 'when the sell in is past' do
let(:items) { [Item.new(name="Conjured Mana Cake", sell_in=0, quality=10)] }
it 'decreases in quality four times as fast' do
expect(subject.first.quality).to eq(6)
end
end
end
context 'basic item' do
let(:items) { [Item.new(name="+5 Dexterity Vest", sell_in=10, quality=20)] }
it 'descreaseing in quality' do
expect(subject.first.quality).to eq(19)
end
it 'decreases the sell in' do
expect(subject.first.sell_in).to eq(9)
end
context 'when the sell in is past' do
let(:items) { [Item.new(name="+5 Dexterity Vest", sell_in=0, quality=20)] }
it 'decreases in quality twice as fast' do
expect(subject.first.quality).to eq(18)
end
end
end
it "does not change the name" do
items = [Item.new("foo", 0, 0)]
GildedRose.new(items).update_quality()
expect(items[0].name).to eq "foo"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment