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")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 outputi'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]"))but at least the output would look alright-ish.
Thank you! Looks great at 1st glance.
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!
Hey Tamás, good? I'd advise you to create a custom matcher for that...
It's worth noticing that I haven't tried that code 🙂 I've copied from another matcher I have and tweaked it for you
Then you can use it as
(deftest includes-d
(is (match? (include-string "d")
"abc")))I expect a much nicer failing test message for the test above