Stopped abuse of set-difference implementation-dependent ordering in defsetf.
[clinton/parenscript.git] / t / ps-tests.lisp
index cb0bd96..a2328a0 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
@@ -68,8 +68,7 @@ x = 2 + sideEffect() + x + 5;")
             "(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 #\)))
@@ -173,8 +172,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,7 +184,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)
@@ -202,7 +200,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,7 +211,48 @@ 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 (script* '(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 (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")