Skip to content

Commit d7763b9

Browse files
authored
Merge pull request #40 from NoahTheDuke/nb/=-impl
Move =? cond to function to reduce generated code
2 parents cfe93bf + 7e703d1 commit d7763b9

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

src/expectations/clojure/test.cljc

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,48 @@
155155
(clojure.string/replace b in-both ""))
156156
"\n"))
157157

158+
(defn ^:no-doc =?-impl
159+
"Perform comparison of expected vs actual, build :expected and :actual forms."
160+
[{:keys [e e# a a# conform?]}]
161+
(cond conform?
162+
[(s/valid? e# a#)
163+
(s/explain-str e# a#)
164+
(list 's/valid? e a)
165+
(list 'not (list 's/valid? e a#))]
166+
(fn? e#)
167+
[(e# a#)
168+
(str a " did not satisfy " e "\n")
169+
(list e a)
170+
(list 'not (list e a#))]
171+
(isa? (type e#)
172+
#?(:clj java.util.regex.Pattern
173+
:cljs (type #"regex")))
174+
[(some? (re-find e# a#))
175+
(str (pr-str a#) " did not match " (pr-str e#) "\n")
176+
(list 're-find e a)
177+
(list 'not (list 're-find e# a#))]
178+
#?(:clj (and (class? e#) (class? a#))
179+
:cljs false) ; maybe figure this out later
180+
[(isa? a# e#) ; (expect parent child)
181+
(str a# " is not derived from " e# "\n")
182+
(list 'isa? a e)
183+
(list 'not (list 'isa? a# e#))]
184+
#?(:clj (class? e#)
185+
:cljs false) ; maybe figure this out later
186+
[(instance? e# a#) ; (expect klazz object)
187+
(str a#
188+
#?(:clj (str " (" (class a#) ")"))
189+
" is not an instance of " e# "\n")
190+
(list 'instance? e a)
191+
#?(:clj (class a#))]
192+
:else
193+
[(= e# a#)
194+
(when (and (string? e#) (string? a#) (not= e# a#))
195+
(let [[_# _# in-both#] (str-diff e# a#)]
196+
(str-msg e# a# in-both#)))
197+
(list '= e a)
198+
(list 'not= e# a#)]))
199+
158200
;; smart equality extension to clojure.test assertion -- if the expected form
159201
;; is a predicate (function) then the assertion is equivalent to (is (e a))
160202
;; rather than (is (= e a)) and we need the type check done at runtime, not
@@ -169,47 +211,9 @@
169211
`(let [e# ~e
170212
a# ~a
171213
f# ~form'
172-
valid?# (when ~conform? s/valid?)
173-
explain-str?# (when ~conform? s/explain-str)
174-
[r# m# ef# af#]
175-
(cond ~conform?
176-
[(valid?# e# a#)
177-
(explain-str?# e# a#)
178-
(list '~'s/valid? '~e '~a)
179-
(list '~'not (list '~'s/valid? '~e a#))]
180-
(fn? e#)
181-
[(e# a#)
182-
(str '~a " did not satisfy " '~e "\n")
183-
(list '~e '~a)
184-
(list '~'not (list '~e a#))]
185-
(isa? (type e#)
186-
#?(:clj java.util.regex.Pattern
187-
:cljs (type #"regex")))
188-
[(some? (re-find e# a#))
189-
(str (pr-str a#) " did not match " (pr-str e#) "\n")
190-
(list '~'re-find '~e '~a)
191-
(list '~'not (list '~'re-find e# a#))]
192-
#?(:clj (and (class? e#) (class? a#))
193-
:cljs false) ; maybe figure this out later
194-
[(isa? a# e#) ; (expect parent child)
195-
(str a# " is not derived from " e# "\n")
196-
(list '~'isa? '~a '~e)
197-
(list '~'not (list '~'isa? a# e#))]
198-
#?(:clj (class? e#)
199-
:cljs false) ; maybe figure this out later
200-
[(instance? e# a#) ; (expect klazz object)
201-
(str a#
202-
#?(:clj (str " (" (class a#) ")"))
203-
" is not an instance of " e# "\n")
204-
(list '~'instance? '~e '~a)
205-
#?(:clj (class a#))]
206-
:else
207-
[(= e# a#)
208-
(when (and (string? e#) (string? a#) (not= e# a#))
209-
(let [[_# _# in-both#] (str-diff e# a#)]
210-
(str-msg e# a# in-both#)))
211-
(list '~'= '~e '~a)
212-
(list '~'not= e# a#)])
214+
[r# m# ef# af#] (=?-impl {:e '~e :e# e#
215+
:a '~a :a# a#
216+
:conform? ~conform?})
213217
humane?# (and humane-test-output? (not (fn? e#)) (not ~conform?))]
214218
(if r#
215219
(t/do-report {:type :pass, :message ~msg,
@@ -218,7 +222,7 @@
218222
a#)})
219223
(t/do-report
220224
{:type :fail,
221-
:message (if m# (if ~msg (str ~msg "\n" m#) m#) ~msg)
225+
:message (if m# (if-let [msg# ~msg] (str msg# "\n" m#) m#) ~msg)
222226
:diffs (if humane?#
223227
[[a# (take 2 (data/diff e# a#))]]
224228
[])
@@ -303,8 +307,8 @@
303307
(let [within (if (and (sequential? e')
304308
(symbol? (first e'))
305309
(= "expect" (name (first e'))))
306-
`(pr-str '~e')
307-
`(pr-str (list '~'expect '~e' '~a)))
310+
(pr-str e')
311+
(pr-str (list 'expect e' a)))
308312
msg' `(str/join
309313
"\n"
310314
(cond-> []
@@ -313,7 +317,7 @@
313317
~(not= e e')
314318
(conj (str " within: " ~within))
315319
:else
316-
(conj (str (pr-str '~a) "\n"))))]
320+
(conj ~(str (pr-str a) "\n"))))]
317321
(cond
318322
(and (sequential? a)
319323
(symbol? (first a))

0 commit comments

Comments
 (0)