(in-package :ps-test)
-;; Other tests not in the reference
+
+;;; Hand-written unit tests
(eval-when (:compile-toplevel :load-toplevel :execute)
(def-suite ps-tests))
(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; };")
+ "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 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")
(decf foo bar)
"foo -= bar")
+(test-ps-js incf2
+ (incf x 5)
+ "x += 5")
+
+(test-ps-js decf2
+ (decf y 10)
+ "y -= 10")
+
(test-ps-js setf-conditional
(setf foo (if x 1 2))
"foo = x ? 1 : 2;")
"foo['bar']")
(test-ps-js slot-value-progn
- (slot-value (progn "abc" "123") "length")
- "('abc', '123')['length']")
+ (slot-value (progn (some-fun "abc") "123") "length")
+ "(someFun('abc'), '123')['length']")
(test-ps-js method-call-block
- (.to-string (progn "abc" "123"))
- "('abc', '123').toString()")
+ (.to-string (progn (some-fun "abc") "123"))
+ "(someFun('abc'), '123').toString()")
(test-ps-js create-blank
(create)
_js1 = undefined === _js1 && { } || _js1;
return foo + bar + _js1.baz;
}")
+
+(test-ps-js cond1
+ (cond ((= x 1) 1))
+ "if (x == 1) {
+ 1;
+}")
+
+(test-ps-js cond2
+ (cond ((= x 1) 2) ((= y (* x 4)) (foo "blah") (* x y)))
+ "if (x == 1) {
+ 2;
+} else if (y == x * 4) {
+ foo('blah');
+ x * y;
+}")
+
+(test-ps-js if-exp-without-else-returns-null
+ (return (if x 1))
+ "return x ? 1 : null")
+
+(test-ps-js progn-expression-single-statement
+ (return (progn (* x y)))
+ "return x * y")
+
+(test-ps-js cond-expression1
+ (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
+ "function foo() {
+ return 1 < 2 ? (bar('foo'), 4 * 5) : null;
+}")
+
+(test-ps-js cond-expression2
+ (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar"))))
+ "function foo() {
+ return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null);
+}")
+
+(test-ps-js cond-expression-final-t-clause
+ (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo"))))
+ "function foo() {
+ return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo'));
+}")
+
+(test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
+ (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar"))))
+ "function foo() {
+ return 2 < 1 ? 5 : 'foo';
+}")
+
+(test-ps-js funcall-if-expression
+ (document.write
+ (if (= *linkornot* 1)
+ (ps-html ((:a :href "#"
+ :onclick (lisp (ps-inline (transport))))
+ img))
+ 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")