This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-09-26
Channels
- # aleph (3)
- # announcements (6)
- # babashka (14)
- # beginners (8)
- # biff (16)
- # calva (4)
- # cider (7)
- # clj-kondo (8)
- # cljs-dev (26)
- # clojure (65)
- # clojure-austin (2)
- # clojure-brasil (1)
- # clojure-europe (35)
- # clojure-nl (4)
- # clojure-norway (45)
- # clojure-portugal (1)
- # clojure-uk (9)
- # clr (5)
- # community-development (6)
- # conjure (1)
- # cursive (3)
- # events (8)
- # fulcro (1)
- # honeysql (19)
- # hyperfiddle (31)
- # introduce-yourself (1)
- # lsp (7)
- # matcher-combinators (10)
- # off-topic (17)
- # practicalli (1)
- # ring (30)
- # shadow-cljs (6)
- # testing (2)
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]"))
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
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!