简体   繁体   中英

Swift - How to compare enum with associated values?

I am attempting to write a XCTest to validate a comparison against associated values in an enum.

Example:

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
        }
    }
}

How do I do a comparison to ensure that the correct enum is without knowing specifically what the Ints are?

In my test I do something like this:

func testPerfectMatch() {
        let orders = [6]
        let units = 6

        let handler = SalesRuleHandler(orders: orders, units: units)

        XCTAssertEqual(handler.matchType!, MatchType.perfectMatch(0, 0))
    }

The SalesRuleHandler decides whether to return a perfect match, lower match, or higher match with the enum,

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
}

}

What I'm trying to do is:

If I feed the class in some orders and units I should be able to test whether the matchType was perfect , lower or higher .

However, in my test I'm having to write something like:

XCTAssertEqual(handler.matchType!, MatchType.perfectMatch(0, 0))

And where (0,0) I put in the index and the value returned.

Is it possible to make a comparison on the enum without knowing the specific numbers?

You can use case to access the associated values of an enum.

switch (lhs, rhs) {
case (.perfectMatch(let a, let b), .perfectMatch(let c, let d):
    // check equality of associated values
    return a == c && b == d
// other cases...
}

You can also access associated values like this using an if statement:

if case .perfectMatch(let a, let b) = handler.matchType {
    // do something with a and b
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM