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 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;") | |
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)) | |
43 | "('' + x).toString()") | |
44 | ||
45 | (test-ps-js method-call-op-form-args | |
46 | ((@ (+ "" x) to-string) 1 2 :baz 3) | |
47 | "('' + x).toString(1, 2, { baz : 3 })") | |
48 | ||
49 | (test-ps-js method-call-number | |
50 | ((@ 10 to-string)) | |
51 | "( 10 ).toString()") | |
52 | ||
53 | (test-ps-js method-call-string | |
54 | ((@ "hi" to-string)) | |
55 | "'hi'.toString()") | |
e2932347 | 56 | |
e2932347 | 57 | (test-ps-js method-call-lit-object |
79630c82 VS |
58 | ((@ (create :to-string (lambda () (return "it works"))) to-string)) |
59 | "( { toString : function () { return 'it works'; } } ).toString()") | |
60 | ||
61 | (test-ps-js method-call-conditional | |
62 | ((if a x y) 1) | |
63 | "(a ? x : y)(1)") | |
e2932347 LC |
64 | |
65 | (test-ps-js method-call-variable | |
79630c82 VS |
66 | ((@ x to-string)) |
67 | "x.toString()") | |
e2932347 LC |
68 | |
69 | (test-ps-js method-call-array | |
79630c82 VS |
70 | ((@ (list 10 20) to-string)) |
71 | "[ 10, 20 ].toString()") | |
72 | ||
e2932347 | 73 | (test-ps-js method-call-fn-call |
79630c82 VS |
74 | ((@ (foo) to-string)) |
75 | "foo().toString()") | |
76 | ||
e2932347 | 77 | (test-ps-js method-call-lambda-fn |
79630c82 VS |
78 | ((@ (lambda () (alert 10)) to-string)) |
79 | "( function () { alert(10); } ).toString()") | |
80 | ||
e2932347 | 81 | (test-ps-js method-call-lambda-call |
79630c82 VS |
82 | ((@ ((lambda (x) (return x)) 10) to-string)) |
83 | "(function (x) { return x; })(10).toString()") | |
e2932347 LC |
84 | |
85 | (test no-whitespace-before-dot | |
79630c82 | 86 | (let* ((str (ps1* '((@ ((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 |
58c4ef4f | 93 | (let* ((foo (create :a 1))) |
f2bb932e | 94 | (alert (slot-value foo 'a))) |
07afea38 VS |
95 | "var foo = { a : 1 }; |
96 | alert(foo.a);") | |
e2932347 LC |
97 | |
98 | (test-ps-js buggy-slot-value | |
58c4ef4f | 99 | (let* ((foo (create :a 1)) |
f2bb932e | 100 | (slot-name "a")) |
e2932347 | 101 | (alert (slot-value foo slot-name))) |
07afea38 | 102 | " var foo = { a : 1 }; |
e2932347 LC |
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)) | |
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]) { | |
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'); | |
158 | }") | |
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'); | |
169 | }") | |
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 | |
cb8f8e58 | 184 | for generated = (ps1* `(let* ((x ,(format nil "hello~ahi" lisp-char))))) |
b508414b TC |
185 | for wanted = (format nil "var x = 'hello\\~ahi';" js-escape) |
186 | do (is (string= (normalize-js-code generated) wanted))))) | |
9da682ca | 187 | |
e0f0d152 VS |
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") | |
cf460f93 VS |
195 | |
196 | (test-ps-js slot-value-setf | |
197 | (setf (slot-value x 'y) (+ (+ a 3) 4)) | |
72332f2a | 198 | "x.y = (a + 3) + 4;") |
eb0befe2 VS |
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 | ||
245d0fbd | 208 | (test script-star-eval1 |
07afea38 | 209 | (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2)))))) |
245d0fbd VS |
210 | |
211 | (test script-star-eval2 | |
07afea38 | 212 | (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1)))))) |
62b4a867 VS |
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 | ||
46f794a4 RD |
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 | ||
62b4a867 VS |
230 | (test-ps-js quoted-nil |
231 | 'nil | |
72332f2a VS |
232 | "null") |
233 | ||
5af33a27 VS |
234 | (test-ps-js defsetf1 |
235 | (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval)) | |
236 | (setf (baz 1 2) 3)) | |
237 | "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);") | |
238 | ||
239 | (test-ps-js defsetf-short | |
240 | (progn (defsetf baz set-baz "docstring") | |
241 | (setf (baz 1 2 3) "foo")) | |
242 | "setBaz(1, 2, 3, 'foo');") | |
243 | ||
244 | (test-ps-js defun-setf1 | |
245 | (progn (defun (setf some-thing) (new-val i1 i2) | |
246 | (setf (aref *some-thing* i1 i2) new-val)) | |
247 | (setf (some-thing 1 2) "foo")) | |
248 | "function __setf_someThing(newVal, i1, i2) { | |
249 | SOMETHING[i1][i2] = newVal; | |
250 | }; | |
251 | var _js3 = 1; | |
252 | var _js4 = 2; | |
253 | var _js2 = 'foo'; | |
254 | __setf_someThing(_js2, _js3, _js4);") | |
dbb7017b | 255 | |
d989d711 VS |
256 | (test-ps-js defun-optional1 |
257 | (defun test-opt (&optional x) (return (if x "yes" "no"))) | |
258 | "function testOpt(x) { | |
0e198f66 TC |
259 | if (x === undefined) { |
260 | x = null; | |
261 | }; | |
262 | return x ? 'yes' : 'no'; | |
a2434734 VS |
263 | }") |
264 | ||
5ac90695 VS |
265 | (test-ps-js defun-optional2 |
266 | (defun foo (x &optional y) (+ x y)) | |
267 | "function foo(x, y) { | |
0e198f66 TC |
268 | if (y === undefined) { |
269 | y = null; | |
270 | }; | |
5ac90695 VS |
271 | x + y; |
272 | }") | |
273 | ||
7be02bed VS |
274 | (test-ps-js defun-optional3 |
275 | (defun blah (&optional (x 0)) | |
276 | (return x)) | |
277 | "function blah(x) { | |
278 | if (x === undefined) { | |
279 | x = 0; | |
280 | }; | |
281 | return x; | |
282 | }") | |
283 | ||
a2434734 VS |
284 | (test-ps-js return-nothing |
285 | (return) | |
286 | "return null") | |
dbb7017b VS |
287 | |
288 | (test-ps-js set-timeout | |
289 | (do-set-timeout (10) (alert "foo")) | |
b44afd8f | 290 | "setTimeout(function () { alert('foo'); }, 10)") |
07afea38 VS |
291 | (test-ps-js operator-precedence |
292 | (* 3 (+ 4 5) 6) | |
293 | "3 * (4 + 5) * 6") | |
294 | ||
2d2c6dc2 TC |
295 | (test-ps-js operators-1 |
296 | (in prop obj) | |
297 | "prop in obj") | |
298 | ||
07afea38 VS |
299 | (test-ps-js incf1 |
300 | (incf foo bar) | |
301 | "foo += bar") | |
302 | ||
303 | (test-ps-js decf1 | |
304 | (decf foo bar) | |
305 | "foo -= bar") | |
306 | ||
49c50da4 VS |
307 | (test-ps-js incf2 |
308 | (incf x 5) | |
309 | "x += 5") | |
310 | ||
311 | (test-ps-js decf2 | |
312 | (decf y 10) | |
313 | "y -= 10") | |
314 | ||
07afea38 VS |
315 | (test-ps-js setf-conditional |
316 | (setf foo (if x 1 2)) | |
317 | "foo = x ? 1 : 2;") | |
318 | ||
319 | (test-ps-js obj-literal-numbers | |
320 | (create 1 "foo") | |
321 | "{ 1 : 'foo' }") | |
322 | ||
323 | (test-ps-js obj-literal-strings | |
324 | (create "foo" 2) | |
a805b30d VS |
325 | "{ 'foo' : 2 }") |
326 | ||
327 | (test-ps-js slot-value-string | |
328 | (slot-value foo "bar") | |
329 | "foo['bar']") | |
b44afd8f | 330 | |
79630c82 VS |
331 | (test-ps-js slot-value-string1 |
332 | (slot-value "bar" 'length) | |
333 | "'bar'.length") | |
334 | ||
b44afd8f | 335 | (test-ps-js slot-value-progn |
13b8268e VS |
336 | (slot-value (progn (some-fun "abc") "123") "length") |
337 | "(someFun('abc'), '123')['length']") | |
b44afd8f VS |
338 | |
339 | (test-ps-js method-call-block | |
79630c82 | 340 | ((@ (progn (some-fun "abc") "123") to-string)) |
13b8268e | 341 | "(someFun('abc'), '123').toString()") |
b44afd8f VS |
342 | |
343 | (test-ps-js create-blank | |
344 | (create) | |
345 | "{ }") | |
346 | ||
347 | (test-ps-js blank-object-literal | |
5ac90695 | 348 | {} |
667e3784 | 349 | "{ }") |
44934751 | 350 | |
06ed0d3a VS |
351 | (test-ps-js array-literal1 |
352 | [] | |
353 | "[]") | |
354 | ||
355 | (test-ps-js array-literal2 | |
356 | ([]) | |
357 | "[]") | |
358 | ||
359 | (test-ps-js array-literal3 | |
360 | ([] 1 2 3) | |
361 | "[1, 2, 3]") | |
362 | ||
363 | (test-ps-js array-literal4 | |
364 | ([] 1 (2 3)) | |
365 | "[1, [2, 3]]") | |
366 | ||
367 | (test-ps-js array-literal5 | |
368 | ([] (1 2) ("a" "b")) | |
369 | "[[1, 2], ['a', 'b']]") | |
370 | ||
44934751 VS |
371 | (test-ps-js defun-rest1 |
372 | (defun foo (&rest bar) (alert bar[1])) | |
373 | "function foo() { | |
374 | var bar = []; | |
d777a405 | 375 | for (var i2 = 0; i2 < arguments.length - 0; i2 += 1) { |
b5e0bcb7 | 376 | bar[i2] = arguments[i2 + 0]; |
44934751 VS |
377 | }; |
378 | alert(bar[1]); | |
379 | }") | |
380 | ||
381 | (test-ps-js defun-rest2 | |
382 | (defun foo (baz &rest bar) (return (+ baz (aref bar 1)))) | |
2e593e4c | 383 | "function foo(baz) { |
44934751 | 384 | var bar = []; |
d777a405 | 385 | for (var i2 = 0; i2 < arguments.length - 1; i2 += 1) { |
b5e0bcb7 | 386 | bar[i2] = arguments[i2 + 1]; |
44934751 VS |
387 | }; |
388 | return baz + bar[1]; | |
389 | }") | |
2e593e4c VS |
390 | |
391 | (test-ps-js defun-keyword1 | |
392 | (defun zoo (foo bar &key baz) (return (+ foo bar baz))) | |
393 | "function zoo(foo, bar, _js1) { | |
0e198f66 TC |
394 | if (_js1 === undefined) { |
395 | _js1 = { }; | |
396 | }; | |
2e593e4c VS |
397 | return foo + bar + _js1.baz; |
398 | }") | |
b0f64e9b | 399 | |
f2bb932e VS |
400 | (test-ps-js defun-keyword2 |
401 | (defun zoo (&key baz) (return (* baz baz))) | |
402 | "function zoo(_js1) { | |
403 | if (_js1 === undefined) { | |
404 | _js1 = { }; | |
405 | }; | |
406 | return _js1.baz * _js1.baz; | |
407 | }") | |
408 | ||
409 | (test-ps-js defun-keyword3 | |
410 | (defun zoo (&key baz (bar 4)) (return (* baz bar))) | |
411 | "function zoo(_js1) { | |
412 | if (_js1 === undefined) { | |
413 | _js1 = { }; | |
414 | }; | |
415 | if (_js1.bar === undefined) { | |
416 | _js1.bar = 4; | |
417 | }; | |
418 | return _js1.baz * _js1.bar; | |
419 | }") | |
420 | ||
421 | (test-ps-js keyword-funcall1 | |
422 | (func :baz 1) | |
423 | "func({ baz : 1 })") | |
424 | ||
425 | (test-ps-js keyword-funcall2 | |
426 | (func :baz 1 :bar foo) | |
427 | "func({ baz : 1, bar : foo })") | |
428 | ||
429 | (test-ps-js keyword-funcall3 | |
430 | (fun a b :baz c) | |
431 | "fun(a, b, { baz : c })") | |
432 | ||
b0f64e9b VS |
433 | (test-ps-js cond1 |
434 | (cond ((= x 1) 1)) | |
435 | "if (x == 1) { | |
436 | 1; | |
437 | }") | |
438 | ||
439 | (test-ps-js cond2 | |
13b8268e | 440 | (cond ((= x 1) 2) ((= y (* x 4)) (foo "blah") (* x y))) |
b0f64e9b VS |
441 | "if (x == 1) { |
442 | 2; | |
443 | } else if (y == x * 4) { | |
13b8268e | 444 | foo('blah'); |
b0f64e9b VS |
445 | x * y; |
446 | }") | |
5705b542 VS |
447 | |
448 | (test-ps-js if-exp-without-else-returns-null | |
449 | (return (if x 1)) | |
450 | "return x ? 1 : null") | |
451 | ||
452 | (test-ps-js progn-expression-single-statement | |
453 | (return (progn (* x y))) | |
454 | "return x * y") | |
0949f072 VS |
455 | |
456 | (test-ps-js cond-expression1 | |
13b8268e | 457 | (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5))))) |
0949f072 | 458 | "function foo() { |
13b8268e | 459 | return 1 < 2 ? (bar('foo'), 4 * 5) : null; |
0949f072 VS |
460 | }") |
461 | ||
462 | (test-ps-js cond-expression2 | |
463 | (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar")))) | |
464 | "function foo() { | |
465 | return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null); | |
466 | }") | |
467 | ||
468 | (test-ps-js cond-expression-final-t-clause | |
13b8268e | 469 | (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo")))) |
0949f072 | 470 | "function foo() { |
13b8268e | 471 | return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo')); |
0949f072 VS |
472 | }") |
473 | ||
474 | (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning? | |
475 | (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar")))) | |
476 | "function foo() { | |
477 | return 2 < 1 ? 5 : 'foo'; | |
478 | }") | |
e5253c5b VS |
479 | |
480 | (test-ps-js funcall-if-expression | |
481 | (document.write | |
482 | (if (= *linkornot* 1) | |
483 | (ps-html ((:a :href "#" | |
e69d0a12 | 484 | :onclick (ps-inline (transport))) |
e5253c5b VS |
485 | img)) |
486 | img)) | |
e69d0a12 | 487 | "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport()') + '\">' + img + '</A>' : img)") |
49c50da4 VS |
488 | |
489 | (test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test! | |
490 | (- 1) | |
491 | "-1") | |
efe35e12 VS |
492 | |
493 | (test macro-environment1 | |
58c4ef4f | 494 | (is (string= (normalize-js-code (let* ((macroname (gensym))) |
efe35e12 VS |
495 | (ps* `(defmacro ,macroname (x) `(+ ,x 123)) |
496 | `(defun test1 () | |
497 | (macrolet ((,macroname (x) `(aref data ,x))) | |
498 | (when (,macroname x) | |
499 | (setf (,macroname x) 123))))))) | |
500 | (normalize-js-code | |
501 | "function test1() { | |
502 | if (data[x]) { | |
503 | data[x] = 123; | |
504 | }; | |
505 | }; | |
506 | ")))) | |
921f2e02 | 507 | |
8cfc6fe9 VS |
508 | (test macro-environment2 |
509 | (is (string= (normalize-js-code (let ((outer-lexical-variable 1)) | |
510 | (defpsmacro macro-environment2-macro (x) | |
511 | `(+ ,outer-lexical-variable ,x)) | |
512 | (ps* '(macro-environment2-macro 2)))) | |
513 | (normalize-js-code "1 + 2;")))) | |
514 | ||
921f2e02 VS |
515 | (test-ps-js ampersand-whole-1 |
516 | (macrolet ((foo (&whole foo bar baz) | |
517 | (declare (ignore bar baz)) | |
518 | (format nil "~a" foo))) | |
519 | (foo 1 2)) | |
6e8ff198 VS |
520 | "'(FOO 1 2)';") |
521 | ||
522 | (test-ps-js keyword-consistent | |
523 | :x | |
f2bb932e | 524 | "'x'") |
43a1d5c3 VS |
525 | |
526 | (test-ps-js simple-symbol-macrolet | |
527 | (symbol-macrolet ((x 1)) x) | |
528 | "1;") | |
529 | ||
530 | (test-ps-js compound-symbol-macrolet | |
531 | (symbol-macrolet ((x 123) | |
532 | (y (* 2 x))) | |
533 | y) | |
534 | "2 * 123;") | |
1af04ae5 VS |
535 | |
536 | (test-ps-js define-symbol-macro | |
537 | (progn (define-symbol-macro tst-sym-macro 2) | |
538 | tst-sym-macro) | |
539 | "2;") | |
e0032a96 VS |
540 | |
541 | (test-ps-js expression-progn | |
542 | (defun f () (return (progn (foo) (if x 1 2)))) | |
543 | "function f() { | |
544 | return (foo(), x ? 1 : 2); | |
545 | }") | |
546 | ||
547 | (test-ps-js let-decl-in-expression | |
58c4ef4f | 548 | (defun f (x) (return (if x 1 (let* ((foo x)) foo)))) |
e0032a96 VS |
549 | "function f(x) { |
550 | var foo; | |
551 | return x ? 1 : (foo = x, foo); | |
552 | }") | |
58c4ef4f VS |
553 | |
554 | (test-ps-js special-var1 | |
555 | (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2))) | |
556 | "var FOO; | |
557 | var tempstackvar1; | |
558 | try { | |
559 | tempstackvar1 = FOO; | |
560 | FOO = 2; | |
561 | FOO * 2; | |
562 | } finally { | |
563 | FOO = tempstackvar1; | |
d777a405 | 564 | delete tempstackvar1; |
58c4ef4f VS |
565 | };") |
566 | ||
567 | (test-ps-js special-var2 | |
568 | (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*))) | |
569 | "var FOO; | |
570 | var BAZ = 3; | |
571 | var tempstackvar1; | |
572 | try { | |
573 | tempstackvar1 = FOO; | |
574 | FOO = 2; | |
575 | FOO * 2 * BAZ; | |
576 | } finally { | |
577 | FOO = tempstackvar1; | |
d777a405 TC |
578 | delete tempstackvar1; |
579 | }; | |
580 | ") | |
5ac90695 VS |
581 | |
582 | (test-ps-js literal1 | |
583 | (setf x undefined) | |
584 | "x = undefined;") | |
585 | ||
586 | (test-ps-js literal2 | |
587 | (aref this x) | |
588 | "this[x]") | |
d89456ee VS |
589 | |
590 | (test-ps-js setf-dec1 | |
591 | (setf x (- 1 x 2)) | |
592 | "x = 1 - x - 2;") | |
593 | ||
594 | (test-ps-js setf-dec2 | |
595 | (setf x (- x 1 2)) | |
596 | "x = x - 1 - 2;") | |
93e99720 VS |
597 | |
598 | (test-ps-js special-char-equals | |
599 | blah= | |
600 | "blahequals") | |
7be02bed VS |
601 | |
602 | (test-ps-js setf-operator-priority | |
603 | (return (or (slot-value cache id) | |
604 | (setf (slot-value cache id) (document.get-element-by-id id)))) | |
605 | "return cache[id] || (cache[id] = document.getElementById(id))") | |
606 | ||
607 | (test-ps-js aref-operator-priority | |
608 | (aref (if (and x (> (length x) 0)) | |
609 | (aref x 0) | |
610 | y) | |
611 | z) | |
612 | "(x && x.length > 0 ? x[0] : y)[z]") | |
613 | ||
614 | (test-ps-js aref-operator-priority1 | |
615 | (aref (or (slot-value x 'y) | |
616 | (slot-value a 'b)) | |
617 | z) | |
618 | "(x.y || a.b)[z]") | |
619 | ||
620 | (test-ps-js aref-operator-priority2 | |
621 | (aref (if a b c) 0) | |
622 | "(a ? b : c)[0]") | |
623 | ||
624 | (test-ps-js negative-operator-priority | |
625 | (- (if x y z)) | |
626 | "-(x ? y : z)") | |
627 | ||
628 | (test-ps-js op-p1 | |
629 | (new (or a b)) | |
630 | "new (a || b)") | |
631 | ||
632 | (test-ps-js op-p2 | |
633 | (delete (if a (or b c) d)) | |
634 | "delete (a ? b || c : d)") | |
635 | ||
636 | (test-ps-js op-p3 | |
637 | (not (if (or x (not y)) z)) | |
638 | "!(x || !y ? z : null)") | |
639 | ||
640 | (test-ps-js op-p4 | |
641 | (- (- (* 1 2) 3)) | |
642 | "-(1 * 2 - 3)") | |
643 | ||
644 | (test-ps-js op-p5 | |
645 | (instanceof (or a b) (if x y z)) | |
646 | "((a || b) instanceof (x ? y : z))") | |
647 | ||
648 | (test-ps-js op-p6 | |
649 | (doeach (x (or a b))) | |
650 | "for (var x in (a || b)) { };") | |
651 | ||
652 | (test-ps-js op-p7 | |
653 | (or x (if (= x 0) "zero" "empty")) | |
654 | "x || (x == 0 ? 'zero' : 'empty')") | |
655 | ||
656 | (test-ps-js named-op-expression | |
657 | (throw (if a b c)) | |
658 | "throw a ? b : c") | |
659 | ||
660 | (test-ps-js named-op-expression1 | |
661 | (typeof (or x y)) | |
662 | "typeof (x || y)") | |
9b31227f VS |
663 | |
664 | (test-ps-js aref-array-expression | |
665 | (aref (or a b c) 0) | |
666 | "(a || b || c)[0]") | |
667 | ||
668 | (test-ps-js slot-value-operator | |
669 | (slot-value (or a b c) 'd) | |
670 | "(a || b || c).d") | |
671 | ||
672 | (test-ps-js slot-value-parens | |
673 | (slot-value (slot-value foo 'bar) 'baz) | |
674 | "foo.bar.baz") | |
9496b3a4 VS |
675 | |
676 | (test-ps-js funcall-funcall | |
677 | ((foo)) | |
678 | "foo()()") | |
679 | ||
680 | (test-ps-js expression-funcall | |
681 | ((or (@ window eval) eval) foo nil) | |
682 | "(window.eval || eval)(foo, null)") | |
683 | ||
684 | (test-ps-js expression-funcall1 | |
685 | (((or (@ window eval) eval) foo nil)) | |
686 | "(window.eval || eval)(foo, null)()") | |
687 | ||
688 | (test-ps-js expression-funcall2 | |
689 | (((or (@ window eval) eval)) foo nil) | |
690 | "(window.eval || eval)()(foo, null)") | |
691 | ||
79630c82 VS |
692 | (test-ps-js slot-value-object-literal |
693 | (slot-value (create :a 1) 'a) | |
694 | "({ a : 1 }).a") | |
695 | ||
696 | (test-ps-js slot-value-lambda | |
697 | (slot-value (lambda ()) 'prototype) | |
698 | "(function () { }).prototype") | |
dde6e656 VS |
699 | |
700 | (test-ps-js who-html1 | |
701 | (who-ps-html (:span :class "ticker-symbol" | |
702 | :ticker-symbol symbol | |
703 | (:a :href "http://foo.com" | |
704 | symbol) | |
705 | (:span :class "ticker-symbol-popup"))) | |
706 | "'<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"' + symbol + '\"><A HREF=\"http://foo.com\">' + symbol + '</A><SPAN CLASS=\"ticker-symbol-popup\"/></SPAN>'") | |
ef3be63e VS |
707 | |
708 | (test-ps-js flet1 | |
709 | ((lambda () (flet ((foo (x) (return (1+ x)))) (return (foo 1))))) | |
710 | "(function () { | |
711 | var foo = function (x) { | |
712 | return x + 1; | |
713 | }; | |
714 | return foo(1); | |
715 | })()") | |
716 | ||
717 | (test-ps-js labels1 | |
718 | ((lambda () (labels ((foo (x) | |
719 | (return (if (=== 0 x) | |
720 | 0 | |
721 | (+ x (foo (1- x))))))) | |
722 | (return (foo 3))))) | |
723 | "(function () { | |
724 | var foo = function foo(x) { | |
725 | return 0 === x ? 0 : x + foo(x - 1); | |
726 | }; | |
727 | return foo(3); | |
728 | })()") | |
083b7f89 VS |
729 | |
730 | (test-ps-js for-loop-var-init-exp | |
731 | ((lambda (x) | |
732 | (return (do* ((y (if x 0 1) (1+ y)) | |
733 | (z 0 (1+ z))) | |
734 | ((= y 3) z)))) | |
735 | true) | |
736 | "(function (x) { | |
737 | return (function () { | |
738 | for (var y = x ? 0 : 1, z = 0; y != 3; y += 1, z += 1) { | |
739 | }; | |
740 | return z; | |
741 | })(); | |
742 | })(true)") | |
5288d666 VS |
743 | |
744 | (test-ps-js math-pi | |
745 | pi | |
746 | "Math.PI") |