Fork me on GitHub
#lambdaisland
<
2023-06-08
>
plexus16:06:14

@itai The thing to realize is that ddiff/pretty-print does actually printing, whereas you seem to be expecting that it returns a string. That's a bit more involved, something like this:

(ns repl-sessions.poke
  (:require
   [lambdaisland.deep-diff2.puget.printer :as ddiff-printer]
   [lambdaisland.deep-diff2.printer-impl :as ddiff-printer-impl]
   [lambdaisland.deep-diff2 :as ddiff]
   [clojure.test :refer :all]))

(defn ddiff->str [expected actual]
  (ddiff-printer/render-str
   (ddiff/printer)
   (ddiff/minimize (ddiff/diff expected actual))))

plexus16:06:35

now you can do

(let [expected {:a 1 :b 2 :c 3}
      actual {:a 1 :b 2 :c 4}]
  (is (submap? expected actual)
      (ddiff->str expected actual)))

plexus16:06:05

or (this is getting close to dark arts), you can override how clojure.test handles the 'submap? predicate

plexus16:06:22

(defmethod clojure.test/assert-expr 'submap? [msg form]
  (let [[_ expected actual] form]
    `(let [exp# ~expected
           act# ~actual]
       (if (submap? exp# act#)
         (do-report {:type :pass, :message ~msg,
                     :expected '~form, :actual (cons 'submap? exp# act#)})
         (do-report {:type :fail, :message (ddiff->str exp# act#)
                     :expected '~form, :actual (list '~'not (list '~'submap? exp# act#))})))))

plexus16:06:37

Now if you use (is (submap? ...)) it'll automatically print the diff

plexus16:06:50

(let [expected {:a 1 :b 2 :c 3}
      actual {:a 1 :b 2 :c 4}]
  (is (submap? expected actual)))

plexus16:06:30

but like @lee said matcher-combinators is the way to go. You should be able to add :diff-style :minimal to your kaocha config in order to reduce the diff size.