Substantially modified the way Parenscript compilation and
[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 x1 = 10;
25 function sideEffect() {
26 x1 = 4;
27 return 3;
28 };
29 x1 = 2 + sideEffect() + x1 + 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 foo1 = { a : 1 };
96 alert(foo1.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 foo1 = { a : 1 };
103 var slotName2 = 'a';
104 alert(foo1[slotName2]);
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 x1 = '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_4 = 1;
222 var _js3_5 = 2;
223 var _js1_6 = 3;
224 setBaz(_js2_4, _js3_5, _js1_6);")
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_4 = 1;
244 var _js3_5 = 2;
245 var _js1_6 = 'foo';
246 __setf_someThing(_js1_6, _js2_4, _js3_5);")
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 () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo"))))
520 "function foo() {
521 return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo'));
522 };")
523
524 (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
525 (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar"))))
526 "function foo() {
527 return 2 < 1 ? 5 : 'foo';
528 };")
529
530 (test-ps-js funcall-if-expression
531 ((@ document write)
532 (if (= *linkornot* 1)
533 (ps-html ((:a :href "#"
534 :onclick (ps-inline (transport)))
535 img))
536 img))
537 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">' + img + '</A>' : img);")
538
539 (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test!
540 (- 1)
541 "-1;")
542
543 (test macro-environment1
544 (is (string= (normalize-js-code (let* ((macroname (gensym)))
545 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
546 `(defun test1 ()
547 (macrolet ((,macroname (x) `(aref data ,x)))
548 (when (,macroname x)
549 (setf (,macroname x) 123)))))))
550 (normalize-js-code
551 "function test1() {
552 if (data[x]) {
553 data[x] = 123;
554 };
555 };
556 "))))
557
558 (test macro-environment2
559 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
560 (defpsmacro macro-environment2-macro (x)
561 `(+ ,outer-lexical-variable ,x))
562 (ps* '(macro-environment2-macro 2))))
563 (normalize-js-code "1 + 2;"))))
564
565 (test-ps-js ampersand-whole-1
566 (macrolet ((foo (&whole foo bar baz)
567 (declare (ignore bar baz))
568 (format nil "~a" foo)))
569 (foo 1 2))
570 "'(FOO 1 2)';")
571
572 (test-ps-js keyword-consistent
573 :x
574 "'x';")
575
576 (test-ps-js simple-symbol-macrolet
577 (symbol-macrolet ((x 1)) x)
578 "1;")
579
580 (test-ps-js compound-symbol-macrolet
581 (symbol-macrolet ((x 123)
582 (y (* 2 x)))
583 y)
584 "2 * 123;")
585
586 (test-ps-js define-symbol-macro
587 (progn (define-symbol-macro tst-sym-macro 2)
588 tst-sym-macro)
589 "2;")
590
591 (test-ps-js expression-progn
592 (defun f () (return (progn (foo) (if x 1 2))))
593 "function f() {
594 return (foo(), x ? 1 : 2);
595 };")
596
597 (test-ps-js let-decl-in-expression
598 (defun f (x) (return (if x 1 (let* ((foo x)) foo))))
599 "function f(x) {
600 var foo1;
601 return x ? 1 : (foo1 = x, foo1);
602 };")
603
604 (test-ps-js special-var1
605 (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2)))
606 "var FOO;
607 var FOO1;
608 try {
609 FOO1 = FOO;
610 FOO = 2;
611 FOO * 2;
612 } finally {
613 FOO = FOO1;
614 };")
615
616 (test-ps-js special-var2
617 (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*)))
618 "var FOO;
619 var BAZ1 = 3;
620 var FOO2;
621 try {
622 FOO2 = FOO;
623 FOO = 2;
624 FOO * 2 * BAZ1;
625 } finally {
626 FOO = FOO2;
627 };")
628
629 (test-ps-js literal1
630 (setf x undefined)
631 "x = undefined;")
632
633 (test-ps-js literal2
634 (aref this x)
635 "this[x];")
636
637 (test-ps-js setf-dec1
638 (setf x (- 1 x 2))
639 "x = 1 - x - 2;")
640
641 (test-ps-js setf-dec2
642 (setf x (- x 1 2))
643 "x = x - 1 - 2;")
644
645 (test-ps-js special-char-equals
646 blah=
647 "blahequals;")
648
649 (test-ps-js setf-operator-priority
650 (return (or (slot-value cache id)
651 (setf (slot-value cache id) ((@ document get-element-by-id) id))))
652 "return cache[id] || (cache[id] = document.getElementById(id));")
653
654 (test-ps-js aref-operator-priority
655 (aref (if (and x (> (length x) 0))
656 (aref x 0)
657 y)
658 z)
659 "(x && x.length > 0 ? x[0] : y)[z];")
660
661 (test-ps-js aref-operator-priority1
662 (aref (or (slot-value x 'y)
663 (slot-value a 'b))
664 z)
665 "(x.y || a.b)[z];")
666
667 (test-ps-js aref-operator-priority2
668 (aref (if a b c) 0)
669 "(a ? b : c)[0];")
670
671 (test-ps-js negative-operator-priority
672 (- (if x y z))
673 "-(x ? y : z);")
674
675 (test-ps-js op-p1
676 (new (or a b))
677 "new (a || b);")
678
679 (test-ps-js op-p2
680 (delete (if a (or b c) d))
681 "delete (a ? b || c : d);")
682
683 (test-ps-js op-p3
684 (not (if (or x (not y)) z))
685 "!(x || !y ? z : null);")
686
687 (test-ps-js op-p4
688 (- (- (* 1 2) 3))
689 "-(1 * 2 - 3);")
690
691 (test-ps-js op-p5
692 (instanceof (or a b) (if x y z))
693 "((a || b) instanceof (x ? y : z));")
694
695 (test-ps-js op-p7
696 (or x (if (= x 0) "zero" "empty"))
697 "x || (x == 0 ? 'zero' : 'empty');")
698
699 (test-ps-js named-op-expression
700 (throw (if a b c))
701 "throw a ? b : c;")
702
703 (test-ps-js named-op-expression1
704 (typeof (or x y))
705 "typeof (x || y);")
706
707 (test-ps-js aref-array-expression
708 (aref (or a b c) 0)
709 "(a || b || c)[0];")
710
711 (test-ps-js slot-value-operator
712 (slot-value (or a b c) 'd)
713 "(a || b || c).d;")
714
715 (test-ps-js slot-value-parens
716 (slot-value (slot-value foo 'bar) 'baz)
717 "foo.bar.baz;")
718
719 (test-ps-js funcall-funcall
720 ((foo))
721 "foo()();")
722
723 (test-ps-js expression-funcall
724 ((or (@ window eval) eval) foo nil)
725 "(window.eval || eval)(foo, null);")
726
727 (test-ps-js expression-funcall1
728 (((or (@ window eval) eval) foo nil))
729 "(window.eval || eval)(foo, null)();")
730
731 (test-ps-js expression-funcall2
732 (((or (@ window eval) eval)) foo nil)
733 "(window.eval || eval)()(foo, null);")
734
735 (test-ps-js slot-value-object-literal
736 (slot-value (create :a 1) 'a)
737 "({ a : 1 }).a;")
738
739 (test-ps-js slot-value-lambda
740 (slot-value (lambda ()) 'prototype)
741 "(function () { }).prototype;")
742
743 (test-ps-js who-html1
744 (who-ps-html (:span :class "ticker-symbol"
745 :ticker-symbol symbol
746 (:a :href "http://foo.com"
747 symbol)
748 (:span :class "ticker-symbol-popup")))
749 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>';")
750
751 (test-ps-js flet1
752 ((lambda () (flet ((foo (x)
753 (return (1+ x))))
754 (return (foo 1)))))
755 "(function () {
756 var foo1 = function (x) {
757 return x + 1;
758 };
759 return foo1(1);
760 })();")
761
762 (test-ps-js flet2
763 (flet ((foo (x) (return (1+ x)))
764 (bar (y) (return (+ 2 y))))
765 (bar (foo 1)))
766 "var foo1 = function (x) {
767 return x + 1;
768 };
769 var bar2 = function (y) {
770 return 2 + y;
771 };
772 bar2(foo1(1));")
773
774 (test-ps-js flet3
775 (flet ((foo (x) (return (1+ x)))
776 (bar (y) (return (+ 2 (foo y)))))
777 (bar (foo 1)))
778 "var foo1 = function (x) {
779 return x + 1;
780 };
781 var bar2 = function (y) {
782 return 2 + foo(y);
783 };
784 bar2(foo1(1));")
785
786 (test-ps-js labels1
787 ((lambda () (labels ((foo (x)
788 (return (if (=== 0 x)
789 0
790 (+ x (foo (1- x)))))))
791 (return (foo 3)))))
792 "(function () {
793 var foo1 = function (x) {
794 return 0 === x ? 0 : x + foo1(x - 1);
795 };
796 return foo1(3);
797 })();")
798
799 (test-ps-js labels2
800 (labels ((foo (x) (return (1+ (bar x))))
801 (bar (y) (return (+ 2 (foo y)))))
802 (bar (foo 1)))
803 "var foo1 = function (x) {
804 return bar2(x) + 1;
805 };
806 var bar2 = function (y) {
807 return 2 + foo1(y);
808 };
809 bar2(foo1(1));")
810
811 (test-ps-js labels3
812 (labels ((foo (x) (return (1+ x)))
813 (bar (y) (return (+ 2 (foo y)))))
814 (bar (foo 1)))
815 "var foo1 = function (x) {
816 return x + 1;
817 };
818 var bar2 = function (y) {
819 return 2 + foo1(y);
820 };
821 bar2(foo1(1));")
822
823 (test-ps-js for-loop-var-init-exp
824 ((lambda (x)
825 (return (do* ((y (if x 0 1) (1+ y))
826 (z 0 (1+ z)))
827 ((= y 3) z))))
828 true)
829 "(function (x) {
830 return (function () {
831 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
832 };
833 return z;
834 })();
835 })(true);")
836
837 (test-ps-js math-pi
838 pi
839 "Math.PI;")
840
841 (test-ps-js literal-array
842 '(1 2 3)
843 "[1, 2, 3];")
844
845 (test-ps-js literal-array-1
846 '(1 foo 3)
847 "[1, 'foo', 3];")
848
849 (test ps-lisp-expands-in-lexical-environment
850 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
851
852 (test ps*-lisp-expands-in-null-lexical-environment
853 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
854
855 (test ps*-lisp-expands-in-dynamic-environment
856 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
857
858 (test ps-lisp-dynamic-environment
859 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))
860
861 (test-ps-js ps-js-target-version-keyword-test1
862 (defun foo (x y &key bar baz))
863 "function foo(x, y) {
864 var x1_3 = Array.prototype.indexOf.call(arguments, 'bar', 2);
865 var bar = -1 == x1_3 ? null : arguments[x1_3 + 1];
866 var x2_4 = Array.prototype.indexOf.call(arguments, 'baz', 2);
867 var baz = -1 == x2_4 ? null : arguments[x2_4 + 1];
868 };"
869 :js-target-version 1.6)
870
871 (test-ps-js nested-if-expressions1
872 (return (if (if x y z) a b))
873 "return (x ? y : z) ? a : b;")
874
875 (test-ps-js nested-if-expressions2
876 (return (if x y (if z a b)))
877 "return x ? y : (z ? a : b);")
878
879 (test-ps-js let1
880 (let (x)
881 (+ x x))
882 "var x1 = null;
883 x1 + x1;")
884
885 (test-ps-js let2
886 (let ((x 1))
887 (+ x x))
888 "var x1 = 1;
889 x1 + x1;")
890
891 (test-ps-js let3
892 (let ((x 1)
893 (y 2))
894 (+ x x))
895 "var x1 = 1;
896 var y2 = 2;
897 x1 + x1;")
898
899 (test-ps-js let4
900 (let ((x 1)
901 (y (1+ x)))
902 (+ x y))
903 "var x1 = 1;
904 var y2 = x + 1;
905 x1 + y2;")
906
907 (test-ps-js let5
908 (let ((x 1))
909 (+ x 1)
910 (let ((x (+ x 5)))
911 (+ x 1))
912 (+ x 1))
913 "var x1 = 1;
914 x1 + 1;
915 var x2 = x1 + 5;
916 x2 + 1;
917 x1 + 1;")
918
919 (test-ps-js let6
920 (let ((x 2))
921 (let ((x 1)
922 (y (1+ x)))
923 (+ x y)))
924 "var x1 = 2;
925 var x2 = 1;
926 var y3 = x1 + 1;
927 x2 + y3;")
928
929 (test-ps-js let-exp1
930 (lambda ()
931 (return (let (x) (+ x x))))
932 "function () {
933 var x1;
934 return (x1 = null, x1 + x1);
935 };")
936
937 (test-ps-js let*1
938 (let* ((x 1)) (+ x x))
939 "var x1 = 1;
940 x1 + x1;")
941
942 (test-ps-js let*2
943 (let* ((x 1)
944 (y (+ x 2)))
945 (+ x y))
946 "var x1 = 1;
947 var y2 = x1 + 2;
948 x1 + y2;")
949
950 (test-ps-js let*3
951 (let ((x 3))
952 (let* ((x 1)
953 (y (+ x 2)))
954 (+ x y)))
955 "var x1 = 3;
956 var x2 = 1;
957 var y3 = x2 + 2;
958 x2 + y3;")
959
960 (test-ps-js let*4
961 (let ((x 3))
962 (let* ((y (+ x 2))
963 (x 1))
964 (+ x y)))
965 "var x1 = 3;
966 var y2 = x1 + 2;
967 var x3 = 1;
968 x3 + y2;")
969
970 (test-ps-js symbol-macrolet-var
971 (symbol-macrolet ((x y))
972 (var x))
973 "var y;")
974
975 (test-ps-js setf-conditional1
976 (setf x (unless (null a) (1+ a)))
977 "x = a != null ? a + 1 : null;")
978
979 (test-ps-js setf-let1
980 (setf x (let ((a 1)) a))
981 "x = (a1 = 1, a1);")
982
983 (test-ps-js setf-let2
984 (setf x (let ((a (foo)))
985 (unless (null a)
986 (1+ a))))
987 "x = (a1 = foo(), a1 != null ? a1 + 1 : null);")
988
989 (test-ps-js symbol-macro-env1
990 (symbol-macrolet ((bar 1))
991 (macrolet ((bar (x y) `(+ ,x ,y)))
992 (bar bar bar)))
993 "1 + 1;")
994
995 (test-ps-js symbol-macrolet-fun1
996 (symbol-macrolet ((baz +))
997 (baz 1 2))
998 "baz(1, 2);")
999
1000 (test-ps-js lisp2-namespaces1
1001 (let ((list nil))
1002 (setf list (list 1 2 3)))
1003 "var list1 = null;
1004 list1 = [1, 2, 3];")