Fork me on GitHub
#matcher-combinators2022-11-10
>
ericdallo18:11:53

Merged, thanks!

❤️ 1
thanks 1
borkdude21:11:08

Started using macher-combinator in babashka now as well, for some tests. I needed to compare expected text spanning multiple lines against actual text. I didn't know if mc had something for this, so I just broke everything in lines and then compared a sequence of lines

borkdude21:11:18

I think it would be useful if mc had something like: This is the expected multi-line string and the actual output should at least start with that. And when it doesn't match, don't output the large string blob, but point into the string where it starts to diverge

dchelimsky21:11:55

Is matcher-combinators.matchers/regex insufficient?

dchelimsky21:11:10

Oh - I see the bit about the blob.

borkdude21:11:53

So this is basically my matching logic now:

(defmacro multiline-equals [s1 s2]
  `(let [lines-s1# (str/split-lines ~s1)
         lines-s2# (str/split-lines ~s2)
         max-lines# (max (count lines-s1#) (count lines-s2#))
         lines-s1# (take max-lines# lines-s1#)
         lines-s2# (take max-lines# lines-s2#)]
     (is (~'match? (map str/trimr lines-s1#) (map str/trimr lines-s2#)))
     #_(run! (fn [i]
               (let [l1 (get lines-s1 i)
                     l2 (get lines-s2 i)]
                 (if (and l1 l2)
                   (is (= l1 l2)
                       (format "Lines did not match.\nLine: %s\nLeft:  %s\nRight: %s"
                               i (pr-str l1) (pr-str l2)))
                   (is false (format "Out of lines at line: %s.\nLeft:  %s\nRight: %s"
                                     i (pr-str l1) (pr-str l2))))))
             (range max-lines))))
The commented out thing is what I did before

borkdude21:11:01

(I moved form a function to a macro for better line info in test failures)

borkdude21:11:36

This works already pretty well but it also ties into an issue that @lee mentioned yesterday: If you have two sequences and they start to diverge because there is one element in between, all the elements after are reported as non-matching

borkdude21:11:19

e.g.

[1,2,3,4] ~ [1,1,2,3,4]
everything after the first 1 is dismissed as non-matching, while you could argue that it should only report the second 1 in the last element

lread21:11:17

@borkdude I also use the split lines strategy when using kaocha to get nice diff results.

lread21:11:50

Would be cool to have such a matcher built into matcher-combinators, and it would make sense if it https://github.com/nubank/matcher-combinators/issues/177.