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