Introduced special global variables to Parenscript; renamed let and lexical-let to...
[clinton/parenscript.git] / t / reference-tests.lisp
CommitLineData
171bbab3 1(in-package :ps-test)
eb17f15c
HH
2;; Tests of everything in the reference.
3;; File is generated automatically from the text in reference.lisp by
4;; the function make-reference-tests-dot-lisp in ref2test.lisp
5;; so do not edit this file.
86cb6d98
LC
6(eval-when (:compile-toplevel :load-toplevel :execute)
7 (def-suite ref-tests))
eb17f15c
HH
8(in-suite ref-tests)
9
94a05cdf
LC
10(test-ps-js statements-and-expressions-1
11 (+ i (if 1 2 3))
eb17f15c
HH
12 "i + (1 ? 2 : 3)")
13
94a05cdf
LC
14(test-ps-js statements-and-expressions-2
15 (if 1 2 3)
eb17f15c
HH
16 "if (1) {
17 2;
18} else {
19 3;
20}")
21
94a05cdf 22(test-ps-js symbol-conversion-1
3b238048
HH
23 !?#@%
24 "bangwhathashatpercent")
eb17f15c 25
94a05cdf
LC
26(test-ps-js symbol-conversion-2
27 bla-foo-bar
eb17f15c
HH
28 "blaFooBar")
29
94a05cdf
LC
30(test-ps-js symbol-conversion-3
31 *array
eb17f15c
HH
32 "Array")
33
94a05cdf
LC
34(test-ps-js symbol-conversion-6
35 *global-array*
eb17f15c
HH
36 "GLOBALARRAY")
37
94a05cdf
LC
38(test-ps-js symbol-conversion-7
39 *global-array*.length
eb17f15c
HH
40 "GLOBALARRAY.length")
41
94a05cdf
LC
42(test-ps-js number-literals-1
43 1
eb17f15c
HH
44 "1")
45
94a05cdf
LC
46(test-ps-js number-literals-2
47 123.123
eb17f15c
HH
48 "123.123")
49
94a05cdf
LC
50(test-ps-js number-literals-3
51 #x10
eb17f15c
HH
52 "16")
53
94a05cdf
LC
54(test-ps-js string-literals-1
55 "foobar"
7a7d6c73
HH
56 "'foobar'")
57
94a05cdf
LC
58(test-ps-js string-literals-2
59 "bratzel bub"
7a7d6c73
HH
60 "'bratzel bub'")
61
94a05cdf
LC
62(test-ps-js array-literals-1
63 (array)
7a7d6c73 64 "[ ]")
eb17f15c 65
94a05cdf
LC
66(test-ps-js array-literals-2
67 (array 1 2 3)
eb17f15c
HH
68 "[ 1, 2, 3 ]")
69
94a05cdf 70(test-ps-js array-literals-3
eb17f15c 71 (array (array 2 3)
94a05cdf 72 (array "foobar" "bratzel bub"))
eb17f15c
HH
73 "[ [ 2, 3 ], [ 'foobar', 'bratzel bub' ] ]")
74
94a05cdf
LC
75(test-ps-js array-literals-4
76 (make-array)
eb17f15c
HH
77 "new Array()")
78
94a05cdf
LC
79(test-ps-js array-literals-5
80 (make-array 1 2 3)
eb17f15c
HH
81 "new Array(1, 2, 3)")
82
94a05cdf 83(test-ps-js array-literals-6
eb17f15c
HH
84 (make-array
85 (make-array 2 3)
94a05cdf 86 (make-array "foobar" "bratzel bub"))
eb17f15c
HH
87 "new Array(new Array(2, 3), new Array('foobar', 'bratzel bub'))")
88
94a05cdf
LC
89(test-ps-js object-literals-1
90 (create :foo "bar" :blorg 1)
91 "{ foo : 'bar',
eb17f15c
HH
92 blorg : 1 }")
93
94a05cdf 94(test-ps-js object-literals-2
eb17f15c
HH
95 (create :foo "hihi"
96 :blorg (array 1 2 3)
94a05cdf
LC
97 :another-object (create :schtrunz 1))
98 "{ foo : 'hihi',
99 blorg : [ 1, 2, 3 ],
eb17f15c
HH
100 anotherObject : { schtrunz : 1 } }")
101
94a05cdf
LC
102(test-ps-js object-literals-3
103 (slot-value an-object 'foo)
eb17f15c
HH
104 "anObject.foo")
105
94a05cdf
LC
106(test-ps-js object-literals-4
107 an-object.foo
eb17f15c
HH
108 "anObject.foo")
109
94a05cdf 110(test-ps-js object-literals-5
eb17f15c 111 (with-slots (a b c) this
94a05cdf 112 (+ a b c))
b44afd8f 113 "this.a + this.b + this.c;")
eb17f15c 114
94a05cdf 115(test-ps-js regular-expression-literals-1
ca4fb762
HH
116 (regex "foobar")
117 "/foobar/")
118
119(test-ps-js regular-expression-literals-2
94a05cdf 120 (regex "/foobar/i")
eb17f15c
HH
121 "/foobar/i")
122
94a05cdf
LC
123(test-ps-js literal-symbols-1
124 T
7a7d6c73
HH
125 "true")
126
94a05cdf
LC
127(test-ps-js literal-symbols-2
128 FALSE
7a7d6c73
HH
129 "false")
130
94a05cdf
LC
131(test-ps-js literal-symbols-3
132 NIL
eb17f15c
HH
133 "null")
134
94a05cdf
LC
135(test-ps-js literal-symbols-4
136 UNDEFINED
eb17f15c
HH
137 "undefined")
138
94a05cdf
LC
139(test-ps-js literal-symbols-5
140 THIS
eb17f15c
HH
141 "this")
142
94a05cdf
LC
143(test-ps-js variables-1
144 variable
eb17f15c
HH
145 "variable")
146
94a05cdf
LC
147(test-ps-js variables-2
148 a-variable
eb17f15c
HH
149 "aVariable")
150
94a05cdf
LC
151(test-ps-js variables-3
152 *math
eb17f15c
HH
153 "Math")
154
94a05cdf
LC
155(test-ps-js variables-4
156 *math.floor
eb17f15c
HH
157 "Math.floor")
158
94a05cdf
LC
159(test-ps-js function-calls-and-method-calls-1
160 (blorg 1 2)
eb17f15c
HH
161 "blorg(1, 2)")
162
94a05cdf
LC
163(test-ps-js function-calls-and-method-calls-2
164 (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
eb17f15c
HH
165 "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ])")
166
94a05cdf
LC
167(test-ps-js function-calls-and-method-calls-3
168 ((aref foo i) 1 2)
eb17f15c
HH
169 "foo[i](1, 2)")
170
94a05cdf
LC
171(test-ps-js function-calls-and-method-calls-4
172 (.blorg this 1 2)
eb17f15c
HH
173 "this.blorg(1, 2)")
174
94a05cdf
LC
175(test-ps-js function-calls-and-method-calls-5
176 (this.blorg 1 2)
eb17f15c
HH
177 "this.blorg(1, 2)")
178
94a05cdf
LC
179(test-ps-js function-calls-and-method-calls-6
180 (.blorg (aref foobar 1) NIL T)
eb17f15c
HH
181 "foobar[1].blorg(null, true)")
182
94a05cdf
LC
183(test-ps-js operator-expressions-1
184 (* 1 2)
eb17f15c
HH
185 "1 * 2")
186
94a05cdf
LC
187(test-ps-js operator-expressions-2
188 (= 1 2)
eb17f15c
HH
189 "1 == 2")
190
94a05cdf
LC
191(test-ps-js operator-expressions-3
192 (eql 1 2)
eb17f15c
HH
193 "1 == 2")
194
94a05cdf
LC
195(test-ps-js operator-expressions-5
196 (* 1 (+ 2 3 4) 4 (/ 6 7))
eb17f15c
HH
197 "1 * (2 + 3 + 4) * 4 * (6 / 7)")
198
94a05cdf 199(test-ps-js operator-expressions-6
94a05cdf 200 (incf i)
eb17f15c
HH
201 "++i")
202
07afea38 203(test-ps-js operator-expressions-7
94a05cdf 204 (decf i)
eb17f15c
HH
205 "--i")
206
07afea38 207(test-ps-js operator-expressions-8
94a05cdf 208 (1- i)
eb17f15c
HH
209 "i - 1")
210
07afea38 211(test-ps-js operator-expressions-9
94a05cdf 212 (1+ i)
eb17f15c
HH
213 "i + 1")
214
07afea38 215(test-ps-js operator-expressions-10
94a05cdf 216 (not (< i 2))
eb17f15c
HH
217 "i >= 2")
218
07afea38 219(test-ps-js operator-expressions-11
94a05cdf 220 (not (eql i 2))
eb17f15c
HH
221 "i != 2")
222
94a05cdf
LC
223(test-ps-js body-forms-1
224 (progn (blorg i) (blafoo i))
eb17f15c
HH
225 "blorg(i);
226blafoo(i);")
227
94a05cdf
LC
228(test-ps-js body-forms-2
229 (+ i (progn (blorg i) (blafoo i)))
eb17f15c
HH
230 "i + (blorg(i), blafoo(i))")
231
94a05cdf 232(test-ps-js function-definition-1
eb17f15c 233 (defun a-function (a b)
94a05cdf 234 (return (+ a b)))
eb17f15c
HH
235 "function aFunction(a, b) {
236 return a + b;
237}")
238
94a05cdf
LC
239(test-ps-js function-definition-2
240 (lambda (a b) (return (+ a b)))
eb17f15c
HH
241 "function (a, b) {
242 return a + b;
243}")
244
94a05cdf
LC
245(test-ps-js assignment-1
246 (setf a 1)
72332f2a 247 "a = 1;")
eb17f15c 248
94a05cdf
LC
249(test-ps-js assignment-2
250 (setf a 2 b 3 c 4 x (+ a b c))
eb17f15c
HH
251 "a = 2;
252b = 3;
253c = 4;
254x = a + b + c;")
255
94a05cdf
LC
256(test-ps-js assignment-3
257 (setf a (1+ a))
72332f2a 258 "a++;")
eb17f15c 259
94a05cdf 260(test-ps-js assignment-4
8dcd51d2 261 (setf a (+ a 2 3 4 a))
72332f2a 262 "a += 2 + 3 + 4 + a;")
eb17f15c 263
94a05cdf
LC
264(test-ps-js assignment-5
265 (setf a (- 1 a))
72332f2a 266 "a = 1 - a;")
eb17f15c 267
a2a9eab0
VS
268(test-ps-js assignment-6
269 (defun (setf color) (new-color el)
270 (setf (slot-value (slot-value el 'style) 'color) new-color))
271 "function __setf_color(newColor, el) {
272 el.style.color = newColor;
273};")
274
275(test-ps-js assignment-7
276 (setf (color some-div) (+ 23 "em"))
277 "var _js2 = someDiv;
278var _js1 = 23 + 'em';
279__setf_color(_js1, _js2);")
280
281(test-ps-js assignment-8
282 (defsetf left (el) (offset)
283 `(setf (slot-value (slot-value ,el 'style) 'left) ,offset))
284 "null")
285
286(test-ps-js assignment-9
287 (setf (left some-div) (+ 123 "px"))
288 "var _js2 = someDiv;
289var _js1 = 123 + 'px';
290_js2.style.left = _js1;")
291
292(test-ps-js assignment-10
293 (progn (defmacro left (el)
294 `(slot-value ,el 'offset-left))
295 (left some-div))
296 "someDiv.offsetLeft;")
297
94a05cdf
LC
298(test-ps-js single-argument-statements-1
299 (return 1)
eb17f15c
HH
300 "return 1")
301
94a05cdf
LC
302(test-ps-js single-argument-statements-2
303 (throw "foobar")
eb17f15c
HH
304 "throw 'foobar'")
305
94a05cdf
LC
306(test-ps-js single-argument-expression-1
307 (delete (new (*foobar 2 3 4)))
eb17f15c
HH
308 "delete new Foobar(2, 3, 4)")
309
94a05cdf 310(test-ps-js single-argument-expression-2
eb17f15c
HH
311 (if (= (typeof blorg) *string)
312 (alert (+ "blorg is a string: " blorg))
94a05cdf 313 (alert "blorg is not a string"))
eb17f15c
HH
314 "if (typeof blorg == String) {
315 alert('blorg is a string: ' + blorg);
316} else {
317 alert('blorg is not a string');
318}")
319
94a05cdf 320(test-ps-js conditional-statements-1
eb17f15c
HH
321 (if (blorg.is-correct)
322 (progn (carry-on) (return i))
94a05cdf 323 (alert "blorg is not correct!"))
eb17f15c
HH
324 "if (blorg.isCorrect()) {
325 carryOn();
326 return i;
327} else {
328 alert('blorg is not correct!');
329}")
330
94a05cdf
LC
331(test-ps-js conditional-statements-2
332 (+ i (if (blorg.add-one) 1 2))
eb17f15c
HH
333 "i + (blorg.addOne() ? 1 : 2)")
334
94a05cdf 335(test-ps-js conditional-statements-3
eb17f15c
HH
336 (when (blorg.is-correct)
337 (carry-on)
94a05cdf 338 (return i))
eb17f15c
HH
339 "if (blorg.isCorrect()) {
340 carryOn();
341 return i;
342}")
343
94a05cdf 344(test-ps-js conditional-statements-4
eb17f15c 345 (unless (blorg.is-correct)
94a05cdf 346 (alert "blorg is not correct!"))
eb17f15c
HH
347 "if (!blorg.isCorrect()) {
348 alert('blorg is not correct!');
349}")
350
94a05cdf
LC
351(test-ps-js variable-declaration-1
352 (defvar *a* (array 1 2 3))
b44afd8f 353 "var A = [ 1, 2, 3 ]")
eb17f15c 354
94a05cdf 355(test-ps-js variable-declaration-2
eb17f15c 356 (if (= i 1)
58c4ef4f
VS
357 (let* ((blorg "hallo"))
358 (alert blorg))
359 (let* ((blorg "blitzel"))
360 (alert blorg)))
eb17f15c
HH
361 "if (i == 1) {
362 var blorg = 'hallo';
363 alert(blorg);
364} else {
365 var blorg = 'blitzel';
366 alert(blorg);
367}")
368
94a05cdf 369(test-ps-js variable-declaration-3
eb17f15c 370 (if (= i 1)
58c4ef4f 371 (lexical-let* ((blorg "hallo"))
eb17f15c 372 (alert blorg))
58c4ef4f 373 (lexical-let* ((blorg "blitzel"))
94a05cdf 374 (alert blorg)))
eb17f15c 375 "if (i == 1) {
b5e0bcb7
VS
376 (function () {
377 var newlexicalcontext1 = new Object;
378 newlexicalcontext1['blorg'] = 'hallo';
379 with (newlexicalcontext1) {
380 alert(blorg);
381 };
382 })();
eb17f15c 383} else {
b5e0bcb7
VS
384 (function () {
385 var newlexicalcontext3 = new Object;
386 newlexicalcontext3['blorg'] = 'blitzel';
387 with (newlexicalcontext3) {
388 alert(blorg);
389 };
390 })();
eb17f15c
HH
391}")
392
94a05cdf 393(test-ps-js iteration-constructs-1
eb17f15c
HH
394 (do ((i 0 (1+ i))
395 (l (aref blorg i) (aref blorg i)))
396 ((or (= i blorg.length)
397 (eql l "Fumitastic")))
94a05cdf
LC
398 (document.write (+ "L is " l)))
399 "for (var i = 0, l = blorg[i];
7a7d6c73 400 !(i == blorg.length || l == 'Fumitastic');
eb17f15c
HH
401 i = i + 1, l = blorg[i]) {
402 document.write('L is ' + l);
403}")
404
94a05cdf 405(test-ps-js iteration-constructs-2
eb17f15c 406 (dotimes (i blorg.length)
94a05cdf 407 (document.write (+ "L is " (aref blorg i))))
7a7d6c73 408 "for (var i = 0; i < blorg.length; i = i + 1) {
eb17f15c
HH
409 document.write('L is ' + blorg[i]);
410}")
411
94a05cdf 412(test-ps-js iteration-constructs-3
eb17f15c 413 (dolist (l blorg)
94a05cdf 414 (document.write (+ "L is " l)))
07afea38 415 " var tmpArr1 = blorg;
7a7d6c73
HH
416 for (var tmpI2 = 0; tmpI2 < tmpArr1.length;
417 tmpI2 = tmpI2 + 1) {
418 var l = tmpArr1[tmpI2];
419 document.write('L is ' + l);
07afea38 420 };")
eb17f15c 421
94a05cdf 422(test-ps-js iteration-constructs-4
eb17f15c 423 (doeach (i object)
94a05cdf 424 (document.write (+ i " is " (aref object i))))
eb17f15c
HH
425 "for (var i in object) {
426 document.write(i + ' is ' + object[i]);
427}")
428
94a05cdf 429(test-ps-js iteration-constructs-5
eb17f15c 430 (while (film.is-not-finished)
94a05cdf 431 (this.eat (new *popcorn)))
eb17f15c
HH
432 "while (film.isNotFinished()) {
433 this.eat(new Popcorn);
434}")
435
94a05cdf 436(test-ps-js the-case-statement-1
eb17f15c 437 (case (aref blorg i)
3c393e09 438 ((1 "one") (alert "one"))
eb17f15c 439 (2 (alert "two"))
3c393e09 440 (t (alert "default clause")))
eb17f15c 441 "switch (blorg[i]) {
b44afd8f 442 case 1:
3c393e09
HH
443 case 'one':
444 alert('one');
445 break;
446 case 2:
447 alert('two');
448 break;
eb17f15c
HH
449 default: alert('default clause');
450}")
451
3c393e09
HH
452(test-ps-js the-case-statement-2
453 (switch (aref blorg i)
454 (1 (alert "If I get here"))
455 (2 (alert "I also get here"))
456 (default (alert "I always get here")))
457 "switch (blorg[i]) {
458 case 1: alert('If I get here');
459 case 2: alert('I also get here');
460 default: alert('I always get here');
461}")
462
94a05cdf 463(test-ps-js the-with-statement-1
5d9cdcad 464 (with (create :foo "foo" :i "i")
94a05cdf
LC
465 (alert (+ "i is now intermediary scoped: " i)))
466 "with ({ foo : 'foo',
eb17f15c
HH
467 i : 'i' }) {
468 alert('i is now intermediary scoped: ' + i);
469}")
470
94a05cdf
LC
471(test-ps-js the-try-statement-1
472 (try (throw "i")
eb17f15c
HH
473 (:catch (error)
474 (alert (+ "an error happened: " error)))
475 (:finally
94a05cdf 476 (alert "Leaving the try form")))
eb17f15c
HH
477 "try {
478 throw 'i';
479} catch (error) {
480 alert('an error happened: ' + error);
481} finally {
482 alert('Leaving the try form');
483}")
484
94a05cdf 485(test-ps-js the-html-generator-1
8bb28ead 486 (ps-html ((:a :href "foobar") "blorg"))
7a7d6c73 487 "'<a href=\"foobar\">blorg</a>'")
eb17f15c 488
94a05cdf 489(test-ps-js the-html-generator-2
8bb28ead 490 (ps-html ((:a :href (generate-a-link)) "blorg"))
7a7d6c73 491 "'<a href=\"' + generateALink() + '\">blorg</a>'")
eb17f15c 492
94a05cdf 493(test-ps-js the-html-generator-3
eb17f15c 494 (document.write
8bb28ead
VS
495 (ps-html ((:a :href "#"
496 :onclick (lisp (ps-inline (transport)))) "link")))
9fbb3004 497 "document.write('<a href=\"#\" onclick=\"' + 'javascript:transport()' + '\">link</a>')")
eb17f15c 498
94a05cdf 499(test-ps-js the-html-generator-4
58c4ef4f 500 (let* ((disabled nil)
b8fa1a27
HH
501 (authorized t))
502 (setf element.inner-h-t-m-l
8bb28ead 503 (ps-html ((:textarea (or disabled (not authorized)) :disabled "disabled")
b8fa1a27 504 "Edit me"))))
07afea38 505 " var disabled = null;
b8fa1a27
HH
506 var authorized = true;
507 element.innerHTML =
508 '<textarea'
509 + (disabled || !authorized ? ' disabled=\"' + 'disabled' + '\"' : '')
07afea38 510 + '>Edit me</textarea>';")
b8fa1a27
HH
511
512(test-ps-js the-html-generator-5
28967ee4 513 (css-inline :color "red"
94a05cdf 514 :font-size "x-small")
28967ee4
HH
515 "'color:red;font-size:x-small'")
516
b8fa1a27 517(test-ps-js the-html-generator-6
28967ee4 518 (defun make-color-div(color-name)
8bb28ead 519 (return (ps-html ((:div :style (css-inline :color color-name))
94a05cdf 520 color-name " looks like this."))))
28967ee4
HH
521 "function makeColorDiv(colorName) {
522 return '<div style=\"' + ('color:' + colorName) + '\">' + colorName
523 + ' looks like this.</div>';
524}")
525