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