Fixed bug where (setf x (- x 1 2)) yielded x -= 1 - 2.
[clinton/parenscript.git] / t / ps-tests.lisp
index fe6112c..715461b 100644 (file)
    "x += 'middle' + 'after';")
 
 (test-ps-js setf-side-effects
-            (progn
-              (let ((x 10))
-                (defun side-effect() 
-                  (setf x 4)
-                  (return 3))
-                (setf x (+ 2 (side-effect) x 5))))
-            "
-var x = 10;
+  (progn
+    (let* ((x 10))
+      (defun side-effect() 
+        (setf x 4)
+        (return 3))
+      (setf x (+ 2 (side-effect) x 5))))
+  "var x = 10;
 function sideEffect() {
   x = 4;
   return 3;
 };
 x = 2 + sideEffect() + x + 5;")
-;; Parenscript used to optimize to much:
+;; Parenscript used to optimize too much:
 ;;   var x = 10;
 ;;   function sideEffect() {
 ;;     x = 4;
@@ -75,28 +74,14 @@ x = 2 + sideEffect() + x + 5;")
          (a-parenthesis #\)))
     (is (char= char-before a-parenthesis))))
 
-;; A problem with long nested operator, when the statement spanned several rows
-;; the rows would not be joined together correctly.
-(test-ps-js bug-dwim-join
-   (alert (ps-html ((:div :id 777
-                          :style (css-inline :border "1pxsssssssssss"
-                                             :font-size "x-small"
-                                             :height (* 2 200)
-                                             :width (* 2 300))))))
-   "alert('<div id=\"777\" style=\"'
- + ('border:1pxsssssssssss;font-size:x-small;height:' + 2 * 200 + ';width:'
- + 2 * 300)
- + '\"></div>')") ;";This line should start with a plus character.
-
-
 (test-ps-js simple-slot-value
-  (let ((foo (create :a 1)))
+  (let* ((foo (create :a 1)))
    (alert (slot-value foo 'a)))
   "var foo = { a : 1 };
    alert(foo.a);")
 
 (test-ps-js buggy-slot-value
-   (let ((foo (create :a 1))
+   (let* ((foo (create :a 1))
         (slot-name "a"))
     (alert (slot-value foo slot-name)))
   " var foo = { a : 1 };
@@ -181,7 +166,7 @@ x = 2 + sideEffect() + x + 5;")
                    ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
                    ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
     (loop for (js-escape . lisp-char) in escapes
-         for generated = (compile-script `(let ((x ,(format nil "hello~ahi" lisp-char)))))
+         for generated = (compile-script `(let* ((x ,(format nil "hello~ahi" lisp-char)))))
          for wanted = (format nil "var x = 'hello\\~ahi';" js-escape)
          do (is (string= (normalize-js-code generated) wanted)))))
   
@@ -234,7 +219,7 @@ x = 2 + sideEffect() + x + 5;")
 (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))
+               (normalize-js-code (let* ((ps:*ps-gensym-counter* 0))
                                     (ps (setf (baz 1 2) 3)))))))
 
 (test defsetf-short
@@ -255,6 +240,13 @@ x = 2 + sideEffect() + x + 5;")
   return x ? 'yes' : 'no';
 }")
 
+(test-ps-js defun-optional2
+  (defun foo (x &optional y) (+ x y))
+  "function foo(x, y) {
+    y = undefined === y && null || y;
+    x + y;
+}")
+
 (test-ps-js return-nothing
   (return)
   "return null")
@@ -312,14 +304,14 @@ x = 2 + sideEffect() + x + 5;")
 
 (test-ps-js blank-object-literal
   {}
-  "{ }")
+  "{}")
 
 (test-ps-js defun-rest1
   (defun foo (&rest bar) (alert bar[1]))
   "function foo() {
     var bar = [];
-    for (var _js2 = 0; _js2 < arguments.length - 0; _js2 = _js2 + 1) {
-        bar[_js2] = arguments[_js2 + 0];
+    for (var i2 = 0; i2 < arguments.length - 0; i2 = i2 + 1) {
+        bar[i2] = arguments[i2 + 0];
     };
     alert(bar[1]);
 }")
@@ -328,8 +320,8 @@ x = 2 + sideEffect() + x + 5;")
   (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
   "function foo(baz) {
     var bar = [];
-    for (var _js2 = 0; _js2 < arguments.length - 1; _js2 = _js2 + 1) {
-        bar[_js2] = arguments[_js2 + 1];
+    for (var i2 = 0; i2 < arguments.length - 1; i2 = i2 + 1) {
+        bar[i2] = arguments[i2 + 1];
     };
     return baz + bar[1];
 }")
@@ -395,14 +387,14 @@ x = 2 + sideEffect() + x + 5;")
                      :onclick (lisp (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)
   "-1")
 
 (test macro-environment1
-  (is (string= (normalize-js-code (let ((macroname (gensym)))
+  (is (string= (normalize-js-code (let* ((macroname (gensym)))
                                     (ps* `(defmacro ,macroname (x) `(+ ,x 123))
                                          `(defun test1 ()
                                            (macrolet ((,macroname (x) `(aref data ,x)))
@@ -421,4 +413,77 @@ x = 2 + sideEffect() + x + 5;")
                (declare (ignore bar baz))
                (format nil "~a" foo)))
     (foo 1 2))
-  "'(FOO 1 2)';")
\ No newline at end of file
+  "'(FOO 1 2)';")
+
+(test-ps-js keyword-consistent
+  :x
+  "x")
+
+(test-ps-js simple-symbol-macrolet
+  (symbol-macrolet ((x 1)) x)
+  "1;")
+
+(test-ps-js compound-symbol-macrolet
+  (symbol-macrolet ((x 123)
+                    (y (* 2 x)))
+     y)
+  "2 * 123;")
+
+(test-ps-js define-symbol-macro
+  (progn (define-symbol-macro tst-sym-macro 2)
+         tst-sym-macro)
+  "2;")
+
+(test-ps-js expression-progn
+  (defun f () (return (progn (foo) (if x 1 2))))
+  "function f() {
+    return (foo(), x ? 1 : 2);
+}")
+
+(test-ps-js let-decl-in-expression
+  (defun f (x) (return (if x 1 (let* ((foo x)) foo))))
+  "function f(x) {
+    var foo;
+    return x ? 1 : (foo = x, foo);
+}")
+
+(test-ps-js special-var1
+  (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2)))
+  "var FOO;
+var tempstackvar1;
+try {
+    tempstackvar1 = FOO;
+    FOO = 2;
+    FOO * 2;
+} finally {
+    FOO = tempstackvar1;
+};")
+
+(test-ps-js special-var2
+  (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*)))
+  "var FOO;
+var BAZ = 3;
+var tempstackvar1;
+try {
+    tempstackvar1 = FOO;
+    FOO = 2;
+    FOO * 2 * BAZ;
+} finally {
+    FOO = tempstackvar1;
+};")
+
+(test-ps-js literal1
+  (setf x undefined)
+  "x = undefined;")
+
+(test-ps-js literal2
+  (aref this x)
+  "this[x]")
+
+(test-ps-js setf-dec1
+  (setf x (- 1 x 2))
+  "x = 1 - x - 2;")
+
+(test-ps-js setf-dec2
+  (setf x (- x 1 2))
+  "x = x - 1 - 2;")