ref2test finds reference.lisp in docs dir
[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 "bangwhathashdollaratpercent")
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/i")
116 "/foobar/i")
117
118 (test-ps-js literal-symbols-1
119 T
120 "true")
121
122 (test-ps-js literal-symbols-2
123 FALSE
124 "false")
125
126 (test-ps-js literal-symbols-3
127 NIL
128 "null")
129
130 (test-ps-js literal-symbols-4
131 UNDEFINED
132 "undefined")
133
134 (test-ps-js literal-symbols-5
135 THIS
136 "this")
137
138 (test-ps-js variables-1
139 variable
140 "variable")
141
142 (test-ps-js variables-2
143 a-variable
144 "aVariable")
145
146 (test-ps-js variables-3
147 *math
148 "Math")
149
150 (test-ps-js variables-4
151 *math.floor
152 "Math.floor")
153
154 (test-ps-js function-calls-and-method-calls-1
155 (blorg 1 2)
156 "blorg(1, 2)")
157
158 (test-ps-js function-calls-and-method-calls-2
159 (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
160 "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ])")
161
162 (test-ps-js function-calls-and-method-calls-3
163 ((aref foo i) 1 2)
164 "foo[i](1, 2)")
165
166 (test-ps-js function-calls-and-method-calls-4
167 (.blorg this 1 2)
168 "this.blorg(1, 2)")
169
170 (test-ps-js function-calls-and-method-calls-5
171 (this.blorg 1 2)
172 "this.blorg(1, 2)")
173
174 (test-ps-js function-calls-and-method-calls-6
175 (.blorg (aref foobar 1) NIL T)
176 "foobar[1].blorg(null, true)")
177
178 (test-ps-js operator-expressions-1
179 (* 1 2)
180 "1 * 2")
181
182 (test-ps-js operator-expressions-2
183 (= 1 2)
184 "1 == 2")
185
186 (test-ps-js operator-expressions-3
187 (eql 1 2)
188 "1 == 2")
189
190 (test-ps-js operator-expressions-5
191 (* 1 (+ 2 3 4) 4 (/ 6 7))
192 "1 * (2 + 3 + 4) * 4 * (6 / 7)")
193
194 (test-ps-js operator-expressions-6
195 (++ i)
196 "i++")
197
198 (test-ps-js operator-expressions-7
199 (-- i)
200 "i--")
201
202 (test-ps-js operator-expressions-8
203 (incf i)
204 "++i")
205
206 (test-ps-js operator-expressions-9
207 (decf i)
208 "--i")
209
210 (test-ps-js operator-expressions-10
211 (1- i)
212 "i - 1")
213
214 (test-ps-js operator-expressions-11
215 (1+ i)
216 "i + 1")
217
218 (test-ps-js operator-expressions-12
219 (not (< i 2))
220 "i >= 2")
221
222 (test-ps-js operator-expressions-13
223 (not (eql i 2))
224 "i != 2")
225
226 (test-ps-js body-forms-1
227 (progn (blorg i) (blafoo i))
228 "blorg(i);
229 blafoo(i);")
230
231 (test-ps-js body-forms-2
232 (+ i (progn (blorg i) (blafoo i)))
233 "i + (blorg(i), blafoo(i))")
234
235 (test-ps-js function-definition-1
236 (defun a-function (a b)
237 (return (+ a b)))
238 "function aFunction(a, b) {
239 return a + b;
240 }")
241
242 (test-ps-js function-definition-2
243 (lambda (a b) (return (+ a b)))
244 "function (a, b) {
245 return a + b;
246 }")
247
248 (test-ps-js assignment-1
249 (setf a 1)
250 "a = 1")
251
252 (test-ps-js assignment-2
253 (setf a 2 b 3 c 4 x (+ a b c))
254 "a = 2;
255 b = 3;
256 c = 4;
257 x = a + b + c;")
258
259 (test-ps-js assignment-3
260 (setf a (1+ a))
261 "a++")
262
263 (test-ps-js assignment-4
264 (setf a (* 2 3 4 a 4 a))
265 "a *= 2 * 3 * 4 * 4 * a")
266
267 (test-ps-js assignment-5
268 (setf a (- 1 a))
269 "a = 1 - a")
270
271 (test-ps-js single-argument-statements-1
272 (return 1)
273 "return 1")
274
275 (test-ps-js single-argument-statements-2
276 (throw "foobar")
277 "throw 'foobar'")
278
279 (test-ps-js single-argument-expression-1
280 (delete (new (*foobar 2 3 4)))
281 "delete new Foobar(2, 3, 4)")
282
283 (test-ps-js single-argument-expression-2
284 (if (= (typeof blorg) *string)
285 (alert (+ "blorg is a string: " blorg))
286 (alert "blorg is not a string"))
287 "if (typeof blorg == String) {
288 alert('blorg is a string: ' + blorg);
289 } else {
290 alert('blorg is not a string');
291 }")
292
293 (test-ps-js conditional-statements-1
294 (if (blorg.is-correct)
295 (progn (carry-on) (return i))
296 (alert "blorg is not correct!"))
297 "if (blorg.isCorrect()) {
298 carryOn();
299 return i;
300 } else {
301 alert('blorg is not correct!');
302 }")
303
304 (test-ps-js conditional-statements-2
305 (+ i (if (blorg.add-one) 1 2))
306 "i + (blorg.addOne() ? 1 : 2)")
307
308 (test-ps-js conditional-statements-3
309 (when (blorg.is-correct)
310 (carry-on)
311 (return i))
312 "if (blorg.isCorrect()) {
313 carryOn();
314 return i;
315 }")
316
317 (test-ps-js conditional-statements-4
318 (unless (blorg.is-correct)
319 (alert "blorg is not correct!"))
320 "if (!blorg.isCorrect()) {
321 alert('blorg is not correct!');
322 }")
323
324 (test-ps-js variable-declaration-1
325 (defvar *a* (array 1 2 3))
326 "var A = [ 1, 2, 3 ];")
327
328 (test-ps-js variable-declaration-2
329 (if (= i 1)
330 (progn (defvar blorg "hallo")
331 (alert blorg))
332 (progn (defvar blorg "blitzel")
333 (alert blorg)))
334 "if (i == 1) {
335 var blorg = 'hallo';
336 alert(blorg);
337 } else {
338 var blorg = 'blitzel';
339 alert(blorg);
340 }")
341
342 (test-ps-js variable-declaration-3
343 (if (= i 1)
344 (let ((blorg "hallo"))
345 (alert blorg))
346 (let ((blorg "blitzel"))
347 (alert blorg)))
348 "if (i == 1) {
349 var blorg = 'hallo';
350 alert(blorg);
351 } else {
352 var blorg = 'blitzel';
353 alert(blorg);
354 }")
355
356 (test-ps-js iteration-constructs-1
357 (do ((i 0 (1+ i))
358 (l (aref blorg i) (aref blorg i)))
359 ((or (= i blorg.length)
360 (eql l "Fumitastic")))
361 (document.write (+ "L is " l)))
362 "for (var i = 0, l = blorg[i];
363 !(i == blorg.length || l == 'Fumitastic');
364 i = i + 1, l = blorg[i]) {
365 document.write('L is ' + l);
366 }")
367
368 (test-ps-js iteration-constructs-2
369 (dotimes (i blorg.length)
370 (document.write (+ "L is " (aref blorg i))))
371 "for (var i = 0; i < blorg.length; i = i + 1) {
372 document.write('L is ' + blorg[i]);
373 }")
374
375 (test-ps-js iteration-constructs-3
376 (dolist (l blorg)
377 (document.write (+ "L is " l)))
378 "{
379 var tmpArr1 = blorg;
380 for (var tmpI2 = 0; tmpI2 < tmpArr1.length;
381 tmpI2 = tmpI2 + 1) {
382 var l = tmpArr1[tmpI2];
383 document.write('L is ' + l);
384 }
385 }")
386
387 (test-ps-js iteration-constructs-4
388 (doeach (i object)
389 (document.write (+ i " is " (aref object i))))
390 "for (var i in object) {
391 document.write(i + ' is ' + object[i]);
392 }")
393
394 (test-ps-js iteration-constructs-5
395 (while (film.is-not-finished)
396 (this.eat (new *popcorn)))
397 "while (film.isNotFinished()) {
398 this.eat(new Popcorn);
399 }")
400
401 (test-ps-js the-case-statement-1
402 (case (aref blorg i)
403 ((1 "one") (alert "one"))
404 (2 (alert "two"))
405 (t (alert "default clause")))
406 "switch (blorg[i]) {
407 case 1: ;
408 case 'one':
409 alert('one');
410 break;
411 case 2:
412 alert('two');
413 break;
414 default: alert('default clause');
415 }")
416
417 (test-ps-js the-case-statement-2
418 (switch (aref blorg i)
419 (1 (alert "If I get here"))
420 (2 (alert "I also get here"))
421 (default (alert "I always get here")))
422 "switch (blorg[i]) {
423 case 1: alert('If I get here');
424 case 2: alert('I also get here');
425 default: alert('I always get here');
426 }")
427
428 (test-ps-js the-with-statement-1
429 (with ((create :foo "foo" :i "i"))
430 (alert (+ "i is now intermediary scoped: " i)))
431 "with ({ foo : 'foo',
432 i : 'i' }) {
433 alert('i is now intermediary scoped: ' + i);
434 }")
435
436 (test-ps-js the-try-statement-1
437 (try (throw "i")
438 (:catch (error)
439 (alert (+ "an error happened: " error)))
440 (:finally
441 (alert "Leaving the try form")))
442 "try {
443 throw 'i';
444 } catch (error) {
445 alert('an error happened: ' + error);
446 } finally {
447 alert('Leaving the try form');
448 }")
449
450 (test-ps-js the-html-generator-1
451 (html ((:a :href "foobar") "blorg"))
452 "'<a href=\"foobar\">blorg</a>'")
453
454 (test-ps-js the-html-generator-2
455 (html ((:a :href (generate-a-link)) "blorg"))
456 "'<a href=\"' + generateALink() + '\">blorg</a>'")
457
458 (test-ps-js the-html-generator-3
459 (document.write
460 (html ((:a :href "#"
461 :onclick (js-inline (transport))) "link")))
462 "document.write
463 ('<a href=\"#\" onclick=\"' + 'javascript:transport();' + '\">link</a>')")
464
465 (test-ps-js the-html-generator-4
466 (css-inline :color "red"
467 :font-size "x-small")
468 "'color:red;font-size:x-small'")
469
470 (test-ps-js the-html-generator-5
471 (defun make-color-div(color-name)
472 (return (html ((:div :style (css-inline :color color-name))
473 color-name " looks like this."))))
474 "function makeColorDiv(colorName) {
475 return '<div style=\"' + ('color:' + colorName) + '\">' + colorName
476 + ' looks like this.</div>';
477 }")
478
479
480 (run-tests)