3 ;;; Hand-written unit tests
5 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
9 (test-ps-js plus-is-not-commutative
10 (setf x
(+ "before" x
"after"))
11 "x = 'before' + x + 'after';")
13 (test-ps-js plus-works-if-first
14 (setf x
(+ x
"middle" "after"))
15 "x += 'middle' + 'after';")
17 (test-ps-js setf-side-effects
23 (setf x
(+ 2 (side-effect) x
5))))
25 function sideEffect() {
29 x1 = 2 + sideEffect() + x1 + 5;")
30 ;; Parenscript used to optimize incorrectly:
32 ;; function sideEffect() {
36 ;; x += 2 + sideEffect() + 5;
38 ;; Which is 20, not 14
41 (test-ps-js method-call-op-form
42 ((@ (+ "" x
) to-string
))
43 "('' + x).toString()")
45 (test-ps-js method-call-op-form-args
46 ((@ (+ "" x
) to-string
) 1 2 :baz
3)
47 "('' + x).toString(1, 2, 'baz', 3)")
49 (test-ps-js method-call-number
53 (test-ps-js method-call-string
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()")
61 (test-ps-js method-call-conditional
65 (test-ps-js method-call-variable
69 (test-ps-js method-call-array
70 ((@ (list 10 20) to-string
))
71 "[ 10, 20 ].toString()")
73 (test-ps-js method-call-fn-call
77 (test-ps-js method-call-lambda-fn
78 ((@ (lambda () (alert 10)) to-string
))
79 "( function () { alert(10); } ).toString()")
81 (test-ps-js method-call-lambda-call
82 ((@ ((lambda (x) (return x
)) 10) to-string
))
83 "(function (x) { return x; })(10).toString()")
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
)))
90 (is (char= char-before a-parenthesis
))))
92 (test-ps-js simple-slot-value
93 (let ((foo (create :a
1)))
94 (alert (slot-value foo
'a
)))
95 "var foo1 = { a : 1 };
98 (test-ps-js buggy-slot-value
99 (let ((foo (create :a
1))
101 (alert (slot-value foo slot-name
)))
102 " var foo1 = { a : 1 };
104 alert(foo1[slotName2]);
105 "); Last line was alert(foo.slotName) before bug-fix.
107 (test-ps-js buggy-slot-value-two
108 (slot-value foo
(get-slot-name))
109 "foo[getSlotName()]")
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
118 (switch (aref blorg i
)
121 (default (alert "default clause")))
123 case 1: alert('one');
124 case 2: alert('two');
125 default: alert('default clause');
128 (test-ps-js lisp-like-case
132 (default (alert "default clause")))
140 default: alert('default clause');
144 (test-ps-js even-lispier-case
146 ((1 2) (alert "Below three"))
148 (t (alert "Something else")))
152 alert('Below three');
157 default: alert('Something else');
160 (test-ps-js otherwise-case
163 (otherwise (alert "default clause")))
168 default: alert('default clause');
171 (test escape-sequences-in-string
172 (let ((escapes `((#\\ .
#\\)
174 (#\f .
,(code-char 12))
175 ("u000B" .
,(code-char #x000b
));;Vertical tab, too uncommon to bother with
178 (#\' .
#\');;Double quote need not be quoted because parenscript strings are single quoted
180 ("u001F" .
,(code-char #x001f
));; character below 32
181 ("u0080" .
,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
182 ("uABCD" .
,(code-char #xabcd
)))));; Really above ascii.
183 (loop for
(js-escape . lisp-char
) in escapes
184 for generated
= (ps-doc* `(let ((x ,(format nil
"hello~ahi" lisp-char
)))))
185 for wanted
= (format nil
"var x1 = 'hello\\~ahi';" js-escape
)
186 do
(is (string= (normalize-js-code generated
) wanted
)))))
188 (test-ps-js slot-value-setf
189 (setf (slot-value x
'y
) (+ (+ a
3) 4))
190 "x.y = (a + 3) + 4;")
192 (test-ps-js slot-value-conditional1
193 (slot-value (if zoo foo bar
) 'x
)
194 "(zoo ? foo : bar).x")
196 (test-ps-js slot-value-conditional2
197 (slot-value (if (not zoo
) foo bar
) 'x
)
198 "(!zoo ? foo : bar).x")
200 (test script-star-eval1
201 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x
1) '(setf y
2))))))
203 (test script-star-eval2
204 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x
1))))))
206 (test-ps-js unquoted-nil
210 (test-ps-js list-with-single-nil
214 (test-ps-js quoted-nil-is-array
219 (progn (defsetf baz
(x y
) (newval) `(set-baz ,x
,y
,newval
))
224 setBaz(_js2_4, _js3_5, _js1_6);")
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');")
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"))
235 "function __setf_someThing(newVal, i1, i2) {
236 SOMETHING[i1][i2] = newVal;
241 __setf_someThing(_js1_6, _js2_4, _js3_5);")
243 (test-ps-js defun-optional1
244 (defun test-opt (&optional x
) (return (if x
"yes" "no")))
245 "function testOpt(x) {
246 if (x === undefined) {
249 return x ? 'yes' : 'no';
252 (test-ps-js defun-optional2
253 (defun foo (x &optional y
) (+ x y
))
254 "function foo(x, y) {
255 if (y === undefined) {
261 (test-ps-js defun-optional3
262 (defun blah (&optional
(x 0))
265 if (x === undefined) {
271 (test-ps-js return-nothing
275 (test-ps-js set-timeout
276 (do-set-timeout (10) (alert "foo"))
277 "setTimeout(function () { alert('foo'); }, 10)")
278 (test-ps-js operator-precedence
282 (test-ps-js operators-1
302 (test-ps-js setf-conditional
303 (setf foo
(if x
1 2))
306 (test-ps-js obj-literal-numbers
310 (test-ps-js obj-literal-strings
314 (test-ps-js slot-value-string
315 (slot-value foo
"bar")
318 (test-ps-js slot-value-string1
319 (slot-value "bar" 'length
)
322 (test-ps-js slot-value-progn
323 (slot-value (progn (some-fun "abc") "123") "length")
324 "(someFun('abc'), '123')['length']")
326 (test-ps-js method-call-block
327 ((@ (progn (some-fun "abc") "123") to-string
))
328 "(someFun('abc'), '123').toString()")
330 (test-ps-js create-blank
334 (test-ps-js blank-object-literal
338 (test-ps-js array-literal1
342 (test-ps-js array-literal2
346 (test-ps-js array-literal3
350 (test-ps-js array-literal4
354 (test-ps-js array-literal5
356 "[[1, 2], ['a', 'b']]")
358 (test-ps-js defun-rest1
359 (defun foo (&rest bar
) (alert (aref bar
1)))
362 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
363 bar[i1] = arguments[i1 + 0];
368 (test-ps-js defun-rest2
369 (defun foo (baz &rest bar
) (return (+ baz
(aref bar
1))))
372 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
373 bar[i1] = arguments[i1 + 1];
378 (test-ps-js defun-keyword1
379 (defun zoo (foo bar
&key baz
) (return (+ foo bar baz
)))
380 "function zoo(foo, bar) {
382 var _js2 = arguments.length;
383 for (var n1 = 2; n1 < _js2; n1 += 2) {
384 switch (arguments[n1]) {
387 baz = arguments[n1 + 1];
391 if (baz === undefined) {
394 return foo + bar + baz;
397 (test-ps-js defun-keyword2
398 (defun zoo (&key baz
) (return (* baz baz
)))
401 var _js2 = arguments.length;
402 for (var n1 = 0; n1 < _js2; n1 += 2) {
403 switch (arguments[n1]) {
406 baz = arguments[n1 + 1];
410 if (baz === undefined) {
416 (test-ps-js defun-keyword3
417 (defun zoo (&key baz
(bar 4)) (return (* baz bar
)))
421 var _js2 = arguments.length;
422 for (var n1 = 0; n1 < _js2; n1 += 2) {
423 switch (arguments[n1]) {
426 baz = arguments[n1 + 1];
431 bar = arguments[n1 + 1];
435 if (baz === undefined) {
438 if (bar === undefined) {
444 (test-ps-js defun-keyword4
445 (defun hello-world (&key
((:my-name-key my-name
) 1))
447 "function helloWorld() {
449 var _js2 = arguments.length;
450 for (var n1 = 0; n1 < _js2; n1 += 2) {
451 switch (arguments[n1]) {
454 myName = arguments[n1 + 1];
458 if (myName === undefined) {
464 (test-ps-js keyword-funcall1
468 (test-ps-js keyword-funcall2
469 (func :baz
1 :bar foo
)
470 "func('baz', 1, 'bar', foo)")
472 (test-ps-js keyword-funcall3
474 "fun(a, b, 'baz', c)")
484 ((= y
(* x
4)) (foo "blah") (* x y
)))
487 } else if (y == x * 4) {
492 (test-ps-js if-exp-without-else-returns-null
494 "return x ? 1 : null")
496 (test-ps-js progn-expression-single-statement
497 (return (progn (* x y
)))
500 (test-ps-js cond-expression1
501 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
503 return 1 < 2 ? (bar('foo'), 4 * 5) : null;
506 (test-ps-js cond-expression2
507 (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar"))))
509 return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null);
512 (test-ps-js cond-expression-final-t-clause
513 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b
) (+ c d
)) ((< 1 2 3 4 5) x
) (t "foo"))))
515 return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo'));
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"))))
521 return 2 < 1 ? 5 : 'foo';
524 (test-ps-js funcall-if-expression
526 (if (= *linkornot
* 1)
527 (ps-html ((:a
:href
"#"
528 :onclick
(ps-inline (transport)))
531 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">' + img + '</A>' : img)")
533 (test-ps-js negate-number-literal
;; ok, this was broken and fixed before, but no one bothered to add the test!
537 (test macro-environment1
538 (is (string= (normalize-js-code (let* ((macroname (gensym)))
539 (ps* `(defmacro ,macroname
(x) `(+ ,x
123))
541 (macrolet ((,macroname
(x) `(aref data
,x
)))
543 (setf (,macroname x
) 123)))))))
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;"))))
559 (test-ps-js ampersand-whole-1
560 (macrolet ((foo (&whole foo bar baz
)
561 (declare (ignore bar baz
))
562 (format nil
"~a" foo
)))
566 (test-ps-js keyword-consistent
570 (test-ps-js simple-symbol-macrolet
571 (symbol-macrolet ((x 1)) x
)
574 (test-ps-js compound-symbol-macrolet
575 (symbol-macrolet ((x 123)
580 (test-ps-js define-symbol-macro
581 (progn (define-symbol-macro tst-sym-macro
2)
585 (test-ps-js expression-progn
586 (defun f () (return (progn (foo) (if x
1 2))))
588 return (foo(), x ? 1 : 2);
591 (test-ps-js let-decl-in-expression
592 (defun f (x) (return (if x
1 (let* ((foo x
)) foo
))))
595 return x ? 1 : (foo1 = x, foo1);
598 (test-ps-js special-var1
599 (progn (defvar *foo
*) (let* ((*foo
* 2)) (* *foo
* 2)))
610 (test-ps-js special-var2
611 (progn (defvar *foo
*) (let* ((*baz
* 3) (*foo
* 2)) (* *foo
* 2 *baz
*)))
631 (test-ps-js setf-dec1
635 (test-ps-js setf-dec2
639 (test-ps-js special-char-equals
643 (test-ps-js setf-operator-priority
644 (return (or (slot-value cache id
)
645 (setf (slot-value cache id
) ((@ document get-element-by-id
) id
))))
646 "return cache[id] || (cache[id] = document.getElementById(id))")
648 (test-ps-js aref-operator-priority
649 (aref (if (and x
(> (length x
) 0))
653 "(x && x.length > 0 ? x[0] : y)[z]")
655 (test-ps-js aref-operator-priority1
656 (aref (or (slot-value x
'y
)
661 (test-ps-js aref-operator-priority2
665 (test-ps-js negative-operator-priority
674 (delete (if a
(or b c
) d
))
675 "delete (a ? b || c : d)")
678 (not (if (or x
(not y
)) z
))
679 "!(x || !y ? z : null)")
686 (instanceof (or a b
) (if x y z
))
687 "((a || b) instanceof (x ? y : z))")
690 (or x
(if (= x
0) "zero" "empty"))
691 "x || (x == 0 ? 'zero' : 'empty')")
693 (test-ps-js named-op-expression
697 (test-ps-js named-op-expression1
701 (test-ps-js aref-array-expression
705 (test-ps-js slot-value-operator
706 (slot-value (or a b c
) 'd
)
709 (test-ps-js slot-value-parens
710 (slot-value (slot-value foo
'bar
) 'baz
)
713 (test-ps-js funcall-funcall
717 (test-ps-js expression-funcall
718 ((or (@ window eval
) eval
) foo nil
)
719 "(window.eval || eval)(foo, null)")
721 (test-ps-js expression-funcall1
722 (((or (@ window eval
) eval
) foo nil
))
723 "(window.eval || eval)(foo, null)()")
725 (test-ps-js expression-funcall2
726 (((or (@ window eval
) eval
)) foo nil
)
727 "(window.eval || eval)()(foo, null)")
729 (test-ps-js slot-value-object-literal
730 (slot-value (create :a
1) 'a
)
733 (test-ps-js slot-value-lambda
734 (slot-value (lambda ()) 'prototype
)
735 "(function () { }).prototype")
737 (test-ps-js who-html1
738 (who-ps-html (:span
:class
"ticker-symbol"
739 :ticker-symbol symbol
740 (:a
:href
"http://foo.com"
742 (:span
:class
"ticker-symbol-popup")))
743 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>'")
746 ((lambda () (flet ((foo (x) (return (1+ x
)))) (return (foo 1)))))
748 var foo = function (x) {
755 ((lambda () (labels ((foo (x)
756 (return (if (=== 0 x
)
758 (+ x
(foo (1- x
)))))))
761 var foo = function foo(x) {
762 return 0 === x ? 0 : x + foo(x - 1);
767 (test-ps-js for-loop-var-init-exp
769 (return (do* ((y (if x
0 1) (1+ y
))
774 return (function () {
775 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
785 (test-ps-js literal-array
789 (test-ps-js literal-array-1
793 (test ps-lisp-expands-in-lexical-environment
794 (is (string= "5;" (let ((x 5)) (ps (lisp x
))))))
796 (test ps
*-lisp-expands-in-null-lexical-environment
797 (signals error
(let ((x 5)) (declare (ignore x
)) (ps* '(lisp x
)))))
799 (test ps
*-lisp-expands-in-dynamic-environment
800 (is (string= "1 + 2;" (let ((*print-level
* 2)) (ps* '(+ 1 (lisp *print-level
*)))))))
802 (test ps-lisp-dynamic-environment
803 (is (string= "1 + 2;" (let ((*print-level
* 2)) (ps (+ 1 (lisp *print-level
*)))))))
805 (test-ps-js ps-js-target-version-keyword-test1
806 (defun foo (x y
&key bar baz
))
807 "function foo(x, y) {
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];
813 :js-target-version
1.6)
815 (test-ps-js nested-if-expressions1
816 (return (if (if x y z
) a b
))
817 "return (x ? y : z) ? a : b")
819 (test-ps-js nested-if-expressions2
820 (return (if x y
(if z a b
)))
821 "return x ? y : (z ? a : b)")
875 (return (let (x) (+ x x
))))
878 return (x1 = null, x1 + x1);
882 (let* ((x 1)) (+ x x
))