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