Updated tests to reflect changes in latest patches from Daniel Gackle.
[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
VS
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;
e2932347
LC
25function sideEffect() {
26 x = 4;
27 return 3;
28};
29x = 2 + sideEffect() + x + 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
58c4ef4f 93 (let* ((foo (create :a 1)))
f2bb932e 94 (alert (slot-value foo 'a)))
07afea38
VS
95 "var foo = { a : 1 };
96 alert(foo.a);")
e2932347
LC
97
98(test-ps-js buggy-slot-value
58c4ef4f 99 (let* ((foo (create :a 1))
f2bb932e 100 (slot-name "a"))
e2932347 101 (alert (slot-value foo slot-name)))
07afea38 102 " var foo = { a : 1 };
e2932347
LC
103 var slotName = 'a';
104 alert(foo[slotName]);
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
cb8f8e58 184 for generated = (ps1* `(let* ((x ,(format nil "hello~ahi" lisp-char)))))
b508414b
TC
185 for wanted = (format nil "var x = 'hello\\~ahi';" js-escape)
186 do (is (string= (normalize-js-code generated) wanted)))))
9da682ca 187
e0f0d152
VS
188(test-ps-js complicated-symbol-name1
189 grid-rows[foo].bar
190 "gridRows[foo].bar")
191
192(test-ps-js complicated-symbol-name2
193 *grid-rows*[foo].bar
194 "GRIDROWS[foo].bar")
cf460f93
VS
195
196(test-ps-js slot-value-setf
197 (setf (slot-value x 'y) (+ (+ a 3) 4))
72332f2a 198 "x.y = (a + 3) + 4;")
eb0befe2
VS
199
200(test-ps-js slot-value-conditional1
201 (slot-value (if zoo foo bar) 'x)
202 "(zoo ? foo : bar).x")
203
204(test-ps-js slot-value-conditional2
205 (slot-value (if (not zoo) foo bar) 'x)
206 "(!zoo ? foo : bar).x")
207
245d0fbd 208(test script-star-eval1
07afea38 209 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
245d0fbd
VS
210
211(test script-star-eval2
07afea38 212 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
62b4a867 213
46f794a4
RD
214(test-ps-js unquoted-nil
215 nil
216 "null")
217
218(test-ps-js list-with-single-nil
fb469285 219 (array nil)
46f794a4
RD
220 "[null]")
221
fb469285 222(test-ps-js quoted-nil-is-array
62b4a867 223 'nil
1cd3540f 224 "[]")
72332f2a 225
5af33a27
VS
226(test-ps-js defsetf1
227 (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
228 (setf (baz 1 2) 3))
229 "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);")
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"))
c7716e67 240"function __setf_someThing(newVal, i1, i2) {
5af33a27
VS
241 SOMETHING[i1][i2] = newVal;
242};
c7716e67
VS
243var _js2 = 1;
244var _js3 = 2;
245var _js1 = 'foo';
246__setf_someThing(_js1, _js2, _js3);")
dbb7017b 247
d989d711
VS
248(test-ps-js defun-optional1
249 (defun test-opt (&optional x) (return (if x "yes" "no")))
250 "function testOpt(x) {
0e198f66
TC
251 if (x === undefined) {
252 x = null;
253 };
254 return x ? 'yes' : 'no';
a2434734
VS
255}")
256
5ac90695
VS
257(test-ps-js defun-optional2
258 (defun foo (x &optional y) (+ x y))
259 "function foo(x, y) {
0e198f66
TC
260 if (y === undefined) {
261 y = null;
262 };
5ac90695
VS
263 x + y;
264}")
265
7be02bed
VS
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
a2434734
VS
276(test-ps-js return-nothing
277 (return)
278 "return null")
dbb7017b
VS
279
280(test-ps-js set-timeout
281 (do-set-timeout (10) (alert "foo"))
b44afd8f 282 "setTimeout(function () { alert('foo'); }, 10)")
07afea38
VS
283(test-ps-js operator-precedence
284 (* 3 (+ 4 5) 6)
285 "3 * (4 + 5) * 6")
286
2d2c6dc2
TC
287(test-ps-js operators-1
288 (in prop obj)
289 "prop in obj")
290
07afea38
VS
291(test-ps-js incf1
292 (incf foo bar)
293 "foo += bar")
294
295(test-ps-js decf1
296 (decf foo bar)
297 "foo -= bar")
298
49c50da4
VS
299(test-ps-js incf2
300 (incf x 5)
301 "x += 5")
302
303(test-ps-js decf2
304 (decf y 10)
305 "y -= 10")
306
07afea38
VS
307(test-ps-js setf-conditional
308 (setf foo (if x 1 2))
309 "foo = x ? 1 : 2;")
310
311(test-ps-js obj-literal-numbers
312 (create 1 "foo")
313 "{ 1 : 'foo' }")
314
315(test-ps-js obj-literal-strings
316 (create "foo" 2)
a805b30d
VS
317 "{ 'foo' : 2 }")
318
319(test-ps-js slot-value-string
320 (slot-value foo "bar")
321 "foo['bar']")
b44afd8f 322
79630c82
VS
323(test-ps-js slot-value-string1
324 (slot-value "bar" 'length)
325 "'bar'.length")
326
b44afd8f 327(test-ps-js slot-value-progn
13b8268e
VS
328 (slot-value (progn (some-fun "abc") "123") "length")
329 "(someFun('abc'), '123')['length']")
b44afd8f
VS
330
331(test-ps-js method-call-block
79630c82 332 ((@ (progn (some-fun "abc") "123") to-string))
13b8268e 333 "(someFun('abc'), '123').toString()")
b44afd8f
VS
334
335(test-ps-js create-blank
336 (create)
337 "{ }")
338
339(test-ps-js blank-object-literal
5ac90695 340 {}
667e3784 341 "{ }")
44934751 342
06ed0d3a
VS
343(test-ps-js array-literal1
344 []
345 "[]")
346
347(test-ps-js array-literal2
348 ([])
349 "[]")
350
351(test-ps-js array-literal3
352 ([] 1 2 3)
353 "[1, 2, 3]")
354
355(test-ps-js array-literal4
356 ([] 1 (2 3))
357 "[1, [2, 3]]")
358
359(test-ps-js array-literal5
360 ([] (1 2) ("a" "b"))
361 "[[1, 2], ['a', 'b']]")
362
44934751
VS
363(test-ps-js defun-rest1
364 (defun foo (&rest bar) (alert bar[1]))
365 "function foo() {
366 var bar = [];
c7716e67
VS
367 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
368 bar[i1] = arguments[i1 + 0];
44934751
VS
369 };
370 alert(bar[1]);
371}")
372
373(test-ps-js defun-rest2
374 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
2e593e4c 375 "function foo(baz) {
44934751 376 var bar = [];
c7716e67
VS
377 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
378 bar[i1] = arguments[i1 + 1];
44934751
VS
379 };
380 return baz + bar[1];
381}")
2e593e4c
VS
382
383(test-ps-js defun-keyword1
384 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
1cd3540f 385"function zoo(foo, bar) {
c7716e67 386 var baz;
1cd3540f
VS
387 var _js3 = arguments.length;
388 for (var n1 = 2; n1 < _js3; n1 += 2) {
c7716e67
VS
389 switch (arguments[n1]) {
390 case 'baz':
391 {
392 baz = arguments[n1 + 1];
393 };
394 };
0e198f66 395 };
c7716e67
VS
396 if (baz === undefined) {
397 baz = null;
398 };
399 return foo + bar + baz;
2e593e4c 400}")
b0f64e9b 401
f2bb932e
VS
402(test-ps-js defun-keyword2
403 (defun zoo (&key baz) (return (* baz baz)))
c7716e67
VS
404 "function zoo() {
405 var baz;
1cd3540f
VS
406 var _js3 = arguments.length;
407 for (var n1 = 0; n1 < _js3; n1 += 2) {
c7716e67
VS
408 switch (arguments[n1]) {
409 case 'baz':
410 {
411 baz = arguments[n1 + 1];
412 };
413 };
c7716e67
VS
414 };
415 if (baz === undefined) {
416 baz = null;
f2bb932e 417 };
c7716e67 418 return baz * baz;
f2bb932e
VS
419}")
420
421(test-ps-js defun-keyword3
422 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
c7716e67
VS
423 "function zoo() {
424 var baz;
425 var bar;
1cd3540f
VS
426 var _js3 = arguments.length;
427 for (var n1 = 0; n1 < _js3; n1 += 2) {
c7716e67
VS
428 switch (arguments[n1]) {
429 case 'baz':
430 {
431 baz = arguments[n1 + 1];
432 };
433 break;
434 case 'bar':
435 {
436 bar = arguments[n1 + 1];
437 };
438 };
c7716e67
VS
439 };
440 if (baz === undefined) {
441 baz = null;
f2bb932e 442 };
c7716e67
VS
443 if (bar === undefined) {
444 bar = 4;
f2bb932e 445 };
c7716e67 446 return baz * bar;
f2bb932e
VS
447}")
448
449(test-ps-js keyword-funcall1
450 (func :baz 1)
c7716e67 451 "func('baz', 1)")
f2bb932e
VS
452
453(test-ps-js keyword-funcall2
454 (func :baz 1 :bar foo)
c7716e67 455 "func('baz', 1, 'bar', foo)")
f2bb932e
VS
456
457(test-ps-js keyword-funcall3
458 (fun a b :baz c)
c7716e67 459 "fun(a, b, 'baz', c)")
f2bb932e 460
b0f64e9b
VS
461(test-ps-js cond1
462 (cond ((= x 1) 1))
463 "if (x == 1) {
464 1;
465}")
466
467(test-ps-js cond2
13b8268e 468 (cond ((= x 1) 2) ((= y (* x 4)) (foo "blah") (* x y)))
b0f64e9b
VS
469 "if (x == 1) {
470 2;
471} else if (y == x * 4) {
13b8268e 472 foo('blah');
b0f64e9b
VS
473 x * y;
474}")
5705b542
VS
475
476(test-ps-js if-exp-without-else-returns-null
477 (return (if x 1))
478 "return x ? 1 : null")
479
480(test-ps-js progn-expression-single-statement
481 (return (progn (* x y)))
482 "return x * y")
0949f072
VS
483
484(test-ps-js cond-expression1
13b8268e 485 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
0949f072 486 "function foo() {
13b8268e 487 return 1 < 2 ? (bar('foo'), 4 * 5) : null;
0949f072
VS
488}")
489
490(test-ps-js cond-expression2
491 (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar"))))
492 "function foo() {
493 return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null);
494}")
495
496(test-ps-js cond-expression-final-t-clause
13b8268e 497 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo"))))
0949f072 498 "function foo() {
13b8268e 499 return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo'));
0949f072
VS
500}")
501
502(test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
503 (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar"))))
504 "function foo() {
505 return 2 < 1 ? 5 : 'foo';
506}")
e5253c5b
VS
507
508(test-ps-js funcall-if-expression
509 (document.write
510 (if (= *linkornot* 1)
511 (ps-html ((:a :href "#"
e69d0a12 512 :onclick (ps-inline (transport)))
e5253c5b
VS
513 img))
514 img))
496ef8be 515 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">' + img + '</A>' : img)")
49c50da4
VS
516
517(test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test!
518 (- 1)
519 "-1")
efe35e12
VS
520
521(test macro-environment1
58c4ef4f 522 (is (string= (normalize-js-code (let* ((macroname (gensym)))
efe35e12
VS
523 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
524 `(defun test1 ()
525 (macrolet ((,macroname (x) `(aref data ,x)))
526 (when (,macroname x)
527 (setf (,macroname x) 123)))))))
528 (normalize-js-code
529"function test1() {
530 if (data[x]) {
531 data[x] = 123;
532 };
533};
534"))))
921f2e02 535
8cfc6fe9
VS
536(test macro-environment2
537 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
538 (defpsmacro macro-environment2-macro (x)
539 `(+ ,outer-lexical-variable ,x))
540 (ps* '(macro-environment2-macro 2))))
541 (normalize-js-code "1 + 2;"))))
542
921f2e02
VS
543(test-ps-js ampersand-whole-1
544 (macrolet ((foo (&whole foo bar baz)
545 (declare (ignore bar baz))
546 (format nil "~a" foo)))
547 (foo 1 2))
6e8ff198
VS
548 "'(FOO 1 2)';")
549
550(test-ps-js keyword-consistent
551 :x
f2bb932e 552 "'x'")
43a1d5c3
VS
553
554(test-ps-js simple-symbol-macrolet
555 (symbol-macrolet ((x 1)) x)
556 "1;")
557
558(test-ps-js compound-symbol-macrolet
559 (symbol-macrolet ((x 123)
560 (y (* 2 x)))
561 y)
562 "2 * 123;")
1af04ae5
VS
563
564(test-ps-js define-symbol-macro
565 (progn (define-symbol-macro tst-sym-macro 2)
566 tst-sym-macro)
567 "2;")
e0032a96
VS
568
569(test-ps-js expression-progn
570 (defun f () (return (progn (foo) (if x 1 2))))
571 "function f() {
572 return (foo(), x ? 1 : 2);
573}")
574
575(test-ps-js let-decl-in-expression
58c4ef4f 576 (defun f (x) (return (if x 1 (let* ((foo x)) foo))))
e0032a96
VS
577 "function f(x) {
578 var foo;
579 return x ? 1 : (foo = x, foo);
580}")
58c4ef4f
VS
581
582(test-ps-js special-var1
583 (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2)))
584 "var FOO;
585var tempstackvar1;
586try {
587 tempstackvar1 = FOO;
588 FOO = 2;
589 FOO * 2;
590} finally {
591 FOO = tempstackvar1;
d777a405 592 delete tempstackvar1;
58c4ef4f
VS
593};")
594
595(test-ps-js special-var2
596 (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*)))
597 "var FOO;
598var BAZ = 3;
599var tempstackvar1;
600try {
601 tempstackvar1 = FOO;
602 FOO = 2;
603 FOO * 2 * BAZ;
604} finally {
605 FOO = tempstackvar1;
d777a405
TC
606 delete tempstackvar1;
607};
608")
5ac90695
VS
609
610(test-ps-js literal1
611 (setf x undefined)
612 "x = undefined;")
613
614(test-ps-js literal2
615 (aref this x)
616 "this[x]")
d89456ee
VS
617
618(test-ps-js setf-dec1
619 (setf x (- 1 x 2))
620 "x = 1 - x - 2;")
621
622(test-ps-js setf-dec2
623 (setf x (- x 1 2))
624 "x = x - 1 - 2;")
93e99720
VS
625
626(test-ps-js special-char-equals
627 blah=
628 "blahequals")
7be02bed
VS
629
630(test-ps-js setf-operator-priority
631 (return (or (slot-value cache id)
632 (setf (slot-value cache id) (document.get-element-by-id id))))
633 "return cache[id] || (cache[id] = document.getElementById(id))")
634
635(test-ps-js aref-operator-priority
636 (aref (if (and x (> (length x) 0))
637 (aref x 0)
638 y)
639 z)
640 "(x && x.length > 0 ? x[0] : y)[z]")
641
642(test-ps-js aref-operator-priority1
643 (aref (or (slot-value x 'y)
644 (slot-value a 'b))
645 z)
646 "(x.y || a.b)[z]")
647
648(test-ps-js aref-operator-priority2
649 (aref (if a b c) 0)
650 "(a ? b : c)[0]")
651
652(test-ps-js negative-operator-priority
653 (- (if x y z))
654 "-(x ? y : z)")
655
656(test-ps-js op-p1
657 (new (or a b))
658 "new (a || b)")
659
660(test-ps-js op-p2
661 (delete (if a (or b c) d))
662 "delete (a ? b || c : d)")
663
664(test-ps-js op-p3
665 (not (if (or x (not y)) z))
666 "!(x || !y ? z : null)")
667
668(test-ps-js op-p4
669 (- (- (* 1 2) 3))
670 "-(1 * 2 - 3)")
671
672(test-ps-js op-p5
673 (instanceof (or a b) (if x y z))
674 "((a || b) instanceof (x ? y : z))")
675
7be02bed
VS
676(test-ps-js op-p7
677 (or x (if (= x 0) "zero" "empty"))
678 "x || (x == 0 ? 'zero' : 'empty')")
679
680(test-ps-js named-op-expression
681 (throw (if a b c))
682 "throw a ? b : c")
683
684(test-ps-js named-op-expression1
685 (typeof (or x y))
686 "typeof (x || y)")
9b31227f
VS
687
688(test-ps-js aref-array-expression
689 (aref (or a b c) 0)
690 "(a || b || c)[0]")
691
692(test-ps-js slot-value-operator
693 (slot-value (or a b c) 'd)
694 "(a || b || c).d")
695
696(test-ps-js slot-value-parens
697 (slot-value (slot-value foo 'bar) 'baz)
698 "foo.bar.baz")
9496b3a4
VS
699
700(test-ps-js funcall-funcall
701 ((foo))
702 "foo()()")
703
704(test-ps-js expression-funcall
705 ((or (@ window eval) eval) foo nil)
706 "(window.eval || eval)(foo, null)")
707
708(test-ps-js expression-funcall1
709 (((or (@ window eval) eval) foo nil))
710 "(window.eval || eval)(foo, null)()")
711
712(test-ps-js expression-funcall2
713 (((or (@ window eval) eval)) foo nil)
714 "(window.eval || eval)()(foo, null)")
715
79630c82
VS
716(test-ps-js slot-value-object-literal
717 (slot-value (create :a 1) 'a)
718 "({ a : 1 }).a")
719
720(test-ps-js slot-value-lambda
721 (slot-value (lambda ()) 'prototype)
722 "(function () { }).prototype")
dde6e656
VS
723
724(test-ps-js who-html1
725 (who-ps-html (:span :class "ticker-symbol"
726 :ticker-symbol symbol
727 (:a :href "http://foo.com"
728 symbol)
729 (:span :class "ticker-symbol-popup")))
0734b390 730 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>'")
ef3be63e
VS
731
732(test-ps-js flet1
733 ((lambda () (flet ((foo (x) (return (1+ x)))) (return (foo 1)))))
734 "(function () {
735 var foo = function (x) {
736 return x + 1;
737 };
738 return foo(1);
739})()")
740
741(test-ps-js labels1
742 ((lambda () (labels ((foo (x)
743 (return (if (=== 0 x)
744 0
745 (+ x (foo (1- x)))))))
746 (return (foo 3)))))
747 "(function () {
748 var foo = function foo(x) {
749 return 0 === x ? 0 : x + foo(x - 1);
750 };
751 return foo(3);
752})()")
083b7f89
VS
753
754(test-ps-js for-loop-var-init-exp
755 ((lambda (x)
756 (return (do* ((y (if x 0 1) (1+ y))
757 (z 0 (1+ z)))
758 ((= y 3) z))))
759 true)
760 "(function (x) {
761 return (function () {
762 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
763 };
764 return z;
765 })();
766})(true)")
5288d666
VS
767
768(test-ps-js math-pi
769 pi
770 "Math.PI")
fb469285
VS
771
772(test-ps-js literal-array
773 '(1 2 3)
774 "[1, 2, 3]")
775
776(test-ps-js literal-array-1
777 '(1 foo 3)
778 "[1, 'foo', 3]")
ceb1f277
VS
779
780(test ps-lisp-expands-in-lexical-environment
781 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
782
783(test ps*-lisp-expands-in-null-lexical-environment
1cd3540f 784 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
ceb1f277
VS
785
786(test ps*-lisp-expands-in-dynamic-environment
787 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
788
789(test ps-lisp-dynamic-environment
790 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))