Updated the documentation and test suite to reflect the fact that html is now ps...
[clinton/parenscript.git] / t / ps-tests.lisp
index 167b84c..e5835df 100644 (file)
@@ -7,11 +7,11 @@
 
 (test-ps-js plus-is-not-commutative
    (setf x (+ "before" x "after"))
-   "x = 'before' + x + 'after'")
+   "x = 'before' + x + 'after';")
 
 (test-ps-js plus-works-if-first
    (setf x (+ x "middle" "after"))
-   "x += 'middle' + 'after'")
+   "x += 'middle' + 'after';")
 
 (test-ps-js setf-side-effects
             (progn
@@ -65,11 +65,10 @@ x = 2 + sideEffect() + x + 5;")
             "( function () { alert(10); } ).toString()")
 (test-ps-js method-call-lambda-call
             (.to-string ((lambda (x) (return x)) 10))
-            "(function (x) { return x; }) (10).toString()")
+            "(function (x) { return x; })(10).toString()")
 
 (test no-whitespace-before-dot
-  (let* ((parenscript::*enable-package-system* nil)
-        (str (compile-script '(.to-string ((lambda (x) (return x)) 10))))
+  (let* ((str (compile-script '(.to-string ((lambda (x) (return x)) 10))))
          (dot-pos (position #\. str :test #'char=))
          (char-before (elt str (1- dot-pos)))
          (a-parenthesis #\)))
@@ -78,13 +77,12 @@ x = 2 + sideEffect() + x + 5;")
 ;; 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 (html ((:div :id 777
-                       :style (css-inline :border "1pxsssssssssss"
-                                          :font-size "x-small"
-                                          :height (* 2 200)
-                                          :width (* 2 300))))))
-   "alert
-('<div id=\"777\" style=\"'
+   (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.
@@ -93,20 +91,17 @@ x = 2 + sideEffect() + x + 5;")
 (test-ps-js simple-slot-value
   (let ((foo (create :a 1)))
    (alert (slot-value foo 'a)))
-  "{
-    var foo = { a : 1 };
-    alert(foo.a);
-   }")
+  "var foo = { a : 1 };
+   alert(foo.a);")
 
 (test-ps-js buggy-slot-value
    (let ((foo (create :a 1))
         (slot-name "a"))
     (alert (slot-value foo slot-name)))
-  "{
-    var foo = { a : 1 };
+  " var foo = { a : 1 };
     var slotName = 'a';
     alert(foo[slotName]);
-   }"); Last line was alert(foo.slotName) before bug-fix.
+   "); Last line was alert(foo.slotName) before bug-fix.
 
 (test-ps-js buggy-slot-value-two
   (slot-value foo (get-slot-name))
@@ -151,7 +146,7 @@ x = 2 + sideEffect() + x + 5;")
       (3 (alert "Three"))
       (t (alert "Something else")))
    "switch (blorg[i]) {
-         case 1:   ;
+         case 1:
          case 2:
                    alert('Below three');
                    break;
@@ -173,8 +168,7 @@ x = 2 + sideEffect() + x + 5;")
          }")
 
 (test escape-sequences-in-string
-  (let ((parenscript::*enable-package-system* nil)
-       (escapes `((#\\ . #\\)
+  (let ((escapes `((#\\ . #\\)
                    (#\b . #\Backspace)
                    (#\f . ,(code-char 12))
                    ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
@@ -186,11 +180,9 @@ 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 wanted = (format nil "{
-  var x = 'hello\\~ahi';
-}" js-escape)
-         do (is (string= generated wanted)))))
+         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)))))
   
 (test-ps-js complicated-symbol-name1
   grid-rows[foo].bar
@@ -202,7 +194,7 @@ x = 2 + sideEffect() + x + 5;")
 
 (test-ps-js slot-value-setf
   (setf (slot-value x 'y) (+ (+ a 3) 4))
-  "x.y = (a + 3) + 4")
+  "x.y = (a + 3) + 4;")
 
 (test-ps-js slot-value-conditional1
   (slot-value (if zoo foo bar) 'x)
@@ -213,10 +205,10 @@ x = 2 + sideEffect() + x + 5;")
   "(!zoo ? foo : bar).x")
 
 (test script-star-eval1
-  (is (string= "x = 1; y = 2;" (normalize-js-code (let ((*enable-package-system* nil)) (script* '(setf x 1) '(setf y 2)))))))
+  (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
 
 (test script-star-eval2
-  (is (string= "x = 1;" (normalize-js-code (let ((*enable-package-system* nil)) (script* '(setf x 1)))))))
+  (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
 
 (test-ps-js slot-value-null1
   (slot-value foo nil)
@@ -226,6 +218,117 @@ x = 2 + sideEffect() + x + 5;")
   (slot-value foo 'nil)
   "foo")
 
+(test-ps-js unquoted-nil
+  nil
+  "null")
+
+(test-ps-js list-with-single-nil
+  (array 'nil)
+  "[null]")
+
 (test-ps-js quoted-nil
   'nil
-  "null")
\ No newline at end of file
+  "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))))
+               "null; function __setf_someThing(newVal, i1, i2) { SOMETHING[i1][i2] = newVal; };")
+           (string= (let ((ps:*ps-gensym-counter* 0)) (normalize-js-code (ps:ps (setf (some-thing 1 2) "foo"))))
+               "var _js2 = 1; var _js3 = 2; var _js1 = 'foo'; __setf_someThing(_js1, _js2, _js3);"))))
+
+(test-ps-js defun-optional1
+  (defun test-opt (&optional x) (return (if x "yes" "no")))
+  "function testOpt(x) {
+  x = undefined === x && null || x;
+  return x ? 'yes' : 'no';
+}")
+
+(test-ps-js return-nothing
+  (return)
+  "return null")
+
+(test-ps-js set-timeout
+  (do-set-timeout (10) (alert "foo"))
+  "setTimeout(function () { alert('foo'); }, 10)")
+
+(test-ps-js operator-precedence
+  (* 3 (+ 4 5) 6)
+  "3 * (4 + 5) * 6")
+
+(test-ps-js incf1
+  (incf foo bar)
+  "foo += bar")
+
+(test-ps-js decf1
+  (decf foo bar)
+  "foo -= bar")
+
+(test-ps-js setf-conditional
+  (setf foo (if x 1 2))
+  "foo = x ? 1 : 2;")
+
+(test-ps-js obj-literal-numbers
+  (create 1 "foo")
+  "{ 1 : 'foo' }")
+
+(test-ps-js obj-literal-strings
+  (create "foo" 2)
+  "{ 'foo' : 2 }")
+
+(test-ps-js slot-value-string
+  (slot-value foo "bar")
+  "foo['bar']")
+
+(test-ps-js slot-value-progn
+  (slot-value (progn "abc" "123") "length")
+  "('abc', '123')['length']")
+
+(test-ps-js method-call-block
+  (.to-string (progn "abc" "123"))
+  "('abc', '123').toString()")
+
+(test-ps-js create-blank
+  (create)
+  "{ }")
+
+(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];
+    };
+    alert(bar[1]);
+}")
+
+(test-ps-js defun-rest2
+  (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];
+    };
+    return baz + bar[1];
+}")
+
+(test-ps-js defun-keyword1
+  (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
+  "function zoo(foo, bar, _js1) {
+    _js1 = undefined === _js1 && {  } || _js1;
+    return foo + bar + _js1.baz;
+}")