Fork me on GitHub
#matcher-combinators
<
2023-09-26
>
onetom05:09:03

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")

onetom05:09:46

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

onetom05:09:18

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]"))

onetom05:09:12

but at least the output would look alright-ish.

André Camargo18:09:45

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

André Camargo18:09:28

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é Camargo18:09:00

Then you can use it as

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

André Camargo18:09:01

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

onetom10:10:52

Thank you! Looks great at 1st glance.

onetom10:10:46

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!