forked from niquola/matcho
-
Notifications
You must be signed in to change notification settings - Fork 4
/
core_test.clj
289 lines (216 loc) · 7.1 KB
/
core_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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
(ns matcho.core-test
(:require [clojure.test :refer :all]
[clojure.spec.alpha :as s]
[matcho.core :refer [match match* matcho matcho* to-spec] :as m]))
#_(deftest fail
(match
{:status 500
:body "some logn string with exception"}
{:status 200
:body {:entry [{:resourceType "Patient"
:id #(integer? %)}]}}))
(defn count-4? [xs]
(= 2 (count xs)))
(defn patch-article [patch]
{:status 200
:body (assoc {:article (merge patch
{:text "nice article text"})}
:article-id 1
:meta {:tags ["nature" "bears"]})})
(def patch {:title "Article about bears"
:description "Very good article"})
(def resp (patch-article patch))
(s/def ::str-coll (s/coll-of string?))
(deftest general-patch-test
(let [body (:body resp)]
(is (< (:status resp) 300))
(is (= (:title patch) (get-in body [:article :title])))
(is (= (:description patch) (get-in body [:article :description])))
(is (s/valid? ::str-coll (get-in body [:meta :tags])))))
(deftest matcho-patch-test
(def pattern
{:status #(< % 300)
:body {:article patch
:meta {:tags ::str-coll}}})
(m/assert pattern resp))
(s/def ::pos-coll (s/coll-of pos?))
(m/valid? [int? string?] [1 "test"])
;; => true
(deftest int-str-pair-test
(m/assert [int? string?] [1 "test"]))
(m/explain-data [int? int? string?] [1 "test"])
;; => [{:expected "#function[clojure.core/int?]", :but "test", :path [1]} {:expected "#function[clojure.core/string?--5132]", :but nil, :path [2]}]
;; (deftest int-str-pair-fail-test
;; (m/assert [int? int? string?] [1 "test"]))
;; [{:expected "#function[clojure.core/int?]", :but "test", :path [1]} {:expected "#function[clojure.core/string?--5132]", :but nil, :path [2]}] [1 "test"] [[#function[clojure.core/int?] #function[clojure.core/int?] #function[clojure.core/string?--5132]]]
(deftest dessert-test
(m/dessert [int? int?] [1 "test"]))
;; is ok!
(deftest user-sensitive-data-test
(testing "open-world exposes sensitive data"
(m/assert
{:body
{:username string?
:age int?}}
{:body
{:username "bob"
:age 42
:password "my-password"}}))
(testing "closed-world will catch accidentially exposed password"
(m/dessert
{:body
^:matcho/strict
{:username string?
:age int?}}
{:body
{:username "bob"
:age 42
:password "my-password"}})))
(deftest vector-strict-match
(def vector-123 [1 2 3])
(m/assert [1 2] [1 2 3])
(m/dessert ^:matcho/strict [1 2] [1 2 3])
(m/assert ^:matcho/strict [1 2] [1 2])
;; ^:matcho/strict works only for current element of the pattern and
;; not inherited by nested nodes
(m/assert ^:matcho/strict {:a [1 2]} {:a [1 2 3]}))
(m/valid? {:status 200} {:status 200 :body "ok"})
;; => true
(m/valid? {:status 200 :body string?} {:status 200})
;; => false
(def sample-resp
{:status 200
:body {:total 10
:elems ["some" "string" "elements"]}})
(deftest readme-test
(is (m/valid? pos? 1))
(m/assert 1 1)
(m/assert {:status #(< % 300)
:body #(not (empty? %))}
{:status 200
:body "hello"})
(m/assert ::pos-coll [1 2 3])
(m/assert [{:expected #"conforms.*pos-coll"}]
(m/explain-data ::pos-coll [1 -1 2])))
(deftest spec-like-interface-test
(is (m/valid? [1 2] [1 2 3]))
(is (m/explain-data #(= 3 (count %)) [1 2]))
(is (nil? (m/explain-data [1 2] [1 2 3])))
(m/assert [1 2] [1 2 3]))
(deftest matcho-test
(testing "Matches"
(match 1 1)
(match {:a 1 :b 2} {:a 1})
(match [1] [1])
(match {:a 1 :b 2} {:a 1})
(match {:a 1 :b 2} {:a odd?})
(match {:a 2} {:a pos?})
(match {:a [1 2 3]} {:a #(= 3 (count %))})
(match [1 2 3] [1 2])
(match {:a {:b [{:c 1 :x 5} {:c 2 :x 6}]}}
{:a {:b [{:c 1} {:c 2}]}})
(match {:a [1 2 3]} {:a ::pos-coll})
(match '(1 2 3) [1 2 3])
#_(match {:a {:b [{:c 1 :x 5} {:c 2 :x 6}]}}
{:a {:b #{{:c odd?}}}})
)
(testing "Many predicates"
(match [{:a 1} {:b 2}]
#(= 2 (count %))
[{:a odd?} {:b even?}])
(match (match* [{:a 2} {:b 1}]
#(= 2 (count %))
[{:a odd?} {:b even?}])
[{:path [0 :a]} {:path [1 :b]}]))
(testing "spec integration"
(match [1 2 3] (s/coll-of number?))
(match (match* [1 2 3] (s/coll-of even?))
[{:path []}]))
(testing "Errors"
(match (match* {:a 1} [1 2])
#(not (empty? %)))
(match (match* {:a [1 2 3]} {:a count-4?})
[{:path [:a] :expected #"count" :but [1 2 3]}])
(match (match* {:a [1 -2 3]} {:a ::pos-coll})
[{:path [:a]
:expected "conforms to spec: :matcho.core-test/pos-coll"
:but map?}])
(match (match* {:a 2} {:a 1})
[{:path [:a], :expected 1, :but 2}])
(match (match* {:a 2} {:a neg?})
[{:path [:a], :expected #"neg" :but 2}])
(match (match* {} {:x ""})
[{:path [:x], :expected "", :but nil}] )
(match (match* {:a [1]} {:a [2]})
[{:path [:a 0], :expected 2, :but 1}])
(match (match* {:a {:b "baaa"}}
{:a {:b #"^a"}} )
[{:path [:a :b], :expected "match regexp: ^a", :but "baaa"}])
(match (match* {:a [1 {:c 3}]} {:a [1 {:c 4}]})
[{:path [:a 1 :c], :expected 4, :but 3}])))
(deftest test-matcho
(matcho {:a 1} {:a odd?})
(matcho [1 2 3] (s/coll-of int?))
(matcho {:status 200
:body "hello"}
{:status #(< % 300)
:body #(not (empty? %))})
(matcho
(matcho* {:status 200
:body "hello"}
{:status 404
:body empty?})
[{:path [:status], :val 200 :in [:status]}
{:path [:body] :val "hello"}])
(matcho [1 2 3] [1 2])
(matcho
(matcho* [1 2 3] [1 3])
[{:path [keyword?] :val 2}])
(matcho
(matcho* [{:a 2}]
[{:a odd?}])
[{:path [keyword? :a] :val 2}])
(matcho* [{:a 2}]
[{:a even?}])
(matcho (matcho* {:a -2 :b {:c {:d 5}}}
{:a neg? :b {:c {:d even?}}})
[{:path [:b :c :d]}]))
(def response {:status 200
:body "ok"})
(deftest with-spec-test
(s/def ::status #(= 200 %))
(s/def ::body #(not-empty %))
(s/def ::response (s/keys :req-un [::status ::body]))
(is (s/valid? ::response response)))
(deftest without-spec-test
(m/assert {:status 200 :body not-empty} response))
(deftest strict-match-test
(testing "vector strict mode"
(m/dessert
{:a ^:matcho/strict [1 2 4]}
{:a [1 2 4 5]})
(m/assert
{:a ^:matcho/strict [1 2 4]}
{:a [1 2 4]}))
(testing "map strict mode"
(m/dessert
^:matcho/strict
{:a :b}
{:a :b :c :d})
(m/assert
^:matcho/strict
{:a :b}
{:a :b}))
(testing "non-inheritance of strict mode"
(m/assert
^:matcho/strict
{:a [1 2]}
{:a [1 2 3]})
(m/dessert
^:matcho/strict
{:a ^:matcho/strict [1 2]}
{:a [1 2 3]})
(m/assert
^:matcho/strict
{:a ^:matcho/strict [1 2]}
{:a [1 2]})))