Removed compile-time constant string concatenation from the Parenscript printer,...
[clinton/parenscript.git] / t / ps-tests.lisp
index 82ece68..d1779e9 100644 (file)
@@ -7,12 +7,12 @@
 (in-suite ps-tests)
 
 (test-ps-js plus-is-not-commutative
-   (setf x (+ "before" x "after"))
-   "x = 'before' + x + 'after';")
+  (setf x (+ "before" x "after"))
+  "x = 'before' + x + 'after';")
 
 (test-ps-js plus-works-if-first
-   (setf x (+ x "middle" "after"))
-   "x += 'middle' + 'after';")
+  (setf x (+ x "middle" "after"))
+  "x += 'middle' + 'after';")
 
 (test-ps-js setf-side-effects
   (progn
@@ -27,7 +27,7 @@ function sideEffect() {
   return 3;
 };
 x = 2 + sideEffect() + x + 5;")
-;; Parenscript used to optimize too much:
+;; Parenscript used to optimize incorrectly:
 ;;   var x = 10;
 ;;   function sideEffect() {
 ;;     x = 4;
@@ -38,37 +38,52 @@ x = 2 + sideEffect() + x + 5;")
 ;;   Which is 20, not 14
 
 
-(test-ps-js dot-notation-bug
-            (.match (+ "" x) "foo")
-            "('' + x).match('foo')")
+(test-ps-js method-call-op-form
+  ((@ (+ "" x) to-string))
+  "('' + x).toString()")
+
+(test-ps-js method-call-op-form-args
+  ((@ (+ "" x) to-string) 1 2 :baz 3)
+  "('' + x).toString(1, 2, { baz : 3 })")
+
+(test-ps-js method-call-number
+  ((@ 10 to-string))
+  "( 10 ).toString()")
+
+(test-ps-js method-call-string
+  ((@ "hi" to-string))
+  "'hi'.toString()")
 
-(test-ps-js method-call-op-form (.to-string (+ "" x)) "('' + x).toString()")
-(test-ps-js method-call-number (.to-string 10) "( 10 ).toString()")
-(test-ps-js method-call-string (.to-string "hi") "'hi'.toString()")
 (test-ps-js method-call-lit-object
-            (.to-string (create :to-string (lambda ()
-                                             (return "it works"))))
-            "( { toString : function () { return 'it works'; } } ).toString()")
+  ((@ (create :to-string (lambda () (return "it works"))) to-string))
+  "( { toString : function () { return 'it works'; } } ).toString()")
+
+(test-ps-js method-call-conditional
+  ((if a x y) 1)
+  "(a ? x : y)(1)")
 
 (test-ps-js method-call-variable
-            (.to-string x)
-            "x.toString()")
+  ((@ x to-string))
+  "x.toString()")
 
 (test-ps-js method-call-array
-            (.to-string (list 10 20))
-            "[ 10, 20 ].toString()")
+  ((@ (list 10 20) to-string))
+  "[ 10, 20 ].toString()")
+
 (test-ps-js method-call-fn-call
-            (.to-string (foo))
-            "foo().toString()")
+  ((@ (foo) to-string))
+  "foo().toString()")
+
 (test-ps-js method-call-lambda-fn
-            (.to-string (lambda () (alert 10)))
-            "( function () { alert(10); } ).toString()")
+  ((@ (lambda () (alert 10)) to-string))
+  "( function () { alert(10); } ).toString()")
+
 (test-ps-js method-call-lambda-call
-            (.to-string ((lambda (x) (return x)) 10))
-            "(function (x) { return x; })(10).toString()")
+  ((@ ((lambda (x) (return x)) 10) to-string))
+  "(function (x) { return x; })(10).toString()")
 
 (test no-whitespace-before-dot
-  (let* ((str (ps1* '(.to-string ((lambda (x) (return x)) 10))))
+  (let* ((str (ps1* '((@ ((lambda (x) (return x)) 10) to-string))))
          (dot-pos (position #\. str :test #'char=))
          (char-before (elt str (1- dot-pos)))
          (a-parenthesis #\)))
@@ -196,42 +211,39 @@ x = 2 + sideEffect() + x + 5;")
 (test script-star-eval2
   (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
 
-(test-ps-js slot-value-null1
-  (slot-value foo nil)
-  "foo")
-
-(test-ps-js slot-value-null2
-  (slot-value foo 'nil)
-  "foo")
-
 (test-ps-js unquoted-nil
   nil
   "null")
 
 (test-ps-js list-with-single-nil
-  (array 'nil)
+  (array nil)
   "[null]")
 
-(test-ps-js quoted-nil
+(test-ps-js quoted-nil-is-array
   'nil
-  "null")
-
-(test defsetf1
-  (ps (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval)))
-  (is (string= "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);"
-               (normalize-js-code (let* ((ps:*ps-gensym-counter* 0))
-                                    (ps (setf (baz 1 2) 3)))))))
-
-(test defsetf-short
-  (ps (defsetf baz set-baz "blah"))
-  (is (string= "setBaz(1, 2, 3, 'foo');" (normalize-js-code (ps (setf (baz 1 2 3) "foo"))))))
-
-(test defun-setf1
-  (is (and (string= (normalize-js-code (ps:ps (defun (setf some-thing) (new-val i1 i2)
-                                                (setf (aref *some-thing* i1 i2) new-val))))
-               "function __setf_someThing(newVal, i1, i2) { SOMETHING[i1][i2] = newVal; };")
-           (string= (normalize-js-code (ps:ps-doc (setf (some-thing 1 2) "foo")))
-                    "var _js2 = 1; var _js3 = 2; var _js1 = 'foo'; __setf_someThing(_js1, _js2, _js3);"))))
+  "new Array()")
+
+(test-ps-js defsetf1
+  (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
+         (setf (baz 1 2) 3))
+  "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);")
+
+(test-ps-js defsetf-short
+  (progn (defsetf baz set-baz "docstring")
+         (setf (baz 1 2 3) "foo"))
+  "setBaz(1, 2, 3, 'foo');")
+
+(test-ps-js defun-setf1
+  (progn (defun (setf some-thing) (new-val i1 i2)
+           (setf (aref *some-thing* i1 i2) new-val))
+         (setf (some-thing 1 2) "foo"))
+  "function __setf_someThing(newVal, i1, i2) {
+    SOMETHING[i1][i2] = newVal;
+};
+var _js3 = 1;
+var _js4 = 2;
+var _js2 = 'foo';
+__setf_someThing(_js2, _js3, _js4);")
 
 (test-ps-js defun-optional1
   (defun test-opt (&optional x) (return (if x "yes" "no")))
@@ -308,12 +320,16 @@ x = 2 + sideEffect() + x + 5;")
   (slot-value foo "bar")
   "foo['bar']")
 
+(test-ps-js slot-value-string1
+  (slot-value "bar" 'length)
+  "'bar'.length")
+
 (test-ps-js slot-value-progn
   (slot-value (progn (some-fun "abc") "123") "length")
   "(someFun('abc'), '123')['length']")
 
 (test-ps-js method-call-block
-  (.to-string (progn (some-fun "abc") "123"))
+  ((@ (progn (some-fun "abc") "123") to-string))
   "(someFun('abc'), '123').toString()")
 
 (test-ps-js create-blank
@@ -324,14 +340,6 @@ x = 2 + sideEffect() + x + 5;")
   {}
   "{ }")
 
-(test-ps-js object-literal-1
-  ({})
-  "{ }")
-
-(test-ps-js object-literal-2
-  ({} a 1 b 2)
-  "{a: 1, b: 2 }")
-
 (test-ps-js array-literal1
   []
   "[]")
@@ -465,10 +473,10 @@ x = 2 + sideEffect() + x + 5;")
   (document.write
    (if (= *linkornot* 1)
        (ps-html ((:a :href "#"
-                     :onclick (lisp (ps-inline (transport))))
+                     :onclick (ps-inline (transport)))
                  img))
        img))
-  "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + 'javascript:transport()' + '\">' + img + '</A>' : img)")
+  "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">' + img + '</A>' : img)")
 
 (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test!
   (- 1)
@@ -673,3 +681,66 @@ try {
   (((or (@ window eval) eval)) foo nil)
   "(window.eval || eval)()(foo, null)")
 
+(test-ps-js slot-value-object-literal
+  (slot-value (create :a 1) 'a)
+  "({ a : 1 }).a")
+
+(test-ps-js slot-value-lambda
+  (slot-value (lambda ()) 'prototype)
+  "(function () { }).prototype")
+
+(test-ps-js who-html1
+  (who-ps-html (:span :class "ticker-symbol"
+                      :ticker-symbol symbol
+                      (:a :href "http://foo.com"
+                          symbol)
+                      (:span :class "ticker-symbol-popup")))
+  "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>'")
+
+(test-ps-js flet1
+  ((lambda () (flet ((foo (x) (return (1+ x)))) (return (foo 1)))))
+  "(function () {
+    var foo = function (x) {
+        return x + 1;
+    };
+    return foo(1);
+})()")
+
+(test-ps-js labels1
+  ((lambda () (labels ((foo (x) 
+                         (return (if (=== 0 x)
+                                     0
+                                     (+ x (foo (1- x)))))))
+                (return (foo 3)))))
+  "(function () {
+    var foo = function foo(x) {
+        return 0 === x ? 0 : x + foo(x - 1);
+    };
+    return foo(3);
+})()")
+
+(test-ps-js for-loop-var-init-exp
+  ((lambda (x)
+     (return (do* ((y (if x 0 1) (1+ y))
+                   (z 0 (1+ z)))
+                  ((= y 3) z))))
+   true)
+  "(function (x) {
+    return (function () {
+        for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) {
+        };
+        return z;
+    })();
+})(true)")
+
+(test-ps-js math-pi
+  pi
+  "Math.PI")
+
+(test-ps-js literal-array
+  '(1 2 3)
+  "[1, 2, 3]")
+
+(test-ps-js literal-array-1
+  '(1 foo 3)
+  "[1, 'foo', 3]")