Test task

This commit is contained in:
Vivek Soni 2023-12-07 16:19:26 +05:30
parent 2e30c563d8
commit 59d0af0362
2 changed files with 82 additions and 46 deletions

View File

@ -1,56 +1,93 @@
# frozen_string_literal: true
class GildedRose
MAX_QUALITY = 50
def initialize(items)
@items = items
end
def update_quality()
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
case item.name
when 'Aged Brie'
update_aged_brie(item)
when 'Backstage passes to a TAFKAL80ETC concert'
update_backstage_passes(item)
when 'Sulfuras, Hand of Ragnaros'
next # Sulfuras remains unchanged, move to the next item
when 'Conjured Mana Cake'
update_conjured(item)
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
update_normal_item(item)
end
end
end
private
def update_aged_brie(item)
increase_quality(item)
decrease_sell_in(item)
end
def update_backstage_passes(item)
if positive_sell?(item)
if item.sell_in > 11
increase_quality(item)
elsif item.sell_in > 6
increase_quality(item, 2)
else
increase_quality(item, 3)
end
else
item.quality = 0
end
decrease_sell_in(item)
end
def update_normal_item(item)
decrease_quality(item, 2)
decrease_sell_in(item)
end
def update_conjured(item)
decrease_quality(item, 2)
decrease_sell_in(item)
end
def increase_quality(item, rate = 1)
return item.quality if minimum_sell?(item)
item.quality += rate if maximum_quality?(item)
end
def decrease_sell_in(item)
item.sell_in -= 1
end
def decrease_quality(item, rate = 1)
return item.quality if minimum_sell?(item)
item.quality -= rate if positive_sell?(item)
end
def positive_sell?(item)
item.sell_in.positive?
end
def negative_sell?(item)
item.sell_in.negative?
end
def minimum_sell?(item)
item.sell_in <= 0
end
def maximum_quality?(item)
item.quality < MAX_QUALITY
end
end
class Item
@ -62,7 +99,7 @@ class Item
@quality = quality
end
def to_s()
def to_s
"#{@name}, #{@sell_in}, #{@quality}"
end
end

View File

@ -1,7 +1,7 @@
require 'rspec'
require File.join(File.dirname(__FILE__), 'gilded_rose')
describe GildedRose do
RSpec.describe GildedRose do
describe "#update_quality" do
it "does not change the name" do
items = [Item.new("foo", 0, 0)]
@ -9,5 +9,4 @@ describe GildedRose do
expect(items[0].name).to eq "fixme"
end
end
end