1 (in-package #:parenscript-test
)
3 ;;; These tests were originally generated from the reference manual
7 (test-ps-js statements-and-expressions-1
11 (test-ps-js statements-and-expressions-2
19 (test-ps-js symbol-conversion-1
21 "bangwhathashatpercent;")
23 (test-ps-js symbol-conversion-2
27 (test-ps-js symbol-conversion-3
31 (test-ps-js symbol-conversion-4
35 (test-ps-js number-literals-1
39 (test-ps-js number-literals-2
43 (test-ps-js number-literals-3
47 (test-ps-js string-literals-1
51 (test-ps-js string-literals-2
55 (test-ps-js string-literals-3
59 (test-ps-js array-literals-1
63 (test-ps-js array-literals-2
67 (test-ps-js array-literals-3
69 (array "foobar" "bratzel bub"))
70 "[ [ 2, 3 ], [ 'foobar', 'bratzel bub' ] ];")
72 (test-ps-js array-literals-4
76 (test-ps-js array-literals-5
78 "new Array(1, 2, 3);")
80 (test-ps-js array-literals-6
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
93 another-object
(create :schtrunz
1))
96 anotherObject : { 'schtrunz' : 1 } };")
98 (test-ps-js object-literals-3
99 (getprop an-object
'foo
)
102 (test-ps-js object-literals-4
103 (@ an-object foo bar
)
106 (test-ps-js object-literals-5
107 (with-slots (a b c
) this
109 "this.a + this.b + this.c;")
111 (test-ps-js regular-expression-literals-1
115 (test-ps-js regular-expression-literals-2
119 (test-ps-js literal-symbols-1
123 (test-ps-js literal-symbols-2
127 (test-ps-js literal-symbols-3
131 (test-ps-js literal-symbols-4
137 (test-ps-js literal-symbols-5
141 (test-ps-js literal-symbols-6
145 (test-ps-js variables-1
149 (test-ps-js variables-2
153 (test-ps-js variables-3
157 (test-ps-js function-calls-and-method-calls-1
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)
169 (test-ps-js function-calls-and-method-calls-4
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
181 (test-ps-js operator-expressions-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
193 (test-ps-js operator-expressions-5
197 (test-ps-js operator-expressions-6
201 (test-ps-js operator-expressions-7
205 (test-ps-js operator-expressions-8
209 (test-ps-js body-forms-1
210 (progn (blorg i
) (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
)
221 "function aFunction(a, b) {
225 (test-ps-js function-definition-2
226 (lambda (a b
) (+ a b
))
231 (test-ps-js assignment-1
235 (test-ps-js assignment-2
236 (setf a
2 b
3 c
4 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
250 (test-ps-js assignment-5
260 (test-ps-js assignment-6
264 (test-ps-js assignment-8
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
276 (defsetf left
(el) (offset)
277 `(setf (getprop (getprop ,el
'style
) 'left
) ,offset
))
278 (setf (left some-div
) (+ 123 "px")))
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
)))
287 "someDiv.offsetLeft;")
289 (test-ps-js single-argument-statements-1
293 (test-ps-js single-argument-statements-2
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);
308 alert('blorg is not a string');
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()) {
319 alert('blorg is not correct!');
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
))
330 "if (blorg.isCorrect()) {
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!');
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
367 (test-ps-js iteration-constructs-1
368 (do* ((a) b
(c (array "a" "b" "c" "d" "e"))
370 (e (aref c d
) (aref c d
)))
371 ((or (= d
(@ c length
)) (string= e
"x")))
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]) {
377 document.write('a: ' + a + ' b: ' + b + '<br/>');
380 (test-ps-js iteration-constructs-2
382 (s 0 (+ s i
(1+ i
))))
384 (funcall (@ document write
) (+ "i: " i
" s: " s
"<br/>")))
388 document.write('i: ' + i + ' s: ' + s + '<br/>');
390 var _js2 = s + i + i + 1;
395 (test-ps-js iteration-constructs-3
397 (s 0 (+ s i
(1- i
))))
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/>');
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/>');
413 (test-ps-js iteration-constructs-5
415 (alert (+ "Summation to 10 is "
417 (incf res
(1+ i
))))))
419 alert('Summation to 10 is ' + (function () {
420 for (var i = 0; i < 10; i += 1) {
426 (test-ps-js iteration-constructs-6
427 (let ((l (list 1 2 4 8 16 32)))
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) {
433 document.write('c: ' + c + '<br/>');
436 (test-ps-js iteration-constructs-7
437 (let ((l '(1 2 4 8 16 32))
439 (alert (+ "Sum of " l
" is: "
442 "var l = [1, 2, 4, 8, 16, 32];
444 alert('Sum of ' + l + ' is: ' + (function () {
445 for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) {
452 (test-ps-js iteration-constructs-8
453 (let ((obj (create a
1 b
2 c
3)))
455 ((@ document write
) (+ i
": " (aref obj i
) "<br/>"))))
456 "var obj = { a : 1, b : 2, c : 3 };
458 document.write(i + ': ' + obj[i] + '<br/>');
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);
468 (test-ps-js the-case-statement-1
470 ((1 "one") (alert "one"))
472 (t (alert "default clause")))
482 alert('default clause');
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")))
491 case 1: alert('If I get here');
492 case 2: alert('I also get here');
493 default: alert('I always get here');
496 (test-ps-js the-try-statement-1
499 (alert (+ "an error happened: " error
)))
501 (alert "Leaving the try form")))
505 alert('an error happened: ' + error);
507 alert('Leaving the try form');
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
527 (setf (getprop element
'inner-h-t-m-l
)
528 (ps-html ((:textarea
(or disabled
(not authorized
)) :disabled
"disabled")
530 "var disabled = null;
531 var authorized = true;
532 element.innerHTML = ['<TEXTAREA', disabled || !authorized ? [' DISABLED=\"', 'disabled', '\"']['join']('') : '', '>Edit me</TEXTAREA>']['join']('');")