Changed extras/js-expander.el to work with the latest CVS snapshot of SLIME.
[clinton/parenscript.git] / t / ps-tests.lisp
CommitLineData
171bbab3 1(in-package :ps-test)
13b8268e
VS
2
3;;; Hand-written unit tests
e2932347
LC
4
5(eval-when (:compile-toplevel :load-toplevel :execute)
6 (def-suite ps-tests))
7(in-suite ps-tests)
8
9(test-ps-js plus-is-not-commutative
10 (setf x (+ "before" x "after"))
72332f2a 11 "x = 'before' + x + 'after';")
e2932347
LC
12
13(test-ps-js plus-works-if-first
14 (setf x (+ x "middle" "after"))
72332f2a 15 "x += 'middle' + 'after';")
e2932347
LC
16
17(test-ps-js setf-side-effects
58c4ef4f
VS
18 (progn
19 (let* ((x 10))
20 (defun side-effect()
21 (setf x 4)
22 (return 3))
23 (setf x (+ 2 (side-effect) x 5))))
24 "var x = 10;
e2932347
LC
25function sideEffect() {
26 x = 4;
27 return 3;
28};
29x = 2 + sideEffect() + x + 5;")
58c4ef4f 30;; Parenscript used to optimize too much:
e2932347
LC
31;; var x = 10;
32;; function sideEffect() {
33;; x = 4;
34;; return 3;
35;; };
36;; x += 2 + sideEffect() + 5;
37;;
38;; Which is 20, not 14
39
40
41(test-ps-js dot-notation-bug
42 (.match (+ "" x) "foo")
43 "('' + x).match('foo')")
44
45(test-ps-js method-call-op-form (.to-string (+ "" x)) "('' + x).toString()")
cf460f93 46(test-ps-js method-call-number (.to-string 10) "( 10 ).toString()")
e2932347
LC
47(test-ps-js method-call-string (.to-string "hi") "'hi'.toString()")
48(test-ps-js method-call-lit-object
49 (.to-string (create :to-string (lambda ()
b508414b 50 (return "it works"))))
cf460f93 51 "( { toString : function () { return 'it works'; } } ).toString()")
e2932347
LC
52
53(test-ps-js method-call-variable
54 (.to-string x)
55 "x.toString()")
56
57(test-ps-js method-call-array
58 (.to-string (list 10 20))
cf460f93 59 "[ 10, 20 ].toString()")
e2932347
LC
60(test-ps-js method-call-fn-call
61 (.to-string (foo))
62 "foo().toString()")
63(test-ps-js method-call-lambda-fn
64 (.to-string (lambda () (alert 10)))
cf460f93 65 "( function () { alert(10); } ).toString()")
e2932347
LC
66(test-ps-js method-call-lambda-call
67 (.to-string ((lambda (x) (return x)) 10))
b44afd8f 68 "(function (x) { return x; })(10).toString()")
e2932347
LC
69
70(test no-whitespace-before-dot
905f534e 71 (let* ((str (compile-script '(.to-string ((lambda (x) (return x)) 10))))
e2932347
LC
72 (dot-pos (position #\. str :test #'char=))
73 (char-before (elt str (1- dot-pos)))
74 (a-parenthesis #\)))
75 (is (char= char-before a-parenthesis))))
76
e2932347 77(test-ps-js simple-slot-value
58c4ef4f 78 (let* ((foo (create :a 1)))
f2bb932e 79 (alert (slot-value foo 'a)))
07afea38
VS
80 "var foo = { a : 1 };
81 alert(foo.a);")
e2932347
LC
82
83(test-ps-js buggy-slot-value
58c4ef4f 84 (let* ((foo (create :a 1))
f2bb932e 85 (slot-name "a"))
e2932347 86 (alert (slot-value foo slot-name)))
07afea38 87 " var foo = { a : 1 };
e2932347
LC
88 var slotName = 'a';
89 alert(foo[slotName]);
07afea38 90 "); Last line was alert(foo.slotName) before bug-fix.
e2932347
LC
91
92(test-ps-js buggy-slot-value-two
93 (slot-value foo (get-slot-name))
94 "foo[getSlotName()]")
95
96(test-ps-js old-case-is-now-switch
97 ;; Switch was "case" before, but that was very non-lispish.
98 ;; For example, this code makes three messages and not one
99 ;; which may have been expected. This is because a switch
100 ;; statment must have a break statement for it to return
101 ;; after the alert. Otherwise it continues on the next
102 ;; clause.
103 (switch (aref blorg i)
104 (1 (alert "one"))
105 (2 (alert "two"))
106 (default (alert "default clause")))
107 "switch (blorg[i]) {
108 case 1: alert('one');
109 case 2: alert('two');
110 default: alert('default clause');
111 }")
112
113(test-ps-js lisp-like-case
114 (case (aref blorg i)
115 (1 (alert "one"))
116 (2 (alert "two"))
117 (default (alert "default clause")))
118 "switch (blorg[i]) {
119 case 1:
120 alert('one');
121 break;
122 case 2:
123 alert('two');
124 break;
125 default: alert('default clause');
126 }")
127
128
129(test-ps-js even-lispier-case
130 (case (aref blorg i)
131 ((1 2) (alert "Below three"))
132 (3 (alert "Three"))
133 (t (alert "Something else")))
134 "switch (blorg[i]) {
b44afd8f 135 case 1:
e2932347
LC
136 case 2:
137 alert('Below three');
138 break;
139 case 3:
140 alert('Three');
141 break;
142 default: alert('Something else');
143 }")
144
145(test-ps-js otherwise-case
146 (case (aref blorg i)
147 (1 (alert "one"))
e0f0d152 148 (otherwise (alert "default clause")))
e2932347
LC
149 "switch (blorg[i]) {
150 case 1:
151 alert('one');
152 break;
153 default: alert('default clause');
154 }")
155
156(test escape-sequences-in-string
905f534e 157 (let ((escapes `((#\\ . #\\)
e2932347
LC
158 (#\b . #\Backspace)
159 (#\f . ,(code-char 12))
160 ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
161 (#\n . #\Newline)
162 (#\r . #\Return)
163 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
164 (#\t . #\Tab)
165 ("u001F" . ,(code-char #x001f));; character below 32
166 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
167 ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
168 (loop for (js-escape . lisp-char) in escapes
b508414b
TC
169 for generated = (compile-script `(let* ((x ,(format nil "hello~ahi" lisp-char)))))
170 for wanted = (format nil "var x = 'hello\\~ahi';" js-escape)
171 do (is (string= (normalize-js-code generated) wanted)))))
9da682ca 172
e0f0d152
VS
173(test-ps-js complicated-symbol-name1
174 grid-rows[foo].bar
175 "gridRows[foo].bar")
176
177(test-ps-js complicated-symbol-name2
178 *grid-rows*[foo].bar
179 "GRIDROWS[foo].bar")
cf460f93
VS
180
181(test-ps-js slot-value-setf
182 (setf (slot-value x 'y) (+ (+ a 3) 4))
72332f2a 183 "x.y = (a + 3) + 4;")
eb0befe2
VS
184
185(test-ps-js slot-value-conditional1
186 (slot-value (if zoo foo bar) 'x)
187 "(zoo ? foo : bar).x")
188
189(test-ps-js slot-value-conditional2
190 (slot-value (if (not zoo) foo bar) 'x)
191 "(!zoo ? foo : bar).x")
192
245d0fbd 193(test script-star-eval1
07afea38 194 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
245d0fbd
VS
195
196(test script-star-eval2
07afea38 197 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
62b4a867
VS
198
199(test-ps-js slot-value-null1
200 (slot-value foo nil)
201 "foo")
202
203(test-ps-js slot-value-null2
204 (slot-value foo 'nil)
205 "foo")
206
46f794a4
RD
207(test-ps-js unquoted-nil
208 nil
209 "null")
210
211(test-ps-js list-with-single-nil
212 (array 'nil)
213 "[null]")
214
62b4a867
VS
215(test-ps-js quoted-nil
216 'nil
72332f2a
VS
217 "null")
218
219(test defsetf1
220 (ps (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval)))
07afea38 221 (is (string= "var _js2 = 1; var _js3 = 2; var _js1 = 3; setBaz(_js2, _js3, _js1);"
58c4ef4f 222 (normalize-js-code (let* ((ps:*ps-gensym-counter* 0))
72332f2a 223 (ps (setf (baz 1 2) 3)))))))
d989d711 224
750651b0
VS
225(test defsetf-short
226 (ps (defsetf baz set-baz "blah"))
227 (is (string= "setBaz(1, 2, 3, 'foo');" (normalize-js-code (ps (setf (baz 1 2 3) "foo"))))))
228
dbb7017b
VS
229(test defun-setf1
230 (is (and (string= (normalize-js-code (ps:ps (defun (setf some-thing) (new-val i1 i2)
231 (setf (aref *some-thing* i1 i2) new-val))))
13b8268e 232 "function __setf_someThing(newVal, i1, i2) { SOMETHING[i1][i2] = newVal; };")
07afea38
VS
233 (string= (let ((ps:*ps-gensym-counter* 0)) (normalize-js-code (ps:ps (setf (some-thing 1 2) "foo"))))
234 "var _js2 = 1; var _js3 = 2; var _js1 = 'foo'; __setf_someThing(_js1, _js2, _js3);"))))
dbb7017b 235
d989d711
VS
236(test-ps-js defun-optional1
237 (defun test-opt (&optional x) (return (if x "yes" "no")))
238 "function testOpt(x) {
0e198f66
TC
239 if (x === undefined) {
240 x = null;
241 };
242 return x ? 'yes' : 'no';
a2434734
VS
243}")
244
5ac90695
VS
245(test-ps-js defun-optional2
246 (defun foo (x &optional y) (+ x y))
247 "function foo(x, y) {
0e198f66
TC
248 if (y === undefined) {
249 y = null;
250 };
5ac90695
VS
251 x + y;
252}")
253
7be02bed
VS
254(test-ps-js defun-optional3
255 (defun blah (&optional (x 0))
256 (return x))
257 "function blah(x) {
258 if (x === undefined) {
259 x = 0;
260 };
261 return x;
262}")
263
a2434734
VS
264(test-ps-js return-nothing
265 (return)
266 "return null")
dbb7017b
VS
267
268(test-ps-js set-timeout
269 (do-set-timeout (10) (alert "foo"))
b44afd8f 270 "setTimeout(function () { alert('foo'); }, 10)")
07afea38
VS
271(test-ps-js operator-precedence
272 (* 3 (+ 4 5) 6)
273 "3 * (4 + 5) * 6")
274
2d2c6dc2
TC
275(test-ps-js operators-1
276 (in prop obj)
277 "prop in obj")
278
07afea38
VS
279(test-ps-js incf1
280 (incf foo bar)
281 "foo += bar")
282
283(test-ps-js decf1
284 (decf foo bar)
285 "foo -= bar")
286
49c50da4
VS
287(test-ps-js incf2
288 (incf x 5)
289 "x += 5")
290
291(test-ps-js decf2
292 (decf y 10)
293 "y -= 10")
294
07afea38
VS
295(test-ps-js setf-conditional
296 (setf foo (if x 1 2))
297 "foo = x ? 1 : 2;")
298
299(test-ps-js obj-literal-numbers
300 (create 1 "foo")
301 "{ 1 : 'foo' }")
302
303(test-ps-js obj-literal-strings
304 (create "foo" 2)
a805b30d
VS
305 "{ 'foo' : 2 }")
306
307(test-ps-js slot-value-string
308 (slot-value foo "bar")
309 "foo['bar']")
b44afd8f
VS
310
311(test-ps-js slot-value-progn
13b8268e
VS
312 (slot-value (progn (some-fun "abc") "123") "length")
313 "(someFun('abc'), '123')['length']")
b44afd8f
VS
314
315(test-ps-js method-call-block
13b8268e
VS
316 (.to-string (progn (some-fun "abc") "123"))
317 "(someFun('abc'), '123').toString()")
b44afd8f
VS
318
319(test-ps-js create-blank
320 (create)
321 "{ }")
322
323(test-ps-js blank-object-literal
5ac90695 324 {}
667e3784 325 "{ }")
44934751 326
d91b35a2
TC
327(test-ps-js object-literal-1
328 ({})
329 "{ }")
330
331(test-ps-js object-literal-2
332 ({} a 1 b 2)
333 "{a: 1, b: 2 }")
334
44934751
VS
335(test-ps-js defun-rest1
336 (defun foo (&rest bar) (alert bar[1]))
337 "function foo() {
338 var bar = [];
d777a405 339 for (var i2 = 0; i2 < arguments.length - 0; i2 += 1) {
b5e0bcb7 340 bar[i2] = arguments[i2 + 0];
44934751
VS
341 };
342 alert(bar[1]);
343}")
344
345(test-ps-js defun-rest2
346 (defun foo (baz &rest bar) (return (+ baz (aref bar 1))))
2e593e4c 347 "function foo(baz) {
44934751 348 var bar = [];
d777a405 349 for (var i2 = 0; i2 < arguments.length - 1; i2 += 1) {
b5e0bcb7 350 bar[i2] = arguments[i2 + 1];
44934751
VS
351 };
352 return baz + bar[1];
353}")
2e593e4c
VS
354
355(test-ps-js defun-keyword1
356 (defun zoo (foo bar &key baz) (return (+ foo bar baz)))
357 "function zoo(foo, bar, _js1) {
0e198f66
TC
358 if (_js1 === undefined) {
359 _js1 = { };
360 };
2e593e4c
VS
361 return foo + bar + _js1.baz;
362}")
b0f64e9b 363
f2bb932e
VS
364(test-ps-js defun-keyword2
365 (defun zoo (&key baz) (return (* baz baz)))
366 "function zoo(_js1) {
367 if (_js1 === undefined) {
368 _js1 = { };
369 };
370 return _js1.baz * _js1.baz;
371}")
372
373(test-ps-js defun-keyword3
374 (defun zoo (&key baz (bar 4)) (return (* baz bar)))
375 "function zoo(_js1) {
376 if (_js1 === undefined) {
377 _js1 = { };
378 };
379 if (_js1.bar === undefined) {
380 _js1.bar = 4;
381 };
382 return _js1.baz * _js1.bar;
383}")
384
385(test-ps-js keyword-funcall1
386 (func :baz 1)
387 "func({ baz : 1 })")
388
389(test-ps-js keyword-funcall2
390 (func :baz 1 :bar foo)
391 "func({ baz : 1, bar : foo })")
392
393(test-ps-js keyword-funcall3
394 (fun a b :baz c)
395 "fun(a, b, { baz : c })")
396
b0f64e9b
VS
397(test-ps-js cond1
398 (cond ((= x 1) 1))
399 "if (x == 1) {
400 1;
401}")
402
403(test-ps-js cond2
13b8268e 404 (cond ((= x 1) 2) ((= y (* x 4)) (foo "blah") (* x y)))
b0f64e9b
VS
405 "if (x == 1) {
406 2;
407} else if (y == x * 4) {
13b8268e 408 foo('blah');
b0f64e9b
VS
409 x * y;
410}")
5705b542
VS
411
412(test-ps-js if-exp-without-else-returns-null
413 (return (if x 1))
414 "return x ? 1 : null")
415
416(test-ps-js progn-expression-single-statement
417 (return (progn (* x y)))
418 "return x * y")
0949f072
VS
419
420(test-ps-js cond-expression1
13b8268e 421 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)))))
0949f072 422 "function foo() {
13b8268e 423 return 1 < 2 ? (bar('foo'), 4 * 5) : null;
0949f072
VS
424}")
425
426(test-ps-js cond-expression2
427 (defun foo () (return (cond ((< 2 1) "foo") ((= 7 7) "bar"))))
428 "function foo() {
429 return 2 < 1 ? 'foo' : (7 == 7 ? 'bar' : null);
430}")
431
432(test-ps-js cond-expression-final-t-clause
13b8268e 433 (defun foo () (return (cond ((< 1 2) (bar "foo") (* 4 5)) ((= a b) (+ c d)) ((< 1 2 3 4 5) x) (t "foo"))))
0949f072 434 "function foo() {
13b8268e 435 return 1 < 2 ? (bar('foo'), 4 * 5) : (a == b ? c + d : (1 < 2 < 3 < 4 < 5 ? x : 'foo'));
0949f072
VS
436}")
437
438(test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
439 (defun foo () (return (cond ((< 2 1) 5) (t "foo") ((< 1 2) "bar"))))
440 "function foo() {
441 return 2 < 1 ? 5 : 'foo';
442}")
e5253c5b
VS
443
444(test-ps-js funcall-if-expression
445 (document.write
446 (if (= *linkornot* 1)
447 (ps-html ((:a :href "#"
448 :onclick (lisp (ps-inline (transport))))
449 img))
450 img))
1937c30a 451 "document.write(LINKORNOT == 1 ? '<A HREF=\"#\" ONCLICK=\"' + 'javascript:transport()' + '\">' + img + '</A>' : img)")
49c50da4
VS
452
453(test-ps-js negate-number-literal ;; ok, this was broken and fixed before, but no one bothered to add the test!
454 (- 1)
455 "-1")
efe35e12
VS
456
457(test macro-environment1
58c4ef4f 458 (is (string= (normalize-js-code (let* ((macroname (gensym)))
efe35e12
VS
459 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
460 `(defun test1 ()
461 (macrolet ((,macroname (x) `(aref data ,x)))
462 (when (,macroname x)
463 (setf (,macroname x) 123)))))))
464 (normalize-js-code
465"function test1() {
466 if (data[x]) {
467 data[x] = 123;
468 };
469};
470"))))
921f2e02
VS
471
472(test-ps-js ampersand-whole-1
473 (macrolet ((foo (&whole foo bar baz)
474 (declare (ignore bar baz))
475 (format nil "~a" foo)))
476 (foo 1 2))
6e8ff198
VS
477 "'(FOO 1 2)';")
478
479(test-ps-js keyword-consistent
480 :x
f2bb932e 481 "'x'")
43a1d5c3
VS
482
483(test-ps-js simple-symbol-macrolet
484 (symbol-macrolet ((x 1)) x)
485 "1;")
486
487(test-ps-js compound-symbol-macrolet
488 (symbol-macrolet ((x 123)
489 (y (* 2 x)))
490 y)
491 "2 * 123;")
1af04ae5
VS
492
493(test-ps-js define-symbol-macro
494 (progn (define-symbol-macro tst-sym-macro 2)
495 tst-sym-macro)
496 "2;")
e0032a96
VS
497
498(test-ps-js expression-progn
499 (defun f () (return (progn (foo) (if x 1 2))))
500 "function f() {
501 return (foo(), x ? 1 : 2);
502}")
503
504(test-ps-js let-decl-in-expression
58c4ef4f 505 (defun f (x) (return (if x 1 (let* ((foo x)) foo))))
e0032a96
VS
506 "function f(x) {
507 var foo;
508 return x ? 1 : (foo = x, foo);
509}")
58c4ef4f
VS
510
511(test-ps-js special-var1
512 (progn (defvar *foo*) (let* ((*foo* 2)) (* *foo* 2)))
513 "var FOO;
514var tempstackvar1;
515try {
516 tempstackvar1 = FOO;
517 FOO = 2;
518 FOO * 2;
519} finally {
520 FOO = tempstackvar1;
d777a405 521 delete tempstackvar1;
58c4ef4f
VS
522};")
523
524(test-ps-js special-var2
525 (progn (defvar *foo*) (let* ((*baz* 3) (*foo* 2)) (* *foo* 2 *baz*)))
526 "var FOO;
527var BAZ = 3;
528var tempstackvar1;
529try {
530 tempstackvar1 = FOO;
531 FOO = 2;
532 FOO * 2 * BAZ;
533} finally {
534 FOO = tempstackvar1;
d777a405
TC
535 delete tempstackvar1;
536};
537")
5ac90695
VS
538
539(test-ps-js literal1
540 (setf x undefined)
541 "x = undefined;")
542
543(test-ps-js literal2
544 (aref this x)
545 "this[x]")
d89456ee
VS
546
547(test-ps-js setf-dec1
548 (setf x (- 1 x 2))
549 "x = 1 - x - 2;")
550
551(test-ps-js setf-dec2
552 (setf x (- x 1 2))
553 "x = x - 1 - 2;")
93e99720
VS
554
555(test-ps-js special-char-equals
556 blah=
557 "blahequals")
7be02bed
VS
558
559(test-ps-js setf-operator-priority
560 (return (or (slot-value cache id)
561 (setf (slot-value cache id) (document.get-element-by-id id))))
562 "return cache[id] || (cache[id] = document.getElementById(id))")
563
564(test-ps-js aref-operator-priority
565 (aref (if (and x (> (length x) 0))
566 (aref x 0)
567 y)
568 z)
569 "(x && x.length > 0 ? x[0] : y)[z]")
570
571(test-ps-js aref-operator-priority1
572 (aref (or (slot-value x 'y)
573 (slot-value a 'b))
574 z)
575 "(x.y || a.b)[z]")
576
577(test-ps-js aref-operator-priority2
578 (aref (if a b c) 0)
579 "(a ? b : c)[0]")
580
581(test-ps-js negative-operator-priority
582 (- (if x y z))
583 "-(x ? y : z)")
584
585(test-ps-js op-p1
586 (new (or a b))
587 "new (a || b)")
588
589(test-ps-js op-p2
590 (delete (if a (or b c) d))
591 "delete (a ? b || c : d)")
592
593(test-ps-js op-p3
594 (not (if (or x (not y)) z))
595 "!(x || !y ? z : null)")
596
597(test-ps-js op-p4
598 (- (- (* 1 2) 3))
599 "-(1 * 2 - 3)")
600
601(test-ps-js op-p5
602 (instanceof (or a b) (if x y z))
603 "((a || b) instanceof (x ? y : z))")
604
605(test-ps-js op-p6
606 (doeach (x (or a b)))
607 "for (var x in (a || b)) { };")
608
609(test-ps-js op-p7
610 (or x (if (= x 0) "zero" "empty"))
611 "x || (x == 0 ? 'zero' : 'empty')")
612
613(test-ps-js named-op-expression
614 (throw (if a b c))
615 "throw a ? b : c")
616
617(test-ps-js named-op-expression1
618 (typeof (or x y))
619 "typeof (x || y)")
9b31227f
VS
620
621(test-ps-js aref-array-expression
622 (aref (or a b c) 0)
623 "(a || b || c)[0]")
624
625(test-ps-js slot-value-operator
626 (slot-value (or a b c) 'd)
627 "(a || b || c).d")
628
629(test-ps-js slot-value-parens
630 (slot-value (slot-value foo 'bar) 'baz)
631 "foo.bar.baz")