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