Improved formatting/whitespace removing code in tests. Added slot-value-setf test.
[clinton/parenscript.git] / t / test.lisp
1 (in-package :js-test)
2
3 ;; Testcases for parenscript
4
5 (defun normalize-whitespace (str)
6 (substitute #\Space #\Newline (substitute #\Space #\Tab str)))
7
8 (defun same-space-between-statements(code)
9 (cl-ppcre:regex-replace-all "\\s*;\\s*" code "; "))
10
11 (defun remove-duplicate-spaces (str)
12 (labels ((spacep (char) (and char (char= char #\Space)))
13 (rds (list)
14 (cond ((null list) nil)
15 ((and (spacep (first list)) (spacep (second list))) (rds (cons #\Space (cddr list))))
16 (t (cons (car list) (rds (cdr list)))))))
17 (coerce (rds (coerce str 'list)) 'string)))
18
19 (defun trim-spaces (str)
20 (string-trim '(#\Space) str))
21
22 (defun remove-spaces-near-brackets (str)
23 (reduce (lambda (str rex-pair) (cl-ppcre:regex-replace-all (first rex-pair) str (second rex-pair)))
24 (cons str '(("\\[ " "[") (" \\]" "]") ("\\( " "(") (" \\)" ")")))))
25
26 (defun normalize-js-code (str)
27 (remove-spaces-near-brackets
28 (trim-spaces
29 (remove-duplicate-spaces
30 (same-space-between-statements
31 (normalize-whitespace str))))))
32
33 (defmacro test-ps-js (testname parenscript javascript)
34 `(test ,testname ()
35 (setf js::*var-counter* 0)
36 ;; is-macro expands its argument again when reporting failures, so
37 ;; the reported temporary js-variables get wrong if we don't evalute first.
38 (let ((generated-code (js-to-string ',parenscript))
39 (js-code ,javascript))
40 (is (string= (normalize-js-code generated-code)
41 (normalize-js-code js-code))))))
42
43 (defun run-tests()
44 (format t "Running reference tests:~&")
45 (run! 'ref-tests)
46 (format t "Running other tests:~&")
47 (run! 'ps-tests))
48