Womble
Womble

Reputation: 5290

Testing assertions in enums using Swift Testing

I have the following example enum:

enum Schedule {
    
    case daily
    
    /// Every nth day.  The interval between days must be > 1 else error.
    case interval(interval: Int)
    
    func description() -> String {
        
        switch self {
            case .daily: return "Daily"
                
            case .interval(let interval):
                switch interval {
                    case ...(1):
                        assertionFailure("Interval must be > 1 day.")
                        return "No good interval"
                        
                    default:
                        return "Good interval"
                }
        }
    }
}

The underlying problem is that I need to sanity-check the value given to the .interval case.

Here, I'm asserting on the invalid argument. Ideally, the error would be caught when an enum is created, but I want to be able to keep Swifty syntax, instead of writing an initializer and hoping that devs remember to use it.

Problem: how do I test this type using Swift Testing?

With XCTesting, we had XCTExpectFailure. How do we handle this situation in Swift Testing? Thanks.

Upvotes: 0

Views: 29

Answers (0)

Related Questions