X-Git-Url: http://git.hcoop.net/clinton/parenscript.git/blobdiff_plain/d91b35a23937247b3865cf1cc78b4f09094add20..cb8f8e58295db370220ad7c2f0f3d8d48fdc2442:/t/ps-tests.lisp diff --git a/t/ps-tests.lisp b/t/ps-tests.lisp index e4a1dac..1dbe3ff 100644 --- a/t/ps-tests.lisp +++ b/t/ps-tests.lisp @@ -68,7 +68,7 @@ x = 2 + sideEffect() + x + 5;") "(function (x) { return x; })(10).toString()") (test no-whitespace-before-dot - (let* ((str (compile-script '(.to-string ((lambda (x) (return x)) 10)))) + (let* ((str (ps1* '(.to-string ((lambda (x) (return x)) 10)))) (dot-pos (position #\. str :test #'char=)) (char-before (elt str (1- dot-pos))) (a-parenthesis #\))) @@ -76,13 +76,13 @@ x = 2 + sideEffect() + x + 5;") (test-ps-js simple-slot-value (let* ((foo (create :a 1))) - (alert (slot-value foo 'a))) + (alert (slot-value foo 'a))) "var foo = { a : 1 }; alert(foo.a);") (test-ps-js buggy-slot-value (let* ((foo (create :a 1)) - (slot-name "a")) + (slot-name "a")) (alert (slot-value foo slot-name))) " var foo = { a : 1 }; var slotName = 'a'; @@ -166,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 = (ps1* `(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))))) @@ -228,25 +228,39 @@ x = 2 + sideEffect() + x + 5;") (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)))) + (setf (aref *some-thing* i1 i2) new-val)))) "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);")))) + (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);")))) (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'; + if (x === undefined) { + x = null; + }; + 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; + if (y === undefined) { + y = null; + }; x + y; }") +(test-ps-js defun-optional3 + (defun blah (&optional (x 0)) + (return x)) + "function blah(x) { + if (x === undefined) { + x = 0; + }; + return x; +}") + (test-ps-js return-nothing (return) "return null") @@ -258,6 +272,10 @@ x = 2 + sideEffect() + x + 5;") (* 3 (+ 4 5) 6) "3 * (4 + 5) * 6") +(test-ps-js operators-1 + (in prop obj) + "prop in obj") + (test-ps-js incf1 (incf foo bar) "foo += bar") @@ -314,6 +332,26 @@ x = 2 + sideEffect() + x + 5;") ({} a 1 b 2) "{a: 1, b: 2 }") +(test-ps-js array-literal1 + [] + "[]") + +(test-ps-js array-literal2 + ([]) + "[]") + +(test-ps-js array-literal3 + ([] 1 2 3) + "[1, 2, 3]") + +(test-ps-js array-literal4 + ([] 1 (2 3)) + "[1, [2, 3]]") + +(test-ps-js array-literal5 + ([] (1 2) ("a" "b")) + "[[1, 2], ['a', 'b']]") + (test-ps-js defun-rest1 (defun foo (&rest bar) (alert bar[1])) "function foo() { @@ -337,10 +375,45 @@ x = 2 + sideEffect() + x + 5;") (test-ps-js defun-keyword1 (defun zoo (foo bar &key baz) (return (+ foo bar baz))) "function zoo(foo, bar, _js1) { - _js1 = undefined === _js1 && { } || _js1; + if (_js1 === undefined) { + _js1 = { }; + }; return foo + bar + _js1.baz; }") +(test-ps-js defun-keyword2 + (defun zoo (&key baz) (return (* baz baz))) + "function zoo(_js1) { + if (_js1 === undefined) { + _js1 = { }; + }; + return _js1.baz * _js1.baz; +}") + +(test-ps-js defun-keyword3 + (defun zoo (&key baz (bar 4)) (return (* baz bar))) + "function zoo(_js1) { + if (_js1 === undefined) { + _js1 = { }; + }; + if (_js1.bar === undefined) { + _js1.bar = 4; + }; + return _js1.baz * _js1.bar; +}") + +(test-ps-js keyword-funcall1 + (func :baz 1) + "func({ baz : 1 })") + +(test-ps-js keyword-funcall2 + (func :baz 1 :bar foo) + "func({ baz : 1, bar : foo })") + +(test-ps-js keyword-funcall3 + (fun a b :baz c) + "fun(a, b, { baz : c })") + (test-ps-js cond1 (cond ((= x 1) 1)) "if (x == 1) { @@ -395,7 +468,7 @@ x = 2 + sideEffect() + x + 5;") :onclick (lisp (ps-inline (transport)))) img)) img)) - "document.write(LINKORNOT == 1 ? '' + img + '' : img)") + "document.write(LINKORNOT == 1 ? '' + img + '' : img)") (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test! (- 1) @@ -425,7 +498,7 @@ x = 2 + sideEffect() + x + 5;") (test-ps-js keyword-consistent :x - "x") + "'x'") (test-ps-js simple-symbol-macrolet (symbol-macrolet ((x 1)) x) @@ -502,3 +575,77 @@ try { (test-ps-js special-char-equals blah= "blahequals") + +(test-ps-js setf-operator-priority + (return (or (slot-value cache id) + (setf (slot-value cache id) (document.get-element-by-id id)))) + "return cache[id] || (cache[id] = document.getElementById(id))") + +(test-ps-js aref-operator-priority + (aref (if (and x (> (length x) 0)) + (aref x 0) + y) + z) + "(x && x.length > 0 ? x[0] : y)[z]") + +(test-ps-js aref-operator-priority1 + (aref (or (slot-value x 'y) + (slot-value a 'b)) + z) + "(x.y || a.b)[z]") + +(test-ps-js aref-operator-priority2 + (aref (if a b c) 0) + "(a ? b : c)[0]") + +(test-ps-js negative-operator-priority + (- (if x y z)) + "-(x ? y : z)") + +(test-ps-js op-p1 + (new (or a b)) + "new (a || b)") + +(test-ps-js op-p2 + (delete (if a (or b c) d)) + "delete (a ? b || c : d)") + +(test-ps-js op-p3 + (not (if (or x (not y)) z)) + "!(x || !y ? z : null)") + +(test-ps-js op-p4 + (- (- (* 1 2) 3)) + "-(1 * 2 - 3)") + +(test-ps-js op-p5 + (instanceof (or a b) (if x y z)) + "((a || b) instanceof (x ? y : z))") + +(test-ps-js op-p6 + (doeach (x (or a b))) + "for (var x in (a || b)) { };") + +(test-ps-js op-p7 + (or x (if (= x 0) "zero" "empty")) + "x || (x == 0 ? 'zero' : 'empty')") + +(test-ps-js named-op-expression + (throw (if a b c)) + "throw a ? b : c") + +(test-ps-js named-op-expression1 + (typeof (or x y)) + "typeof (x || y)") + +(test-ps-js aref-array-expression + (aref (or a b c) 0) + "(a || b || c)[0]") + +(test-ps-js slot-value-operator + (slot-value (or a b c) 'd) + "(a || b || c).d") + +(test-ps-js slot-value-parens + (slot-value (slot-value foo 'bar) 'baz) + "foo.bar.baz")