mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-18 07:51:29 +00:00
commit
476116d783
50
swift/Sources/GildedRose/CustomisedItemFactory.swift
Normal file
50
swift/Sources/GildedRose/CustomisedItemFactory.swift
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 16/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
protocol CustomisedItemFactoryCreator {
|
||||||
|
func getCustomisedItem(item: Item) -> CustomisedItem
|
||||||
|
}
|
||||||
|
|
||||||
|
class CustomisedItemFactory: CustomisedItemFactoryCreator {
|
||||||
|
// Returns the Created Customised Item based on the Item name
|
||||||
|
func getCustomisedItem(item: Item) -> CustomisedItem {
|
||||||
|
switch item.name {
|
||||||
|
case ItemNameConstants.kAgedBrieItem:
|
||||||
|
return AgedBrieItem(item: item)
|
||||||
|
case ItemNameConstants.kBackstagePassesItem:
|
||||||
|
return BackstagePassesItem(item: item)
|
||||||
|
case ItemNameConstants.kSulfurasItem:
|
||||||
|
return SulfurasItem(item: item)
|
||||||
|
default:
|
||||||
|
return StandardItem(item: item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CustomisedItemFactoryWithNewItems: CustomisedItemFactory {
|
||||||
|
// Creates Conjured Item for newly added Conjured Item. For the old items calls the super class function
|
||||||
|
override func getCustomisedItem(item: Item) -> CustomisedItem {
|
||||||
|
switch item.name {
|
||||||
|
case ItemNameConstants.kConjuredItem:
|
||||||
|
return ConjuredItem(item: item)
|
||||||
|
default:
|
||||||
|
return super.getCustomisedItem(item: item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class CustomItemFactoryManager {
|
||||||
|
var itemFactory: CustomisedItemFactoryCreator
|
||||||
|
public init(customItemFactory: CustomisedItemFactoryCreator) {
|
||||||
|
self.itemFactory = customItemFactory
|
||||||
|
}
|
||||||
|
func getCustomisedItem(item: Item) -> CustomisedItem {
|
||||||
|
return itemFactory.getCustomisedItem(item: item)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,54 +6,7 @@ public class GildedRose {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func updateQuality() {
|
public func updateQuality() {
|
||||||
for i in 0..<items.count {
|
let itemFactoryManager = CustomItemFactoryManager(customItemFactory: CustomisedItemFactoryWithNewItems())
|
||||||
if (items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert") {
|
_ = items.map({ itemFactoryManager.getCustomisedItem(item: $0).updateItemState()})
|
||||||
if (items[i].quality > 0) {
|
|
||||||
if (items[i].name != "Sulfuras, Hand of Ragnaros") {
|
|
||||||
items[i].quality = items[i].quality - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1
|
|
||||||
|
|
||||||
if (items[i].name == "Backstage passes to a TAFKAL80ETC concert") {
|
|
||||||
if (items[i].sellIn < 11) {
|
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items[i].sellIn < 6) {
|
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
swift/Sources/GildedRose/GlidedRoseConstants.swift
Normal file
20
swift/Sources/GildedRose/GlidedRoseConstants.swift
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 16/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct ItemNameConstants {
|
||||||
|
static let kAgedBrieItem: String = "Aged Brie"
|
||||||
|
static let kBackstagePassesItem: String = "Backstage passes to a TAFKAL80ETC concert"
|
||||||
|
static let kSulfurasItem: String = "Sulfuras, Hand of Ragnaros"
|
||||||
|
static let kConjuredItem: String = "Conjured Mana Cake"
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ValueConstants {
|
||||||
|
static let kHightestQualityValue = 50
|
||||||
|
static let kLowestQualityValue = 0
|
||||||
|
}
|
||||||
30
swift/Sources/GildedRose/Items/AgedBrieItem.swift
Normal file
30
swift/Sources/GildedRose/Items/AgedBrieItem.swift
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 16/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct AgedBrieItem: CustomisedItem, ItemStateUpdater{
|
||||||
|
|
||||||
|
var item: Item
|
||||||
|
|
||||||
|
private var isItemUnderHighestQuality: Bool {
|
||||||
|
return item.quality < ValueConstants.kHightestQualityValue
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(item: Item) {
|
||||||
|
self.item = item
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateItemState() {
|
||||||
|
// update the sell in days. Reduce the Sell In days by 1
|
||||||
|
self.updateSellInDays()
|
||||||
|
|
||||||
|
// Increment the Item quality by 1 if the quality is less than 50
|
||||||
|
guard isItemUnderHighestQuality else { return }
|
||||||
|
increaseItemQuality(by: 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
51
swift/Sources/GildedRose/Items/BackstagePassesItem.swift
Normal file
51
swift/Sources/GildedRose/Items/BackstagePassesItem.swift
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 16/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct BackstagePassesItem: CustomisedItem, ItemStateUpdater {
|
||||||
|
|
||||||
|
var item: Item
|
||||||
|
|
||||||
|
private var isItemUnderHighestQuality: Bool {
|
||||||
|
return item.quality < ValueConstants.kHightestQualityValue
|
||||||
|
}
|
||||||
|
|
||||||
|
private var isSellInDatePassed: Bool{
|
||||||
|
return item.sellIn < 0
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(item: Item) {
|
||||||
|
self.item = item
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateItemState() {
|
||||||
|
// Reduce the Sell in days by 1
|
||||||
|
updateSellInDays()
|
||||||
|
|
||||||
|
// If the sell in date is passed, sets the quality of item to 0
|
||||||
|
guard !isSellInDatePassed else {
|
||||||
|
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// If the Quality of item is above 50, return
|
||||||
|
guard isItemUnderHighestQuality else {
|
||||||
|
setItemQuality(to: ValueConstants.kHightestQualityValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch item.sellIn {
|
||||||
|
case 10...:
|
||||||
|
increaseItemQuality(by: 1) // If the sell in days is more than 10, increase quality by 1
|
||||||
|
case 5..<10:
|
||||||
|
increaseItemQuality(by: 2) // If sell in days is between 5 and 10, increase quality by 2
|
||||||
|
case 0..<5:
|
||||||
|
increaseItemQuality(by: 3) // If sell in days is between 0 and 5, increase quality by 3
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
swift/Sources/GildedRose/Items/ConjuredItem.swift
Normal file
18
swift/Sources/GildedRose/Items/ConjuredItem.swift
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 18/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class ConjuredItem: StandardItem{
|
||||||
|
// Overrides decreasingQualityValueBeforeSellInDate property of Standard item as the Conjured Item degrades twice fast than Standard Iten
|
||||||
|
override var decreasingQualityValueBeforeSellInDate: Int {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
override init(item: Item) {
|
||||||
|
super.init(item: item)
|
||||||
|
}
|
||||||
|
}
|
||||||
13
swift/Sources/GildedRose/Items/CustomisedItem.swift
Normal file
13
swift/Sources/GildedRose/Items/CustomisedItem.swift
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 17/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
protocol CustomisedItem {
|
||||||
|
var item: Item {get set}
|
||||||
|
func updateItemState()
|
||||||
|
}
|
||||||
45
swift/Sources/GildedRose/Items/StandardItem.swift
Normal file
45
swift/Sources/GildedRose/Items/StandardItem.swift
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 16/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class StandardItem: CustomisedItem, ItemStateUpdater {
|
||||||
|
var item: Item
|
||||||
|
|
||||||
|
private var isSellInDatePassed: Bool{
|
||||||
|
return item.sellIn < 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var decreasingQualityValueBeforeSellInDate: Int {
|
||||||
|
return 1 // Quality degrades by 1 if the sellIn date is not passed
|
||||||
|
}
|
||||||
|
private var decreasingQualityValueAfterSellInDate: Int {
|
||||||
|
return 2 * decreasingQualityValueBeforeSellInDate // Quality degrades twice after SellIn date is passed
|
||||||
|
}
|
||||||
|
|
||||||
|
private var isItemMoreThanLowestQuality: Bool {
|
||||||
|
return item.quality > ValueConstants.kLowestQualityValue
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(item: Item) {
|
||||||
|
self.item = item
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateItemState() {
|
||||||
|
// Reduce the sellIn days for Item by 1
|
||||||
|
updateSellInDays()
|
||||||
|
|
||||||
|
// Reduce the item quality by 1 , if the sell in date is passed decrement by double the value
|
||||||
|
isSellInDatePassed ? reduceItemQuality(by: decreasingQualityValueAfterSellInDate) : reduceItemQuality(by: decreasingQualityValueBeforeSellInDate)
|
||||||
|
|
||||||
|
guard isItemMoreThanLowestQuality else {
|
||||||
|
// Sets the quality to zero if the quality is negative
|
||||||
|
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
swift/Sources/GildedRose/Items/SulfurasItem.swift
Normal file
20
swift/Sources/GildedRose/Items/SulfurasItem.swift
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 16/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct SulfurasItem: CustomisedItem {
|
||||||
|
var item: Item
|
||||||
|
|
||||||
|
public init(item: Item) {
|
||||||
|
self.item = item
|
||||||
|
}
|
||||||
|
func updateItemState() {
|
||||||
|
// No code as there is no change in quality or sell in days of sulfuras item
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
33
swift/Sources/GildedRose/Protocols/ItemQualityUpdater.swift
Normal file
33
swift/Sources/GildedRose/Protocols/ItemQualityUpdater.swift
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 17/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
protocol ItemQualityUpdater: CustomisedItem {
|
||||||
|
func reduceItemQuality(by value:Int)
|
||||||
|
func increaseItemQuality(by value:Int)
|
||||||
|
func setItemQuality(to value: Int)
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ItemQualityUpdater {
|
||||||
|
// Reduces the item Quality by the value passed as parameter
|
||||||
|
func reduceItemQuality(by value:Int) {
|
||||||
|
item.quality -= value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increases Item Quality by the value passed as parameter
|
||||||
|
func increaseItemQuality(by value:Int) {
|
||||||
|
item.quality += value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sets the Item Quality to the value passed as parameter
|
||||||
|
func setItemQuality(to value: Int){
|
||||||
|
item.quality = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typealias ItemStateUpdater = ItemSellInUpdater & ItemQualityUpdater
|
||||||
24
swift/Sources/GildedRose/Protocols/ItemSellInUpdater.swift
Normal file
24
swift/Sources/GildedRose/Protocols/ItemSellInUpdater.swift
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 17/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
protocol ItemSellInUpdater: CustomisedItem{
|
||||||
|
var decreasingNumberOfSellInDays: Int { get }
|
||||||
|
func updateSellInDays()
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ItemSellInUpdater {
|
||||||
|
var decreasingNumberOfSellInDays: Int {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reduces the sell in days by 1
|
||||||
|
func updateSellInDays() {
|
||||||
|
item.sellIn -= decreasingNumberOfSellInDays
|
||||||
|
}
|
||||||
|
}
|
||||||
47
swift/Tests/GildedRoseTests/AgedBrieItemTests.swift
Normal file
47
swift/Tests/GildedRoseTests/AgedBrieItemTests.swift
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 17/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
@testable import GildedRose
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
class AgedBrieItemTests: XCTestCase {
|
||||||
|
// MARK: - Test Cases for Aged Brie Items
|
||||||
|
func testAgedBrieItems() {
|
||||||
|
let items = [
|
||||||
|
Item(name: "Aged Brie", sellIn: 10, quality: 20),
|
||||||
|
Item(name: "Aged Brie", sellIn: 2, quality: 0)]
|
||||||
|
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
|
||||||
|
let days = 2
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(8, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(0, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(22, app.items[0].quality)
|
||||||
|
XCTAssertEqual(2, app.items[1].quality)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAgedBrieItemsWithQualityMoreThanFifty() {
|
||||||
|
let items = [Item(name: "Aged Brie", sellIn: 5, quality: 49)]
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
let days = 4
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(1, app.items[0].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(50, app.items[0].quality) // Quality of Aged Brie Item cannot be more than 50
|
||||||
|
}
|
||||||
|
}
|
||||||
72
swift/Tests/GildedRoseTests/BackstagePassesItemTests.swift
Normal file
72
swift/Tests/GildedRoseTests/BackstagePassesItemTests.swift
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 17/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
@testable import GildedRose
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
class BackstagePassesItemTests: XCTestCase {
|
||||||
|
// MARK: - Test Cases for Backstage Passes Items
|
||||||
|
func testBackstagePassesItems() {
|
||||||
|
let items = [
|
||||||
|
Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 15, quality: 20),
|
||||||
|
Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 6, quality: 30),
|
||||||
|
Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 7, quality: 28)]
|
||||||
|
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
|
||||||
|
let days = 6
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(9, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(0, app.items[1].sellIn)
|
||||||
|
XCTAssertEqual(1, app.items[2].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(27, app.items[0].quality)
|
||||||
|
XCTAssertEqual(47, app.items[1].quality)
|
||||||
|
XCTAssertEqual(44, app.items[2].quality)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testBackstagePassesItemAfterSellInDatePassed() {
|
||||||
|
let items = [
|
||||||
|
Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 5, quality: 49),
|
||||||
|
Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 4, quality: 30)]
|
||||||
|
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
|
||||||
|
let days = 6
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(-1, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(-2, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(0, app.items[0].quality) // Quality of Backstage passes item is 0 after the sell in date is passed
|
||||||
|
XCTAssertEqual(0, app.items[1].quality)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testBackstagePassesItemsWithQualityMoreThanFifty() {
|
||||||
|
let items = [ Item(name: "Backstage passes to a TAFKAL80ETC concert", sellIn: 10, quality: 49)]
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
let days = 3
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(7, app.items[0].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(50, app.items[0].quality) // Quality of Backstage passes item cannot be more than 50
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
76
swift/Tests/GildedRoseTests/ConjuredItemTests.swift
Normal file
76
swift/Tests/GildedRoseTests/ConjuredItemTests.swift
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 17/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
@testable import GildedRose
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
class ConjuredItemTests: XCTestCase {
|
||||||
|
// MARK: -Test Cases for Conjured Items
|
||||||
|
|
||||||
|
func testConjuredItems() {
|
||||||
|
let items = [
|
||||||
|
Item(name: "Conjured Mana Cake", sellIn: 3, quality: 50),
|
||||||
|
Item(name: "Conjured Mana Cake", sellIn: 2, quality: 3)]
|
||||||
|
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
|
||||||
|
let days = 2
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(1, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(0, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(46, app.items[0].quality)
|
||||||
|
XCTAssertEqual(0, app.items[1].quality)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testConjuredItemsAfterSellInDatePassed() {
|
||||||
|
let items = [ Item(name: "Conjured Mana Cake", sellIn: 0, quality: 25),
|
||||||
|
Item(name: "Conjured Mana Cake", sellIn: 1, quality: 27),
|
||||||
|
]
|
||||||
|
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
|
||||||
|
let days = 2
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(-2, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(-1, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(17, app.items[0].quality) //Quality of Conjured Items degrade twice as normal item
|
||||||
|
XCTAssertEqual(21, app.items[1].quality)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testConjuredItemsWithQualityBelowZero() {
|
||||||
|
let items = [ Item(name: "Conjured Mana Cake", sellIn: 1, quality: 2),
|
||||||
|
Item(name: "Conjured Mana Cake", sellIn: -1, quality: 3),
|
||||||
|
]
|
||||||
|
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
|
||||||
|
let days = 2
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(-1, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(-3, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(0, app.items[0].quality) //Quality of Conjured Items cannot be negative
|
||||||
|
XCTAssertEqual(0, app.items[1].quality)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,9 +7,13 @@ class GildedRoseTests: XCTestCase {
|
|||||||
let items = [Item(name: "foo", sellIn: 0, quality: 0)]
|
let items = [Item(name: "foo", sellIn: 0, quality: 0)]
|
||||||
let app = GildedRose(items: items);
|
let app = GildedRose(items: items);
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
XCTAssertEqual("fixme", app.items[0].name);
|
XCTAssertEqual("foo", app.items[0].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static var allTests : [(String, (GildedRoseTests) -> () throws -> Void)] {
|
static var allTests : [(String, (GildedRoseTests) -> () throws -> Void)] {
|
||||||
return [
|
return [
|
||||||
("testFoo", testFoo),
|
("testFoo", testFoo),
|
||||||
|
|||||||
65
swift/Tests/GildedRoseTests/StandardItemTests.swift
Normal file
65
swift/Tests/GildedRoseTests/StandardItemTests.swift
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 17/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
@testable import GildedRose
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
class StandardItemTests: XCTestCase {
|
||||||
|
// MARK: - Test Cases for Standard Items
|
||||||
|
func testStandardItemsForSellInDatePassed() {
|
||||||
|
let items = [ Item(name: "Elixir of the Mongoose", sellIn: 1, quality: 7),
|
||||||
|
Item(name: "+5 Dexterity Vest", sellIn: 0, quality: 10)
|
||||||
|
]
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
let days = 3
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(-2, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(-3, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(2, app.items[0].quality) // Item degrades twice in quality after sell date passed
|
||||||
|
XCTAssertEqual(4, app.items[1].quality) // Quality never negative
|
||||||
|
}
|
||||||
|
|
||||||
|
func testStandardItemsWithQualityBelowZero() {
|
||||||
|
let items = [Item(name: "+5 Dexterity Vest", sellIn: 3, quality: 1),
|
||||||
|
Item(name: "Elixir of the Mongoose", sellIn: 0, quality: 1)]
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
let days = 2
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(1, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(-2, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(0, app.items[0].quality) // Quality never negative
|
||||||
|
XCTAssertEqual(0, app.items[1].quality)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testStandardItemsForSellInDateNotPassed() {
|
||||||
|
let items = [
|
||||||
|
Item(name: "+5 Dexterity Vest", sellIn: 10, quality: 20),
|
||||||
|
Item(name: "Elixir of the Mongoose", sellIn: 5, quality: 7)]
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
let days = 2
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(8, app.items[0].sellIn)
|
||||||
|
XCTAssertEqual(3, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(18, app.items[0].quality)
|
||||||
|
XCTAssertEqual(5, app.items[1].quality)
|
||||||
|
}
|
||||||
|
}
|
||||||
32
swift/Tests/GildedRoseTests/SulfurasItemTests.swift
Normal file
32
swift/Tests/GildedRoseTests/SulfurasItemTests.swift
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 17/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
@testable import GildedRose
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
class SulfurasItemTests: XCTestCase {
|
||||||
|
// MARK: - Test Cases for Sulfuras Items
|
||||||
|
func testSulfurasItem() {
|
||||||
|
let items = [ Item(name: "Sulfuras, Hand of Ragnaros", sellIn: 0, quality: 80),
|
||||||
|
Item(name: "Sulfuras, Hand of Ragnaros", sellIn: -1, quality: 80) ]
|
||||||
|
|
||||||
|
let app = GildedRose(items: items)
|
||||||
|
|
||||||
|
let days = 2
|
||||||
|
for _ in 0..<days {
|
||||||
|
app.updateQuality()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sellIn value
|
||||||
|
XCTAssertEqual(0, app.items[0].sellIn) // Sulfuras Item is never sold. No change in Sell In
|
||||||
|
XCTAssertEqual(-1, app.items[1].sellIn)
|
||||||
|
|
||||||
|
// quality value
|
||||||
|
XCTAssertEqual(80, app.items[0].quality) // Sulfuras Item never changes the quality
|
||||||
|
XCTAssertEqual(80, app.items[1].quality)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user