-
Notifications
You must be signed in to change notification settings - Fork 3
/
decl-tests.el
237 lines (220 loc) · 11.9 KB
/
decl-tests.el
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
(ert-deftest decl--test--decl--plist-put! ()
"Tests the function DECL--PLIST-PUT!"
(let ((temp nil))
(decl--plist-put! temp :test 5)
(should (eq 5 (plist-get temp :test)))
(decl--plist-put! temp :test 7)
(should (eq 7 (plist-get temp :test)))
(decl--plist-put! temp :other 3)
(should (eq 3 (plist-get temp :other)))))
(ert-deftest decl--test--decl-cons! ()
"Tests the function DECL-CONS!"
(let ((temp nil))
(should (equal (car (decl-cons! temp 6)) 6))
(decl-cons! temp "test")
(should (equal (car temp) "test"))
(decl-cons! temp :test)
(should (eq (car temp) :test))
(decl-cons! temp 6)
(should (eq (car temp) 6))
(should (eq (length temp) 4))))
(ert-deftest decl--test--decl--concat! ()
"Tests the function DECL--CONCAT!"
(let ((a "test")
(b "other"))
(decl--concat! a b)
(should (equal "testother" a))
(should (equal "othertestother" (decl--concat! b a)))))
(ert-deftest decl--test--decl--generate-increasing-number-string ()
"Tests the function DECL--GENERATE-INCREASING-NUMBER-STRING"
(let ((decl--increasing-count 0))
(should (equal "1" (decl--generate-increasing-number-string)))
(dotimes (i 100) (decl--generate-increasing-number-string))
(should (equal "102" (decl--generate-increasing-number-string)))))
(ert-deftest decl--test--decl--plist-keys () ; Relies on decl--plist-put!
"Tests the function DECL--PLIST-KEYS"
(let ((test-plist nil))
(decl--plist-put! test-plist :other 78)
(should (equal '(:other) (decl--plist-keys test-plist)))
(decl--plist-put! test-plist :test 8)
(should (equal '(:test :other) (decl--plist-keys test-plist)))))
(ert-deftest decl--test--decl-compare-plist-based-digraph-consisting-of-eq-able-elements ()
"Tests the function DECL---COMPARE-PLIST-BASED-DIGRAPH-CONSISTING-OF-EQ-ABLE-ELEMENTS"
(should (eq (decl---compare-plist-based-digraph-consisting-of-eq-able-elements
'(:a (:b) :b (:a)) '(:b (:a) :a (:b))) t))
(should (eq (decl---compare-plist-based-digraph-consisting-of-eq-able-elements
'(:a (:b) :b (:a)) '(:b (:a) :a (2))) nil)))
(ert-deftest decl--test--decl-tarjan-strongly-connected-components-algorithm-for-plist-based-digraphs-consisting-of-eq-able-elements
() "Tests the function decl--test--decl-tarjan-strongly-connected-components-algorithm-for-plist-based-digraphs-consisting-of-eq-able-elements
WARNING: This test is not robust! Failure of this test does not necessarily imply failure of the function this is testing! Comparison function is not adequate.
WARNING 2: Only 'visually' verified!"
(should (eq
(cl-tree-equal
(decl--tarjan-strongly-connected-components-algorithm-for-plist-based-digraph-consisting-of-eq-able-elements
'(1 (2) 2 (3) 3 (4) 4 nil))
'(((:value 1 :index 3 :lowlink 3)) ((:value 2 :index 2 :lowlink 2)) ((:value 3 :index 1 :lowlink 1)) ((:value 4 :index 0 :lowlink 0))))
t))
(should (eq
(cl-tree-equal
(decl--tarjan-strongly-connected-components-algorithm-for-plist-based-digraph-consisting-of-eq-able-elements
'(1 (2 3) 2 (1) 3 (1) 4 (5) 5 nil))
'(((:value 3 :index 2 :lowlink 2)) ((:value 1 :index 2 :lowlink 2) (:value 2 :index 4 :lowlink 3)) ((:value 4 :index 1 :lowlink 1)) ((:value 5 :index 0 :lowlink 0)))
)
t))
(should
(eq
(cl-tree-equal
(decl--tarjan-strongly-connected-components-algorithm-for-plist-based-digraph-consisting-of-eq-able-elements
'(:a (:b :c) :b (:a) :c (:a) :d (:e) :e nil))
'(((:value :c :index 2 :lowlink 2)) ((:value :a :index 2 :lowlink 2) (:value :b :index 4 :lowlink 3)) ((:value :d :index 1 :lowlink 1)) ((:value :e :index 0 :lowlink 0))))
t)))
(ert-deftest decl--test--decl-solve1 ()
"Tests the decl-solve function. Straightforward test."
(let ((decl--decl-block-holder nil)
(decl--keyword-database (make-hash-table :test 'eq))
(test nil)
(plist-of-nodes nil))
(progn
(decl-block :test)
(decl-node :good :test (lambda () t))
(decl-node :bad :test (lambda () nil))
(decl-node :blessed :test (lambda () t) '(:good))
(decl-node :evil :test (lambda () t) '(:bad))
(decl-solve :test))
(setq test (plist-get decl--decl-block-holder :test))
(setq plist-of-nodes (decl--decl--block--access-item-from-generated-data-structures-and-results test :plist-of-nodes))
(should (eq :successful (oref (plist-get plist-of-nodes :good) execution-status)))
(should (eq :successful (oref (plist-get plist-of-nodes :blessed) execution-status)))
(should (eq :failed-via-returning-nil (oref (plist-get plist-of-nodes :bad) execution-status)))
(should (eq :failed-via-failed-dependency (oref (plist-get plist-of-nodes :evil) execution-status)))))
(ert-deftest decl--test--decl-solve2 ()
"Tests the decl-solve function. Cyclical relationship test."
(let ((decl--decl-block-holder nil)
(decl--keyword-database (make-hash-table :test 'eq))
(test nil)
(plist-of-nodes nil))
(progn
(decl-block :test)
(decl-node :good :test (lambda () t))
(decl-node :bad :test (lambda () nil) '(:evil))
(decl-node :blessed :test (lambda () t) '(:good))
(decl-node :evil :test (lambda () t) '(:bad))
(decl-solve :test))
(setq test (plist-get decl--decl-block-holder :test))
(setq plist-of-nodes (decl--decl--block--access-item-from-generated-data-structures-and-results test :plist-of-nodes))
(should (eq :successful (oref (plist-get plist-of-nodes :good) execution-status)))
(should (eq :successful (oref (plist-get plist-of-nodes :blessed) execution-status)))
(should (eq :involved-in-cyclical-relationship (oref (plist-get plist-of-nodes :bad) execution-status)))
(should (eq :involved-in-cyclical-relationship (oref (plist-get plist-of-nodes :evil) execution-status)))))
(ert-deftest decl--test--decl-solve3 ()
"Tests the decl-solve function.
Tests library's handling of nodes that depend on missing constraints"
(let ((decl--decl-block-holder nil)
(decl--keyword-database (make-hash-table :test 'eq))
(test nil)
(plist-of-nodes nil))
(progn
(decl-block :test)
(decl-node :good :test (lambda () t))
(decl-node :bad :test (lambda () nil))
(decl-node :blessed :test (lambda () t) '(:good))
(decl-node :evil :test (lambda () t) '(:disabled))
(decl-solve :test))
(setq test (plist-get decl--decl-block-holder :test))
(setq plist-of-nodes (decl--decl--block--access-item-from-generated-data-structures-and-results test :plist-of-nodes))
(should (eq :successful (oref (plist-get plist-of-nodes :good) execution-status)))
(should (eq :successful (oref (plist-get plist-of-nodes :blessed) execution-status)))
(should (eq :failed-via-returning-nil (oref (plist-get plist-of-nodes :bad) execution-status)))
(should (eq :depends-on-non-existent-constraint (oref (plist-get plist-of-nodes :evil) execution-status)))
(should (eq :non-existent-constraint (oref (plist-get plist-of-nodes :disabled) execution-status)))))
(ert-deftest decl--test--decl-solve4 ()
"Tests the decl-solve function. Test involving a decl-node whose
stored lambda function throws an error.."
(let ((decl--decl-block-holder nil)
(decl--keyword-database (make-hash-table :test 'eq))
(test nil)
(plist-of-nodes nil))
(progn
(decl-block :test)
(decl-node :good :test (lambda () t))
(decl-node :bad :test (lambda () (error "ERROR")))
(decl-node :blessed :test (lambda () t) '(:good))
(decl-node :evil :test (lambda () t) '(:bad))
(decl-solve :test))
(setq test (plist-get decl--decl-block-holder :test))
(setq plist-of-nodes (decl--decl--block--access-item-from-generated-data-structures-and-results test :plist-of-nodes))
(should (eq :successful (oref (plist-get plist-of-nodes :good) execution-status)))
(should (eq :successful (oref (plist-get plist-of-nodes :blessed) execution-status)))
(should (eq :failed-via-throwing-error (oref (plist-get plist-of-nodes :bad) execution-status)))
(should (eq :failed-via-failed-dependency (oref (plist-get plist-of-nodes :evil) execution-status)))))
(ert-deftest decl--test--decl-solve5 ()
"Tests the decl-solve function. Tests the functionality of the defcustom
variables 'decl-config-allow-duplicate-keyword-name-usage',
'decl-config-allow-decl-blocks-to-be-overwritten', and
'decl-config-allow-decl-nodes-to-be-overwritten' in the defgroup 'decl'."
(let ((decl--decl-block-holder nil)
(decl--keyword-database (make-hash-table :test 'eq))
(test nil)
(plist-of-nodes nil)
(decl-config-allow-duplicate-keyword-name-usage t)
(decl-config-allow-decl-blocks-to-be-overwritten t)
(decl-config-allow-decl-nodes-to-be-overwritten t))
(progn
(decl-block :test)
(decl-node :good :test (lambda () t))
(decl-node :bad :test (lambda () (error "ERROR")))
(decl-node :blessed :test (lambda () t) '(:good))
(decl-node :evil :test (lambda () t) '(:bad))
(decl-solve :test))
(setq test (plist-get decl--decl-block-holder :test))
(setq plist-of-nodes (decl--decl--block--access-item-from-generated-data-structures-and-results test :plist-of-nodes))
(should (eq :successful (oref (plist-get plist-of-nodes :good) execution-status)))
(should (eq :successful (oref (plist-get plist-of-nodes :blessed) execution-status)))
(should (eq :failed-via-throwing-error (oref (plist-get plist-of-nodes :bad) execution-status)))
(should (eq :failed-via-failed-dependency (oref (plist-get plist-of-nodes :evil) execution-status)))
(progn
(decl-block :test) ; Duplicate decl-node / keyword name reuse
(decl-node :good :test (lambda () t))
(decl-node :bad :test (lambda () (error "")))
(decl-node :bad :test (lambda () nil)) ; Duplicate decl-node / keyword name reuse
(decl-node :blessed :test (lambda () t) '(:good))
(decl-node :evil :test (lambda () t) '(:bad))
(decl-solve :test))
(setq test (plist-get decl--decl-block-holder :test))
(setq plist-of-nodes (decl--decl--block--access-item-from-generated-data-structures-and-results test :plist-of-nodes))
(should (eq :successful (oref (plist-get plist-of-nodes :good) execution-status)))
(should (eq :successful (oref (plist-get plist-of-nodes :blessed) execution-status)))
(should (eq :failed-via-returning-nil (oref (plist-get plist-of-nodes :bad) execution-status)))
(should (eq :failed-via-failed-dependency (oref (plist-get plist-of-nodes :evil) execution-status)))
))
(ert-deftest decl--test--decl-solve6 ()
"Tests the decl-solve function. Tests the functionality of the defcustom
variable 'decl-config-fail-at-errors' in the defgroup 'decl'."
(let ((decl--decl-block-holder nil)
(decl--keyword-database (make-hash-table :test 'eq))
(test nil)
(decl-config-fail-at-errors t)
(plist-of-nodes nil))
(unwind-protect
(condition-case err
(progn
(decl-block :test)
(decl-node :bad :test (lambda () (error "SOLVE6 Error")))
(decl-solve :test))
(error
(setq test t)
(should (equal "SOLVE6 Error" (error-message-string err))))))
(unless test (error "FAIL"))))
(ert-deftest decl--test--decl-solve7 ()
"Tests the decl-solve function. To see if library can detect cycles of size 1."
(let ((decl--decl-block-holder nil)
(decl--keyword-database (make-hash-table :test 'eq))
(test nil)
(plist-of-nodes nil))
(progn
(decl-block :test)
(decl-node :evil :test (lambda () t) '(:evil))
(decl-solve :test))
(setq test (plist-get decl--decl-block-holder :test))
(setq plist-of-nodes (decl--decl--block--access-item-from-generated-data-structures-and-results test :plist-of-nodes))
(should (eq :involved-in-cyclical-relationship (oref (plist-get plist-of-nodes :evil) execution-status)))))