Modified ParenScript tests to reflect the big compiler refactoring.
[clinton/parenscript.git] / t / ps-tests.lisp
1 (in-package :ps-test)
2 ;; Other tests not in the reference
3
4 (eval-when (:compile-toplevel :load-toplevel :execute)
5 (def-suite ps-tests))
6 (in-suite ps-tests)
7
8 (test-ps-js plus-is-not-commutative
9 (setf x (+ "before" x "after"))
10 "x = 'before' + x + 'after';")
11
12 (test-ps-js plus-works-if-first
13 (setf x (+ x "middle" "after"))
14 "x += 'middle' + 'after';")
15
16 (test-ps-js setf-side-effects
17 (progn
18 (let ((x 10))
19 (defun side-effect()
20 (setf x 4)
21 (return 3))
22 (setf x (+ 2 (side-effect) x 5))))
23 "
24 var x = 10;
25 function sideEffect() {
26 x = 4;
27 return 3;
28 };
29 x = 2 + sideEffect() + x + 5;")
30 ;; Parenscript used to optimize to much:
31 ;; var x = 10;
32 ;; function sideEffect() {
33 ;; x = 4;
34 ;; return 3;
35 ;; };
36 ;; x += 2 + sideEffect() + 5;
37 ;;
38 ;; Which is 20, not 14
39
40
41 (test-ps-js dot-notation-bug
42 (.match (+ "" x) "foo")
43 "('' + x).match('foo')")
44
45 (test-ps-js method-call-op-form (.to-string (+ "" x)) "('' + x).toString()")
46 (test-ps-js method-call-number (.to-string 10) "( 10 ).toString()")
47 (test-ps-js method-call-string (.to-string "hi") "'hi'.toString()")
48 (test-ps-js method-call-lit-object
49 (.to-string (create :to-string (lambda ()
50 (return "it works"))))
51 "( { toString : function () { return 'it works'; } } ).toString()")
52
53 (test-ps-js method-call-variable
54 (.to-string x)
55 "x.toString()")
56
57 (test-ps-js method-call-array
58 (.to-string (list 10 20))
59 "[ 10, 20 ].toString()")
60 (test-ps-js method-call-fn-call
61 (.to-string (foo))
62 "foo().toString()")
63 (test-ps-js method-call-lambda-fn
64 (.to-string (lambda () (alert 10)))
65 "( function () { alert(10); } ).toString()")
66 (test-ps-js method-call-lambda-call
67 (.to-string ((lambda (x) (return x)) 10))
68 "(function (x) { return x; }) (10).toString()")
69
70 (test no-whitespace-before-dot
71 (let* ((str (compile-script '(.to-string ((lambda (x) (return x)) 10))))
72 (dot-pos (position #\. str :test #'char=))
73 (char-before (elt str (1- dot-pos)))
74 (a-parenthesis #\)))
75 (is (char= char-before a-parenthesis))))
76
77 ;; A problem with long nested operator, when the statement spanned several rows
78 ;; the rows would not be joined together correctly.
79 (test-ps-js bug-dwim-join
80 (alert (html ((:div :id 777
81 :style (css-inline :border "1pxsssssssssss"
82 :font-size "x-small"
83 :height (* 2 200)
84 :width (* 2 300))))))
85 "alert
86 ('<div id=\"777\" style=\"'
87 + ('border:1pxsssssssssss;font-size:x-small;height:' + 2 * 200 + ';width:'
88 + 2 * 300)
89 + '\"></div>')") ;";This line should start with a plus character.
90
91
92 (test-ps-js simple-slot-value
93 (let ((foo (create :a 1)))
94 (alert (slot-value foo 'a)))
95 "var foo = { a : 1 };
96 alert(foo.a);")
97
98 (test-ps-js buggy-slot-value
99 (let ((foo (create :a 1))
100 (slot-name "a"))
101 (alert (slot-value foo slot-name)))
102 " var foo = { a : 1 };
103 var slotName = 'a';
104 alert(foo[slotName]);
105 "); Last line was alert(foo.slotName) before bug-fix.
106
107 (test-ps-js buggy-slot-value-two
108 (slot-value foo (get-slot-name))
109 "foo[getSlotName()]")
110
111 (test-ps-js old-case-is-now-switch
112 ;; Switch was "case" before, but that was very non-lispish.
113 ;; For example, this code makes three messages and not one
114 ;; which may have been expected. This is because a switch
115 ;; statment must have a break statement for it to return
116 ;; after the alert. Otherwise it continues on the next
117 ;; clause.
118 (switch (aref blorg i)
119 (1 (alert "one"))
120 (2 (alert "two"))
121 (default (alert "default clause")))
122 "switch (blorg[i]) {
123 case 1: alert('one');
124 case 2: alert('two');
125 default: alert('default clause');
126 }")
127
128 (test-ps-js lisp-like-case
129 (case (aref blorg i)
130 (1 (alert "one"))
131 (2 (alert "two"))
132 (default (alert "default clause")))
133 "switch (blorg[i]) {
134 case 1:
135 alert('one');
136 break;
137 case 2:
138 alert('two');
139 break;
140 default: alert('default clause');
141 }")
142
143
144 (test-ps-js even-lispier-case
145 (case (aref blorg i)
146 ((1 2) (alert "Below three"))
147 (3 (alert "Three"))
148 (t (alert "Something else")))
149 "switch (blorg[i]) {
150 case 1: ;
151 case 2:
152 alert('Below three');
153 break;
154 case 3:
155 alert('Three');
156 break;
157 default: alert('Something else');
158 }")
159
160 (test-ps-js otherwise-case
161 (case (aref blorg i)
162 (1 (alert "one"))
163 (otherwise (alert "default clause")))
164 "switch (blorg[i]) {
165 case 1:
166 alert('one');
167 break;
168 default: alert('default clause');
169 }")
170
171 (test escape-sequences-in-string
172 (let ((escapes `((#\\ . #\\)
173 (#\b . #\Backspace)
174 (#\f . ,(code-char 12))
175 ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
176 (#\n . #\Newline)
177 (#\r . #\Return)
178 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
179 (#\t . #\Tab)
180 ("u001F" . ,(code-char #x001f));; character below 32
181 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
182 ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
183 (loop for (js-escape . lisp-char) in escapes
184 for generated = (compile-script `(let ((x ,(format nil "hello~ahi" lisp-char)))))
185 for wanted = (format nil "var x = 'hello\\~ahi';" js-escape)
186 do (is (string= (normalize-js-code generated) wanted)))))
187
188 (test-ps-js complicated-symbol-name1
189 grid-rows[foo].bar
190 "gridRows[foo].bar")
191
192 (test-ps-js complicated-symbol-name2
193 *grid-rows*[foo].bar
194 "GRIDROWS[foo].bar")
195
196 (test-ps-js slot-value-setf
197 (setf (slot-value x 'y) (+ (+ a 3) 4))
198 "x.y = (a + 3) + 4;")
199
200 (test-ps-js slot-value-conditional1
201 (slot-value (if zoo foo bar) 'x)
202 "(zoo ? foo : bar).x")
203
204 (test-ps-js slot-value-conditional2
205 (slot-value (if (not zoo) foo bar) 'x)
206 "(!zoo ? foo : bar).x")
207
208 (test script-star-eval1
209 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
210
211 (test script-star-eval2
212 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
213
214 (test-ps-js slot-value-null1
215 (slot-value foo nil)
216 "foo")
217
218 (test-ps-js slot-value-null2
219 (slot-value foo 'nil)
220 "foo")
221
222 (test-ps-js unquoted-nil
223 nil
224 "null")
225
226 (test-ps-js list-with-single-nil
227 (array 'nil)
228 "[null]")
229
230 (test-ps-js quoted-nil
231 'nil
232 "null")
233
234 (test defsetf1
235 (ps (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval)))
236 (is (string= "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);"
237 (normalize-js-code (let ((ps:*ps-gensym-counter* 0))
238 (ps (setf (baz 1 2) 3)))))))
239
240 (test defsetf-short
241 (ps (defsetf baz set-baz "blah"))
242 (is (string= "setBaz(1, 2, 3, 'foo');" (normalize-js-code (ps (setf (baz 1 2 3) "foo"))))))
243
244 (test defun-setf1
245 (is (and (string= (normalize-js-code (ps:ps (defun (setf some-thing) (new-val i1 i2)
246 (setf (aref *some-thing* i1 i2) new-val))))
247 "null; function __setf_someThing(newVal, i1, i2) { SOMETHING[i1][i2] = newVal; };")
248 (string= (let ((ps:*ps-gensym-counter* 0)) (normalize-js-code (ps:ps (setf (some-thing 1 2) "foo"))))
249 "var _js2 = 1; var _js3 = 2; var _js1 = 'foo'; __setf_someThing(_js1, _js2, _js3);"))))
250
251 (test-ps-js defun-optional1
252 (defun test-opt (&optional x) (return (if x "yes" "no")))
253 "function testOpt(x) {
254 x = undefined === x && null || x;
255 return x ? 'yes' : 'no';
256 }")
257
258 (test-ps-js return-nothing
259 (return)
260 "return null")
261
262 (test-ps-js set-timeout
263 (do-set-timeout (10) (alert "foo"))
264 "setTimeout (function () { alert('foo'); }, 10)")
265
266 (test-ps-js operator-precedence
267 (* 3 (+ 4 5) 6)
268 "3 * (4 + 5) * 6")
269
270 (test-ps-js incf1
271 (incf foo bar)
272 "foo += bar")
273
274 (test-ps-js decf1
275 (decf foo bar)
276 "foo -= bar")
277
278 (test-ps-js setf-conditional
279 (setf foo (if x 1 2))
280 "foo = x ? 1 : 2;")
281
282 (test-ps-js obj-literal-numbers
283 (create 1 "foo")
284 "{ 1 : 'foo' }")
285
286 (test-ps-js obj-literal-strings
287 (create "foo" 2)
288 "{ 'foo' : 2 }")