Fixed PS-COMPILE-STREAM breakage (was trying to compile forms one at a
[parenscript.git] / t / ps-tests.lisp
blob76c17af23506d6f9ef0df25a28d589113424e724
1 (in-package "PARENSCRIPT-TEST")
3 (in-suite ps-tests)
5 (test-ps-js plus-is-not-commutative
6 (setf x (+ "before" x "after"))
7 "x = 'before' + x + 'after';")
9 (test-ps-js plus-works-if-first
10 (setf x (+ x "middle" "after"))
11 "x += 'middle' + 'after';")
13 (test-ps-js setf-side-effects
14 (progn
15 (let ((x 10))
16 (defun side-effect()
17 (setf x 4)
19 (setf x (+ 2 (side-effect) x 5))))
20 "var x = 10;
21 function sideEffect() {
22 x = 4;
23 return 3;
25 x = 2 + sideEffect() + x + 5;")
27 (test-ps-js method-call-op-form
28 ((@ (+ "" x) to-string))
29 "('' + x).toString();")
31 (test-ps-js method-call-op-form-args
32 ((@ (+ "" x) to-string) 1 2 :baz 3)
33 "('' + x).toString(1, 2, 'baz', 3);")
35 (test-ps-js method-call-number
36 ((@ 10 to-string))
37 "( 10 ).toString();")
39 (test-ps-js method-call-string
40 ((@ "hi" to-string))
41 "'hi'.toString();")
43 (test-ps-js method-call-lit-object
44 ((@ (create to-string (lambda () (return "it works"))) to-string))
45 "( { toString : function () { return 'it works'; } } ).toString();")
47 (test-ps-js method-call-conditional
48 ((if a x y) 1)
49 "(a ? x : y)(1);")
51 (test-ps-js method-call-variable
52 ((@ x to-string))
53 "x.toString();")
55 (test-ps-js method-call-array
56 ((@ (list 10 20) to-string))
57 "[ 10, 20 ].toString();")
59 (test-ps-js method-call-fn-call
60 ((@ (foo) to-string))
61 "foo().toString();")
63 (test-ps-js method-call-lambda-fn
64 ((@ (lambda () (alert 10)) to-string))
65 "( function () { return alert(10); } ).toString();")
67 (test-ps-js method-call-lambda-call
68 ((@ ((lambda (x) (return x)) 10) to-string))
69 "(function (x) { return x; })(10).toString();")
71 (test no-whitespace-before-dot
72 (let* ((str (ps* '((@ ((lambda (x) (return x)) 10) to-string))))
73 (dot-pos (position #\. str :test #'char=))
74 (char-before (elt str (1- dot-pos)))
75 (a-parenthesis #\)))
76 (is (char= char-before a-parenthesis))))
78 (test-ps-js simple-getprop
79 (let ((foo (create a 1)))
80 (alert (getprop foo 'a)))
81 "var foo = { a : 1 };
82 alert(foo.a);")
84 (test-ps-js buggy-getprop
85 (let ((foo (create a 1))
86 (slot-name "a"))
87 (alert (getprop foo slot-name)))
88 " var foo = { a : 1 };
89 var slotName = 'a';
90 alert(foo[slotName]);
91 "); Last line was alert(foo.slotName) before bug-fix.
93 (test-ps-js buggy-getprop-two
94 (getprop foo (get-slot-name))
95 "foo[getSlotName()];")
97 (test-ps-js old-case-is-now-switch
98 ;; Switch was "case" before, but that was very non-lispish.
99 ;; For example, this code makes three messages and not one
100 ;; which may have been expected. This is because a switch
101 ;; statment must have a break statement for it to return
102 ;; after the alert. Otherwise it continues on the next
103 ;; clause.
104 (switch (aref blorg i)
105 (1 (alert "one"))
106 (2 (alert "two"))
107 (default (alert "default clause")))
108 "switch (blorg[i]) {
109 case 1: alert('one');
110 case 2: alert('two');
111 default: alert('default clause');
112 };")
114 (test-ps-js lisp-like-case
115 (case (aref blorg i)
116 (1 (alert "one"))
117 (2 (alert "two"))
118 (default (alert "default clause")))
119 "switch (blorg[i]) {
120 case 1:
121 alert('one');
122 break;
123 case 2:
124 alert('two');
125 break;
126 default: alert('default clause');
127 };")
130 (test-ps-js even-lispier-case
131 (case (aref blorg i)
132 ((1 2) (alert "Below three"))
133 (3 (alert "Three"))
134 (t (alert "Something else")))
135 "switch (blorg[i]) {
136 case 1:
137 case 2:
138 alert('Below three');
139 break;
140 case 3:
141 alert('Three');
142 break;
143 default: alert('Something else');
144 };")
146 (test-ps-js otherwise-case
147 (case (aref blorg i)
148 (1 (alert "one"))
149 (otherwise (alert "default clause")))
150 "switch (blorg[i]) {
151 case 1:
152 alert('one');
153 break;
154 default: alert('default clause');
155 };")
157 (test escape-sequences-in-string
158 (let ((escapes `((#\\ . #\\)
159 (#\b . #\Backspace)
160 (#\f . ,(code-char 12))
161 ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
162 (#\n . #\Newline)
163 (#\r . #\Return)
164 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
165 (#\t . #\Tab)
166 ("u001F" . ,(code-char #x001f));; character below 32
167 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
168 ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
169 (loop for (js-escape . lisp-char) in escapes
170 for generated = (ps-doc* `(let ((x ,(format nil "hello~ahi" lisp-char)))))
171 for wanted = (format nil "var x = 'hello\\~ahi';" js-escape)
172 do (is (string= (normalize-js-code generated) wanted)))))
174 (test-ps-js getprop-setf
175 (setf (getprop x 'y) (+ (+ a 3) 4))
176 "x.y = (a + 3) + 4;")
178 (test-ps-js getprop-conditional1
179 (getprop (if zoo foo bar) 'x)
180 "(zoo ? foo : bar).x;")
182 (test-ps-js getprop-conditional2
183 (getprop (if (not zoo) foo bar) 'x)
184 "(!zoo ? foo : bar).x;")
186 (test script-star-eval1
187 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
189 (test script-star-eval2
190 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
192 (test-ps-js list-with-single-nil
193 (array nil)
194 "[null];")
196 (test-ps-js quoted-nil-is-array
197 'nil
198 "[];")
200 (test-ps-js defsetf1
201 (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
202 (setf (baz 1 2) 3))
203 "var _js2 = 1;
204 var _js3 = 2;
205 var _js1 = 3;
206 setBaz(_js2, _js3, _js1);")
208 (test-ps-js setf-macroexpands1
209 (macrolet ((bar (x y)
210 `(aref ,x ,y 1)))
211 (setf (bar foo 2) 3))
212 "foo[2][1] = 3;")
214 (test-ps-js defsetf-short
215 (progn (defsetf baz set-baz "docstring")
216 (setf (baz 1 2 3) "foo"))
217 "setBaz(1, 2, 3, 'foo');")
219 (test-ps-js defun-setf1
220 (progn (defun (setf some-thing) (new-val i1 i2)
221 (setf (aref *some-thing* i1 i2) new-val))
222 (setf (some-thing 1 2) "foo"))
223 "function __setf_someThing(newVal, i1, i2) {
224 return SOMETHING[i1][i2] = newVal;
226 __setf_someThing('foo', 1, 2);")
228 (test-ps-js defun-optional1
229 (defun test-opt (&optional x)
230 (if x "yes" "no"))
231 "function testOpt(x) {
232 if (x === undefined) {
233 x = null;
235 if (x) {
236 return 'yes';
237 } else {
238 return 'no';
240 };")
242 (test-ps-js defun-optional2
243 (defun foo (x &optional y)
244 (+ x y))
245 "function foo(x, y) {
246 if (y === undefined) {
247 y = null;
249 return x + y;
250 };")
252 (test-ps-js defun-optional3
253 (defun blah (&optional (x 0))
255 "function blah(x) {
256 if (x === undefined) {
257 x = 0;
259 return x;
260 };")
262 (test-ps-js return-nothing
263 (return)
264 "return null;")
266 (test-ps-js set-timeout
267 (do-set-timeout (10) (alert "foo"))
268 "setTimeout(function () { return alert('foo'); }, 10);")
270 (test-ps-js operator-precedence
271 (* 3 (+ 4 5) 6)
272 "3 * (4 + 5) * 6;")
274 (test-ps-js operators-1
275 (in prop obj)
276 "prop in obj;")
278 (test-ps-js incf1
279 (incf foo bar)
280 "foo += bar;")
282 (test-ps-js decf1
283 (decf foo bar)
284 "foo -= bar;")
286 (test-ps-js incf2
287 (incf x 5)
288 "x += 5;")
290 (test-ps-js decf2
291 (decf y 10)
292 "y -= 10;")
294 (test-ps-js setf-conditional
295 (setf foo (if x 1 2))
296 "foo = x ? 1 : 2;")
298 (test-ps-js obj-literal-numbers
299 (create 1 "foo")
300 "{ 1 : 'foo' };")
302 (test-ps-js obj-literal-strings
303 (create "foo" 2)
304 "{ 'foo' : 2 };")
306 (test-ps-js getprop-string
307 (getprop foo "bar")
308 "foo['bar'];")
310 (test-ps-js getprop-string1
311 (getprop "bar" 'length)
312 "'bar'.length;")
314 (test-ps-js getprop-progn
315 (getprop (progn (some-fun "abc") "123") "length")
316 "(someFun('abc'), '123')['length'];")
318 (test-ps-js method-call-block
319 ((@ (progn (some-fun "abc") "123") to-string))
320 "(someFun('abc'), '123').toString();")
322 (test-ps-js create-blank
323 (create)
324 "{ };")
326 (test-ps-js blank-object-literal
328 "{ };")
330 (test-ps-js array-literal1
332 "[];")
334 (test-ps-js array-literal2
335 ([])
336 "[];")
338 (test-ps-js array-literal3
339 ([] 1 2 3)
340 "[1, 2, 3];")
342 (test-ps-js array-literal4
343 ([] 1 (2 3))
344 "[1, [2, 3]];")
346 (test-ps-js array-literal5
347 ([] (1 2) ("a" "b"))
348 "[[1, 2], ['a', 'b']];")
350 (test-ps-js defun-rest1
351 (defun foo (&rest bar)
352 (alert (aref bar 1)))
353 "function foo() {
354 var bar = [];
355 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
356 bar[i1] = arguments[i1 + 0];
358 return alert(bar[1]);
359 };")
361 (test-ps-js defun-rest2
362 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
363 "function foo(baz) {
364 var bar = [];
365 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
366 bar[i1] = arguments[i1 + 1];
368 return baz + bar[1];
369 };")
371 (test-ps-js defun-keyword1
372 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
373 "function zoo(foo, bar) {
374 var baz;
375 var _js2 = arguments.length;
376 for (var n1 = 2; n1 < _js2; n1 += 2) {
377 switch (arguments[n1]) {
378 case 'baz':
379 baz = arguments[n1 + 1];
382 if (baz === undefined) {
383 baz = null;
385 return foo + bar + baz;
386 };")
388 (test-ps-js defun-keyword2
389 (defun zoo (&key baz) (return (* baz baz)))
390 "function zoo() {
391 var baz;
392 var _js2 = arguments.length;
393 for (var n1 = 0; n1 < _js2; n1 += 2) {
394 switch (arguments[n1]) {
395 case 'baz':
396 baz = arguments[n1 + 1];
399 if (baz === undefined) {
400 baz = null;
402 return baz * baz;
403 };")
405 (test-ps-js defun-keyword3
406 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
407 "function zoo() {
408 var baz;
409 var bar;
410 var _js2 = arguments.length;
411 for (var n1 = 0; n1 < _js2; n1 += 2) {
412 switch (arguments[n1]) {
413 case 'baz':
414 baz = arguments[n1 + 1];
415 break;
416 case 'bar':
417 bar = arguments[n1 + 1];
420 if (baz === undefined) {
421 baz = null;
423 if (bar === undefined) {
424 bar = 4;
426 return baz * bar;
427 };")
429 (test-ps-js defun-keyword4
430 (defun hello-world (&key ((:my-name-key my-name) 1))
431 my-name)
432 "function helloWorld() {
433 var myName;
434 var _js2 = arguments.length;
435 for (var n1 = 0; n1 < _js2; n1 += 2) {
436 switch (arguments[n1]) {
437 case 'my-name-key':
438 myName = arguments[n1 + 1];
441 if (myName === undefined) {
442 myName = 1;
444 return myName;
445 };")
447 (test-ps-js keyword-funcall1
448 (func :baz 1)
449 "func('baz', 1);")
451 (test-ps-js keyword-funcall2
452 (func :baz 1 :bar foo)
453 "func('baz', 1, 'bar', foo);")
455 (test-ps-js keyword-funcall3
456 (fun a b :baz c)
457 "fun(a, b, 'baz', c);")
459 (test-ps-js cond1
460 (cond ((= x 1) 1))
461 "if (x == 1) {
463 };")
465 (test-ps-js cond2
466 (cond ((= x 1) 2)
467 ((= y (* x 4)) (foo "blah") (* x y)))
468 "if (x == 1) {
470 } else if (y == x * 4) {
471 foo('blah');
472 x * y;
473 };")
475 (test-ps-js if-exp-without-else-return
476 (return (if x 1))
477 "if (x) {
478 return 1;
479 };")
481 (test-ps-js progn-expression-single-statement
482 (return (progn (* x y)))
483 "return x * y;")
485 (test-ps-js cond-expression1
486 (defun foo ()
487 (cond ((< 1 2) (bar "foo") (* 4 5))))
488 "function foo() {
489 if (1 < 2) {
490 bar('foo');
491 return 4 * 5;
493 };")
495 (test-ps-js cond-expression2
496 (defun foo ()
497 (cond ((< 2 1) "foo")
498 ((= 7 7) "bar")))
499 "function foo() {
500 if (2 < 1) {
501 return 'foo';
502 } else if (7 == 7) {
503 return 'bar';
505 };")
507 (test-ps-js cond-expression-final-t-clause
508 (defun foo ()
509 (cond ((< 1 2) (bar "foo") (* 4 5))
510 ((= a b) (+ c d))
511 ((< 1 2 3 4 5) x)
512 (t "foo")))
513 "function foo() {
514 var _cmp3;
515 var _cmp2;
516 var _cmp1;
517 if (1 < 2) {
518 bar('foo');
519 return 4 * 5;
520 } else if (a == b) {
521 return c + d;
522 } else if ((_cmp1 = 2, _cmp2 = 3, _cmp3 = 4, 1 < _cmp1 && _cmp1 < _cmp2 && _cmp2 < _cmp3 && _cmp3 < 5)) {
523 return x;
524 } else {
525 return 'foo';
527 };")
529 (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
530 (defun foo ()
531 (cond ((< 2 1) 5)
532 (t "foo")
533 ((< 1 2) "bar")))
534 "function foo() {
535 if (2 < 1) {
536 return 5;
537 } else {
538 return 'foo';
540 };")
542 (test-ps-js funcall-if-expression
543 ((@ document write)
544 (if (= *linkornot* 1)
545 (ps-html ((:a :href "#"
546 :onclick (ps-inline (transport)))
547 img))
548 img))
549 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport()') + '\">' + img + '</A>' : img);")
551 (test-ps-js negate-number-literal
552 (- 1)
553 "-1;")
555 (test macro-environment1
556 (is (string= (normalize-js-code (let* ((macroname (gensym)))
557 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
558 `(defun test1 ()
559 (macrolet ((,macroname (x) `(aref data ,x)))
560 (when (,macroname x)
561 (setf (,macroname x) 123)))))))
562 (normalize-js-code
563 "function test1() {
564 if (data[x]) {
565 return data[x] = 123;
567 };"))))
569 (test macro-environment2
570 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
571 (defpsmacro macro-environment2-macro (x)
572 `(+ ,outer-lexical-variable ,x))
573 (ps* '(macro-environment2-macro 2))))
574 (normalize-js-code "1 + 2;"))))
576 (test-ps-js ampersand-whole-1
577 (macrolet ((foo (&whole foo bar baz)
578 (declare (ignore bar baz))
579 (format nil "~a" foo)))
580 (foo 1 2))
581 "'(FOO 1 2)';")
583 (test-ps-js keyword-consistent
585 "'x';")
587 (test-ps-js simple-symbol-macrolet
588 (symbol-macrolet ((x 1)) x)
589 "1;")
591 (test-ps-js compound-symbol-macrolet
592 (symbol-macrolet ((x 123)
593 (y (* 2 x)))
595 "2 * 123;")
597 (test-ps-js define-symbol-macro
598 (progn (define-symbol-macro tst-sym-macro 2)
599 tst-sym-macro)
600 "2;")
602 (test-ps-js define-symbol-macro1
603 (progn (define-symbol-macro tst-sym-macro1 2)
604 (foo tst-sym-macro1))
605 "foo(2);")
607 (test-ps-js expression-progn
608 (1+ (progn (foo) (if x 1 2)))
609 "(foo(), x ? 1 : 2) + 1;")
611 (test-ps-js let-decl-in-expression
612 (defun f (x)
613 (if x 1 (let* ((foo x))
614 foo)))
615 "function f(x) {
616 if (x) {
617 return 1;
618 } else {
619 var foo = x;
620 return foo;
622 };")
624 (test-ps-js special-var1
625 (progn (defvar *foo*)
626 (let* ((*foo* 2))
627 (* *foo* 2)))
628 "var FOO;
629 var FOO_TMPSTACK1;
630 try {
631 FOO_TMPSTACK1 = FOO;
632 FOO = 2;
633 FOO * 2;
634 } finally {
635 FOO = FOO_TMPSTACK1;
636 };")
638 (test-ps-js special-var2
639 (progn (defvar *foo*)
640 (let* ((*baz* 3)
641 (*foo* 2))
642 (* *foo* 2 *baz*)))
643 "var FOO;
644 var BAZ = 3;
645 var FOO_TMPSTACK1;
646 try {
647 FOO_TMPSTACK1 = FOO;
648 FOO = 2;
649 FOO * 2 * BAZ;
650 } finally {
651 FOO = FOO_TMPSTACK1;
652 };")
654 (test-ps-js literal1
655 (setf x undefined)
656 "x = undefined;")
658 (test-ps-js literal2
659 (aref this x)
660 "this[x];")
662 (test-ps-js setf-dec1
663 (setf x (- 1 x 2))
664 "x = 1 - x - 2;")
666 (test-ps-js setf-dec2
667 (setf x (- x 1 2))
668 "x = x - 1 - 2;")
670 (test-ps-js special-char-equals
671 blah=
672 "blahequals;")
674 (test-ps-js setf-operator-priority
675 (return (or (getprop cache id)
676 (setf (getprop cache id) ((@ document get-element-by-id) id))))
677 "return cache[id] || (cache[id] = document.getElementById(id));")
679 (test-ps-js aref-operator-priority
680 (aref (if (and x (> (length x) 0))
681 (aref x 0)
684 "(x && x.length > 0 ? x[0] : y)[z];")
686 (test-ps-js aref-operator-priority1
687 (aref (or (getprop x 'y)
688 (getprop a 'b))
690 "(x.y || a.b)[z];")
692 (test-ps-js aref-operator-priority2
693 (aref (if a b c) 0)
694 "(a ? b : c)[0];")
696 (test-ps-js negative-operator-priority
697 (- (if x y z))
698 "-(x ? y : z);")
700 (test-ps-js op-p1
701 (new (or a b))
702 "new (a || b);")
704 (test-ps-js op-p2
705 (delete (if a (or b c) d))
706 "delete (a ? b || c : d);")
708 (test-ps-js op-p3
709 (not (if (or x (not y)) z))
710 "!(x || !y ? z : null);")
712 (test-ps-js op-p4
713 (- (- (* 1 2) 3))
714 "-(1 * 2 - 3);")
716 (test-ps-js op-p5
717 (instanceof (or a b) (if x y z))
718 "((a || b) instanceof (x ? y : z));")
720 (test-ps-js op-p7
721 (or x (if (= x 0) "zero" "empty"))
722 "x || (x == 0 ? 'zero' : 'empty');")
724 (test-ps-js named-op-expression
725 (throw (if a b c))
726 "throw a ? b : c;")
728 (test-ps-js named-op-expression1
729 (typeof (or x y))
730 "typeof (x || y);")
732 (test-ps-js aref-array-expression
733 (aref (or a b c) 0)
734 "(a || b || c)[0];")
736 (test-ps-js getprop-operator
737 (getprop (or a b c) 'd)
738 "(a || b || c).d;")
740 (test-ps-js getprop-parens
741 (getprop (getprop foo 'bar) 'baz)
742 "foo.bar.baz;")
744 (test-ps-js funcall-funcall
745 ((foo))
746 "foo()();")
748 (test-ps-js expression-funcall
749 ((or (@ window eval) eval) foo nil)
750 "(window.eval || eval)(foo, null);")
752 (test-ps-js expression-funcall1
753 (((or (@ window eval) eval) foo nil))
754 "(window.eval || eval)(foo, null)();")
756 (test-ps-js expression-funcall2
757 (((or (@ window eval) eval)) foo nil)
758 "(window.eval || eval)()(foo, null);")
760 (test-ps-js getprop-object-literal
761 (getprop (create a 1) 'a)
762 "({ a : 1 }).a;")
764 (test-ps-js getprop-lambda
765 (getprop (lambda ()) 'prototype)
766 "(function () { return null; }).prototype;")
768 (test-ps-js who-html1
769 (who-ps-html (:span :class "ticker-symbol"
770 :ticker-symbol symbol
771 (:a :href "http://foo.com"
772 symbol)
773 (:span :class "ticker-symbol-popup")))
774 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>';")
776 (test-ps-js flet1
777 ((lambda () (flet ((foo (x)
778 (1+ x)))
779 (return (foo 1)))))
780 "(function () {
781 var foo = function (x) {
782 return x + 1;
784 return foo(1);
785 })();")
787 (test-ps-js flet2
788 (flet ((foo (x) (return (1+ x)))
789 (bar (y) (return (+ 2 y))))
790 (bar (foo 1)))
791 "var foo = function (x) {
792 return x + 1;
794 var bar = function (y) {
795 return 2 + y;
797 bar(foo(1));")
799 (test-ps-js flet3
800 (flet ((foo (x) (+ 2 x)))
801 (flet ((foo (x) (1+ x))
802 (bar (y) (+ 2 (foo y))))
803 (bar (foo 1))))
804 "var foo = function (x) {
805 return 2 + x;
807 var foo1 = function (x) {
808 return x + 1;
810 var bar = function (y) {
811 return 2 + foo(y);
813 bar(foo1(1));")
815 (test-ps-js labels1
816 ((lambda () (labels ((foo (x)
817 (if (=== 0 x)
819 (+ x (foo (1- x))))))
820 (foo 3))))
821 "(function () {
822 var foo = function (x) {
823 if (0 === x) {
824 return 0;
825 } else {
826 return x + foo(x - 1);
829 return foo(3);
830 })();")
832 (test-ps-js labels2
833 (labels ((foo (x) (return (1+ (bar x))))
834 (bar (y) (return (+ 2 (foo y)))))
835 (bar (foo 1)))
836 "var foo = function (x) {
837 return bar(x) + 1;
839 var bar = function (y) {
840 return 2 + foo(y);
842 bar(foo(1));")
844 (test-ps-js labels3
845 (labels ((foo (x) (return (1+ x)))
846 (bar (y) (return (+ 2 (foo y)))))
847 (bar (foo 1)))
848 "var foo = function (x) {
849 return x + 1;
851 var bar = function (y) {
852 return 2 + foo(y);
854 bar(foo(1));")
856 (test-ps-js for-loop-var-init-exp
857 ((lambda (x)
858 (return (do* ((y (if x 0 1) (1+ y))
859 (z 0 (1+ z)))
860 ((= y 3) z))))
861 true)
862 "(function (x) {
863 return (function () {
864 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
866 return z;
867 })();
868 })(true);")
870 (test-ps-js math-pi
872 "Math.PI;")
874 (test-ps-js literal-array
875 '(1 2 3)
876 "[1, 2, 3];")
878 (test-ps-js literal-array-1
879 '(1 foo 3)
880 "[1, 'foo', 3];")
882 (test ps-lisp-expands-in-lexical-environment
883 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
885 (test ps*-lisp-expands-in-null-lexical-environment
886 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
888 (test ps*-lisp-expands-in-dynamic-environment
889 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
891 (test ps-lisp-dynamic-environment
892 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))
894 (test-ps-js ps-js-target-version-keyword-test1
895 (defun foo (x y &key bar baz))
896 "function foo(x, y) {
897 var baz;
898 var x1 = Array.prototype.indexOf.call(arguments, 'bar', 2);
899 var bar = -1 == x1 ? null : arguments[x1 + 1];
900 var x2 = Array.prototype.indexOf.call(arguments, 'baz', 2);
901 return baz = -1 == x2 ? null : arguments[x2 + 1];
903 :js-target-version 1.6)
905 (test-ps-js nested-if-expressions1
906 (return (if (if x y z) a b))
907 "if (x ? y : z) {
908 return a;
909 } else {
910 return b;
911 };")
913 (test-ps-js nested-if-expressions2
914 (return (if x y (if z a b)))
915 "if (x) {
916 return y;
917 } else {
918 if (z) {
919 return a;
920 } else {
921 return b;
923 };")
925 (test-ps-js let1
926 (let (x)
927 (+ x x))
928 "var x = null;
929 x + x;")
931 (test-ps-js let2
932 (let ((x 1))
933 (+ x x))
934 "var x = 1;
935 x + x;")
937 (test-ps-js let-x-x
938 (let ((x (1+ x)))
939 (+ x x))
940 "var x1 = x + 1;
941 x1 + x1;")
943 (test-ps-js let3
944 (let ((x 1)
945 (y 2))
946 (+ x x))
947 "var x = 1;
948 var y = 2;
949 x + x;")
951 (test-ps-js let4
952 (let ((x 1)
953 (y (1+ x)))
954 (+ x y))
955 "var x1 = 1;
956 var y = x + 1;
957 x1 + y;")
959 (test-ps-js let5
960 (let ((x 1))
961 (+ x 1)
962 (let ((x (+ x 5)))
963 (+ x 1))
964 (+ x 1))
965 "var x = 1;
966 x + 1;
967 var x1 = x + 5;
968 x1 + 1;
969 x + 1;")
971 (test-ps-js let6
972 (let ((x 2))
973 (let ((x 1)
974 (y (1+ x)))
975 (+ x y)))
976 "var x = 2;
977 var x1 = 1;
978 var y = x + 1;
979 x1 + y;")
981 (test-ps-js let-exp1
982 (lambda ()
983 (let (x)
984 (+ x x)))
985 "function () {
986 var x = null;
987 return x + x;
988 };")
990 (test-ps-js let*1
991 (let* ((x 1))
992 (+ x x))
993 "var x = 1;
994 x + x;")
996 (test-ps-js let*2
997 (let* ((x 1)
998 (y (+ x 2)))
999 (+ x y))
1000 "var x = 1;
1001 var y = x + 2;
1002 x + y;")
1004 (test-ps-js let*3
1005 (let ((x 3))
1006 (let* ((x 1)
1007 (y (+ x 2)))
1008 (+ x y)))
1009 "var x = 3;
1010 var x1 = 1;
1011 var y = x1 + 2;
1012 x1 + y;")
1014 (test-ps-js let*4
1015 (let ((x 3))
1016 (let* ((y (+ x 2))
1017 (x 1))
1018 (+ x y)))
1019 "var x = 3;
1020 var y = x + 2;
1021 var x1 = 1;
1022 x1 + y;")
1024 (test-ps-js symbol-macrolet-var
1025 (symbol-macrolet ((x y))
1026 (var x))
1027 "var y;")
1029 (test-ps-js setf-conditional1
1030 (setf x (unless (null a) (1+ a)))
1031 "x = a != null ? a + 1 : null;")
1033 (test-ps-js setf-let1
1034 (setf x (let ((a 1)) a))
1035 "x = (a = 1, a);")
1037 (test-ps-js setf-let2
1038 (setf x (let ((a (foo)))
1039 (unless (null a)
1040 (1+ a))))
1041 "x = (a = foo(), a != null ? a + 1 : null);")
1043 (test-ps-js symbol-macro-env1
1044 (symbol-macrolet ((bar 1))
1045 (macrolet ((bar (x y) `(+ ,x ,y)))
1046 (bar bar bar)))
1047 "1 + 1;")
1049 (test-ps-js symbol-macrolet-fun1
1050 (symbol-macrolet ((baz +))
1051 (baz 1 2))
1052 "baz(1, 2);")
1054 (test-ps-js lisp2-namespaces1
1055 (let ((list nil))
1056 (setf list (list 1 2 3)))
1057 "var list = null;
1058 list = [1, 2, 3];")
1060 (test-ps-js let-shadows-symbol-macrolet
1061 (symbol-macrolet ((x y))
1062 (let ((x 1))
1063 (+ x x))
1064 (+ x x))
1065 "var x1 = 1;
1066 x1 + x1;
1067 y + y;")
1069 (test-ps-js let-rename-optimization1
1070 (let ((x 1))
1071 (+ x x))
1072 "var x = 1;
1073 x + x;")
1075 (test-ps-js let-rename-optimization2
1076 (lambda (x)
1077 (let ((x (+ 1 x)))
1078 (return x)))
1079 "function (x) {
1080 var x1 = 1 + x;
1081 return x1;
1082 };")
1084 (test-ps-js symbol-macro-array
1085 (symbol-macrolet ((x 1))
1086 (list x))
1087 "[1];")
1089 (test-ps-js symbol-macro-obj
1090 (symbol-macrolet ((x y))
1091 (create x 1))
1092 "{ x : 1 };")
1094 (test-ps-js symbol-macro-conditional1
1095 (symbol-macrolet ((x y))
1096 (if x x x))
1097 "if (y) {
1099 } else {
1101 };")
1103 (test-ps-js symbol-macro-conditional2
1104 (symbol-macrolet ((x y))
1105 (return (if x x x)))
1106 "if (y) {
1107 return y;
1108 } else {
1109 return y;
1110 };")
1112 (test-ps-js flet-apply
1113 (flet ((foo () 'bar))
1114 (apply (function foo) nil))
1115 "var foo = function () {
1116 return 'bar';
1118 foo.apply(this, null);")
1120 (test-ps-js let-apply
1121 (let ((foo (lambda () (return 1))))
1122 (let ((foo (lambda () (return 2))))
1123 (apply foo nil)))
1124 "var foo = function () {
1125 return 1;
1127 var foo1 = function () {
1128 return 2;
1130 foo1.apply(this, null);")
1132 (test-ps-js flet-let
1133 (flet ((x (x) (return (1+ x))))
1134 (let ((x 2))
1135 (x x)))
1136 "var x = function (x) {
1137 return x + 1;
1139 var x1 = 2;
1140 x(x1);")
1142 (test-ps-js let-flet
1143 (let ((x 2))
1144 (flet ((x (x) (return (1+ x))))
1145 (x x)))
1146 "var x = 2;
1147 var x1 = function (x) {
1148 return x + 1;
1150 x1(x);")
1152 (test-ps-js labels-let
1153 (labels ((x (x) (return (1+ x))))
1154 (let ((x 2))
1155 (x x)))
1156 "var x = function (x) {
1157 return x + 1;
1159 var x1 = 2;
1160 x(x1);")
1162 (test-ps-js let-labels
1163 (let ((x 2))
1164 (labels ((x (x) (return (1+ x))))
1165 (x x)))
1166 "var x = 2;
1167 var x1 = function (x) {
1168 return x + 1;
1170 x1(x);")
1172 (test-ps-js macrolet-let-inteference
1173 (macrolet ((a (n) `(+ ,n 5)))
1174 (let ((a (a 1)))
1175 (let ((b (a (- a 4))))
1176 (+ a b))))
1177 "var a = 1 + 5;
1178 var b = (a - 4) + 5;
1179 a + b;")
1181 (test-ps-js let-subtract-add
1182 (let ((x 1))
1183 (let ((x 2))
1184 (- x x)
1185 (- x)
1186 (decf x)
1187 (incf x)))
1188 "var x = 1;
1189 var x1 = 2;
1190 x1 - x1;
1191 -x1;
1192 --x1;
1193 ++x1;")
1195 (test-ps-js create-reserved-word
1196 (create :default 1)
1197 "{ 'default' : 1 };")
1199 (test-ps-js getprop-reserved-word
1200 (getprop foo :default)
1201 "foo['default'];")
1203 (test-ps-js eval-when-ps-side
1204 (eval-when (:execute)
1206 "5;")
1208 (defvar *lisp-output* nil)
1210 (test eval-when-lisp-side ()
1211 (setf *lisp-output* 'original-value)
1212 (let ((js-output (normalize-js-code
1213 (ps-doc* `(eval-when (:compile-toplevel)
1214 (setf *lisp-output* 'it-works))))))
1215 (is (eql 'it-works *lisp-output*))
1216 (is (string= "" js-output))))
1218 (defpsmacro my-in-package (package-name)
1219 `(eval-when (:compile-toplevel)
1220 (setf *lisp-output* ,package-name)))
1222 (test eval-when-macro-expansion ()
1223 (setf *lisp-output* 'original-value)
1224 (let ((js-output (normalize-js-code
1225 (ps-doc* `(progn
1226 (my-in-package :cl-user)
1227 3)))))
1228 (declare (ignore js-output))
1229 (is (eql :cl-user *lisp-output*))))
1231 (test eval-when-macrolet-expansion ()
1232 (setf *lisp-output* 'original-value)
1233 (let ((js-output (normalize-js-code
1234 (ps-doc* `(macrolet ((my-in-package2 (package-name)
1235 `(eval-when (:compile-toplevel)
1236 (setf *lisp-output* ,package-name))))
1237 (my-in-package2 :cl-user)
1238 3)))))
1239 (declare (ignore js-output))
1240 (is (eql :cl-user *lisp-output*))))
1242 (test-ps-js getprop-keyword
1243 (getprop foo :bar)
1244 "foo['bar'];")
1246 (test-ps-js nary-comparison1
1247 (lambda () (return (< 1 2 3)))
1248 "function () {
1249 var _cmp1;
1250 return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
1251 };")
1253 (test-ps-js chain-getprop1
1254 (chain ($ "foo") (bar x z) frob (baz 5))
1255 "$('foo').bar(x, z).frob.baz(5);")
1257 (test-ps-js chain-getprop2
1258 (chain ($ "foo") bar baz)
1259 "$('foo').bar.baz;")
1261 (test-ps-js chain-getprop3
1262 (chain ($ "foo") bar (x y) baz)
1263 "$('foo').bar.x(y).baz;")
1265 (test-ps-js flet-expression
1266 (1+ (flet ((foo (x) (1+ x)))
1267 (foo 1)))
1268 "(foo = function (x) {
1269 return x + 1;
1270 }, foo(1)) + 1;")
1272 (test-ps-js return-case-break-elimination
1273 (return (case 1
1274 (0 1)
1275 (otherwise 2)))
1276 "switch (1) {
1277 case 0:
1278 return 1;
1279 default:
1280 return 2;
1281 };")
1283 (test-ps-js aplusplus
1285 "aplusplus;")
1287 (test-ps-js astarstar
1289 "astarstar;")
1291 (test-ps-js switch-return-fallthrough
1292 (return
1293 (switch x
1294 (1 (foo) break)
1295 (2 (bar))
1296 (default 4)))
1297 "switch (x) {
1298 case 1:
1299 return foo();
1300 case 2:
1301 bar();
1302 default:
1303 return 4;
1304 };")
1306 (test-ps-js return-last-case
1307 (return
1308 (case x
1309 (a 'eh)
1310 (b 'bee)))
1311 "switch (x) {
1312 case a:
1313 return 'eh';
1314 case b:
1315 return 'bee';
1316 };")
1318 (test-ps-js return-macrolet
1319 (return
1320 (macrolet ((x () 1))
1321 (case (x)
1322 (a 'eh)
1323 (b 'bee))))
1324 "switch (1) {
1325 case a:
1326 return 'eh';
1327 case b:
1328 return 'bee';
1329 };")
1331 (test-ps-js mv-bind1
1332 (multiple-value-bind (a b)
1333 (progn
1334 (returns-mv)
1335 (doesnt))
1336 (alert a)
1337 (alert b))
1338 "returnsMv();
1339 var prevmv2 = arguments['callee']['mv'];
1340 try {
1341 arguments['callee']['mv'] = true;
1342 var a = doesnt();
1343 var mv1 = typeof arguments['callee']['mv'] == 'object' ? arguments['callee']['mv'] : new Array(1);
1344 var b = mv1[0];
1345 alert(a);
1346 alert(b);
1347 } finally {
1348 if (undefined === prevmv2) {
1349 delete arguments['callee']['mv'];
1350 } else {
1351 arguments['callee']['mv'] = prevmv2;
1353 };")
1355 (test-ps-js values0
1356 (lambda () (values))
1357 "function () {
1358 return null;
1359 };")
1361 (test-ps-js values1
1362 (values x)
1363 "x;")
1365 (test-ps-js values2
1366 (values x y)
1367 "var val1_1 = x;
1368 var valrest2 = [y];
1369 if (undefined !== arguments['callee']['caller']['mv']) {
1370 arguments['callee']['caller']['mv'] = valrest2;
1372 val1_1;")
1374 (test-ps-js values3
1375 (values x y z)
1376 "var val1_1 = x;
1377 var valrest2 = [y, z];
1378 if (undefined !== arguments['callee']['caller']['mv']) {
1379 arguments['callee']['caller']['mv'] = valrest2;
1381 val1_1;")
1383 (test-ps-js values-return
1384 (return (values x y))
1385 "var val1_1 = x;
1386 var valrest2 = [y];
1387 if (undefined !== arguments['callee']['caller']['mv']) {
1388 arguments['callee']['caller']['mv'] = valrest2;
1390 return val1_1;")
1392 (test-ps-js return-macrolet
1393 (return
1394 (symbol-macrolet ((x 2))
1395 (loop do (+ x x))))
1396 "for (; true; ) {
1397 2 + 2;
1399 return null;")
1401 (test-ps-js return-cond
1402 (return
1403 (cond ((foo? x) (loop for y in x do (foo y)))
1404 ((bar? x) x)
1405 (t 3)))
1406 "if (foowhat(x)) {
1407 var _js2 = x.length;
1408 var _js1 = 0;
1409 if (_js1 < _js2) {
1410 var y = x[_js1];
1411 while (true) {
1412 foo(y);
1413 _js1 += 1;
1414 if (_js1 >= _js2) {
1415 break;
1417 y = x[_js1];
1420 return null;
1421 } else if (barwhat(x)) {
1422 return x;
1423 } else {
1424 return 3;
1425 };")
1427 (test-ps-js switch-loop
1428 (case x
1429 (1 (dolist (a b))))
1430 "switch (x) {
1431 case 1:
1432 for (var a = null, _js_idx1 = 0; _js_idx1 < b.length; _js_idx1 += 1) {
1433 a = b[_js_idx1];
1435 };")
1437 (test-ps-js switch-folds-blocks
1438 (case x
1439 (1 (loop repeat 3 do (alert "foo"))))
1440 "switch (x) {
1441 case 1:
1442 for (var _js1 = 0; _js1 < 3; _js1 += 1) {
1443 alert('foo');
1445 null;
1446 };")
1448 (test-ps-js setf-places-before-macros
1449 (progn
1450 (defsetf left (el) (offset)
1451 `(setf (@ ,el style left) ,offset))
1452 (macrolet ((left (el)
1453 `(@ ,el offset-left)))
1454 (setf (left x) 10)
1455 (left x)))
1456 "var _js2 = x;
1457 var _js1 = 10;
1458 _js2.style.left = _js1;
1459 x.offsetLeft;")
1461 (test-ps-js for-return
1462 (return (dolist (arg args) (foo arg)))
1463 "for (var arg = null, _js_idx1 = 0; _js_idx1 < args.length; _js_idx1 += 1) {
1464 arg = args[_js_idx1];
1465 foo(arg);
1466 };")
1468 (test-ps-js try-catch-return
1469 (return (try (foo)
1470 (:catch (e)
1471 (bar))
1472 (:finally
1473 (cleanup))))
1474 "try {
1475 return foo();
1476 } catch (e) {
1477 return bar();
1478 } finally {
1479 cleanup();
1480 };")
1482 (test-ps-js defun-setf-optional
1483 (defun (setf foo) (new-value b &optional c)
1484 (setf (aref b (or c 0)) new-value))
1485 "function __setf_foo(newValue, b, c) {
1486 if (c === undefined) {
1487 c = null;
1489 return b[c || 0] = newValue;
1490 };")
1492 (test-ps-js defun-setf-rest
1493 (progn (defun (setf foo) (new-value b &rest foo)
1494 (do-something b foo new-value))
1495 (setf (foo x 1 2 3 4) 5))
1496 "function __setf_foo(newValue, b) {
1497 var foo = [];
1498 for (var i1 = 0; i1 < arguments.length - 2; i1 += 1) {
1499 foo[i1] = arguments[i1 + 2];
1501 return doSomething(b, foo, newValue);
1503 __setf_foo(5, x, 1, 2, 3, 4);")
1505 (test-ps-js return-null
1506 (return nil)
1507 "return null;")
1509 (test-ps-js implicit-return-null
1510 (lambda ()
1512 "function () {
1513 return null;
1514 };")
1516 (test-ps-js implicit-return-null
1517 (lambda ()
1518 nil)
1519 "function () {
1520 return null;
1521 };")
1523 (test-ps-js return-conditional-neste
1524 (defun blep (ss x y)
1525 (when foo?
1526 (let ((pair (bar)))
1527 (unless (null pair)
1528 (destructuring-bind (a b) pair
1529 (unless (or (null a) (null b))
1530 (let ((val (baz a b)))
1531 (unless (null val)
1532 (when (blah val)
1533 (unless (blee)
1534 t))))))))))
1535 "function blep(ss, x, y) {
1536 if (foowhat) {
1537 var pair = bar();
1538 if (pair != null) {
1539 var a = pair[0];
1540 var b = pair[1];
1541 if (!(a == null || b == null)) {
1542 var val = baz(a, b);
1543 if (val != null) {
1544 if (blah(val)) {
1545 if (!blee()) {
1546 return true;
1553 };")
1555 (test-ps-js return-case-conditional
1556 (return
1557 (case foo
1558 (123 (when (bar) t))
1559 (345 (blah))))
1560 "switch (foo) {
1561 case 123:
1562 if (bar()) {
1563 return true;
1564 } else {
1565 return null;
1567 case 345:
1568 return blah();
1569 };")
1571 (test-ps-js return-try-conditional
1572 (return
1573 (try (when x 1)
1574 (:catch (x) 2)
1575 (:finally (bar))))
1576 "try {
1577 if (x) {
1578 return 1;
1579 } else {
1580 return null;
1582 } catch (x) {
1583 return 2;
1584 } finally {
1585 bar();
1586 };")
1588 (test-ps-js function-declare-special
1589 (lambda ()
1590 (declare (special *foo*))
1591 (let ((*foo* 1))
1592 (1+ *foo*)))
1593 "function () {
1594 var FOO_TMPSTACK1;
1595 try {
1596 FOO_TMPSTACK1 = FOO;
1597 FOO = 1;
1598 return FOO + 1;
1599 } finally {
1600 FOO = FOO_TMPSTACK1;
1602 };")
1604 (test-ps-js declare-special-let
1605 (let ((*foo* 123))
1606 (declare (special *foo*))
1607 (blah))
1608 "var FOO_TMPSTACK1;
1609 try {
1610 FOO_TMPSTACK1 = FOO;
1611 FOO = 123;
1612 blah();
1613 } finally {
1614 FOO = FOO_TMPSTACK1;
1615 };")
1617 (test-ps-js macro-null-toplevel
1618 (progn
1619 (defmacro macro-null-toplevel ()
1620 nil)
1621 (macro-null-toplevel))
1624 (test-ps-js define-symbol-macro-let
1625 (progn
1626 (define-symbol-macro test-symbol-macro 1)
1627 (let ((test-symbol-macro 2))
1628 (1+ test-symbol-macro))
1629 (1+ test-symbol-macro))
1630 "var testSymbolMacro1 = 2;
1631 testSymbolMacro1 + 1;
1632 1 + 1;")
1634 (test-ps-js define-symbol-macro-flet
1635 (progn
1636 (define-symbol-macro test-symbol-macro1 1)
1637 (flet ((test-symbol-macro1 () 2))
1638 (1+ test-symbol-macro1)
1639 (test-symbol-macro1))
1640 (1+ test-symbol-macro1))
1641 "var testSymbolMacro1_1 = function () {
1642 return 2;
1644 1 + 1;
1645 testSymbolMacro1_1();
1646 1 + 1;")
1648 (test compile-stream-nulls
1649 (is (string=
1651 (with-input-from-string (s "
1652 (defmacro macro-null-toplevel ()
1653 nil)
1654 (macro-null-toplevel)")
1655 (ps-compile-stream s)))))