Fixed the CHAIN macro to correctly chain plain slot values.
[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))))
16151f19 24 "var x = 10;
e2932347 25function sideEffect() {
16151f19 26 x = 4;
e2932347
LC
27 return 3;
28};
16151f19 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))
5a69278c 43 "('' + x).toString();")
79630c82
VS
44
45(test-ps-js method-call-op-form-args
46 ((@ (+ "" x) to-string) 1 2 :baz 3)
5a69278c 47 "('' + x).toString(1, 2, 'baz', 3);")
79630c82
VS
48
49(test-ps-js method-call-number
50 ((@ 10 to-string))
5a69278c 51 "( 10 ).toString();")
79630c82
VS
52
53(test-ps-js method-call-string
54 ((@ "hi" to-string))
5a69278c 55 "'hi'.toString();")
e2932347 56
e2932347 57(test-ps-js method-call-lit-object
fc772f72 58 ((@ (create to-string (lambda () (return "it works"))) to-string))
5a69278c 59 "( { toString : function () { return 'it works'; } } ).toString();")
79630c82
VS
60
61(test-ps-js method-call-conditional
62 ((if a x y) 1)
5a69278c 63 "(a ? x : y)(1);")
e2932347
LC
64
65(test-ps-js method-call-variable
79630c82 66 ((@ x to-string))
5a69278c 67 "x.toString();")
e2932347
LC
68
69(test-ps-js method-call-array
79630c82 70 ((@ (list 10 20) to-string))
5a69278c 71 "[ 10, 20 ].toString();")
79630c82 72
e2932347 73(test-ps-js method-call-fn-call
79630c82 74 ((@ (foo) to-string))
5a69278c 75 "foo().toString();")
79630c82 76
e2932347 77(test-ps-js method-call-lambda-fn
79630c82 78 ((@ (lambda () (alert 10)) to-string))
5a69278c 79 "( function () { alert(10); } ).toString();")
79630c82 80
e2932347 81(test-ps-js method-call-lambda-call
79630c82 82 ((@ ((lambda (x) (return x)) 10) to-string))
5a69278c 83 "(function (x) { return x; })(10).toString();")
e2932347
LC
84
85(test no-whitespace-before-dot
5a69278c 86 (let* ((str (ps* '((@ ((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
fc772f72 93 (let ((foo (create a 1)))
f2bb932e 94 (alert (slot-value foo 'a)))
16151f19
VS
95 "var foo = { a : 1 };
96 alert(foo.a);")
e2932347
LC
97
98(test-ps-js buggy-slot-value
fc772f72 99 (let ((foo (create a 1))
5ffb1eba 100 (slot-name "a"))
e2932347 101 (alert (slot-value foo slot-name)))
16151f19
VS
102 " var foo = { a : 1 };
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))
5a69278c 109 "foo[getSlotName()];")
e2932347
LC
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")))
5a69278c 122 "switch (blorg[i]) {
e2932347
LC
123 case 1: alert('one');
124 case 2: alert('two');
125 default: alert('default clause');
5a69278c 126 };")
e2932347
LC
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');
5a69278c 141 };")
e2932347
LC
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');
5a69278c 158 };")
e2932347
LC
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');
5a69278c 169 };")
e2932347
LC
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 184 for generated = (ps-doc* `(let ((x ,(format nil "hello~ahi" lisp-char)))))
16151f19 185 for wanted = (format nil "var x = '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)
5a69278c 194 "(zoo ? foo : bar).x;")
eb0befe2
VS
195
196(test-ps-js slot-value-conditional2
197 (slot-value (if (not zoo) foo bar) 'x)
5a69278c 198 "(!zoo ? foo : bar).x;")
eb0befe2 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
5a69278c 208 "null;")
46f794a4
RD
209
210(test-ps-js list-with-single-nil
fb469285 211 (array nil)
5a69278c 212 "[null];")
46f794a4 213
fb469285 214(test-ps-js quoted-nil-is-array
62b4a867 215 'nil
5a69278c 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))
16151f19
VS
221 "var _js2 = 1;
222var _js3 = 2;
223var _js1 = 3;
224setBaz(_js2, _js3, _js1);")
5af33a27 225
5a69278c
VS
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
5af33a27
VS
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};
16151f19
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';
5a69278c 255};")
a2434734 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 263 x + y;
5a69278c 264};")
5ac90695 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;
5a69278c 274};")
7be02bed 275
a2434734
VS
276(test-ps-js return-nothing
277 (return)
5a69278c 278 "return null;")
dbb7017b
VS
279
280(test-ps-js set-timeout
281 (do-set-timeout (10) (alert "foo"))
5a69278c
VS
282 "setTimeout(function () { alert('foo'); }, 10);")
283
07afea38
VS
284(test-ps-js operator-precedence
285 (* 3 (+ 4 5) 6)
5a69278c 286 "3 * (4 + 5) * 6;")
07afea38 287
2d2c6dc2
TC
288(test-ps-js operators-1
289 (in prop obj)
5a69278c 290 "prop in obj;")
2d2c6dc2 291
07afea38
VS
292(test-ps-js incf1
293 (incf foo bar)
5a69278c 294 "foo += bar;")
07afea38
VS
295
296(test-ps-js decf1
297 (decf foo bar)
5a69278c 298 "foo -= bar;")
07afea38 299
49c50da4
VS
300(test-ps-js incf2
301 (incf x 5)
5a69278c 302 "x += 5;")
49c50da4
VS
303
304(test-ps-js decf2
305 (decf y 10)
5a69278c 306 "y -= 10;")
49c50da4 307
07afea38
VS
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")
5a69278c 314 "{ 1 : 'foo' };")
07afea38
VS
315
316(test-ps-js obj-literal-strings
317 (create "foo" 2)
5a69278c 318 "{ 'foo' : 2 };")
a805b30d
VS
319
320(test-ps-js slot-value-string
321 (slot-value foo "bar")
5a69278c 322 "foo['bar'];")
b44afd8f 323
79630c82
VS
324(test-ps-js slot-value-string1
325 (slot-value "bar" 'length)
5a69278c 326 "'bar'.length;")
79630c82 327
b44afd8f 328(test-ps-js slot-value-progn
13b8268e 329 (slot-value (progn (some-fun "abc") "123") "length")
5a69278c 330 "(someFun('abc'), '123')['length'];")
b44afd8f
VS
331
332(test-ps-js method-call-block
79630c82 333 ((@ (progn (some-fun "abc") "123") to-string))
5a69278c 334 "(someFun('abc'), '123').toString();")
b44afd8f
VS
335
336(test-ps-js create-blank
337 (create)
5a69278c 338 "{ };")
b44afd8f
VS
339
340(test-ps-js blank-object-literal
5ac90695 341 {}
5a69278c 342 "{ };")
44934751 343
06ed0d3a
VS
344(test-ps-js array-literal1
345 []
5a69278c 346 "[];")
06ed0d3a
VS
347
348(test-ps-js array-literal2
349 ([])
5a69278c 350 "[];")
06ed0d3a
VS
351
352(test-ps-js array-literal3
353 ([] 1 2 3)
5a69278c 354 "[1, 2, 3];")
06ed0d3a
VS
355
356(test-ps-js array-literal4
357 ([] 1 (2 3))
5a69278c 358 "[1, [2, 3]];")
06ed0d3a
VS
359
360(test-ps-js array-literal5
361 ([] (1 2) ("a" "b"))
5a69278c 362 "[[1, 2], ['a', 'b']];")
06ed0d3a 363
44934751 364(test-ps-js defun-rest1
5ffb1eba 365 (defun foo (&rest bar) (alert (aref bar 1)))
44934751
VS
366 "function foo() {
367 var bar = [];
c7716e67
VS
368 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
369 bar[i1] = arguments[i1 + 0];
44934751
VS
370 };
371 alert(bar[1]);
5a69278c 372};")
44934751
VS
373
374(test-ps-js defun-rest2
375 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
2e593e4c 376 "function foo(baz) {
44934751 377 var bar = [];
c7716e67
VS
378 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
379 bar[i1] = arguments[i1 + 1];
44934751
VS
380 };
381 return baz + bar[1];
5a69278c 382};")
2e593e4c
VS
383
384(test-ps-js defun-keyword1
385 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
1cd3540f 386"function zoo(foo, bar) {
c7716e67 387 var baz;
5ffb1eba
VS
388 var _js2 = arguments.length;
389 for (var n1 = 2; n1 < _js2; n1 += 2) {
c7716e67
VS
390 switch (arguments[n1]) {
391 case 'baz':
392 {
393 baz = arguments[n1 + 1];
394 };
395 };
0e198f66 396 };
c7716e67
VS
397 if (baz === undefined) {
398 baz = null;
399 };
400 return foo + bar + baz;
5a69278c 401};")
b0f64e9b 402
f2bb932e
VS
403(test-ps-js defun-keyword2
404 (defun zoo (&key baz) (return (* baz baz)))
c7716e67
VS
405 "function zoo() {
406 var baz;
5ffb1eba
VS
407 var _js2 = arguments.length;
408 for (var n1 = 0; n1 < _js2; n1 += 2) {
c7716e67
VS
409 switch (arguments[n1]) {
410 case 'baz':
411 {
412 baz = arguments[n1 + 1];
413 };
414 };
c7716e67
VS
415 };
416 if (baz === undefined) {
417 baz = null;
f2bb932e 418 };
c7716e67 419 return baz * baz;
5a69278c 420};")
f2bb932e
VS
421
422(test-ps-js defun-keyword3
423 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
c7716e67
VS
424 "function zoo() {
425 var baz;
426 var bar;
5ffb1eba
VS
427 var _js2 = arguments.length;
428 for (var n1 = 0; n1 < _js2; n1 += 2) {
c7716e67
VS
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 };
c7716e67
VS
440 };
441 if (baz === undefined) {
442 baz = null;
f2bb932e 443 };
c7716e67
VS
444 if (bar === undefined) {
445 bar = 4;
f2bb932e 446 };
c7716e67 447 return baz * bar;
5a69278c 448};")
f2bb932e 449
62ddca23
VS
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;
5ffb1eba
VS
455 var _js2 = arguments.length;
456 for (var n1 = 0; n1 < _js2; n1 += 2) {
62ddca23
VS
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;
5a69278c 468};")
62ddca23 469
f2bb932e
VS
470(test-ps-js keyword-funcall1
471 (func :baz 1)
5a69278c 472 "func('baz', 1);")
f2bb932e
VS
473
474(test-ps-js keyword-funcall2
475 (func :baz 1 :bar foo)
5a69278c 476 "func('baz', 1, 'bar', foo);")
f2bb932e
VS
477
478(test-ps-js keyword-funcall3
479 (fun a b :baz c)
5a69278c 480 "fun(a, b, 'baz', c);")
f2bb932e 481
b0f64e9b
VS
482(test-ps-js cond1
483 (cond ((= x 1) 1))
484 "if (x == 1) {
485 1;
5a69278c 486};")
b0f64e9b
VS
487
488(test-ps-js cond2
fdfa77fc
VS
489 (cond ((= x 1) 2)
490 ((= y (* x 4)) (foo "blah") (* x y)))
b0f64e9b
VS
491 "if (x == 1) {
492 2;
493} else if (y == x * 4) {
13b8268e 494 foo('blah');
b0f64e9b 495 x * y;
5a69278c 496};")
5705b542
VS
497
498(test-ps-js if-exp-without-else-returns-null
499 (return (if x 1))
5a69278c 500 "return x ? 1 : null;")
5705b542
VS
501
502(test-ps-js progn-expression-single-statement
503 (return (progn (* x y)))
5a69278c 504 "return x * y;")
0949f072
VS
505
506(test-ps-js cond-expression1
13b8268e 507 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
0949f072 508 "function foo() {
13b8268e 509 return 1 < 2 ? (bar('foo'), 4 * 5) : null;
5a69278c 510};")
0949f072
VS
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);
5a69278c 516};")
0949f072
VS
517
518(test-ps-js cond-expression-final-t-clause
a14fb2cb
VS
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"))))
0949f072 524 "function foo() {
a14fb2cb
VS
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'));
5a69278c 529};")
0949f072
VS
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';
5a69278c 535};")
e5253c5b
VS
536
537(test-ps-js funcall-if-expression
5ffb1eba 538 ((@ document write)
e5253c5b
VS
539 (if (= *linkornot* 1)
540 (ps-html ((:a :href "#"
e69d0a12 541 :onclick (ps-inline (transport)))
e5253c5b
VS
542 img))
543 img))
d434e1d5 544 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport()') + '\">' + img + '</A>' : img);")
49c50da4
VS
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)
5a69278c 548 "-1;")
efe35e12
VS
549
550(test macro-environment1
58c4ef4f 551 (is (string= (normalize-js-code (let* ((macroname (gensym)))
efe35e12
VS
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"))))
921f2e02 564
8cfc6fe9
VS
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
921f2e02
VS
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))
6e8ff198
VS
577 "'(FOO 1 2)';")
578
579(test-ps-js keyword-consistent
580 :x
5a69278c 581 "'x';")
43a1d5c3
VS
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;")
1af04ae5
VS
592
593(test-ps-js define-symbol-macro
594 (progn (define-symbol-macro tst-sym-macro 2)
595 tst-sym-macro)
596 "2;")
e0032a96
VS
597
598(test-ps-js expression-progn
599 (defun f () (return (progn (foo) (if x 1 2))))
600 "function f() {
601 return (foo(), x ? 1 : 2);
5a69278c 602};")
e0032a96
VS
603
604(test-ps-js let-decl-in-expression
16151f19
VS
605 (defun f (x)
606 (return (if x
607 1
608 (let* ((foo x))
609 foo))))
e0032a96 610 "function f(x) {
16151f19
VS
611 var foo;
612 return x ? 1 : (foo = x, foo);
5a69278c 613};")
58c4ef4f
VS
614
615(test-ps-js special-var1
16151f19
VS
616 (progn (defvar *foo*)
617 (let* ((*foo* 2))
618 (* *foo* 2)))
58c4ef4f 619 "var FOO;
16151f19 620var FOO_TMPSTACK1;
58c4ef4f 621try {
16151f19 622 FOO_TMPSTACK1 = FOO;
58c4ef4f
VS
623 FOO = 2;
624 FOO * 2;
625} finally {
16151f19 626 FOO = FOO_TMPSTACK1;
58c4ef4f
VS
627};")
628
629(test-ps-js special-var2
16151f19
VS
630 (progn (defvar *foo*)
631 (let* ((*baz* 3)
632 (*foo* 2))
633 (* *foo* 2 *baz*)))
58c4ef4f 634 "var FOO;
16151f19
VS
635var BAZ = 3;
636var FOO_TMPSTACK1;
58c4ef4f 637try {
16151f19 638 FOO_TMPSTACK1 = FOO;
58c4ef4f 639 FOO = 2;
16151f19 640 FOO * 2 * BAZ;
58c4ef4f 641} finally {
16151f19 642 FOO = FOO_TMPSTACK1;
5ffb1eba 643};")
5ac90695
VS
644
645(test-ps-js literal1
646 (setf x undefined)
647 "x = undefined;")
648
649(test-ps-js literal2
650 (aref this x)
5a69278c 651 "this[x];")
d89456ee
VS
652
653(test-ps-js setf-dec1
654 (setf x (- 1 x 2))
655 "x = 1 - x - 2;")
656
657(test-ps-js setf-dec2
658 (setf x (- x 1 2))
659 "x = x - 1 - 2;")
93e99720
VS
660
661(test-ps-js special-char-equals
662 blah=
5a69278c 663 "blahequals;")
7be02bed
VS
664
665(test-ps-js setf-operator-priority
666 (return (or (slot-value cache id)
5ffb1eba 667 (setf (slot-value cache id) ((@ document get-element-by-id) id))))
5a69278c 668 "return cache[id] || (cache[id] = document.getElementById(id));")
7be02bed
VS
669
670(test-ps-js aref-operator-priority
671 (aref (if (and x (> (length x) 0))
672 (aref x 0)
673 y)
674 z)
5a69278c 675 "(x && x.length > 0 ? x[0] : y)[z];")
7be02bed
VS
676
677(test-ps-js aref-operator-priority1
678 (aref (or (slot-value x 'y)
679 (slot-value a 'b))
680 z)
5a69278c 681 "(x.y || a.b)[z];")
7be02bed
VS
682
683(test-ps-js aref-operator-priority2
684 (aref (if a b c) 0)
5a69278c 685 "(a ? b : c)[0];")
7be02bed
VS
686
687(test-ps-js negative-operator-priority
688 (- (if x y z))
5a69278c 689 "-(x ? y : z);")
7be02bed
VS
690
691(test-ps-js op-p1
692 (new (or a b))
5a69278c 693 "new (a || b);")
7be02bed
VS
694
695(test-ps-js op-p2
696 (delete (if a (or b c) d))
5a69278c 697 "delete (a ? b || c : d);")
7be02bed
VS
698
699(test-ps-js op-p3
700 (not (if (or x (not y)) z))
5a69278c 701 "!(x || !y ? z : null);")
7be02bed
VS
702
703(test-ps-js op-p4
704 (- (- (* 1 2) 3))
5a69278c 705 "-(1 * 2 - 3);")
7be02bed
VS
706
707(test-ps-js op-p5
708 (instanceof (or a b) (if x y z))
5a69278c 709 "((a || b) instanceof (x ? y : z));")
7be02bed 710
7be02bed
VS
711(test-ps-js op-p7
712 (or x (if (= x 0) "zero" "empty"))
5a69278c 713 "x || (x == 0 ? 'zero' : 'empty');")
7be02bed
VS
714
715(test-ps-js named-op-expression
716 (throw (if a b c))
5a69278c 717 "throw a ? b : c;")
7be02bed
VS
718
719(test-ps-js named-op-expression1
720 (typeof (or x y))
5a69278c 721 "typeof (x || y);")
9b31227f
VS
722
723(test-ps-js aref-array-expression
724 (aref (or a b c) 0)
5a69278c 725 "(a || b || c)[0];")
9b31227f
VS
726
727(test-ps-js slot-value-operator
728 (slot-value (or a b c) 'd)
5a69278c 729 "(a || b || c).d;")
9b31227f
VS
730
731(test-ps-js slot-value-parens
732 (slot-value (slot-value foo 'bar) 'baz)
5a69278c 733 "foo.bar.baz;")
9496b3a4
VS
734
735(test-ps-js funcall-funcall
736 ((foo))
5a69278c 737 "foo()();")
9496b3a4
VS
738
739(test-ps-js expression-funcall
740 ((or (@ window eval) eval) foo nil)
5a69278c 741 "(window.eval || eval)(foo, null);")
9496b3a4
VS
742
743(test-ps-js expression-funcall1
744 (((or (@ window eval) eval) foo nil))
5a69278c 745 "(window.eval || eval)(foo, null)();")
9496b3a4
VS
746
747(test-ps-js expression-funcall2
748 (((or (@ window eval) eval)) foo nil)
5a69278c 749 "(window.eval || eval)()(foo, null);")
9496b3a4 750
79630c82 751(test-ps-js slot-value-object-literal
fc772f72 752 (slot-value (create a 1) 'a)
5a69278c 753 "({ a : 1 }).a;")
79630c82
VS
754
755(test-ps-js slot-value-lambda
756 (slot-value (lambda ()) 'prototype)
5a69278c 757 "(function () { }).prototype;")
dde6e656
VS
758
759(test-ps-js who-html1
760 (who-ps-html (:span :class "ticker-symbol"
761 :ticker-symbol symbol
762 (:a :href "http://foo.com"
763 symbol)
764 (:span :class "ticker-symbol-popup")))
5a69278c 765 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>';")
ef3be63e
VS
766
767(test-ps-js flet1
058f137f
VS
768 ((lambda () (flet ((foo (x)
769 (return (1+ x))))
770 (return (foo 1)))))
ef3be63e 771 "(function () {
058f137f 772 var foo1 = function (x) {
ef3be63e
VS
773 return x + 1;
774 };
058f137f 775 return foo1(1);
5a69278c 776})();")
ef3be63e 777
058f137f
VS
778(test-ps-js flet2
779 (flet ((foo (x) (return (1+ x)))
780 (bar (y) (return (+ 2 y))))
781 (bar (foo 1)))
782"var foo1 = function (x) {
783 return x + 1;
784};
785var bar2 = function (y) {
786 return 2 + y;
787};
788bar2(foo1(1));")
789
790(test-ps-js flet3
791 (flet ((foo (x) (return (1+ x)))
792 (bar (y) (return (+ 2 (foo y)))))
793 (bar (foo 1)))
794 "var foo1 = function (x) {
795 return x + 1;
796};
797var bar2 = function (y) {
798 return 2 + foo(y);
799};
800bar2(foo1(1));")
801
ef3be63e
VS
802(test-ps-js labels1
803 ((lambda () (labels ((foo (x)
804 (return (if (=== 0 x)
805 0
806 (+ x (foo (1- x)))))))
807 (return (foo 3)))))
808 "(function () {
058f137f
VS
809 var foo1 = function (x) {
810 return 0 === x ? 0 : x + foo1(x - 1);
ef3be63e 811 };
058f137f 812 return foo1(3);
5a69278c 813})();")
083b7f89 814
058f137f
VS
815(test-ps-js labels2
816 (labels ((foo (x) (return (1+ (bar x))))
817 (bar (y) (return (+ 2 (foo y)))))
818 (bar (foo 1)))
819 "var foo1 = function (x) {
820 return bar2(x) + 1;
821};
822var bar2 = function (y) {
823 return 2 + foo1(y);
824};
825bar2(foo1(1));")
826
827(test-ps-js labels3
828 (labels ((foo (x) (return (1+ x)))
829 (bar (y) (return (+ 2 (foo y)))))
830 (bar (foo 1)))
831 "var foo1 = function (x) {
832 return x + 1;
833};
834var bar2 = function (y) {
835 return 2 + foo1(y);
836};
837bar2(foo1(1));")
838
083b7f89
VS
839(test-ps-js for-loop-var-init-exp
840 ((lambda (x)
841 (return (do* ((y (if x 0 1) (1+ y))
842 (z 0 (1+ z)))
843 ((= y 3) z))))
844 true)
845 "(function (x) {
846 return (function () {
847 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
848 };
849 return z;
850 })();
5a69278c 851})(true);")
5288d666
VS
852
853(test-ps-js math-pi
854 pi
5a69278c 855 "Math.PI;")
fb469285
VS
856
857(test-ps-js literal-array
858 '(1 2 3)
5a69278c 859 "[1, 2, 3];")
fb469285
VS
860
861(test-ps-js literal-array-1
862 '(1 foo 3)
5a69278c 863 "[1, 'foo', 3];")
ceb1f277
VS
864
865(test ps-lisp-expands-in-lexical-environment
866 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
867
868(test ps*-lisp-expands-in-null-lexical-environment
1cd3540f 869 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
ceb1f277
VS
870
871(test ps*-lisp-expands-in-dynamic-environment
872 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
873
874(test ps-lisp-dynamic-environment
875 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))
4525e3cd
VS
876
877(test-ps-js ps-js-target-version-keyword-test1
878 (defun foo (x y &key bar baz))
879 "function foo(x, y) {
16151f19
VS
880 var x1 = Array.prototype.indexOf.call(arguments, 'bar', 2);
881 var bar = -1 == x1 ? null : arguments[x1 + 1];
882 var x2 = Array.prototype.indexOf.call(arguments, 'baz', 2);
883 var baz = -1 == x2 ? null : arguments[x2 + 1];
5a69278c 884};"
4525e3cd 885 :js-target-version 1.6)
20ba6d7a
VS
886
887(test-ps-js nested-if-expressions1
888 (return (if (if x y z) a b))
5a69278c 889 "return (x ? y : z) ? a : b;")
20ba6d7a
VS
890
891(test-ps-js nested-if-expressions2
892 (return (if x y (if z a b)))
5a69278c 893 "return x ? y : (z ? a : b);")
5ffb1eba
VS
894
895(test-ps-js let1
896 (let (x)
897 (+ x x))
16151f19
VS
898 "var x = null;
899x + x;")
5ffb1eba
VS
900
901(test-ps-js let2
902 (let ((x 1))
903 (+ x x))
16151f19
VS
904 "var x = 1;
905x + x;")
906
907(test-ps-js let-x-x
908 (let ((x (1+ x)))
909 (+ x x))
910 "var x1 = x + 1;
5ffb1eba
VS
911x1 + x1;")
912
913(test-ps-js let3
914 (let ((x 1)
915 (y 2))
916 (+ x x))
16151f19
VS
917 "var x = 1;
918var y = 2;
919x + x;")
5ffb1eba
VS
920
921(test-ps-js let4
922 (let ((x 1)
923 (y (1+ x)))
924 (+ x y))
925 "var x1 = 1;
16151f19
VS
926var y = x + 1;
927x1 + y;")
5ffb1eba
VS
928
929(test-ps-js let5
930 (let ((x 1))
931 (+ x 1)
932 (let ((x (+ x 5)))
933 (+ x 1))
934 (+ x 1))
16151f19
VS
935 "var x = 1;
936x + 1;
937var x1 = x + 5;
5ffb1eba 938x1 + 1;
16151f19 939x + 1;")
5ffb1eba
VS
940
941(test-ps-js let6
942 (let ((x 2))
943 (let ((x 1)
944 (y (1+ x)))
945 (+ x y)))
16151f19
VS
946 "var x = 2;
947var x1 = 1;
948var y = x + 1;
949x1 + y;")
5ffb1eba
VS
950
951(test-ps-js let-exp1
952 (lambda ()
953 (return (let (x) (+ x x))))
954 "function () {
16151f19
VS
955 var x;
956 return (x = null, x + x);
5a69278c 957};")
5ffb1eba
VS
958
959(test-ps-js let*1
960 (let* ((x 1)) (+ x x))
16151f19
VS
961"var x = 1;
962x + x;")
5ffb1eba
VS
963
964(test-ps-js let*2
965 (let* ((x 1)
966 (y (+ x 2)))
967 (+ x y))
16151f19
VS
968 "var x = 1;
969var y = x + 2;
970x + y;")
5ffb1eba
VS
971
972(test-ps-js let*3
973 (let ((x 3))
974 (let* ((x 1)
975 (y (+ x 2)))
976 (+ x y)))
16151f19
VS
977 "var x = 3;
978var x1 = 1;
979var y = x1 + 2;
980x1 + y;")
5ffb1eba
VS
981
982(test-ps-js let*4
983 (let ((x 3))
984 (let* ((y (+ x 2))
985 (x 1))
986 (+ x y)))
16151f19
VS
987 "var x = 3;
988var y = x + 2;
989var x1 = 1;
990x1 + y;")
058f137f
VS
991
992(test-ps-js symbol-macrolet-var
993 (symbol-macrolet ((x y))
994 (var x))
3bfb836a
VS
995 "var y;")
996
997(test-ps-js setf-conditional1
998 (setf x (unless (null a) (1+ a)))
999 "x = a != null ? a + 1 : null;")
1000
1001(test-ps-js setf-let1
1002 (setf x (let ((a 1)) a))
16151f19 1003 "x = (a = 1, a);")
3bfb836a
VS
1004
1005(test-ps-js setf-let2
1006 (setf x (let ((a (foo)))
1007 (unless (null a)
1008 (1+ a))))
16151f19 1009 "x = (a = foo(), a != null ? a + 1 : null);")
5a69278c
VS
1010
1011(test-ps-js symbol-macro-env1
1012 (symbol-macrolet ((bar 1))
1013 (macrolet ((bar (x y) `(+ ,x ,y)))
1014 (bar bar bar)))
1015 "1 + 1;")
1016
1017(test-ps-js symbol-macrolet-fun1
1018 (symbol-macrolet ((baz +))
1019 (baz 1 2))
1020 "baz(1, 2);")
1021
1022(test-ps-js lisp2-namespaces1
1023 (let ((list nil))
1024 (setf list (list 1 2 3)))
16151f19
VS
1025 "var list = null;
1026list = [1, 2, 3];")
1027
1028(test-ps-js let-shadows-symbol-macrolet
1029 (symbol-macrolet ((x y))
1030 (let ((x 1))
1031 (+ x x))
1032 (+ x x))
1033 "var x1 = 1;
1034x1 + x1;
1035y + y;")
1036
1037(test-ps-js let-rename-optimization1
1038 (let ((x 1))
1039 (+ x x))
1040 "var x = 1;
1041x + x;")
1042
1043(test-ps-js let-rename-optimization2
1044 (lambda (x)
1045 (let ((x (+ 1 x)))
1046 (return x)))
1047 "function (x) {
1048 var x1 = 1 + x;
1049 return x1;
1050};")
6dce8f77
VS
1051
1052(test-ps-js symbol-macro-array
1053 (symbol-macrolet ((x 1))
1054 (list x))
1055 "[1];")
1056
1057(test-ps-js symbol-macro-obj
1058 (symbol-macrolet ((x y))
1059 (create x 1))
1060 "{ y : 1 };")
1061
1062(test-ps-js symbol-macro-conditional1
1063 (symbol-macrolet ((x y))
1064 (if x x x))
1065 "if (y) {
1066 y;
1067} else {
1068 y;
1069};")
1070
1071(test-ps-js symbol-macro-conditional2
1072 (symbol-macrolet ((x y))
1073 (return (if x x x)))
1074 "return y ? y : y;")
c8d008ad
VS
1075
1076(test-ps-js flet-apply
1077 (flet ((foo () 'bar))
1078 (apply (function foo) nil))
1079 "var foo1 = function () {
1080 'bar';
1081};
1082foo1.apply(this, null);")
1083
1084(test-ps-js let-apply
1085 (let ((foo (lambda () (return 1))))
1086 (let ((foo (lambda () (return 2))))
1087 (apply foo nil)))
1088 "var foo = function () {
1089 return 1;
1090};
1091var foo1 = function () {
1092 return 2;
1093};
1094foo1.apply(this, null);")
1095
1096(test-ps-js flet-let
1097 (flet ((x (x) (return (1+ x))))
1098 (let ((x 2))
1099 (x x)))
1100 "var x1 = function (x) {
1101 return x + 1;
1102};
1103var x = 2;
1104x1(x);")
1105
1106(test-ps-js let-flet
1107 (let ((x 2))
1108 (flet ((x (x) (return (1+ x))))
1109 (x x)))
1110 "var x = 2;
1111var x1 = function (x) {
1112 return x + 1;
1113};
1114x1(x);")
1115
ead240bb
VS
1116(test-ps-js macrolet-let-inteference
1117 (macrolet ((a (n) `(+ ,n 5)))
1118 (let ((a (a 1)))
1119 (let ((b (a (- a 4))))
1120 (+ a b))))
1121 "var a = 1 + 5;
1122var b = (a - 4) + 5;
1123a + b;")
1124
1125(test-ps-js let-subtract-add
1126 (let ((x 1))
1127 (let ((x 2))
1128 (- x x)
1129 (- x)
1130 (decf x)
1131 (incf x)))
1132 "var x = 1;
1133var x1 = 2;
1134x1 - x1;
1135-x1;
1136--x1;
1137++x1;")
1138
837bcc37
VS
1139(test-ps-js create-reserved-word
1140 (create :default 1)
1141 "{ 'default' : 1 };")
1142
1143(test-ps-js slot-value-reserved-word
1144 (slot-value foo :default)
1145 "foo['default'];")
0f5e99ff
RD
1146
1147(test-ps-js eval-when-ps-side
1148 (eval-when (:execute)
1149 5)
1150 "5;")
1151
1152(defvar *lisp-output* nil)
1153
1154(test eval-when-lisp-side ()
1155 (setf *lisp-output* 'original-value)
1156 (let ((js-output (normalize-js-code
1157 (ps-doc* `(eval-when (:compile-toplevel)
1158 (setf *lisp-output* 'it-works))))))
1159 (is (eql 'it-works *lisp-output*))
1160 (is (string= "" js-output))))
1161
1162(defpsmacro my-in-package (package-name)
1163 `(eval-when (:compile-toplevel)
1164 (setf *lisp-output* ,package-name)))
1165
1166(test eval-when-macro-expansion ()
1167 (setf *lisp-output* 'original-value)
1168 (let ((js-output (normalize-js-code
1169 (ps-doc* `(progn
1170 (my-in-package :cl-user)
1171 3)))))
1172 (declare (ignore js-output))
1173 (is (eql :cl-user *lisp-output*))))
1174
1175(test eval-when-macrolet-expansion ()
1176 (setf *lisp-output* 'original-value)
1177 (let ((js-output (normalize-js-code
1178 (ps-doc* `(macrolet ((my-in-package2 (package-name)
1179 `(eval-when (:compile-toplevel)
1180 (setf *lisp-output* ,package-name))))
1181 (my-in-package2 :cl-user)
1182 3)))))
1183 (declare (ignore js-output))
1184 (is (eql :cl-user *lisp-output*))))
3b4f81fb
VS
1185
1186(test-ps-js slot-value-keyword
1187 (slot-value foo :bar)
1188 "foo['bar'];")
a14fb2cb
VS
1189
1190(test-ps-js nary-comparison1
1191 (lambda () (return (< 1 2 3)))
1192 "function () {
1193 var _cmp1;
1194 return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
1195};")
2471a2cf
VS
1196
1197(test-ps-js chain-slot-value1
1198 (chain ($ "foo") (bar x z) frob (baz 5))
1199 "$('foo').bar(x, z).frob.baz(5);")
1200
1201(test-ps-js chain-slot-value2
1202 (chain ($ "foo") bar baz)
1203 "$('foo').bar.baz;")
1204
1205(test-ps-js chain-slot-value3
1206 (chain ($ "foo") bar (x y) baz)
1207 "$('foo').bar.x(y).baz;")