Implemented LET and LET* by variable renaming, which provides the
[clinton/parenscript.git] / t / ps-tests.lisp
CommitLineData
171bbab3 1(in-package :ps-test)
13b8268e
VS
2
3;;; Hand-written unit tests
e2932347
LC
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
79630c82
VS
10 (setf x (+ "before" x "after"))
11 "x = 'before' + x + 'after';")
e2932347
LC
12
13(test-ps-js plus-works-if-first
79630c82
VS
14 (setf x (+ x "middle" "after"))
15 "x += 'middle' + 'after';")
e2932347
LC
16
17(test-ps-js setf-side-effects
58c4ef4f 18 (progn
5ffb1eba 19 (let ((x 10))
58c4ef4f
VS
20 (defun side-effect()
21 (setf x 4)
22 (return 3))
23 (setf x (+ 2 (side-effect) x 5))))
5ffb1eba 24 "var x1 = 10;
e2932347 25function sideEffect() {
5ffb1eba 26 x1 = 4;
e2932347
LC
27 return 3;
28};
5ffb1eba 29x1 = 2 + sideEffect() + x1 + 5;")
79630c82 30;; Parenscript used to optimize incorrectly:
e2932347
LC
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
79630c82
VS
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)
c7716e67 47 "('' + x).toString(1, 2, 'baz', 3)")
79630c82
VS
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()")
e2932347 56
e2932347 57(test-ps-js method-call-lit-object
79630c82
VS
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)")
e2932347
LC
64
65(test-ps-js method-call-variable
79630c82
VS
66 ((@ x to-string))
67 "x.toString()")
e2932347
LC
68
69(test-ps-js method-call-array
79630c82
VS
70 ((@ (list 10 20) to-string))
71 "[ 10, 20 ].toString()")
72
e2932347 73(test-ps-js method-call-fn-call
79630c82
VS
74 ((@ (foo) to-string))
75 "foo().toString()")
76
e2932347 77(test-ps-js method-call-lambda-fn
79630c82
VS
78 ((@ (lambda () (alert 10)) to-string))
79 "( function () { alert(10); } ).toString()")
80
e2932347 81(test-ps-js method-call-lambda-call
79630c82
VS
82 ((@ ((lambda (x) (return x)) 10) to-string))
83 "(function (x) { return x; })(10).toString()")
e2932347
LC
84
85(test no-whitespace-before-dot
79630c82 86 (let* ((str (ps1* '((@ ((lambda (x) (return x)) 10) to-string))))
e2932347
LC
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
e2932347 92(test-ps-js simple-slot-value
5ffb1eba 93 (let ((foo (create :a 1)))
f2bb932e 94 (alert (slot-value foo 'a)))
5ffb1eba
VS
95 "var foo1 = { a : 1 };
96 alert(foo1.a);")
e2932347
LC
97
98(test-ps-js buggy-slot-value
5ffb1eba
VS
99 (let ((foo (create :a 1))
100 (slot-name "a"))
e2932347 101 (alert (slot-value foo slot-name)))
5ffb1eba
VS
102 " var foo1 = { a : 1 };
103 var slotName2 = 'a';
104 alert(foo1[slotName2]);
07afea38 105 "); Last line was alert(foo.slotName) before bug-fix.
e2932347
LC
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]) {
b44afd8f 150 case 1:
e2932347
LC
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"))
e0f0d152 163 (otherwise (alert "default clause")))
e2932347
LC
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
905f534e 172 (let ((escapes `((#\\ . #\\)
e2932347
LC
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
5ffb1eba
VS
184 for generated = (ps-doc* `(let ((x ,(format nil "hello~ahi" lisp-char)))))
185 for wanted = (format nil "var x1 = 'hello\\~ahi';" js-escape)
b508414b 186 do (is (string= (normalize-js-code generated) wanted)))))
9da682ca 187
cf460f93
VS
188(test-ps-js slot-value-setf
189 (setf (slot-value x 'y) (+ (+ a 3) 4))
72332f2a 190 "x.y = (a + 3) + 4;")
eb0befe2
VS
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
245d0fbd 200(test script-star-eval1
07afea38 201 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
245d0fbd
VS
202
203(test script-star-eval2
07afea38 204 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
62b4a867 205
46f794a4
RD
206(test-ps-js unquoted-nil
207 nil
208 "null")
209
210(test-ps-js list-with-single-nil
fb469285 211 (array nil)
46f794a4
RD
212 "[null]")
213
fb469285 214(test-ps-js quoted-nil-is-array
62b4a867 215 'nil
1cd3540f 216 "[]")
72332f2a 217
5af33a27
VS
218(test-ps-js defsetf1
219 (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
220 (setf (baz 1 2) 3))
5ffb1eba
VS
221 "var _js2_4 = 1;
222var _js3_5 = 2;
223var _js1_6 = 3;
224setBaz(_js2_4, _js3_5, _js1_6);")
5af33a27
VS
225
226(test-ps-js defsetf-short
227 (progn (defsetf baz set-baz "docstring")
228 (setf (baz 1 2 3) "foo"))
229 "setBaz(1, 2, 3, 'foo');")
230
231(test-ps-js defun-setf1
232 (progn (defun (setf some-thing) (new-val i1 i2)
233 (setf (aref *some-thing* i1 i2) new-val))
234 (setf (some-thing 1 2) "foo"))
c7716e67 235"function __setf_someThing(newVal, i1, i2) {
5af33a27
VS
236 SOMETHING[i1][i2] = newVal;
237};
5ffb1eba
VS
238var _js2_4 = 1;
239var _js3_5 = 2;
240var _js1_6 = 'foo';
241__setf_someThing(_js1_6, _js2_4, _js3_5);")
dbb7017b 242
d989d711
VS
243(test-ps-js defun-optional1
244 (defun test-opt (&optional x) (return (if x "yes" "no")))
245 "function testOpt(x) {
0e198f66
TC
246 if (x === undefined) {
247 x = null;
248 };
249 return x ? 'yes' : 'no';
a2434734
VS
250}")
251
5ac90695
VS
252(test-ps-js defun-optional2
253 (defun foo (x &optional y) (+ x y))
254 "function foo(x, y) {
0e198f66
TC
255 if (y === undefined) {
256 y = null;
257 };
5ac90695
VS
258 x + y;
259}")
260
7be02bed
VS
261(test-ps-js defun-optional3
262 (defun blah (&optional (x 0))
263 (return x))
264 "function blah(x) {
265 if (x === undefined) {
266 x = 0;
267 };
268 return x;
269}")
270
a2434734
VS
271(test-ps-js return-nothing
272 (return)
273 "return null")
dbb7017b
VS
274
275(test-ps-js set-timeout
276 (do-set-timeout (10) (alert "foo"))
b44afd8f 277 "setTimeout(function () { alert('foo'); }, 10)")
07afea38
VS
278(test-ps-js operator-precedence
279 (* 3 (+ 4 5) 6)
280 "3 * (4 + 5) * 6")
281
2d2c6dc2
TC
282(test-ps-js operators-1
283 (in prop obj)
284 "prop in obj")
285
07afea38
VS
286(test-ps-js incf1
287 (incf foo bar)
288 "foo += bar")
289
290(test-ps-js decf1
291 (decf foo bar)
292 "foo -= bar")
293
49c50da4
VS
294(test-ps-js incf2
295 (incf x 5)
296 "x += 5")
297
298(test-ps-js decf2
299 (decf y 10)
300 "y -= 10")
301
07afea38
VS
302(test-ps-js setf-conditional
303 (setf foo (if x 1 2))
304 "foo = x ? 1 : 2;")
305
306(test-ps-js obj-literal-numbers
307 (create 1 "foo")
308 "{ 1 : 'foo' }")
309
310(test-ps-js obj-literal-strings
311 (create "foo" 2)
a805b30d
VS
312 "{ 'foo' : 2 }")
313
314(test-ps-js slot-value-string
315 (slot-value foo "bar")
316 "foo['bar']")
b44afd8f 317
79630c82
VS
318(test-ps-js slot-value-string1
319 (slot-value "bar" 'length)
320 "'bar'.length")
321
b44afd8f 322(test-ps-js slot-value-progn
13b8268e
VS
323 (slot-value (progn (some-fun "abc") "123") "length")
324 "(someFun('abc'), '123')['length']")
b44afd8f
VS
325
326(test-ps-js method-call-block
79630c82 327 ((@ (progn (some-fun "abc") "123") to-string))
13b8268e 328 "(someFun('abc'), '123').toString()")
b44afd8f
VS
329
330(test-ps-js create-blank
331 (create)
332 "{ }")
333
334(test-ps-js blank-object-literal
5ac90695 335 {}
667e3784 336 "{ }")
44934751 337
06ed0d3a
VS
338(test-ps-js array-literal1
339 []
340 "[]")
341
342(test-ps-js array-literal2
343 ([])
344 "[]")
345
346(test-ps-js array-literal3
347 ([] 1 2 3)
348 "[1, 2, 3]")
349
350(test-ps-js array-literal4
351 ([] 1 (2 3))
352 "[1, [2, 3]]")
353
354(test-ps-js array-literal5
355 ([] (1 2) ("a" "b"))
356 "[[1, 2], ['a', 'b']]")
357
44934751 358(test-ps-js defun-rest1
5ffb1eba 359 (defun foo (&rest bar) (alert (aref bar 1)))
44934751
VS
360 "function foo() {
361 var bar = [];
c7716e67
VS
362 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
363 bar[i1] = arguments[i1 + 0];
44934751
VS
364 };
365 alert(bar[1]);
366}")
367
368(test-ps-js defun-rest2
369 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
2e593e4c 370 "function foo(baz) {
44934751 371 var bar = [];
c7716e67
VS
372 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
373 bar[i1] = arguments[i1 + 1];
44934751
VS
374 };
375 return baz + bar[1];
376}")
2e593e4c
VS
377
378(test-ps-js defun-keyword1
379 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
1cd3540f 380"function zoo(foo, bar) {
c7716e67 381 var baz;
5ffb1eba
VS
382 var _js2 = arguments.length;
383 for (var n1 = 2; n1 < _js2; n1 += 2) {
c7716e67
VS
384 switch (arguments[n1]) {
385 case 'baz':
386 {
387 baz = arguments[n1 + 1];
388 };
389 };
0e198f66 390 };
c7716e67
VS
391 if (baz === undefined) {
392 baz = null;
393 };
394 return foo + bar + baz;
2e593e4c 395}")
b0f64e9b 396
f2bb932e
VS
397(test-ps-js defun-keyword2
398 (defun zoo (&key baz) (return (* baz baz)))
c7716e67
VS
399 "function zoo() {
400 var baz;
5ffb1eba
VS
401 var _js2 = arguments.length;
402 for (var n1 = 0; n1 < _js2; n1 += 2) {
c7716e67
VS
403 switch (arguments[n1]) {
404 case 'baz':
405 {
406 baz = arguments[n1 + 1];
407 };
408 };
c7716e67
VS
409 };
410 if (baz === undefined) {
411 baz = null;
f2bb932e 412 };
c7716e67 413 return baz * baz;
f2bb932e
VS
414}")
415
416(test-ps-js defun-keyword3
417 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
c7716e67
VS
418 "function zoo() {
419 var baz;
420 var bar;
5ffb1eba
VS
421 var _js2 = arguments.length;
422 for (var n1 = 0; n1 < _js2; n1 += 2) {
c7716e67
VS
423 switch (arguments[n1]) {
424 case 'baz':
425 {
426 baz = arguments[n1 + 1];
427 };
428 break;
429 case 'bar':
430 {
431 bar = arguments[n1 + 1];
432 };
433 };
c7716e67
VS
434 };
435 if (baz === undefined) {
436 baz = null;
f2bb932e 437 };
c7716e67
VS
438 if (bar === undefined) {
439 bar = 4;
f2bb932e 440 };
c7716e67 441 return baz * bar;
f2bb932e
VS
442}")
443
62ddca23
VS
444(test-ps-js defun-keyword4
445 (defun hello-world (&key ((:my-name-key my-name) 1))
446 my-name)
447 "function helloWorld() {
448 var myName;
5ffb1eba
VS
449 var _js2 = arguments.length;
450 for (var n1 = 0; n1 < _js2; n1 += 2) {
62ddca23
VS
451 switch (arguments[n1]) {
452 case 'my-name-key':
453 {
454 myName = arguments[n1 + 1];
455 };
456 };
457 };
458 if (myName === undefined) {
459 myName = 1;
460 };
461 myName;
462}")
463
f2bb932e
VS
464(test-ps-js keyword-funcall1
465 (func :baz 1)
c7716e67 466 "func('baz', 1)")
f2bb932e
VS
467
468(test-ps-js keyword-funcall2
469 (func :baz 1 :bar foo)
c7716e67 470 "func('baz', 1, 'bar', foo)")
f2bb932e
VS
471
472(test-ps-js keyword-funcall3
473 (fun a b :baz c)
c7716e67 474 "fun(a, b, 'baz', c)")
f2bb932e 475
b0f64e9b
VS
476(test-ps-js cond1
477 (cond ((= x 1) 1))
478 "if (x == 1) {
479 1;
480}")
481
482(test-ps-js cond2
fdfa77fc
VS
483 (cond ((= x 1) 2)
484 ((= y (* x 4)) (foo "blah") (* x y)))
b0f64e9b
VS
485 "if (x == 1) {
486 2;
487} else if (y == x * 4) {
13b8268e 488 foo('blah');
b0f64e9b
VS
489 x * y;
490}")
5705b542
VS
491
492(test-ps-js if-exp-without-else-returns-null
493 (return (if x 1))
494 "return x ? 1 : null")
495
496(test-ps-js progn-expression-single-statement
497 (return (progn (* x y)))
498 "return x * y")
0949f072
VS
499
500(test-ps-js cond-expression1
13b8268e 501 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
0949f072 502 "function foo() {
13b8268e 503 return 1 < 2 ? (bar('foo'), 4 * 5) : null;
0949f072
VS
504}")
505
506(test-ps-js cond-expression2
507 (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar"))))
508 "function foo() {
509 return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null);
510}")
511
512(test-ps-js cond-expression-final-t-clause
13b8268e 513 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo"))))
0949f072 514 "function foo() {
13b8268e 515 return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo'));
0949f072
VS
516}")
517
518(test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
519 (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar"))))
520 "function foo() {
521 return 2 < 1 ? 5 : 'foo';
522}")
e5253c5b
VS
523
524(test-ps-js funcall-if-expression
5ffb1eba 525 ((@ document write)
e5253c5b
VS
526 (if (= *linkornot* 1)
527 (ps-html ((:a :href "#"
e69d0a12 528 :onclick (ps-inline (transport)))
e5253c5b
VS
529 img))
530 img))
496ef8be 531 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">' + img + '</A>' : img)")
49c50da4
VS
532
533(test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test!
534 (- 1)
535 "-1")
efe35e12
VS
536
537(test macro-environment1
58c4ef4f 538 (is (string= (normalize-js-code (let* ((macroname (gensym)))
efe35e12
VS
539 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
540 `(defun test1 ()
541 (macrolet ((,macroname (x) `(aref data ,x)))
542 (when (,macroname x)
543 (setf (,macroname x) 123)))))))
544 (normalize-js-code
545"function test1() {
546 if (data[x]) {
547 data[x] = 123;
548 };
549};
550"))))
921f2e02 551
8cfc6fe9
VS
552(test macro-environment2
553 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
554 (defpsmacro macro-environment2-macro (x)
555 `(+ ,outer-lexical-variable ,x))
556 (ps* '(macro-environment2-macro 2))))
557 (normalize-js-code "1 + 2;"))))
558
921f2e02
VS
559(test-ps-js ampersand-whole-1
560 (macrolet ((foo (&whole foo bar baz)
561 (declare (ignore bar baz))
562 (format nil "~a" foo)))
563 (foo 1 2))
6e8ff198
VS
564 "'(FOO 1 2)';")
565
566(test-ps-js keyword-consistent
567 :x
f2bb932e 568 "'x'")
43a1d5c3
VS
569
570(test-ps-js simple-symbol-macrolet
571 (symbol-macrolet ((x 1)) x)
572 "1;")
573
574(test-ps-js compound-symbol-macrolet
575 (symbol-macrolet ((x 123)
576 (y (* 2 x)))
577 y)
578 "2 * 123;")
1af04ae5
VS
579
580(test-ps-js define-symbol-macro
581 (progn (define-symbol-macro tst-sym-macro 2)
582 tst-sym-macro)
583 "2;")
e0032a96
VS
584
585(test-ps-js expression-progn
586 (defun f () (return (progn (foo) (if x 1 2))))
587 "function f() {
588 return (foo(), x ? 1 : 2);
589}")
590
591(test-ps-js let-decl-in-expression
58c4ef4f 592 (defun f (x) (return (if x 1 (let* ((foo x)) foo))))
e0032a96 593 "function f(x) {
5ffb1eba
VS
594 var foo1;
595 return x ? 1 : (foo1 = x, foo1);
e0032a96 596}")
58c4ef4f
VS
597
598(test-ps-js special-var1
599 (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2)))
600 "var FOO;
5ffb1eba 601var FOO1;
58c4ef4f 602try {
5ffb1eba 603 FOO1 = FOO;
58c4ef4f
VS
604 FOO = 2;
605 FOO * 2;
606} finally {
5ffb1eba 607 FOO = FOO1;
58c4ef4f
VS
608};")
609
610(test-ps-js special-var2
611 (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*)))
612 "var FOO;
5ffb1eba
VS
613var BAZ1 = 3;
614var FOO2;
58c4ef4f 615try {
5ffb1eba 616 FOO2 = FOO;
58c4ef4f 617 FOO = 2;
5ffb1eba 618 FOO * 2 * BAZ1;
58c4ef4f 619} finally {
5ffb1eba
VS
620 FOO = FOO2;
621};")
5ac90695
VS
622
623(test-ps-js literal1
624 (setf x undefined)
625 "x = undefined;")
626
627(test-ps-js literal2
628 (aref this x)
629 "this[x]")
d89456ee
VS
630
631(test-ps-js setf-dec1
632 (setf x (- 1 x 2))
633 "x = 1 - x - 2;")
634
635(test-ps-js setf-dec2
636 (setf x (- x 1 2))
637 "x = x - 1 - 2;")
93e99720
VS
638
639(test-ps-js special-char-equals
640 blah=
641 "blahequals")
7be02bed
VS
642
643(test-ps-js setf-operator-priority
644 (return (or (slot-value cache id)
5ffb1eba 645 (setf (slot-value cache id) ((@ document get-element-by-id) id))))
7be02bed
VS
646 "return cache[id] || (cache[id] = document.getElementById(id))")
647
648(test-ps-js aref-operator-priority
649 (aref (if (and x (> (length x) 0))
650 (aref x 0)
651 y)
652 z)
653 "(x && x.length > 0 ? x[0] : y)[z]")
654
655(test-ps-js aref-operator-priority1
656 (aref (or (slot-value x 'y)
657 (slot-value a 'b))
658 z)
659 "(x.y || a.b)[z]")
660
661(test-ps-js aref-operator-priority2
662 (aref (if a b c) 0)
663 "(a ? b : c)[0]")
664
665(test-ps-js negative-operator-priority
666 (- (if x y z))
667 "-(x ? y : z)")
668
669(test-ps-js op-p1
670 (new (or a b))
671 "new (a || b)")
672
673(test-ps-js op-p2
674 (delete (if a (or b c) d))
675 "delete (a ? b || c : d)")
676
677(test-ps-js op-p3
678 (not (if (or x (not y)) z))
679 "!(x || !y ? z : null)")
680
681(test-ps-js op-p4
682 (- (- (* 1 2) 3))
683 "-(1 * 2 - 3)")
684
685(test-ps-js op-p5
686 (instanceof (or a b) (if x y z))
687 "((a || b) instanceof (x ? y : z))")
688
7be02bed
VS
689(test-ps-js op-p7
690 (or x (if (= x 0) "zero" "empty"))
691 "x || (x == 0 ? 'zero' : 'empty')")
692
693(test-ps-js named-op-expression
694 (throw (if a b c))
695 "throw a ? b : c")
696
697(test-ps-js named-op-expression1
698 (typeof (or x y))
699 "typeof (x || y)")
9b31227f
VS
700
701(test-ps-js aref-array-expression
702 (aref (or a b c) 0)
703 "(a || b || c)[0]")
704
705(test-ps-js slot-value-operator
706 (slot-value (or a b c) 'd)
707 "(a || b || c).d")
708
709(test-ps-js slot-value-parens
710 (slot-value (slot-value foo 'bar) 'baz)
711 "foo.bar.baz")
9496b3a4
VS
712
713(test-ps-js funcall-funcall
714 ((foo))
715 "foo()()")
716
717(test-ps-js expression-funcall
718 ((or (@ window eval) eval) foo nil)
719 "(window.eval || eval)(foo, null)")
720
721(test-ps-js expression-funcall1
722 (((or (@ window eval) eval) foo nil))
723 "(window.eval || eval)(foo, null)()")
724
725(test-ps-js expression-funcall2
726 (((or (@ window eval) eval)) foo nil)
727 "(window.eval || eval)()(foo, null)")
728
79630c82
VS
729(test-ps-js slot-value-object-literal
730 (slot-value (create :a 1) 'a)
731 "({ a : 1 }).a")
732
733(test-ps-js slot-value-lambda
734 (slot-value (lambda ()) 'prototype)
735 "(function () { }).prototype")
dde6e656
VS
736
737(test-ps-js who-html1
738 (who-ps-html (:span :class "ticker-symbol"
739 :ticker-symbol symbol
740 (:a :href "http://foo.com"
741 symbol)
742 (:span :class "ticker-symbol-popup")))
0734b390 743 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>'")
ef3be63e
VS
744
745(test-ps-js flet1
746 ((lambda () (flet ((foo (x) (return (1+ x)))) (return (foo 1)))))
747 "(function () {
748 var foo = function (x) {
749 return x + 1;
750 };
751 return foo(1);
752})()")
753
754(test-ps-js labels1
755 ((lambda () (labels ((foo (x)
756 (return (if (=== 0 x)
757 0
758 (+ x (foo (1- x)))))))
759 (return (foo 3)))))
760 "(function () {
761 var foo = function foo(x) {
762 return 0 === x ? 0 : x + foo(x - 1);
763 };
764 return foo(3);
765})()")
083b7f89
VS
766
767(test-ps-js for-loop-var-init-exp
768 ((lambda (x)
769 (return (do* ((y (if x 0 1) (1+ y))
770 (z 0 (1+ z)))
771 ((= y 3) z))))
772 true)
773 "(function (x) {
774 return (function () {
775 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
776 };
777 return z;
778 })();
779})(true)")
5288d666
VS
780
781(test-ps-js math-pi
782 pi
783 "Math.PI")
fb469285
VS
784
785(test-ps-js literal-array
786 '(1 2 3)
787 "[1, 2, 3]")
788
789(test-ps-js literal-array-1
790 '(1 foo 3)
791 "[1, 'foo', 3]")
ceb1f277
VS
792
793(test ps-lisp-expands-in-lexical-environment
794 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
795
796(test ps*-lisp-expands-in-null-lexical-environment
1cd3540f 797 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
ceb1f277
VS
798
799(test ps*-lisp-expands-in-dynamic-environment
800 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
801
802(test ps-lisp-dynamic-environment
803 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))
4525e3cd
VS
804
805(test-ps-js ps-js-target-version-keyword-test1
806 (defun foo (x y &key bar baz))
807 "function foo(x, y) {
5ffb1eba
VS
808 var x1_3 = Array.prototype.indexOf.call(arguments, 'bar', 2);
809 var bar = -1 == x1_3 ? null : arguments[x1_3 + 1];
810 var x2_4 = Array.prototype.indexOf.call(arguments, 'baz', 2);
811 var baz = -1 == x2_4 ? null : arguments[x2_4 + 1];
4525e3cd
VS
812}"
813 :js-target-version 1.6)
20ba6d7a
VS
814
815(test-ps-js nested-if-expressions1
816 (return (if (if x y z) a b))
817 "return (x ? y : z) ? a : b")
818
819(test-ps-js nested-if-expressions2
820 (return (if x y (if z a b)))
821 "return x ? y : (z ? a : b)")
5ffb1eba
VS
822
823(test-ps-js let1
824 (let (x)
825 (+ x x))
826 "var x1 = null;
827x1 + x1;")
828
829(test-ps-js let2
830 (let ((x 1))
831 (+ x x))
832 "var x1 = 1;
833x1 + x1;")
834
835(test-ps-js let3
836 (let ((x 1)
837 (y 2))
838 (+ x x))
839 "var x1 = 1;
840var y2 = 2;
841x1 + x1;")
842
843(test-ps-js let4
844 (let ((x 1)
845 (y (1+ x)))
846 (+ x y))
847 "var x1 = 1;
848var y2 = x + 1;
849x1 + y2;")
850
851(test-ps-js let5
852 (let ((x 1))
853 (+ x 1)
854 (let ((x (+ x 5)))
855 (+ x 1))
856 (+ x 1))
857 "var x1 = 1;
858x1 + 1;
859var x2 = x1 + 5;
860x2 + 1;
861x1 + 1;")
862
863(test-ps-js let6
864 (let ((x 2))
865 (let ((x 1)
866 (y (1+ x)))
867 (+ x y)))
868 "var x1 = 2;
869var x2 = 1;
870var y3 = x1 + 1;
871x2 + y3;")
872
873(test-ps-js let-exp1
874 (lambda ()
875 (return (let (x) (+ x x))))
876 "function () {
877 var x1;
878 return (x1 = null, x1 + x1);
879}")
880
881(test-ps-js let*1
882 (let* ((x 1)) (+ x x))
883"var x1 = 1;
884x1 + x1;")
885
886(test-ps-js let*2
887 (let* ((x 1)
888 (y (+ x 2)))
889 (+ x y))
890 "var x1 = 1;
891var y2 = x1 + 2;
892x1 + y2;")
893
894(test-ps-js let*3
895 (let ((x 3))
896 (let* ((x 1)
897 (y (+ x 2)))
898 (+ x y)))
899 "var x1 = 3;
900var x2 = 1;
901var y3 = x2 + 2;
902x2 + y3;")
903
904(test-ps-js let*4
905 (let ((x 3))
906 (let* ((y (+ x 2))
907 (x 1))
908 (+ x y)))
909 "var x1 = 3;
910var y2 = x1 + 2;
911var x3 = 1;
912x3 + y2;")