Quick / Nimble

A Matcher Framework for Swift and Objective-C
https://quick.github.io/Nimble/documentation/nimble/
Apache License 2.0
4.79k stars 595 forks source link

Trying to write my own matcher but predicate is not working as I expected #1117

Closed drekka closed 5 months ago

drekka commented 5 months ago

Hi, I'm trying to write my own matcher to catch a specific error. The doco talks about a Matcher but that won't resolve so I looked at the code for .throwsError(...) and based a very simple one on using a Predicate like this:

    // TEST
    func testStringToShort() throws {
        expect(
            try self.parse("ab") { $0.root <- "abc" }
        )
        .to(throwParseFailure(error: .inputTruncated))
    }

// MATCHER
func throwParseFailure<Out>(error _: ParzError) -> Nimble.Predicate<Out> {
    Predicate { actualExpression in
        let message = ExpectationMessage.fail("xxx")
        do {
            _ = try actualExpression.evaluate()
            return PredicateResult(bool: false, message: message)
        } catch ParzError.inputTruncated {
            return PredicateResult(bool: true, message: message)
        } catch {
            return PredicateResult(bool: false, message: message)
        }
    }
}

But I've been having all sorts of issues trying to get this to work. Currently for example, it's executing the expect expression twice and I cannot get it to output any of the custom messages I try and write. But when I look through the code in Nimbles matchers I can't see what I've done wrong.

Any suggestions? And is there any documentation on writing predicate matchers?

drekka commented 5 months ago

My bad, thought I was on the latest Nimble when I had v11 in the package file.