nubank / matcher-combinators

Library for creating matcher combinator to compare nested data structures
Other
467 stars 23 forks source link

implement a "mismatch" matcher #158

Closed philomates closed 3 years ago

philomates commented 3 years ago

useful for asserting that something is not in the result: https://github.com/nubank/matcher-combinators/issues/99 for example:

(deftest mismatch
  (testing "predicate not-match"
    (is (match? [1 (m/mismatch odd?) 3]
                [1 2 3])))
  (testing "declarative not-match"
    (is (match? [1 (m/mismatch {:a 1}) 3]
                [1 {:a 2 :b 1} 3])))
  (testing "in-any-order with not-match"
    (is (match? (m/in-any-order
                 [odd? pos? (m/mismatch odd?)])
                [1 2 3]))))

open questions:

for both of these questions I decided to discourage negation matching in the README and the docstring

example failure messages

FAIL in (absent-matcher) (test_test.clj:124)
expected: (match? [1 (m/mismatch {:a 2}) 3] [1 {:a 2} 3])
  actual: [1
 (mismatch "expected mismatch from: {:a 2}" {:a 2})
 3]

FAIL in (absent-pred) (test_test.clj:120)
expected: (match? [1 (m/mismatch even?) 3] [1 2 3])
  actual: [1
 (mismatch
  "expected mismatch from: clojure.core$even_QMARK_@6a0a7d89"
  2)
 3]

mismatch

other

importantly this doesn't conflate exceptions and values like the Midje =not=> does, which has traditionally lead to many uncaught bugs in our test suites:

(facts "=not=> vs mismatch"
  (fact "wHHYYY does this pass?"
    (throw (ex-info "boom" {})) =not=> 1)
  (fact "we sleep well at night knowing this doesn't pass"
    (throw (ex-info "boom" {})) => (match (m/mismatch 1))))