/
api.cljc
210 lines (173 loc) · 6.65 KB
/
api.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
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
203
204
205
206
207
208
209
210
(ns unit.nedap.utils.test.api
(:require
#?(:clj [clojure.test :refer [do-report run-tests deftest testing are is use-fixtures]] :cljs [cljs.test :refer-macros [deftest testing is are run-tests] :refer [use-fixtures do-report]])
[clojure.string :as string]
[matcher-combinators.test :refer [match?]]
[nedap.utils.test.impl :as impl]
[nedap.utils.test.api :as sut])
#?(:clj (:import (clojure.lang ExceptionInfo Compiler$CompilerException))))
(defrecord Student [name])
(defrecord School [students])
(deftest simple=
(testing "Assert behaviour of simple="
(are [a b] (sut/simple= a b)
[:a :b :b :c]
#{:a :b :c}
{:key [:c :b :a]}
{:key #{:a :c :b}}
{:nested {:key [:value :other]}}
{:nested {:key #{:other :value}}}
{:students #{{:name "Luna Lovegood"}
{:name "Neville Longbottom"}
{:name "Harry Potter"}}}
(->School [(->Student "Luna Lovegood")
(->Student "Neville Longbottom")
(->Student "Harry Potter")]))))
(deftest meta=
(testing "Basic equality is analog to that of `clojure.core/=`"
(are [a b] (testing [:= a b]
(= (sut/meta= a b)
(= a b)))
"a" "a"
"a" "b"
1 1
1 2
[1 2 3] [1 2 3]
[1 2 3] [1 3 2]
[] []
[] {}
nil 1
nil nil
{} {}
{1 1} {1 1}
{1 1} {1 2}
'a 'a
'a 'b
:a :a
:a :b))
(testing "Metadata-wise equality is different to that of `clojure.core/=`"
(are [a b] (testing [:= a b]
(not= (sut/meta= a b)
(= a b)))
[] ^:a []
[[]] [^:a []]
'a (with-meta 'a {:a true})))
(testing "Metadata-wise equality"
(are [a b expectation] (testing [:= a b]
(= expectation
(sut/meta= a b)))
[] [] true
[] (with-meta [] {}) true
[] ^:a [] false
[[]] [^:a []] false
[1 2 ^:a []] [1 2 ^:a []] true
[1 2 ^:a []] [1 2 ^:b []] false))
(testing "Medatata metadata (and so on, recursively) also accounts for equality"
(are [a b expectation] (testing [:= a b]
(= expectation
(sut/meta= a b)))
^{:thing ^:other []} []
^{:thing ^:other []} []
true
^{:thing ^:other []} []
^{:thing ^:OTHER []} []
false
^{:thing ^{:other ^:final []} []} []
^{:thing ^{:other ^:final []} []} []
true
^{:thing ^{:other ^:final []} []} []
^{:thing ^{:other ^:FINAL []} []} []
false
[^{:thing ^{:other ^:final []} []} []]
[^{:thing ^{:other ^:final []} []} []]
true
[^{:thing ^{:other ^:final []} []} []]
[^{:thing ^{:other ^:FINAL []} []} []]
false)))
(deftest macroexpansion=
(testing "Equality"
(are [a b] (sut/macroexpansion= a b)
1 1
[1 [2]] [1 [2]]
(gensym) (gensym)
[[1 (gensym) 2]] [[1 (gensym) 2]]))
(testing "Inequality"
(are [a b] (not (sut/macroexpansion= a b))
(gensym) 42
(gensym) nil
[[1 (gensym) 2]] [[1 (gensym) 3]])))
(deftest expect
(let [a (atom 0)]
(sut/expect (swap! a inc)
:to-change @a
:from 0
:to 1)
(testing "bodies can span more than one expression"
(sut/expect (swap! a inc)
(swap! a inc)
:to-change @a
:from 1
:to 3)))
(testing "the macroexpansion evaluation"
(let [proof (atom [])]
(sut/expect
(swap! proof conj :body)
:to-change (do (swap! proof conj :to-change) true)
:from (do (swap! proof conj :from) true)
:to (do (swap! proof conj :to) true))
(is (= [:to-change :from :body :to-change :to]
@proof))))
(testing "possible to test metadata"
(let [proof (atom {})]
(sut/expect (swap! proof with-meta {::test true})
:to-change @proof
:from {}
:to ^::test {})
(let [proof (atom ^::test {})]
(sut/expect (swap! proof with-meta {})
:to-change @proof
:from ^::test {}
:to {}))))
(testing "exception in body"
(is (thrown-with-msg? #?(:clj ExceptionInfo :cljs js/Error) #"my special failure"
(sut/expect (throw (ex-info "my special failure" {})) :to-change 0 :from 0 :to 1))))
(testing ":to failures"
(let [test-result (atom {})
a (atom 0)]
(are [form expected] (match? expected
(with-redefs [do-report (partial reset! test-result)]
form
@test-result))
(sut/expect 0 :to-change 0 :from 0 :to 1)
`{:type :fail, :expected (impl/meta= [0 1]), :actual (~'not (impl/meta= [0 1]))}
(sut/expect 0 :to-change {} :from {} :to ^::test {})
`{:type :fail, :expected (impl/meta= [{} {}]), :actual (~'not (impl/meta= [{} {}]))}
(sut/expect (swap! a inc) :to-change @a :from 0 :to 2)
`{:type :fail, :expected (impl/meta= [(deref ~'a) 2]), :actual (~'not (impl/meta= [1 2]))})))
#?(:clj
(testing "macroexpansion-time validation"
(letfn [(assertion-thrown? [assertion form]
(try
(eval form)
false
(catch Compiler$CompilerException e
(or (string/includes? (ex-message (ex-cause e)) assertion)
(throw (ex-cause e))))))]
(testing "asserts correct options"
(are [failure form] (assertion-thrown? failure form)
"#{:to-change :from :to}"
`(sut/expect () :to-tjainge 0 :from 0 :to 1)
"#{:to-change :from :to}"
`(sut/expect () :to-change 0 :from 0 :to 1 :extra :value)
"#{:to-change :from :to}"
`(sut/expect () :missing :keys)
"#{:to-change :from :to}"
`(sut/expect () :unexpected () :signature 4 :keys)
"0 should be different from 0"
`(sut/expect () :to-change 0 :from 0 :to 0)
"{} should be different from {}"
`(sut/expect () :to-change 0 :from ^::wat {} :to ^::wat {})))
(testing "asserts at least one body"
(is (assertion-thrown?
"(seq bodies)"
`(sut/expect :to-change 0 :from 0 :to 1))))))))