Commit | Line | Data |
---|---|---|
171bbab3 | 1 | (in-package :ps-test) |
13b8268e VS |
2 | |
3 | ;;; Hand-written unit tests | |
e2932347 LC |
4 | |
5 | (eval-when (:compile-toplevel :load-toplevel :execute) | |
6 | (def-suite ps-tests)) | |
7 | (in-suite ps-tests) | |
8 | ||
9 | (test-ps-js plus-is-not-commutative | |
10 | (setf x (+ "before" x "after")) | |
72332f2a | 11 | "x = 'before' + x + 'after';") |
e2932347 LC |
12 | |
13 | (test-ps-js plus-works-if-first | |
14 | (setf x (+ x "middle" "after")) | |
72332f2a | 15 | "x += 'middle' + 'after';") |
e2932347 LC |
16 | |
17 | (test-ps-js setf-side-effects | |
58c4ef4f VS |
18 | (progn |
19 | (let* ((x 10)) | |
20 | (defun side-effect() | |
21 | (setf x 4) | |
22 | (return 3)) | |
23 | (setf x (+ 2 (side-effect) x 5)))) | |
24 | "var x = 10; | |
e2932347 LC |
25 | function sideEffect() { |
26 | x = 4; | |
27 | return 3; | |
28 | }; | |
29 | x = 2 + sideEffect() + x + 5;") | |
58c4ef4f | 30 | ;; Parenscript used to optimize too much: |
e2932347 LC |
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()") | |
cf460f93 | 46 | (test-ps-js method-call-number (.to-string 10) "( 10 ).toString()") |
e2932347 LC |
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")))) | |
cf460f93 | 51 | "( { toString : function () { return 'it works'; } } ).toString()") |
e2932347 LC |
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)) | |
cf460f93 | 59 | "[ 10, 20 ].toString()") |
e2932347 LC |
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))) | |
cf460f93 | 65 | "( function () { alert(10); } ).toString()") |
e2932347 LC |
66 | (test-ps-js method-call-lambda-call |
67 | (.to-string ((lambda (x) (return x)) 10)) | |
b44afd8f | 68 | "(function (x) { return x; })(10).toString()") |
e2932347 LC |
69 | |
70 | (test no-whitespace-before-dot | |
905f534e | 71 | (let* ((str (compile-script '(.to-string ((lambda (x) (return x)) 10)))) |
e2932347 LC |
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 | |
8bb28ead VS |
80 | (alert (ps-html ((:div :id 777 |
81 | :style (css-inline :border "1pxsssssssssss" | |
82 | :font-size "x-small" | |
83 | :height (* 2 200) | |
84 | :width (* 2 300)))))) | |
b44afd8f | 85 | "alert('<div id=\"777\" style=\"' |
e2932347 LC |
86 | + ('border:1pxsssssssssss;font-size:x-small;height:' + 2 * 200 + ';width:' |
87 | + 2 * 300) | |
88 | + '\"></div>')") ;";This line should start with a plus character. | |
89 | ||
90 | ||
91 | (test-ps-js simple-slot-value | |
58c4ef4f | 92 | (let* ((foo (create :a 1))) |
e2932347 | 93 | (alert (slot-value foo 'a))) |
07afea38 VS |
94 | "var foo = { a : 1 }; |
95 | alert(foo.a);") | |
e2932347 LC |
96 | |
97 | (test-ps-js buggy-slot-value | |
58c4ef4f | 98 | (let* ((foo (create :a 1)) |
e2932347 LC |
99 | (slot-name "a")) |
100 | (alert (slot-value foo slot-name))) | |
07afea38 | 101 | " var foo = { a : 1 }; |
e2932347 LC |
102 | var slotName = 'a'; |
103 | alert(foo[slotName]); | |
07afea38 | 104 | "); Last line was alert(foo.slotName) before bug-fix. |
e2932347 LC |
105 | |
106 | (test-ps-js buggy-slot-value-two | |
107 | (slot-value foo (get-slot-name)) | |
108 | "foo[getSlotName()]") | |
109 | ||
110 | (test-ps-js old-case-is-now-switch | |
111 | ;; Switch was "case" before, but that was very non-lispish. | |
112 | ;; For example, this code makes three messages and not one | |
113 | ;; which may have been expected. This is because a switch | |
114 | ;; statment must have a break statement for it to return | |
115 | ;; after the alert. Otherwise it continues on the next | |
116 | ;; clause. | |
117 | (switch (aref blorg i) | |
118 | (1 (alert "one")) | |
119 | (2 (alert "two")) | |
120 | (default (alert "default clause"))) | |
121 | "switch (blorg[i]) { | |
122 | case 1: alert('one'); | |
123 | case 2: alert('two'); | |
124 | default: alert('default clause'); | |
125 | }") | |
126 | ||
127 | (test-ps-js lisp-like-case | |
128 | (case (aref blorg i) | |
129 | (1 (alert "one")) | |
130 | (2 (alert "two")) | |
131 | (default (alert "default clause"))) | |
132 | "switch (blorg[i]) { | |
133 | case 1: | |
134 | alert('one'); | |
135 | break; | |
136 | case 2: | |
137 | alert('two'); | |
138 | break; | |
139 | default: alert('default clause'); | |
140 | }") | |
141 | ||
142 | ||
143 | (test-ps-js even-lispier-case | |
144 | (case (aref blorg i) | |
145 | ((1 2) (alert "Below three")) | |
146 | (3 (alert "Three")) | |
147 | (t (alert "Something else"))) | |
148 | "switch (blorg[i]) { | |
b44afd8f | 149 | case 1: |
e2932347 LC |
150 | case 2: |
151 | alert('Below three'); | |
152 | break; | |
153 | case 3: | |
154 | alert('Three'); | |
155 | break; | |
156 | default: alert('Something else'); | |
157 | }") | |
158 | ||
159 | (test-ps-js otherwise-case | |
160 | (case (aref blorg i) | |
161 | (1 (alert "one")) | |
e0f0d152 | 162 | (otherwise (alert "default clause"))) |
e2932347 LC |
163 | "switch (blorg[i]) { |
164 | case 1: | |
165 | alert('one'); | |
166 | break; | |
167 | default: alert('default clause'); | |
168 | }") | |
169 | ||
170 | (test escape-sequences-in-string | |
905f534e | 171 | (let ((escapes `((#\\ . #\\) |
e2932347 LC |
172 | (#\b . #\Backspace) |
173 | (#\f . ,(code-char 12)) | |
174 | ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with | |
175 | (#\n . #\Newline) | |
176 | (#\r . #\Return) | |
177 | (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted | |
178 | (#\t . #\Tab) | |
179 | ("u001F" . ,(code-char #x001f));; character below 32 | |
180 | ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure. | |
181 | ("uABCD" . ,(code-char #xabcd)))));; Really above ascii. | |
182 | (loop for (js-escape . lisp-char) in escapes | |
58c4ef4f | 183 | for generated = (compile-script `(let* ((x ,(format nil "hello~ahi" lisp-char))))) |
07afea38 VS |
184 | for wanted = (format nil "var x = 'hello\\~ahi';" js-escape) |
185 | do (is (string= (normalize-js-code generated) wanted))))) | |
9da682ca | 186 | |
e0f0d152 VS |
187 | (test-ps-js complicated-symbol-name1 |
188 | grid-rows[foo].bar | |
189 | "gridRows[foo].bar") | |
190 | ||
191 | (test-ps-js complicated-symbol-name2 | |
192 | *grid-rows*[foo].bar | |
193 | "GRIDROWS[foo].bar") | |
cf460f93 VS |
194 | |
195 | (test-ps-js slot-value-setf | |
196 | (setf (slot-value x 'y) (+ (+ a 3) 4)) | |
72332f2a | 197 | "x.y = (a + 3) + 4;") |
eb0befe2 VS |
198 | |
199 | (test-ps-js slot-value-conditional1 | |
200 | (slot-value (if zoo foo bar) 'x) | |
201 | "(zoo ? foo : bar).x") | |
202 | ||
203 | (test-ps-js slot-value-conditional2 | |
204 | (slot-value (if (not zoo) foo bar) 'x) | |
205 | "(!zoo ? foo : bar).x") | |
206 | ||
245d0fbd | 207 | (test script-star-eval1 |
07afea38 | 208 | (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2)))))) |
245d0fbd VS |
209 | |
210 | (test script-star-eval2 | |
07afea38 | 211 | (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1)))))) |
62b4a867 VS |
212 | |
213 | (test-ps-js slot-value-null1 | |
214 | (slot-value foo nil) | |
215 | "foo") | |
216 | ||
217 | (test-ps-js slot-value-null2 | |
218 | (slot-value foo 'nil) | |
219 | "foo") | |
220 | ||
46f794a4 RD |
221 | (test-ps-js unquoted-nil |
222 | nil | |
223 | "null") | |
224 | ||
225 | (test-ps-js list-with-single-nil | |
226 | (array 'nil) | |
227 | "[null]") | |
228 | ||
62b4a867 VS |
229 | (test-ps-js quoted-nil |
230 | 'nil | |
72332f2a VS |
231 | "null") |
232 | ||
233 | (test defsetf1 | |
234 | (ps (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))) | |
07afea38 | 235 | (is (string= "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);" |
58c4ef4f | 236 | (normalize-js-code (let* ((ps:*ps-gensym-counter* 0)) |
72332f2a | 237 | (ps (setf (baz 1 2) 3))))))) |
d989d711 | 238 | |
750651b0 VS |
239 | (test defsetf-short |
240 | (ps (defsetf baz set-baz "blah")) | |
241 | (is (string= "setBaz(1, 2, 3, 'foo');" (normalize-js-code (ps (setf (baz 1 2 3) "foo")))))) | |
242 | ||
dbb7017b VS |
243 | (test defun-setf1 |
244 | (is (and (string= (normalize-js-code (ps:ps (defun (setf some-thing) (new-val i1 i2) | |
245 | (setf (aref *some-thing* i1 i2) new-val)))) | |
13b8268e | 246 | "function __setf_someThing(newVal, i1, i2) { SOMETHING[i1][i2] = newVal; };") |
07afea38 VS |
247 | (string= (let ((ps:*ps-gensym-counter* 0)) (normalize-js-code (ps:ps (setf (some-thing 1 2) "foo")))) |
248 | "var _js2 = 1; var _js3 = 2; var _js1 = 'foo'; __setf_someThing(_js1, _js2, _js3);")))) | |
dbb7017b | 249 | |
d989d711 VS |
250 | (test-ps-js defun-optional1 |
251 | (defun test-opt (&optional x) (return (if x "yes" "no"))) | |
252 | "function testOpt(x) { | |
253 | x = undefined === x && null || x; | |
254 | return x ? 'yes' : 'no'; | |
a2434734 VS |
255 | }") |
256 | ||
257 | (test-ps-js return-nothing | |
258 | (return) | |
259 | "return null") | |
dbb7017b VS |
260 | |
261 | (test-ps-js set-timeout | |
262 | (do-set-timeout (10) (alert "foo")) | |
b44afd8f | 263 | "setTimeout(function () { alert('foo'); }, 10)") |
07afea38 VS |
264 | (test-ps-js operator-precedence |
265 | (* 3 (+ 4 5) 6) | |
266 | "3 * (4 + 5) * 6") | |
267 | ||
268 | (test-ps-js incf1 | |
269 | (incf foo bar) | |
270 | "foo += bar") | |
271 | ||
272 | (test-ps-js decf1 | |
273 | (decf foo bar) | |
274 | "foo -= bar") | |
275 | ||
49c50da4 VS |
276 | (test-ps-js incf2 |
277 | (incf x 5) | |
278 | "x += 5") | |
279 | ||
280 | (test-ps-js decf2 | |
281 | (decf y 10) | |
282 | "y -= 10") | |
283 | ||
07afea38 VS |
284 | (test-ps-js setf-conditional |
285 | (setf foo (if x 1 2)) | |
286 | "foo = x ? 1 : 2;") | |
287 | ||
288 | (test-ps-js obj-literal-numbers | |
289 | (create 1 "foo") | |
290 | "{ 1 : 'foo' }") | |
291 | ||
292 | (test-ps-js obj-literal-strings | |
293 | (create "foo" 2) | |
a805b30d VS |
294 | "{ 'foo' : 2 }") |
295 | ||
296 | (test-ps-js slot-value-string | |
297 | (slot-value foo "bar") | |
298 | "foo['bar']") | |
b44afd8f VS |
299 | |
300 | (test-ps-js slot-value-progn | |
13b8268e VS |
301 | (slot-value (progn (some-fun "abc") "123") "length") |
302 | "(someFun('abc'), '123')['length']") | |
b44afd8f VS |
303 | |
304 | (test-ps-js method-call-block | |
13b8268e VS |
305 | (.to-string (progn (some-fun "abc") "123")) |
306 | "(someFun('abc'), '123').toString()") | |
b44afd8f VS |
307 | |
308 | (test-ps-js create-blank | |
309 | (create) | |
310 | "{ }") | |
311 | ||
312 | (test-ps-js blank-object-literal | |
f326f929 | 313 | ({}) |
b44afd8f | 314 | "{ }") |
44934751 VS |
315 | |
316 | (test-ps-js defun-rest1 | |
317 | (defun foo (&rest bar) (alert bar[1])) | |
318 | "function foo() { | |
319 | var bar = []; | |
b5e0bcb7 VS |
320 | for (var i2 = 0; i2 < arguments.length - 0; i2 = i2 + 1) { |
321 | bar[i2] = arguments[i2 + 0]; | |
44934751 VS |
322 | }; |
323 | alert(bar[1]); | |
324 | }") | |
325 | ||
326 | (test-ps-js defun-rest2 | |
327 | (defun foo (baz &rest bar) (return (+ baz (aref bar 1)))) | |
2e593e4c | 328 | "function foo(baz) { |
44934751 | 329 | var bar = []; |
b5e0bcb7 VS |
330 | for (var i2 = 0; i2 < arguments.length - 1; i2 = i2 + 1) { |
331 | bar[i2] = arguments[i2 + 1]; | |
44934751 VS |
332 | }; |
333 | return baz + bar[1]; | |
334 | }") | |
2e593e4c VS |
335 | |
336 | (test-ps-js defun-keyword1 | |
337 | (defun zoo (foo bar &key baz) (return (+ foo bar baz))) | |
338 | "function zoo(foo, bar, _js1) { | |
339 | _js1 = undefined === _js1 && { } || _js1; | |
340 | return foo + bar + _js1.baz; | |
341 | }") | |
b0f64e9b VS |
342 | |
343 | (test-ps-js cond1 | |
344 | (cond ((= x 1) 1)) | |
345 | "if (x == 1) { | |
346 | 1; | |
347 | }") | |
348 | ||
349 | (test-ps-js cond2 | |
13b8268e | 350 | (cond ((= x 1) 2) ((= y (* x 4)) (foo "blah") (* x y))) |
b0f64e9b VS |
351 | "if (x == 1) { |
352 | 2; | |
353 | } else if (y == x * 4) { | |
13b8268e | 354 | foo('blah'); |
b0f64e9b VS |
355 | x * y; |
356 | }") | |
5705b542 VS |
357 | |
358 | (test-ps-js if-exp-without-else-returns-null | |
359 | (return (if x 1)) | |
360 | "return x ? 1 : null") | |
361 | ||
362 | (test-ps-js progn-expression-single-statement | |
363 | (return (progn (* x y))) | |
364 | "return x * y") | |
0949f072 VS |
365 | |
366 | (test-ps-js cond-expression1 | |
13b8268e | 367 | (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5))))) |
0949f072 | 368 | "function foo() { |
13b8268e | 369 | return 1 < 2 ? (bar('foo'), 4 * 5) : null; |
0949f072 VS |
370 | }") |
371 | ||
372 | (test-ps-js cond-expression2 | |
373 | (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar")))) | |
374 | "function foo() { | |
375 | return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null); | |
376 | }") | |
377 | ||
378 | (test-ps-js cond-expression-final-t-clause | |
13b8268e | 379 | (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo")))) |
0949f072 | 380 | "function foo() { |
13b8268e | 381 | return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo')); |
0949f072 VS |
382 | }") |
383 | ||
384 | (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning? | |
385 | (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar")))) | |
386 | "function foo() { | |
387 | return 2 < 1 ? 5 : 'foo'; | |
388 | }") | |
e5253c5b VS |
389 | |
390 | (test-ps-js funcall-if-expression | |
391 | (document.write | |
392 | (if (= *linkornot* 1) | |
393 | (ps-html ((:a :href "#" | |
394 | :onclick (lisp (ps-inline (transport)))) | |
395 | img)) | |
396 | img)) | |
9fbb3004 | 397 | "document.write(LINKORNOT == 1 ? '<a href=\"#\" onclick=\"' + 'javascript:transport()' + '\">' + img + '</a>' : img)") |
49c50da4 VS |
398 | |
399 | (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test! | |
400 | (- 1) | |
401 | "-1") | |
efe35e12 VS |
402 | |
403 | (test macro-environment1 | |
58c4ef4f | 404 | (is (string= (normalize-js-code (let* ((macroname (gensym))) |
efe35e12 VS |
405 | (ps* `(defmacro ,macroname (x) `(+ ,x 123)) |
406 | `(defun test1 () | |
407 | (macrolet ((,macroname (x) `(aref data ,x))) | |
408 | (when (,macroname x) | |
409 | (setf (,macroname x) 123))))))) | |
410 | (normalize-js-code | |
411 | "function test1() { | |
412 | if (data[x]) { | |
413 | data[x] = 123; | |
414 | }; | |
415 | }; | |
416 | ")))) | |
921f2e02 VS |
417 | |
418 | (test-ps-js ampersand-whole-1 | |
419 | (macrolet ((foo (&whole foo bar baz) | |
420 | (declare (ignore bar baz)) | |
421 | (format nil "~a" foo))) | |
422 | (foo 1 2)) | |
6e8ff198 VS |
423 | "'(FOO 1 2)';") |
424 | ||
425 | (test-ps-js keyword-consistent | |
426 | :x | |
427 | "x") | |
43a1d5c3 VS |
428 | |
429 | (test-ps-js simple-symbol-macrolet | |
430 | (symbol-macrolet ((x 1)) x) | |
431 | "1;") | |
432 | ||
433 | (test-ps-js compound-symbol-macrolet | |
434 | (symbol-macrolet ((x 123) | |
435 | (y (* 2 x))) | |
436 | y) | |
437 | "2 * 123;") | |
1af04ae5 VS |
438 | |
439 | (test-ps-js define-symbol-macro | |
440 | (progn (define-symbol-macro tst-sym-macro 2) | |
441 | tst-sym-macro) | |
442 | "2;") | |
e0032a96 VS |
443 | |
444 | (test-ps-js expression-progn | |
445 | (defun f () (return (progn (foo) (if x 1 2)))) | |
446 | "function f() { | |
447 | return (foo(), x ? 1 : 2); | |
448 | }") | |
449 | ||
450 | (test-ps-js let-decl-in-expression | |
58c4ef4f | 451 | (defun f (x) (return (if x 1 (let* ((foo x)) foo)))) |
e0032a96 VS |
452 | "function f(x) { |
453 | var foo; | |
454 | return x ? 1 : (foo = x, foo); | |
455 | }") | |
58c4ef4f VS |
456 | |
457 | (test-ps-js special-var1 | |
458 | (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2))) | |
459 | "var FOO; | |
460 | var tempstackvar1; | |
461 | try { | |
462 | tempstackvar1 = FOO; | |
463 | FOO = 2; | |
464 | FOO * 2; | |
465 | } finally { | |
466 | FOO = tempstackvar1; | |
467 | };") | |
468 | ||
469 | (test-ps-js special-var2 | |
470 | (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*))) | |
471 | "var FOO; | |
472 | var BAZ = 3; | |
473 | var tempstackvar1; | |
474 | try { | |
475 | tempstackvar1 = FOO; | |
476 | FOO = 2; | |
477 | FOO * 2 * BAZ; | |
478 | } finally { | |
479 | FOO = tempstackvar1; | |
480 | };") |