Verified Commit 9d2e2aa4 authored by Vladimir Sedach's avatar Vladimir Sedach
Browse files

Tests: Refer to fiveam symbols by package, run-tests runs top-level suite

parent 47be1794
......@@ -5,7 +5,7 @@
#+sbcl (declaim (sb-ext:muffle-conditions sb-ext:compiler-note))
(in-suite parenscript.tests::eval-tests)
(fiveam:in-suite parenscript.tests::eval-tests)
(test-js-eval number
3
......
......@@ -3,7 +3,7 @@
(in-package #:parenscript.tests)
(named-readtables:in-readtable :parenscript)
(in-suite output-tests)
(fiveam:in-suite output-tests)
(test-ps-js statements-and-expressions-1
(+ i (if 1 2 3))
......@@ -901,12 +901,9 @@ return element.innerHTML = ['<TEXTAREA', disabled || !authorized ? [' DISABLED=\
(funcall (getprop (funcall (lambda (x) x) 10) 'to-string))
"(function (x) { return x; })(10).toString();")
(test no-whitespace-before-dot
(let* ((str (ps* '((@ ((lambda (x) x) 10) to-string))))
(dot-pos (position #\. str :test #'char=))
(char-before (elt str (1- dot-pos)))
(a-parenthesis #\)))
(is (char= char-before a-parenthesis))))
(fiveam:test no-whitespace-before-dot
(let ((str (ps* '((@ ((lambda (x) x) 10) to-string)))))
(fiveam:is (char= #\) (elt str (1- (position #\. str)))))))
(test-ps-js simple-getprop
(let ((foo (create a 1)))
......@@ -984,22 +981,25 @@ return element.innerHTML = ['<TEXTAREA', disabled || !authorized ? [' DISABLED=\
default: alert('default clause');
};")
(test escape-sequences-in-string
(let ((escapes `((#\\ . #\\)
(#\b . #\Backspace)
(#\f . ,(code-char 12))
("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
(#\n . #\Newline)
(#\r . #\Return)
(#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
(#\t . #\Tab)
("u001F" . ,(code-char #x001f));; character below 32
("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
(fiveam:test escape-sequences-in-string
(let ((escapes
`((#\\ . #\\)
(#\b . #\Backspace)
(#\f . ,(code-char 12))
("u000B" . ,(code-char #x000b)) ; vertical tab
(#\n . #\Newline)
(#\r . #\Return)
(#\' . #\')
(#\t . #\Tab)
("u001F" . ,(code-char #x001f)) ; character below 32
("u0080" . ,(code-char 128)) ; character over 127
("uABCD" . ,(code-char #xabcd))))) ; well above ASCII
;; Note that double quote need not be quoted because Parenscript
;; strings are single quoted
(loop for (js-escape . lisp-char) in escapes
for generated = (ps-doc* (format nil "hello~ahi" lisp-char))
for wanted = (format nil "'hello\\~ahi';" js-escape)
do (is (string= (normalize-js-output generated) wanted)))))
do (fiveam:is (string= generated wanted)))))
(test-ps-js getprop-setf
(setf (getprop x 'y) (+ (+ a 3) 4))
......@@ -1013,11 +1013,15 @@ return element.innerHTML = ['<TEXTAREA', disabled || !authorized ? [' DISABLED=\
(getprop (if (not zoo) foo bar) 'x)
"(!zoo ? foo : bar).x;")
(test script-star-eval1
(is (string= "x = 1; y = 2;" (normalize-js-output (ps* '(setf x 1) '(setf y 2))))))
(fiveam:test script-star-eval1
(fiveam:is (string=
(normalize-js-output (ps* '(setf x 1) '(setf y 2)))
"x = 1; y = 2;")))
(test script-star-eval2
(is (string= "x = 1;" (normalize-js-output (ps* '(setf x 1))))))
(fiveam:test script-star-eval2
(fiveam:is (string=
(normalize-js-output (ps* '(setf x 1)))
"x = 1;")))
(test-ps-js list-with-single-nil
(array nil)
......@@ -1412,24 +1416,29 @@ __setf_someThing('foo', 1, 2);")
(- 1)
"-1;")
(test macro-environment1
(is (string= (normalize-js-output (let* ((macroname (gensym)))
(ps* `(defmacro ,macroname (x) `(+ ,x 123))
`(defun test1 ()
(macrolet ((,macroname (x) `(aref data ,x)))
(when (,macroname x)
(setf (,macroname x) 123)))))))
(normalize-js-output
(fiveam:test macro-environment1
(fiveam:is
(string=
(normalize-js-output
(let* ((macroname (gensym)))
(ps* `(defmacro ,macroname (x) `(+ ,x 123))
`(defun test1 ()
(macrolet ((,macroname (x) `(aref data ,x)))
(when (,macroname x)
(setf (,macroname x) 123)))))))
(normalize-js-output
"function test1() {
return data[x] ? (data[x] = 123) : null;
};"))))
(test macro-environment2
(is (string= (normalize-js-output (let ((outer-lexical-variable 1))
(defpsmacro macro-environment2-macro (x)
`(+ ,outer-lexical-variable ,x))
(ps* '(macro-environment2-macro 2))))
(normalize-js-output "1 + 2;"))))
(fiveam:test macro-environment2
(fiveam:is
(string=
(let ((outer-lexical-variable 1))
(defpsmacro macro-environment2-macro (x)
`(+ ,outer-lexical-variable ,x))
(ps* '(macro-environment2-macro 2)))
"1 + 2;")))
(test-ps-js ampersand-whole-1
(macrolet ((foo (&whole foo bar baz)
......@@ -1468,6 +1477,11 @@ __setf_someThing('foo', 1, 2);")
(foo tst-sym-macro1))
"foo(2);")
(test-ps-js define-symbol-macro2
(progn (define-symbol-macro tst-sym-macro2 3)
(list tst-sym-macro2))
"[3];")
(test-ps-js expression-progn
(1+ (progn (foo) (if x 1 2)))
"(foo(), x ? 1 : 2) + 1;")
......@@ -1769,34 +1783,38 @@ return foo(1);
'(1 [])
"[1, []];")
(test ps-lisp-expands-in-lexical-environment
(is (string= "5;" (let ((x 5))
(ps (lisp x))))))
(fiveam:test ps-lisp-expands-in-lexical-environment
(fiveam:is (string= (let ((x 5)) (ps (lisp x)))
"5;")))
(test ps-lisp-expands-in-lexical-environment1
(is (string= "1 + 5;" (let ((x 5))
(ps (+ 1 (lisp x)))))))
(fiveam:test ps-lisp-expands-in-lexical-environment1
(fiveam:is (string= (let ((x 5)) (ps (+ 1 (lisp x))))
"1 + 5;")))
(test ps-lisp-expands-in-lexical-environment2
(is (string= "1 + 2 + 3;" (let ((x 2))
(ps (+ 1 (lisp x) 3))))))
(fiveam:test ps-lisp-expands-in-lexical-environment2
(fiveam:is (string= (let ((x 2)) (ps (+ 1 (lisp x) 3)))
"1 + 2 + 3;")))
(test ps*-lisp-expands-in-null-lexical-environment
(signals unbound-variable (let ((x 5))
(declare (ignore x))
(ps* '(lisp x)))))
(fiveam:test ps*-lisp-expands-in-null-lexical-environment
(fiveam:signals unbound-variable
(let ((x 5))
(declare (ignore x))
(ps* '(lisp x)))))
(test ps*-lisp-expands-in-dynamic-environment
(is (string= "1 + 2;"
(fiveam:test ps*-lisp-expands-in-dynamic-environment
(fiveam:is (string=
(let ((foo 2))
(declare (special foo))
(ps* '(+ 1 (lisp (locally (declare (special foo)) foo))))))))
(ps* '(+ 1 (lisp (locally (declare (special foo))
foo)))))
"1 + 2;")))
(test ps-lisp-dynamic-environment
(is (string= "1 + 2;"
(fiveam:test ps-lisp-dynamic-environment
(fiveam:is (string=
(let ((foo 2))
(declare (special foo))
(ps (+ 1 (lisp foo)))))))
(ps (+ 1 (lisp foo))))
"1 + 2;")))
(test-ps-js nested-if-expressions1
(defun foo ()
......@@ -2191,37 +2209,41 @@ return ++x1;
(defvar *lisp-output* nil)
(test eval-when-lisp-side ()
(setf *lisp-output* 'original-value)
(let ((js-output (normalize-js-output
(ps-doc* `(eval-when (:compile-toplevel)
(setf *lisp-output* 'it-works))))))
(is (eql 'it-works *lisp-output*))
(is (string= "" js-output))))
(fiveam:test eval-when-lisp-side ()
(setf *lisp-output* 'original-value)
(let ((js-output
(normalize-js-output
(ps-doc* `(eval-when (:compile-toplevel)
(setf *lisp-output* 'it-works))))))
(fiveam:is (eql 'it-works *lisp-output*))
(fiveam:is (string= "" js-output))))
(defpsmacro my-in-package (package-name)
`(eval-when (:compile-toplevel)
(setf *lisp-output* ,package-name)))
(test eval-when-macro-expansion ()
(setf *lisp-output* 'original-value)
(let ((js-output (normalize-js-output
(ps-doc* `(progn
(my-in-package :cl-user)
3)))))
(declare (ignore js-output))
(is (eql :cl-user *lisp-output*))))
(test eval-when-macrolet-expansion ()
(setf *lisp-output* 'original-value)
(let ((js-output (normalize-js-output
(ps-doc* `(macrolet ((my-in-package2 (package-name)
(fiveam:test eval-when-macro-expansion ()
(setf *lisp-output* 'original-value)
(let ((js-output
(normalize-js-output
(ps-doc* `(progn
(my-in-package :cl-user)
3)))))
(declare (ignore js-output))
(fiveam:is (eql :cl-user *lisp-output*))))
(fiveam:test eval-when-macrolet-expansion ()
(setf *lisp-output* 'original-value)
(let ((js-output
(normalize-js-output
(ps-doc*
`(macrolet ((my-in-package2 (package-name)
`(eval-when (:compile-toplevel)
(setf *lisp-output* ,package-name))))
(my-in-package2 :cl-user)
3)))))
(declare (ignore js-output))
(is (eql :cl-user *lisp-output*))))
(setf *lisp-output* ,package-name))))
(my-in-package2 :cl-user)
3)))))
(declare (ignore js-output))
(fiveam:is (eql :cl-user *lisp-output*))))
(test-ps-js getprop-keyword
(getprop foo :bar)
......@@ -2865,18 +2887,28 @@ return testSymbolMacro1_1();
})();
bar(1);")
(test compile-stream-nulls
(is (string=
""
(with-input-from-string (s "
(defmacro macro-null-toplevel ()
nil)
(macro-null-toplevel)")
(ps-compile-stream s)))))
(test compile-stream1
(is (string=
"(function () {
(fiveam:test compile-stream-nulls
(fiveam:is
(string=
(with-input-from-string (s "
(defmacro macro-null-toplevel ()
nil)
(macro-null-toplevel)")
(ps-compile-stream s))
"")))
(fiveam:test compile-stream1
(fiveam:is
(string=
(with-input-from-string (s "
(define-symbol-macro test-symbol-macro1 1)
(flet ((test-symbol-macro1 () 2))
(foo test-symbol-macro1)
(test-symbol-macro1))
(bar test-symbol-macro1)")
(ps::with-blank-compilation-environment
(ps-compile-stream s)))
"(function () {
var testSymbolMacro1_1 = function () {
return 2;
};
......@@ -2884,14 +2916,7 @@ bar(1);")
return testSymbolMacro1_1();
})();
bar(1);
"
(with-input-from-string (s "
(define-symbol-macro test-symbol-macro1 1)
(flet ((test-symbol-macro1 () 2))
(foo test-symbol-macro1)
(test-symbol-macro1))
(bar test-symbol-macro1)")
(ps::with-blank-compilation-environment (ps-compile-stream s))))))
")))
(test-ps-js equality-nary1
(let ((x 10) (y 10) (z 10))
......
......@@ -2,7 +2,7 @@
(in-package #:parenscript.tests)
(in-suite package-system-tests)
(fiveam:in-suite package-system-tests)
(test-ps-js operator-packages1
(#:new)
......@@ -10,7 +10,8 @@
(defpackage #:parenscript.tests.my-library
(:use #:parenscript))
(setf (ps-package-prefix '#:parenscript.tests.my-library) "my_library_")
(setf (ps-package-prefix '#:parenscript.tests.my-library)
"my_library_")
(test-ps-js lib-function1
(defun parenscript.tests.my-library::library-function (x y)
......@@ -33,7 +34,8 @@ foo.my_library_foo;")
(symbol-name symbol)))))))
(defpackage #:parenscript.tests.obfuscate-me)
(obfuscate-package '#:parenscript.tests.obfuscate-me #'symbol-obfuscator)
(obfuscate-package '#:parenscript.tests.obfuscate-me
#'symbol-obfuscator)
(test-ps-js obfuscation1
(defun parenscript.tests.obfuscate-me::libfun2 (a b parenscript.tests.obfuscate-me::foo)
......@@ -60,8 +62,10 @@ foo.my_library_foo;")
(setf (ps-package-prefix '#:parenscript.tests.pststpkg) "prefix_")
(test namespace1 ()
(is (string= "prefix_foo;" (normalize-js-output (ps* 'parenscript.tests.pststpkg::foo)))))
(fiveam:test namespace1 ()
(fiveam:is (string=
(ps* 'parenscript.tests.pststpkg::foo)
"prefix_foo;")))
(cl:in-package #:parenscript.tests.pststpkg)
......@@ -88,16 +92,10 @@ return !foo1 && foo1.prefix_bar + prefix_someOtherVar;
(cl:in-package #:parenscript.tests)
(test compile-stream-in-package
(is (string=
"function __FOO___ygvo(a, __FOO___c, my_library_d) {
return a * mjcgvo3(__FOO___c, a) * my_library_libraryFunction(my_library_d, __FOO___c);
};
function interfaceFunction(prefix_baz) {
return prefix_baz + gpp;
};
"
(with-input-from-string (s "
(fiveam:test compile-stream-in-package
(fiveam:is
(string=
(with-input-from-string (s "
(defun parenscript.tests.obfuscate-and-prefix::xfun (a parenscript.tests.obfuscate-and-prefix::b parenscript.tests.my-library::d)
(* a
(parenscript.tests.obfuscate-me::libfun2 parenscript.tests.obfuscate-and-prefix::b a)
......@@ -108,4 +106,11 @@ function interfaceFunction(prefix_baz) {
(defun parenscript.tests:interface-function (baz)
(+ baz parenscript.tests.obfuscate-me::foo))
")
(ps-compile-stream s)))))
(ps-compile-stream s))
"function __FOO___ygvo(a, __FOO___c, my_library_d) {
return a * mjcgvo3(__FOO___c, a) * my_library_libraryFunction(my_library_d, __FOO___c);
};
function interfaceFunction(prefix_baz) {
return prefix_baz + gpp;
};
")))
......@@ -2,7 +2,7 @@
(named-readtables:in-readtable :parenscript)
(defpackage #:parenscript.tests
(:use #:cl #:parenscript #:fiveam)
(:use #:cl #:parenscript)
(:export
#:parenscript-tests
#:run-tests
......@@ -12,4 +12,4 @@
#:jsarray))
(defpackage #:parenscript.eval-tests
(:use #:cl #:fiveam #:parenscript #:parenscript.tests))
(:use #:cl #:parenscript #:parenscript.tests))
......@@ -7,11 +7,12 @@
(defmacro test-ps-js (testname parenscript javascript
&key (js-target-version *js-target-version*))
`(test ,testname ()
(is (string= (normalize-js-output ,javascript)
(normalize-js-output
(let ((*js-target-version* ,js-target-version))
(ps-doc* ',parenscript)))))))
`(fiveam:test ,testname ()
(fiveam:is
(string= (normalize-js-output ,javascript)
(normalize-js-output
(let ((*js-target-version* ,js-target-version))
(ps-doc* ',parenscript)))))))
(defun jsarray (contents)
(cl-js:js-array
......@@ -24,31 +25,26 @@
:adjustable t)))
(defmacro %test-js-eval (testname parenscript test-statement)
`(test ,testname ()
`(fiveam:test ,testname ()
(cl-js:with-js-env ()
(let ((js-result (cl-js:run-js (ps-doc* ',parenscript))))
,test-statement))))
(defmacro test-js-eval (testname parenscript result)
`(%test-js-eval ,testname ,parenscript
(is (equalp js-result
,(if (atom result)
result
`(jsarray ,result))))))
(fiveam:is (equalp js-result
,(if (atom result)
result
`(jsarray ,result))))))
(defmacro test-js-eval-epsilon (testname parenscript result)
`(%test-js-eval ,testname ,parenscript
(is (< (abs (- js-result ,result)) 0.0001))))
(def-suite parenscript-tests)
(def-suite output-tests :in parenscript-tests)
(def-suite package-system-tests :in parenscript-tests)
(def-suite eval-tests :in parenscript-tests)
(defun run-tests()
(format t "Running output tests:~&")
(run! 'output-tests)
(format t "Running package system tests:~&")
(run! 'package-system-tests)
(format t "Running CL-JavaScript eval tests:~&")
(run! 'eval-tests))
(fiveam:is (< (abs (- js-result ,result)) 0.0001))))
(fiveam:def-suite parenscript-tests)
(fiveam:def-suite output-tests :in parenscript-tests)
(fiveam:def-suite package-system-tests :in parenscript-tests)
(fiveam:def-suite eval-tests :in parenscript-tests)
(defun run-tests ()
(fiveam:run! 'parenscript-tests))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment