Я пытаюсь написать XCTest для проверки сравнения с соответствующими значениями в перечислении.
Пример:
enum MatchType : Equatable {
case perfectMatch(Int, Int)
case lowerMatch(Int, Int)
case higherMatch(Int, Int)
}
extension MatchType {
static func == (lhs: MatchType, rhs: MatchType) -> Bool {
switch (lhs, rhs) {
case (.perfectMatch, .perfectMatch):
return true
case (.lowerMatch, .lowerMatch):
return true
case (.higherMatch, .higherMatch):
return true
default:
return false
}
}
}
Как мне сделать сравнение, чтобы убедиться, что правильное перечисление не зная точно, что такое Ints?
В моем тесте я делаю что-то вроде этого:
func testPerfectMatch() {
let orders = [6]
let units = 6
let handler = SalesRuleHandler(orders: orders, units: units)
XCTAssertEqual(handler.matchType!, MatchType.perfectMatch(0, 0))
}
SalesRuleHandler
решает, вернуть ли идеальное совпадение, более низкое совпадение или более высокое совпадение с перечислением,
class SalesRuleHandler {
private var orders: [Int]
private var units: Int
var matchType: MatchType?
init(orders: [Int], units: Int) {
self.orders = orders
self.units = units
self.matchType = self.handler()
}
private func handler() -> MatchType? {
let rule = SalesRules(orders)
if let match = rule.perfectMatch(units) {
print("Found perfect match for: \(units) in orders \(rule.orders) at index: \(match.0) which is the value \(match.1)")
return MatchType.perfectMatch(match.0, match.1)
}
else {
if let match = rule.lowerMatch(units) {
print("Found lower match for: \(units) in orders \(rule.orders) at index: \(match.0) which is the value \(match.1)")
return MatchType.lowerMatch(match.0, match.1)
}
else {
if let match = rule.higherMatch(units) {
return MatchType.higherMatch(match.0, match.1)
}
}
}
return nil
}
}
Я пытаюсь сделать следующее:
Если я прокормлю класс в некоторых orders
и units
, я смогу проверить, было ли matchType
perfect
, lower
или higher
.
Однако в моем тесте мне нужно написать что-то вроде:
XCTAssertEqual(handler.matchType!, MatchType.perfectMatch(0, 0))
А где (0,0) я положил индекс и вернул значение.
Можно ли сравнить перечисление, не зная конкретных чисел?