cleaned reference
[clinton/parenscript.git] / test.lisp
index 02ec086..2ed47b4 100644 (file)
--- a/test.lisp
+++ b/test.lisp
@@ -4,10 +4,27 @@
 (defun trim-whitespace(str)
   (string-trim '(#\Space #\Tab #\Newline) str))
 
+(defun same-space-between-statements(code)
+  (cl-ppcre:regex-replace-all "\\s*;\\s*" code (concatenate 'string (list #\; #\Newline))))
+
+(defun no-indentation(code)
+  (cl-ppcre:regex-replace-all (cl-ppcre:create-scanner "^\\s*" :multi-line-mode t) code ""))
+
+(defun no-trailing-spaces(code)
+  (cl-ppcre:regex-replace-all (cl-ppcre:create-scanner "\\s*$" :multi-line-mode t) code ""))
+
+(defun normalize-js-code(str)
+  (trim-whitespace (no-indentation (no-trailing-spaces (same-space-between-statements str)))))
+
 (defmacro test-ps-js (testname parenscript javascript)
   `(test ,testname ()
-    (is (string= (trim-whitespace (js-to-string ',parenscript))
-                 (trim-whitespace ,javascript)))))
+    (setf js::*var-counter* 0)
+    ;; is-macro expands its argument again when reporting failures, so
+    ;; the reported temporary js-variables get wrong if we don't evalute first.
+    (let ((generated-code (js-to-string ',parenscript))
+          (js-code ,javascript))
+      (is (string= (normalize-js-code generated-code)
+                   (normalize-js-code js-code)))))) 
 
 (defun run-tests()
   (run! 'ref-tests))