Substantially modified the way Parenscript compilation and
[clinton/parenscript.git] / t / reference-tests.lisp
1 (in-package :ps-test)
2 ;; Tests of everything in the reference.
3 ;; File is generated automatically from the text in reference.lisp by
4 ;; the function make-reference-tests-dot-lisp in ref2test.lisp
5 ;; so do not edit this file.
6 (eval-when (:compile-toplevel :load-toplevel :execute)
7 (def-suite ref-tests))
8 (in-suite ref-tests)
9
10 (test-ps-js statements-and-expressions-1
11 (+ i (if 1 2 3))
12 "i + (1 ? 2 : 3);")
13
14 (test-ps-js statements-and-expressions-2
15 (if 1 2 3)
16 "if (1) {
17 2;
18 } else {
19 3;
20 };")
21
22 (test-ps-js symbol-conversion-1
23 !?#@%
24 "bangwhathashatpercent;")
25
26 (test-ps-js symbol-conversion-2
27 bla-foo-bar
28 "blaFooBar;")
29
30 (test-ps-js symbol-conversion-3
31 *array
32 "Array;")
33
34 (test-ps-js symbol-conversion-4
35 *global-array*
36 "GLOBALARRAY;")
37
38 (test-ps-js number-literals-1
39 1
40 "1;")
41
42 (test-ps-js number-literals-2
43 123.123
44 "123.123;")
45
46 (test-ps-js number-literals-3
47 #x10
48 "16;")
49
50 (test-ps-js string-literals-1
51 "foobar"
52 "'foobar';")
53
54 (test-ps-js string-literals-2
55 "bratzel bub"
56 "'bratzel bub';")
57
58 (test-ps-js string-literals-3
59 " "
60 "'\\t';")
61
62 (test-ps-js array-literals-1
63 (array)
64 "[ ];")
65
66 (test-ps-js array-literals-2
67 (array 1 2 3)
68 "[ 1, 2, 3 ];")
69
70 (test-ps-js array-literals-3
71 (array (array 2 3)
72 (array "foobar" "bratzel bub"))
73 "[ [ 2, 3 ], [ 'foobar', 'bratzel bub' ] ];")
74
75 (test-ps-js array-literals-4
76 (make-array)
77 "new Array();")
78
79 (test-ps-js array-literals-5
80 (make-array 1 2 3)
81 "new Array(1, 2, 3);")
82
83 (test-ps-js array-literals-6
84 (make-array
85 (make-array 2 3)
86 (make-array "foobar" "bratzel bub"))
87 "new Array(new Array(2, 3), new Array('foobar', 'bratzel bub'));")
88
89 (test-ps-js object-literals-1
90 (create :foo "bar" :blorg 1)
91 "{ foo : 'bar', blorg : 1 };")
92
93 (test-ps-js object-literals-2
94 (create :foo "hihi"
95 :blorg (array 1 2 3)
96 :another-object (create :schtrunz 1))
97 "{ foo : 'hihi',
98 blorg : [ 1, 2, 3 ],
99 anotherObject : { schtrunz : 1 } };")
100
101 (test-ps-js object-literals-3
102 (slot-value an-object 'foo)
103 "anObject.foo;")
104
105 (test-ps-js object-literals-4
106 (@ an-object foo bar)
107 "anObject.foo.bar;")
108
109 (test-ps-js object-literals-5
110 (with-slots (a b c) this
111 (+ a b c))
112 "this.a + this.b + this.c;")
113
114 (test-ps-js regular-expression-literals-1
115 (regex "foobar")
116 "/foobar/;")
117
118 (test-ps-js regular-expression-literals-2
119 (regex "/foobar/i")
120 "/foobar/i;")
121
122 (test-ps-js literal-symbols-1
123 T
124 "true;")
125
126 (test-ps-js literal-symbols-2
127 FALSE
128 "false;")
129
130 (test-ps-js literal-symbols-3
131 F
132 "false;")
133
134 (test-ps-js literal-symbols-4
135 NIL
136 "null;")
137
138 (test-ps-js literal-symbols-5
139 UNDEFINED
140 "undefined;")
141
142 (test-ps-js literal-symbols-6
143 THIS
144 "this;")
145
146 (test-ps-js variables-1
147 variable
148 "variable;")
149
150 (test-ps-js variables-2
151 a-variable
152 "aVariable;")
153
154 (test-ps-js variables-3
155 *math
156 "Math;")
157
158 (test-ps-js function-calls-and-method-calls-1
159 (blorg 1 2)
160 "blorg(1, 2);")
161
162 (test-ps-js function-calls-and-method-calls-2
163 (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
164 "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ]);")
165
166 (test-ps-js function-calls-and-method-calls-3
167 ((slot-value this 'blorg) 1 2)
168 "this.blorg(1, 2);")
169
170 (test-ps-js function-calls-and-method-calls-4
171 ((aref foo i) 1 2)
172 "foo[i](1, 2);")
173
174 (test-ps-js function-calls-and-method-calls-5
175 ((slot-value (aref foobar 1) 'blorg) NIL T)
176 "foobar[1].blorg(null, true);")
177
178 (test-ps-js operator-expressions-1
179 (* 1 2)
180 "1 * 2;")
181
182 (test-ps-js operator-expressions-2
183 (= 1 2)
184 "1 == 2;")
185
186 (test-ps-js operator-expressions-3
187 (eql 1 2)
188 "1 == 2;")
189
190 (test-ps-js operator-expressions-4
191 (* 1 (+ 2 3 4) 4 (/ 6 7))
192 "1 * (2 + 3 + 4) * 4 * (6 / 7);")
193
194 (test-ps-js operator-expressions-5
195 (incf i)
196 "++i;")
197
198 (test-ps-js operator-expressions-6
199 (decf i)
200 "--i;")
201
202 (test-ps-js operator-expressions-7
203 (1- i)
204 "i - 1;")
205
206 (test-ps-js operator-expressions-8
207 (1+ i)
208 "i + 1;")
209
210 (test-ps-js operator-expressions-9
211 (not (< i 2))
212 "i >= 2;")
213
214 (test-ps-js operator-expressions-10
215 (not (eql i 2))
216 "i != 2;")
217
218 (test-ps-js body-forms-1
219 (progn (blorg i) (blafoo i))
220 "blorg(i);
221 blafoo(i);")
222
223 (test-ps-js body-forms-2
224 (+ i (progn (blorg i) (blafoo i)))
225 "i + (blorg(i), blafoo(i));")
226
227 (test-ps-js function-definition-1
228 (defun a-function (a b)
229 (return (+ a b)))
230 "function aFunction(a, b) {
231 return a + b;
232 };")
233
234 (test-ps-js function-definition-2
235 (lambda (a b) (return (+ a b)))
236 "function (a, b) {
237 return a + b;
238 };")
239
240 (test-ps-js assignment-1
241 (setf a 1)
242 "a = 1;")
243
244 (test-ps-js assignment-2
245 (setf a 2 b 3 c 4 x (+ a b c))
246 "a = 2;
247 b = 3;
248 c = 4;
249 x = a + b + c;")
250
251 (test-ps-js assignment-3
252 (setf a (+ a 2 3 4 a))
253 "a += 2 + 3 + 4 + a;")
254
255 (test-ps-js assignment-4
256 (setf a (- 1 a))
257 "a = 1 - a;")
258
259 (test-ps-js assignment-5
260 (let ((a 1) (b 2))
261 (psetf a b b a))
262 "var a1 = 1;
263 var b2 = 2;
264 var _js3_5 = b2;
265 var _js4_6 = a1;
266 a1 = _js3_5;
267 b2 = _js4_6;")
268
269 (test-ps-js assignment-6
270 (setq a 1)
271 "a = 1;")
272
273 (test-ps-js assignment-8
274 (defun (setf color) (new-color el)
275 (setf (slot-value (slot-value el 'style) 'color) new-color))
276 "function __setf_color(newColor, el) {
277 el.style.color = newColor;
278 };")
279
280 (test-ps-js assignment-9
281 (setf (color some-div) (+ 23 "em"))
282 "var _js2_3 = someDiv;
283 var _js1_4 = 23 + 'em';
284 __setf_color(_js1_4, _js2_3);")
285
286 (test-ps-js assignment-10
287 (defsetf left (el) (offset)
288 `(setf (slot-value (slot-value ,el 'style) 'left) ,offset))
289 "null;")
290
291 (test-ps-js assignment-11
292 (setf (left some-div) (+ 123 "px"))
293 "var _js2_3 = someDiv;
294 var _js1_4 = 123 + 'px';
295 _js2_3.style.left = _js1_4;")
296
297 (test-ps-js assignment-12
298 (macrolet ((left (el)
299 `(slot-value ,el 'offset-left)))
300 (left some-div))
301 "someDiv.offsetLeft;")
302
303 (test-ps-js single-argument-statements-1
304 (return 1)
305 "return 1;")
306
307 (test-ps-js single-argument-statements-2
308 (throw "foobar")
309 "throw 'foobar';")
310
311 (test-ps-js single-argument-expression-1
312 (delete (new (*foobar 2 3 4)))
313 "delete new Foobar(2, 3, 4);")
314
315 (test-ps-js single-argument-expression-2
316 (if (= (typeof blorg) *string)
317 (alert (+ "blorg is a string: " blorg))
318 (alert "blorg is not a string"))
319 "if (typeof blorg == String) {
320 alert('blorg is a string: ' + blorg);
321 } else {
322 alert('blorg is not a string');
323 };")
324
325 (test-ps-js conditional-statements-1
326 (if ((@ blorg is-correct))
327 (progn (carry-on) (return i))
328 (alert "blorg is not correct!"))
329 "if (blorg.isCorrect()) {
330 carryOn();
331 return i;
332 } else {
333 alert('blorg is not correct!');
334 };")
335
336 (test-ps-js conditional-statements-2
337 (+ i (if ((@ blorg add-one)) 1 2))
338 "i + (blorg.addOne() ? 1 : 2);")
339
340 (test-ps-js conditional-statements-3
341 (when ((@ blorg is-correct))
342 (carry-on)
343 (return i))
344 "if (blorg.isCorrect()) {
345 carryOn();
346 return i;
347 };")
348
349 (test-ps-js conditional-statements-4
350 (unless ((@ blorg is-correct))
351 (alert "blorg is not correct!"))
352 "if (!blorg.isCorrect()) {
353 alert('blorg is not correct!');
354 };")
355
356 (test-ps-js variable-declaration-1
357 (defvar *a* (array 1 2 3))
358 "var A = [ 1, 2, 3 ];")
359
360 (test-ps-js variable-declaration-2
361 (progn
362 (defvar *a* 4)
363 (let ((x 1)
364 (*a* 2))
365 (let* ((y (+ x 1))
366 (x (+ x y)))
367 (+ *a* x y))))
368 "var A = 4;
369 var x1 = 1;
370 var A2;
371 try {
372 A2 = A;
373 A = 2;
374 var y3 = x1 + 1;
375 var x4 = x1 + y3;
376 A + x4 + y3;
377 } finally {
378 A = A2;
379 };")
380
381 (test-ps-js iteration-constructs-1
382 (do* ((a) b (c (array "a" "b" "c" "d" "e"))
383 (d 0 (1+ d))
384 (e (aref c d) (aref c d)))
385 ((or (= d (@ c length)) (eql e "x")))
386 (setf a d b e)
387 ((@ document write) (+ "a: " a " b: " b "<br/>")))
388 "for (var a = null, b = null, c = ['a', 'b', 'c', 'd', 'e'], d = 0, e = c[d]; !(d == c.length || e == 'x'); d += 1, e = c[d]) {
389 a = d;
390 b = e;
391 document.write('a: ' + a + ' b: ' + b + '<br/>');
392 };")
393
394 (test-ps-js iteration-constructs-2
395 (do ((i 0 (1+ i))
396 (s 0 (+ s i (1+ i))))
397 ((> i 10))
398 ((@ document write) (+ "i: " i " s: " s "<br/>")))
399 "var i1 = 0;
400 var s2 = 0;
401 for (; i1 <= 10; ) {
402 document.write('i: ' + i1 + ' s: ' + s2 + '<br/>');
403 var _js3_5 = i1 + 1;
404 var _js4_6 = s2 + i1 + (i1 + 1);
405 i1 = _js3_5;
406 s2 = _js4_6;
407 };")
408
409 (test-ps-js iteration-constructs-3
410 (do* ((i 0 (1+ i))
411 (s 0 (+ s i (1- i))))
412 ((> i 10))
413 ((@ document write) (+ "i: " i " s: " s "<br/>")))
414 "for (var i = 0, s = 0; i <= 10; i += 1, s += i + (i - 1)) {
415 document.write('i: ' + i + ' s: ' + s + '<br/>');
416 };")
417
418 (test-ps-js iteration-constructs-4
419 (let ((arr (array "a" "b" "c" "d" "e")))
420 (dotimes (i (@ arr length))
421 ((@ document write) (+ "i: " i " arr[i]: " (aref arr i) "<br/>"))))
422 "var arr1 = ['a', 'b', 'c', 'd', 'e'];
423 for (var i = 0; i < arr1.length; i += 1) {
424 document.write('i: ' + i + ' arr[i]: ' + arr1[i] + '<br/>');
425 };")
426
427 (test-ps-js iteration-constructs-5
428 (let ((res 0))
429 (alert (+ "Summation to 10 is "
430 (dotimes (i 10 res)
431 (incf res (1+ i))))))
432 "var res1 = 0;
433 alert('Summation to 10 is ' + (function () {
434 for (var i = 0; i < 10; i += 1) {
435 res1 += i + 1;
436 };
437 return res1;
438 })());")
439
440 (test-ps-js iteration-constructs-6
441 (let ((l (list 1 2 4 8 16 32)))
442 (dolist (c l)
443 ((@ document write) (+ "c: " c "<br/>"))))
444 "var l1 = [1, 2, 4, 8, 16, 32];
445 for (var c = null, _js_arrvar3 = l1, _js_idx2 = 0; _js_idx2 < _js_arrvar3.length; _js_idx2 += 1) {
446 c = _js_arrvar3[_js_idx2];
447 document.write('c: ' + c + '<br/>');
448 };")
449
450 (test-ps-js iteration-constructs-7
451 (let ((l '(1 2 4 8 16 32))
452 (s 0))
453 (alert (+ "Sum of " l " is: "
454 (dolist (c l s)
455 (incf s c)))))
456 "var l1 = [1, 2, 4, 8, 16, 32];
457 var s2 = 0;
458 alert('Sum of ' + l1 + ' is: ' + (function () {
459 for (var c = null, _js_arrvar4 = l1, _js_idx3 = 0; _js_idx3 < _js_arrvar4.length; _js_idx3 += 1) {
460 c = _js_arrvar4[_js_idx3];
461 s2 += c;
462 };
463 return s2;
464 })());")
465
466 (test-ps-js iteration-constructs-8
467 (let ((obj (create :a 1 :b 2 :c 3)))
468 (for-in (i obj)
469 ((@ document write) (+ i ": " (aref obj i) "<br/>"))))
470 "var obj1 = { a : 1, b : 2, c : 3 };
471 for (var i in obj1) {
472 document.write(i + ': ' + obj1[i] + '<br/>');
473 };")
474
475 (test-ps-js iteration-constructs-9
476 (while ((@ film is-not-finished))
477 ((@ this eat) (new *popcorn)))
478 "while (film.isNotFinished()) {
479 this.eat(new Popcorn);
480 };")
481
482 (test-ps-js the-case-statement-1
483 (case (aref blorg i)
484 ((1 "one") (alert "one"))
485 (2 (alert "two"))
486 (t (alert "default clause")))
487 "switch (blorg[i]) {
488 case 1:
489 case 'one':
490 alert('one');
491 break;
492 case 2:
493 alert('two');
494 break;
495 default:
496 alert('default clause');
497 };")
498
499 (test-ps-js the-case-statement-2
500 (switch (aref blorg i)
501 (1 (alert "If I get here"))
502 (2 (alert "I also get here"))
503 (default (alert "I always get here")))
504 "switch (blorg[i]) {
505 case 1: alert('If I get here');
506 case 2: alert('I also get here');
507 default: alert('I always get here');
508 };")
509
510 (test-ps-js the-with-statement-1
511 (with (create :foo "foo" :i "i")
512 (alert (+ "i is now intermediary scoped: " i)))
513 "with ({ foo : 'foo', i : 'i' }) {
514 alert('i is now intermediary scoped: ' + i);
515 };")
516
517 (test-ps-js the-try-statement-1
518 (try (throw "i")
519 (:catch (error)
520 (alert (+ "an error happened: " error)))
521 (:finally
522 (alert "Leaving the try form")))
523 "try {
524 throw 'i';
525 } catch (error) {
526 alert('an error happened: ' + error);
527 } finally {
528 alert('Leaving the try form');
529 };")
530
531 (test-ps-js the-html-generator-1
532 (ps-html ((:a :href "foobar") "blorg"))
533 "'<A HREF=\"foobar\">blorg</A>';")
534
535 (test-ps-js the-html-generator-2
536 (ps-html ((:a :href (generate-a-link)) "blorg"))
537 "'<A HREF=\"' + generateALink() + '\">blorg</A>';")
538
539 (test-ps-js the-html-generator-3
540 ((@ document write)
541 (ps-html ((:a :href "#"
542 :onclick (ps-inline (transport))) "link")))
543 "document.write('<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">link</A>');")
544
545 (test-ps-js the-html-generator-4
546 (let ((disabled nil)
547 (authorized t))
548 (setf (@ element inner-h-t-m-l)
549 (ps-html ((:textarea (or disabled (not authorized)) :disabled "disabled")
550 "Edit me"))))
551 "var disabled1 = null;
552 var authorized2 = true;
553 element.innerHTML =
554 '<TEXTAREA'
555 + (disabled1 || !authorized2 ? ' DISABLED=\"' + 'disabled' + '\"' : '')
556 + '>Edit me</TEXTAREA>';")
557