Added code to handle SPECIAL declarations.
[parenscript.git] / t / ps-tests.lisp
blob3f51d59509d932f70181121c81b271583370501e
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 unquoted-nil
194 "null;")
196 (test-ps-js list-with-single-nil
197 (array nil)
198 "[null];")
200 (test-ps-js quoted-nil-is-array
201 'nil
202 "[];")
204 (test-ps-js defsetf1
205 (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
206 (setf (baz 1 2) 3))
207 "var _js2 = 1;
208 var _js3 = 2;
209 var _js1 = 3;
210 setBaz(_js2, _js3, _js1);")
212 (test-ps-js setf-macroexpands1
213 (macrolet ((bar (x y)
214 `(aref ,x ,y 1)))
215 (setf (bar foo 2) 3))
216 "foo[2][1] = 3;")
218 (test-ps-js defsetf-short
219 (progn (defsetf baz set-baz "docstring")
220 (setf (baz 1 2 3) "foo"))
221 "setBaz(1, 2, 3, 'foo');")
223 (test-ps-js defun-setf1
224 (progn (defun (setf some-thing) (new-val i1 i2)
225 (setf (aref *some-thing* i1 i2) new-val))
226 (setf (some-thing 1 2) "foo"))
227 "function __setf_someThing(newVal, i1, i2) {
228 return SOMETHING[i1][i2] = newVal;
230 __setf_someThing('foo', 1, 2);")
232 (test-ps-js defun-optional1
233 (defun test-opt (&optional x)
234 (if x "yes" "no"))
235 "function testOpt(x) {
236 if (x === undefined) {
237 x = null;
239 if (x) {
240 return 'yes';
241 } else {
242 return 'no';
244 };")
246 (test-ps-js defun-optional2
247 (defun foo (x &optional y)
248 (+ x y))
249 "function foo(x, y) {
250 if (y === undefined) {
251 y = null;
253 return x + y;
254 };")
256 (test-ps-js defun-optional3
257 (defun blah (&optional (x 0))
259 "function blah(x) {
260 if (x === undefined) {
261 x = 0;
263 return x;
264 };")
266 (test-ps-js return-nothing
267 (return)
268 "return null;")
270 (test-ps-js set-timeout
271 (do-set-timeout (10) (alert "foo"))
272 "setTimeout(function () { return alert('foo'); }, 10);")
274 (test-ps-js operator-precedence
275 (* 3 (+ 4 5) 6)
276 "3 * (4 + 5) * 6;")
278 (test-ps-js operators-1
279 (in prop obj)
280 "prop in obj;")
282 (test-ps-js incf1
283 (incf foo bar)
284 "foo += bar;")
286 (test-ps-js decf1
287 (decf foo bar)
288 "foo -= bar;")
290 (test-ps-js incf2
291 (incf x 5)
292 "x += 5;")
294 (test-ps-js decf2
295 (decf y 10)
296 "y -= 10;")
298 (test-ps-js setf-conditional
299 (setf foo (if x 1 2))
300 "foo = x ? 1 : 2;")
302 (test-ps-js obj-literal-numbers
303 (create 1 "foo")
304 "{ 1 : 'foo' };")
306 (test-ps-js obj-literal-strings
307 (create "foo" 2)
308 "{ 'foo' : 2 };")
310 (test-ps-js getprop-string
311 (getprop foo "bar")
312 "foo['bar'];")
314 (test-ps-js getprop-string1
315 (getprop "bar" 'length)
316 "'bar'.length;")
318 (test-ps-js getprop-progn
319 (getprop (progn (some-fun "abc") "123") "length")
320 "(someFun('abc'), '123')['length'];")
322 (test-ps-js method-call-block
323 ((@ (progn (some-fun "abc") "123") to-string))
324 "(someFun('abc'), '123').toString();")
326 (test-ps-js create-blank
327 (create)
328 "{ };")
330 (test-ps-js blank-object-literal
332 "{ };")
334 (test-ps-js array-literal1
336 "[];")
338 (test-ps-js array-literal2
339 ([])
340 "[];")
342 (test-ps-js array-literal3
343 ([] 1 2 3)
344 "[1, 2, 3];")
346 (test-ps-js array-literal4
347 ([] 1 (2 3))
348 "[1, [2, 3]];")
350 (test-ps-js array-literal5
351 ([] (1 2) ("a" "b"))
352 "[[1, 2], ['a', 'b']];")
354 (test-ps-js defun-rest1
355 (defun foo (&rest bar)
356 (alert (aref bar 1)))
357 "function foo() {
358 var bar = [];
359 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
360 bar[i1] = arguments[i1 + 0];
362 return alert(bar[1]);
363 };")
365 (test-ps-js defun-rest2
366 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
367 "function foo(baz) {
368 var bar = [];
369 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
370 bar[i1] = arguments[i1 + 1];
372 return baz + bar[1];
373 };")
375 (test-ps-js defun-keyword1
376 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
377 "function zoo(foo, bar) {
378 var baz;
379 var _js2 = arguments.length;
380 for (var n1 = 2; n1 < _js2; n1 += 2) {
381 switch (arguments[n1]) {
382 case 'baz':
383 baz = arguments[n1 + 1];
386 if (baz === undefined) {
387 baz = null;
389 return foo + bar + baz;
390 };")
392 (test-ps-js defun-keyword2
393 (defun zoo (&key baz) (return (* baz baz)))
394 "function zoo() {
395 var baz;
396 var _js2 = arguments.length;
397 for (var n1 = 0; n1 < _js2; n1 += 2) {
398 switch (arguments[n1]) {
399 case 'baz':
400 baz = arguments[n1 + 1];
403 if (baz === undefined) {
404 baz = null;
406 return baz * baz;
407 };")
409 (test-ps-js defun-keyword3
410 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
411 "function zoo() {
412 var baz;
413 var bar;
414 var _js2 = arguments.length;
415 for (var n1 = 0; n1 < _js2; n1 += 2) {
416 switch (arguments[n1]) {
417 case 'baz':
418 baz = arguments[n1 + 1];
419 break;
420 case 'bar':
421 bar = arguments[n1 + 1];
424 if (baz === undefined) {
425 baz = null;
427 if (bar === undefined) {
428 bar = 4;
430 return baz * bar;
431 };")
433 (test-ps-js defun-keyword4
434 (defun hello-world (&key ((:my-name-key my-name) 1))
435 my-name)
436 "function helloWorld() {
437 var myName;
438 var _js2 = arguments.length;
439 for (var n1 = 0; n1 < _js2; n1 += 2) {
440 switch (arguments[n1]) {
441 case 'my-name-key':
442 myName = arguments[n1 + 1];
445 if (myName === undefined) {
446 myName = 1;
448 return myName;
449 };")
451 (test-ps-js keyword-funcall1
452 (func :baz 1)
453 "func('baz', 1);")
455 (test-ps-js keyword-funcall2
456 (func :baz 1 :bar foo)
457 "func('baz', 1, 'bar', foo);")
459 (test-ps-js keyword-funcall3
460 (fun a b :baz c)
461 "fun(a, b, 'baz', c);")
463 (test-ps-js cond1
464 (cond ((= x 1) 1))
465 "if (x == 1) {
467 };")
469 (test-ps-js cond2
470 (cond ((= x 1) 2)
471 ((= y (* x 4)) (foo "blah") (* x y)))
472 "if (x == 1) {
474 } else if (y == x * 4) {
475 foo('blah');
476 x * y;
477 };")
479 (test-ps-js if-exp-without-else-return
480 (return (if x 1))
481 "if (x) {
482 return 1;
483 };")
485 (test-ps-js progn-expression-single-statement
486 (return (progn (* x y)))
487 "return x * y;")
489 (test-ps-js cond-expression1
490 (defun foo ()
491 (cond ((< 1 2) (bar "foo") (* 4 5))))
492 "function foo() {
493 if (1 < 2) {
494 bar('foo');
495 return 4 * 5;
497 };")
499 (test-ps-js cond-expression2
500 (defun foo ()
501 (cond ((< 2 1) "foo")
502 ((= 7 7) "bar")))
503 "function foo() {
504 if (2 < 1) {
505 return 'foo';
506 } else if (7 == 7) {
507 return 'bar';
509 };")
511 (test-ps-js cond-expression-final-t-clause
512 (defun foo ()
513 (cond ((< 1 2) (bar "foo") (* 4 5))
514 ((= a b) (+ c d))
515 ((< 1 2 3 4 5) x)
516 (t "foo")))
517 "function foo() {
518 var _cmp3;
519 var _cmp2;
520 var _cmp1;
521 if (1 < 2) {
522 bar('foo');
523 return 4 * 5;
524 } else if (a == b) {
525 return c + d;
526 } else if ((_cmp1 = 2, _cmp2 = 3, _cmp3 = 4, 1 < _cmp1 && _cmp1 < _cmp2 && _cmp2 < _cmp3 && _cmp3 < 5)) {
527 return x;
528 } else {
529 return 'foo';
531 };")
533 (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
534 (defun foo ()
535 (cond ((< 2 1) 5)
536 (t "foo")
537 ((< 1 2) "bar")))
538 "function foo() {
539 if (2 < 1) {
540 return 5;
541 } else {
542 return 'foo';
544 };")
546 (test-ps-js funcall-if-expression
547 ((@ document write)
548 (if (= *linkornot* 1)
549 (ps-html ((:a :href "#"
550 :onclick (ps-inline (transport)))
551 img))
552 img))
553 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport()') + '\">' + img + '</A>' : img);")
555 (test-ps-js negate-number-literal
556 (- 1)
557 "-1;")
559 (test macro-environment1
560 (is (string= (normalize-js-code (let* ((macroname (gensym)))
561 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
562 `(defun test1 ()
563 (macrolet ((,macroname (x) `(aref data ,x)))
564 (when (,macroname x)
565 (setf (,macroname x) 123)))))))
566 (normalize-js-code
567 "function test1() {
568 if (data[x]) {
569 return data[x] = 123;
571 };"))))
573 (test macro-environment2
574 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
575 (defpsmacro macro-environment2-macro (x)
576 `(+ ,outer-lexical-variable ,x))
577 (ps* '(macro-environment2-macro 2))))
578 (normalize-js-code "1 + 2;"))))
580 (test-ps-js ampersand-whole-1
581 (macrolet ((foo (&whole foo bar baz)
582 (declare (ignore bar baz))
583 (format nil "~a" foo)))
584 (foo 1 2))
585 "'(FOO 1 2)';")
587 (test-ps-js keyword-consistent
589 "'x';")
591 (test-ps-js simple-symbol-macrolet
592 (symbol-macrolet ((x 1)) x)
593 "1;")
595 (test-ps-js compound-symbol-macrolet
596 (symbol-macrolet ((x 123)
597 (y (* 2 x)))
599 "2 * 123;")
601 (test-ps-js define-symbol-macro
602 (progn (define-symbol-macro tst-sym-macro 2)
603 tst-sym-macro)
604 "2;")
606 (test-ps-js define-symbol-macro1
607 (progn (define-symbol-macro tst-sym-macro1 2)
608 (foo tst-sym-macro1))
609 "foo(2);")
611 (test-ps-js expression-progn
612 (1+ (progn (foo) (if x 1 2)))
613 "(foo(), x ? 1 : 2) + 1;")
615 (test-ps-js let-decl-in-expression
616 (defun f (x)
617 (if x 1 (let* ((foo x))
618 foo)))
619 "function f(x) {
620 if (x) {
621 return 1;
622 } else {
623 var foo = x;
624 return foo;
626 };")
628 (test-ps-js special-var1
629 (progn (defvar *foo*)
630 (let* ((*foo* 2))
631 (* *foo* 2)))
632 "var FOO;
633 var FOO_TMPSTACK1;
634 try {
635 FOO_TMPSTACK1 = FOO;
636 FOO = 2;
637 FOO * 2;
638 } finally {
639 FOO = FOO_TMPSTACK1;
640 };")
642 (test-ps-js special-var2
643 (progn (defvar *foo*)
644 (let* ((*baz* 3)
645 (*foo* 2))
646 (* *foo* 2 *baz*)))
647 "var FOO;
648 var BAZ = 3;
649 var FOO_TMPSTACK1;
650 try {
651 FOO_TMPSTACK1 = FOO;
652 FOO = 2;
653 FOO * 2 * BAZ;
654 } finally {
655 FOO = FOO_TMPSTACK1;
656 };")
658 (test-ps-js literal1
659 (setf x undefined)
660 "x = undefined;")
662 (test-ps-js literal2
663 (aref this x)
664 "this[x];")
666 (test-ps-js setf-dec1
667 (setf x (- 1 x 2))
668 "x = 1 - x - 2;")
670 (test-ps-js setf-dec2
671 (setf x (- x 1 2))
672 "x = x - 1 - 2;")
674 (test-ps-js special-char-equals
675 blah=
676 "blahequals;")
678 (test-ps-js setf-operator-priority
679 (return (or (getprop cache id)
680 (setf (getprop cache id) ((@ document get-element-by-id) id))))
681 "return cache[id] || (cache[id] = document.getElementById(id));")
683 (test-ps-js aref-operator-priority
684 (aref (if (and x (> (length x) 0))
685 (aref x 0)
688 "(x && x.length > 0 ? x[0] : y)[z];")
690 (test-ps-js aref-operator-priority1
691 (aref (or (getprop x 'y)
692 (getprop a 'b))
694 "(x.y || a.b)[z];")
696 (test-ps-js aref-operator-priority2
697 (aref (if a b c) 0)
698 "(a ? b : c)[0];")
700 (test-ps-js negative-operator-priority
701 (- (if x y z))
702 "-(x ? y : z);")
704 (test-ps-js op-p1
705 (new (or a b))
706 "new (a || b);")
708 (test-ps-js op-p2
709 (delete (if a (or b c) d))
710 "delete (a ? b || c : d);")
712 (test-ps-js op-p3
713 (not (if (or x (not y)) z))
714 "!(x || !y ? z : null);")
716 (test-ps-js op-p4
717 (- (- (* 1 2) 3))
718 "-(1 * 2 - 3);")
720 (test-ps-js op-p5
721 (instanceof (or a b) (if x y z))
722 "((a || b) instanceof (x ? y : z));")
724 (test-ps-js op-p7
725 (or x (if (= x 0) "zero" "empty"))
726 "x || (x == 0 ? 'zero' : 'empty');")
728 (test-ps-js named-op-expression
729 (throw (if a b c))
730 "throw a ? b : c;")
732 (test-ps-js named-op-expression1
733 (typeof (or x y))
734 "typeof (x || y);")
736 (test-ps-js aref-array-expression
737 (aref (or a b c) 0)
738 "(a || b || c)[0];")
740 (test-ps-js getprop-operator
741 (getprop (or a b c) 'd)
742 "(a || b || c).d;")
744 (test-ps-js getprop-parens
745 (getprop (getprop foo 'bar) 'baz)
746 "foo.bar.baz;")
748 (test-ps-js funcall-funcall
749 ((foo))
750 "foo()();")
752 (test-ps-js expression-funcall
753 ((or (@ window eval) eval) foo nil)
754 "(window.eval || eval)(foo, null);")
756 (test-ps-js expression-funcall1
757 (((or (@ window eval) eval) foo nil))
758 "(window.eval || eval)(foo, null)();")
760 (test-ps-js expression-funcall2
761 (((or (@ window eval) eval)) foo nil)
762 "(window.eval || eval)()(foo, null);")
764 (test-ps-js getprop-object-literal
765 (getprop (create a 1) 'a)
766 "({ a : 1 }).a;")
768 (test-ps-js getprop-lambda
769 (getprop (lambda ()) 'prototype)
770 "(function () { return null; }).prototype;")
772 (test-ps-js who-html1
773 (who-ps-html (:span :class "ticker-symbol"
774 :ticker-symbol symbol
775 (:a :href "http://foo.com"
776 symbol)
777 (:span :class "ticker-symbol-popup")))
778 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>';")
780 (test-ps-js flet1
781 ((lambda () (flet ((foo (x)
782 (1+ x)))
783 (return (foo 1)))))
784 "(function () {
785 var foo = function (x) {
786 return x + 1;
788 return foo(1);
789 })();")
791 (test-ps-js flet2
792 (flet ((foo (x) (return (1+ x)))
793 (bar (y) (return (+ 2 y))))
794 (bar (foo 1)))
795 "var foo = function (x) {
796 return x + 1;
798 var bar = function (y) {
799 return 2 + y;
801 bar(foo(1));")
803 (test-ps-js flet3
804 (flet ((foo (x) (+ 2 x)))
805 (flet ((foo (x) (1+ x))
806 (bar (y) (+ 2 (foo y))))
807 (bar (foo 1))))
808 "var foo = function (x) {
809 return 2 + x;
811 var foo1 = function (x) {
812 return x + 1;
814 var bar = function (y) {
815 return 2 + foo(y);
817 bar(foo1(1));")
819 (test-ps-js labels1
820 ((lambda () (labels ((foo (x)
821 (if (=== 0 x)
823 (+ x (foo (1- x))))))
824 (foo 3))))
825 "(function () {
826 var foo = function (x) {
827 if (0 === x) {
828 return 0;
829 } else {
830 return x + foo(x - 1);
833 return foo(3);
834 })();")
836 (test-ps-js labels2
837 (labels ((foo (x) (return (1+ (bar x))))
838 (bar (y) (return (+ 2 (foo y)))))
839 (bar (foo 1)))
840 "var foo = function (x) {
841 return bar(x) + 1;
843 var bar = function (y) {
844 return 2 + foo(y);
846 bar(foo(1));")
848 (test-ps-js labels3
849 (labels ((foo (x) (return (1+ x)))
850 (bar (y) (return (+ 2 (foo y)))))
851 (bar (foo 1)))
852 "var foo = function (x) {
853 return x + 1;
855 var bar = function (y) {
856 return 2 + foo(y);
858 bar(foo(1));")
860 (test-ps-js for-loop-var-init-exp
861 ((lambda (x)
862 (return (do* ((y (if x 0 1) (1+ y))
863 (z 0 (1+ z)))
864 ((= y 3) z))))
865 true)
866 "(function (x) {
867 return (function () {
868 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
870 return z;
871 })();
872 })(true);")
874 (test-ps-js math-pi
876 "Math.PI;")
878 (test-ps-js literal-array
879 '(1 2 3)
880 "[1, 2, 3];")
882 (test-ps-js literal-array-1
883 '(1 foo 3)
884 "[1, 'foo', 3];")
886 (test ps-lisp-expands-in-lexical-environment
887 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
889 (test ps*-lisp-expands-in-null-lexical-environment
890 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
892 (test ps*-lisp-expands-in-dynamic-environment
893 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
895 (test ps-lisp-dynamic-environment
896 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))
898 (test-ps-js ps-js-target-version-keyword-test1
899 (defun foo (x y &key bar baz))
900 "function foo(x, y) {
901 var baz;
902 var x1 = Array.prototype.indexOf.call(arguments, 'bar', 2);
903 var bar = -1 == x1 ? null : arguments[x1 + 1];
904 var x2 = Array.prototype.indexOf.call(arguments, 'baz', 2);
905 return baz = -1 == x2 ? null : arguments[x2 + 1];
907 :js-target-version 1.6)
909 (test-ps-js nested-if-expressions1
910 (return (if (if x y z) a b))
911 "if (x ? y : z) {
912 return a;
913 } else {
914 return b;
915 };")
917 (test-ps-js nested-if-expressions2
918 (return (if x y (if z a b)))
919 "if (x) {
920 return y;
921 } else {
922 if (z) {
923 return a;
924 } else {
925 return b;
927 };")
929 (test-ps-js let1
930 (let (x)
931 (+ x x))
932 "var x = null;
933 x + x;")
935 (test-ps-js let2
936 (let ((x 1))
937 (+ x x))
938 "var x = 1;
939 x + x;")
941 (test-ps-js let-x-x
942 (let ((x (1+ x)))
943 (+ x x))
944 "var x1 = x + 1;
945 x1 + x1;")
947 (test-ps-js let3
948 (let ((x 1)
949 (y 2))
950 (+ x x))
951 "var x = 1;
952 var y = 2;
953 x + x;")
955 (test-ps-js let4
956 (let ((x 1)
957 (y (1+ x)))
958 (+ x y))
959 "var x1 = 1;
960 var y = x + 1;
961 x1 + y;")
963 (test-ps-js let5
964 (let ((x 1))
965 (+ x 1)
966 (let ((x (+ x 5)))
967 (+ x 1))
968 (+ x 1))
969 "var x = 1;
970 x + 1;
971 var x1 = x + 5;
972 x1 + 1;
973 x + 1;")
975 (test-ps-js let6
976 (let ((x 2))
977 (let ((x 1)
978 (y (1+ x)))
979 (+ x y)))
980 "var x = 2;
981 var x1 = 1;
982 var y = x + 1;
983 x1 + y;")
985 (test-ps-js let-exp1
986 (lambda ()
987 (let (x)
988 (+ x x)))
989 "function () {
990 var x = null;
991 return x + x;
992 };")
994 (test-ps-js let*1
995 (let* ((x 1))
996 (+ x x))
997 "var x = 1;
998 x + x;")
1000 (test-ps-js let*2
1001 (let* ((x 1)
1002 (y (+ x 2)))
1003 (+ x y))
1004 "var x = 1;
1005 var y = x + 2;
1006 x + y;")
1008 (test-ps-js let*3
1009 (let ((x 3))
1010 (let* ((x 1)
1011 (y (+ x 2)))
1012 (+ x y)))
1013 "var x = 3;
1014 var x1 = 1;
1015 var y = x1 + 2;
1016 x1 + y;")
1018 (test-ps-js let*4
1019 (let ((x 3))
1020 (let* ((y (+ x 2))
1021 (x 1))
1022 (+ x y)))
1023 "var x = 3;
1024 var y = x + 2;
1025 var x1 = 1;
1026 x1 + y;")
1028 (test-ps-js symbol-macrolet-var
1029 (symbol-macrolet ((x y))
1030 (var x))
1031 "var y;")
1033 (test-ps-js setf-conditional1
1034 (setf x (unless (null a) (1+ a)))
1035 "x = a != null ? a + 1 : null;")
1037 (test-ps-js setf-let1
1038 (setf x (let ((a 1)) a))
1039 "x = (a = 1, a);")
1041 (test-ps-js setf-let2
1042 (setf x (let ((a (foo)))
1043 (unless (null a)
1044 (1+ a))))
1045 "x = (a = foo(), a != null ? a + 1 : null);")
1047 (test-ps-js symbol-macro-env1
1048 (symbol-macrolet ((bar 1))
1049 (macrolet ((bar (x y) `(+ ,x ,y)))
1050 (bar bar bar)))
1051 "1 + 1;")
1053 (test-ps-js symbol-macrolet-fun1
1054 (symbol-macrolet ((baz +))
1055 (baz 1 2))
1056 "baz(1, 2);")
1058 (test-ps-js lisp2-namespaces1
1059 (let ((list nil))
1060 (setf list (list 1 2 3)))
1061 "var list = null;
1062 list = [1, 2, 3];")
1064 (test-ps-js let-shadows-symbol-macrolet
1065 (symbol-macrolet ((x y))
1066 (let ((x 1))
1067 (+ x x))
1068 (+ x x))
1069 "var x1 = 1;
1070 x1 + x1;
1071 y + y;")
1073 (test-ps-js let-rename-optimization1
1074 (let ((x 1))
1075 (+ x x))
1076 "var x = 1;
1077 x + x;")
1079 (test-ps-js let-rename-optimization2
1080 (lambda (x)
1081 (let ((x (+ 1 x)))
1082 (return x)))
1083 "function (x) {
1084 var x1 = 1 + x;
1085 return x1;
1086 };")
1088 (test-ps-js symbol-macro-array
1089 (symbol-macrolet ((x 1))
1090 (list x))
1091 "[1];")
1093 (test-ps-js symbol-macro-obj
1094 (symbol-macrolet ((x y))
1095 (create x 1))
1096 "{ x : 1 };")
1098 (test-ps-js symbol-macro-conditional1
1099 (symbol-macrolet ((x y))
1100 (if x x x))
1101 "if (y) {
1103 } else {
1105 };")
1107 (test-ps-js symbol-macro-conditional2
1108 (symbol-macrolet ((x y))
1109 (return (if x x x)))
1110 "if (y) {
1111 return y;
1112 } else {
1113 return y;
1114 };")
1116 (test-ps-js flet-apply
1117 (flet ((foo () 'bar))
1118 (apply (function foo) nil))
1119 "var foo = function () {
1120 return 'bar';
1122 foo.apply(this, null);")
1124 (test-ps-js let-apply
1125 (let ((foo (lambda () (return 1))))
1126 (let ((foo (lambda () (return 2))))
1127 (apply foo nil)))
1128 "var foo = function () {
1129 return 1;
1131 var foo1 = function () {
1132 return 2;
1134 foo1.apply(this, null);")
1136 (test-ps-js flet-let
1137 (flet ((x (x) (return (1+ x))))
1138 (let ((x 2))
1139 (x x)))
1140 "var x = function (x) {
1141 return x + 1;
1143 var x1 = 2;
1144 x(x1);")
1146 (test-ps-js let-flet
1147 (let ((x 2))
1148 (flet ((x (x) (return (1+ x))))
1149 (x x)))
1150 "var x = 2;
1151 var x1 = function (x) {
1152 return x + 1;
1154 x1(x);")
1156 (test-ps-js labels-let
1157 (labels ((x (x) (return (1+ x))))
1158 (let ((x 2))
1159 (x x)))
1160 "var x = function (x) {
1161 return x + 1;
1163 var x1 = 2;
1164 x(x1);")
1166 (test-ps-js let-labels
1167 (let ((x 2))
1168 (labels ((x (x) (return (1+ x))))
1169 (x x)))
1170 "var x = 2;
1171 var x1 = function (x) {
1172 return x + 1;
1174 x1(x);")
1176 (test-ps-js macrolet-let-inteference
1177 (macrolet ((a (n) `(+ ,n 5)))
1178 (let ((a (a 1)))
1179 (let ((b (a (- a 4))))
1180 (+ a b))))
1181 "var a = 1 + 5;
1182 var b = (a - 4) + 5;
1183 a + b;")
1185 (test-ps-js let-subtract-add
1186 (let ((x 1))
1187 (let ((x 2))
1188 (- x x)
1189 (- x)
1190 (decf x)
1191 (incf x)))
1192 "var x = 1;
1193 var x1 = 2;
1194 x1 - x1;
1195 -x1;
1196 --x1;
1197 ++x1;")
1199 (test-ps-js create-reserved-word
1200 (create :default 1)
1201 "{ 'default' : 1 };")
1203 (test-ps-js getprop-reserved-word
1204 (getprop foo :default)
1205 "foo['default'];")
1207 (test-ps-js eval-when-ps-side
1208 (eval-when (:execute)
1210 "5;")
1212 (defvar *lisp-output* nil)
1214 (test eval-when-lisp-side ()
1215 (setf *lisp-output* 'original-value)
1216 (let ((js-output (normalize-js-code
1217 (ps-doc* `(eval-when (:compile-toplevel)
1218 (setf *lisp-output* 'it-works))))))
1219 (is (eql 'it-works *lisp-output*))
1220 (is (string= "" js-output))))
1222 (defpsmacro my-in-package (package-name)
1223 `(eval-when (:compile-toplevel)
1224 (setf *lisp-output* ,package-name)))
1226 (test eval-when-macro-expansion ()
1227 (setf *lisp-output* 'original-value)
1228 (let ((js-output (normalize-js-code
1229 (ps-doc* `(progn
1230 (my-in-package :cl-user)
1231 3)))))
1232 (declare (ignore js-output))
1233 (is (eql :cl-user *lisp-output*))))
1235 (test eval-when-macrolet-expansion ()
1236 (setf *lisp-output* 'original-value)
1237 (let ((js-output (normalize-js-code
1238 (ps-doc* `(macrolet ((my-in-package2 (package-name)
1239 `(eval-when (:compile-toplevel)
1240 (setf *lisp-output* ,package-name))))
1241 (my-in-package2 :cl-user)
1242 3)))))
1243 (declare (ignore js-output))
1244 (is (eql :cl-user *lisp-output*))))
1246 (test-ps-js getprop-keyword
1247 (getprop foo :bar)
1248 "foo['bar'];")
1250 (test-ps-js nary-comparison1
1251 (lambda () (return (< 1 2 3)))
1252 "function () {
1253 var _cmp1;
1254 return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
1255 };")
1257 (test-ps-js chain-getprop1
1258 (chain ($ "foo") (bar x z) frob (baz 5))
1259 "$('foo').bar(x, z).frob.baz(5);")
1261 (test-ps-js chain-getprop2
1262 (chain ($ "foo") bar baz)
1263 "$('foo').bar.baz;")
1265 (test-ps-js chain-getprop3
1266 (chain ($ "foo") bar (x y) baz)
1267 "$('foo').bar.x(y).baz;")
1269 (test-ps-js flet-expression
1270 (1+ (flet ((foo (x) (1+ x)))
1271 (foo 1)))
1272 "(foo = function (x) {
1273 return x + 1;
1274 }, foo(1)) + 1;")
1276 (test-ps-js return-case-break-elimination
1277 (return (case 1
1278 (0 1)
1279 (otherwise 2)))
1280 "switch (1) {
1281 case 0:
1282 return 1;
1283 default:
1284 return 2;
1285 };")
1287 (test-ps-js aplusplus
1289 "aplusplus;")
1291 (test-ps-js astarstar
1293 "astarstar;")
1295 (test-ps-js switch-return-fallthrough
1296 (return
1297 (switch x
1298 (1 (foo) break)
1299 (2 (bar))
1300 (default 4)))
1301 "switch (x) {
1302 case 1:
1303 return foo();
1304 case 2:
1305 bar();
1306 default:
1307 return 4;
1308 };")
1310 (test-ps-js return-last-case
1311 (return
1312 (case x
1313 (a 'eh)
1314 (b 'bee)))
1315 "switch (x) {
1316 case a:
1317 return 'eh';
1318 case b:
1319 return 'bee';
1320 };")
1322 (test-ps-js return-macrolet
1323 (return
1324 (macrolet ((x () 1))
1325 (case (x)
1326 (a 'eh)
1327 (b 'bee))))
1328 "switch (1) {
1329 case a:
1330 return 'eh';
1331 case b:
1332 return 'bee';
1333 };")
1335 (test-ps-js mv-bind1
1336 (multiple-value-bind (a b)
1337 (progn
1338 (returns-mv)
1339 (doesnt))
1340 (alert a)
1341 (alert b))
1342 "returnsMv();
1343 var prevmv2 = arguments['callee']['mv'];
1344 try {
1345 arguments['callee']['mv'] = true;
1346 var a = doesnt();
1347 var mv1 = typeof arguments['callee']['mv'] == 'object' ? arguments['callee']['mv'] : new Array(1);
1348 var b = mv1[0];
1349 alert(a);
1350 alert(b);
1351 } finally {
1352 if (undefined === prevmv2) {
1353 delete arguments['callee']['mv'];
1354 } else {
1355 arguments['callee']['mv'] = prevmv2;
1357 };")
1359 (test-ps-js values0
1360 (values)
1361 "null;")
1363 (test-ps-js values1
1364 (values x)
1365 "x;")
1367 (test-ps-js values2
1368 (values x y)
1369 "var val1_1 = x;
1370 var valrest2 = [y];
1371 if (undefined !== arguments['callee']['caller']['mv']) {
1372 arguments['callee']['caller']['mv'] = valrest2;
1374 val1_1;")
1376 (test-ps-js values3
1377 (values x y z)
1378 "var val1_1 = x;
1379 var valrest2 = [y, z];
1380 if (undefined !== arguments['callee']['caller']['mv']) {
1381 arguments['callee']['caller']['mv'] = valrest2;
1383 val1_1;")
1385 (test-ps-js values-return
1386 (return (values x y))
1387 "var val1_1 = x;
1388 var valrest2 = [y];
1389 if (undefined !== arguments['callee']['caller']['mv']) {
1390 arguments['callee']['caller']['mv'] = valrest2;
1392 return val1_1;")
1394 (test-ps-js return-macrolet
1395 (return
1396 (symbol-macrolet ((x 2))
1397 (loop do (+ x x))))
1398 "for (; true; ) {
1399 2 + 2;
1401 return null;")
1403 (test-ps-js return-cond
1404 (return
1405 (cond ((foo? x) (loop for y in x do (foo y)))
1406 ((bar? x) x)
1407 (t 3)))
1408 "if (foowhat(x)) {
1409 var _js2 = x.length;
1410 var _js1 = 0;
1411 if (_js1 < _js2) {
1412 var y = x[_js1];
1413 while (true) {
1414 foo(y);
1415 _js1 += 1;
1416 if (_js1 >= _js2) {
1417 break;
1419 y = x[_js1];
1422 return null;
1423 } else if (barwhat(x)) {
1424 return x;
1425 } else {
1426 return 3;
1427 };")
1429 (test-ps-js switch-loop
1430 (case x
1431 (1 (dolist (a b))))
1432 "switch (x) {
1433 case 1:
1434 for (var a = null, _js_idx1 = 0; _js_idx1 < b.length; _js_idx1 += 1) {
1435 a = b[_js_idx1];
1437 };")
1439 (test-ps-js switch-folds-blocks
1440 (case x
1441 (1 (loop repeat 3 do (alert "foo"))))
1442 "switch (x) {
1443 case 1:
1444 for (var _js1 = 0; _js1 < 3; _js1 += 1) {
1445 alert('foo');
1447 null;
1448 };")
1450 (test-ps-js setf-places-before-macros
1451 (progn
1452 (defsetf left (el) (offset)
1453 `(setf (@ ,el style left) ,offset))
1454 (macrolet ((left (el)
1455 `(@ ,el offset-left)))
1456 (setf (left x) 10)
1457 (left x)))
1458 "var _js2 = x;
1459 var _js1 = 10;
1460 _js2.style.left = _js1;
1461 x.offsetLeft;")
1463 (test-ps-js for-return
1464 (return (dolist (arg args) (foo arg)))
1465 "for (var arg = null, _js_idx1 = 0; _js_idx1 < args.length; _js_idx1 += 1) {
1466 arg = args[_js_idx1];
1467 foo(arg);
1468 };")
1470 (test-ps-js try-catch-return
1471 (return (try (foo)
1472 (:catch (e)
1473 (bar))
1474 (:finally
1475 (cleanup))))
1476 "try {
1477 return foo();
1478 } catch (e) {
1479 return bar();
1480 } finally {
1481 cleanup();
1482 };")
1484 (test-ps-js defun-setf-optional
1485 (defun (setf foo) (new-value b &optional c)
1486 (setf (aref b (or c 0)) new-value))
1487 "function __setf_foo(newValue, b, c) {
1488 if (c === undefined) {
1489 c = null;
1491 return b[c || 0] = newValue;
1492 };")
1494 (test-ps-js defun-setf-rest
1495 (progn (defun (setf foo) (new-value b &rest foo)
1496 (do-something b foo new-value))
1497 (setf (foo x 1 2 3 4) 5))
1498 "function __setf_foo(newValue, b) {
1499 var foo = [];
1500 for (var i1 = 0; i1 < arguments.length - 2; i1 += 1) {
1501 foo[i1] = arguments[i1 + 2];
1503 return doSomething(b, foo, newValue);
1505 __setf_foo(5, x, 1, 2, 3, 4);")
1507 (test-ps-js return-null
1508 (return nil)
1509 "return null;")
1511 (test-ps-js implicit-return-null
1512 (lambda ()
1514 "function () {
1515 return null;
1516 };")
1518 (test-ps-js implicit-return-null
1519 (lambda ()
1520 nil)
1521 "function () {
1522 return null;
1523 };")
1525 (test-ps-js return-conditional-neste
1526 (defun blep (ss x y)
1527 (when foo?
1528 (let ((pair (bar)))
1529 (unless (null pair)
1530 (destructuring-bind (a b) pair
1531 (unless (or (null a) (null b))
1532 (let ((val (baz a b)))
1533 (unless (null val)
1534 (when (blah val)
1535 (unless (blee)
1536 t))))))))))
1537 "function blep(ss, x, y) {
1538 if (foowhat) {
1539 var pair = bar();
1540 if (pair != null) {
1541 var a = pair[0];
1542 var b = pair[1];
1543 if (!(a == null || b == null)) {
1544 var val = baz(a, b);
1545 if (val != null) {
1546 if (blah(val)) {
1547 if (!blee()) {
1548 return true;
1555 };")
1557 (test-ps-js return-case-conditional
1558 (return
1559 (case foo
1560 (123 (when (bar) t))
1561 (345 (blah))))
1562 "switch (foo) {
1563 case 123:
1564 if (bar()) {
1565 return true;
1566 } else {
1567 return null;
1569 case 345:
1570 return blah();
1571 };")
1573 (test-ps-js return-try-conditional
1574 (return
1575 (try (when x 1)
1576 (:catch (x) 2)
1577 (:finally (bar))))
1578 "try {
1579 if (x) {
1580 return 1;
1581 } else {
1582 return null;
1584 } catch (x) {
1585 return 2;
1586 } finally {
1587 bar();
1588 };")
1590 (test-ps-js function-declare-special
1591 (lambda ()
1592 (declare (special *foo*))
1593 (let ((*foo* 1))
1594 (1+ *foo*)))
1595 "function () {
1596 var FOO_TMPSTACK1;
1597 try {
1598 FOO_TMPSTACK1 = FOO;
1599 FOO = 1;
1600 return FOO + 1;
1601 } finally {
1602 FOO = FOO_TMPSTACK1;
1604 };")
1606 (test-ps-js declare-special-let
1607 (let ((*foo* 123))
1608 (declare (special *foo*))
1609 (blah))
1610 "var FOO_TMPSTACK1;
1611 try {
1612 FOO_TMPSTACK1 = FOO;
1613 FOO = 123;
1614 blah();
1615 } finally {
1616 FOO = FOO_TMPSTACK1;
1617 };")