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