output-tests.lisp 80.6 KB
Newer Older
1
;; SPDX-License-Identifier: BSD-3-Clause
2

3
(in-package #:parenscript.tests)
4
(named-readtables:in-readtable :parenscript)
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
35

(in-suite output-tests)

(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;")

36
37
38
39
40
41
42
43
(test-ps-js symbol-conversion-5
  encodeURIComponent
  "encodeURIComponent;")

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

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
69
70
71
72
73
74
75
76
77
(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)
  "[  ];")

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

(test-ps-js array-literals-3
  (array (array 2 3)
78
         (array "foobar" "bratzel bub"))
79
80
81
82
83
84
85
86
87
88
89
90
  "[ [ 2, 3 ], [ 'foobar', 'bratzel bub' ] ];")

(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
91
92
   (make-array 2 3)
   (make-array "foobar" "bratzel bub"))
93
94
  "new Array(new Array(2, 3), new Array('foobar', 'bratzel bub'));")

95
96
97
(test-ps-js array-init-1
  (make-array 2 :initial-contents '(10 20))
  "(function () {
98
99
100
101
102
103
104
    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;
})();")
105
106
107
108

(test-ps-js array-init-2
  (make-array 5 :initial-element 10)
  "(function () {
109
110
111
112
113
114
115
    var arr1 = new Array(5);
    var elt3 = 10;
    for (var i4 = 0; i4 < arr1.length; i4 += 1) {
        arr1[i4] = elt3;
    };
    return arr1;
})();")
116

117
118
(test-ps-js object-literals-1
  (create foo "bar" :blorg 1)
119
  "({ foo : 'bar', 'blorg' : 1 });")
120
121
122

(test-ps-js object-literals-2
  (create foo "hihi"
123
124
          blorg (array 1 2 3)
          another-object (create :schtrunz 1))
125
  "({ foo : 'hihi',
126
  blorg : [ 1, 2, 3 ],
127
  anotherObject : { 'schtrunz' : 1 } });")
128
129
130
131
132
133
134
135
136
137
138

(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
139
    (+ a b c))
140
141
  "this.a + this.b + this.c;")

142
143
144
145
146
147
148
(test-ps-js with-slots-single-eval
  (lambda () (with-slots (a b) (foo) (+ a b)))
  "(function () {
    var object1 = foo();
    return object1.a + object1.b;
});")

149
150
151
152
(test-ps-js object-literal-quoted-symbols
  (create 'test "bang" 'symbol-saved-my-life "parenscript")
  "({ 'test' : 'bang', 'symbolSavedMyLife' : 'parenscript' });")

153
154
155
156
157
158
159
160
161
162
163
164
(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;
    } };
165
166
};"
  :js-target-version "1.8.5")
167

168
169
(test-ps-js object-method-apply-1
  (apply (@ an-object foo) nil)
170
  "anObject.foo.apply(anObject, null);")
171
172
173
174
175
176
177
178
179

(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);
    })();")

180
181
182
183
184
185
186
187
(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());
})();")

188
189
190
191
192
193
194
195
196
(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
197
  t
198
199
200
  "true;")

(test-ps-js literal-symbols-2
201
  false
202
203
204
  "false;")

(test-ps-js literal-symbols-3
205
  f
206
207
208
  "false;")

(test-ps-js literal-symbols-4
209
  (lambda () nil)
210
  "(function () {
211
    return null;
212
});")
213
214

(test-ps-js literal-symbols-5
215
  undefined
216
217
218
  "undefined;")

(test-ps-js literal-symbols-6
219
  this
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  "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))
  "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ]);")

(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
251
  ((getprop (aref foobar 1) 'blorg) nil t)
252
253
254
255
256
257
258
259
260
261
262
263
  "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))
264
  "1 * (2 + 3 + 4) * 4 * (6 / 7);")
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

(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;
};")

302
(test-ps-js lambda-definition-2
303
  (lambda (a b) (+ a b))
304
  "(function (a, b) {
305
    return a + b;
306
});")
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

(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))
329
330
331
    (psetf a b b a))
  "(function () {
var a = 1;
332
var b = 2;
333
334
335
336
var _js3 = b;
var _js4 = a;
a = _js3;
return b = _js4;
337
})();")
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

(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")))
358
359
  "(function () {
var _js2 = someDiv;
360
var _js1 = 123 + 'px';
361
362
return _js2.style.left = _js1;
})();")
363
364
365
366
367
368
369
370

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

(test-ps-js nil-block-return-1
371
372
373
374
375
  (block nil (return) 1)
  "(function () {
    return null;
    return 1;
})();")
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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

(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))
  "var A = [ 1, 2, 3 ];")

(test-ps-js variable-declaration-2
437
438
439
440
441
442
  (progn (defvar *a* 4)
         (let ((x 1)
               (*a* 2))
           (let* ((y (+ x 1))
                  (x (+ x y)))
             (+ *a* x y))))
443
  "var A = 4;
444
(function () {
445
446
447
448
449
450
451
var x = 1;
var A_TMPSTACK1;
try {
    A_TMPSTACK1 = A;
    A = 2;
    var y = x + 1;
    var x2 = x + y;
452
    return A + x2 + y;
453
454
} finally {
    A = A_TMPSTACK1;
455
456
};
})();")
457
458
459

(test-ps-js iteration-constructs-1
  (do* ((a) b (c (array "a" "b" "c" "d" "e"))
460
461
462
463
464
        (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/>")))
465
466
  "(function () {
for (var a = null, b = null, c = ['a', 'b', 'c', 'd', 'e'], d = 0, e = c[d];
467
       !(d === c.length || e === 'x'); d += 1, e = c[d]) {
468
469
470
    a = d;
    b = e;
    document.write('a: ' + a + ' b: ' + b + '<br/>');
471
472
};
})();")
473
474
475

(test-ps-js iteration-constructs-2
  (do ((i 0 (1+ i))
476
477
478
       (s 0 (+ s i (1+ i))))
      ((> i 10))
    (funcall (@ document write) (+ "i: " i " s: " s "<br/>")))
479
480
  "(function () {
var i = 0;
481
482
483
484
var s = 0;
for (; i <= 10; ) {
    document.write('i: ' + i + ' s: ' + s + '<br/>');
    var _js1 = i + 1;
485
    var _js2 = s + i + (i + 1);
486
487
    i = _js1;
    s = _js2;
488
489
};
})();")
490
491
492

(test-ps-js iteration-constructs-3
  (do* ((i 0 (1+ i))
493
494
495
        (s 0 (+ s i (1- i))))
       ((> i 10))
    ((@ document write) (+ "i: " i " s: " s "<br/>")))
496
497
  "(function () {
for (var i = 0, s = 0; i <= 10; i += 1, s = s + i + (i - 1)) {
498
    document.write('i: ' + i + ' s: ' + s + '<br/>');
499
500
};
})();")
501
502
503

(test-ps-js iteration-constructs-4
  (let ((arr (array "a" "b" "c" "d" "e")))
504
505
506
507
    (dotimes (i (@ arr length))
      ((@ document write) (+ "i: " i " arr[i]: " (aref arr i) "<br/>"))))
  "(function () {
var arr = ['a', 'b', 'c', 'd', 'e'];
508
509
for (var i = 0; i < arr.length; i += 1) {
    document.write('i: ' + i + ' arr[i]: ' + arr[i] + '<br/>');
510
511
};
})();")
512
513
514

(test-ps-js iteration-constructs-5
  (let ((res 0))
515
516
517
518
519
520
    (alert (+ "Summation to 10 is "
              (dotimes (i 10 res)
                (incf res (1+ i))))))
  "(function () {
var res = 0;
return alert('Summation to 10 is ' + (function () {
521
522
523
524
    for (var i = 0; i < 10; i += 1) {
        res += i + 1;
    };
    return res;
525
526
})());
})();")
527
528
529

(test-ps-js iteration-constructs-6
  (let ((l (list 1 2 4 8 16 32)))
530
531
532
533
    (dolist (c l)
      ((@ document write) (+ "c: " c "<br/>"))))
  "(function () {
var l = [1, 2, 4, 8, 16, 32];
534
535
for (var c = null, _js_idx2 = 0; _js_idx2 < l.length; _js_idx2 += 1) {
    c = l[_js_idx2];
536
    document.write('c: ' + c + '<br/>');
537
538
};
})();")
539
540
541
542
543
544
545

(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)))))
546
547
  "(function () {
var l = [1, 2, 4, 8, 16, 32];
548
var s = 0;
549
return alert('Sum of ' + l + ' is: ' + (function () {
550
551
552
553
554
    for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) {
        c = l[_js_idx1];
        s += c;
    };
    return s;
555
556
})());
})();")
557
558
559

(test-ps-js iteration-constructs-8
  (let ((obj (create a 1 b 2 c 3)))
560
561
562
563
    (for-in (i obj)
            ((@ document write) (+ i ": " (aref obj i) "<br/>"))))
  "(function () {
var obj = { a : 1, b : 2, c : 3 };
564
565
for (var i in obj) {
    document.write(i + ': ' + obj[i] + '<br/>');
566
567
};
})();")
568
569
570
571
572
573
574
575

(test-ps-js iteration-constructs-9
  (while ((@ film is-not-finished))
  ((@ this eat) (new *popcorn)))
  "while (film.isNotFinished()) {
    this.eat(new Popcorn);
};")

Daniel Gackle's avatar
Daniel Gackle committed
576
577
(test-ps-js loop-for-bindings
  (loop :for ((a b) (:c :d)) :in arr :do (foo a b c d))
578
579
"(function () {
var _js2 = arr.length;
Daniel Gackle's avatar
Daniel Gackle committed
580
for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
581
582
583
584
585
    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
586
587
588
    var c = _js3['c'];
    var d = _js3['d'];
    foo(a, b, c, d);
589
590
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
591
592
593

(test-ps-js loop-for-on
  (loop :for (k v) :on plist :by 2 :do (foo k v))
594
595
  "(function () {
for (var _js1 = plist; _js1.length > 0; _js1 = _js1['slice'](2)) {
Daniel Gackle's avatar
Daniel Gackle committed
596
597
598
    var k = _js1[0];
    var v = _js1[1];
    foo(k, v);
599
600
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
601
602
603

(test-ps-js loop-for-keys-of
  (loop :for k :of obj :do (foo k))
604
605
  "(function () {
for (var k in obj) {
Daniel Gackle's avatar
Daniel Gackle committed
606
    foo(k);
607
608
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
609
610
611

(test-ps-js loop-for-key-val-pairs-of
   (loop :for (k v) :of obj :do (foo k v))
612
613
   "(function () {
for (var k in obj) {
Daniel Gackle's avatar
Daniel Gackle committed
614
615
    var v = obj[k];
    foo(k, v);
616
617
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
618
619
620

(test-ps-js loop-for-key-val-pairs-of-with-bindings
   (loop :for (k (a b)) :of obj :do (foo k a b))
621
622
"(function () {
for (var k in obj) {
623
624
625
    var _db1 = obj[k];
    var a = _db1[0];
    var b = _db1[1];
Daniel Gackle's avatar
Daniel Gackle committed
626
    foo(k, a, b);
627
628
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
629
630
631

(test-ps-js loop-for-just-vals-of
   (loop :for (nil v) :of obj :do (foo k v))
632
633
   "(function () {
for (var _js1 in obj) {
Daniel Gackle's avatar
Daniel Gackle committed
634
635
    var v = obj[_js1];
    foo(k, v);
636
637
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674

(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))
675
676
"(function () {
var _js2 = b.length;
Daniel Gackle's avatar
Daniel Gackle committed
677
678
679
680
681
682
683
684
for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
    var a = b[_js1];
    if (a > 100) {
        break;
    };
    if (a < 50) {
        foo(a);
    };
685
686
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
687
688
689
690
691

(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))
692
693
"(function () {
var c = c1;
Daniel Gackle's avatar
Daniel Gackle committed
694
695
696
697
698
for (var d = c1; d < c2; d += 1) {
    if (foo(c, d)) {
        c = d;
    };
    bar(d);
699
700
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723

(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))
724
725
"(function () {
for (var a = pop(stack); a; a = pop(stack)) {
726
727
728
    var _db1 = foo(a);
    var b = _db1[0];
    var c = _db1[1];
Daniel Gackle's avatar
Daniel Gackle committed
729
730
731
    if (b) {
        bar(c);
    };
732
733
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753

(test-ps-js loop-for-of-for-in
   (defun blah (obj b)
     (loop :for k :of obj :for a :in b :do (foo k a)))
"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))
754
755
"(function () {
var _js2 = expr.length;
Daniel Gackle's avatar
Daniel Gackle committed
756
for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
757
758
759
    var _db3 = expr[_js1];
    var op = _db3[0];
    var args = _db3.length > 1 ? _db3.slice(1) : [];
Daniel Gackle's avatar
Daniel Gackle committed
760
    foo(op, args);
761
762
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
763
764
765

(test-ps-js loop-for-rest
   (loop :for (op &rest args) :in expr :do (foo op args))
766
767
"(function () {
var _js2 = expr.length;
Daniel Gackle's avatar
Daniel Gackle committed
768
for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
769
770
771
    var _db3 = expr[_js1];
    var op = _db3[0];
    var args = _db3.length > 1 ? _db3.slice(1) : [];
Daniel Gackle's avatar
Daniel Gackle committed
772
    foo(op, args);
773
774
};
})();")
Daniel Gackle's avatar
Daniel Gackle committed
775
776
777
778
779
780
781
782

(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
783
        collect3.push(foo(a));
Daniel Gackle's avatar
Daniel Gackle committed
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
    };
    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;
})();")

800
801
(test-ps-js the-case-statement-1
  (case (aref blorg i)
802
803
804
    ((1 "one") (alert "one"))
    (2 (alert "two"))
    (t (alert "default clause")))
805
806
807
808
809
810
811
812
813
814
815
816
817
818
  "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)
819
820
821
    (1 (alert "If I get here"))
    (2 (alert "I also get here"))
    (default (alert "I always get here")))
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
  "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"))
  "'<A HREF=\"foobar\">blorg</A>';")

(test-ps-js the-html-generator-2
  (ps-html ((:a :href (generate-a-link)) "blorg"))
Daniel Gackle's avatar
Daniel Gackle committed
848
  "['<A HREF=\"', generateALink(), '\">blorg</A>'].join('');")
849
850
851
852
853

(test-ps-js the-html-generator-3
  (funcall (getprop document 'write)
           (ps-html ((:a :href "#"
                         :onclick (ps-inline (transport))) "link")))
Daniel Gackle's avatar
Daniel Gackle committed
854
  "document.write(['<A HREF=\"#\" ONCLICK=\"', 'javascript:' + 'transport()', '\">link</A>'].join(''));")
855
856
857
858
859
860
861

(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"))))
862
863
  "(function () {
var disabled = null;
864
var authorized = true;
Daniel Gackle's avatar
Daniel Gackle committed
865
return element.innerHTML = ['<TEXTAREA', disabled || !authorized ? [' DISABLED=\"', 'disabled', '\"'].join('') : '', '>Edit me</TEXTAREA>'].join('');
866
})();")
867
868

(test-ps-js plus-is-not-commutative
869
870
  (setf x (+ "before" x "after"))
  "x = 'before' + x + 'after';")
871
872

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

876
(test-ps-js method-call-op-form
Vladimir Sedach's avatar
Vladimir Sedach committed
877
  (funcall (getprop (+ "" x) 'to-string))
878
  "('' + x).toString();")
879
880

(test-ps-js method-call-op-form-args
Vladimir Sedach's avatar
Vladimir Sedach committed
881
882
  (funcall (getprop (+ "" x) 'foo) 1 2 :baz 3)
  "('' + x).foo(1, 2, 'baz', 3);")
883
884

(test-ps-js method-call-string
Vladimir Sedach's avatar
Vladimir Sedach committed
885
  ((getprop "hi" 'to-string))
886
  "'hi'.toString();")
887

888
889
(test-ps-js method-call-conditional
  ((if a x y) 1)
890
  "(a ? x : y)(1);")
891
892

(test-ps-js method-call-variable
893
  ((@ x to-string))
894
  "x.toString();")
895
896

(test-ps-js method-call-array
897
  ((@ (list 10 20) to-string))
898
  "[ 10, 20 ].toString();")
899

900
(test-ps-js method-call-lambda-call
901
  (funcall (getprop (funcall (lambda (x) x) 10) 'to-string))
902
  "(function (x) { return x; })(10).toString();")
903
904

(test no-whitespace-before-dot
905
  (let* ((str (ps* '((@ ((lambda (x) x) 10) to-string))))
906
907
908
909
910
         (dot-pos (position #\. str :test #'char=))
         (char-before (elt str (1- dot-pos)))
         (a-parenthesis #\)))
    (is (char= char-before a-parenthesis))))

911
(test-ps-js simple-getprop
912
  (let ((foo (create a 1)))
913
    (alert (getprop foo 'a)))
914
915
916
917
  "(function () {
   var foo = { a : 1 };
   return alert(foo.a);
})();")
918

919
(test-ps-js buggy-getprop
Vladimir Sedach's avatar
Vladimir Sedach committed
920
921
  (getprop foo slot-name)
  "foo[slotName];")
922

923
924
(test-ps-js buggy-getprop-two
  (getprop foo (get-slot-name))
925
  "foo[getSlotName()];")
926
927
928
929
930
931
932
933
934

(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)
935
936
     (1       (alert "one"))
     (2       (alert "two"))
937
     (default (alert "default clause")))
938
  "switch (blorg[i]) {
939
940
         case 1:   alert('one');
         case 2:   alert('two');
941
         default:  alert('default clause');
942
         };")
943
944
945
946
947

(test-ps-js lisp-like-case
   (case (aref blorg i)
     (1 (alert "one"))
     (2 (alert "two"))
948
     (t (alert "default clause")))
949
950
951
952
953
954
955
956
     "switch (blorg[i]) {
         case 1:
                   alert('one');
                   break;
         case 2:
                   alert('two');
                   break;
         default:   alert('default clause');
957
         };")
958
959
960
961
962
963
964
965


(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]) {
966
         case 1:
967
968
969
970
971
972
973
         case 2:
                   alert('Below three');
                   break;
         case 3:
                   alert('Three');
                   break;
         default:   alert('Something else');
974
    };")
975
976
977
978

(test-ps-js otherwise-case
   (case (aref blorg i)
     (1 (alert "one"))
979
     (otherwise (alert "default clause")))
980
981
982
983
984
     "switch (blorg[i]) {
         case 1:
                   alert('one');
                   break;
         default:   alert('default clause');
985
         };")
986
987

(test escape-sequences-in-string
988
  (let ((escapes `((#\\ . #\\)
989
990
991
992
993
994
995
996
997
998
999
                   (#\b . #\Backspace)
                   (#\f . ,(code-char 12))
                   ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
                   (#\n . #\Newline)
                   (#\r . #\Return)
                   (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
                   (#\t . #\Tab)
                   ("u001F" . ,(code-char #x001f));; character below 32
                   ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
                   ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
    (loop for (js-escape . lisp-char) in escapes
1000
          for generated = (ps-doc* (format nil "hello~ahi" lisp-char))