Skip to content

Preferred idiom for testing exception-throwing code #271

Description

@jgongo

Sorry if this is the wrong place to ask this... is there any preferred idiom for testing code that should throw an exception using SwiftCheck?

For example, I have the following pair of tests, and I'd like to make the second one less verbose if possible:

property("UInt values generate valid values") <- forAll { (number: UInt) -> Testable in
    return try! String(number).parseAsConditionalUInt(forElement: "element") == ConditionalUInt.uint(value: number)
}
property("Negative values cause exception") <- forAll { (number: Int) -> Testable in
    (number < 0) ==> {
        do {
            let _ = try String(number).parseAsConditionalUInt(forElement: "element")
            return false
        } catch {
            return error is ParseError<String>
        }
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions