Added sell-in modification rules

This commit is contained in:
Lucas van Dongen 2020-11-23 17:31:04 +01:00
parent 44bc9f5718
commit 4e79a86e03
3 changed files with 77 additions and 26 deletions

View File

@ -1,35 +1,14 @@
public class GildedRose {
var items:[Item]
private(set) var items: [Item]
public init(items:[Item]) {
public init(items: [Item]) {
self.items = items
}
public func updateQuality() {
for i in 0..<items.count {
items[i] = QualityModificationRule.process(item: items[i])
if (items[i].name != "Sulfuras, Hand of Ragnaros") {
items[i].sellIn = items[i].sellIn - 1
}
// if (items[i].sellIn < 0) {
// if (items[i].name != "Aged Brie") {
// if (items[i].name != "Backstage passes to a TAFKAL80ETC concert") {
// if (items[i].quality > 0) {
// if (items[i].name != "Sulfuras, Hand of Ragnaros") {
// items[i].quality = items[i].quality - 1
// }
// }
// } else {
// items[i].quality = items[i].quality - items[i].quality
// }
// } else {
// if (items[i].quality < 50) {
// items[i].quality = items[i].quality + 1
// }
// }
// }
items = items.map { item in
let qualityUpdatedItem = QualityModificationRule.process(item: item)
return SellInModificationRule.process(item: qualityUpdatedItem)
}
}
}

View File

@ -0,0 +1,36 @@
//
// SellInModificationRule.swift
//
//
// Created by Lucas van Dongen on 23/11/2020.
//
import Foundation
enum SellInModificationRule: Rule {
typealias RuleType = Self
case regular, legendary
static func rule(for item: Item) -> RuleType {
switch item.name {
case "Sulfuras, Hand of Ragnaros":
return .legendary
default:
return .regular
}
}
func apply(to item: Item) -> Item {
switch self {
case .regular:
return applyRegularRule(to: item)
case .legendary:
return item // Unmodified
}
}
func applyRegularRule(to item: Item) -> Item {
return Item(name: item.name, sellIn: item.sellIn - 1, quality: item.quality)
}
}

View File

@ -0,0 +1,36 @@
//
// TestQualityModificationRules.swift
// GildedRose
//
// Created by Lucas van Dongen on 23/11/2020.
//
@testable import GildedRose
import XCTest
class TestQualityModificationRules: XCTestCase {
private let regularItem = Item(name: "Regular Item", sellIn: 1, quality: 5)
private let sulfuras = Item(name: "Sulfuras, Hand of Ragnaros", sellIn: 0, quality: 50)
//- All items have a SellIn value which denotes the number of days we have to sell the item
func testRuleSelection() {
XCTAssertEqual(SellInModificationRule.rule(for: regularItem), .regular)
XCTAssertEqual(SellInModificationRule.rule(for: sulfuras), .legendary)
}
func testApplyLegendary() {
let initialSellIn = sulfuras.sellIn
let system = GildedRose(items: [sulfuras])
system.updateQuality()
XCTAssertEqual(system.items.first!.sellIn, initialSellIn)
}
func testApplyRegular() {
let initialSellIn = regularItem.sellIn
let system = GildedRose(items: [regularItem])
system.updateQuality()
XCTAssertLessThan(system.items.first!.sellIn, initialSellIn)
}
}