Removed unneeded EVAL-WHEN from around macro environment definitions.
[parenscript.git] / t / ps-tests.lisp
blob818b9b2f7df1177e1f8defbd04293aea04733969
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 var _js2 = 1;
231 var _js3 = 2;
232 var _js1 = 'foo';
233 __setf_someThing(_js1, _js2, _js3);")
235 (test-ps-js defun-optional1
236 (defun test-opt (&optional x)
237 (if x "yes" "no"))
238 "function testOpt(x) {
239 if (x === undefined) {
240 x = null;
242 if (x) {
243 return 'yes';
244 } else {
245 return 'no';
247 };")
249 (test-ps-js defun-optional2
250 (defun foo (x &optional y)
251 (+ x y))
252 "function foo(x, y) {
253 if (y === undefined) {
254 y = null;
256 return x + y;
257 };")
259 (test-ps-js defun-optional3
260 (defun blah (&optional (x 0))
262 "function blah(x) {
263 if (x === undefined) {
264 x = 0;
266 return x;
267 };")
269 (test-ps-js return-nothing
270 (return)
271 "return null;")
273 (test-ps-js set-timeout
274 (do-set-timeout (10) (alert "foo"))
275 "setTimeout(function () { return alert('foo'); }, 10);")
277 (test-ps-js operator-precedence
278 (* 3 (+ 4 5) 6)
279 "3 * (4 + 5) * 6;")
281 (test-ps-js operators-1
282 (in prop obj)
283 "prop in obj;")
285 (test-ps-js incf1
286 (incf foo bar)
287 "foo += bar;")
289 (test-ps-js decf1
290 (decf foo bar)
291 "foo -= bar;")
293 (test-ps-js incf2
294 (incf x 5)
295 "x += 5;")
297 (test-ps-js decf2
298 (decf y 10)
299 "y -= 10;")
301 (test-ps-js setf-conditional
302 (setf foo (if x 1 2))
303 "foo = x ? 1 : 2;")
305 (test-ps-js obj-literal-numbers
306 (create 1 "foo")
307 "{ 1 : 'foo' };")
309 (test-ps-js obj-literal-strings
310 (create "foo" 2)
311 "{ 'foo' : 2 };")
313 (test-ps-js getprop-string
314 (getprop foo "bar")
315 "foo['bar'];")
317 (test-ps-js getprop-string1
318 (getprop "bar" 'length)
319 "'bar'.length;")
321 (test-ps-js getprop-progn
322 (getprop (progn (some-fun "abc") "123") "length")
323 "(someFun('abc'), '123')['length'];")
325 (test-ps-js method-call-block
326 ((@ (progn (some-fun "abc") "123") to-string))
327 "(someFun('abc'), '123').toString();")
329 (test-ps-js create-blank
330 (create)
331 "{ };")
333 (test-ps-js blank-object-literal
335 "{ };")
337 (test-ps-js array-literal1
339 "[];")
341 (test-ps-js array-literal2
342 ([])
343 "[];")
345 (test-ps-js array-literal3
346 ([] 1 2 3)
347 "[1, 2, 3];")
349 (test-ps-js array-literal4
350 ([] 1 (2 3))
351 "[1, [2, 3]];")
353 (test-ps-js array-literal5
354 ([] (1 2) ("a" "b"))
355 "[[1, 2], ['a', 'b']];")
357 (test-ps-js defun-rest1
358 (defun foo (&rest bar)
359 (alert (aref bar 1)))
360 "function foo() {
361 var bar = [];
362 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
363 bar[i1] = arguments[i1 + 0];
365 return alert(bar[1]);
366 };")
368 (test-ps-js defun-rest2
369 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
370 "function foo(baz) {
371 var bar = [];
372 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
373 bar[i1] = arguments[i1 + 1];
375 return baz + bar[1];
376 };")
378 (test-ps-js defun-keyword1
379 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
380 "function zoo(foo, bar) {
381 var baz;
382 var _js2 = arguments.length;
383 for (var n1 = 2; n1 < _js2; n1 += 2) {
384 switch (arguments[n1]) {
385 case 'baz':
386 baz = arguments[n1 + 1];
389 if (baz === undefined) {
390 baz = null;
392 return foo + bar + baz;
393 };")
395 (test-ps-js defun-keyword2
396 (defun zoo (&key baz) (return (* baz baz)))
397 "function zoo() {
398 var baz;
399 var _js2 = arguments.length;
400 for (var n1 = 0; n1 < _js2; n1 += 2) {
401 switch (arguments[n1]) {
402 case 'baz':
403 baz = arguments[n1 + 1];
406 if (baz === undefined) {
407 baz = null;
409 return baz * baz;
410 };")
412 (test-ps-js defun-keyword3
413 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
414 "function zoo() {
415 var baz;
416 var bar;
417 var _js2 = arguments.length;
418 for (var n1 = 0; n1 < _js2; n1 += 2) {
419 switch (arguments[n1]) {
420 case 'baz':
421 baz = arguments[n1 + 1];
422 break;
423 case 'bar':
424 bar = arguments[n1 + 1];
427 if (baz === undefined) {
428 baz = null;
430 if (bar === undefined) {
431 bar = 4;
433 return baz * bar;
434 };")
436 (test-ps-js defun-keyword4
437 (defun hello-world (&key ((:my-name-key my-name) 1))
438 my-name)
439 "function helloWorld() {
440 var myName;
441 var _js2 = arguments.length;
442 for (var n1 = 0; n1 < _js2; n1 += 2) {
443 switch (arguments[n1]) {
444 case 'my-name-key':
445 myName = arguments[n1 + 1];
448 if (myName === undefined) {
449 myName = 1;
451 return myName;
452 };")
454 (test-ps-js keyword-funcall1
455 (func :baz 1)
456 "func('baz', 1);")
458 (test-ps-js keyword-funcall2
459 (func :baz 1 :bar foo)
460 "func('baz', 1, 'bar', foo);")
462 (test-ps-js keyword-funcall3
463 (fun a b :baz c)
464 "fun(a, b, 'baz', c);")
466 (test-ps-js cond1
467 (cond ((= x 1) 1))
468 "if (x == 1) {
470 };")
472 (test-ps-js cond2
473 (cond ((= x 1) 2)
474 ((= y (* x 4)) (foo "blah") (* x y)))
475 "if (x == 1) {
477 } else if (y == x * 4) {
478 foo('blah');
479 x * y;
480 };")
482 (test-ps-js if-exp-without-else-returns-null
483 (return (if x 1))
484 "if (x) {
485 return 1;
486 } else {
487 return null;
488 };")
490 (test-ps-js progn-expression-single-statement
491 (return (progn (* x y)))
492 "return x * y;")
494 (test-ps-js cond-expression1
495 (defun foo ()
496 (cond ((< 1 2) (bar "foo") (* 4 5))))
497 "function foo() {
498 if (1 < 2) {
499 bar('foo');
500 return 4 * 5;
502 };")
504 (test-ps-js cond-expression2
505 (defun foo ()
506 (cond ((< 2 1) "foo")
507 ((= 7 7) "bar")))
508 "function foo() {
509 if (2 < 1) {
510 return 'foo';
511 } else if (7 == 7) {
512 return 'bar';
514 };")
516 (test-ps-js cond-expression-final-t-clause
517 (defun foo ()
518 (cond ((< 1 2) (bar "foo") (* 4 5))
519 ((= a b) (+ c d))
520 ((< 1 2 3 4 5) x)
521 (t "foo")))
522 "function foo() {
523 var _cmp3;
524 var _cmp2;
525 var _cmp1;
526 if (1 < 2) {
527 bar('foo');
528 return 4 * 5;
529 } else if (a == b) {
530 return c + d;
531 } else if ((_cmp1 = 2, _cmp2 = 3, _cmp3 = 4, 1 < _cmp1 && _cmp1 < _cmp2 && _cmp2 < _cmp3 && _cmp3 < 5)) {
532 return x;
533 } else {
534 return 'foo';
536 };")
538 (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
539 (defun foo ()
540 (cond ((< 2 1) 5)
541 (t "foo")
542 ((< 1 2) "bar")))
543 "function foo() {
544 if (2 < 1) {
545 return 5;
546 } else {
547 return 'foo';
549 };")
551 (test-ps-js funcall-if-expression
552 ((@ document write)
553 (if (= *linkornot* 1)
554 (ps-html ((:a :href "#"
555 :onclick (ps-inline (transport)))
556 img))
557 img))
558 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport()') + '\">' + img + '</A>' : img);")
560 (test-ps-js negate-number-literal
561 (- 1)
562 "-1;")
564 (test macro-environment1
565 (is (string= (normalize-js-code (let* ((macroname (gensym)))
566 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
567 `(defun test1 ()
568 (macrolet ((,macroname (x) `(aref data ,x)))
569 (when (,macroname x)
570 (setf (,macroname x) 123)))))))
571 (normalize-js-code
572 "function test1() {
573 if (data[x]) {
574 return data[x] = 123;
575 } else {
576 return null;
578 };"))))
580 (test macro-environment2
581 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
582 (defpsmacro macro-environment2-macro (x)
583 `(+ ,outer-lexical-variable ,x))
584 (ps* '(macro-environment2-macro 2))))
585 (normalize-js-code "1 + 2;"))))
587 (test-ps-js ampersand-whole-1
588 (macrolet ((foo (&whole foo bar baz)
589 (declare (ignore bar baz))
590 (format nil "~a" foo)))
591 (foo 1 2))
592 "'(FOO 1 2)';")
594 (test-ps-js keyword-consistent
596 "'x';")
598 (test-ps-js simple-symbol-macrolet
599 (symbol-macrolet ((x 1)) x)
600 "1;")
602 (test-ps-js compound-symbol-macrolet
603 (symbol-macrolet ((x 123)
604 (y (* 2 x)))
606 "2 * 123;")
608 (test-ps-js define-symbol-macro
609 (progn (define-symbol-macro tst-sym-macro 2)
610 tst-sym-macro)
611 "2;")
613 (test-ps-js define-symbol-macro1
614 (progn (define-symbol-macro tst-sym-macro1 2)
615 (foo tst-sym-macro1))
616 "foo(2);")
618 (test-ps-js expression-progn
619 (1+ (progn (foo) (if x 1 2)))
620 "(foo(), x ? 1 : 2) + 1;")
622 (test-ps-js let-decl-in-expression
623 (defun f (x)
624 (if x 1 (let* ((foo x))
625 foo)))
626 "function f(x) {
627 if (x) {
628 return 1;
629 } else {
630 var foo = x;
631 return foo;
633 };")
635 (test-ps-js special-var1
636 (progn (defvar *foo*)
637 (let* ((*foo* 2))
638 (* *foo* 2)))
639 "var FOO;
640 var FOO_TMPSTACK1;
641 try {
642 FOO_TMPSTACK1 = FOO;
643 FOO = 2;
644 FOO * 2;
645 } finally {
646 FOO = FOO_TMPSTACK1;
647 };")
649 (test-ps-js special-var2
650 (progn (defvar *foo*)
651 (let* ((*baz* 3)
652 (*foo* 2))
653 (* *foo* 2 *baz*)))
654 "var FOO;
655 var BAZ = 3;
656 var FOO_TMPSTACK1;
657 try {
658 FOO_TMPSTACK1 = FOO;
659 FOO = 2;
660 FOO * 2 * BAZ;
661 } finally {
662 FOO = FOO_TMPSTACK1;
663 };")
665 (test-ps-js literal1
666 (setf x undefined)
667 "x = undefined;")
669 (test-ps-js literal2
670 (aref this x)
671 "this[x];")
673 (test-ps-js setf-dec1
674 (setf x (- 1 x 2))
675 "x = 1 - x - 2;")
677 (test-ps-js setf-dec2
678 (setf x (- x 1 2))
679 "x = x - 1 - 2;")
681 (test-ps-js special-char-equals
682 blah=
683 "blahequals;")
685 (test-ps-js setf-operator-priority
686 (return (or (getprop cache id)
687 (setf (getprop cache id) ((@ document get-element-by-id) id))))
688 "return cache[id] || (cache[id] = document.getElementById(id));")
690 (test-ps-js aref-operator-priority
691 (aref (if (and x (> (length x) 0))
692 (aref x 0)
695 "(x && x.length > 0 ? x[0] : y)[z];")
697 (test-ps-js aref-operator-priority1
698 (aref (or (getprop x 'y)
699 (getprop a 'b))
701 "(x.y || a.b)[z];")
703 (test-ps-js aref-operator-priority2
704 (aref (if a b c) 0)
705 "(a ? b : c)[0];")
707 (test-ps-js negative-operator-priority
708 (- (if x y z))
709 "-(x ? y : z);")
711 (test-ps-js op-p1
712 (new (or a b))
713 "new (a || b);")
715 (test-ps-js op-p2
716 (delete (if a (or b c) d))
717 "delete (a ? b || c : d);")
719 (test-ps-js op-p3
720 (not (if (or x (not y)) z))
721 "!(x || !y ? z : null);")
723 (test-ps-js op-p4
724 (- (- (* 1 2) 3))
725 "-(1 * 2 - 3);")
727 (test-ps-js op-p5
728 (instanceof (or a b) (if x y z))
729 "((a || b) instanceof (x ? y : z));")
731 (test-ps-js op-p7
732 (or x (if (= x 0) "zero" "empty"))
733 "x || (x == 0 ? 'zero' : 'empty');")
735 (test-ps-js named-op-expression
736 (throw (if a b c))
737 "throw a ? b : c;")
739 (test-ps-js named-op-expression1
740 (typeof (or x y))
741 "typeof (x || y);")
743 (test-ps-js aref-array-expression
744 (aref (or a b c) 0)
745 "(a || b || c)[0];")
747 (test-ps-js getprop-operator
748 (getprop (or a b c) 'd)
749 "(a || b || c).d;")
751 (test-ps-js getprop-parens
752 (getprop (getprop foo 'bar) 'baz)
753 "foo.bar.baz;")
755 (test-ps-js funcall-funcall
756 ((foo))
757 "foo()();")
759 (test-ps-js expression-funcall
760 ((or (@ window eval) eval) foo nil)
761 "(window.eval || eval)(foo, null);")
763 (test-ps-js expression-funcall1
764 (((or (@ window eval) eval) foo nil))
765 "(window.eval || eval)(foo, null)();")
767 (test-ps-js expression-funcall2
768 (((or (@ window eval) eval)) foo nil)
769 "(window.eval || eval)()(foo, null);")
771 (test-ps-js getprop-object-literal
772 (getprop (create a 1) 'a)
773 "({ a : 1 }).a;")
775 (test-ps-js getprop-lambda
776 (getprop (lambda ()) 'prototype)
777 "(function () { return null; }).prototype;")
779 (test-ps-js who-html1
780 (who-ps-html (:span :class "ticker-symbol"
781 :ticker-symbol symbol
782 (:a :href "http://foo.com"
783 symbol)
784 (:span :class "ticker-symbol-popup")))
785 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>';")
787 (test-ps-js flet1
788 ((lambda () (flet ((foo (x)
789 (1+ x)))
790 (return (foo 1)))))
791 "(function () {
792 var foo1 = function (x) {
793 return x + 1;
795 return foo1(1);
796 })();")
798 (test-ps-js flet2
799 (flet ((foo (x) (return (1+ x)))
800 (bar (y) (return (+ 2 y))))
801 (bar (foo 1)))
802 "var foo1 = function (x) {
803 return x + 1;
805 var bar2 = function (y) {
806 return 2 + y;
808 bar2(foo1(1));")
810 (test-ps-js flet3
811 (flet ((foo (x) (return (1+ x)))
812 (bar (y) (return (+ 2 (foo y)))))
813 (bar (foo 1)))
814 "var foo1 = function (x) {
815 return x + 1;
817 var bar2 = function (y) {
818 return 2 + foo(y);
820 bar2(foo1(1));")
822 (test-ps-js labels1
823 ((lambda () (labels ((foo (x)
824 (if (=== 0 x)
826 (+ x (foo (1- x))))))
827 (foo 3))))
828 "(function () {
829 var foo1 = function (x) {
830 if (0 === x) {
831 return 0;
832 } else {
833 return x + foo1(x - 1);
836 return foo1(3);
837 })();")
839 (test-ps-js labels2
840 (labels ((foo (x) (return (1+ (bar x))))
841 (bar (y) (return (+ 2 (foo y)))))
842 (bar (foo 1)))
843 "var foo1 = function (x) {
844 return bar2(x) + 1;
846 var bar2 = function (y) {
847 return 2 + foo1(y);
849 bar2(foo1(1));")
851 (test-ps-js labels3
852 (labels ((foo (x) (return (1+ x)))
853 (bar (y) (return (+ 2 (foo y)))))
854 (bar (foo 1)))
855 "var foo1 = function (x) {
856 return x + 1;
858 var bar2 = function (y) {
859 return 2 + foo1(y);
861 bar2(foo1(1));")
863 (test-ps-js for-loop-var-init-exp
864 ((lambda (x)
865 (return (do* ((y (if x 0 1) (1+ y))
866 (z 0 (1+ z)))
867 ((= y 3) z))))
868 true)
869 "(function (x) {
870 return (function () {
871 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
873 return z;
874 })();
875 })(true);")
877 (test-ps-js math-pi
879 "Math.PI;")
881 (test-ps-js literal-array
882 '(1 2 3)
883 "[1, 2, 3];")
885 (test-ps-js literal-array-1
886 '(1 foo 3)
887 "[1, 'foo', 3];")
889 (test ps-lisp-expands-in-lexical-environment
890 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
892 (test ps*-lisp-expands-in-null-lexical-environment
893 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
895 (test ps*-lisp-expands-in-dynamic-environment
896 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
898 (test ps-lisp-dynamic-environment
899 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))
901 (test-ps-js ps-js-target-version-keyword-test1
902 (defun foo (x y &key bar baz))
903 "function foo(x, y) {
904 var baz;
905 var x1 = Array.prototype.indexOf.call(arguments, 'bar', 2);
906 var bar = -1 == x1 ? null : arguments[x1 + 1];
907 var x2 = Array.prototype.indexOf.call(arguments, 'baz', 2);
908 return baz = -1 == x2 ? null : arguments[x2 + 1];
910 :js-target-version 1.6)
912 (test-ps-js nested-if-expressions1
913 (return (if (if x y z) a b))
914 "if (x ? y : z) {
915 return a;
916 } else {
917 return b;
918 };")
920 (test-ps-js nested-if-expressions2
921 (return (if x y (if z a b)))
922 "if (x) {
923 return y;
924 } else {
925 if (z) {
926 return a;
927 } else {
928 return b;
930 };")
932 (test-ps-js let1
933 (let (x)
934 (+ x x))
935 "var x = null;
936 x + x;")
938 (test-ps-js let2
939 (let ((x 1))
940 (+ x x))
941 "var x = 1;
942 x + x;")
944 (test-ps-js let-x-x
945 (let ((x (1+ x)))
946 (+ x x))
947 "var x1 = x + 1;
948 x1 + x1;")
950 (test-ps-js let3
951 (let ((x 1)
952 (y 2))
953 (+ x x))
954 "var x = 1;
955 var y = 2;
956 x + x;")
958 (test-ps-js let4
959 (let ((x 1)
960 (y (1+ x)))
961 (+ x y))
962 "var x1 = 1;
963 var y = x + 1;
964 x1 + y;")
966 (test-ps-js let5
967 (let ((x 1))
968 (+ x 1)
969 (let ((x (+ x 5)))
970 (+ x 1))
971 (+ x 1))
972 "var x = 1;
973 x + 1;
974 var x1 = x + 5;
975 x1 + 1;
976 x + 1;")
978 (test-ps-js let6
979 (let ((x 2))
980 (let ((x 1)
981 (y (1+ x)))
982 (+ x y)))
983 "var x = 2;
984 var x1 = 1;
985 var y = x + 1;
986 x1 + y;")
988 (test-ps-js let-exp1
989 (lambda ()
990 (let (x)
991 (+ x x)))
992 "function () {
993 var x = null;
994 return x + x;
995 };")
997 (test-ps-js let*1
998 (let* ((x 1))
999 (+ x x))
1000 "var x = 1;
1001 x + x;")
1003 (test-ps-js let*2
1004 (let* ((x 1)
1005 (y (+ x 2)))
1006 (+ x y))
1007 "var x = 1;
1008 var y = x + 2;
1009 x + y;")
1011 (test-ps-js let*3
1012 (let ((x 3))
1013 (let* ((x 1)
1014 (y (+ x 2)))
1015 (+ x y)))
1016 "var x = 3;
1017 var x1 = 1;
1018 var y = x1 + 2;
1019 x1 + y;")
1021 (test-ps-js let*4
1022 (let ((x 3))
1023 (let* ((y (+ x 2))
1024 (x 1))
1025 (+ x y)))
1026 "var x = 3;
1027 var y = x + 2;
1028 var x1 = 1;
1029 x1 + y;")
1031 (test-ps-js symbol-macrolet-var
1032 (symbol-macrolet ((x y))
1033 (var x))
1034 "var y;")
1036 (test-ps-js setf-conditional1
1037 (setf x (unless (null a) (1+ a)))
1038 "x = a != null ? a + 1 : null;")
1040 (test-ps-js setf-let1
1041 (setf x (let ((a 1)) a))
1042 "x = (a = 1, a);")
1044 (test-ps-js setf-let2
1045 (setf x (let ((a (foo)))
1046 (unless (null a)
1047 (1+ a))))
1048 "x = (a = foo(), a != null ? a + 1 : null);")
1050 (test-ps-js symbol-macro-env1
1051 (symbol-macrolet ((bar 1))
1052 (macrolet ((bar (x y) `(+ ,x ,y)))
1053 (bar bar bar)))
1054 "1 + 1;")
1056 (test-ps-js symbol-macrolet-fun1
1057 (symbol-macrolet ((baz +))
1058 (baz 1 2))
1059 "baz(1, 2);")
1061 (test-ps-js lisp2-namespaces1
1062 (let ((list nil))
1063 (setf list (list 1 2 3)))
1064 "var list = null;
1065 list = [1, 2, 3];")
1067 (test-ps-js let-shadows-symbol-macrolet
1068 (symbol-macrolet ((x y))
1069 (let ((x 1))
1070 (+ x x))
1071 (+ x x))
1072 "var x1 = 1;
1073 x1 + x1;
1074 y + y;")
1076 (test-ps-js let-rename-optimization1
1077 (let ((x 1))
1078 (+ x x))
1079 "var x = 1;
1080 x + x;")
1082 (test-ps-js let-rename-optimization2
1083 (lambda (x)
1084 (let ((x (+ 1 x)))
1085 (return x)))
1086 "function (x) {
1087 var x1 = 1 + x;
1088 return x1;
1089 };")
1091 (test-ps-js symbol-macro-array
1092 (symbol-macrolet ((x 1))
1093 (list x))
1094 "[1];")
1096 (test-ps-js symbol-macro-obj
1097 (symbol-macrolet ((x y))
1098 (create x 1))
1099 "{ x : 1 };")
1101 (test-ps-js symbol-macro-conditional1
1102 (symbol-macrolet ((x y))
1103 (if x x x))
1104 "if (y) {
1106 } else {
1108 };")
1110 (test-ps-js symbol-macro-conditional2
1111 (symbol-macrolet ((x y))
1112 (return (if x x x)))
1113 "if (y) {
1114 return y;
1115 } else {
1116 return y;
1117 };")
1119 (test-ps-js flet-apply
1120 (flet ((foo () 'bar))
1121 (apply (function foo) nil))
1122 "var foo1 = function () {
1123 return 'bar';
1125 foo1.apply(this, null);")
1127 (test-ps-js let-apply
1128 (let ((foo (lambda () (return 1))))
1129 (let ((foo (lambda () (return 2))))
1130 (apply foo nil)))
1131 "var foo = function () {
1132 return 1;
1134 var foo1 = function () {
1135 return 2;
1137 foo1.apply(this, null);")
1139 (test-ps-js flet-let
1140 (flet ((x (x) (return (1+ x))))
1141 (let ((x 2))
1142 (x x)))
1143 "var x1 = function (x) {
1144 return x + 1;
1146 var x = 2;
1147 x1(x);")
1149 (test-ps-js let-flet
1150 (let ((x 2))
1151 (flet ((x (x) (return (1+ x))))
1152 (x x)))
1153 "var x = 2;
1154 var x1 = function (x) {
1155 return x + 1;
1157 x1(x);")
1159 (test-ps-js macrolet-let-inteference
1160 (macrolet ((a (n) `(+ ,n 5)))
1161 (let ((a (a 1)))
1162 (let ((b (a (- a 4))))
1163 (+ a b))))
1164 "var a = 1 + 5;
1165 var b = (a - 4) + 5;
1166 a + b;")
1168 (test-ps-js let-subtract-add
1169 (let ((x 1))
1170 (let ((x 2))
1171 (- x x)
1172 (- x)
1173 (decf x)
1174 (incf x)))
1175 "var x = 1;
1176 var x1 = 2;
1177 x1 - x1;
1178 -x1;
1179 --x1;
1180 ++x1;")
1182 (test-ps-js create-reserved-word
1183 (create :default 1)
1184 "{ 'default' : 1 };")
1186 (test-ps-js getprop-reserved-word
1187 (getprop foo :default)
1188 "foo['default'];")
1190 (test-ps-js eval-when-ps-side
1191 (eval-when (:execute)
1193 "5;")
1195 (defvar *lisp-output* nil)
1197 (test eval-when-lisp-side ()
1198 (setf *lisp-output* 'original-value)
1199 (let ((js-output (normalize-js-code
1200 (ps-doc* `(eval-when (:compile-toplevel)
1201 (setf *lisp-output* 'it-works))))))
1202 (is (eql 'it-works *lisp-output*))
1203 (is (string= "" js-output))))
1205 (defpsmacro my-in-package (package-name)
1206 `(eval-when (:compile-toplevel)
1207 (setf *lisp-output* ,package-name)))
1209 (test eval-when-macro-expansion ()
1210 (setf *lisp-output* 'original-value)
1211 (let ((js-output (normalize-js-code
1212 (ps-doc* `(progn
1213 (my-in-package :cl-user)
1214 3)))))
1215 (declare (ignore js-output))
1216 (is (eql :cl-user *lisp-output*))))
1218 (test eval-when-macrolet-expansion ()
1219 (setf *lisp-output* 'original-value)
1220 (let ((js-output (normalize-js-code
1221 (ps-doc* `(macrolet ((my-in-package2 (package-name)
1222 `(eval-when (:compile-toplevel)
1223 (setf *lisp-output* ,package-name))))
1224 (my-in-package2 :cl-user)
1225 3)))))
1226 (declare (ignore js-output))
1227 (is (eql :cl-user *lisp-output*))))
1229 (test-ps-js getprop-keyword
1230 (getprop foo :bar)
1231 "foo['bar'];")
1233 (test-ps-js nary-comparison1
1234 (lambda () (return (< 1 2 3)))
1235 "function () {
1236 var _cmp1;
1237 return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
1238 };")
1240 (test-ps-js chain-getprop1
1241 (chain ($ "foo") (bar x z) frob (baz 5))
1242 "$('foo').bar(x, z).frob.baz(5);")
1244 (test-ps-js chain-getprop2
1245 (chain ($ "foo") bar baz)
1246 "$('foo').bar.baz;")
1248 (test-ps-js chain-getprop3
1249 (chain ($ "foo") bar (x y) baz)
1250 "$('foo').bar.x(y).baz;")
1252 (test-ps-js flet-expression
1253 (1+ (flet ((foo (x) (1+ x)))
1254 (foo 1)))
1255 "(foo1 = function (x) {
1256 return x + 1;
1257 }, foo1(1)) + 1;")
1259 (test-ps-js return-case-break-elimination
1260 (return (case 1
1261 (0 1)
1262 (otherwise 2)))
1263 "switch (1) {
1264 case 0:
1265 return 1;
1266 default:
1267 return 2;
1268 };")
1270 (test-ps-js aplusplus
1272 "aplusplus;")
1274 (test-ps-js astarstar
1276 "astarstar;")
1278 (test-ps-js switch-return-fallthrough
1279 (return
1280 (switch x
1281 (1 (foo) break)
1282 (2 (bar))
1283 (default 4)))
1284 "switch (x) {
1285 case 1:
1286 return foo();
1287 case 2:
1288 bar();
1289 default:
1290 return 4;
1291 };")
1293 (test-ps-js return-last-case
1294 (return
1295 (case x
1296 (a 'eh)
1297 (b 'bee)))
1298 "switch (x) {
1299 case a:
1300 return 'eh';
1301 case b:
1302 return 'bee';
1303 };")
1305 (test-ps-js return-macrolet
1306 (return
1307 (macrolet ((x () 1))
1308 (case (x)
1309 (a 'eh)
1310 (b 'bee))))
1311 "switch (1) {
1312 case a:
1313 return 'eh';
1314 case b:
1315 return 'bee';
1316 };")
1318 (test-ps-js mv-bind1
1319 (multiple-value-bind (a b)
1320 (progn
1321 (returns-mv)
1322 (doesnt))
1323 (alert a)
1324 (alert b))
1325 "returnsMv();
1326 var prevmv2 = arguments['callee']['mv'];
1327 try {
1328 arguments['callee']['mv'] = true;
1329 var a = doesnt();
1330 var mv1 = typeof arguments['callee']['mv'] == 'object' ? arguments['callee']['mv'] : new Array(1);
1331 var b = mv1[0];
1332 alert(a);
1333 alert(b);
1334 } finally {
1335 if (undefined === prevmv2) {
1336 delete arguments['callee']['mv'];
1337 } else {
1338 arguments['callee']['mv'] = prevmv2;
1340 };")
1342 (test-ps-js values0
1343 (values)
1344 "null;")
1346 (test-ps-js values1
1347 (values x)
1348 "x;")
1350 (test-ps-js values2
1351 (values x y)
1352 "var val1_1 = x;
1353 var valrest2 = [y];
1354 if (undefined !== arguments['callee']['caller']['mv']) {
1355 arguments['callee']['caller']['mv'] = valrest2;
1357 val1_1;")
1359 (test-ps-js values3
1360 (values x y z)
1361 "var val1_1 = x;
1362 var valrest2 = [y, z];
1363 if (undefined !== arguments['callee']['caller']['mv']) {
1364 arguments['callee']['caller']['mv'] = valrest2;
1366 val1_1;")
1368 (test-ps-js values-return
1369 (return (values x y))
1370 "var val1_1 = x;
1371 var valrest2 = [y];
1372 if (undefined !== arguments['callee']['caller']['mv']) {
1373 arguments['callee']['caller']['mv'] = valrest2;
1375 return val1_1;")
1377 (test-ps-js return-macrolet
1378 (return
1379 (symbol-macrolet ((x 2))
1380 (loop do (+ x x))))
1381 "for (; true; ) {
1382 2 + 2;
1384 return null;")
1386 (test-ps-js return-cond
1387 (return
1388 (cond ((foo? x) (loop for y in x do (foo y)))
1389 ((bar? x) x)
1390 (t 3)))
1391 "if (foowhat(x)) {
1392 var _js2 = x.length;
1393 var _js1 = 0;
1394 if (_js1 < _js2) {
1395 var y = x[_js1];
1396 while (true) {
1397 foo(y);
1398 _js1 += 1;
1399 if (_js1 >= _js2) {
1400 break;
1402 y = x[_js1];
1405 return null;
1406 } else if (barwhat(x)) {
1407 return x;
1408 } else {
1409 return 3;
1410 };")
1412 (test-ps-js switch-loop
1413 (case x
1414 (1 (dolist (a b))))
1415 "switch (x) {
1416 case 1:
1417 for (var a = null, _js_idx1 = 0; _js_idx1 < b.length; _js_idx1 += 1) {
1418 a = b[_js_idx1];
1420 };")
1422 (test-ps-js switch-folds-blocks
1423 (case x
1424 (1 (loop repeat 3 do (alert "foo"))))
1425 "switch (x) {
1426 case 1:
1427 for (var _js1 = 0; _js1 < 3; _js1 += 1) {
1428 alert('foo');
1430 null;
1431 };")
1433 (test-ps-js setf-places-before-macros
1434 (progn
1435 (defsetf left (el) (offset)
1436 `(setf (@ ,el style left) ,offset))
1437 (macrolet ((left (el)
1438 `(@ ,el offset-left)))
1439 (setf (left x) 10)
1440 (left x)))
1441 "var _js2 = x;
1442 var _js1 = 10;
1443 _js2.style.left = _js1;
1444 x.offsetLeft;")
1446 (test-ps-js for-return
1447 (return (dolist (arg args) (foo arg)))
1448 "for (var arg = null, _js_idx1 = 0; _js_idx1 < args.length; _js_idx1 += 1) {
1449 arg = args[_js_idx1];
1450 foo(arg);
1451 };")
1453 (test-ps-js try-catch-return
1454 (return (try (foo)
1455 (:catch (e)
1456 (bar))
1457 (:finally
1458 (cleanup))))
1459 "try {
1460 return foo();
1461 } catch (e) {
1462 return bar();
1463 } finally {
1464 cleanup();
1465 };")