Stopped abuse of set-difference implementation-dependent ordering in defsetf.
[clinton/parenscript.git] / t / ps-tests.lisp
index 9ce20ea..a2328a0 100644 (file)
@@ -1,4 +1,4 @@
-(in-package :js-test)
+(in-package :ps-test)
 ;; Other tests not in the reference
 
 (eval-when (:compile-toplevel :load-toplevel :execute)
@@ -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
@@ -43,14 +43,12 @@ x = 2 + sideEffect() + x + 5;")
             "('' + x).match('foo')")
 
 (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-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()")
+            "( { toString : function () { return 'it works'; } } ).toString()")
 
 (test-ps-js method-call-variable
             (.to-string x)
@@ -58,19 +56,19 @@ x = 2 + sideEffect() + x + 5;")
 
 (test-ps-js method-call-array
             (.to-string (list 10 20))
-            "[10, 20].toString()")
+            "[ 10, 20 ].toString()")
 (test-ps-js method-call-fn-call
             (.to-string (foo))
             "foo().toString()")
 (test-ps-js method-call-lambda-fn
             (.to-string (lambda () (alert 10)))
-            "(function () {alert(10);}).toString()")
+            "( 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* ((str (js:js* '(.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 #\)))
@@ -165,7 +163,7 @@ x = 2 + sideEffect() + x + 5;")
 (test-ps-js otherwise-case
    (case (aref blorg i)
      (1 (alert "one"))
-     (otherwise (alert "default clause")))    
+     (otherwise (alert "default clause")))
      "switch (blorg[i]) {
          case 1:
                    alert('one');
@@ -186,9 +184,75 @@ 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 = (js-to-string `(let ((x , (format nil "hello~ahi" lisp-char)))))
-          for wanted = (format nil "{
+         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)))))
+         do (is (string= generated wanted)))))
+  
+(test-ps-js complicated-symbol-name1
+  grid-rows[foo].bar
+  "gridRows[foo].bar")
 
+(test-ps-js complicated-symbol-name2
+  *grid-rows*[foo].bar
+  "GRIDROWS[foo].bar")
+
+(test-ps-js slot-value-setf
+  (setf (slot-value x 'y) (+ (+ a 3) 4))
+  "x.y = (a + 3) + 4;")
+
+(test-ps-js slot-value-conditional1
+  (slot-value (if zoo foo bar) 'x)
+  "(zoo ? foo : bar).x")
+
+(test-ps-js slot-value-conditional2
+  (slot-value (if (not zoo) foo bar) 'x)
+  "(!zoo ? foo : bar).x")
+
+(test script-star-eval1
+  (is (string= "x = 1; y = 2;" (normalize-js-code (script* '(setf x 1) '(setf y 2))))))
+
+(test script-star-eval2
+  (is (string= "x = 1;" (normalize-js-code (script* '(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)
+  "[null]")
+
+(test-ps-js quoted-nil
+  'nil
+  "null")
+
+(test defsetf1
+  (ps (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval)))
+  (is (string= "var PS_GS_2 = 1; var PS_GS_3 = 2; var PS_GS_1 = 3; setBaz(PS_GS_2, PS_GS_3, PS_GS_1);"
+               (normalize-js-code (let ((ps::*gen-script-name-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-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")