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