Skip to content

Preferred idiom for testing exception-throwing code #271

@jgongo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions