Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns player-ladder.test-util
(:require [clojure.data :as data]
[clojure.pprint :as pp]
[clojure.string :as str]
[clojure.test :refer :all]))
(defn diff->string [diffs]
(str/join
(for [[actual [a b]] diffs
:when (or a b)]
(str
(if a
(str "- "
(with-out-str (pp/pprint a))
" + ")
"+ ")
(if b (with-out-str (pp/pprint b)))))))
(defn diff-map [diffs [m & ms :as more]]
{:diffstrs diffs :actual (if ms more m)})
(remove-method clojure.test/assert-expr '=)
(remove-method clojure.test/assert-expr 'clojure.core/=)
(defn assert-= [msg a more]
(let [test-str (str (list '= a (first more)))]
`(let [a# ~a]
(if-let [more# (seq (list ~@more))]
(let [result# (apply = a# more#)
diffs# (map vector
more#
(map #(take 2 (data/diff a# %))
more#))
diff-str# (diff->string diffs#)]
(if result#
(do-report {:type :pass, :message ~msg,
:expected a#, :actual more#})
(do-report (merge {:type :fail, :message ~msg,
:expected a#, :actual more#,
:test-form ~test-str
:diffs diffs#}
(diff-map diff-str# more#))))
result#)
(throw (Exception. "= expects more than one argument"))))))
(defmethod clojure.test/assert-expr '= [msg [_ a & more]]
(assert-= msg a more))
(defmethod clojure.test/assert-expr 'clojure.core/= [msg [_ a & more]]
(assert-= msg a more))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment