1e85593a02efa9f7d4b3efc3ae69abef7a25d59c
[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-6
35 *global-array*
36 "GLOBALARRAY")
37
38 (test-ps-js symbol-conversion-7
39 *global-array*.length
40 "GLOBALARRAY.length")
41
42 (test-ps-js number-literals-1
43 1
44 "1")
45
46 (test-ps-js number-literals-2
47 123.123
48 "123.123")
49
50 (test-ps-js number-literals-3
51 #x10
52 "16")
53
54 (test-ps-js string-literals-1
55 "foobar"
56 "'foobar'")
57
58 (test-ps-js string-literals-2
59 "bratzel bub"
60 "'bratzel bub'")
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
107 "anObject.foo")
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 variables-4
159 *math.floor
160 "Math.floor")
161
162 (test-ps-js function-calls-and-method-calls-1
163 (blorg 1 2)
164 "blorg(1, 2)")
165
166 (test-ps-js function-calls-and-method-calls-2
167 (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
168 "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ])")
169
170 (test-ps-js function-calls-and-method-calls-3
171 ((slot-value this 'blorg) 1 2)
172 "this.blorg(1, 2)")
173
174 (test-ps-js function-calls-and-method-calls-4
175 ((aref foo i) 1 2)
176 "foo[i](1, 2)")
177
178 (test-ps-js function-calls-and-method-calls-5
179 ((slot-value (aref foobar 1) 'blorg) NIL T)
180 "foobar[1].blorg(null, true)")
181
182 (test-ps-js function-calls-and-method-calls-6
183 (this.blorg 1 2)
184 "this.blorg(1, 2)")
185
186 (test-ps-js operator-expressions-1
187 (* 1 2)
188 "1 * 2")
189
190 (test-ps-js operator-expressions-2
191 (= 1 2)
192 "1 == 2")
193
194 (test-ps-js operator-expressions-3
195 (eql 1 2)
196 "1 == 2")
197
198 (test-ps-js operator-expressions-4
199 (* 1 (+ 2 3 4) 4 (/ 6 7))
200 "1 * (2 + 3 + 4) * 4 * (6 / 7)")
201
202 (test-ps-js operator-expressions-5
203 (incf i)
204 "++i")
205
206 (test-ps-js operator-expressions-6
207 (decf i)
208 "--i")
209
210 (test-ps-js operator-expressions-7
211 (1- i)
212 "i - 1")
213
214 (test-ps-js operator-expressions-8
215 (1+ i)
216 "i + 1")
217
218 (test-ps-js operator-expressions-9
219 (not (< i 2))
220 "i >= 2")
221
222 (test-ps-js operator-expressions-10
223 (not (eql i 2))
224 "i != 2")
225
226 (test-ps-js body-forms-1
227 (progn (blorg i) (blafoo i))
228 "blorg(i);
229 blafoo(i);")
230
231 (test-ps-js body-forms-2
232 (+ i (progn (blorg i) (blafoo i)))
233 "i + (blorg(i), blafoo(i))")
234
235 (test-ps-js function-definition-1
236 (defun a-function (a b)
237 (return (+ a b)))
238 "function aFunction(a, b) {
239 return a + b;
240 }")
241
242 (test-ps-js function-definition-2
243 (lambda (a b) (return (+ a b)))
244 "function (a, b) {
245 return a + b;
246 }")
247
248 (test-ps-js assignment-1
249 (setf a 1)
250 "a = 1;")
251
252 (test-ps-js assignment-2
253 (setf a 2 b 3 c 4 x (+ a b c))
254 "a = 2;
255 b = 3;
256 c = 4;
257 x = a + b + c;")
258
259 (test-ps-js assignment-3
260 (setf a (+ a 2 3 4 a))
261 "a += 2 + 3 + 4 + a;")
262
263 (test-ps-js assignment-4
264 (setf a (- 1 a))
265 "a = 1 - a;")
266
267 (test-ps-js assignment-5
268 (let* ((a 1) (b 2))
269 (psetf a b b a))
270 "var a = 1;
271 var b = 2;
272 var _js1 = b;
273 var _js2 = a;
274 a = _js1;
275 b = _js2;")
276
277 (test-ps-js assignment-6
278 (setq a 1)
279 "a = 1;")
280
281 (test-ps-js assignment-8
282 (defun (setf color) (new-color el)
283 (setf (slot-value (slot-value el 'style) 'color) new-color))
284 "function __setf_color(newColor, el) {
285 el.style.color = newColor;
286 };")
287
288 (test-ps-js assignment-9
289 (setf (color some-div) (+ 23 "em"))
290 "var _js2 = someDiv;
291 var _js1 = 23 + 'em';
292 __setf_color(_js1, _js2);")
293
294 (test-ps-js assignment-10
295 (defsetf left (el) (offset)
296 `(setf (slot-value (slot-value ,el 'style) 'left) ,offset))
297 "null")
298
299 (test-ps-js assignment-11
300 (setf (left some-div) (+ 123 "px"))
301 "var _js2 = someDiv;
302 var _js1 = 123 + 'px';
303 _js2.style.left = _js1;")
304
305 (test-ps-js assignment-12
306 (progn (defmacro left (el)
307 `(slot-value ,el 'offset-left))
308 (left some-div))
309 "someDiv.offsetLeft;")
310
311 (test-ps-js single-argument-statements-1
312 (return 1)
313 "return 1")
314
315 (test-ps-js single-argument-statements-2
316 (throw "foobar")
317 "throw 'foobar'")
318
319 (test-ps-js single-argument-expression-1
320 (delete (new (*foobar 2 3 4)))
321 "delete new Foobar(2, 3, 4)")
322
323 (test-ps-js single-argument-expression-2
324 (if (= (typeof blorg) *string)
325 (alert (+ "blorg is a string: " blorg))
326 (alert "blorg is not a string"))
327 "if (typeof blorg == String) {
328 alert('blorg is a string: ' + blorg);
329 } else {
330 alert('blorg is not a string');
331 }")
332
333 (test-ps-js conditional-statements-1
334 (if (blorg.is-correct)
335 (progn (carry-on) (return i))
336 (alert "blorg is not correct!"))
337 "if (blorg.isCorrect()) {
338 carryOn();
339 return i;
340 } else {
341 alert('blorg is not correct!');
342 }")
343
344 (test-ps-js conditional-statements-2
345 (+ i (if (blorg.add-one) 1 2))
346 "i + (blorg.addOne() ? 1 : 2)")
347
348 (test-ps-js conditional-statements-3
349 (when (blorg.is-correct)
350 (carry-on)
351 (return i))
352 "if (blorg.isCorrect()) {
353 carryOn();
354 return i;
355 }")
356
357 (test-ps-js conditional-statements-4
358 (unless (blorg.is-correct)
359 (alert "blorg is not correct!"))
360 "if (!blorg.isCorrect()) {
361 alert('blorg is not correct!');
362 }")
363
364 (test-ps-js variable-declaration-1
365 (defvar *a* (array 1 2 3))
366 "var A = [ 1, 2, 3 ]")
367
368 (test-ps-js variable-declaration-2
369 (simple-let* ((a 0) (b 1))
370 (alert (+ a b)))
371 "var a = 0;
372 var b = 1;
373 alert(a + b);")
374
375 (test-ps-js variable-declaration-3
376 (simple-let* ((a "World") (b "Hello"))
377 (simple-let ((a b) (b a))
378 (alert (+ a b))))
379 "var a = 'World';
380 var b = 'Hello';
381 var _js_a1 = b;
382 var _js_b2 = a;
383 var a = _js_a1;
384 var b = _js_b2;
385 delete _js_a1;
386 delete _js_b2;
387 alert(a + b);")
388
389 (test-ps-js variable-declaration-4
390 (simple-let* ((a 0) (b 1))
391 (lexical-let* ((a 9) (b 8))
392 (alert (+ a b)))
393 (alert (+ a b)))
394 "var a = 0;
395 var b = 1;
396 (function () {
397 var a = 9;
398 var b = 8;
399 alert(a + b);
400 })();
401 alert(a + b);")
402
403 (test-ps-js variable-declaration-5
404 (simple-let* ((a "World") (b "Hello"))
405 (lexical-let ((a b) (b a))
406 (alert (+ a b)))
407 (alert (+ a b)))
408 "var a = 'World';
409 var b = 'Hello';
410 (function (a, b) {
411 alert(a + b);
412 })(b, a);
413 alert(a + b);")
414
415 (test-ps-js iteration-constructs-1
416 (do* ((a) b (c (array "a" "b" "c" "d" "e"))
417 (d 0 (1+ d))
418 (e (aref c d) (aref c d)))
419 ((or (= d c.length) (eql e "x")))
420 (setf a d b e)
421 (document.write (+ "a: " a " b: " b "<br/>")))
422 "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]) {
423 a = d;
424 b = e;
425 document.write('a: ' + a + ' b: ' + b + '<br/>');
426 };")
427
428 (test-ps-js iteration-constructs-2
429 (do ((i 0 (1+ i))
430 (s 0 (+ s i (1+ i))))
431 ((> i 10))
432 (document.write (+ "i: " i " s: " s "<br/>")))
433 "var _js_i1 = 0;
434 var _js_s2 = 0;
435 var i = _js_i1;
436 var s = _js_s2;
437 delete _js_i1;
438 delete _js_s2;
439 for (; i <= 10; ) {
440 document.write('i: ' + i + ' s: ' + s + '<br/>');
441 var _js3 = i + 1;
442 var _js4 = s + i + (i + 1);
443 i = _js3;
444 s = _js4;
445 };")
446
447 (test-ps-js iteration-constructs-3
448 (do* ((i 0 (1+ i))
449 (s 0 (+ s i (1- i))))
450 ((> i 10))
451 (document.write (+ "i: " i " s: " s "<br/>")))
452 "for (var i = 0, s = 0; i <= 10; i += 1, s += i + (i - 1)) {
453 document.write('i: ' + i + ' s: ' + s + '<br/>');
454 };")
455
456 (test-ps-js iteration-constructs-4
457 (let* ((arr (array "a" "b" "c" "d" "e")))
458 (dotimes (i arr.length)
459 (document.write (+ "i: " i " arr[i]: " (aref arr i) "<br/>"))))
460 "var arr = ['a', 'b', 'c', 'd', 'e'];
461 for (var i = 0; i < arr.length; i += 1) {
462 document.write('i: ' + i + ' arr[i]: ' + arr[i] + '<br/>');
463 };")
464
465 (test-ps-js iteration-constructs-5
466 (let* ((res 0))
467 (alert (+ "Summation to 10 is "
468 (dotimes (i 10 res)
469 (incf res (1+ i))))))
470 "var res = 0;
471 alert('Summation to 10 is ' + (function () {
472 for (var i = 0; i < 10; i += 1) {
473 res += i + 1;
474 };
475 return res;
476 })());")
477
478 (test-ps-js iteration-constructs-6
479 (let* ((l (list 1 2 4 8 16 32)))
480 (dolist (c l)
481 (document.write (+ "c: " c "<br/>"))))
482 "var l = [1, 2, 4, 8, 16, 32];
483 for (var c = null, _js_arrvar2 = l, _js_idx1 = 0; _js_idx1 < _js_arrvar2.length; _js_idx1 += 1) {
484 c = _js_arrvar2[_js_idx1];
485 document.write('c: ' + c + '<br/>');
486 };")
487
488 (test-ps-js iteration-constructs-7
489 (let* ((l (list 1 2 4 8 16 32))
490 (s 0))
491 (alert (+ "Sum of " l " is: "
492 (dolist (c l s)
493 (incf s c)))))
494 "var l = [1, 2, 4, 8, 16, 32];
495 var s = 0;
496 alert('Sum of ' + l + ' is: ' + (function () {
497 for (var c = null, _js_arrvar2 = l, _js_idx1 = 0; _js_idx1 < _js_arrvar2.length; _js_idx1 += 1) {
498 c = _js_arrvar2[_js_idx1];
499 s += c;
500 };
501 return s;
502 })());")
503
504 (test-ps-js iteration-constructs-8
505 (let* ((obj (create :a 1 :b 2 :c 3)))
506 (doeach (i obj)
507 (document.write (+ i ": " (aref obj i) "<br/>"))))
508 "var obj = { a : 1, b : 2, c : 3 };
509 for (var i in obj) {
510 document.write(i + ': ' + obj[i] + '<br/>');
511 };")
512
513 (test-ps-js iteration-constructs-9
514 (let* ((obj (create :a 1 :b 2 :c 3)))
515 (doeach ((k v) obj)
516 (document.write (+ k ": " v "<br/>"))))
517 "var obj = { a : 1, b : 2, c : 3 };
518 var v;
519 for (var k in obj) {
520 v = obj[k];
521 document.write(k + ': ' + v + '<br/>');
522 };")
523
524 (test-ps-js iteration-constructs-10
525 (while (film.is-not-finished)
526 (this.eat (new *popcorn)))
527 "while (film.isNotFinished()) {
528 this.eat(new Popcorn);
529 }")
530
531 (test-ps-js the-case-statement-1
532 (case (aref blorg i)
533 ((1 "one") (alert "one"))
534 (2 (alert "two"))
535 (t (alert "default clause")))
536 "switch (blorg[i]) {
537 case 1:
538 case 'one':
539 alert('one');
540 break;
541 case 2:
542 alert('two');
543 break;
544 default:
545 alert('default clause');
546 }")
547
548 (test-ps-js the-case-statement-2
549 (switch (aref blorg i)
550 (1 (alert "If I get here"))
551 (2 (alert "I also get here"))
552 (default (alert "I always get here")))
553 "switch (blorg[i]) {
554 case 1: alert('If I get here');
555 case 2: alert('I also get here');
556 default: alert('I always get here');
557 }")
558
559 (test-ps-js the-with-statement-1
560 (with (create :foo "foo" :i "i")
561 (alert (+ "i is now intermediary scoped: " i)))
562 "with ({ foo : 'foo', i : 'i' }) {
563 alert('i is now intermediary scoped: ' + i);
564 }")
565
566 (test-ps-js the-try-statement-1
567 (try (throw "i")
568 (:catch (error)
569 (alert (+ "an error happened: " error)))
570 (:finally
571 (alert "Leaving the try form")))
572 "try {
573 throw 'i';
574 } catch (error) {
575 alert('an error happened: ' + error);
576 } finally {
577 alert('Leaving the try form');
578 }")
579
580 (test-ps-js the-html-generator-1
581 (ps-html ((:a :href "foobar") "blorg"))
582 "'<A HREF=\"foobar\">blorg</A>'")
583
584 (test-ps-js the-html-generator-2
585 (ps-html ((:a :href (generate-a-link)) "blorg"))
586 "'<A HREF=\"' + generateALink() + '\">blorg</A>'")
587
588 (test-ps-js the-html-generator-3
589 (document.write
590 (ps-html ((:a :href "#"
591 :onclick (ps-inline (transport))) "link")))
592 "document.write('<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport()') + '\">link</A>')")
593
594 (test-ps-js the-html-generator-4
595 (let* ((disabled nil)
596 (authorized t))
597 (setf element.inner-h-t-m-l
598 (ps-html ((:textarea (or disabled (not authorized)) :disabled "disabled")
599 "Edit me"))))
600 "var disabled = null;
601 var authorized = true;
602 element.innerHTML =
603 '<TEXTAREA'
604 + (disabled || !authorized ? ' DISABLED=\"' + 'disabled' + '\"' : '')
605 + '>Edit me</TEXTAREA>';")
606