Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move =? cond to function to reduce generated code #40

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 49 additions & 45 deletions src/expectations/clojure/test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,48 @@
(clojure.string/replace b in-both ""))
"\n"))

(defn ^:no-doc =?-impl
"Perform comparison of expected vs actual, build :expected and :actual forms."
[{:keys [e e# a a# conform?]}]
(cond conform?
[(s/valid? e# a#)
(s/explain-str e# a#)
(list 's/valid? e a)
(list 'not (list 's/valid? e a#))]
(fn? e#)
[(e# a#)
(str a " did not satisfy " e "\n")
(list e a)
(list 'not (list e a#))]
(isa? (type e#)
#?(:clj java.util.regex.Pattern
:cljs (type #"regex")))
[(some? (re-find e# a#))
(str (pr-str a#) " did not match " (pr-str e#) "\n")
(list 're-find e a)
(list 'not (list 're-find e# a#))]
#?(:clj (and (class? e#) (class? a#))
:cljs false) ; maybe figure this out later
[(isa? a# e#) ; (expect parent child)
(str a# " is not derived from " e# "\n")
(list 'isa? a e)
(list 'not (list 'isa? a# e#))]
#?(:clj (class? e#)
:cljs false) ; maybe figure this out later
[(instance? e# a#) ; (expect klazz object)
(str a#
#?(:clj (str " (" (class a#) ")"))
" is not an instance of " e# "\n")
(list 'instance? e a)
#?(:clj (class a#))]
:else
[(= e# a#)
(when (and (string? e#) (string? a#) (not= e# a#))
(let [[_# _# in-both#] (str-diff e# a#)]
(str-msg e# a# in-both#)))
(list '= e a)
(list 'not= e# a#)]))

;; smart equality extension to clojure.test assertion -- if the expected form
;; is a predicate (function) then the assertion is equivalent to (is (e a))
;; rather than (is (= e a)) and we need the type check done at runtime, not
Expand All @@ -169,47 +211,9 @@
`(let [e# ~e
a# ~a
f# ~form'
valid?# (when ~conform? s/valid?)
explain-str?# (when ~conform? s/explain-str)
[r# m# ef# af#]
(cond ~conform?
[(valid?# e# a#)
(explain-str?# e# a#)
(list '~'s/valid? '~e '~a)
(list '~'not (list '~'s/valid? '~e a#))]
(fn? e#)
[(e# a#)
(str '~a " did not satisfy " '~e "\n")
(list '~e '~a)
(list '~'not (list '~e a#))]
(isa? (type e#)
#?(:clj java.util.regex.Pattern
:cljs (type #"regex")))
[(some? (re-find e# a#))
(str (pr-str a#) " did not match " (pr-str e#) "\n")
(list '~'re-find '~e '~a)
(list '~'not (list '~'re-find e# a#))]
#?(:clj (and (class? e#) (class? a#))
:cljs false) ; maybe figure this out later
[(isa? a# e#) ; (expect parent child)
(str a# " is not derived from " e# "\n")
(list '~'isa? '~a '~e)
(list '~'not (list '~'isa? a# e#))]
#?(:clj (class? e#)
:cljs false) ; maybe figure this out later
[(instance? e# a#) ; (expect klazz object)
(str a#
#?(:clj (str " (" (class a#) ")"))
" is not an instance of " e# "\n")
(list '~'instance? '~e '~a)
#?(:clj (class a#))]
:else
[(= e# a#)
(when (and (string? e#) (string? a#) (not= e# a#))
(let [[_# _# in-both#] (str-diff e# a#)]
(str-msg e# a# in-both#)))
(list '~'= '~e '~a)
(list '~'not= e# a#)])
[r# m# ef# af#] (=?-impl {:e '~e :e# e#
:a '~a :a# a#
:conform? ~conform?})
humane?# (and humane-test-output? (not (fn? e#)) (not ~conform?))]
(if r#
(t/do-report {:type :pass, :message ~msg,
Expand All @@ -218,7 +222,7 @@
a#)})
(t/do-report
{:type :fail,
:message (if m# (if ~msg (str ~msg "\n" m#) m#) ~msg)
:message (if m# (if-let [msg# ~msg] (str msg# "\n" m#) m#) ~msg)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also evals msg once so it doesn't put a potentially big msg in multiple times (in addition to wasting effort executing it multiple times).

:diffs (if humane?#
[[a# (take 2 (data/diff e# a#))]]
[])
Expand Down Expand Up @@ -303,8 +307,8 @@
(let [within (if (and (sequential? e')
(symbol? (first e'))
(= "expect" (name (first e'))))
`(pr-str '~e')
`(pr-str (list '~'expect '~e' '~a)))
(pr-str e')
(pr-str (list 'expect e' a)))
msg' `(str/join
"\n"
(cond-> []
Expand All @@ -313,7 +317,7 @@
~(not= e e')
(conj (str " within: " ~within))
:else
(conj (str (pr-str '~a) "\n"))))]
(conj ~(str (pr-str a) "\n"))))]
(cond
(and (sequential? a)
(symbol? (first a))
Expand Down