-
Notifications
You must be signed in to change notification settings - Fork 21
/
clj_test.clj
202 lines (183 loc) · 7.89 KB
/
clj_test.clj
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
(ns matcher-combinators.clj-test
(:require [matcher-combinators.core :as core]
[matcher-combinators.dispatch :as dispatch]
[matcher-combinators.matchers :as matchers]
[matcher-combinators.printer :as printer]
[matcher-combinators.parser]
[matcher-combinators.result :as result]
[clojure.string :as str]
[clojure.test :as clojure.test]))
(defn- stacktrace-file-and-line
[stacktrace]
(if (seq stacktrace)
(let [^StackTraceElement s (first stacktrace)]
{:file (.getFileName s) :line (.getLineNumber s)})
{:file nil :line nil}))
(defn- core-or-this-class-name? [^StackTraceElement stacktrace]
(let [cl-name (.getClassName stacktrace)]
(or (str/starts-with? cl-name "matcher_combinators.clj-test$")
(str/starts-with? cl-name "java.lang."))))
;; had to include this from `clojure.test` because there is no good way to run
;; this logic when not reporting a `:fail` or `:error`.
(defn with-file+line-info [report]
(->> (.getStackTrace (Thread/currentThread))
(drop-while core-or-this-class-name?)
stacktrace-file-and-line
(merge report)))
(defn tagged-for-pretty-printing [actual-summary result]
(with-meta {:summary actual-summary
:match-result result}
{:type ::mismatch}))
(defmethod clojure.test/assert-expr 'match? [msg form]
`(let [args# (list ~@(rest form))
[matcher# actual#] args#]
(cond
(not (= 2 (count args#)))
(clojure.test/do-report
{:type :fail
:message ~msg
:expected (symbol "`match?` expects 2 arguments: a `matcher` and the `actual`")
:actual (symbol (str (count args#) " were provided: " '~form))})
(core/matcher? matcher#)
(let [result# (core/match matcher# actual#)]
(clojure.test/do-report
(if (core/match? result#)
{:type :pass
:message ~msg
:expected '~form
:actual (list 'match? matcher# actual#)}
(with-file+line-info
{:type :fail
:message ~msg
:expected '~form
:actual (tagged-for-pretty-printing (list '~'not (list 'match? matcher# actual#))
result#)}))))
:else
(clojure.test/do-report
{:type :fail
:message ~msg
:expected (str "The first argument of match? needs to be a matcher (implement the match protocol)")
:actual '~form}))))
(defmethod clojure.test/assert-expr 'match-with? [msg form]
(let [args (rest form)
[type->matcher
matcher
actual] args]
(dispatch/match-with-inner
type->matcher
`(cond
(not (= 3 (count '~args)))
(clojure.test/do-report
{:type :fail
:message ~msg
:expected (symbol "`match-with?` expects 3 arguments: a `type->matcher` map, a `matcher`, and the `actual`")
:actual (symbol (str (count '~args) " were provided: " '~form))})
(core/matcher? ~matcher)
(let [result# (core/match ~matcher ~actual)]
(clojure.test/do-report
(if (core/match? result#)
{:type :pass
:message ~msg
:expected '~form
:actual (list 'match? ~matcher ~actual)}
(with-file+line-info
{:type :fail
:message ~msg
:expected '~form
:actual (tagged-for-pretty-printing (list '~'not (list 'match? ~matcher ~actual))
result#)}))))
:else
(clojure.test/do-report
{:type :fail
:message ~msg
:expected (str "The second argument of match-with? needs to be a matcher (implement the match protocol)")
:actual '~form})))))
(defmethod clojure.test/assert-expr 'thrown-match? [msg form]
;; (is (thrown-with-match? exception-class matcher expr))
;; Asserts that evaluating expr throws an exception of class c.
;; Also asserts that the exception data satisfies the provided matcher.
(let [klass (nth form 1)
matcher (nth form 2)
body (nthnext form 3)]
`(try ~@body
(let [args# (list ~@(rest form))]
(if (not (= 3 (count args#)))
(clojure.test/do-report
{:type :fail
:message ~msg
:expected (symbol "`thrown-match?` expects 3 arguments: an exception class, a `matcher`, and the `actual`")
:actual (symbol (str (count args#) " were provided: " '~form))})
(clojure.test/do-report {:type :fail
:message ~msg
:expected '~form
:actual (symbol "the expected exception wasn't thrown")})))
(catch ~klass e#
(let [result# (core/match ~matcher (ex-data e#))]
(clojure.test/do-report
(if (core/match? result#)
{:type :pass
:message ~msg
:expected '~form
:actual (list 'thrown-match? ~klass ~matcher '~body)}
(with-file+line-info
{:type :fail
:message ~msg
:expected '~form
:actual (tagged-for-pretty-printing (list '~'not (list 'thrown-match? ~klass ~matcher '~body))
result#)
:ex-class ~klass}))))
e#))))
(defmethod clojure.core/print-method ::mismatch [{:keys [match-result]} out]
(binding [*out* out]
(printer/pretty-print (::result/value match-result))))
(defn- class->symbol [cls]
(-> cls
str
(clojure.string/replace #"^class " "")
(clojure.string/replace #"^interface " "")
symbol))
(defn build-match-assert
"Allows you to define a custom clojure.test match assert:
`(defmethod clojure.test/assert-expr 'baz? [msg form]
(build-match-assert 'baz? {java.lang.Long greater-than-matcher} msg form))`"
[match-assert-name type->matcher msg form]
(let [args (rest form)
[matcher actual] args
;; no idea why this is needed:
type->matcher' (->> type->matcher
(map (fn [[k v]] [(class->symbol k) v]))
(into {}))]
(dispatch/match-with-inner
type->matcher'
`(cond
(not (= 2 (count '~args)))
(clojure.test/do-report
{:type :fail
:message ~msg
:expected (symbol (str "`" '~match-assert-name "` expects 3 arguments: a `type->matcher` map, a `matcher`, and the `actual`"))
:actual (symbol (str (count '~args) " were provided: " '~form))})
(core/matcher? ~matcher)
(let [result# (core/match ~matcher ~actual)]
(clojure.test/do-report
(if (core/match? result#)
{:type :pass
:message ~msg
:expected '~form
:actual (list 'match? ~matcher ~actual)}
(with-file+line-info
{:type :fail
:message ~msg
:expected '~form
:actual (tagged-for-pretty-printing (list '~'not (list 'match? ~matcher ~actual))
result#)}))))
:else
(clojure.test/do-report
{:type :fail
:message ~msg
:expected (str "The second argument of " '~match-assert-name " needs to be a matcher (implement the match protocol)")
:actual '~form})))))
(defmethod clojure.test/assert-expr 'match-equals? [msg form]
(build-match-assert 'match-equals?
{clojure.lang.IPersistentMap matchers/equals}
msg
form))