s/js:funcall/js::funcall/
[clinton/parenscript.git] / t / ps-tests.lisp
1 (in-package :ps-test)
2
3 ;;; Hand-written unit tests
4
5 (eval-when (:compile-toplevel :load-toplevel :execute)
6 (def-suite ps-tests))
7 (in-suite ps-tests)
8
9 (test-ps-js plus-is-not-commutative
10 (setf x (+ "before" x "after"))
11 "x = 'before' + x + 'after';")
12
13 (test-ps-js plus-works-if-first
14 (setf x (+ x "middle" "after"))
15 "x += 'middle' + 'after';")
16
17 (test-ps-js setf-side-effects
18 (progn
19 (let ((x 10))
20 (defun side-effect()
21 (setf x 4)
22 (return 3))
23 (setf x (+ 2 (side-effect) x 5))))
24 "var x = 10;
25 function sideEffect() {
26 x = 4;
27 return 3;
28 };
29 x = 2 + sideEffect() + x + 5;")
30 ;; Parenscript used to optimize incorrectly:
31 ;; var x = 10;
32 ;; function sideEffect() {
33 ;; x = 4;
34 ;; return 3;
35 ;; };
36 ;; x += 2 + sideEffect() + 5;
37 ;;
38 ;; Which is 20, not 14
39
40
41 (test-ps-js method-call-op-form
42 ((@ (+ "" x) to-string))
43 "('' + x).toString();")
44
45 (test-ps-js method-call-op-form-args
46 ((@ (+ "" x) to-string) 1 2 :baz 3)
47 "('' + x).toString(1, 2, 'baz', 3);")
48
49 (test-ps-js method-call-number
50 ((@ 10 to-string))
51 "( 10 ).toString();")
52
53 (test-ps-js method-call-string
54 ((@ "hi" to-string))
55 "'hi'.toString();")
56
57 (test-ps-js method-call-lit-object
58 ((@ (create to-string (lambda () (return "it works"))) to-string))
59 "( { toString : function () { return 'it works'; } } ).toString();")
60
61 (test-ps-js method-call-conditional
62 ((if a x y) 1)
63 "(a ? x : y)(1);")
64
65 (test-ps-js method-call-variable
66 ((@ x to-string))
67 "x.toString();")
68
69 (test-ps-js method-call-array
70 ((@ (list 10 20) to-string))
71 "[ 10, 20 ].toString();")
72
73 (test-ps-js method-call-fn-call
74 ((@ (foo) to-string))
75 "foo().toString();")
76
77 (test-ps-js method-call-lambda-fn
78 ((@ (lambda () (alert 10)) to-string))
79 "( function () { alert(10); } ).toString();")
80
81 (test-ps-js method-call-lambda-call
82 ((@ ((lambda (x) (return x)) 10) to-string))
83 "(function (x) { return x; })(10).toString();")
84
85 (test no-whitespace-before-dot
86 (let* ((str (ps* '((@ ((lambda (x) (return x)) 10) to-string))))
87 (dot-pos (position #\. str :test #'char=))
88 (char-before (elt str (1- dot-pos)))
89 (a-parenthesis #\)))
90 (is (char= char-before a-parenthesis))))
91
92 (test-ps-js simple-slot-value
93 (let ((foo (create a 1)))
94 (alert (slot-value foo 'a)))
95 "var foo = { a : 1 };
96 alert(foo.a);")
97
98 (test-ps-js buggy-slot-value
99 (let ((foo (create a 1))
100 (slot-name "a"))
101 (alert (slot-value foo slot-name)))
102 " var foo = { a : 1 };
103 var slotName = 'a';
104 alert(foo[slotName]);
105 "); Last line was alert(foo.slotName) before bug-fix.
106
107 (test-ps-js buggy-slot-value-two
108 (slot-value foo (get-slot-name))
109 "foo[getSlotName()];")
110
111 (test-ps-js old-case-is-now-switch
112 ;; Switch was "case" before, but that was very non-lispish.
113 ;; For example, this code makes three messages and not one
114 ;; which may have been expected. This is because a switch
115 ;; statment must have a break statement for it to return
116 ;; after the alert. Otherwise it continues on the next
117 ;; clause.
118 (switch (aref blorg i)
119 (1 (alert "one"))
120 (2 (alert "two"))
121 (default (alert "default clause")))
122 "switch (blorg[i]) {
123 case 1: alert('one');
124 case 2: alert('two');
125 default: alert('default clause');
126 };")
127
128 (test-ps-js lisp-like-case
129 (case (aref blorg i)
130 (1 (alert "one"))
131 (2 (alert "two"))
132 (default (alert "default clause")))
133 "switch (blorg[i]) {
134 case 1:
135 alert('one');
136 break;
137 case 2:
138 alert('two');
139 break;
140 default: alert('default clause');
141 };")
142
143
144 (test-ps-js even-lispier-case
145 (case (aref blorg i)
146 ((1 2) (alert "Below three"))
147 (3 (alert "Three"))
148 (t (alert "Something else")))
149 "switch (blorg[i]) {
150 case 1:
151 case 2:
152 alert('Below three');
153 break;
154 case 3:
155 alert('Three');
156 break;
157 default: alert('Something else');
158 };")
159
160 (test-ps-js otherwise-case
161 (case (aref blorg i)
162 (1 (alert "one"))
163 (otherwise (alert "default clause")))
164 "switch (blorg[i]) {
165 case 1:
166 alert('one');
167 break;
168 default: alert('default clause');
169 };")
170
171 (test escape-sequences-in-string
172 (let ((escapes `((#\\ . #\\)
173 (#\b . #\Backspace)
174 (#\f . ,(code-char 12))
175 ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
176 (#\n . #\Newline)
177 (#\r . #\Return)
178 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
179 (#\t . #\Tab)
180 ("u001F" . ,(code-char #x001f));; character below 32
181 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
182 ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
183 (loop for (js-escape . lisp-char) in escapes
184 for generated = (ps-doc* `(let ((x ,(format nil "hello~ahi" lisp-char)))))
185 for wanted = (format nil "var x = 'hello\\~ahi';" js-escape)
186 do (is (string= (normalize-js-code generated) wanted)))))
187
188 (test-ps-js slot-value-setf
189 (setf (slot-value x 'y) (+ (+ a 3) 4))
190 "x.y = (a + 3) + 4;")
191
192 (test-ps-js slot-value-conditional1
193 (slot-value (if zoo foo bar) 'x)
194 "(zoo ? foo : bar).x;")
195
196 (test-ps-js slot-value-conditional2
197 (slot-value (if (not zoo) foo bar) 'x)
198 "(!zoo ? foo : bar).x;")
199
200 (test script-star-eval1
201 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
202
203 (test script-star-eval2
204 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
205
206 (test-ps-js unquoted-nil
207 nil
208 "null;")
209
210 (test-ps-js list-with-single-nil
211 (array nil)
212 "[null];")
213
214 (test-ps-js quoted-nil-is-array
215 'nil
216 "[];")
217
218 (test-ps-js defsetf1
219 (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
220 (setf (baz 1 2) 3))
221 "var _js2 = 1;
222 var _js3 = 2;
223 var _js1 = 3;
224 setBaz(_js2, _js3, _js1);")
225
226 (test-ps-js setf-macroexpands1
227 (macrolet ((baz (x y) `(aref ,x ,y 1)))
228 (setf (baz foo 2) 3))
229 "foo[2][1] = 3;")
230
231 (test-ps-js defsetf-short
232 (progn (defsetf baz set-baz "docstring")
233 (setf (baz 1 2 3) "foo"))
234 "setBaz(1, 2, 3, 'foo');")
235
236 (test-ps-js defun-setf1
237 (progn (defun (setf some-thing) (new-val i1 i2)
238 (setf (aref *some-thing* i1 i2) new-val))
239 (setf (some-thing 1 2) "foo"))
240 "function __setf_someThing(newVal, i1, i2) {
241 SOMETHING[i1][i2] = newVal;
242 };
243 var _js2 = 1;
244 var _js3 = 2;
245 var _js1 = 'foo';
246 __setf_someThing(_js1, _js2, _js3);")
247
248 (test-ps-js defun-optional1
249 (defun test-opt (&optional x) (return (if x "yes" "no")))
250 "function testOpt(x) {
251 if (x === undefined) {
252 x = null;
253 };
254 return x ? 'yes' : 'no';
255 };")
256
257 (test-ps-js defun-optional2
258 (defun foo (x &optional y) (+ x y))
259 "function foo(x, y) {
260 if (y === undefined) {
261 y = null;
262 };
263 x + y;
264 };")
265
266 (test-ps-js defun-optional3
267 (defun blah (&optional (x 0))
268 (return x))
269 "function blah(x) {
270 if (x === undefined) {
271 x = 0;
272 };
273 return x;
274 };")
275
276 (test-ps-js return-nothing
277 (return)
278 "return null;")
279
280 (test-ps-js set-timeout
281 (do-set-timeout (10) (alert "foo"))
282 "setTimeout(function () { alert('foo'); }, 10);")
283
284 (test-ps-js operator-precedence
285 (* 3 (+ 4 5) 6)
286 "3 * (4 + 5) * 6;")
287
288 (test-ps-js operators-1
289 (in prop obj)
290 "prop in obj;")
291
292 (test-ps-js incf1
293 (incf foo bar)
294 "foo += bar;")
295
296 (test-ps-js decf1
297 (decf foo bar)
298 "foo -= bar;")
299
300 (test-ps-js incf2
301 (incf x 5)
302 "x += 5;")
303
304 (test-ps-js decf2
305 (decf y 10)
306 "y -= 10;")
307
308 (test-ps-js setf-conditional
309 (setf foo (if x 1 2))
310 "foo = x ? 1 : 2;")
311
312 (test-ps-js obj-literal-numbers
313 (create 1 "foo")
314 "{ 1 : 'foo' };")
315
316 (test-ps-js obj-literal-strings
317 (create "foo" 2)
318 "{ 'foo' : 2 };")
319
320 (test-ps-js slot-value-string
321 (slot-value foo "bar")
322 "foo['bar'];")
323
324 (test-ps-js slot-value-string1
325 (slot-value "bar" 'length)
326 "'bar'.length;")
327
328 (test-ps-js slot-value-progn
329 (slot-value (progn (some-fun "abc") "123") "length")
330 "(someFun('abc'), '123')['length'];")
331
332 (test-ps-js method-call-block
333 ((@ (progn (some-fun "abc") "123") to-string))
334 "(someFun('abc'), '123').toString();")
335
336 (test-ps-js create-blank
337 (create)
338 "{ };")
339
340 (test-ps-js blank-object-literal
341 {}
342 "{ };")
343
344 (test-ps-js array-literal1
345 []
346 "[];")
347
348 (test-ps-js array-literal2
349 ([])
350 "[];")
351
352 (test-ps-js array-literal3
353 ([] 1 2 3)
354 "[1, 2, 3];")
355
356 (test-ps-js array-literal4
357 ([] 1 (2 3))
358 "[1, [2, 3]];")
359
360 (test-ps-js array-literal5
361 ([] (1 2) ("a" "b"))
362 "[[1, 2], ['a', 'b']];")
363
364 (test-ps-js defun-rest1
365 (defun foo (&rest bar) (alert (aref bar 1)))
366 "function foo() {
367 var bar = [];
368 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
369 bar[i1] = arguments[i1 + 0];
370 };
371 alert(bar[1]);
372 };")
373
374 (test-ps-js defun-rest2
375 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
376 "function foo(baz) {
377 var bar = [];
378 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
379 bar[i1] = arguments[i1 + 1];
380 };
381 return baz + bar[1];
382 };")
383
384 (test-ps-js defun-keyword1
385 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
386 "function zoo(foo, bar) {
387 var baz;
388 var _js2 = arguments.length;
389 for (var n1 = 2; n1 < _js2; n1 += 2) {
390 switch (arguments[n1]) {
391 case 'baz':
392 {
393 baz = arguments[n1 + 1];
394 };
395 };
396 };
397 if (baz === undefined) {
398 baz = null;
399 };
400 return foo + bar + baz;
401 };")
402
403 (test-ps-js defun-keyword2
404 (defun zoo (&key baz) (return (* baz baz)))
405 "function zoo() {
406 var baz;
407 var _js2 = arguments.length;
408 for (var n1 = 0; n1 < _js2; n1 += 2) {
409 switch (arguments[n1]) {
410 case 'baz':
411 {
412 baz = arguments[n1 + 1];
413 };
414 };
415 };
416 if (baz === undefined) {
417 baz = null;
418 };
419 return baz * baz;
420 };")
421
422 (test-ps-js defun-keyword3
423 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
424 "function zoo() {
425 var baz;
426 var bar;
427 var _js2 = arguments.length;
428 for (var n1 = 0; n1 < _js2; n1 += 2) {
429 switch (arguments[n1]) {
430 case 'baz':
431 {
432 baz = arguments[n1 + 1];
433 };
434 break;
435 case 'bar':
436 {
437 bar = arguments[n1 + 1];
438 };
439 };
440 };
441 if (baz === undefined) {
442 baz = null;
443 };
444 if (bar === undefined) {
445 bar = 4;
446 };
447 return baz * bar;
448 };")
449
450 (test-ps-js defun-keyword4
451 (defun hello-world (&key ((:my-name-key my-name) 1))
452 my-name)
453 "function helloWorld() {
454 var myName;
455 var _js2 = arguments.length;
456 for (var n1 = 0; n1 < _js2; n1 += 2) {
457 switch (arguments[n1]) {
458 case 'my-name-key':
459 {
460 myName = arguments[n1 + 1];
461 };
462 };
463 };
464 if (myName === undefined) {
465 myName = 1;
466 };
467 myName;
468 };")
469
470 (test-ps-js keyword-funcall1
471 (func :baz 1)
472 "func('baz', 1);")
473
474 (test-ps-js keyword-funcall2
475 (func :baz 1 :bar foo)
476 "func('baz', 1, 'bar', foo);")
477
478 (test-ps-js keyword-funcall3
479 (fun a b :baz c)
480 "fun(a, b, 'baz', c);")
481
482 (test-ps-js cond1
483 (cond ((= x 1) 1))
484 "if (x == 1) {
485 1;
486 };")
487
488 (test-ps-js cond2
489 (cond ((= x 1) 2)
490 ((= y (* x 4)) (foo "blah") (* x y)))
491 "if (x == 1) {
492 2;
493 } else if (y == x * 4) {
494 foo('blah');
495 x * y;
496 };")
497
498 (test-ps-js if-exp-without-else-returns-null
499 (return (if x 1))
500 "return x ? 1 : null;")
501
502 (test-ps-js progn-expression-single-statement
503 (return (progn (* x y)))
504 "return x * y;")
505
506 (test-ps-js cond-expression1
507 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
508 "function foo() {
509 return 1 < 2 ? (bar('foo'), 4 * 5) : null;
510 };")
511
512 (test-ps-js cond-expression2
513 (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar"))))
514 "function foo() {
515 return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null);
516 };")
517
518 (test-ps-js cond-expression-final-t-clause
519 (defun foo ()
520 (return (cond ((< 1 2) (bar "foo") (* 4 5))
521 ((= a b) (+ c d))
522 ((< 1 2 3 4 5) x)
523 (t "foo"))))
524 "function foo() {
525 var _cmp3;
526 var _cmp2;
527 var _cmp1;
528 return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : ((_cmp1 = 2, _cmp2 = 3, _cmp3 = 4, 1 < _cmp1 && _cmp1 < _cmp2 && _cmp2 < _cmp3 && _cmp3 < 5) ? x : 'foo'));
529 };")
530
531 (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
532 (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar"))))
533 "function foo() {
534 return 2 < 1 ? 5 : 'foo';
535 };")
536
537 (test-ps-js funcall-if-expression
538 ((@ document write)
539 (if (= *linkornot* 1)
540 (ps-html ((:a :href "#"
541 :onclick (ps-inline (transport)))
542 img))
543 img))
544 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport()') + '\">' + img + '</A>' : img);")
545
546 (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test!
547 (- 1)
548 "-1;")
549
550 (test macro-environment1
551 (is (string= (normalize-js-code (let* ((macroname (gensym)))
552 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
553 `(defun test1 ()
554 (macrolet ((,macroname (x) `(aref data ,x)))
555 (when (,macroname x)
556 (setf (,macroname x) 123)))))))
557 (normalize-js-code
558 "function test1() {
559 if (data[x]) {
560 data[x] = 123;
561 };
562 };
563 "))))
564
565 (test macro-environment2
566 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
567 (defpsmacro macro-environment2-macro (x)
568 `(+ ,outer-lexical-variable ,x))
569 (ps* '(macro-environment2-macro 2))))
570 (normalize-js-code "1 + 2;"))))
571
572 (test-ps-js ampersand-whole-1
573 (macrolet ((foo (&whole foo bar baz)
574 (declare (ignore bar baz))
575 (format nil "~a" foo)))
576 (foo 1 2))
577 "'(FOO 1 2)';")
578
579 (test-ps-js keyword-consistent
580 :x
581 "'x';")
582
583 (test-ps-js simple-symbol-macrolet
584 (symbol-macrolet ((x 1)) x)
585 "1;")
586
587 (test-ps-js compound-symbol-macrolet
588 (symbol-macrolet ((x 123)
589 (y (* 2 x)))
590 y)
591 "2 * 123;")
592
593 (test-ps-js define-symbol-macro
594 (progn (define-symbol-macro tst-sym-macro 2)
595 tst-sym-macro)
596 "2;")
597
598 (test-ps-js define-symbol-macro1
599 (progn (define-symbol-macro tst-sym-macro1 2)
600 (foo tst-sym-macro1))
601 "foo(2);")
602
603 (test-ps-js expression-progn
604 (defun f () (return (progn (foo) (if x 1 2))))
605 "function f() {
606 return (foo(), x ? 1 : 2);
607 };")
608
609 (test-ps-js let-decl-in-expression
610 (defun f (x)
611 (return (if x
612 1
613 (let* ((foo x))
614 foo))))
615 "function f(x) {
616 var foo;
617 return x ? 1 : (foo = x, foo);
618 };")
619
620 (test-ps-js special-var1
621 (progn (defvar *foo*)
622 (let* ((*foo* 2))
623 (* *foo* 2)))
624 "var FOO;
625 var FOO_TMPSTACK1;
626 try {
627 FOO_TMPSTACK1 = FOO;
628 FOO = 2;
629 FOO * 2;
630 } finally {
631 FOO = FOO_TMPSTACK1;
632 };")
633
634 (test-ps-js special-var2
635 (progn (defvar *foo*)
636 (let* ((*baz* 3)
637 (*foo* 2))
638 (* *foo* 2 *baz*)))
639 "var FOO;
640 var BAZ = 3;
641 var FOO_TMPSTACK1;
642 try {
643 FOO_TMPSTACK1 = FOO;
644 FOO = 2;
645 FOO * 2 * BAZ;
646 } finally {
647 FOO = FOO_TMPSTACK1;
648 };")
649
650 (test-ps-js literal1
651 (setf x undefined)
652 "x = undefined;")
653
654 (test-ps-js literal2
655 (aref this x)
656 "this[x];")
657
658 (test-ps-js setf-dec1
659 (setf x (- 1 x 2))
660 "x = 1 - x - 2;")
661
662 (test-ps-js setf-dec2
663 (setf x (- x 1 2))
664 "x = x - 1 - 2;")
665
666 (test-ps-js special-char-equals
667 blah=
668 "blahequals;")
669
670 (test-ps-js setf-operator-priority
671 (return (or (slot-value cache id)
672 (setf (slot-value cache id) ((@ document get-element-by-id) id))))
673 "return cache[id] || (cache[id] = document.getElementById(id));")
674
675 (test-ps-js aref-operator-priority
676 (aref (if (and x (> (length x) 0))
677 (aref x 0)
678 y)
679 z)
680 "(x && x.length > 0 ? x[0] : y)[z];")
681
682 (test-ps-js aref-operator-priority1
683 (aref (or (slot-value x 'y)
684 (slot-value a 'b))
685 z)
686 "(x.y || a.b)[z];")
687
688 (test-ps-js aref-operator-priority2
689 (aref (if a b c) 0)
690 "(a ? b : c)[0];")
691
692 (test-ps-js negative-operator-priority
693 (- (if x y z))
694 "-(x ? y : z);")
695
696 (test-ps-js op-p1
697 (new (or a b))
698 "new (a || b);")
699
700 (test-ps-js op-p2
701 (delete (if a (or b c) d))
702 "delete (a ? b || c : d);")
703
704 (test-ps-js op-p3
705 (not (if (or x (not y)) z))
706 "!(x || !y ? z : null);")
707
708 (test-ps-js op-p4
709 (- (- (* 1 2) 3))
710 "-(1 * 2 - 3);")
711
712 (test-ps-js op-p5
713 (instanceof (or a b) (if x y z))
714 "((a || b) instanceof (x ? y : z));")
715
716 (test-ps-js op-p7
717 (or x (if (= x 0) "zero" "empty"))
718 "x || (x == 0 ? 'zero' : 'empty');")
719
720 (test-ps-js named-op-expression
721 (throw (if a b c))
722 "throw a ? b : c;")
723
724 (test-ps-js named-op-expression1
725 (typeof (or x y))
726 "typeof (x || y);")
727
728 (test-ps-js aref-array-expression
729 (aref (or a b c) 0)
730 "(a || b || c)[0];")
731
732 (test-ps-js slot-value-operator
733 (slot-value (or a b c) 'd)
734 "(a || b || c).d;")
735
736 (test-ps-js slot-value-parens
737 (slot-value (slot-value foo 'bar) 'baz)
738 "foo.bar.baz;")
739
740 (test-ps-js funcall-funcall
741 ((foo))
742 "foo()();")
743
744 (test-ps-js expression-funcall
745 ((or (@ window eval) eval) foo nil)
746 "(window.eval || eval)(foo, null);")
747
748 (test-ps-js expression-funcall1
749 (((or (@ window eval) eval) foo nil))
750 "(window.eval || eval)(foo, null)();")
751
752 (test-ps-js expression-funcall2
753 (((or (@ window eval) eval)) foo nil)
754 "(window.eval || eval)()(foo, null);")
755
756 (test-ps-js slot-value-object-literal
757 (slot-value (create a 1) 'a)
758 "({ a : 1 }).a;")
759
760 (test-ps-js slot-value-lambda
761 (slot-value (lambda ()) 'prototype)
762 "(function () { }).prototype;")
763
764 (test-ps-js who-html1
765 (who-ps-html (:span :class "ticker-symbol"
766 :ticker-symbol symbol
767 (:a :href "http://foo.com"
768 symbol)
769 (:span :class "ticker-symbol-popup")))
770 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>';")
771
772 (test-ps-js flet1
773 ((lambda () (flet ((foo (x)
774 (return (1+ x))))
775 (return (foo 1)))))
776 "(function () {
777 var foo1 = function (x) {
778 return x + 1;
779 };
780 return foo1(1);
781 })();")
782
783 (test-ps-js flet2
784 (flet ((foo (x) (return (1+ x)))
785 (bar (y) (return (+ 2 y))))
786 (bar (foo 1)))
787 "var foo1 = function (x) {
788 return x + 1;
789 };
790 var bar2 = function (y) {
791 return 2 + y;
792 };
793 bar2(foo1(1));")
794
795 (test-ps-js flet3
796 (flet ((foo (x) (return (1+ x)))
797 (bar (y) (return (+ 2 (foo y)))))
798 (bar (foo 1)))
799 "var foo1 = function (x) {
800 return x + 1;
801 };
802 var bar2 = function (y) {
803 return 2 + foo(y);
804 };
805 bar2(foo1(1));")
806
807 (test-ps-js labels1
808 ((lambda () (labels ((foo (x)
809 (return (if (=== 0 x)
810 0
811 (+ x (foo (1- x)))))))
812 (return (foo 3)))))
813 "(function () {
814 var foo1 = function (x) {
815 return 0 === x ? 0 : x + foo1(x - 1);
816 };
817 return foo1(3);
818 })();")
819
820 (test-ps-js labels2
821 (labels ((foo (x) (return (1+ (bar x))))
822 (bar (y) (return (+ 2 (foo y)))))
823 (bar (foo 1)))
824 "var foo1 = function (x) {
825 return bar2(x) + 1;
826 };
827 var bar2 = function (y) {
828 return 2 + foo1(y);
829 };
830 bar2(foo1(1));")
831
832 (test-ps-js labels3
833 (labels ((foo (x) (return (1+ x)))
834 (bar (y) (return (+ 2 (foo y)))))
835 (bar (foo 1)))
836 "var foo1 = function (x) {
837 return x + 1;
838 };
839 var bar2 = function (y) {
840 return 2 + foo1(y);
841 };
842 bar2(foo1(1));")
843
844 (test-ps-js for-loop-var-init-exp
845 ((lambda (x)
846 (return (do* ((y (if x 0 1) (1+ y))
847 (z 0 (1+ z)))
848 ((= y 3) z))))
849 true)
850 "(function (x) {
851 return (function () {
852 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
853 };
854 return z;
855 })();
856 })(true);")
857
858 (test-ps-js math-pi
859 pi
860 "Math.PI;")
861
862 (test-ps-js literal-array
863 '(1 2 3)
864 "[1, 2, 3];")
865
866 (test-ps-js literal-array-1
867 '(1 foo 3)
868 "[1, 'foo', 3];")
869
870 (test ps-lisp-expands-in-lexical-environment
871 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
872
873 (test ps*-lisp-expands-in-null-lexical-environment
874 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
875
876 (test ps*-lisp-expands-in-dynamic-environment
877 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
878
879 (test ps-lisp-dynamic-environment
880 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))
881
882 (test-ps-js ps-js-target-version-keyword-test1
883 (defun foo (x y &key bar baz))
884 "function foo(x, y) {
885 var x1 = Array.prototype.indexOf.call(arguments, 'bar', 2);
886 var bar = -1 == x1 ? null : arguments[x1 + 1];
887 var x2 = Array.prototype.indexOf.call(arguments, 'baz', 2);
888 var baz = -1 == x2 ? null : arguments[x2 + 1];
889 };"
890 :js-target-version 1.6)
891
892 (test-ps-js nested-if-expressions1
893 (return (if (if x y z) a b))
894 "return (x ? y : z) ? a : b;")
895
896 (test-ps-js nested-if-expressions2
897 (return (if x y (if z a b)))
898 "return x ? y : (z ? a : b);")
899
900 (test-ps-js let1
901 (let (x)
902 (+ x x))
903 "var x = null;
904 x + x;")
905
906 (test-ps-js let2
907 (let ((x 1))
908 (+ x x))
909 "var x = 1;
910 x + x;")
911
912 (test-ps-js let-x-x
913 (let ((x (1+ x)))
914 (+ x x))
915 "var x1 = x + 1;
916 x1 + x1;")
917
918 (test-ps-js let3
919 (let ((x 1)
920 (y 2))
921 (+ x x))
922 "var x = 1;
923 var y = 2;
924 x + x;")
925
926 (test-ps-js let4
927 (let ((x 1)
928 (y (1+ x)))
929 (+ x y))
930 "var x1 = 1;
931 var y = x + 1;
932 x1 + y;")
933
934 (test-ps-js let5
935 (let ((x 1))
936 (+ x 1)
937 (let ((x (+ x 5)))
938 (+ x 1))
939 (+ x 1))
940 "var x = 1;
941 x + 1;
942 var x1 = x + 5;
943 x1 + 1;
944 x + 1;")
945
946 (test-ps-js let6
947 (let ((x 2))
948 (let ((x 1)
949 (y (1+ x)))
950 (+ x y)))
951 "var x = 2;
952 var x1 = 1;
953 var y = x + 1;
954 x1 + y;")
955
956 (test-ps-js let-exp1
957 (lambda ()
958 (return (let (x) (+ x x))))
959 "function () {
960 var x;
961 return (x = null, x + x);
962 };")
963
964 (test-ps-js let*1
965 (let* ((x 1)) (+ x x))
966 "var x = 1;
967 x + x;")
968
969 (test-ps-js let*2
970 (let* ((x 1)
971 (y (+ x 2)))
972 (+ x y))
973 "var x = 1;
974 var y = x + 2;
975 x + y;")
976
977 (test-ps-js let*3
978 (let ((x 3))
979 (let* ((x 1)
980 (y (+ x 2)))
981 (+ x y)))
982 "var x = 3;
983 var x1 = 1;
984 var y = x1 + 2;
985 x1 + y;")
986
987 (test-ps-js let*4
988 (let ((x 3))
989 (let* ((y (+ x 2))
990 (x 1))
991 (+ x y)))
992 "var x = 3;
993 var y = x + 2;
994 var x1 = 1;
995 x1 + y;")
996
997 (test-ps-js symbol-macrolet-var
998 (symbol-macrolet ((x y))
999 (var x))
1000 "var y;")
1001
1002 (test-ps-js setf-conditional1
1003 (setf x (unless (null a) (1+ a)))
1004 "x = a != null ? a + 1 : null;")
1005
1006 (test-ps-js setf-let1
1007 (setf x (let ((a 1)) a))
1008 "x = (a = 1, a);")
1009
1010 (test-ps-js setf-let2
1011 (setf x (let ((a (foo)))
1012 (unless (null a)
1013 (1+ a))))
1014 "x = (a = foo(), a != null ? a + 1 : null);")
1015
1016 (test-ps-js symbol-macro-env1
1017 (symbol-macrolet ((bar 1))
1018 (macrolet ((bar (x y) `(+ ,x ,y)))
1019 (bar bar bar)))
1020 "1 + 1;")
1021
1022 (test-ps-js symbol-macrolet-fun1
1023 (symbol-macrolet ((baz +))
1024 (baz 1 2))
1025 "baz(1, 2);")
1026
1027 (test-ps-js lisp2-namespaces1
1028 (let ((list nil))
1029 (setf list (list 1 2 3)))
1030 "var list = null;
1031 list = [1, 2, 3];")
1032
1033 (test-ps-js let-shadows-symbol-macrolet
1034 (symbol-macrolet ((x y))
1035 (let ((x 1))
1036 (+ x x))
1037 (+ x x))
1038 "var x1 = 1;
1039 x1 + x1;
1040 y + y;")
1041
1042 (test-ps-js let-rename-optimization1
1043 (let ((x 1))
1044 (+ x x))
1045 "var x = 1;
1046 x + x;")
1047
1048 (test-ps-js let-rename-optimization2
1049 (lambda (x)
1050 (let ((x (+ 1 x)))
1051 (return x)))
1052 "function (x) {
1053 var x1 = 1 + x;
1054 return x1;
1055 };")
1056
1057 (test-ps-js symbol-macro-array
1058 (symbol-macrolet ((x 1))
1059 (list x))
1060 "[1];")
1061
1062 (test-ps-js symbol-macro-obj
1063 (symbol-macrolet ((x y))
1064 (create x 1))
1065 "{ y : 1 };")
1066
1067 (test-ps-js symbol-macro-conditional1
1068 (symbol-macrolet ((x y))
1069 (if x x x))
1070 "if (y) {
1071 y;
1072 } else {
1073 y;
1074 };")
1075
1076 (test-ps-js symbol-macro-conditional2
1077 (symbol-macrolet ((x y))
1078 (return (if x x x)))
1079 "return y ? y : y;")
1080
1081 (test-ps-js flet-apply
1082 (flet ((foo () 'bar))
1083 (apply (function foo) nil))
1084 "var foo1 = function () {
1085 'bar';
1086 };
1087 foo1.apply(this, null);")
1088
1089 (test-ps-js let-apply
1090 (let ((foo (lambda () (return 1))))
1091 (let ((foo (lambda () (return 2))))
1092 (apply foo nil)))
1093 "var foo = function () {
1094 return 1;
1095 };
1096 var foo1 = function () {
1097 return 2;
1098 };
1099 foo1.apply(this, null);")
1100
1101 (test-ps-js flet-let
1102 (flet ((x (x) (return (1+ x))))
1103 (let ((x 2))
1104 (x x)))
1105 "var x1 = function (x) {
1106 return x + 1;
1107 };
1108 var x = 2;
1109 x1(x);")
1110
1111 (test-ps-js let-flet
1112 (let ((x 2))
1113 (flet ((x (x) (return (1+ x))))
1114 (x x)))
1115 "var x = 2;
1116 var x1 = function (x) {
1117 return x + 1;
1118 };
1119 x1(x);")
1120
1121 (test-ps-js macrolet-let-inteference
1122 (macrolet ((a (n) `(+ ,n 5)))
1123 (let ((a (a 1)))
1124 (let ((b (a (- a 4))))
1125 (+ a b))))
1126 "var a = 1 + 5;
1127 var b = (a - 4) + 5;
1128 a + b;")
1129
1130 (test-ps-js let-subtract-add
1131 (let ((x 1))
1132 (let ((x 2))
1133 (- x x)
1134 (- x)
1135 (decf x)
1136 (incf x)))
1137 "var x = 1;
1138 var x1 = 2;
1139 x1 - x1;
1140 -x1;
1141 --x1;
1142 ++x1;")
1143
1144 (test-ps-js create-reserved-word
1145 (create :default 1)
1146 "{ 'default' : 1 };")
1147
1148 (test-ps-js slot-value-reserved-word
1149 (slot-value foo :default)
1150 "foo['default'];")
1151
1152 (test-ps-js eval-when-ps-side
1153 (eval-when (:execute)
1154 5)
1155 "5;")
1156
1157 (defvar *lisp-output* nil)
1158
1159 (test eval-when-lisp-side ()
1160 (setf *lisp-output* 'original-value)
1161 (let ((js-output (normalize-js-code
1162 (ps-doc* `(eval-when (:compile-toplevel)
1163 (setf *lisp-output* 'it-works))))))
1164 (is (eql 'it-works *lisp-output*))
1165 (is (string= "" js-output))))
1166
1167 (defpsmacro my-in-package (package-name)
1168 `(eval-when (:compile-toplevel)
1169 (setf *lisp-output* ,package-name)))
1170
1171 (test eval-when-macro-expansion ()
1172 (setf *lisp-output* 'original-value)
1173 (let ((js-output (normalize-js-code
1174 (ps-doc* `(progn
1175 (my-in-package :cl-user)
1176 3)))))
1177 (declare (ignore js-output))
1178 (is (eql :cl-user *lisp-output*))))
1179
1180 (test eval-when-macrolet-expansion ()
1181 (setf *lisp-output* 'original-value)
1182 (let ((js-output (normalize-js-code
1183 (ps-doc* `(macrolet ((my-in-package2 (package-name)
1184 `(eval-when (:compile-toplevel)
1185 (setf *lisp-output* ,package-name))))
1186 (my-in-package2 :cl-user)
1187 3)))))
1188 (declare (ignore js-output))
1189 (is (eql :cl-user *lisp-output*))))
1190
1191 (test-ps-js slot-value-keyword
1192 (slot-value foo :bar)
1193 "foo['bar'];")
1194
1195 (test-ps-js nary-comparison1
1196 (lambda () (return (< 1 2 3)))
1197 "function () {
1198 var _cmp1;
1199 return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
1200 };")
1201
1202 (test-ps-js chain-slot-value1
1203 (chain ($ "foo") (bar x z) frob (baz 5))
1204 "$('foo').bar(x, z).frob.baz(5);")
1205
1206 (test-ps-js chain-slot-value2
1207 (chain ($ "foo") bar baz)
1208 "$('foo').bar.baz;")
1209
1210 (test-ps-js chain-slot-value3
1211 (chain ($ "foo") bar (x y) baz)
1212 "$('foo').bar.x(y).baz;")