generated from mvc-works/calcit-nodejs-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cljs
178 lines (163 loc) · 5.54 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
(ns lilac.test
(:require [cljs.test :refer [deftest is testing run-tests]]
[lilac.core
:refer
[validate-lilac
number+
string+
keyword+
boolean+
nil+
vector+
list+
map+
set+
deflilac
or+
and+
not+
custom+
is+
optional+]]
[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-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 "an empty map" (is (=ok true (validate-lilac {} (map+ [])))))
(testing
"an map of numbers"
(is (=ok true (validate-lilac {1 100, 2 200} (map+ {1 (number+), 2 (number+)} nil)))))
(testing
"an map of numbers of not keyword/number"
(is (=ok false (validate-lilac {:a 100, :b 200} (map+ {1 (number+), 2 (number+)} nil)))))
(testing
"an map of number and vector/string"
(is
(=ok
true
(validate-lilac
{:a 100, :b ["red" "blue"]}
(map+ {:a (number+), :b (vector+ (string+))} nil)))))
(testing
"add restriction to keys"
(is
(=ok
false
(validate-lilac
{:a 100, :b ["red" "blue"]}
(map+ {:a (number+)} {:restricted-keys #{:a}}))))))
(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-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+)))))))