forked from dm3/clojure.java-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.clj
More file actions
241 lines (224 loc) · 10.3 KB
/
test_utils.clj
File metadata and controls
241 lines (224 loc) · 10.3 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(ns java-time.test-utils
(:refer-clojure :exclude [boolean?])
(:require [clojure.math.combinatorics :as comb]
[clojure.test :refer [deftest is]]))
(def boolean? #(or (true? %) (false? %)))
(defn ^:private validate-_is-args [msg]
(assert (string? msg) (str "is: message must be a string: " (pr-str msg))))
(defn ^:dynamic _is [f args msg]
(validate-_is-args msg)
(is (apply f args) msg))
(defn ^:private reorder-vector [v order]
(assert ((every-pred vector?) v order))
(assert (= (count v) (count order)))
(assert (next order))
(assert (apply distinct? order))
(reduce (fn [v' [from to]]
(assoc v' to (nth v from)))
v (map vector (range (count v)) order)))
(deftest ^:private reorder-vector-test
(is (= '[a b c d]
(reorder-vector '[a b c d] [0 1 2 3])))
(is (= '[a b d c]
(reorder-vector '[a b c d] [0 1 3 2])))
(is (= '[d c b a]
(reorder-vector '[a b c d] [3 2 1 0]))))
; aRb => !bRa
; aRb && bRc => !bRa && !cRb && !cRa && aRc
(defn is-asymmetric* [prop R args R-syn args-syn]
{:pre [((every-pred vector?) args args-syn)
(#{:asymmetric :antisymmetric} prop)]
:post [(boolean? %)]}
(let [nargs (count args)
_ (assert (= nargs (count args-syn)))
_ (assert (<= 2 nargs) "Must provide at least 2 arguments")
split-args+syn (fn [args+syn]
[(mapv first args+syn)
(mapv second args+syn)])
args+syn (mapv vector args args-syn)
continue (volatile! true)
_ (doseq [;; for each combination of 2 or more args
nargs (range 2 (inc nargs))
:while @continue
args+syn (map vec (comb/combinations args+syn nargs))
:let [[args args-syn] (split-args+syn args+syn)]
;; (R args...) is true
:while (or (_is R args (pr-str (list* R-syn args-syn)))
(vreset! continue false))
:let [original-order (range nargs)]
order (comb/permutations original-order)
:while @continue
:when (not= order original-order)
:let [[args args-syn] (split-args+syn (reorder-vector args+syn order))]
:while (or (case prop
;; and (R args..) is false for every *other* permutation of args
:asymmetric (_is (complement R) args (pr-str (list 'not (list* R-syn args-syn))))
;; and (R args..) is false for every *other* permutation of args, or one of the
;; out-of-order arguments is equal
:antisymmetric (let [out-of-order-args (loop [out []
order order]
(if (next order)
(let [[l r] order]
(recur (cond-> out
(> l r) (conj [l r]))
(next order)))
out))
_ (assert (seq out-of-order-args))
f (fn [& args]
(let [args (vec args)]
(if (apply R args)
(every? (fn [[l r]]
(= (nth args l)
(nth args r)))
out-of-order-args)
true)))
conjunction-syn (mapv (fn [[l r]]
(list '= (nth args-syn l) (nth args-syn r)))
out-of-order-args)]
(_is f args (pr-str (list 'or (list 'not (list* R-syn args-syn))
(if (= 1 (count conjunction-syn))
(first conjunction-syn)
(list* 'and conjunction-syn)))))))
(vreset! continue false))])]
@continue))
(defmacro is-asymmetric
"With two arguments, tests that (R a b) is true and (R b a) is false.
With three arguments (R a b c):
1. tests the previous property for (R a b), (R b c), and (R a c)
2. tests that (R a b c) is true.
3. tests that these are false: (R a c b), (R b a c), (R b c a), (R c a b), (R c b a).
Similar for four or more arguments."
[[R a b & args :as all]]
(assert (seq? all))
(assert (<= 3 (count all))
(str "Must provide 2 or more arguments: " (pr-str all)))
`(is-asymmetric* :asymmetric
~R (into [~a ~b] ~(vec args))
'~R (into '~[a b] '~(vec args))))
; aRb && bRa => a=b
(defmacro is-antisymmetric
"With two arguments (R a b), tests that:
1. (R a b) is true
2. if (R b a) then a=b
With three arguments (R a b c):
1. tests the previous property for (R a b), (R b c), and (R a c)
2. tests that (R a b c) is true.
3. tests that
- if (R a c b) then b=c
- if (R b a c) then a=b
- if (R b c a) then c=a
- if (R c a b) then c=a
- if (R c b a) then a=b and b=c
Similar for four or more arguments."
[[R a b & args :as all]]
(assert (seq? all))
(assert (<= 3 (count all))
(str "Must provide 2 or more arguments: " (pr-str all)))
`(is-asymmetric* :antisymmetric
~R (into [~a ~b] ~(vec args))
'~R (into '~[a b] '~(vec args))))
(defn ^:private with-expected-results*
[expected-results f]
(assert (even? (count expected-results)))
(let [actual-results (atom [])
_ (binding [_is (fn [f args msg]
(validate-_is-args msg)
(let [res (apply f args)]
(swap! actual-results conj (read-string msg) (if res :pass :fail))
res))]
(f))
actual-results @actual-results]
(or (= expected-results actual-results)
(throw (ex-info (str "Expected result: "
(pr-str expected-results)
"\n Actual result: "
(pr-str actual-results))
{:expected-result expected-results
:actual-results actual-results})))))
(defmacro ^:private with-expected-results
[results & body]
`(with-expected-results* '~results #(do ~@body)))
(defn ^:private is-ex-data* [expected-ex-data f]
(try (f)
(is false (str "No error thrown"))
(catch Exception e
(is (= expected-ex-data (ex-data e))))))
(defmacro ^:private is-ex-data [expected-ex-data body]
`(is-ex-data* ~expected-ex-data #(do ~body)))
(deftest ^:private _is-test
(is (with-expected-results [(< 2 1) :pass]
(_is (constantly true) [] "(< 2 1)")))
(is-ex-data
'{:expected-result [(< 2 1) :fail]
:actual-results [(< 2 1) :pass]}
(with-expected-results [(< 2 1) :fail]
(_is (constantly true) [] "(< 2 1)")))
(is-ex-data
'{:expected-result [(< 2 1) :pass]
:actual-results [(< 2 1) :pass
(< 2 1) :pass]}
(with-expected-results [(< 2 1) :pass]
(_is (constantly true) [] "(< 2 1)")
(_is (constantly true) [] "(< 2 1)"))))
(deftest ^:private is-asymmetric-test
(is (with-expected-results [(< 1 2) :pass
(not (< 2 1)) :pass]
(is-asymmetric (< 1 2))))
(let [a 1
b 2]
(is (with-expected-results [(< a b) :pass
(not (< b a)) :pass]
(is-asymmetric (< a b)))))
(is (with-expected-results [(> 1 2) :fail]
(is-asymmetric (> 1 2))))
(is (with-expected-results [(<= 1 1) :pass
(not (<= 1 1)) :fail]
(is-asymmetric (<= 1 1))))
(is (with-expected-results [(<= 1 1) :pass
(not (<= 1 1)) :fail]
(is-asymmetric (<= 1 1 1 1 1 1))))
(is (with-expected-results
[(< 1 2) :pass
(not (< 2 1)) :pass
(< 1 3) :pass
(not (< 3 1)) :pass
(< 2 3) :pass
(not (< 3 2)) :pass
(< 1 2 3) :pass
(not (< 1 3 2)) :pass
(not (< 2 1 3)) :pass
(not (< 3 1 2)) :pass
(not (< 2 3 1)) :pass
(not (< 3 2 1)) :pass]
(is-asymmetric (< 1 2 3)))))
(deftest ^:private is-antisymmetric-test
(is (with-expected-results [(<= 1 1) :pass
(or (not (<= 1 1)) (= 1 1)) :pass]
(is-antisymmetric (<= 1 1))))
(let [a 1
b 1]
(is (with-expected-results [(<= a b) :pass
(or (not (<= b a)) (= a b)) :pass]
(is-antisymmetric (<= a b)))))
(is (with-expected-results [(<= 1 2) :pass
(or (not (<= 2 1)) (= 1 2)) :pass]
(is-antisymmetric (<= 1 2))))
(is (with-expected-results [(> 1 2) :fail]
(is-antisymmetric (> 1 2))))
(is (with-expected-results [(> 2 1) :pass
(or (not (> 1 2)) (= 2 1)) :pass]
(is-antisymmetric (> 2 1))))
(is (with-expected-results
[(<= 1 2) :pass
(or (not (<= 2 1)) (= 1 2)) :pass
(<= 1 3) :pass
(or (not (<= 3 1)) (= 1 3)) :pass
(<= 2 3) :pass
(or (not (<= 3 2)) (= 2 3)) :pass
(<= 1 2 3) :pass
(or (not (<= 1 3 2)) (= 2 3)) :pass
(or (not (<= 2 1 3)) (= 1 2)) :pass
(or (not (<= 3 1 2)) (= 2 3)) :pass
(or (not (<= 2 3 1)) (= 1 2)) :pass
(or (not (<= 3 2 1)) (and (= 1 2) (= 2 3))) :pass]
(is-antisymmetric (<= 1 2 3)))))