8d3657d1e2b902c76a74af3633b829420bb27491
[clinton/parenscript.git] / t / test.lisp
1 (in-package :js-test)
2
3 ;; Testcases for parenscript
4
5 (defun trim-whitespace(str)
6 (string-trim '(#\Space #\Tab #\Newline) str))
7
8 (defun same-space-between-statements(code)
9 (cl-ppcre:regex-replace-all "\\s*;\\s*" code (concatenate 'string (list #\; #\Newline))))
10
11 (defun no-indentation(code)
12 (cl-ppcre:regex-replace-all (cl-ppcre:create-scanner "^\\s*" :multi-line-mode t) code ""))
13
14 (defun no-trailing-spaces(code)
15 (cl-ppcre:regex-replace-all (cl-ppcre:create-scanner "\\s*$" :multi-line-mode t) code ""))
16
17 (defun normalize-js-code(str)
18 (trim-whitespace (no-indentation (no-trailing-spaces (same-space-between-statements str)))))
19
20 (defmacro test-ps-js (testname parenscript javascript)
21 `(test ,testname ()
22 (setf js::*var-counter* 0)
23 ;; is-macro expands its argument again when reporting failures, so
24 ;; the reported temporary js-variables get wrong if we don't evalute first.
25 (let ((generated-code (js-to-string ',parenscript))
26 (js-code ,javascript))
27 (is (string= (normalize-js-code generated-code)
28 (normalize-js-code js-code))))))
29
30 (defun run-tests()
31 (format t "Running reference tests:~&")
32 (run! 'ref-tests)
33 (format t "Running other tests:~&")
34 (run! 'ps-tests))
35
36 ;;---------------------------------------------------------------------------
37 (eval-when (:compile-toplevel :load-toplevel :execute)
38 (def-suite ps-tests))
39 (in-suite ps-tests)
40
41 (test-ps-js plus-is-not-commutative
42 (setf x (+ "before" x "after"))
43 "x = 'before' + x + 'after'")
44
45 (test-ps-js plus-works-if-first
46 (setf x (+ x "middle" "after"))
47 "x += 'middle' + 'after'")
48
49 (test-ps-js setf-side-effects
50 (progn
51 (let ((x 10))
52 (defun side-effect()
53 (setf x 4)
54 (return 3))
55 (setf x (+ 2 (side-effect) x 5))))
56 "
57 var x = 10;
58 function sideEffect() {
59 x = 4;
60 return 3;
61 };
62 x = 2 + sideEffect() + x + 5;")
63 ;; Parenscript used to optimize to much:
64 ;; var x = 10;
65 ;; function sideEffect() {
66 ;; x = 4;
67 ;; return 3;
68 ;; };
69 ;; x += 2 + sideEffect() + 5;
70 ;;
71 ;; Which is 20, not 14
72
73
74 (test-ps-js dot-notation-bug
75 (.match (+ "" x) "foo")
76 "('' + x).match('foo')")
77
78 (test-ps-js method-call-op-form (.to-string (+ "" x)) "('' + x).toString()")
79 (test-ps-js method-call-number (.to-string 10) "(10).toString()")
80 (test-ps-js method-call-string (.to-string "hi") "'hi'.toString()")
81 (test-ps-js method-call-lit-object
82 (.to-string (create :to-string (lambda ()
83 (return "it works"))))
84 "({ toString : function () {
85 return 'it works';
86 } }).toString()")
87
88 (test-ps-js method-call-variable
89 (.to-string x)
90 "x.toString()")
91
92 (test-ps-js method-call-array
93 (.to-string (list 10 20))
94 "[10, 20].toString()")
95 (test-ps-js method-call-fn-call
96 (.to-string (foo))
97 "foo().toString()")
98 (test-ps-js method-call-lambda-fn
99 (.to-string (lambda () (alert 10)))
100 "(function () {alert(10);}).toString()")
101 (test-ps-js method-call-lambda-call
102 (.to-string ((lambda (x) (return x)) 10))
103 "(function (x) {return x;})(10).toString()")
104
105 (test no-whitespace-before-dot
106 (let* ((str (js:js* '(.to-string ((lambda (x) (return x)) 10))))
107 (dot-pos (position #\. str :test #'char=))
108 (char-before (elt str (1- dot-pos)))
109 (a-parenthesis #\)))
110 (is (char= char-before a-parenthesis))))
111
112 ;; A problem with long nested operator, when the statement spanned several rows
113 ;; the rows would not be joined together correctly.
114 (test-ps-js bug-dwim-join
115 (alert (html ((:div :id 777
116 :style (css-inline :border "1pxsssssssssss"
117 :font-size "x-small"
118 :height (* 2 200)
119 :width (* 2 300))))))
120 "alert
121 ('<div id=\"777\" style=\"'
122 + ('border:1pxsssssssssss;font-size:x-small;height:' + 2 * 200 + ';width:'
123 + 2 * 300)
124 + '\"></div>')") ;";This line should start with a plus character.
125
126
127 (test-ps-js simple-slot-value
128 (let ((foo (create :a 1)))
129 (alert (slot-value foo 'a)))
130 "{
131 var foo = { a : 1 };
132 alert(foo.a);
133 }")
134
135 (test-ps-js buggy-slot-value
136 (let ((foo (create :a 1))
137 (slot-name "a"))
138 (alert (slot-value foo slot-name)))
139 "{
140 var foo = { a : 1 };
141 var slotName = 'a';
142 alert(foo[slotName]);
143 }"); Last line was alert(foo.slotName) before bug-fix.
144
145 (test-ps-js buggy-slot-value-two
146 (slot-value foo (get-slot-name))
147 "foo[getSlotName()]")
148
149 (test-ps-js old-case-is-now-switch
150 ;; Switch was "case" before, but that was very non-lispish.
151 ;; For example, this code makes three messages and not one
152 ;; which may have been expected. This is because a switch
153 ;; statment must have a break statement for it to return
154 ;; after the alert. Otherwise it continues on the next
155 ;; clause.
156 (switch (aref blorg i)
157 (1 (alert "one"))
158 (2 (alert "two"))
159 (default (alert "default clause")))
160 "switch (blorg[i]) {
161 case 1: alert('one');
162 case 2: alert('two');
163 default: alert('default clause');
164 }")
165
166 (test-ps-js lisp-like-case
167 (case (aref blorg i)
168 (1 (alert "one"))
169 (2 (alert "two"))
170 (default (alert "default clause")))
171 "switch (blorg[i]) {
172 case 1:
173 alert('one');
174 break;
175 case 2:
176 alert('two');
177 break;
178 default: alert('default clause');
179 }")
180
181
182 (test-ps-js even-lispier-case
183 (case (aref blorg i)
184 ((1 2) (alert "Below three"))
185 (3 (alert "Three"))
186 (t (alert "Something else")))
187 "switch (blorg[i]) {
188 case 1: ;
189 case 2:
190 alert('Below three');
191 break;
192 case 3:
193 alert('Three');
194 break;
195 default: alert('Something else');
196 }")
197
198 (test-ps-js otherwise-case
199 (case (aref blorg i)
200 (1 (alert "one"))
201 (otherwise (alert "default clause")))
202 "switch (blorg[i]) {
203 case 1:
204 alert('one');
205 break;
206 default: alert('default clause');
207 }")
208
209 (test escape-sequences-in-string
210 (let ((escapes `((#\\ . #\\)
211 (#\b . #\Backspace)
212 (#\f . ,(code-char 12))
213 ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
214 (#\n . #\Newline)
215 (#\r . #\Return)
216 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
217 (#\t . #\Tab)
218 ("u001F" . ,(code-char #x001f));; character below 32
219 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
220 ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
221 (loop for (js-escape . lisp-char) in escapes
222 for generated = (js-to-string `(let ((x , (format nil "hello~ahi" lisp-char)))))
223 for wanted = (format nil "{
224 var x = 'hello\\~ahi';
225 }" js-escape)
226 do (is (string= generated wanted)))))