output-tests.lisp 90.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
;;; Copyright 2005-2006 Henrik Hjelte
;;; Copyright 2007-2012 Vladimir Sedach

;;; SPDX-License-Identifier: BSD-3-Clause

;;; Redistribution and use in source and binary forms, with or
;;; without modification, are permitted provided that the following
;;; conditions are met:

;;; 1. Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.

;;; 2. Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials provided
;;; with the distribution.

;;; 3. Neither the name of the copyright holder nor the names of its
;;; contributors may be used to endorse or promote products derived
;;; from this software without specific prior written permission.

;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
;;; CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
;;; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
;;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
;;; BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
;;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
;;; ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;;; POSSIBILITY OF SUCH DAMAGE.
35

36
(in-package #:parenscript.tests)
37
(named-readtables:in-readtable :parenscript)
38

39
(fiveam:in-suite output-tests)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

(test-ps-js statements-and-expressions-1
  (+ i (if 1 2 3))
  "i + (1 ? 2 : 3);")

(test-ps-js statements-and-expressions-2
  (if 1 2 3)
  "if (1) {
    2;
} else {
    3;
};")

(test-ps-js symbol-conversion-1
  !?#@%
  "bangwhathashatpercent;")

(test-ps-js symbol-conversion-2
  bla-foo-bar
  "blaFooBar;")

(test-ps-js symbol-conversion-3
  *array
  "Array;")

(test-ps-js symbol-conversion-4
  *global-array*
  "GLOBALARRAY;")

69
70
71
72
73
74
75
76
(test-ps-js symbol-conversion-5
  encodeURIComponent
  "encodeURIComponent;")

(test-ps-js symbol-conversion-6
  URI
  "URI;")

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(test-ps-js number-literals-1
  1
  "1;")

(test-ps-js number-literals-2
  123.123
  "123.123;")

(test-ps-js number-literals-3
  #x10
  "16;")

(test-ps-js string-literals-1
  "foobar"
  "'foobar';")

(test-ps-js string-literals-2
  "bratzel bub"
  "'bratzel bub';")

(test-ps-js string-literals-3
  "	"
  "'\\t';")

(test-ps-js array-literals-1
  (array)
103
  "[];")
104
105
106

(test-ps-js array-literals-2
  (array 1 2 3)
107
  "[1, 2, 3];")
108
109
110

(test-ps-js array-literals-3
  (array (array 2 3)
111
         (array "foobar" "bratzel bub"))
112
  "[[2, 3], ['foobar', 'bratzel bub']];")
113
114
115
116
117
118
119
120
121
122
123

(test-ps-js array-literals-4
  (make-array)
  "new Array();")

(test-ps-js array-literals-5
  (make-array 1 2 3)
  "new Array(1, 2, 3);")

(test-ps-js array-literals-6
  (make-array
124
125
   (make-array 2 3)
   (make-array "foobar" "bratzel bub"))
126
127
  "new Array(new Array(2, 3), new Array('foobar', 'bratzel bub'));")

128
129
130
(test-ps-js array-init-1
  (make-array 2 :initial-contents '(10 20))
  "(function () {
131
132
133
134
135
136
137
    var arr1 = new Array(2);
    var init2 = [10, 20];
    for (var i4 = 0; i4 < Math.min(arr1.length, init2.length); i4 += 1) {
        arr1[i4] = init2[i4];
    };
    return arr1;
})();")
138
139
140
141

(test-ps-js array-init-2
  (make-array 5 :initial-element 10)
  "(function () {
142
143
144
145
146
147
148
    var arr1 = new Array(5);
    var elt3 = 10;
    for (var i4 = 0; i4 < arr1.length; i4 += 1) {
        arr1[i4] = elt3;
    };
    return arr1;
})();")
149

150
151
(test-ps-js object-literals-1
  (create foo "bar" :blorg 1)
152
  "({ foo : 'bar', 'blorg' : 1 });")
153
154
155

(test-ps-js object-literals-2
  (create foo "hihi"
156
157
          blorg (array 1 2 3)
          another-object (create :schtrunz 1))
158
  "({ foo : 'hihi',
159
  blorg : [1, 2, 3],
160
  anotherObject : { 'schtrunz' : 1 } });")
161
162
163
164
165
166
167
168
169
170
171

(test-ps-js object-literals-3
  (getprop an-object 'foo)
  "anObject.foo;")

(test-ps-js object-literals-4
  (@ an-object foo bar)
  "anObject.foo.bar;")

(test-ps-js object-literals-5
  (with-slots (a b c) this
172
    (+ a b c))
173
174
  "this.a + this.b + this.c;")

175
176
177
178
179
180
181
(test-ps-js with-slots-single-eval
  (lambda () (with-slots (a b) (foo) (+ a b)))
  "(function () {
    var object1 = foo();
    return object1.a + object1.b;
});")

182
183
184
185
(test-ps-js object-literal-quoted-symbols
  (create 'test "bang" 'symbol-saved-my-life "parenscript")
  "({ 'test' : 'bang', 'symbolSavedMyLife' : 'parenscript' });")

186
187
188
189
190
191
192
193
194
195
196
197
(test-ps-js object-literal-property-accessors
  (defun foo ()
    (let ((x 10))
      (create (get x)   x
              (set x v) (setf x v))))
  "function foo() {
    var x = 10;
    return { get x() {
        return x;
    }, set x(v) {
        return x = v;
    } };
198
199
};"
  :js-target-version "1.8.5")
200

201
202
(test-ps-js object-method-apply-1
  (apply (@ an-object foo) nil)
203
  "anObject.foo.apply(anObject, null);")
204
205
206
207
208
209
210
211
212

(test-ps-js object-method-apply-2
  (apply (getprop (make-an-object) foo 'bar) nil)
  "(function () {
      var _js1 = makeAnObject()[foo];
      var _js2 = _js1.bar;
      return _js2.apply(_js1, null);
    })();")

213
214
215
216
217
218
219
220
(test-ps-js object-method-apply-3
  (apply (@ (make-an-object) foo) (bar))
  "(function () {
    var _js1 = makeAnObject();
    var _js2 = _js1.foo;
    return _js2.apply(_js1, bar());
})();")

221
222
223
224
225
226
227
228
229
(test-ps-js regular-expression-literals-1
  (regex "foobar")
  "/foobar/;")

(test-ps-js regular-expression-literals-2
  (regex "/foobar/i")
  "/foobar/i;")

(test-ps-js literal-symbols-1
230
  t
231
232
233
  "true;")

(test-ps-js literal-symbols-2
234
  false
235
236
237
  "false;")

(test-ps-js literal-symbols-3
238
  f
239
240
241
  "false;")

(test-ps-js literal-symbols-4
242
  (lambda () nil)
243
  "(function () {
244
    return null;
245
});")
246
247

(test-ps-js literal-symbols-5
248
  undefined
249
250
251
  "undefined;")

(test-ps-js literal-symbols-6
252
  this
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  "this;")

(test-ps-js variables-1
  variable
  "variable;")

(test-ps-js variables-2
  a-variable
  "aVariable;")

(test-ps-js variables-3
  *math
  "Math;")

(test-ps-js function-calls-and-method-calls-1
  (blorg 1 2)
  "blorg(1, 2);")

(test-ps-js function-calls-and-method-calls-2
  (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
273
  "foobar(blorg(1, 2), blabla(3, 4), [2, 3, 4]);")
274
275
276
277
278
279
280
281
282
283

(test-ps-js function-calls-and-method-calls-3
  ((getprop this 'blorg) 1 2)
  "this.blorg(1, 2);")

(test-ps-js function-calls-and-method-calls-4
  ((aref foo i) 1 2)
  "foo[i](1, 2);")

(test-ps-js function-calls-and-method-calls-5
284
  ((getprop (aref foobar 1) 'blorg) nil t)
285
286
287
288
289
290
291
292
293
294
295
296
  "foobar[1].blorg(null, true);")

(test-ps-js operator-expressions-1
  (* 1 2)
  "1 * 2;")

(test-ps-js operator-expressions-2
  (= 1 2)
  "1 === 2;")

(test-ps-js operator-expressions-3
  (* 1 (+ 2 3 4) 4 (/ 6 7))
297
  "1 * (2 + 3 + 4) * 4 * (6 / 7);")
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334

(test-ps-js operator-expressions-4
  (incf i)
  "++i;")

(test-ps-js operator-expressions-5
  (decf i)
  "--i;")

(test-ps-js operator-expressions-6
  (1- i)
  "i - 1;")

(test-ps-js operator-expressions-7
  (1+ i)
  "i + 1;")

(test-ps-js operator-expressions-8
  (not (< i 2))
  "i >= 2;")

(test-ps-js body-forms-1
  (progn (blorg i) (blafoo i))
  "blorg(i);
blafoo(i);")

(test-ps-js body-forms-2
  (+ i (progn (blorg i) (blafoo i)))
  "i + (blorg(i), blafoo(i));")

(test-ps-js function-definition-1
  (defun a-function (a b)
    (+ a b))
  "function aFunction(a, b) {
    return a + b;
};")

335
(test-ps-js lambda-definition-2
336
  (lambda (a b) (+ a b))
337
  "(function (a, b) {
338
    return a + b;
339
});")
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

(test-ps-js assignment-1
  (setf a 1)
  "a = 1;")

(test-ps-js assignment-2
  (setf a 2 b 3 c 4 x (+ a b c))
  "a = 2;
b = 3;
c = 4;
x = a + b + c;")

(test-ps-js assignment-3
  (setf a (+ a 2 3 4 a))
  "a = a + 2 + 3 + 4 + a;")

(test-ps-js assignment-4
  (setf a (- 1 a))
  "a = 1 - a;")

(test-ps-js assignment-5
  (let ((a 1) (b 2))
362
363
364
    (psetf a b b a))
  "(function () {
var a = 1;
365
var b = 2;
366
367
368
369
var _js3 = b;
var _js4 = a;
a = _js3;
return b = _js4;
370
})();")
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

(test-ps-js assignment-6
  (setq a 1)
  "a = 1;")

(test-ps-js assignment-8
  (progn
    (defun (setf color) (new-color el)
      (setf (getprop (getprop el 'style) 'color) new-color))
    (setf (color some-div) (+ 23 "em")))
  "function __setf_color(newColor, el) {
    return el.style.color = newColor;
};
__setf_color(23 + 'em', someDiv);")

(test-ps-js assignment-10
  (progn
    (defsetf left (el) (offset)
      `(setf (getprop (getprop ,el 'style) 'left) ,offset))
    (setf (left some-div) (+ 123 "px")))
391
392
  "(function () {
var _js2 = someDiv;
393
var _js1 = 123 + 'px';
394
395
return _js2.style.left = _js1;
})();")
396
397
398
399
400
401
402
403

(test-ps-js assignment-12
  (macrolet ((left (el)
             `(getprop ,el 'offset-left)))
    (left some-div))
  "someDiv.offsetLeft;")

(test-ps-js nil-block-return-1
404
405
406
407
408
  (block nil (return) 1)
  "(function () {
    return null;
    return 1;
})();")
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466

(test-ps-js single-argument-statements-2
  (throw "foobar")
  "throw 'foobar';")

(test-ps-js single-argument-expression-1
  (delete (new (*foobar 2 3 4)))
  "delete new Foobar(2, 3, 4);")

(test-ps-js single-argument-expression-2
  (if (= (typeof blorg) *string)
    (alert (+ "blorg is a string: " blorg))
    (alert "blorg is not a string"))
  "if (typeof blorg === String) {
    alert('blorg is a string: ' + blorg);
} else {
    alert('blorg is not a string');
};")

(test-ps-js conditional-statements-1
  (defun foo ()
    (if ((@ blorg is-correct))
        (progn (carry-on) (return-from foo i))
        (alert "blorg is not correct!")))
"function foo() {
    if (blorg.isCorrect()) {
        carryOn();
        return i;
    } else {
        return alert('blorg is not correct!');
    };
};")

(test-ps-js conditional-statements-2
  (+ i (if ((@ blorg add-one)) 1 2))
  "i + (blorg.addOne() ? 1 : 2);")

(test-ps-js conditional-statements-3
  (defun foo ()
    (when ((@ blorg is-correct))
      (carry-on)
      (return-from foo i)))
  "function foo() {
    if (blorg.isCorrect()) {
        carryOn();
        return i;
    };
};")

(test-ps-js conditional-statements-4
  (unless ((@ blorg is-correct))
  (alert "blorg is not correct!"))
  "if (!blorg.isCorrect()) {
    alert('blorg is not correct!');
};")

(test-ps-js variable-declaration-1
  (defvar *a* (array 1 2 3))
467
  "if ('undefined' === typeof A) { var A = [1, 2, 3]; };")
468
469

(test-ps-js variable-declaration-2
470
471
472
473
474
475
  (progn (defvar *a* 4)
         (let ((x 1)
               (*a* 2))
           (let* ((y (+ x 1))
                  (x (+ x y)))
             (+ *a* x y))))
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  "if ('undefined' === typeof A) { var A = 4; };
(function () {
var x = 1;
var A_TMPSTACK1;
try {
    A_TMPSTACK1 = A;
    A = 2;
    var y = x + 1;
    var x2 = x + y;
    return A + x2 + y;
} finally {
    A = A_TMPSTACK1;
};
})();")

(test-ps-js variable-declaration-3
  (defparameter A 987)
  "var A = 987;")

(test-ps-js variable-declaration-4
  (progn (defparameter *a* 4)
         (let ((x 1)
               (*a* 2))
           (let* ((y (+ x 1))
                  (x (+ x y)))
             (+ *a* x y))))
502
  "var A = 4;
503
(function () {
504
505
506
507
508
509
510
var x = 1;
var A_TMPSTACK1;
try {
    A_TMPSTACK1 = A;
    A = 2;
    var y = x + 1;
    var x2 = x + y;
511
    return A + x2 + y;
512
513
} finally {
    A = A_TMPSTACK1;
514
515
};
})();")
516

517
518
519
520
(test-ps-js variable-declaration-5
  (defvar BAZ)
  "var BAZ;")

521
522
(test-ps-js iteration-constructs-1
  (do* ((a) b (c (array "a" "b" "c" "d" "e"))
523
524
525
526
527
        (d 0 (1+ d))
        (e (aref c d) (aref c d)))
       ((or (= d (@ c length)) (string= e "x")))
    (setf a d b e)
    (funcall (@ document write) (+ "a: " a " b: " b "<br/>")))
528
529
  "(function () {
for (var a = null, b = null, c = ['a', 'b', 'c', 'd', 'e'], d = 0, e = c[d];
530
       !(d === c.length || e === 'x'); d += 1, e = c[d]) {
531
532
533
    a = d;
    b = e;
    document.write('a: ' + a + ' b: ' + b + '<br/>');
534
535
};
})();")
536
537
538

(test-ps-js iteration-constructs-2
  (do ((i 0 (1+ i))
539
540
541
       (s 0 (+ s i (1+ i))))
      ((> i 10))
    (funcall (@ document write) (+ "i: " i " s: " s "<br/>")))
542
543
  "(function () {
var i = 0;
544
545
546
547
var s = 0;
for (; i <= 10; ) {
    document.write('i: ' + i + ' s: ' + s + '<br/>');
    var _js1 = i + 1;
548
    var _js2 = s + i + (i + 1);
549
550
    i = _js1;
    s = _js2;
551
552
};
})();")
553
554
555

(test-ps-js iteration-constructs-3
  (do* ((i 0 (1+ i))
556
557
558
        (s 0 (+ s i (1- i))))
       ((> i 10))
    ((@ document write) (+ "i: " i " s: " s "<br/>")))
559
560
  "(function () {
for (var i = 0, s = 0; i <= 10; i += 1, s = s + i + (i - 1)) {
561
    document.write('i: ' + i + ' s: ' + s + '<br/>');
562
563
};
})();")
564
565
566

(test-ps-js iteration-constructs-4
  (let ((arr (array "a" "b" "c" "d" "e")))
567
568
569
570
    (dotimes (i (@ arr length))
      ((@ document write) (+ "i: " i " arr[i]: " (aref arr i) "<br/>"))))
  "(function () {
var arr = ['a', 'b', 'c', 'd', 'e'];
571
572
for (var i = 0; i < arr.length; i += 1) {
    document.write('i: ' + i + ' arr[i]: ' + arr[i] + '<br/>');
573
574
};
})();")
575
576
577

(test-ps-js iteration-constructs-5
  (let ((res 0))
578
579
580
581
582
583
    (alert (+ "Summation to 10 is "
              (dotimes (i 10 res)
                (incf res (1+ i))))))
  "(function () {
var res = 0;
return alert('Summation to 10 is ' + (function () {
584
585
586
    for (var i = 0; i < 10; i += 1) {
        res += i + 1;
    };
587
    var i = null;
588
    return res;
589
590
})());
})();")
591
592
593

(test-ps-js iteration-constructs-6
  (let ((l (list 1 2 4 8 16 32)))
594
595
596
597
    (dolist (c l)
      ((@ document write) (+ "c: " c "<br/>"))))
  "(function () {
var l = [1, 2, 4, 8, 16, 32];
598
599
for (var c = null, _js_idx2 = 0; _js_idx2 < l.length; _js_idx2 += 1) {
    c = l[_js_idx2];
600
    document.write('c: ' + c + '<br/>');
601
602
};
})();")
603
604
605
606
607
608
609

(test-ps-js iteration-constructs-7
  (let ((l '(1 2 4 8 16 32))
        (s 0))
    (alert (+ "Sum of " l " is: "
              (dolist (c l s)
                (incf s c)))))
610
611
  "(function () {
var l = [1, 2, 4, 8, 16, 32];
612
var s = 0;
613
return alert('Sum of ' + l + ' is: ' + (function () {
614
615
616
617
    for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) {
        c = l[_js_idx1];
        s += c;
    };
618
    var c = null;
619
    return s;
620
621
})());
})();")
622
623
624

(test-ps-js iteration-constructs-8
  (let ((obj (create a 1 b 2 c 3)))
625
626
627
628
    (for-in (i obj)
            ((@ document write) (+ i ": " (aref obj i) "<br/>"))))
  "(function () {
var obj = { a : 1, b : 2, c : 3 };
629
630
for (var i in obj) {
    document.write(i + ': ' + obj[i] + '<br/>');
631
632
};
})();")
633
634

(test-ps-js iteration-constructs-9
635
636
637
638
  (loop while (funcall (@ film is-not-finished)) do
       (funcall (@ this eat) (new *popcorn)))
  "(function () {
while (film.isNotFinished()) {
639
    this.eat(new Popcorn);
640
641
};
}).call(this);")
642

Daniel Gackle's avatar
Daniel Gackle committed
643
644
(test-ps-js loop-for-bindings
  (loop :for ((a b) (:c :d)) :in arr :do (foo a b c d))
645
646
"(function () {
var _js2 = arr.length;
Daniel Gackle's avatar
Daniel Gackle committed
647
for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
648
649
650
651
652
    var _db4 = arr[_js1];
    var _db5 = _db4[0];
    var a = _db5[0];
    var b = _db5[1];
    var _js3 = _db4[1];
Daniel Gackle's avatar
Daniel Gackle committed
653
654
655
    var c = _js3['c'];
    var d = _js3['d'];
    foo(a, b, c, d);
656
657
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
658
659
660

(test-ps-js loop-for-on
  (loop :for (k v) :on plist :by 2 :do (foo k v))
661
662
  "(function () {
for (var _js1 = plist; _js1.length > 0; _js1 = _js1['slice'](2)) {
Daniel Gackle's avatar
Daniel Gackle committed
663
664
665
    var k = _js1[0];
    var v = _js1[1];
    foo(k, v);
666
667
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
668
669
670

(test-ps-js loop-for-keys-of
  (loop :for k :of obj :do (foo k))
671
672
  "(function () {
for (var k in obj) {
Daniel Gackle's avatar
Daniel Gackle committed
673
    foo(k);
674
675
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
676
677
678

(test-ps-js loop-for-key-val-pairs-of
   (loop :for (k v) :of obj :do (foo k v))
679
680
   "(function () {
for (var k in obj) {
Daniel Gackle's avatar
Daniel Gackle committed
681
682
    var v = obj[k];
    foo(k, v);
683
684
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
685
686
687

(test-ps-js loop-for-key-val-pairs-of-with-bindings
   (loop :for (k (a b)) :of obj :do (foo k a b))
688
689
"(function () {
for (var k in obj) {
690
691
692
    var _db1 = obj[k];
    var a = _db1[0];
    var b = _db1[1];
Daniel Gackle's avatar
Daniel Gackle committed
693
    foo(k, a, b);
694
695
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
696
697
698

(test-ps-js loop-for-just-vals-of
   (loop :for (nil v) :of obj :do (foo k v))
699
700
   "(function () {
for (var _js1 in obj) {
Daniel Gackle's avatar
Daniel Gackle committed
701
702
    var v = obj[_js1];
    foo(k, v);
703
704
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741

(test-ps-js loop-map-to
   (loop :for str :in strs :map str :to (length str))
"(function () {
    var _js2 = strs.length;
    var map3 = {  };
    for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
        var str = strs[_js1];
        map3[str] = str.length;
    };
    return map3;
})();")

(test-ps-js loop-for-of-map-to
   (loop :for k :of obj :map k :to (foo k))
"(function () {
    var map1 = {  };
    for (var k in obj) {
        map1[k] = foo(k);
    };
    return map1;
})();")

(test-ps-js loop-for-of-when
   (loop :for k :of obj :when (foo k) :map k :to (bar k))
"(function () {
    var map1 = {  };
    for (var k in obj) {
        if (foo(k)) {
            map1[k] = bar(k);
        };
    };
    return map1;
})();")

(test-ps-js loop-for-in-until-when
   (loop :for a :in b :until (> a 100) :when (< a 50) :do (foo a))
742
743
"(function () {
var _js2 = b.length;
Daniel Gackle's avatar
Daniel Gackle committed
744
745
746
747
748
749
750
751
for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
    var a = b[_js1];
    if (a > 100) {
        break;
    };
    if (a < 50) {
        foo(a);
    };
752
753
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
754
755
756
757
758

(test-ps-js loop-with-for-when
   (loop :with c = c1 :for d :from c1 :below c2
     :when (foo c d) :do (setf c d)
     :do (bar d))
759
760
"(function () {
var c = c1;
Daniel Gackle's avatar
Daniel Gackle committed
761
762
763
764
765
for (var d = c1; d < c2; d += 1) {
    if (foo(c, d)) {
        c = d;
    };
    bar(d);
766
767
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790

(test-ps-js loop-for-then-for-in-while
   (defun blah (c)
     (loop :for a = (foo) :then (bar) :for b :in c :while b :do (foo a b c)))
"function blah(c) {
    var _js2 = c.length;
    var FIRST3 = true;
    for (var a = foo(); true; a = bar()) {
        var _js1 = FIRST3 ? 0 : _js1 + 1;
        if (_js1 >= _js2) {
            break;
        };
        var b = c[_js1];
        if (!b) {
            break;
        };
        foo(a, b, c);
        FIRST3 = null;
    };
};")

(test-ps-js loop-while-when
   (loop :for a = (pop stack) :while a :for (b c) = (foo a) :when b :do (bar c))
791
792
"(function () {
for (var a = pop(stack); a; a = pop(stack)) {
793
794
795
    var _db1 = foo(a);
    var b = _db1[0];
    var c = _db1[1];
Daniel Gackle's avatar
Daniel Gackle committed
796
797
798
    if (b) {
        bar(c);
    };
799
800
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
801
802

(test-ps-js loop-for-of-for-in
803
804
  (defun blah (obj b)
    (loop :for k :of obj :for a :in b :do (foo k a)))
Daniel Gackle's avatar
Daniel Gackle committed
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
"function blah(obj, b) {
    var _js2 = b.length;
    var FIRST3 = true;
    for (var k in obj) {
        var _js1 = FIRST3 ? 0 : _js1 + 1;
        if (_js1 >= _js2) {
            break;
        };
        var a = b[_js1];
        foo(k, a);
        FIRST3 = null;
    };
};")

(test-ps-js loop-for-dot
   (loop :for (op . args) :in expr :do (foo op args))
821
822
"(function () {
var _js2 = expr.length;
Daniel Gackle's avatar
Daniel Gackle committed
823
for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
824
825
826
    var _db3 = expr[_js1];
    var op = _db3[0];
    var args = _db3.length > 1 ? _db3.slice(1) : [];
Daniel Gackle's avatar
Daniel Gackle committed
827
    foo(op, args);
828
829
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
830
831
832

(test-ps-js loop-for-rest
   (loop :for (op &rest args) :in expr :do (foo op args))
833
834
"(function () {
var _js2 = expr.length;
Daniel Gackle's avatar
Daniel Gackle committed
835
for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
836
837
838
    var _db3 = expr[_js1];
    var op = _db3[0];
    var args = _db3.length > 1 ? _db3.slice(1) : [];
Daniel Gackle's avatar
Daniel Gackle committed
839
    foo(op, args);
840
841
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
842
843
844
845
846
847
848
849

(test-ps-js loop-collect
   (setf x (loop :for a :in b :collect (foo a)))
"x = (function () {
    var _js2 = b.length;
    var collect3 = [];
    for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
        var a = b[_js1];
Daniel Gackle's avatar
Daniel Gackle committed
850
        collect3.push(foo(a));
Daniel Gackle's avatar
Daniel Gackle committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
    };
    return collect3;
})();")

(test-ps-js loop-append
   (loop :for a :in b :append a)
"(function () {
    var _js2 = b.length;
    var append3 = [];
    for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
        var a = b[_js1];
        append3 = append3.concat(a);
    };
    return append3;
})();")

867
868
(test-ps-js the-case-statement-1
  (case (aref blorg i)
869
870
871
    ((1 "one") (alert "one"))
    (2 (alert "two"))
    (t (alert "default clause")))
872
873
874
875
876
877
878
879
880
881
882
883
884
885
  "switch (blorg[i]) {
    case 1:
    case 'one':
        alert('one');
        break;
    case 2:
        alert('two');
        break;
    default:
        alert('default clause');
    };")

(test-ps-js the-case-statement-2
  (switch (aref blorg i)
886
887
888
    (1 (alert "If I get here"))
    (2 (alert "I also get here"))
    (default (alert "I always get here")))
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
  "switch (blorg[i]) {
    case 1: alert('If I get here');
    case 2: alert('I also get here');
    default: alert('I always get here');
};")

(test-ps-js the-try-statement-1
  (try (throw "i")
 (:catch (error)
   (alert (+ "an error happened: " error)))
 (:finally
   (alert "Leaving the try form")))
  "try {
    throw 'i';
} catch (error) {
    alert('an error happened: ' + error);
} finally {
    alert('Leaving the try form');
};")

(test-ps-js the-html-generator-1
  (ps-html ((:a :href "foobar") "blorg"))
911
  "'<a href=\\\"foobar\\\">blorg</a>';")
912
913
914

(test-ps-js the-html-generator-2
  (ps-html ((:a :href (generate-a-link)) "blorg"))
915
  "['<a href=\\\"', generateALink(), '\\\">blorg</a>'].join('');")
916
917
918
919
920

(test-ps-js the-html-generator-3
  (funcall (getprop document 'write)
           (ps-html ((:a :href "#"
                         :onclick (ps-inline (transport))) "link")))
921
  "document.write(['<a href=\\\"#\\\" onclick=\\\"', 'javascript:' + 'transport()', '\\\">link</a>'].join(''));")
922
923
924
925
926
927
928

(test-ps-js the-html-generator-4
  (let ((disabled nil)
        (authorized t))
    (setf (getprop element 'inner-h-t-m-l)
          (ps-html ((:textarea (or disabled (not authorized)) :disabled "disabled")
                    "Edit me"))))
929
930
  "(function () {
var disabled = null;
931
var authorized = true;
932
return element.innerHTML = ['<textarea', disabled || !authorized ? [' disabled=\\\"', 'disabled', '\\\"'].join('') : '', '>Edit me</textarea>'].join('');
933
})();")
934
935

(test-ps-js plus-is-not-commutative
936
937
  (setf x (+ "before" x "after"))
  "x = 'before' + x + 'after';")
938
939

(test-ps-js plus-works-if-first
940
  (setf x (+ x "middle" "after"))
Vladimir Sedach's avatar
Vladimir Sedach committed
941
  "x = x + 'middle' + 'after';")
942

943
(test-ps-js method-call-op-form
Vladimir Sedach's avatar
Vladimir Sedach committed
944
  (funcall (getprop (+ "" x) 'to-string))
945
  "('' + x).toString();")
946
947

(test-ps-js method-call-op-form-args
Vladimir Sedach's avatar
Vladimir Sedach committed
948
949
  (funcall (getprop (+ "" x) 'foo) 1 2 :baz 3)
  "('' + x).foo(1, 2, 'baz', 3);")
950
951

(test-ps-js method-call-string
Vladimir Sedach's avatar
Vladimir Sedach committed
952
  ((getprop "hi" 'to-string))
953
  "'hi'.toString();")
954

955
956
(test-ps-js method-call-conditional
  ((if a x y) 1)
957
  "(a ? x : y)(1);")
958
959

(test-ps-js method-call-variable
960
  ((@ x to-string))
961
  "x.toString();")
962
963

(test-ps-js method-call-array
964
  ((@ (list 10 20) to-string))
965
  "[10, 20].toString();")
966

967
(test-ps-js method-call-lambda-call
968
  (funcall (getprop (funcall (lambda (x) x) 10) 'to-string))
969
  "(function (x) { return x; })(10).toString();")
970

971
972
973
(fiveam:test no-whitespace-before-dot
  (let ((str (ps* '((@ ((lambda (x) x) 10) to-string)))))
    (fiveam:is (char= #\) (elt str (1- (position #\. str)))))))
974

975
(test-ps-js simple-getprop
976
  (let ((foo (create a 1)))
977
    (alert (getprop foo 'a)))
978
979
980
981
  "(function () {
   var foo = { a : 1 };
   return alert(foo.a);
})();")
982

983
(test-ps-js buggy-getprop
Vladimir Sedach's avatar
Vladimir Sedach committed
984
985
  (getprop foo slot-name)
  "foo[slotName];")
986

987
988
(test-ps-js buggy-getprop-two
  (getprop foo (get-slot-name))
989
  "foo[getSlotName()];")
990
991
992
993
994
995
996
997
998

(test-ps-js old-case-is-now-switch
  ;; Switch was "case" before, but that was very non-lispish.
  ;; For example, this code makes three messages and not one
  ;; which may have been expected. This is because a switch
  ;; statment must have a break statement for it to return
  ;; after the alert. Otherwise it continues on the next
  ;; clause.
  (switch (aref blorg i)
999
1000
     (1       (alert "one"))
     (2       (alert "two"))
1001
     (default (alert "default clause")))
1002
  "switch (blorg[i]) {
1003
1004
         case 1:   alert('one');
         case 2:   alert('two');
1005
         default:  alert('default clause');
1006
         };")
1007
1008
1009
1010
1011

(test-ps-js lisp-like-case
   (case (aref blorg i)
     (1 (alert "one"))
     (2 (alert "two"))
1012
     (t (alert "default clause")))
1013
1014
1015
1016
1017
1018
1019
1020
     "switch (blorg[i]) {
         case 1:
                   alert('one');
                   break;
         case 2:
                   alert('two');
                   break;
         default:   alert('default clause');
1021
         };")
1022
1023
1024
1025
1026
1027
1028
1029


(test-ps-js even-lispier-case
  (case (aref blorg i)
      ((1 2) (alert "Below three"))
      (3 (alert "Three"))
      (t (alert "Something else")))
   "switch (blorg[i]) {
1030
         case 1:
1031
1032
1033
1034
1035
1036
1037
         case 2:
                   alert('Below three');
                   break;
         case 3:
                   alert('Three');
                   break;
         default:   alert('Something else');
1038
    };")
1039
1040
1041
1042

(test-ps-js otherwise-case
   (case (aref blorg i)
     (1 (alert "one"))
1043
     (otherwise (alert "default clause")))
1044
1045
1046
1047
1048
     "switch (blorg[i]) {
         case 1:
                   alert('one');
                   break;
         default:   alert('default clause');
1049
         };")
1050

1051
1052
1053
1054
1055
(fiveam:test escape-sequences-in-string
  (let ((escapes
         `((#\\     . #\\)
           (#\b     . #\Backspace)
           (#\f     . ,(code-char 12))
1056
           ("u000B" . ,(code-char #xB))       ; vertical tab
1057
1058
1059
           (#\n     . #\Newline)
           (#\r     . #\Return)
           (#\'     . #\')
1060
           (#\"     . #\")
1061
           (#\t     . #\Tab)
1062
           ("u001F" . ,(code-char #x1F))      ; character below 32
1063
           ("u0080" . ,(code-char 128))       ; character over 127
1064
1065
1066
1067
1068
           ("u00A0" . ,(code-char 160))       ; non-breaking space
           ("u00AD" . ,(code-char 173))       ; soft hyphen
           ("u200B" . ,(code-char #x200B))    ; zero-width space
           ("u200C" . ,(code-char #x200C))    ; zero-width non-joiner
           )))
1069
    (loop for (js-escape . lisp-char) in escapes
1070
1071
          for generated = (ps-doc* (format nil "hello~ahi" lisp-char))
          for wanted = (format nil "'hello\\~ahi';" js-escape)
1072
          do (fiveam:is (string= generated wanted)))))
1073

1074
1075
1076
1077
(fiveam:test escape-doublequotes
  (let ((*js-string-delimiter* #\"))
    (fiveam:is (string= (ps-doc* "hello\"hi") "\"hello\\\"\hi\";"))))

1078
1079
(test-ps-js getprop-setf
  (setf (getprop x 'y) (+ (+ a 3) 4))
1080
  "x.y = (a + 3) + 4;")
1081

1082
1083
(test-ps-js getprop-conditional1
  (getprop (if zoo foo bar) 'x)
1084
  "(zoo ? foo : bar).x;")
1085

1086
1087
(test-ps-js getprop-conditional2
  (getprop (if (not zoo) foo bar) 'x)
1088
  "(!zoo ? foo : bar).x;")
1089

1090
1091
1092
1093
(fiveam:test script-star-eval1
  (fiveam:is (string=
              (normalize-js-output (ps* '(setf x 1) '(setf y 2)))
              "x = 1; y = 2;")))
1094

1095
1096
1097
1098
(fiveam:test script-star-eval2
  (fiveam:is (string=
              (normalize-js-output (ps* '(setf x 1)))
              "x = 1;")))
1099

1100
(test-ps-js list-with-single-nil
1101
  (array nil)
1102
  "[null];")
1103

1104
(test-ps-js quoted-nil-is-array
1105
  'nil
1106
  "[];")
Vladimir Sedach's avatar
Vladimir Sedach committed
1107

1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
(test-ps-js quoted-nil-is-array1
  '()
  "[];")

(test-ps-js literal-nil
  (foo ())
  "foo(null);")

(test-ps-js quoted-quoted-nil
  '(())
  "[null];")

(test-ps-js quoted-quoted-nil1
  '(1 ())
  "[1, null];")

1124
1125
1126
(test-ps-js defsetf1
  (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
         (setf (baz 1 2) 3))
1127
1128
  "(function () {
var _js2 = 1;
1129
1130
var _js3 = 2;
var _js1 = 3;
1131
1132
return setBaz(_js2, _js3, _js1);
})();")
1133

1134
(test-ps-js setf-macroexpands1
1135
1136
1137
  (macrolet ((bar (x y)
               `(aref ,x ,y 1)))
    (setf (bar foo 2) 3))
1138
1139
  "foo[2][1] = 3;")

1140
1141
1142
1143
1144
1145
1146
1147
1148
(test-ps-js defsetf-short
  (progn (defsetf baz set-baz "docstring")
         (setf (baz 1 2 3) "foo"))
  "setBaz(1, 2, 3, 'foo');")

(test-ps-js defun-setf1
  (progn (defun (setf some-thing) (new-val i1 i2)
           (setf (aref *some-thing* i1 i2) new-val))
         (setf (some-thing 1 2) "foo"))
1149
"function __setf_someThing(newVal, i1, i2) {
1150
    return SOMETHING[i1][i2] = newVal;
1151
};
1152
__setf_someThing('foo', 1, 2);")
Vladimir Sedach's avatar
Vladimir Sedach committed
1153

1154
(test-ps-js defun-optional1
1155
  (defun test-opt (&optional x)
1156
    (if x "yes" "no"))
1157
"function testOpt(x) {
1158
    return x ? 'yes' : 'no';
1159
};")
Vladimir Sedach's avatar
Vladimir Sedach committed
1160

Vladimir Sedach's avatar
Vladimir Sedach committed
1161
(test-ps-js defun-optional2
1162
1163
  (defun foo (x &optional y)
    (+ x y))
Vladimir Sedach's avatar
Vladimir Sedach committed
1164
  "function foo(x, y) {
1165
    return x + y;
1166
};")
Vladimir Sedach's avatar
Vladimir Sedach committed
1167

1168
1169
(test-ps-js defun-optional3
  (defun blah (&optional (x 0))
1170
    x)
1171
1172
1173
1174
1175
  "function blah(x) {
    if (x === undefined) {
        x = 0;
    };
    return x;
1176
};")
1177

1178
(test-ps-js arglist-optional4
1179
1180
  (lambda (&optional (x 0 supplied?))
    x)
1181
  "(function (x) {
1182
1183
1184
1185
1186
    var suppliedwhat = x !== undefined;
    if (!suppliedwhat) {
        x = 0;
    };
    return x;
1187
});")
1188

Vladimir Sedach's avatar
Vladimir Sedach committed
1189
(test-ps-js return-nothing
1190
1191
1192
1193
  (defun foo ()  (return-from foo))
  "function foo() {
    return null;
};")
Vladimir Sedach's avatar
Vladimir Sedach committed
1194

1195
1196
1197
(test-ps-js return-values
  (defun foo ()  (return-from foo (values 1 2 3)))
  "function foo() {
1198
1199
    __PS_MV_REG = { 'tag' : arguments.callee, 'values' : [2, 3] };
    return 1;
1200
1201
};")

Vladimir Sedach's avatar
Vladimir Sedach committed
1202
(test-ps-js set-timeout
Vladimir Sedach's avatar
Vladimir Sedach committed
1203
  (set-timeout (lambda () (alert "foo")) 10)
1204
  "setTimeout(function () { return alert('foo'); }, 10);")
1205

1206
1207
(test-ps-js operator-precedence
  (* 3 (+ 4 5) 6)
1208
  "3 * (4 + 5) * 6;")
1209

1210
1211
(test-ps-js operators-1
  (in prop obj)
1212
  "prop in obj;")
1213

1214
1215
(test-ps-js incf1
  (incf foo bar)
1216
  "foo += bar;")
1217
1218
1219

(test-ps-js decf1
  (decf foo bar)
1220
  "foo -= bar;")
1221

1222
1223
(test-ps-js incf2
  (incf x 5)
1224
  "x += 5;")
1225
1226
1227

(test-ps-js decf2
  (decf y 10)
1228
  "y -= 10;")
1229

1230
1231
1232
1233
1234
1235
(test-ps-js setf-conditional
  (setf foo (if x 1 2))
  "foo = x ? 1 : 2;")

(test-ps-js obj-literal-numbers
  (create 1 "foo")
1236
  "({ 1 : 'foo' });")
1237
1238
1239

(test-ps-js obj-literal-strings
  (create "foo" 2)
1240
  "({ 'foo' : 2 });")
1241

1242
1243
(test-ps-js getprop-string
  (getprop foo "bar")
1244
  "foo['bar'];")
1245

1246
1247
(test-ps-js getprop-string1
  (getprop "bar" 'length)
1248
  "'bar'.length;")
Vladimir Sedach's avatar