-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.lisp
More file actions
354 lines (322 loc) · 10.7 KB
/
tests.lisp
File metadata and controls
354 lines (322 loc) · 10.7 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
(in-package :cheat-js-tests)
(def-suite :cheat-js-tests :description "Tests for CSS generation.")
(in-suite :cheat-js-tests)
(defun defclass-expander (args)
(let* ((names (mapcar #'second args)))
`(:function nil ,names
,(mapcar (lambda (name)
`(:stat
(:assign t
(:dot (:name "this") ,name)
(:name ,name))))
names))))
(test defclass
(cheat-js:clear-macros)
(cheat-js:register-args-macro "@defclass")
(let ((js-code "var Person = @defclass(name, shoeSize);"))
(is (equal (cheat-js:parse-js js-code)
'(:TOPLEVEL
((:VAR
(("Person" :MACRO-CALL (:NAME "@defclass")
(:ARGS (:NAME "name") (:NAME "shoeSize")))))))))
(cheat-js:register-macro-expander "@defclass" #'defclass-expander)
(is (equal (cheat-js:explode js-code)
"var Person = function(name, shoeSize) {
this.name = name;
this.shoeSize = shoeSize;
};"))))
(defun iife-expander (body)
`(:CALL (:FUNCTION NIL NIL ,body)
NIL))
(test iife
(cheat-js:clear-macros)
(cheat-js:register-body-macro "@iife")
(let ((js-code "var a = @iife(alert(1);alert(2););"))
(is (equal (cheat-js:parse-js js-code)
'(:TOPLEVEL
((:VAR
(("a" :MACRO-CALL (:NAME "@iife")
(:BODY (:STAT (:CALL (:NAME "alert") ((:NUM 1))))
(:STAT (:CALL (:NAME "alert") ((:NUM 2))))))))))))
(cheat-js:register-macro-expander "@iife" #'iife-expander)
(is (equal (cheat-js:explode js-code)
"var a = function() {
alert(1);
alert(2);
}();"))
(is (equal (cheat-js:explode "@iife(alert(1);alert(2););")
"(function() {
alert(1);
alert(2);
})();"))))
(defun safe-defclass-expander (args)
(let* ((names (mapcar #'second args))
(class-name (first names))
(field-names (rest names)))
`(:MACRO-CALL
(:NAME "@iife")
(:BODY
(:DEFUN ,class-name ,field-names
,(mapcar (lambda (field)
`(:STAT (:ASSIGN T
(:DOT (:NAME "this") ,field)
(:NAME ,field))))
field-names))
(:BLOCK NIL)
(:RETURN
(:FUNCTION NIL ,field-names
((:RETURN (:NEW (:NAME ,class-name)
,(mapcar (lambda (field)
(list :name field))
field-names))))))))))
(test safe-defclass
(cheat-js:clear-macros)
(cheat-js:register-body-macro "@iife")
(cheat-js:register-macro-expander "@iife" #'iife-expander)
(cheat-js:register-args-macro "@safeDefclass")
(let ((js-code "var Person = @safeDefclass(Person, name, shoeSize);"))
(is (equal (cheat-js:parse-js js-code)
'(:TOPLEVEL
((:VAR
(("Person" :MACRO-CALL (:NAME "@safeDefclass")
(:ARGS (:NAME "Person")
(:NAME "name")
(:NAME "shoeSize")))))))))
(cheat-js:register-macro-expander "@safeDefclass"
#'safe-defclass-expander)
(is (equal (cheat-js:explode js-code)
"var Person = function() {
function Person(name, shoeSize) {
this.name = name;
this.shoeSize = shoeSize;
}
return function(name, shoeSize) {
return new Person(name, shoeSize);
};
}();"))))
(defun when-let-expander (args body)
(let* ((grouped-args (group args 2))
(arg-vars (mapcar #'first grouped-args))
(arg-values (mapcar #'second grouped-args))
(arg-var-names (mapcar #'second arg-vars)))
`(:CALL
(:FUNCTION NIL ,arg-var-names
((:IF ,(make-binary-and-ast arg-vars)
(:BLOCK
,body)
NIL)))
,arg-values)))
(defun group (list n)
(if (< (length list) n)
(if list
(list list))
(cons (subseq list 0 n)
(group (subseq list n) n))))
(defun make-binary-and-ast (operands)
(cond ((= 1 (length operands))
(first operands))
((= 2 (length operands))
(list* :binary :&& operands))
((> (length operands) 2)
(let ((first-two (subseq operands 0 2))
(rest (subseq operands 2)))
(make-binary-and-ast (cons (make-binary-and-ast first-two)
rest))))
(t (error "Incorrect number of operands for @whenLet."))))
(test when-let
(cheat-js:clear-macros)
(cheat-js:register-args-and-body-macro "@whenLet")
(let ((js-code "@whenLet(t1, 1, t2, 2, t3, 3; f(t1, t2, t3););"))
(is (equal (cheat-js:parse-js js-code)
'(:TOPLEVEL
((:STAT
(:MACRO-CALL (:NAME "@whenLet")
((:ARGS (:NAME "t1") (:NUM 1)
(:NAME "t2") (:NUM 2)
(:NAME "t3") (:NUM 3))
(:BODY
(:STAT (:CALL (:NAME "f") ((:NAME "t1")
(:NAME "t2")
(:NAME "t3"))))))))))))
(cheat-js:register-macro-expander "@whenLet" #'when-let-expander)
(is (equal (cheat-js:explode "@whenLet(t1, 1, t2, 2, t3, 3; f(t1, t2, t3););")
"(function(t1, t2, t3) {
if (t1 && t2 && t3) {
f(t1, t2, t3);
}
})(1, 2, 3);"))))
(defun awhen-expander (args body)
`(:MACRO-CALL (:NAME "@whenLet")
((:ARGS (:NAME "it") ,(first args))
(:BODY ,@body))))
(test awhen
(cheat-js:clear-macros)
(cheat-js:register-args-and-body-macro "@whenLet")
(cheat-js:register-macro-expander "@whenLet" #'when-let-expander)
(cheat-js:register-args-and-body-macro "@awhen")
(cheat-js:register-macro-expander "@awhen" #'awhen-expander)
(is (equal (cheat-js:explode "@awhen(expr;f(expr););")
"(function(it) {
if (it) {
f(expr);
}
})(expr);")))
(defun init-library-tests ()
(cheat-js:clear-macros)
(cj-macro-library:install-macros (cj-macro-library:list-macros))
(cj-macro-library:reset-gensym-counter))
(defun one-library-test (invocation expansion)
(is (equal (cheat-js:explode invocation)
expansion)))
(test library-iife-1
(init-library-tests)
(one-library-test "var Test = @iife(return 2;);"
"var Test = function() {
return 2;
}();"))
(test library-iife-2
(init-library-tests)
(one-library-test "@iife(doThis(); thenDoThat(););"
"(function() {
doThis();
thenDoThat();
})();"))
(test library-defclass-1
(init-library-tests)
(one-library-test "var Person = @defclass(Person, name, shoeSize);"
"var Person = function() {
function _Person(name, shoeSize) {
this.name = name;
this.shoeSize = shoeSize;
}
function Person(name, shoeSize) {
var self = new _Person(name, shoeSize);
return self;
}
return Person;
}();"))
(test library-defclass-2
(init-library-tests)
(one-library-test "var Person = @defclass(
Person, name, shoeSize;
self.firstName = name.split(' ')[0];
);"
"var Person = function() {
function _Person(name, shoeSize) {
this.name = name;
this.shoeSize = shoeSize;
}
function Person(name, shoeSize) {
var self = new _Person(name, shoeSize);
self.firstName = name.split(\" \")[0];
return self;
}
return Person;
}();"))
(test library-defclass-3
(init-library-tests)
(one-library-test "var Person = @defclass(
Person, name, shoeSize, $that;
that.firstName = name.split(' ')[0];
);"
"var Person = function() {
function _Person(name, shoeSize) {
this.name = name;
this.shoeSize = shoeSize;
}
function Person(name, shoeSize) {
var that = new _Person(name, shoeSize);
that.firstName = name.split(\" \")[0];
return that;
}
return Person;
}();"))
(test library-if
(init-library-tests)
(one-library-test "var a = @if(someCondition,thenResult,elseResult);"
"var a = someCondition ? thenResult : elseResult;"))
(test library-and-or
(init-library-tests)
(one-library-test "if (@and(cond1,cond2,cond3,@or(cond4,cond5))) { doIt(); }"
"if (cond1 && cond2 && cond3 && (cond4 || cond5)) {
doIt();
}"))
(test library-fn
(init-library-tests)
(one-library-test "@fn(a, b; a < b)"
"function(a, b) {
return a < b;
};"))
(test library-fn0
(init-library-tests)
(one-library-test "var action = @fn0(
doThis();
thenDoThat();
);"
"var action = function() {
doThis();
thenDoThat();
};"))
(test library-dbind-1
(init-library-tests)
(one-library-test "@dbind([a, b], [1, 2]);"
"{
var tmp1 = [ 1, 2 ], a = tmp1[0], b = tmp1[1];
};"))
(test library-dbind-2
(init-library-tests)
(one-library-test "@dbind([a, b, c], [1, 2, 'a']);"
"{
var tmp1 = [ 1, 2, \"a\" ], a = tmp1[0], b = tmp1[1], c = tmp1[2];
};"))
(test library-dotimes-1
(init-library-tests)
(one-library-test "@dotimes(a+b; doit());"
"{
var limit2 = a + b;
for (var i1 = 0; i1 < limit2; i1++) {
doit();
}
};"))
(test library-dotimes-2
(init-library-tests)
(one-library-test "@dotimes(j, a+b; doit());"
"{
var limit1 = a + b;
for (var j = 0; j < limit1; j++) {
doit();
}
};"))
(test library-dolist-1
(init-library-tests)
(one-library-test "@dolist(j, $i, a(b); doIt(i, j); doItAgain(j););"
"{
var list1 = a(b), len2 = list1.length;
for (var i = 0; i < len2; i++) {
var j = list1[i];
doIt(i, j);
doItAgain(j);
}
};"))
(test library-dolist-2
(init-library-tests)
(one-library-test "@dolist(j, a(b), $i; doIt(i, j); doItAgain(j););"
"{
var list1 = a(b), len2 = list1.length;
for (var i = 0; i < len2; i++) {
var j = list1[i];
doIt(i, j);
doItAgain(j);
}
};"))
(test library-dolist-3
(init-library-tests)
(one-library-test "@dolist(j, a(b); doIt(j); doItAgain(j););"
"{
var list2 = a(b), len3 = list2.length;
for (var i1 = 0; i1 < len3; i1++) {
var j = list2[i1];
doIt(j);
doItAgain(j);
}
};"))