Introduced the "funcall" macro (which really doesn't do anything - only CL needs...
[clinton/parenscript.git] / t / ps-tests.lisp
1 (in-package :ps-test)
2
3 ;;; Hand-written unit tests
4
5 (eval-when (:compile-toplevel :load-toplevel :execute)
6 (def-suite ps-tests))
7 (in-suite ps-tests)
8
9 (test-ps-js plus-is-not-commutative
10 (setf x (+ "before" x "after"))
11 "x = 'before' + x + 'after';")
12
13 (test-ps-js plus-works-if-first
14 (setf x (+ x "middle" "after"))
15 "x += 'middle' + 'after';")
16
17 (test-ps-js setf-side-effects
18 (progn
19 (let* ((x 10))
20 (defun side-effect()
21 (setf x 4)
22 (return 3))
23 (setf x (+ 2 (side-effect) x 5))))
24 "var x = 10;
25 function sideEffect() {
26 x = 4;
27 return 3;
28 };
29 x = 2 + sideEffect() + x + 5;")
30 ;; Parenscript used to optimize incorrectly:
31 ;; var x = 10;
32 ;; function sideEffect() {
33 ;; x = 4;
34 ;; return 3;
35 ;; };
36 ;; x += 2 + sideEffect() + 5;
37 ;;
38 ;; Which is 20, not 14
39
40
41 (test-ps-js method-call-op-form
42 ((@ (+ "" x) to-string))
43 "('' + x).toString()")
44
45 (test-ps-js method-call-op-form-args
46 ((@ (+ "" x) to-string) 1 2 :baz 3)
47 "('' + x).toString(1, 2, { baz : 3 })")
48
49 (test-ps-js method-call-number
50 ((@ 10 to-string))
51 "( 10 ).toString()")
52
53 (test-ps-js method-call-string
54 ((@ "hi" to-string))
55 "'hi'.toString()")
56
57 (test-ps-js method-call-lit-object
58 ((@ (create :to-string (lambda () (return "it works"))) to-string))
59 "( { toString : function () { return 'it works'; } } ).toString()")
60
61 (test-ps-js method-call-conditional
62 ((if a x y) 1)
63 "(a ? x : y)(1)")
64
65 (test-ps-js method-call-variable
66 ((@ x to-string))
67 "x.toString()")
68
69 (test-ps-js method-call-array
70 ((@ (list 10 20) to-string))
71 "[ 10, 20 ].toString()")
72
73 (test-ps-js method-call-fn-call
74 ((@ (foo) to-string))
75 "foo().toString()")
76
77 (test-ps-js method-call-lambda-fn
78 ((@ (lambda () (alert 10)) to-string))
79 "( function () { alert(10); } ).toString()")
80
81 (test-ps-js method-call-lambda-call
82 ((@ ((lambda (x) (return x)) 10) to-string))
83 "(function (x) { return x; })(10).toString()")
84
85 (test no-whitespace-before-dot
86 (let* ((str (ps1* '((@ ((lambda (x) (return x)) 10) to-string))))
87 (dot-pos (position #\. str :test #'char=))
88 (char-before (elt str (1- dot-pos)))
89 (a-parenthesis #\)))
90 (is (char= char-before a-parenthesis))))
91
92 (test-ps-js simple-slot-value
93 (let* ((foo (create :a 1)))
94 (alert (slot-value foo 'a)))
95 "var foo = { a : 1 };
96 alert(foo.a);")
97
98 (test-ps-js buggy-slot-value
99 (let* ((foo (create :a 1))
100 (slot-name "a"))
101 (alert (slot-value foo slot-name)))
102 " var foo = { a : 1 };
103 var slotName = 'a';
104 alert(foo[slotName]);
105 "); Last line was alert(foo.slotName) before bug-fix.
106
107 (test-ps-js buggy-slot-value-two
108 (slot-value foo (get-slot-name))
109 "foo[getSlotName()]")
110
111 (test-ps-js old-case-is-now-switch
112 ;; Switch was "case" before, but that was very non-lispish.
113 ;; For example, this code makes three messages and not one
114 ;; which may have been expected. This is because a switch
115 ;; statment must have a break statement for it to return
116 ;; after the alert. Otherwise it continues on the next
117 ;; clause.
118 (switch (aref blorg i)
119 (1 (alert "one"))
120 (2 (alert "two"))
121 (default (alert "default clause")))
122 "switch (blorg[i]) {
123 case 1: alert('one');
124 case 2: alert('two');
125 default: alert('default clause');
126 }")
127
128 (test-ps-js lisp-like-case
129 (case (aref blorg i)
130 (1 (alert "one"))
131 (2 (alert "two"))
132 (default (alert "default clause")))
133 "switch (blorg[i]) {
134 case 1:
135 alert('one');
136 break;
137 case 2:
138 alert('two');
139 break;
140 default: alert('default clause');
141 }")
142
143
144 (test-ps-js even-lispier-case
145 (case (aref blorg i)
146 ((1 2) (alert "Below three"))
147 (3 (alert "Three"))
148 (t (alert "Something else")))
149 "switch (blorg[i]) {
150 case 1:
151 case 2:
152 alert('Below three');
153 break;
154 case 3:
155 alert('Three');
156 break;
157 default: alert('Something else');
158 }")
159
160 (test-ps-js otherwise-case
161 (case (aref blorg i)
162 (1 (alert "one"))
163 (otherwise (alert "default clause")))
164 "switch (blorg[i]) {
165 case 1:
166 alert('one');
167 break;
168 default: alert('default clause');
169 }")
170
171 (test escape-sequences-in-string
172 (let ((escapes `((#\\ . #\\)
173 (#\b . #\Backspace)
174 (#\f . ,(code-char 12))
175 ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
176 (#\n . #\Newline)
177 (#\r . #\Return)
178 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
179 (#\t . #\Tab)
180 ("u001F" . ,(code-char #x001f));; character below 32
181 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
182 ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
183 (loop for (js-escape . lisp-char) in escapes
184 for generated = (ps1* `(let* ((x ,(format nil "hello~ahi" lisp-char)))))
185 for wanted = (format nil "var x = 'hello\\~ahi';" js-escape)
186 do (is (string= (normalize-js-code generated) wanted)))))
187
188 (test-ps-js 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")
195
196 (test-ps-js slot-value-setf
197 (setf (slot-value x 'y) (+ (+ a 3) 4))
198 "x.y = (a + 3) + 4;")
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
208 (test script-star-eval1
209 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
210
211 (test script-star-eval2
212 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
213
214 (test-ps-js slot-value-null1
215 (slot-value foo nil)
216 "foo")
217
218 (test-ps-js slot-value-null2
219 (slot-value foo 'nil)
220 "foo")
221
222 (test-ps-js unquoted-nil
223 nil
224 "null")
225
226 (test-ps-js list-with-single-nil
227 (array 'nil)
228 "[null]")
229
230 (test-ps-js quoted-nil
231 'nil
232 "null")
233
234 (test defsetf1
235 (ps (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval)))
236 (is (string= "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);"
237 (normalize-js-code (let* ((ps:*ps-gensym-counter* 0))
238 (ps (setf (baz 1 2) 3)))))))
239
240 (test defsetf-short
241 (ps (defsetf baz set-baz "blah"))
242 (is (string= "setBaz(1, 2, 3, 'foo');" (normalize-js-code (ps (setf (baz 1 2 3) "foo"))))))
243
244 (test defun-setf1
245 (is (and (string= (normalize-js-code (ps:ps (defun (setf some-thing) (new-val i1 i2)
246 (setf (aref *some-thing* i1 i2) new-val))))
247 "function __setf_someThing(newVal, i1, i2) { SOMETHING[i1][i2] = newVal; };")
248 (string= (normalize-js-code (ps:ps-doc (setf (some-thing 1 2) "foo")))
249 "var _js2 = 1; var _js3 = 2; var _js1 = 'foo'; __setf_someThing(_js1, _js2, _js3);"))))
250
251 (test-ps-js defun-optional1
252 (defun test-opt (&optional x) (return (if x "yes" "no")))
253 "function testOpt(x) {
254 if (x === undefined) {
255 x = null;
256 };
257 return x ? 'yes' : 'no';
258 }")
259
260 (test-ps-js defun-optional2
261 (defun foo (x &optional y) (+ x y))
262 "function foo(x, y) {
263 if (y === undefined) {
264 y = null;
265 };
266 x + y;
267 }")
268
269 (test-ps-js defun-optional3
270 (defun blah (&optional (x 0))
271 (return x))
272 "function blah(x) {
273 if (x === undefined) {
274 x = 0;
275 };
276 return x;
277 }")
278
279 (test-ps-js return-nothing
280 (return)
281 "return null")
282
283 (test-ps-js set-timeout
284 (do-set-timeout (10) (alert "foo"))
285 "setTimeout(function () { alert('foo'); }, 10)")
286 (test-ps-js operator-precedence
287 (* 3 (+ 4 5) 6)
288 "3 * (4 + 5) * 6")
289
290 (test-ps-js operators-1
291 (in prop obj)
292 "prop in obj")
293
294 (test-ps-js incf1
295 (incf foo bar)
296 "foo += bar")
297
298 (test-ps-js decf1
299 (decf foo bar)
300 "foo -= bar")
301
302 (test-ps-js incf2
303 (incf x 5)
304 "x += 5")
305
306 (test-ps-js decf2
307 (decf y 10)
308 "y -= 10")
309
310 (test-ps-js setf-conditional
311 (setf foo (if x 1 2))
312 "foo = x ? 1 : 2;")
313
314 (test-ps-js obj-literal-numbers
315 (create 1 "foo")
316 "{ 1 : 'foo' }")
317
318 (test-ps-js obj-literal-strings
319 (create "foo" 2)
320 "{ 'foo' : 2 }")
321
322 (test-ps-js slot-value-string
323 (slot-value foo "bar")
324 "foo['bar']")
325
326 (test-ps-js slot-value-string1
327 (slot-value "bar" 'length)
328 "'bar'.length")
329
330 (test-ps-js slot-value-progn
331 (slot-value (progn (some-fun "abc") "123") "length")
332 "(someFun('abc'), '123')['length']")
333
334 (test-ps-js method-call-block
335 ((@ (progn (some-fun "abc") "123") to-string))
336 "(someFun('abc'), '123').toString()")
337
338 (test-ps-js create-blank
339 (create)
340 "{ }")
341
342 (test-ps-js blank-object-literal
343 {}
344 "{ }")
345
346 (test-ps-js array-literal1
347 []
348 "[]")
349
350 (test-ps-js array-literal2
351 ([])
352 "[]")
353
354 (test-ps-js array-literal3
355 ([] 1 2 3)
356 "[1, 2, 3]")
357
358 (test-ps-js array-literal4
359 ([] 1 (2 3))
360 "[1, [2, 3]]")
361
362 (test-ps-js array-literal5
363 ([] (1 2) ("a" "b"))
364 "[[1, 2], ['a', 'b']]")
365
366 (test-ps-js defun-rest1
367 (defun foo (&rest bar) (alert bar[1]))
368 "function foo() {
369 var bar = [];
370 for (var i2 = 0; i2 < arguments.length - 0; i2 += 1) {
371 bar[i2] = arguments[i2 + 0];
372 };
373 alert(bar[1]);
374 }")
375
376 (test-ps-js defun-rest2
377 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
378 "function foo(baz) {
379 var bar = [];
380 for (var i2 = 0; i2 < arguments.length - 1; i2 += 1) {
381 bar[i2] = arguments[i2 + 1];
382 };
383 return baz + bar[1];
384 }")
385
386 (test-ps-js defun-keyword1
387 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
388 "function zoo(foo, bar, _js1) {
389 if (_js1 === undefined) {
390 _js1 = { };
391 };
392 return foo + bar + _js1.baz;
393 }")
394
395 (test-ps-js defun-keyword2
396 (defun zoo (&key baz) (return (* baz baz)))
397 "function zoo(_js1) {
398 if (_js1 === undefined) {
399 _js1 = { };
400 };
401 return _js1.baz * _js1.baz;
402 }")
403
404 (test-ps-js defun-keyword3
405 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
406 "function zoo(_js1) {
407 if (_js1 === undefined) {
408 _js1 = { };
409 };
410 if (_js1.bar === undefined) {
411 _js1.bar = 4;
412 };
413 return _js1.baz * _js1.bar;
414 }")
415
416 (test-ps-js keyword-funcall1
417 (func :baz 1)
418 "func({ baz : 1 })")
419
420 (test-ps-js keyword-funcall2
421 (func :baz 1 :bar foo)
422 "func({ baz : 1, bar : foo })")
423
424 (test-ps-js keyword-funcall3
425 (fun a b :baz c)
426 "fun(a, b, { baz : c })")
427
428 (test-ps-js cond1
429 (cond ((= x 1) 1))
430 "if (x == 1) {
431 1;
432 }")
433
434 (test-ps-js cond2
435 (cond ((= x 1) 2) ((= y (* x 4)) (foo "blah") (* x y)))
436 "if (x == 1) {
437 2;
438 } else if (y == x * 4) {
439 foo('blah');
440 x * y;
441 }")
442
443 (test-ps-js if-exp-without-else-returns-null
444 (return (if x 1))
445 "return x ? 1 : null")
446
447 (test-ps-js progn-expression-single-statement
448 (return (progn (* x y)))
449 "return x * y")
450
451 (test-ps-js cond-expression1
452 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
453 "function foo() {
454 return 1 < 2 ? (bar('foo'), 4 * 5) : null;
455 }")
456
457 (test-ps-js cond-expression2
458 (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar"))))
459 "function foo() {
460 return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null);
461 }")
462
463 (test-ps-js cond-expression-final-t-clause
464 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo"))))
465 "function foo() {
466 return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo'));
467 }")
468
469 (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
470 (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar"))))
471 "function foo() {
472 return 2 < 1 ? 5 : 'foo';
473 }")
474
475 (test-ps-js funcall-if-expression
476 (document.write
477 (if (= *linkornot* 1)
478 (ps-html ((:a :href "#"
479 :onclick (ps-inline (transport)))
480 img))
481 img))
482 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport()') + '\">' + img + '</A>' : img)")
483
484 (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test!
485 (- 1)
486 "-1")
487
488 (test macro-environment1
489 (is (string= (normalize-js-code (let* ((macroname (gensym)))
490 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
491 `(defun test1 ()
492 (macrolet ((,macroname (x) `(aref data ,x)))
493 (when (,macroname x)
494 (setf (,macroname x) 123)))))))
495 (normalize-js-code
496 "function test1() {
497 if (data[x]) {
498 data[x] = 123;
499 };
500 };
501 "))))
502
503 (test macro-environment2
504 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
505 (defpsmacro macro-environment2-macro (x)
506 `(+ ,outer-lexical-variable ,x))
507 (ps* '(macro-environment2-macro 2))))
508 (normalize-js-code "1 + 2;"))))
509
510 (test-ps-js ampersand-whole-1
511 (macrolet ((foo (&whole foo bar baz)
512 (declare (ignore bar baz))
513 (format nil "~a" foo)))
514 (foo 1 2))
515 "'(FOO 1 2)';")
516
517 (test-ps-js keyword-consistent
518 :x
519 "'x'")
520
521 (test-ps-js simple-symbol-macrolet
522 (symbol-macrolet ((x 1)) x)
523 "1;")
524
525 (test-ps-js compound-symbol-macrolet
526 (symbol-macrolet ((x 123)
527 (y (* 2 x)))
528 y)
529 "2 * 123;")
530
531 (test-ps-js define-symbol-macro
532 (progn (define-symbol-macro tst-sym-macro 2)
533 tst-sym-macro)
534 "2;")
535
536 (test-ps-js expression-progn
537 (defun f () (return (progn (foo) (if x 1 2))))
538 "function f() {
539 return (foo(), x ? 1 : 2);
540 }")
541
542 (test-ps-js let-decl-in-expression
543 (defun f (x) (return (if x 1 (let* ((foo x)) foo))))
544 "function f(x) {
545 var foo;
546 return x ? 1 : (foo = x, foo);
547 }")
548
549 (test-ps-js special-var1
550 (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2)))
551 "var FOO;
552 var tempstackvar1;
553 try {
554 tempstackvar1 = FOO;
555 FOO = 2;
556 FOO * 2;
557 } finally {
558 FOO = tempstackvar1;
559 delete tempstackvar1;
560 };")
561
562 (test-ps-js special-var2
563 (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*)))
564 "var FOO;
565 var BAZ = 3;
566 var tempstackvar1;
567 try {
568 tempstackvar1 = FOO;
569 FOO = 2;
570 FOO * 2 * BAZ;
571 } finally {
572 FOO = tempstackvar1;
573 delete tempstackvar1;
574 };
575 ")
576
577 (test-ps-js literal1
578 (setf x undefined)
579 "x = undefined;")
580
581 (test-ps-js literal2
582 (aref this x)
583 "this[x]")
584
585 (test-ps-js setf-dec1
586 (setf x (- 1 x 2))
587 "x = 1 - x - 2;")
588
589 (test-ps-js setf-dec2
590 (setf x (- x 1 2))
591 "x = x - 1 - 2;")
592
593 (test-ps-js special-char-equals
594 blah=
595 "blahequals")
596
597 (test-ps-js setf-operator-priority
598 (return (or (slot-value cache id)
599 (setf (slot-value cache id) (document.get-element-by-id id))))
600 "return cache[id] || (cache[id] = document.getElementById(id))")
601
602 (test-ps-js aref-operator-priority
603 (aref (if (and x (> (length x) 0))
604 (aref x 0)
605 y)
606 z)
607 "(x && x.length > 0 ? x[0] : y)[z]")
608
609 (test-ps-js aref-operator-priority1
610 (aref (or (slot-value x 'y)
611 (slot-value a 'b))
612 z)
613 "(x.y || a.b)[z]")
614
615 (test-ps-js aref-operator-priority2
616 (aref (if a b c) 0)
617 "(a ? b : c)[0]")
618
619 (test-ps-js negative-operator-priority
620 (- (if x y z))
621 "-(x ? y : z)")
622
623 (test-ps-js op-p1
624 (new (or a b))
625 "new (a || b)")
626
627 (test-ps-js op-p2
628 (delete (if a (or b c) d))
629 "delete (a ? b || c : d)")
630
631 (test-ps-js op-p3
632 (not (if (or x (not y)) z))
633 "!(x || !y ? z : null)")
634
635 (test-ps-js op-p4
636 (- (- (* 1 2) 3))
637 "-(1 * 2 - 3)")
638
639 (test-ps-js op-p5
640 (instanceof (or a b) (if x y z))
641 "((a || b) instanceof (x ? y : z))")
642
643 (test-ps-js op-p6
644 (doeach (x (or a b)))
645 "for (var x in (a || b)) { };")
646
647 (test-ps-js op-p7
648 (or x (if (= x 0) "zero" "empty"))
649 "x || (x == 0 ? 'zero' : 'empty')")
650
651 (test-ps-js named-op-expression
652 (throw (if a b c))
653 "throw a ? b : c")
654
655 (test-ps-js named-op-expression1
656 (typeof (or x y))
657 "typeof (x || y)")
658
659 (test-ps-js aref-array-expression
660 (aref (or a b c) 0)
661 "(a || b || c)[0]")
662
663 (test-ps-js slot-value-operator
664 (slot-value (or a b c) 'd)
665 "(a || b || c).d")
666
667 (test-ps-js slot-value-parens
668 (slot-value (slot-value foo 'bar) 'baz)
669 "foo.bar.baz")
670
671 (test-ps-js funcall-funcall
672 ((foo))
673 "foo()()")
674
675 (test-ps-js expression-funcall
676 ((or (@ window eval) eval) foo nil)
677 "(window.eval || eval)(foo, null)")
678
679 (test-ps-js expression-funcall1
680 (((or (@ window eval) eval) foo nil))
681 "(window.eval || eval)(foo, null)()")
682
683 (test-ps-js expression-funcall2
684 (((or (@ window eval) eval)) foo nil)
685 "(window.eval || eval)()(foo, null)")
686
687 (test-ps-js slot-value-object-literal
688 (slot-value (create :a 1) 'a)
689 "({ a : 1 }).a")
690
691 (test-ps-js slot-value-lambda
692 (slot-value (lambda ()) 'prototype)
693 "(function () { }).prototype")
694
695 (test-ps-js who-html1
696 (who-ps-html (:span :class "ticker-symbol"
697 :ticker-symbol symbol
698 (:a :href "http://foo.com"
699 symbol)
700 (:span :class "ticker-symbol-popup")))
701 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"/></SPAN>'")
702
703 (test-ps-js flet1
704 ((lambda () (flet ((foo (x) (return (1+ x)))) (return (foo 1)))))
705 "(function () {
706 var foo = function (x) {
707 return x + 1;
708 };
709 return foo(1);
710 })()")
711
712 (test-ps-js labels1
713 ((lambda () (labels ((foo (x)
714 (return (if (=== 0 x)
715 0
716 (+ x (foo (1- x)))))))
717 (return (foo 3)))))
718 "(function () {
719 var foo = function foo(x) {
720 return 0 === x ? 0 : x + foo(x - 1);
721 };
722 return foo(3);
723 })()")
724
725 (test-ps-js for-loop-var-init-exp
726 ((lambda (x)
727 (return (do* ((y (if x 0 1) (1+ y))
728 (z 0 (1+ z)))
729 ((= y 3) z))))
730 true)
731 "(function (x) {
732 return (function () {
733 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
734 };
735 return z;
736 })();
737 })(true)")