matcher-combinators

onetom 2023-09-26T05:05:03.834399Z

I'm trying assert the presence of a substring in a string. When such a test fails, how can I get the substring appear in the expected part of the failure message? eg, expected appears as (pred [matcher-combinators.matchers/pred]):

(is (match? #(str/includes? % "x") "asd"))

FAIL in () (microsoft_excel_test.clj:208)
expected: (match? (fn* [p1__12987#] (str/includes? p1__12987# "x")) "asd")
  actual: (mismatch
 (expected (pred [matcher-combinators.matchers/pred]))
 (actual "asd"))
instead of, let's say #(str/includes? % "x")

onetom 2023-09-26T05:08:46.525469Z

i tried to use the off-the-shelf regex matching, but that would require quoting the substring:

(is (match? (-> "[123]" java.util.regex.Pattern/quote re-pattern) "asd[1234]"))

FAIL in () (microsoft_excel_test.clj:209)
expected: (match? (-> "[123]" java.util.regex.Pattern/quote re-pattern) "asd[1234]")
  actual: (mismatch
 (expected #"\Q[123]\E")
 (actual "asd[1234]"))
but at least the expected value show up within the (expected ...) part of the output

onetom 2023-09-26T05:11:18.934389Z

i'm notice, that in recent versions, there is a 2-arity version of m/pred, but using that would require me to state the expected value twice:

(is (match? (m/pred #(str/includes? % "[123]") "[123]") "asd[1234]"))

FAIL in () (microsoft_excel_test.clj:212)
expected: (match? (m/pred (fn* [p1__13143#] (str/includes? p1__13143# "[123]")) "[123]") "asd[1234]")
  actual: (mismatch (expected "[123]") (actual "asd[1234]"))

onetom 2023-09-26T05:12:12.673289Z

but at least the output would look alright-ish.

onetom 2023-10-02T10:48:52.395819Z

Thank you! Looks great at 1st glance.

onetom 2023-10-02T10:51:46.152939Z

makes me wonder if it would be possible to solve this in a more generic way... it requires quite some familiarity with the matcher-combinator protocols to whip up a custom one. i still feel like im missing something, because it should be simpler. anyway, in the meantime, i will use your implementation!

André Camargo 2023-09-26T18:00:45.278249Z

Hey Tamás, good? I'd advise you to create a custom matcher for that...

André Camargo 2023-09-26T18:01:28.266259Z

It's worth noticing that I haven't tried that code 🙂 I've copied from another matcher I have and tweaked it for you

André Camargo 2023-09-26T18:03:00.358179Z

Then you can use it as

(deftest includes-d
  (is (match? (include-string "d")
              "abc")))

André Camargo 2023-09-26T18:04:01.020699Z

I expect a much nicer failing test message for the test above