generated from mvc-works/calcit-nodejs-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cljs
231 lines (214 loc) · 7.46 KB
/
test.cljs
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
(ns lilac.test
(:require [cljs.test :refer [deftest is testing run-tests]]
[lilac.core
:refer
[validate-lilac
deflilac
optional+
keyword+
boolean+
number+
string+
custom+
vector+
list+
record+
map+
not+
and+
set+
nil+
or+
is+
register-custom-rule!]]
[lilac.router :refer [lilac-router+ router-data]]))
(defn =ok [x obj] (= x (:ok? obj)))
(deflilac lilac-good-number+ (n) (number+ {:min n}))
(deftest
test-and
(testing
"and number"
(is (=ok true (validate-lilac 10 (and+ [(number+) (number+ {:min 0})])))))
(testing
"string not number"
(is (=ok false (validate-lilac 10 (and+ [(number+) (string+)]))))))
(deftest
test-boolean
(testing "true is boolean" (is (=ok true (validate-lilac true (boolean+)))))
(testing "false is boolean" (is (=ok true (validate-lilac false (boolean+)))))
(testing "nil is no a boolean" (is (=ok false (validate-lilac nil (boolean+)))))
(testing "string is no a boolean" (is (=ok false (validate-lilac "x" (boolean+))))))
(deftest
test-component-args
(testing "number 10 > 8" (is (=ok true (validate-lilac 10 (lilac-good-number+ 8)))))
(testing "number 10 not > 18" (is (=ok false (validate-lilac 10 (lilac-good-number+ 18))))))
(deftest
test-custom
(let [method-1 (fn [x]
(if (and (> x 10) (< x 20))
{:ok? true}
{:ok? false, :message (str "expects number between 10 amd 20, got " x)}))]
(testing
"validating number with custom function"
(is (=ok true (validate-lilac 11 (custom+ method-1)))))
(testing
"validating number with custom function"
(is (=ok false (validate-lilac 21 (custom+ method-1))))))
(let [validate-method-2 (fn [data rule coord]
(if (and (> data 10) (< data 20))
{:ok? true}
{:ok? false,
:data data,
:rule rule,
:coord coord,
:message (str "expects number between 10 amd 20, got " data)}))
method-2+ (fn [] {:lilac-type :method-2})]
(register-custom-rule! :method-2 validate-method-2)
(testing
"validating number with custom function"
(is (=ok true (validate-lilac 11 (method-2+)))))
(testing
"validating number with custom function"
(is (=ok false (validate-lilac 21 (method-2+)))))))
(deftest
test-list
(testing
"a list of boolean"
(is (=ok true (validate-lilac (list true true false) (list+ (boolean+))))))
(testing "a empty list" (is (=ok true (validate-lilac (list) (list+ (boolean+))))))
(testing "nil is not a list" (is (=ok false (validate-lilac nil (list+ (boolean+))))))
(testing
"a list of string is not list of boolean"
(is (=ok false (validate-lilac (list "true" "false") (list+ (boolean+))))))
(testing
"vector is not a empty vector"
(is (=ok false (validate-lilac [] (list+ (boolean+))))))
(testing
"boolean is not a empty vector"
(is (=ok false (validate-lilac false (list+ (boolean+)))))))
(deftest
test-map
(testing
"a map of strings"
(is (=ok true (validate-lilac {"a" "a", "b" "b"} (map+ (string+) (string+))))))
(testing
"a map of strings has no keyword"
(is (=ok false (validate-lilac {:a "a", "b" "b"} (map+ (string+) (string+))))))
(testing
"a map of keyword/number"
(is (=ok true (validate-lilac {:a 1, :b 2} (map+ (keyword+) (number+))))))
(testing
"a map of keyword/number not number/keyword"
(is (=ok false (validate-lilac {:a 1, 2 :b} (map+ (keyword+) (number+))))))
(testing
"a map of keyword/number or keyword/string"
(is
(=ok
true
(validate-lilac {:a 1, :b "two"} (map+ (keyword+) (or+ [(number+) (string+)])))))))
(deftest
test-nil
(testing "a nil" (is (=ok true (validate-lilac nil (nil+)))))
(testing "string not nil" (is (=ok false (validate-lilac "x" (nil+))))))
(deftest
test-number
(testing "a number" (is (=ok true (validate-lilac 1 (number+)))))
(testing "keyword not a number" (is (=ok false (validate-lilac :k (number+)))))
(testing "nil not a number" (is (=ok false (validate-lilac nil (number+)))))
(testing
"number larger than 100"
(is (=ok true (validate-lilac 101 (number+ {:min 100})))))
(testing
"99 is not larger than 100"
(is (=ok false (validate-lilac 99 (number+ {:min 100}))))))
(deftest
test-optional
(testing "optional value" (is (=ok true (validate-lilac nil (optional+ (number+))))))
(testing
"optional value a number"
(is (=ok true (validate-lilac 1 (optional+ (number+))))))
(testing
"not not fit optional number"
(is (=ok false (validate-lilac "1" (optional+ (number+)))))))
(deftest
test-or
(testing
"number or string"
(is (=ok true (validate-lilac 10 (or+ [(number+) (string+)])))))
(testing
"number or string"
(is (=ok true (validate-lilac "10" (or+ [(number+) (string+)])))))
(testing
"keyword is not number or string"
(is (=ok false (validate-lilac :x (or+ [(number+) (string+)]))))))
(deftest
test-record
(testing "an empty record" (is (=ok true (validate-lilac {} (record+ [])))))
(testing
"an record of numbers"
(is (=ok true (validate-lilac {1 100, 2 200} (record+ {1 (number+), 2 (number+)} nil)))))
(testing
"an record of numbers of not keyword/number"
(is
(=ok false (validate-lilac {:a 100, :b 200} (record+ {1 (number+), 2 (number+)} nil)))))
(testing
"an record of number and vector/string"
(is
(=ok
true
(validate-lilac
{:a 100, :b ["red" "blue"]}
(record+ {:a (number+), :b (vector+ (string+))} nil)))))
(testing
"add restriction to keys"
(is
(=ok
false
(validate-lilac
{:a 100, :b ["red" "blue"]}
(record+ {:a (number+)} {:restricted-keys #{:a}}))))))
(deftest
test-router-config
(testing "valid config" (is (=ok true (validate-lilac router-data (lilac-router+)))))
(testing
"overwriten config"
(is
(=ok
false
(validate-lilac
(assoc-in router-data [:routes 1 :next 1 :get] "overwriten")
(lilac-router+)))))
(testing
"config with no file"
(is
(=ok
false
(validate-lilac
(assoc-in router-data [:routes 1 :next 1 :get :file] nil)
(lilac-router+)))))
(testing
"string is not router config"
(is (=ok false (validate-lilac "random text" (lilac-router+)))))
(testing
"routes need to be a string"
(is (=ok false (validate-lilac {:port 0, :routes 0} (lilac-router+))))))
(deftest
test-string
(testing "a string" (is (=ok true (validate-lilac "x" (string+)))))
(testing "nil not a string" (is (=ok false (validate-lilac nil (string+)))))
(testing "keyword not a string" (is (=ok false (validate-lilac :x (string+)))))
(testing "blank string" (is (=ok true (validate-lilac "" (string+ {:nonblank? false})))))
(testing "blank string" (is (=ok false (validate-lilac "" (string+ {:nonblank? true})))))
(testing "blank string" (is (=ok true (validate-lilac "x" (string+ {:nonblank? true}))))))
(deftest
test-vector
(testing
"a vector of boolean"
(is (=ok true (validate-lilac [true true false] (vector+ (boolean+))))))
(testing "a empty vector" (is (=ok true (validate-lilac [] (vector+ (boolean+))))))
(testing
"list is not a empty vector"
(is (=ok false (validate-lilac (list) (vector+ (boolean+))))))
(testing
"boolean is not a empty vector"
(is (=ok false (validate-lilac false (vector+ (boolean+)))))))