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 x = 2 + sideEffect() + x + 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
)))
98 (test-ps-js buggy-slot-value
99 (let* ((foo (create :a
1))
101 (alert (slot-value foo slot-name
)))
102 " var foo = { a : 1 };
104 alert(foo[slotName]);
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
= (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
)))))
188 (test-ps-js complicated-symbol-name1
192 (test-ps-js complicated-symbol-name2
196 (test-ps-js slot-value-setf
197 (setf (slot-value x 'y) (+ (+ a 3) 4))
198 "x.y = (a + 3) + 4;")
200 (test-ps-js slot-value-conditional1
201 (slot-value (if zoo foo bar) 'x)
202 "(zoo ? foo : bar).x")
204 (test-ps-js slot-value-conditional2
205 (slot-value (if (not zoo) foo bar) 'x)
206 "(!zoo ? foo : bar).x")
208 (test script-star-eval1
209 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
211 (test script-star-eval2
212 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
214 (test-ps-js unquoted-nil
218 (test-ps-js list-with-single-nil
222 (test-ps-js quoted-nil-is-array
227 (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
229 "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);")
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');")
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"))
240 "function __setf_someThing(newVal, i1, i2) {
241 SOMETHING[i1][i2] = newVal;
246 __setf_someThing(_js1, _js2, _js3);")
248 (test-ps-js defun-optional1
249 (defun test-opt (&optional x) (return (if x "yes" "no")))
250 "function testOpt(x) {
251 if (x === undefined) {
254 return x ? 'yes' : 'no';
257 (test-ps-js defun-optional2
258 (defun foo (x &optional y) (+ x y))
259 "function foo(x, y) {
260 if (y === undefined) {
266 (test-ps-js defun-optional3
267 (defun blah (&optional (x 0))
270 if (x === undefined) {
276 (test-ps-js return-nothing
280 (test-ps-js set-timeout
281 (do-set-timeout (10) (alert "foo"))
282 "setTimeout(function () { alert('foo'); }, 10)")
283 (test-ps-js operator-precedence
287 (test-ps-js operators-1
307 (test-ps-js setf-conditional
308 (setf foo (if x 1 2))
311 (test-ps-js obj-literal-numbers
315 (test-ps-js obj-literal-strings
319 (test-ps-js slot-value-string
320 (slot-value foo "bar")
323 (test-ps-js slot-value-string1
324 (slot-value "bar" 'length)
327 (test-ps-js slot-value-progn
328 (slot-value (progn (some-fun "abc") "123") "length")
329 "(someFun('abc'), '123')['length']")
331 (test-ps-js method-call-block
332 ((@ (progn (some-fun "abc") "123") to-string))
333 "(someFun('abc'), '123').toString()")
335 (test-ps-js create-blank
339 (test-ps-js blank-object-literal
343 (test-ps-js array-literal1
347 (test-ps-js array-literal2
351 (test-ps-js array-literal3
355 (test-ps-js array-literal4
359 (test-ps-js array-literal5
361 "[[1, 2], ['a', 'b']]")
363 (test-ps-js defun-rest1
364 (defun foo (&rest bar) (alert bar[1]))
367 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
368 bar[i1] = arguments[i1 + 0];
373 (test-ps-js defun-rest2
374 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
377 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
378 bar[i1] = arguments[i1 + 1];
383 (test-ps-js defun-keyword1
384 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
385 "function zoo(foo, bar) {
387 var _js3 = arguments.length;
388 for (var n1 = 2; n1 < _js3; n1 += 2) {
389 switch (arguments[n1]) {
392 baz = arguments[n1 + 1];
396 if (baz === undefined) {
399 return foo + bar + baz;
402 (test-ps-js defun-keyword2
403 (defun zoo (&key baz) (return (* baz baz)))
406 var _js3 = arguments.length;
407 for (var n1 = 0; n1 < _js3; n1 += 2) {
408 switch (arguments[n1]) {
411 baz = arguments[n1 + 1];
415 if (baz === undefined) {
421 (test-ps-js defun-keyword3
422 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
426 var _js3 = arguments.length;
427 for (var n1 = 0; n1 < _js3; n1 += 2) {
428 switch (arguments[n1]) {
431 baz = arguments[n1 + 1];
436 bar = arguments[n1 + 1];
440 if (baz === undefined) {
443 if (bar === undefined) {
449 (test-ps-js defun-keyword4
450 (defun hello-world (&key ((:my-name-key my-name) 1))
452 "function helloWorld() {
454 var _js3 = arguments.length;
455 for (var n1 = 0; n1 < _js3; n1 += 2) {
456 switch (arguments[n1]) {
459 myName = arguments[n1 + 1];
463 if (myName === undefined) {
469 (test-ps-js keyword-funcall1
473 (test-ps-js keyword-funcall2
474 (func :baz 1 :bar foo)
475 "func('baz', 1, 'bar', foo)")
477 (test-ps-js keyword-funcall3
479 "fun(a, b, 'baz', c)")
489 ((= y (* x 4)) (foo "blah") (* x y)))
492 } else if (y == x * 4) {
497 (test-ps-js if-exp-without-else-returns-null
499 "return x ? 1 : null")
501 (test-ps-js progn-expression-single-statement
502 (return (progn (* x y)))
505 (test-ps-js cond-expression1
506 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
508 return 1 < 2 ? (bar('foo'), 4 * 5) : null;
511 (test-ps-js cond-expression2
512 (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar"))))
514 return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null);
517 (test-ps-js cond-expression-final-t-clause
518 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo"))))
520 return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo'));
523 (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
524 (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar"))))
526 return 2 < 1 ? 5 : 'foo';
529 (test-ps-js funcall-if-expression
531 (if (= *linkornot* 1)
532 (ps-html ((:a :href "#"
533 :onclick (ps-inline (transport)))
536 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">' + img + '</A>' : img)")
538 (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test!
542 (test macro-environment1
543 (is (string= (normalize-js-code (let* ((macroname (gensym)))
544 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
546 (macrolet ((,macroname (x) `(aref data ,x)))
548 (setf (,macroname x) 123)))))))
557 (test macro-environment2
558 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
559 (defpsmacro macro-environment2-macro (x)
560 `(+ ,outer-lexical-variable ,x))
561 (ps* '(macro-environment2-macro 2))))
562 (normalize-js-code "1 + 2;"))))
564 (test-ps-js ampersand-whole-1
565 (macrolet ((foo (&whole foo bar baz)
566 (declare (ignore bar baz))
567 (format nil "~a" foo)))
571 (test-ps-js keyword-consistent
575 (test-ps-js simple-symbol-macrolet
576 (symbol-macrolet ((x 1)) x)
579 (test-ps-js compound-symbol-macrolet
580 (symbol-macrolet ((x 123)
585 (test-ps-js define-symbol-macro
586 (progn (define-symbol-macro tst-sym-macro 2)
590 (test-ps-js expression-progn
591 (defun f () (return (progn (foo) (if x 1 2))))
593 return (foo(), x ? 1 : 2);
596 (test-ps-js let-decl-in-expression
597 (defun f (x) (return (if x 1 (let* ((foo x)) foo))))
600 return x ? 1 : (foo = x, foo);
603 (test-ps-js special-var1
604 (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2)))
613 delete tempstackvar1;
616 (test-ps-js special-var2
617 (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*)))
627 delete tempstackvar1;
639 (test-ps-js setf-dec1
643 (test-ps-js setf-dec2
647 (test-ps-js special-char-equals
651 (test-ps-js setf-operator-priority
652 (return (or (slot-value cache id)
653 (setf (slot-value cache id) (document.get-element-by-id id))))
654 "return cache[id] || (cache[id] = document.getElementById(id))")
656 (test-ps-js aref-operator-priority
657 (aref (if (and x (> (length x) 0))
661 "(x && x.length > 0 ? x[0] : y)[z]")
663 (test-ps-js aref-operator-priority1
664 (aref (or (slot-value x 'y)
669 (test-ps-js aref-operator-priority2
673 (test-ps-js negative-operator-priority
682 (delete (if a (or b c) d))
683 "delete (a ? b || c : d)")
686 (not (if (or x (not y)) z))
687 "!(x || !y ? z : null)")
694 (instanceof (or a b) (if x y z))
695 "((a || b) instanceof (x ? y : z))")
698 (or x (if (= x 0) "zero" "empty"))
699 "x || (x == 0 ? 'zero' : 'empty')")
701 (test-ps-js named-op-expression
705 (test-ps-js named-op-expression1
709 (test-ps-js aref-array-expression
713 (test-ps-js slot-value-operator
714 (slot-value (or a b c) 'd)
717 (test-ps-js slot-value-parens
718 (slot-value (slot-value foo 'bar) 'baz)
721 (test-ps-js funcall-funcall
725 (test-ps-js expression-funcall
726 ((or (@ window eval) eval) foo nil)
727 "(window.eval || eval)(foo, null)")
729 (test-ps-js expression-funcall1
730 (((or (@ window eval) eval) foo nil))
731 "(window.eval || eval)(foo, null)()")
733 (test-ps-js expression-funcall2
734 (((or (@ window eval) eval)) foo nil)
735 "(window.eval || eval)()(foo, null)")
737 (test-ps-js slot-value-object-literal
738 (slot-value (create :a 1) 'a)
741 (test-ps-js slot-value-lambda
742 (slot-value (lambda ()) 'prototype)
743 "(function () { }).prototype")
745 (test-ps-js who-html1
746 (who-ps-html (:span :class "ticker-symbol"
747 :ticker-symbol symbol
748 (:a :href "http://foo.com"
750 (:span :class "ticker-symbol-popup")))
751 "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>'")
754 ((lambda () (flet ((foo (x) (return (1+ x)))) (return (foo 1)))))
756 var foo = function (x) {
763 ((lambda () (labels ((foo (x)
764 (return (if (=== 0 x)
766 (+ x (foo (1- x)))))))
769 var foo = function foo(x) {
770 return 0 === x ? 0 : x + foo(x - 1);
775 (test-ps-js for-loop-var-init-exp
777 (return (do* ((y (if x 0 1) (1+ y))
782 return (function () {
783 for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
793 (test-ps-js literal-array
797 (test-ps-js literal-array-1
801 (test ps-lisp-expands-in-lexical-environment
802 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
804 (test ps*-lisp-expands-in-null-lexical-environment
805 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
807 (test ps*-lisp-expands-in-dynamic-environment
808 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*)))))))
810 (test ps-lisp-dynamic-environment
811 (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*)))))))
813 (test-ps-js ps-js-target-version-keyword-test1
814 (defun foo (x y &key bar baz))
815 "function foo(x, y) {
816 var x1 = Array.prototype.indexOf.call(arguments, 'bar', 2);
817 var bar = -1 == x1 ? null : arguments[x1 + 1];
818 var x2 = Array.prototype.indexOf.call(arguments, 'baz', 2);
819 var baz = -1 == x2 ? null : arguments[x2 + 1];
821 :js-target-version 1.6)