-
Notifications
You must be signed in to change notification settings - Fork 6
/
assertions.cljc
99 lines (87 loc) · 3.24 KB
/
assertions.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(ns fulcro-spec.assertions
#?(:cljs
(:require-macros fulcro-spec.assertions))
(:require
#?(:clj [clojure.test])
cljs.test ;; contains multimethod in clojure file
[clojure.spec.alpha :as s]
#?(:clj
[fulcro-spec.impl.macros :as im])
[fulcro-spec.spec :as fss]))
(s/def ::arrow (comp #{"=>" "=fn=>" "=throws=>"} str))
(s/def ::behavior string?)
(s/def ::triple (s/cat
:actual ::fss/any
:arrow ::arrow
:expected ::fss/any))
(s/def ::block (s/cat
:behavior (s/? ::behavior)
:triples (s/+ ::triple)))
(s/def ::assertions (s/+ ::block))
(defn fn-assert-expr [msg [f arg :as form]]
`(let [arg# ~arg
result# (~f arg#)]
{:type (if result# :pass :fail)
:message ~msg
:assert-type '~'exec
::actual arg#
::expected ~f
:actual (list ~f arg#)
:expected '~form}))
(defn eq-assert-expr [msg [exp act :as form]]
`(let [act# ~act
exp# ~exp
result# (im/try-report ~msg (= exp# act#))]
{:type (if result# :pass :fail)
:message ~msg :assert-type '~'eq
:actual act# :expected exp#}))
(defn assert-expr [msg [disp-key & form]]
(cond
(= '= disp-key) (eq-assert-expr msg form)
(= 'exec disp-key) (fn-assert-expr msg form)
:else {:type :fail
:message msg
:actual (cons disp-key form)
:expected (list #{"exec" "eq"} disp-key)}))
#?(:clj
(defn triple->assertion [cljs? {:keys [actual arrow expected]}]
(let [prefix (if cljs? "cljs.test" "clojure.test")
is (symbol prefix "is")
msg (str (pr-str actual) " " arrow " " (pr-str expected))]
(case arrow
=>
`(~is (~'= ~expected ~actual)
~msg)
=fn=>
(let [checker expected
arg actual]
`(~is (~'exec ~checker ~arg)
~msg))
=throws=>
(let [cls (if cljs? :default Throwable)]
(if (instance? java.util.regex.Pattern expected)
`(~is (~'thrown-with-msg? ~cls ~expected ~actual)
~msg)
`(~is (~'thrown? ~expected ~actual)
~msg)))
(throw (ex-info "invalid arrow" {:arrow arrow}))))))
(defn fix-conform [conformed-assertions]
;;see issue: #31
(if (vector? (second conformed-assertions))
(vec (cons (first conformed-assertions) (second conformed-assertions)))
conformed-assertions))
#?(:clj
(defn block->asserts [cljs? {:keys [behavior triples]}]
(let [asserts (map (partial triple->assertion cljs?) triples)]
`(im/with-reporting ~{:type :behavior :string (if (empty? behavior) "unmarked" behavior)}
~@asserts))))
#?(:clj
(do
(defmethod cljs.test/assert-expr '= [env msg form]
`(cljs.test/do-report ~(assert-expr msg form)))
(defmethod cljs.test/assert-expr 'exec [env msg form]
`(cljs.test/do-report ~(assert-expr msg form)))
(defmethod clojure.test/assert-expr '= [msg form]
`(clojure.test/do-report ~(assert-expr msg form)))
(defmethod clojure.test/assert-expr 'exec [msg form]
`(clojure.test/do-report ~(assert-expr msg form)))))