mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Added sell-in modification rules
This commit is contained in:
parent
44bc9f5718
commit
4e79a86e03
@ -1,35 +1,14 @@
|
|||||||
public class GildedRose {
|
public class GildedRose {
|
||||||
var items:[Item]
|
private(set) var items: [Item]
|
||||||
|
|
||||||
public init(items:[Item]) {
|
public init(items: [Item]) {
|
||||||
self.items = items
|
self.items = items
|
||||||
}
|
}
|
||||||
|
|
||||||
public func updateQuality() {
|
public func updateQuality() {
|
||||||
for i in 0..<items.count {
|
items = items.map { item in
|
||||||
items[i] = QualityModificationRule.process(item: items[i])
|
let qualityUpdatedItem = QualityModificationRule.process(item: item)
|
||||||
|
return SellInModificationRule.process(item: qualityUpdatedItem)
|
||||||
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
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
36
swift/Sources/GildedRose/Rules/SellInModificationRules.swift
Normal file
36
swift/Sources/GildedRose/Rules/SellInModificationRules.swift
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
36
swift/Tests/GildedRoseTests/TestSellInModificationRule.swift
Normal file
36
swift/Tests/GildedRoseTests/TestSellInModificationRule.swift
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user