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