Deprecated WITH special operator.
[parenscript.git] / t / reference-tests.lisp
blob746d05d8625dc0bfb227dae9a66b7f0617e9a9a7
1 (in-package #:parenscript-test)
3 ;;; These tests were originally generated from the reference manual
5 (in-suite ref-tests)
7 (test-ps-js statements-and-expressions-1
8 (+ i (if 1 2 3))
9 "i + (1 ? 2 : 3);")
11 (test-ps-js statements-and-expressions-2
12 (if 1 2 3)
13 "if (1) {
15 } else {
17 };")
19 (test-ps-js symbol-conversion-1
20 !?#@%
21 "bangwhathashatpercent;")
23 (test-ps-js symbol-conversion-2
24 bla-foo-bar
25 "blaFooBar;")
27 (test-ps-js symbol-conversion-3
28 *array
29 "Array;")
31 (test-ps-js symbol-conversion-4
32 *global-array*
33 "GLOBALARRAY;")
35 (test-ps-js number-literals-1
37 "1;")
39 (test-ps-js number-literals-2
40 123.123
41 "123.123;")
43 (test-ps-js number-literals-3
44 #x10
45 "16;")
47 (test-ps-js string-literals-1
48 "foobar"
49 "'foobar';")
51 (test-ps-js string-literals-2
52 "bratzel bub"
53 "'bratzel bub';")
55 (test-ps-js string-literals-3
56 " "
57 "'\\t';")
59 (test-ps-js array-literals-1
60 (array)
61 "[ ];")
63 (test-ps-js array-literals-2
64 (array 1 2 3)
65 "[ 1, 2, 3 ];")
67 (test-ps-js array-literals-3
68 (array (array 2 3)
69 (array "foobar" "bratzel bub"))
70 "[ [ 2, 3 ], [ 'foobar', 'bratzel bub' ] ];")
72 (test-ps-js array-literals-4
73 (make-array)
74 "new Array();")
76 (test-ps-js array-literals-5
77 (make-array 1 2 3)
78 "new Array(1, 2, 3);")
80 (test-ps-js array-literals-6
81 (make-array
82 (make-array 2 3)
83 (make-array "foobar" "bratzel bub"))
84 "new Array(new Array(2, 3), new Array('foobar', 'bratzel bub'));")
86 (test-ps-js object-literals-1
87 (create foo "bar" :blorg 1)
88 "{ foo : 'bar', 'blorg' : 1 };")
90 (test-ps-js object-literals-2
91 (create foo "hihi"
92 blorg (array 1 2 3)
93 another-object (create :schtrunz 1))
94 "{ foo : 'hihi',
95 blorg : [ 1, 2, 3 ],
96 anotherObject : { 'schtrunz' : 1 } };")
98 (test-ps-js object-literals-3
99 (getprop an-object 'foo)
100 "anObject.foo;")
102 (test-ps-js object-literals-4
103 (@ an-object foo bar)
104 "anObject.foo.bar;")
106 (test-ps-js object-literals-5
107 (with-slots (a b c) this
108 (+ a b c))
109 "this.a + this.b + this.c;")
111 (test-ps-js regular-expression-literals-1
112 (regex "foobar")
113 "/foobar/;")
115 (test-ps-js regular-expression-literals-2
116 (regex "/foobar/i")
117 "/foobar/i;")
119 (test-ps-js literal-symbols-1
121 "true;")
123 (test-ps-js literal-symbols-2
124 FALSE
125 "false;")
127 (test-ps-js literal-symbols-3
129 "false;")
131 (test-ps-js literal-symbols-4
132 (lambda () NIL)
133 "function () {
134 return null;
135 };")
137 (test-ps-js literal-symbols-5
138 UNDEFINED
139 "undefined;")
141 (test-ps-js literal-symbols-6
142 THIS
143 "this;")
145 (test-ps-js variables-1
146 variable
147 "variable;")
149 (test-ps-js variables-2
150 a-variable
151 "aVariable;")
153 (test-ps-js variables-3
154 *math
155 "Math;")
157 (test-ps-js function-calls-and-method-calls-1
158 (blorg 1 2)
159 "blorg(1, 2);")
161 (test-ps-js function-calls-and-method-calls-2
162 (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
163 "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ]);")
165 (test-ps-js function-calls-and-method-calls-3
166 ((getprop this 'blorg) 1 2)
167 "this.blorg(1, 2);")
169 (test-ps-js function-calls-and-method-calls-4
170 ((aref foo i) 1 2)
171 "foo[i](1, 2);")
173 (test-ps-js function-calls-and-method-calls-5
174 ((getprop (aref foobar 1) 'blorg) NIL T)
175 "foobar[1].blorg(null, true);")
177 (test-ps-js operator-expressions-1
178 (* 1 2)
179 "1 * 2;")
181 (test-ps-js operator-expressions-2
182 (= 1 2)
183 "1 === 2;")
185 (test-ps-js operator-expressions-3
186 (* 1 (+ 2 3 4) 4 (/ 6 7))
187 "1 * (2 + 3 + 4) * 4 * 6 / 7;")
189 (test-ps-js operator-expressions-4
190 (incf i)
191 "++i;")
193 (test-ps-js operator-expressions-5
194 (decf i)
195 "--i;")
197 (test-ps-js operator-expressions-6
198 (1- i)
199 "i - 1;")
201 (test-ps-js operator-expressions-7
202 (1+ i)
203 "i + 1;")
205 (test-ps-js operator-expressions-8
206 (not (< i 2))
207 "i >= 2;")
209 (test-ps-js body-forms-1
210 (progn (blorg i) (blafoo i))
211 "blorg(i);
212 blafoo(i);")
214 (test-ps-js body-forms-2
215 (+ i (progn (blorg i) (blafoo i)))
216 "i + (blorg(i), blafoo(i));")
218 (test-ps-js function-definition-1
219 (defun a-function (a b)
220 (+ a b))
221 "function aFunction(a, b) {
222 return a + b;
223 };")
225 (test-ps-js function-definition-2
226 (lambda (a b) (+ a b))
227 "function (a, b) {
228 return a + b;
229 };")
231 (test-ps-js assignment-1
232 (setf a 1)
233 "a = 1;")
235 (test-ps-js assignment-2
236 (setf a 2 b 3 c 4 x (+ a b c))
237 "a = 2;
238 b = 3;
239 c = 4;
240 x = a + b + c;")
242 (test-ps-js assignment-3
243 (setf a (+ a 2 3 4 a))
244 "a = a + 2 + 3 + 4 + a;")
246 (test-ps-js assignment-4
247 (setf a (- 1 a))
248 "a = 1 - a;")
250 (test-ps-js assignment-5
251 (let ((a 1) (b 2))
252 (psetf a b b a))
253 "var a = 1;
254 var b = 2;
255 var _js1 = b;
256 var _js2 = a;
257 a = _js1;
258 b = _js2;")
260 (test-ps-js assignment-6
261 (setq a 1)
262 "a = 1;")
264 (test-ps-js assignment-8
265 (progn
266 (defun (setf color) (new-color el)
267 (setf (getprop (getprop el 'style) 'color) new-color))
268 (setf (color some-div) (+ 23 "em")))
269 "function __setf_color(newColor, el) {
270 return el.style.color = newColor;
272 __setf_color(23 + 'em', someDiv);")
274 (test-ps-js assignment-10
275 (progn
276 (defsetf left (el) (offset)
277 `(setf (getprop (getprop ,el 'style) 'left) ,offset))
278 (setf (left some-div) (+ 123 "px")))
279 "var _js2 = someDiv;
280 var _js1 = 123 + 'px';
281 _js2.style.left = _js1;")
283 (test-ps-js assignment-12
284 (macrolet ((left (el)
285 `(getprop ,el 'offset-left)))
286 (left some-div))
287 "someDiv.offsetLeft;")
289 (test-ps-js single-argument-statements-1
290 (return 1)
291 "return 1;")
293 (test-ps-js single-argument-statements-2
294 (throw "foobar")
295 "throw 'foobar';")
297 (test-ps-js single-argument-expression-1
298 (delete (new (*foobar 2 3 4)))
299 "delete new Foobar(2, 3, 4);")
301 (test-ps-js single-argument-expression-2
302 (if (= (typeof blorg) *string)
303 (alert (+ "blorg is a string: " blorg))
304 (alert "blorg is not a string"))
305 "if (typeof blorg === String) {
306 alert('blorg is a string: ' + blorg);
307 } else {
308 alert('blorg is not a string');
309 };")
311 (test-ps-js conditional-statements-1
312 (if ((@ blorg is-correct))
313 (progn (carry-on) (return i))
314 (alert "blorg is not correct!"))
315 "if (blorg.isCorrect()) {
316 carryOn();
317 return i;
318 } else {
319 alert('blorg is not correct!');
320 };")
322 (test-ps-js conditional-statements-2
323 (+ i (if ((@ blorg add-one)) 1 2))
324 "i + (blorg.addOne() ? 1 : 2);")
326 (test-ps-js conditional-statements-3
327 (when ((@ blorg is-correct))
328 (carry-on)
329 (return i))
330 "if (blorg.isCorrect()) {
331 carryOn();
332 return i;
333 };")
335 (test-ps-js conditional-statements-4
336 (unless ((@ blorg is-correct))
337 (alert "blorg is not correct!"))
338 "if (!blorg.isCorrect()) {
339 alert('blorg is not correct!');
340 };")
342 (test-ps-js variable-declaration-1
343 (defvar *a* (array 1 2 3))
344 "var A = [ 1, 2, 3 ];")
346 (test-ps-js variable-declaration-2
347 (progn
348 (defvar *a* 4)
349 (let ((x 1)
350 (*a* 2))
351 (let* ((y (+ x 1))
352 (x (+ x y)))
353 (+ *a* x y))))
354 "var A = 4;
355 var x = 1;
356 var A_TMPSTACK1;
357 try {
358 A_TMPSTACK1 = A;
359 A = 2;
360 var y = x + 1;
361 var x2 = x + y;
362 A + x2 + y;
363 } finally {
364 A = A_TMPSTACK1;
365 };")
367 (test-ps-js iteration-constructs-1
368 (do* ((a) b (c (array "a" "b" "c" "d" "e"))
369 (d 0 (1+ d))
370 (e (aref c d) (aref c d)))
371 ((or (= d (@ c length)) (string= e "x")))
372 (setf a d b e)
373 (funcall (@ document write) (+ "a: " a " b: " b "<br/>")))
374 "for (var a = null, b = null, c = ['a', 'b', 'c', 'd', 'e'], d = 0, e = c[d]; !(d === c.length || e === 'x'); d += 1, e = c[d]) {
375 a = d;
376 b = e;
377 document.write('a: ' + a + ' b: ' + b + '<br/>');
378 };")
380 (test-ps-js iteration-constructs-2
381 (do ((i 0 (1+ i))
382 (s 0 (+ s i (1+ i))))
383 ((> i 10))
384 (funcall (@ document write) (+ "i: " i " s: " s "<br/>")))
385 "var i = 0;
386 var s = 0;
387 for (; i <= 10; ) {
388 document.write('i: ' + i + ' s: ' + s + '<br/>');
389 var _js1 = i + 1;
390 var _js2 = s + i + i + 1;
391 i = _js1;
392 s = _js2;
393 };")
395 (test-ps-js iteration-constructs-3
396 (do* ((i 0 (1+ i))
397 (s 0 (+ s i (1- i))))
398 ((> i 10))
399 ((@ document write) (+ "i: " i " s: " s "<br/>")))
400 "for (var i = 0, s = 0; i <= 10; i += 1, s = s + i + i - 1) {
401 document.write('i: ' + i + ' s: ' + s + '<br/>');
402 };")
404 (test-ps-js iteration-constructs-4
405 (let ((arr (array "a" "b" "c" "d" "e")))
406 (dotimes (i (@ arr length))
407 ((@ document write) (+ "i: " i " arr[i]: " (aref arr i) "<br/>"))))
408 "var arr = ['a', 'b', 'c', 'd', 'e'];
409 for (var i = 0; i < arr.length; i += 1) {
410 document.write('i: ' + i + ' arr[i]: ' + arr[i] + '<br/>');
411 };")
413 (test-ps-js iteration-constructs-5
414 (let ((res 0))
415 (alert (+ "Summation to 10 is "
416 (dotimes (i 10 res)
417 (incf res (1+ i))))))
418 "var res = 0;
419 alert('Summation to 10 is ' + (function () {
420 for (var i = 0; i < 10; i += 1) {
421 res += i + 1;
423 return res;
424 })());")
426 (test-ps-js iteration-constructs-6
427 (let ((l (list 1 2 4 8 16 32)))
428 (dolist (c l)
429 ((@ document write) (+ "c: " c "<br/>"))))
430 "var l = [1, 2, 4, 8, 16, 32];
431 for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) {
432 c = l[_js_idx1];
433 document.write('c: ' + c + '<br/>');
434 };")
436 (test-ps-js iteration-constructs-7
437 (let ((l '(1 2 4 8 16 32))
438 (s 0))
439 (alert (+ "Sum of " l " is: "
440 (dolist (c l s)
441 (incf s c)))))
442 "var l = [1, 2, 4, 8, 16, 32];
443 var s = 0;
444 alert('Sum of ' + l + ' is: ' + (function () {
445 for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) {
446 c = l[_js_idx1];
447 s += c;
449 return s;
450 })());")
452 (test-ps-js iteration-constructs-8
453 (let ((obj (create a 1 b 2 c 3)))
454 (for-in (i obj)
455 ((@ document write) (+ i ": " (aref obj i) "<br/>"))))
456 "var obj = { a : 1, b : 2, c : 3 };
457 for (var i in obj) {
458 document.write(i + ': ' + obj[i] + '<br/>');
459 };")
461 (test-ps-js iteration-constructs-9
462 (while ((@ film is-not-finished))
463 ((@ this eat) (new *popcorn)))
464 "while (film.isNotFinished()) {
465 this.eat(new Popcorn);
466 };")
468 (test-ps-js the-case-statement-1
469 (case (aref blorg i)
470 ((1 "one") (alert "one"))
471 (2 (alert "two"))
472 (t (alert "default clause")))
473 "switch (blorg[i]) {
474 case 1:
475 case 'one':
476 alert('one');
477 break;
478 case 2:
479 alert('two');
480 break;
481 default:
482 alert('default clause');
483 };")
485 (test-ps-js the-case-statement-2
486 (switch (aref blorg i)
487 (1 (alert "If I get here"))
488 (2 (alert "I also get here"))
489 (default (alert "I always get here")))
490 "switch (blorg[i]) {
491 case 1: alert('If I get here');
492 case 2: alert('I also get here');
493 default: alert('I always get here');
494 };")
496 (test-ps-js the-try-statement-1
497 (try (throw "i")
498 (:catch (error)
499 (alert (+ "an error happened: " error)))
500 (:finally
501 (alert "Leaving the try form")))
502 "try {
503 throw 'i';
504 } catch (error) {
505 alert('an error happened: ' + error);
506 } finally {
507 alert('Leaving the try form');
508 };")
510 (test-ps-js the-html-generator-1
511 (ps-html ((:a :href "foobar") "blorg"))
512 "'<A HREF=\"foobar\">blorg</A>';")
514 (test-ps-js the-html-generator-2
515 (ps-html ((:a :href (generate-a-link)) "blorg"))
516 "['<A HREF=\"', generateALink(), '\">blorg</A>']['join']('');")
518 (test-ps-js the-html-generator-3
519 (funcall (getprop document 'write)
520 (ps-html ((:a :href "#"
521 :onclick (ps-inline (transport))) "link")))
522 "document.write(['<A HREF=\"#\" ONCLICK=\"', 'javascript:' + 'transport()', '\">link</A>']['join'](''));")
524 (test-ps-js the-html-generator-4
525 (let ((disabled nil)
526 (authorized t))
527 (setf (getprop element 'inner-h-t-m-l)
528 (ps-html ((:textarea (or disabled (not authorized)) :disabled "disabled")
529 "Edit me"))))
530 "var disabled = null;
531 var authorized = true;
532 element.innerHTML = ['<TEXTAREA', disabled || !authorized ? [' DISABLED=\"', 'disabled', '\"']['join']('') : '', '>Edit me</TEXTAREA>']['join']('');")