8712a4ff8b4817eab9b6e7b45e93af640f5dda82
[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 (* 1 (+ 2 3 4) 4 (/ 6 7))
188 "1 * (2 + 3 + 4) * 4 * (6 / 7);")
189
190 (test-ps-js operator-expressions-4
191 (incf i)
192 "++i;")
193
194 (test-ps-js operator-expressions-5
195 (decf i)
196 "--i;")
197
198 (test-ps-js operator-expressions-6
199 (1- i)
200 "i - 1;")
201
202 (test-ps-js operator-expressions-7
203 (1+ i)
204 "i + 1;")
205
206 (test-ps-js operator-expressions-8
207 (not (< i 2))
208 "i >= 2;")
209
210 (test-ps-js body-forms-1
211 (progn (blorg i) (blafoo i))
212 "blorg(i);
213 blafoo(i);")
214
215 (test-ps-js body-forms-2
216 (+ i (progn (blorg i) (blafoo i)))
217 "i + (blorg(i), blafoo(i));")
218
219 (test-ps-js function-definition-1
220 (defun a-function (a b)
221 (return (+ a b)))
222 "function aFunction(a, b) {
223 return a + b;
224 };")
225
226 (test-ps-js function-definition-2
227 (lambda (a b) (return (+ a b)))
228 "function (a, b) {
229 return a + b;
230 };")
231
232 (test-ps-js assignment-1
233 (setf a 1)
234 "a = 1;")
235
236 (test-ps-js assignment-2
237 (setf a 2 b 3 c 4 x (+ a b c))
238 "a = 2;
239 b = 3;
240 c = 4;
241 x = a + b + c;")
242
243 (test-ps-js assignment-3
244 (setf a (+ a 2 3 4 a))
245 "a += 2 + 3 + 4 + a;")
246
247 (test-ps-js assignment-4
248 (setf a (- 1 a))
249 "a = 1 - a;")
250
251 (test-ps-js assignment-5
252 (let ((a 1) (b 2))
253 (psetf a b b a))
254 "var a = 1;
255 var b = 2;
256 var _js1 = b;
257 var _js2 = a;
258 a = _js1;
259 b = _js2;")
260
261 (test-ps-js assignment-6
262 (setq a 1)
263 "a = 1;")
264
265 (test-ps-js assignment-8
266 (defun (setf color) (new-color el)
267 (setf (slot-value (slot-value el 'style) 'color) new-color))
268 "function __setf_color(newColor, el) {
269 el.style.color = newColor;
270 };")
271
272 (test-ps-js assignment-9
273 (setf (color some-div) (+ 23 "em"))
274 "var _js2 = someDiv;
275 var _js1 = 23 + 'em';
276 __setf_color(_js1, _js2);")
277
278 (test-ps-js assignment-10
279 (defsetf left (el) (offset)
280 `(setf (slot-value (slot-value ,el 'style) 'left) ,offset))
281 "null;")
282
283 (test-ps-js assignment-11
284 (setf (left some-div) (+ 123 "px"))
285 "var _js2 = someDiv;
286 var _js1 = 123 + 'px';
287 _js2.style.left = _js1;")
288
289 (test-ps-js assignment-12
290 (macrolet ((left (el)
291 `(slot-value ,el 'offset-left)))
292 (left some-div))
293 "someDiv.offsetLeft;")
294
295 (test-ps-js single-argument-statements-1
296 (return 1)
297 "return 1;")
298
299 (test-ps-js single-argument-statements-2
300 (throw "foobar")
301 "throw 'foobar';")
302
303 (test-ps-js single-argument-expression-1
304 (delete (new (*foobar 2 3 4)))
305 "delete new Foobar(2, 3, 4);")
306
307 (test-ps-js single-argument-expression-2
308 (if (= (typeof blorg) *string)
309 (alert (+ "blorg is a string: " blorg))
310 (alert "blorg is not a string"))
311 "if (typeof blorg == String) {
312 alert('blorg is a string: ' + blorg);
313 } else {
314 alert('blorg is not a string');
315 };")
316
317 (test-ps-js conditional-statements-1
318 (if ((@ blorg is-correct))
319 (progn (carry-on) (return i))
320 (alert "blorg is not correct!"))
321 "if (blorg.isCorrect()) {
322 carryOn();
323 return i;
324 } else {
325 alert('blorg is not correct!');
326 };")
327
328 (test-ps-js conditional-statements-2
329 (+ i (if ((@ blorg add-one)) 1 2))
330 "i + (blorg.addOne() ? 1 : 2);")
331
332 (test-ps-js conditional-statements-3
333 (when ((@ blorg is-correct))
334 (carry-on)
335 (return i))
336 "if (blorg.isCorrect()) {
337 carryOn();
338 return i;
339 };")
340
341 (test-ps-js conditional-statements-4
342 (unless ((@ blorg is-correct))
343 (alert "blorg is not correct!"))
344 "if (!blorg.isCorrect()) {
345 alert('blorg is not correct!');
346 };")
347
348 (test-ps-js variable-declaration-1
349 (defvar *a* (array 1 2 3))
350 "var A = [ 1, 2, 3 ];")
351
352 (test-ps-js variable-declaration-2
353 (progn
354 (defvar *a* 4)
355 (let ((x 1)
356 (*a* 2))
357 (let* ((y (+ x 1))
358 (x (+ x y)))
359 (+ *a* x y))))
360 "var A = 4;
361 var x = 1;
362 var A_TMPSTACK1;
363 try {
364 A_TMPSTACK1 = A;
365 A = 2;
366 var y = x + 1;
367 var x2 = x + y;
368 A + x2 + y;
369 } finally {
370 A = A_TMPSTACK1;
371 };")
372
373 (test-ps-js iteration-constructs-1
374 (do* ((a) b (c (array "a" "b" "c" "d" "e"))
375 (d 0 (1+ d))
376 (e (aref c d) (aref c d)))
377 ((or (= d (@ c length)) (== e "x")))
378 (setf a d b e)
379 ((@ document write) (+ "a: " a " b: " b "<br/>")))
380 "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]) {
381 a = d;
382 b = e;
383 document.write('a: ' + a + ' b: ' + b + '<br/>');
384 };")
385
386 (test-ps-js iteration-constructs-2
387 (do ((i 0 (1+ i))
388 (s 0 (+ s i (1+ i))))
389 ((> i 10))
390 ((@ document write) (+ "i: " i " s: " s "<br/>")))
391 "var i = 0;
392 var s = 0;
393 for (; i <= 10; ) {
394 document.write('i: ' + i + ' s: ' + s + '<br/>');
395 var _js1 = i + 1;
396 var _js2 = s + i + (i + 1);
397 i = _js1;
398 s = _js2;
399 };")
400
401 (test-ps-js iteration-constructs-3
402 (do* ((i 0 (1+ i))
403 (s 0 (+ s i (1- i))))
404 ((> i 10))
405 ((@ document write) (+ "i: " i " s: " s "<br/>")))
406 "for (var i = 0, s = 0; i <= 10; i += 1, s += i + (i - 1)) {
407 document.write('i: ' + i + ' s: ' + s + '<br/>');
408 };")
409
410 (test-ps-js iteration-constructs-4
411 (let ((arr (array "a" "b" "c" "d" "e")))
412 (dotimes (i (@ arr length))
413 ((@ document write) (+ "i: " i " arr[i]: " (aref arr i) "<br/>"))))
414 "var arr = ['a', 'b', 'c', 'd', 'e'];
415 for (var i = 0; i < arr.length; i += 1) {
416 document.write('i: ' + i + ' arr[i]: ' + arr[i] + '<br/>');
417 };")
418
419 (test-ps-js iteration-constructs-5
420 (let ((res 0))
421 (alert (+ "Summation to 10 is "
422 (dotimes (i 10 res)
423 (incf res (1+ i))))))
424 "var res = 0;
425 alert('Summation to 10 is ' + (function () {
426 for (var i = 0; i < 10; i += 1) {
427 res += i + 1;
428 };
429 return res;
430 })());")
431
432 (test-ps-js iteration-constructs-6
433 (let ((l (list 1 2 4 8 16 32)))
434 (dolist (c l)
435 ((@ document write) (+ "c: " c "<br/>"))))
436 "var l = [1, 2, 4, 8, 16, 32];
437 for (var c = null, _js_arrvar2 = l, _js_idx1 = 0; _js_idx1 < _js_arrvar2.length; _js_idx1 += 1) {
438 c = _js_arrvar2[_js_idx1];
439 document.write('c: ' + c + '<br/>');
440 };")
441
442 (test-ps-js iteration-constructs-7
443 (let ((l '(1 2 4 8 16 32))
444 (s 0))
445 (alert (+ "Sum of " l " is: "
446 (dolist (c l s)
447 (incf s c)))))
448 "var l = [1, 2, 4, 8, 16, 32];
449 var s = 0;
450 alert('Sum of ' + l + ' is: ' + (function () {
451 for (var c = null, _js_arrvar2 = l, _js_idx1 = 0; _js_idx1 < _js_arrvar2.length; _js_idx1 += 1) {
452 c = _js_arrvar2[_js_idx1];
453 s += c;
454 };
455 return s;
456 })());")
457
458 (test-ps-js iteration-constructs-8
459 (let ((obj (create a 1 b 2 c 3)))
460 (for-in (i obj)
461 ((@ document write) (+ i ": " (aref obj i) "<br/>"))))
462 "var obj = { a : 1, b : 2, c : 3 };
463 for (var i in obj) {
464 document.write(i + ': ' + obj[i] + '<br/>');
465 };")
466
467 (test-ps-js iteration-constructs-9
468 (while ((@ film is-not-finished))
469 ((@ this eat) (new *popcorn)))
470 "while (film.isNotFinished()) {
471 this.eat(new Popcorn);
472 };")
473
474 (test-ps-js the-case-statement-1
475 (case (aref blorg i)
476 ((1 "one") (alert "one"))
477 (2 (alert "two"))
478 (t (alert "default clause")))
479 "switch (blorg[i]) {
480 case 1:
481 case 'one':
482 alert('one');
483 break;
484 case 2:
485 alert('two');
486 break;
487 default:
488 alert('default clause');
489 };")
490
491 (test-ps-js the-case-statement-2
492 (switch (aref blorg i)
493 (1 (alert "If I get here"))
494 (2 (alert "I also get here"))
495 (default (alert "I always get here")))
496 "switch (blorg[i]) {
497 case 1: alert('If I get here');
498 case 2: alert('I also get here');
499 default: alert('I always get here');
500 };")
501
502 (test-ps-js the-with-statement-1
503 (with (create foo "foo" i "i")
504 (alert (+ "i is now intermediary scoped: " i)))
505 "with ({ foo : 'foo', i : 'i' }) {
506 alert('i is now intermediary scoped: ' + i);
507 };")
508
509 (test-ps-js the-try-statement-1
510 (try (throw "i")
511 (:catch (error)
512 (alert (+ "an error happened: " error)))
513 (:finally
514 (alert "Leaving the try form")))
515 "try {
516 throw 'i';
517 } catch (error) {
518 alert('an error happened: ' + error);
519 } finally {
520 alert('Leaving the try form');
521 };")
522
523 (test-ps-js the-html-generator-1
524 (ps-html ((:a :href "foobar") "blorg"))
525 "'<A HREF=\"foobar\">blorg</A>';")
526
527 (test-ps-js the-html-generator-2
528 (ps-html ((:a :href (generate-a-link)) "blorg"))
529 "'<A HREF=\"' + generateALink() + '\">blorg</A>';")
530
531 (test-ps-js the-html-generator-3
532 ((@ document write)
533 (ps-html ((:a :href "#"
534 :onclick (ps-inline (transport))) "link")))
535 "document.write('<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">link</A>');")
536
537 (test-ps-js the-html-generator-4
538 (let ((disabled nil)
539 (authorized t))
540 (setf (@ element inner-h-t-m-l)
541 (ps-html ((:textarea (or disabled (not authorized)) :disabled "disabled")
542 "Edit me"))))
543 "var disabled = null;
544 var authorized = true;
545 element.innerHTML =
546 '<TEXTAREA'
547 + (disabled || !authorized ? ' DISABLED=\"' + 'disabled' + '\"' : '')
548 + '>Edit me</TEXTAREA>';")
549