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 | |
79630c82 VS |
10 | (setf x (+ "before" x "after")) |
11 | "x = 'before' + x + 'after';") | |
e2932347 LC |
12 | |
13 | (test-ps-js plus-works-if-first | |
79630c82 VS |
14 | (setf x (+ x "middle" "after")) |
15 | "x += 'middle' + 'after';") | |
e2932347 LC |
16 | |
17 | (test-ps-js setf-side-effects | |
58c4ef4f | 18 | (progn |
5ffb1eba | 19 | (let ((x 10)) |
58c4ef4f VS |
20 | (defun side-effect() |
21 | (setf x 4) | |
22 | (return 3)) | |
23 | (setf x (+ 2 (side-effect) x 5)))) | |
16151f19 | 24 | "var x = 10; |
e2932347 | 25 | function sideEffect() { |
16151f19 | 26 | x = 4; |
e2932347 LC |
27 | return 3; |
28 | }; | |
16151f19 | 29 | x = 2 + sideEffect() + x + 5;") |
79630c82 | 30 | ;; Parenscript used to optimize incorrectly: |
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 | ||
79630c82 VS |
41 | (test-ps-js method-call-op-form |
42 | ((@ (+ "" x) to-string)) | |
5a69278c | 43 | "('' + x).toString();") |
79630c82 VS |
44 | |
45 | (test-ps-js method-call-op-form-args | |
46 | ((@ (+ "" x) to-string) 1 2 :baz 3) | |
5a69278c | 47 | "('' + x).toString(1, 2, 'baz', 3);") |
79630c82 VS |
48 | |
49 | (test-ps-js method-call-number | |
50 | ((@ 10 to-string)) | |
5a69278c | 51 | "( 10 ).toString();") |
79630c82 VS |
52 | |
53 | (test-ps-js method-call-string | |
54 | ((@ "hi" to-string)) | |
5a69278c | 55 | "'hi'.toString();") |
e2932347 | 56 | |
e2932347 | 57 | (test-ps-js method-call-lit-object |
79630c82 | 58 | ((@ (create :to-string (lambda () (return "it works"))) to-string)) |
5a69278c | 59 | "( { toString : function () { return 'it works'; } } ).toString();") |
79630c82 VS |
60 | |
61 | (test-ps-js method-call-conditional | |
62 | ((if a x y) 1) | |
5a69278c | 63 | "(a ? x : y)(1);") |
e2932347 LC |
64 | |
65 | (test-ps-js method-call-variable | |
79630c82 | 66 | ((@ x to-string)) |
5a69278c | 67 | "x.toString();") |
e2932347 LC |
68 | |
69 | (test-ps-js method-call-array | |
79630c82 | 70 | ((@ (list 10 20) to-string)) |
5a69278c | 71 | "[ 10, 20 ].toString();") |
79630c82 | 72 | |
e2932347 | 73 | (test-ps-js method-call-fn-call |
79630c82 | 74 | ((@ (foo) to-string)) |
5a69278c | 75 | "foo().toString();") |
79630c82 | 76 | |
e2932347 | 77 | (test-ps-js method-call-lambda-fn |
79630c82 | 78 | ((@ (lambda () (alert 10)) to-string)) |
5a69278c | 79 | "( function () { alert(10); } ).toString();") |
79630c82 | 80 | |
e2932347 | 81 | (test-ps-js method-call-lambda-call |
79630c82 | 82 | ((@ ((lambda (x) (return x)) 10) to-string)) |
5a69278c | 83 | "(function (x) { return x; })(10).toString();") |
e2932347 LC |
84 | |
85 | (test no-whitespace-before-dot | |
5a69278c | 86 | (let* ((str (ps* '((@ ((lambda (x) (return x)) 10) to-string)))) |
e2932347 LC |
87 | (dot-pos (position #\. str :test #'char=)) |
88 | (char-before (elt str (1- dot-pos))) | |
89 | (a-parenthesis #\))) | |
90 | (is (char= char-before a-parenthesis)))) | |
91 | ||
e2932347 | 92 | (test-ps-js simple-slot-value |
5ffb1eba | 93 | (let ((foo (create :a 1))) |
f2bb932e | 94 | (alert (slot-value foo 'a))) |
16151f19 VS |
95 | "var foo = { a : 1 }; |
96 | alert(foo.a);") | |
e2932347 LC |
97 | |
98 | (test-ps-js buggy-slot-value | |
5ffb1eba VS |
99 | (let ((foo (create :a 1)) |
100 | (slot-name "a")) | |
e2932347 | 101 | (alert (slot-value foo slot-name))) |
16151f19 VS |
102 | " var foo = { a : 1 }; |
103 | var slotName = 'a'; | |
104 | alert(foo[slotName]); | |
07afea38 | 105 | "); Last line was alert(foo.slotName) before bug-fix. |
e2932347 LC |
106 | |
107 | (test-ps-js buggy-slot-value-two | |
108 | (slot-value foo (get-slot-name)) | |
5a69278c | 109 | "foo[getSlotName()];") |
e2932347 LC |
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"))) | |
5a69278c | 122 | "switch (blorg[i]) { |
e2932347 LC |
123 | case 1: alert('one'); |
124 | case 2: alert('two'); | |
125 | default: alert('default clause'); | |
5a69278c | 126 | };") |
e2932347 LC |
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'); | |
5a69278c | 141 | };") |
e2932347 LC |
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]) { | |
b44afd8f | 150 | case 1: |
e2932347 LC |
151 | case 2: |
152 | alert('Below three'); | |
153 | break; | |
154 | case 3: | |
155 | alert('Three'); | |
156 | break; | |
157 | default: alert('Something else'); | |
5a69278c | 158 | };") |
e2932347 LC |
159 | |
160 | (test-ps-js otherwise-case | |
161 | (case (aref blorg i) | |
162 | (1 (alert "one")) | |
e0f0d152 | 163 | (otherwise (alert "default clause"))) |
e2932347 LC |
164 | "switch (blorg[i]) { |
165 | case 1: | |
166 | alert('one'); | |
167 | break; | |
168 | default: alert('default clause'); | |
5a69278c | 169 | };") |
e2932347 LC |
170 | |
171 | (test escape-sequences-in-string | |
905f534e | 172 | (let ((escapes `((#\\ . #\\) |
e2932347 LC |
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 | |
5ffb1eba | 184 | for generated = (ps-doc* `(let ((x ,(format nil "hello~ahi" lisp-char))))) |
16151f19 | 185 | for wanted = (format nil "var x = 'hello\\~ahi';" js-escape) |
b508414b | 186 | do (is (string= (normalize-js-code generated) wanted))))) |
9da682ca | 187 | |
cf460f93 VS |
188 | (test-ps-js slot-value-setf |
189 | (setf (slot-value x 'y) (+ (+ a 3) 4)) | |
72332f2a | 190 | "x.y = (a + 3) + 4;") |
eb0befe2 VS |
191 | |
192 | (test-ps-js slot-value-conditional1 | |
193 | (slot-value (if zoo foo bar) 'x) | |
5a69278c | 194 | "(zoo ? foo : bar).x;") |
eb0befe2 VS |
195 | |
196 | (test-ps-js slot-value-conditional2 | |
197 | (slot-value (if (not zoo) foo bar) 'x) | |
5a69278c | 198 | "(!zoo ? foo : bar).x;") |
eb0befe2 | 199 | |
245d0fbd | 200 | (test script-star-eval1 |
07afea38 | 201 | (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2)))))) |
245d0fbd VS |
202 | |
203 | (test script-star-eval2 | |
07afea38 | 204 | (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1)))))) |
62b4a867 | 205 | |
46f794a4 RD |
206 | (test-ps-js unquoted-nil |
207 | nil | |
5a69278c | 208 | "null;") |
46f794a4 RD |
209 | |
210 | (test-ps-js list-with-single-nil | |
fb469285 | 211 | (array nil) |
5a69278c | 212 | "[null];") |
46f794a4 | 213 | |
fb469285 | 214 | (test-ps-js quoted-nil-is-array |
62b4a867 | 215 | 'nil |
5a69278c | 216 | "[];") |
72332f2a | 217 | |
5af33a27 VS |
218 | (test-ps-js defsetf1 |
219 | (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval)) | |
220 | (setf (baz 1 2) 3)) | |
16151f19 VS |
221 | "var _js2 = 1; |
222 | var _js3 = 2; | |
223 | var _js1 = 3; | |
224 | setBaz(_js2, _js3, _js1);") | |
5af33a27 | 225 | |
5a69278c VS |
226 | (test-ps-js setf-macroexpands1 |
227 | (macrolet ((baz (x y) `(aref ,x ,y 1))) | |
228 | (setf (baz foo 2) 3)) | |
229 | "foo[2][1] = 3;") | |
230 | ||
5af33a27 VS |
231 | (test-ps-js defsetf-short |
232 | (progn (defsetf baz set-baz "docstring") | |
233 | (setf (baz 1 2 3) "foo")) | |
234 | "setBaz(1, 2, 3, 'foo');") | |
235 | ||
236 | (test-ps-js defun-setf1 | |
237 | (progn (defun (setf some-thing) (new-val i1 i2) | |
238 | (setf (aref *some-thing* i1 i2) new-val)) | |
239 | (setf (some-thing 1 2) "foo")) | |
c7716e67 | 240 | "function __setf_someThing(newVal, i1, i2) { |
5af33a27 VS |
241 | SOMETHING[i1][i2] = newVal; |
242 | }; | |
16151f19 VS |
243 | var _js2 = 1; |
244 | var _js3 = 2; | |
245 | var _js1 = 'foo'; | |
246 | __setf_someThing(_js1, _js2, _js3);") | |
dbb7017b | 247 | |
d989d711 VS |
248 | (test-ps-js defun-optional1 |
249 | (defun test-opt (&optional x) (return (if x "yes" "no"))) | |
250 | "function testOpt(x) { | |
0e198f66 TC |
251 | if (x === undefined) { |
252 | x = null; | |
253 | }; | |
254 | return x ? 'yes' : 'no'; | |
5a69278c | 255 | };") |
a2434734 | 256 | |
5ac90695 VS |
257 | (test-ps-js defun-optional2 |
258 | (defun foo (x &optional y) (+ x y)) | |
259 | "function foo(x, y) { | |
0e198f66 TC |
260 | if (y === undefined) { |
261 | y = null; | |
262 | }; | |
5ac90695 | 263 | x + y; |
5a69278c | 264 | };") |
5ac90695 | 265 | |
7be02bed VS |
266 | (test-ps-js defun-optional3 |
267 | (defun blah (&optional (x 0)) | |
268 | (return x)) | |
269 | "function blah(x) { | |
270 | if (x === undefined) { | |
271 | x = 0; | |
272 | }; | |
273 | return x; | |
5a69278c | 274 | };") |
7be02bed | 275 | |
a2434734 VS |
276 | (test-ps-js return-nothing |
277 | (return) | |
5a69278c | 278 | "return null;") |
dbb7017b VS |
279 | |
280 | (test-ps-js set-timeout | |
281 | (do-set-timeout (10) (alert "foo")) | |
5a69278c VS |
282 | "setTimeout(function () { alert('foo'); }, 10);") |
283 | ||
07afea38 VS |
284 | (test-ps-js operator-precedence |
285 | (* 3 (+ 4 5) 6) | |
5a69278c | 286 | "3 * (4 + 5) * 6;") |
07afea38 | 287 | |
2d2c6dc2 TC |
288 | (test-ps-js operators-1 |
289 | (in prop obj) | |
5a69278c | 290 | "prop in obj;") |
2d2c6dc2 | 291 | |
07afea38 VS |
292 | (test-ps-js incf1 |
293 | (incf foo bar) | |
5a69278c | 294 | "foo += bar;") |
07afea38 VS |
295 | |
296 | (test-ps-js decf1 | |
297 | (decf foo bar) | |
5a69278c | 298 | "foo -= bar;") |
07afea38 | 299 | |
49c50da4 VS |
300 | (test-ps-js incf2 |
301 | (incf x 5) | |
5a69278c | 302 | "x += 5;") |
49c50da4 VS |
303 | |
304 | (test-ps-js decf2 | |
305 | (decf y 10) | |
5a69278c | 306 | "y -= 10;") |
49c50da4 | 307 | |
07afea38 VS |
308 | (test-ps-js setf-conditional |
309 | (setf foo (if x 1 2)) | |
310 | "foo = x ? 1 : 2;") | |
311 | ||
312 | (test-ps-js obj-literal-numbers | |
313 | (create 1 "foo") | |
5a69278c | 314 | "{ 1 : 'foo' };") |
07afea38 VS |
315 | |
316 | (test-ps-js obj-literal-strings | |
317 | (create "foo" 2) | |
5a69278c | 318 | "{ 'foo' : 2 };") |
a805b30d VS |
319 | |
320 | (test-ps-js slot-value-string | |
321 | (slot-value foo "bar") | |
5a69278c | 322 | "foo['bar'];") |
b44afd8f | 323 | |
79630c82 VS |
324 | (test-ps-js slot-value-string1 |
325 | (slot-value "bar" 'length) | |
5a69278c | 326 | "'bar'.length;") |
79630c82 | 327 | |
b44afd8f | 328 | (test-ps-js slot-value-progn |
13b8268e | 329 | (slot-value (progn (some-fun "abc") "123") "length") |
5a69278c | 330 | "(someFun('abc'), '123')['length'];") |
b44afd8f VS |
331 | |
332 | (test-ps-js method-call-block | |
79630c82 | 333 | ((@ (progn (some-fun "abc") "123") to-string)) |
5a69278c | 334 | "(someFun('abc'), '123').toString();") |
b44afd8f VS |
335 | |
336 | (test-ps-js create-blank | |
337 | (create) | |
5a69278c | 338 | "{ };") |
b44afd8f VS |
339 | |
340 | (test-ps-js blank-object-literal | |
5ac90695 | 341 | {} |
5a69278c | 342 | "{ };") |
44934751 | 343 | |
06ed0d3a VS |
344 | (test-ps-js array-literal1 |
345 | [] | |
5a69278c | 346 | "[];") |
06ed0d3a VS |
347 | |
348 | (test-ps-js array-literal2 | |
349 | ([]) | |
5a69278c | 350 | "[];") |
06ed0d3a VS |
351 | |
352 | (test-ps-js array-literal3 | |
353 | ([] 1 2 3) | |
5a69278c | 354 | "[1, 2, 3];") |
06ed0d3a VS |
355 | |
356 | (test-ps-js array-literal4 | |
357 | ([] 1 (2 3)) | |
5a69278c | 358 | "[1, [2, 3]];") |
06ed0d3a VS |
359 | |
360 | (test-ps-js array-literal5 | |
361 | ([] (1 2) ("a" "b")) | |
5a69278c | 362 | "[[1, 2], ['a', 'b']];") |
06ed0d3a | 363 | |
44934751 | 364 | (test-ps-js defun-rest1 |
5ffb1eba | 365 | (defun foo (&rest bar) (alert (aref bar 1))) |
44934751 VS |
366 | "function foo() { |
367 | var bar = []; | |
c7716e67 VS |
368 | for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) { |
369 | bar[i1] = arguments[i1 + 0]; | |
44934751 VS |
370 | }; |
371 | alert(bar[1]); | |
5a69278c | 372 | };") |
44934751 VS |
373 | |
374 | (test-ps-js defun-rest2 | |
375 | (defun foo (baz &rest bar) (return (+ baz (aref bar 1)))) | |
2e593e4c | 376 | "function foo(baz) { |
44934751 | 377 | var bar = []; |
c7716e67 VS |
378 | for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) { |
379 | bar[i1] = arguments[i1 + 1]; | |
44934751 VS |
380 | }; |
381 | return baz + bar[1]; | |
5a69278c | 382 | };") |
2e593e4c VS |
383 | |
384 | (test-ps-js defun-keyword1 | |
385 | (defun zoo (foo bar &key baz) (return (+ foo bar baz))) | |
1cd3540f | 386 | "function zoo(foo, bar) { |
c7716e67 | 387 | var baz; |
5ffb1eba VS |
388 | var _js2 = arguments.length; |
389 | for (var n1 = 2; n1 < _js2; n1 += 2) { | |
c7716e67 VS |
390 | switch (arguments[n1]) { |
391 | case 'baz': | |
392 | { | |
393 | baz = arguments[n1 + 1]; | |
394 | }; | |
395 | }; | |
0e198f66 | 396 | }; |
c7716e67 VS |
397 | if (baz === undefined) { |
398 | baz = null; | |
399 | }; | |
400 | return foo + bar + baz; | |
5a69278c | 401 | };") |
b0f64e9b | 402 | |
f2bb932e VS |
403 | (test-ps-js defun-keyword2 |
404 | (defun zoo (&key baz) (return (* baz baz))) | |
c7716e67 VS |
405 | "function zoo() { |
406 | var baz; | |
5ffb1eba VS |
407 | var _js2 = arguments.length; |
408 | for (var n1 = 0; n1 < _js2; n1 += 2) { | |
c7716e67 VS |
409 | switch (arguments[n1]) { |
410 | case 'baz': | |
411 | { | |
412 | baz = arguments[n1 + 1]; | |
413 | }; | |
414 | }; | |
c7716e67 VS |
415 | }; |
416 | if (baz === undefined) { | |
417 | baz = null; | |
f2bb932e | 418 | }; |
c7716e67 | 419 | return baz * baz; |
5a69278c | 420 | };") |
f2bb932e VS |
421 | |
422 | (test-ps-js defun-keyword3 | |
423 | (defun zoo (&key baz (bar 4)) (return (* baz bar))) | |
c7716e67 VS |
424 | "function zoo() { |
425 | var baz; | |
426 | var bar; | |
5ffb1eba VS |
427 | var _js2 = arguments.length; |
428 | for (var n1 = 0; n1 < _js2; n1 += 2) { | |
c7716e67 VS |
429 | switch (arguments[n1]) { |
430 | case 'baz': | |
431 | { | |
432 | baz = arguments[n1 + 1]; | |
433 | }; | |
434 | break; | |
435 | case 'bar': | |
436 | { | |
437 | bar = arguments[n1 + 1]; | |
438 | }; | |
439 | }; | |
c7716e67 VS |
440 | }; |
441 | if (baz === undefined) { | |
442 | baz = null; | |
f2bb932e | 443 | }; |
c7716e67 VS |
444 | if (bar === undefined) { |
445 | bar = 4; | |
f2bb932e | 446 | }; |
c7716e67 | 447 | return baz * bar; |
5a69278c | 448 | };") |
f2bb932e | 449 | |
62ddca23 VS |
450 | (test-ps-js defun-keyword4 |
451 | (defun hello-world (&key ((:my-name-key my-name) 1)) | |
452 | my-name) | |
453 | "function helloWorld() { | |
454 | var myName; | |
5ffb1eba VS |
455 | var _js2 = arguments.length; |
456 | for (var n1 = 0; n1 < _js2; n1 += 2) { | |
62ddca23 VS |
457 | switch (arguments[n1]) { |
458 | case 'my-name-key': | |
459 | { | |
460 | myName = arguments[n1 + 1]; | |
461 | }; | |
462 | }; | |
463 | }; | |
464 | if (myName === undefined) { | |
465 | myName = 1; | |
466 | }; | |
467 | myName; | |
5a69278c | 468 | };") |
62ddca23 | 469 | |
f2bb932e VS |
470 | (test-ps-js keyword-funcall1 |
471 | (func :baz 1) | |
5a69278c | 472 | "func('baz', 1);") |
f2bb932e VS |
473 | |
474 | (test-ps-js keyword-funcall2 | |
475 | (func :baz 1 :bar foo) | |
5a69278c | 476 | "func('baz', 1, 'bar', foo);") |
f2bb932e VS |
477 | |
478 | (test-ps-js keyword-funcall3 | |
479 | (fun a b :baz c) | |
5a69278c | 480 | "fun(a, b, 'baz', c);") |
f2bb932e | 481 | |
b0f64e9b VS |
482 | (test-ps-js cond1 |
483 | (cond ((= x 1) 1)) | |
484 | "if (x == 1) { | |
485 | 1; | |
5a69278c | 486 | };") |
b0f64e9b VS |
487 | |
488 | (test-ps-js cond2 | |
fdfa77fc VS |
489 | (cond ((= x 1) 2) |
490 | ((= y (* x 4)) (foo "blah") (* x y))) | |
b0f64e9b VS |
491 | "if (x == 1) { |
492 | 2; | |
493 | } else if (y == x * 4) { | |
13b8268e | 494 | foo('blah'); |
b0f64e9b | 495 | x * y; |
5a69278c | 496 | };") |
5705b542 VS |
497 | |
498 | (test-ps-js if-exp-without-else-returns-null | |
499 | (return (if x 1)) | |
5a69278c | 500 | "return x ? 1 : null;") |
5705b542 VS |
501 | |
502 | (test-ps-js progn-expression-single-statement | |
503 | (return (progn (* x y))) | |
5a69278c | 504 | "return x * y;") |
0949f072 VS |
505 | |
506 | (test-ps-js cond-expression1 | |
13b8268e | 507 | (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5))))) |
0949f072 | 508 | "function foo() { |
13b8268e | 509 | return 1 < 2 ? (bar('foo'), 4 * 5) : null; |
5a69278c | 510 | };") |
0949f072 VS |
511 | |
512 | (test-ps-js cond-expression2 | |
513 | (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar")))) | |
514 | "function foo() { | |
515 | return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null); | |
5a69278c | 516 | };") |
0949f072 VS |
517 | |
518 | (test-ps-js cond-expression-final-t-clause | |
13b8268e | 519 | (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo")))) |
0949f072 | 520 | "function foo() { |
13b8268e | 521 | return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo')); |
5a69278c | 522 | };") |
0949f072 VS |
523 | |
524 | (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning? | |
525 | (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar")))) | |
526 | "function foo() { | |
527 | return 2 < 1 ? 5 : 'foo'; | |
5a69278c | 528 | };") |
e5253c5b VS |
529 | |
530 | (test-ps-js funcall-if-expression | |
5ffb1eba | 531 | ((@ document write) |
e5253c5b VS |
532 | (if (= *linkornot* 1) |
533 | (ps-html ((:a :href "#" | |
e69d0a12 | 534 | :onclick (ps-inline (transport))) |
e5253c5b VS |
535 | img)) |
536 | img)) | |
5a69278c | 537 | "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">' + img + '</A>' : img);") |
49c50da4 VS |
538 | |
539 | (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test! | |
540 | (- 1) | |
5a69278c | 541 | "-1;") |
efe35e12 VS |
542 | |
543 | (test macro-environment1 | |
58c4ef4f | 544 | (is (string= (normalize-js-code (let* ((macroname (gensym))) |
efe35e12 VS |
545 | (ps* `(defmacro ,macroname (x) `(+ ,x 123)) |
546 | `(defun test1 () | |
547 | (macrolet ((,macroname (x) `(aref data ,x))) | |
548 | (when (,macroname x) | |
549 | (setf (,macroname x) 123))))))) | |
550 | (normalize-js-code | |
551 | "function test1() { | |
552 | if (data[x]) { | |
553 | data[x] = 123; | |
554 | }; | |
555 | }; | |
556 | ")))) | |
921f2e02 | 557 | |
8cfc6fe9 VS |
558 | (test macro-environment2 |
559 | (is (string= (normalize-js-code (let ((outer-lexical-variable 1)) | |
560 | (defpsmacro macro-environment2-macro (x) | |
561 | `(+ ,outer-lexical-variable ,x)) | |
562 | (ps* '(macro-environment2-macro 2)))) | |
563 | (normalize-js-code "1 + 2;")))) | |
564 | ||
921f2e02 VS |
565 | (test-ps-js ampersand-whole-1 |
566 | (macrolet ((foo (&whole foo bar baz) | |
567 | (declare (ignore bar baz)) | |
568 | (format nil "~a" foo))) | |
569 | (foo 1 2)) | |
6e8ff198 VS |
570 | "'(FOO 1 2)';") |
571 | ||
572 | (test-ps-js keyword-consistent | |
573 | :x | |
5a69278c | 574 | "'x';") |
43a1d5c3 VS |
575 | |
576 | (test-ps-js simple-symbol-macrolet | |
577 | (symbol-macrolet ((x 1)) x) | |
578 | "1;") | |
579 | ||
580 | (test-ps-js compound-symbol-macrolet | |
581 | (symbol-macrolet ((x 123) | |
582 | (y (* 2 x))) | |
583 | y) | |
584 | "2 * 123;") | |
1af04ae5 VS |
585 | |
586 | (test-ps-js define-symbol-macro | |
587 | (progn (define-symbol-macro tst-sym-macro 2) | |
588 | tst-sym-macro) | |
589 | "2;") | |
e0032a96 VS |
590 | |
591 | (test-ps-js expression-progn | |
592 | (defun f () (return (progn (foo) (if x 1 2)))) | |
593 | "function f() { | |
594 | return (foo(), x ? 1 : 2); | |
5a69278c | 595 | };") |
e0032a96 VS |
596 | |
597 | (test-ps-js let-decl-in-expression | |
16151f19 VS |
598 | (defun f (x) |
599 | (return (if x | |
600 | 1 | |
601 | (let* ((foo x)) | |
602 | foo)))) | |
e0032a96 | 603 | "function f(x) { |
16151f19 VS |
604 | var foo; |
605 | return x ? 1 : (foo = x, foo); | |
5a69278c | 606 | };") |
58c4ef4f VS |
607 | |
608 | (test-ps-js special-var1 | |
16151f19 VS |
609 | (progn (defvar *foo*) |
610 | (let* ((*foo* 2)) | |
611 | (* *foo* 2))) | |
58c4ef4f | 612 | "var FOO; |
16151f19 | 613 | var FOO_TMPSTACK1; |
58c4ef4f | 614 | try { |
16151f19 | 615 | FOO_TMPSTACK1 = FOO; |
58c4ef4f VS |
616 | FOO = 2; |
617 | FOO * 2; | |
618 | } finally { | |
16151f19 | 619 | FOO = FOO_TMPSTACK1; |
58c4ef4f VS |
620 | };") |
621 | ||
622 | (test-ps-js special-var2 | |
16151f19 VS |
623 | (progn (defvar *foo*) |
624 | (let* ((*baz* 3) | |
625 | (*foo* 2)) | |
626 | (* *foo* 2 *baz*))) | |
58c4ef4f | 627 | "var FOO; |
16151f19 VS |
628 | var BAZ = 3; |
629 | var FOO_TMPSTACK1; | |
58c4ef4f | 630 | try { |
16151f19 | 631 | FOO_TMPSTACK1 = FOO; |
58c4ef4f | 632 | FOO = 2; |
16151f19 | 633 | FOO * 2 * BAZ; |
58c4ef4f | 634 | } finally { |
16151f19 | 635 | FOO = FOO_TMPSTACK1; |
5ffb1eba | 636 | };") |
5ac90695 VS |
637 | |
638 | (test-ps-js literal1 | |
639 | (setf x undefined) | |
640 | "x = undefined;") | |
641 | ||
642 | (test-ps-js literal2 | |
643 | (aref this x) | |
5a69278c | 644 | "this[x];") |
d89456ee VS |
645 | |
646 | (test-ps-js setf-dec1 | |
647 | (setf x (- 1 x 2)) | |
648 | "x = 1 - x - 2;") | |
649 | ||
650 | (test-ps-js setf-dec2 | |
651 | (setf x (- x 1 2)) | |
652 | "x = x - 1 - 2;") | |
93e99720 VS |
653 | |
654 | (test-ps-js special-char-equals | |
655 | blah= | |
5a69278c | 656 | "blahequals;") |
7be02bed VS |
657 | |
658 | (test-ps-js setf-operator-priority | |
659 | (return (or (slot-value cache id) | |
5ffb1eba | 660 | (setf (slot-value cache id) ((@ document get-element-by-id) id)))) |
5a69278c | 661 | "return cache[id] || (cache[id] = document.getElementById(id));") |
7be02bed VS |
662 | |
663 | (test-ps-js aref-operator-priority | |
664 | (aref (if (and x (> (length x) 0)) | |
665 | (aref x 0) | |
666 | y) | |
667 | z) | |
5a69278c | 668 | "(x && x.length > 0 ? x[0] : y)[z];") |
7be02bed VS |
669 | |
670 | (test-ps-js aref-operator-priority1 | |
671 | (aref (or (slot-value x 'y) | |
672 | (slot-value a 'b)) | |
673 | z) | |
5a69278c | 674 | "(x.y || a.b)[z];") |
7be02bed VS |
675 | |
676 | (test-ps-js aref-operator-priority2 | |
677 | (aref (if a b c) 0) | |
5a69278c | 678 | "(a ? b : c)[0];") |
7be02bed VS |
679 | |
680 | (test-ps-js negative-operator-priority | |
681 | (- (if x y z)) | |
5a69278c | 682 | "-(x ? y : z);") |
7be02bed VS |
683 | |
684 | (test-ps-js op-p1 | |
685 | (new (or a b)) | |
5a69278c | 686 | "new (a || b);") |
7be02bed VS |
687 | |
688 | (test-ps-js op-p2 | |
689 | (delete (if a (or b c) d)) | |
5a69278c | 690 | "delete (a ? b || c : d);") |
7be02bed VS |
691 | |
692 | (test-ps-js op-p3 | |
693 | (not (if (or x (not y)) z)) | |
5a69278c | 694 | "!(x || !y ? z : null);") |
7be02bed VS |
695 | |
696 | (test-ps-js op-p4 | |
697 | (- (- (* 1 2) 3)) | |
5a69278c | 698 | "-(1 * 2 - 3);") |
7be02bed VS |
699 | |
700 | (test-ps-js op-p5 | |
701 | (instanceof (or a b) (if x y z)) | |
5a69278c | 702 | "((a || b) instanceof (x ? y : z));") |
7be02bed | 703 | |
7be02bed VS |
704 | (test-ps-js op-p7 |
705 | (or x (if (= x 0) "zero" "empty")) | |
5a69278c | 706 | "x || (x == 0 ? 'zero' : 'empty');") |
7be02bed VS |
707 | |
708 | (test-ps-js named-op-expression | |
709 | (throw (if a b c)) | |
5a69278c | 710 | "throw a ? b : c;") |
7be02bed VS |
711 | |
712 | (test-ps-js named-op-expression1 | |
713 | (typeof (or x y)) | |
5a69278c | 714 | "typeof (x || y);") |
9b31227f VS |
715 | |
716 | (test-ps-js aref-array-expression | |
717 | (aref (or a b c) 0) | |
5a69278c | 718 | "(a || b || c)[0];") |
9b31227f VS |
719 | |
720 | (test-ps-js slot-value-operator | |
721 | (slot-value (or a b c) 'd) | |
5a69278c | 722 | "(a || b || c).d;") |
9b31227f VS |
723 | |
724 | (test-ps-js slot-value-parens | |
725 | (slot-value (slot-value foo 'bar) 'baz) | |
5a69278c | 726 | "foo.bar.baz;") |
9496b3a4 VS |
727 | |
728 | (test-ps-js funcall-funcall | |
729 | ((foo)) | |
5a69278c | 730 | "foo()();") |
9496b3a4 VS |
731 | |
732 | (test-ps-js expression-funcall | |
733 | ((or (@ window eval) eval) foo nil) | |
5a69278c | 734 | "(window.eval || eval)(foo, null);") |
9496b3a4 VS |
735 | |
736 | (test-ps-js expression-funcall1 | |
737 | (((or (@ window eval) eval) foo nil)) | |
5a69278c | 738 | "(window.eval || eval)(foo, null)();") |
9496b3a4 VS |
739 | |
740 | (test-ps-js expression-funcall2 | |
741 | (((or (@ window eval) eval)) foo nil) | |
5a69278c | 742 | "(window.eval || eval)()(foo, null);") |
9496b3a4 | 743 | |
79630c82 VS |
744 | (test-ps-js slot-value-object-literal |
745 | (slot-value (create :a 1) 'a) | |
5a69278c | 746 | "({ a : 1 }).a;") |
79630c82 VS |
747 | |
748 | (test-ps-js slot-value-lambda | |
749 | (slot-value (lambda ()) 'prototype) | |
5a69278c | 750 | "(function () { }).prototype;") |
dde6e656 VS |
751 | |
752 | (test-ps-js who-html1 | |
753 | (who-ps-html (:span :class "ticker-symbol" | |
754 | :ticker-symbol symbol | |
755 | (:a :href "http://foo.com" | |
756 | symbol) | |
757 | (:span :class "ticker-symbol-popup"))) | |
5a69278c | 758 | "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>';") |
ef3be63e VS |
759 | |
760 | (test-ps-js flet1 | |
058f137f VS |
761 | ((lambda () (flet ((foo (x) |
762 | (return (1+ x)))) | |
763 | (return (foo 1))))) | |
ef3be63e | 764 | "(function () { |
058f137f | 765 | var foo1 = function (x) { |
ef3be63e VS |
766 | return x + 1; |
767 | }; | |
058f137f | 768 | return foo1(1); |
5a69278c | 769 | })();") |
ef3be63e | 770 | |
058f137f VS |
771 | (test-ps-js flet2 |
772 | (flet ((foo (x) (return (1+ x))) | |
773 | (bar (y) (return (+ 2 y)))) | |
774 | (bar (foo 1))) | |
775 | "var foo1 = function (x) { | |
776 | return x + 1; | |
777 | }; | |
778 | var bar2 = function (y) { | |
779 | return 2 + y; | |
780 | }; | |
781 | bar2(foo1(1));") | |
782 | ||
783 | (test-ps-js flet3 | |
784 | (flet ((foo (x) (return (1+ x))) | |
785 | (bar (y) (return (+ 2 (foo y))))) | |
786 | (bar (foo 1))) | |
787 | "var foo1 = function (x) { | |
788 | return x + 1; | |
789 | }; | |
790 | var bar2 = function (y) { | |
791 | return 2 + foo(y); | |
792 | }; | |
793 | bar2(foo1(1));") | |
794 | ||
ef3be63e VS |
795 | (test-ps-js labels1 |
796 | ((lambda () (labels ((foo (x) | |
797 | (return (if (=== 0 x) | |
798 | 0 | |
799 | (+ x (foo (1- x))))))) | |
800 | (return (foo 3))))) | |
801 | "(function () { | |
058f137f VS |
802 | var foo1 = function (x) { |
803 | return 0 === x ? 0 : x + foo1(x - 1); | |
ef3be63e | 804 | }; |
058f137f | 805 | return foo1(3); |
5a69278c | 806 | })();") |
083b7f89 | 807 | |
058f137f VS |
808 | (test-ps-js labels2 |
809 | (labels ((foo (x) (return (1+ (bar x)))) | |
810 | (bar (y) (return (+ 2 (foo y))))) | |
811 | (bar (foo 1))) | |
812 | "var foo1 = function (x) { | |
813 | return bar2(x) + 1; | |
814 | }; | |
815 | var bar2 = function (y) { | |
816 | return 2 + foo1(y); | |
817 | }; | |
818 | bar2(foo1(1));") | |
819 | ||
820 | (test-ps-js labels3 | |
821 | (labels ((foo (x) (return (1+ x))) | |
822 | (bar (y) (return (+ 2 (foo y))))) | |
823 | (bar (foo 1))) | |
824 | "var foo1 = function (x) { | |
825 | return x + 1; | |
826 | }; | |
827 | var bar2 = function (y) { | |
828 | return 2 + foo1(y); | |
829 | }; | |
830 | bar2(foo1(1));") | |
831 | ||
083b7f89 VS |
832 | (test-ps-js for-loop-var-init-exp |
833 | ((lambda (x) | |
834 | (return (do* ((y (if x 0 1) (1+ y)) | |
835 | (z 0 (1+ z))) | |
836 | ((= y 3) z)))) | |
837 | true) | |
838 | "(function (x) { | |
839 | return (function () { | |
840 | for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) { | |
841 | }; | |
842 | return z; | |
843 | })(); | |
5a69278c | 844 | })(true);") |
5288d666 VS |
845 | |
846 | (test-ps-js math-pi | |
847 | pi | |
5a69278c | 848 | "Math.PI;") |
fb469285 VS |
849 | |
850 | (test-ps-js literal-array | |
851 | '(1 2 3) | |
5a69278c | 852 | "[1, 2, 3];") |
fb469285 VS |
853 | |
854 | (test-ps-js literal-array-1 | |
855 | '(1 foo 3) | |
5a69278c | 856 | "[1, 'foo', 3];") |
ceb1f277 VS |
857 | |
858 | (test ps-lisp-expands-in-lexical-environment | |
859 | (is (string= "5;" (let ((x 5)) (ps (lisp x)))))) | |
860 | ||
861 | (test ps*-lisp-expands-in-null-lexical-environment | |
1cd3540f | 862 | (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x))))) |
ceb1f277 VS |
863 | |
864 | (test ps*-lisp-expands-in-dynamic-environment | |
865 | (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*))))))) | |
866 | ||
867 | (test ps-lisp-dynamic-environment | |
868 | (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*))))))) | |
4525e3cd VS |
869 | |
870 | (test-ps-js ps-js-target-version-keyword-test1 | |
871 | (defun foo (x y &key bar baz)) | |
872 | "function foo(x, y) { | |
16151f19 VS |
873 | var x1 = Array.prototype.indexOf.call(arguments, 'bar', 2); |
874 | var bar = -1 == x1 ? null : arguments[x1 + 1]; | |
875 | var x2 = Array.prototype.indexOf.call(arguments, 'baz', 2); | |
876 | var baz = -1 == x2 ? null : arguments[x2 + 1]; | |
5a69278c | 877 | };" |
4525e3cd | 878 | :js-target-version 1.6) |
20ba6d7a VS |
879 | |
880 | (test-ps-js nested-if-expressions1 | |
881 | (return (if (if x y z) a b)) | |
5a69278c | 882 | "return (x ? y : z) ? a : b;") |
20ba6d7a VS |
883 | |
884 | (test-ps-js nested-if-expressions2 | |
885 | (return (if x y (if z a b))) | |
5a69278c | 886 | "return x ? y : (z ? a : b);") |
5ffb1eba VS |
887 | |
888 | (test-ps-js let1 | |
889 | (let (x) | |
890 | (+ x x)) | |
16151f19 VS |
891 | "var x = null; |
892 | x + x;") | |
5ffb1eba VS |
893 | |
894 | (test-ps-js let2 | |
895 | (let ((x 1)) | |
896 | (+ x x)) | |
16151f19 VS |
897 | "var x = 1; |
898 | x + x;") | |
899 | ||
900 | (test-ps-js let-x-x | |
901 | (let ((x (1+ x))) | |
902 | (+ x x)) | |
903 | "var x1 = x + 1; | |
5ffb1eba VS |
904 | x1 + x1;") |
905 | ||
906 | (test-ps-js let3 | |
907 | (let ((x 1) | |
908 | (y 2)) | |
909 | (+ x x)) | |
16151f19 VS |
910 | "var x = 1; |
911 | var y = 2; | |
912 | x + x;") | |
5ffb1eba VS |
913 | |
914 | (test-ps-js let4 | |
915 | (let ((x 1) | |
916 | (y (1+ x))) | |
917 | (+ x y)) | |
918 | "var x1 = 1; | |
16151f19 VS |
919 | var y = x + 1; |
920 | x1 + y;") | |
5ffb1eba VS |
921 | |
922 | (test-ps-js let5 | |
923 | (let ((x 1)) | |
924 | (+ x 1) | |
925 | (let ((x (+ x 5))) | |
926 | (+ x 1)) | |
927 | (+ x 1)) | |
16151f19 VS |
928 | "var x = 1; |
929 | x + 1; | |
930 | var x1 = x + 5; | |
5ffb1eba | 931 | x1 + 1; |
16151f19 | 932 | x + 1;") |
5ffb1eba VS |
933 | |
934 | (test-ps-js let6 | |
935 | (let ((x 2)) | |
936 | (let ((x 1) | |
937 | (y (1+ x))) | |
938 | (+ x y))) | |
16151f19 VS |
939 | "var x = 2; |
940 | var x1 = 1; | |
941 | var y = x + 1; | |
942 | x1 + y;") | |
5ffb1eba VS |
943 | |
944 | (test-ps-js let-exp1 | |
945 | (lambda () | |
946 | (return (let (x) (+ x x)))) | |
947 | "function () { | |
16151f19 VS |
948 | var x; |
949 | return (x = null, x + x); | |
5a69278c | 950 | };") |
5ffb1eba VS |
951 | |
952 | (test-ps-js let*1 | |
953 | (let* ((x 1)) (+ x x)) | |
16151f19 VS |
954 | "var x = 1; |
955 | x + x;") | |
5ffb1eba VS |
956 | |
957 | (test-ps-js let*2 | |
958 | (let* ((x 1) | |
959 | (y (+ x 2))) | |
960 | (+ x y)) | |
16151f19 VS |
961 | "var x = 1; |
962 | var y = x + 2; | |
963 | x + y;") | |
5ffb1eba VS |
964 | |
965 | (test-ps-js let*3 | |
966 | (let ((x 3)) | |
967 | (let* ((x 1) | |
968 | (y (+ x 2))) | |
969 | (+ x y))) | |
16151f19 VS |
970 | "var x = 3; |
971 | var x1 = 1; | |
972 | var y = x1 + 2; | |
973 | x1 + y;") | |
5ffb1eba VS |
974 | |
975 | (test-ps-js let*4 | |
976 | (let ((x 3)) | |
977 | (let* ((y (+ x 2)) | |
978 | (x 1)) | |
979 | (+ x y))) | |
16151f19 VS |
980 | "var x = 3; |
981 | var y = x + 2; | |
982 | var x1 = 1; | |
983 | x1 + y;") | |
058f137f VS |
984 | |
985 | (test-ps-js symbol-macrolet-var | |
986 | (symbol-macrolet ((x y)) | |
987 | (var x)) | |
3bfb836a VS |
988 | "var y;") |
989 | ||
990 | (test-ps-js setf-conditional1 | |
991 | (setf x (unless (null a) (1+ a))) | |
992 | "x = a != null ? a + 1 : null;") | |
993 | ||
994 | (test-ps-js setf-let1 | |
995 | (setf x (let ((a 1)) a)) | |
16151f19 | 996 | "x = (a = 1, a);") |
3bfb836a VS |
997 | |
998 | (test-ps-js setf-let2 | |
999 | (setf x (let ((a (foo))) | |
1000 | (unless (null a) | |
1001 | (1+ a)))) | |
16151f19 | 1002 | "x = (a = foo(), a != null ? a + 1 : null);") |
5a69278c VS |
1003 | |
1004 | (test-ps-js symbol-macro-env1 | |
1005 | (symbol-macrolet ((bar 1)) | |
1006 | (macrolet ((bar (x y) `(+ ,x ,y))) | |
1007 | (bar bar bar))) | |
1008 | "1 + 1;") | |
1009 | ||
1010 | (test-ps-js symbol-macrolet-fun1 | |
1011 | (symbol-macrolet ((baz +)) | |
1012 | (baz 1 2)) | |
1013 | "baz(1, 2);") | |
1014 | ||
1015 | (test-ps-js lisp2-namespaces1 | |
1016 | (let ((list nil)) | |
1017 | (setf list (list 1 2 3))) | |
16151f19 VS |
1018 | "var list = null; |
1019 | list = [1, 2, 3];") | |
1020 | ||
1021 | (test-ps-js let-shadows-symbol-macrolet | |
1022 | (symbol-macrolet ((x y)) | |
1023 | (let ((x 1)) | |
1024 | (+ x x)) | |
1025 | (+ x x)) | |
1026 | "var x1 = 1; | |
1027 | x1 + x1; | |
1028 | y + y;") | |
1029 | ||
1030 | (test-ps-js let-rename-optimization1 | |
1031 | (let ((x 1)) | |
1032 | (+ x x)) | |
1033 | "var x = 1; | |
1034 | x + x;") | |
1035 | ||
1036 | (test-ps-js let-rename-optimization2 | |
1037 | (lambda (x) | |
1038 | (let ((x (+ 1 x))) | |
1039 | (return x))) | |
1040 | "function (x) { | |
1041 | var x1 = 1 + x; | |
1042 | return x1; | |
1043 | };") |