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