forked from anko/eslisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ls
executable file
·1395 lines (1163 loc) · 37.9 KB
/
test.ls
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
35
36
37
38
39
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
69
70
71
72
73
74
75
76
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
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
467
468
469
470
471
472
473
474
475
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env lsc
consume-map = require \source-map .SourceMapConsumer .bind!
{ spawn } = require \child_process
{ unique } = require \prelude-ls
require! <[ tape tmp fs uuid rimraf path ]>
concat = require \concat-stream
esl = require \./src/index.ls
test = (name, test-func) ->
tape name, (t) ->
test-func.call t # Make `this` refer to tape's asserts
t.end! # Automatically end tests
test-async = (name, test-func) ->
tape name, (t) ->
test-func.call t
# Don't end automatically
test "nothing" ->
esl ""
.. `@equals` ""
test "plain comment" ->
esl "\n; nothing\n"
..`@equals` ""
test "first-line shebang" ->
esl "#!something goes here\n(hello)\n"
..`@equals` "hello();"
test "plain numeric literal" ->
esl "3"
..`@equals` "3;"
test "plain negative numeric literal" ->
esl "-3"
..`@equals` "-3;"
test "plain literal with trailing digits" ->
esl "asd39"
..`@equals` "asd39;"
test "plain string literal" ->
esl '"ok then"'
..`@equals` "'ok then';"
test "string literal escaping" ->
esl '"\\"ok\\" then"'
..`@equals` "'\"ok\" then';"
test "string literal newline" ->
esl '"ok\nthen"'
..`@equals` "'ok\\nthen';"
test "string literal newline escape" ->
esl '"ok\\nthen"'
..`@equals` "'ok\\nthen';"
test "n-ary plus" ->
esl "(+ 3 4 5)"
..`@equals` "3 + (4 + 5);"
test "plus nests" ->
esl "(+ 1 (+ 2 3))"
..`@equals` "1 + (2 + 3);"
test "unary plus" ->
esl "(+ 1)"
..`@equals` "+1;"
test "unary minus" ->
esl "(- 1)"
..`@equals` "-1;"
test "n-ary minus" ->
esl "(- 10 2 1)"
..`@equals` "10 - (2 - 1);"
test "n-ary multiplication" ->
esl "(* 1 2 3)"
..`@equals` "1 * (2 * 3);"
test "unary multiplication is invalid" ->
(-> esl "(* 2)")
..`@throws` Error
test "n-ary division" ->
esl "(/ 1 2 3)"
..`@equals` "1 / (2 / 3);"
test "unary division is invalid" ->
(-> esl "(/ 2)")
..`@throws` Error
test "n-ary modulus" ->
esl "(% 1 2 3)"
..`@equals` "1 % (2 % 3);"
test "prefix increment expression" ->
esl "(_++ x)"
..`@equals` "x++;"
test "postfix incremente expression" ->
esl "(++_ x) (++ x)"
..`@equals` "++x;\n++x;"
test "prefix decrement expression" ->
esl "(--_ x) (-- x)"
..`@equals` "--x;\n--x;"
test "postfix decrement expression" ->
esl "(_-- x)"
..`@equals` "x--;"
test "chainable logical expressions" ->
esl "(&& 1 2 3) (|| 1 2 3)"
..`@equals` "1 && (2 && 3);\n1 || (2 || 3);"
test "unary logical not" ->
esl "(! 1)"
..`@equals` "!1;"
test "unary delete" ->
esl "(delete x)"
..`@equals` "delete x;"
test "unary delete" ->
esl "(typeof x)"
..`@equals` "typeof x;"
test "unary void" ->
esl "(void x)"
..`@equals` "void x;"
test "chainable instanceof" -> # yes, making that chain is maybe odd
esl "(instanceof x y z)"
..`@equals` "x instanceof (y instanceof z);"
test "chainable in" ->
esl "(in x y z)"
..`@equals` "x in (y in z);"
test "bitwise &, |, ^ are chainable" ->
esl "(& 1 2 3) (| 1 2 3) (^ 1 2 3)"
..`@equals` "1 & (2 & 3);\n1 | (2 | 3);\n1 ^ (2 ^ 3);"
test "bitwise shifts are chainable" ->
esl "(<< 1 2 3) (>> 1 2 3) (>>> 1 2 3)"
..`@equals` "1 << (2 << 3);\n1 >> (2 >> 3);\n1 >>> (2 >>> 3);"
test "unary bitwise not" ->
esl "(~ x)"
..`@equals` "~x;"
test "equals expression, chainable" ->
esl "(== x y z)"
..`@equals` "x == (y == z);"
test "disequals expression, chainable" ->
esl "(!= x y z)"
..`@equals` "x != (y != z);"
test "strong-equals expression, chainable" ->
esl "(=== x y z)"
..`@equals` "x === (y === z);"
test "strong-disequals expression, chainable" ->
esl "(!== x y z)"
..`@equals` "x !== (y !== z);"
test "comparison expressions, chainable" -> # >, <= and >= are same code path
esl "(< x y z)"
..`@equals` "x < (y < z);"
test "sequence expression (comma-separated expressions)" ->
esl "(seq x y z)"
..`@equals` "x, y, z;"
test "function expression" ->
esl "(lambda (x) (return (+ x 1)))"
..`@equals` "(function (x) {\n return x + 1;\n});"
test "function expression with name" ->
esl "(lambda f (x) (return (+ x 1)))"
..`@equals` "(function f(x) {\n return x + 1;\n});"
test "function declaration" ->
esl "(function f (x) (return (+ x 1)))"
..`@equals` "function f(x) {\n return x + 1;\n}"
test "function declaration without name throws error" ->
-> esl "(function (x) (return (+ x 1)))"
..`@throws` Error
test "function with no arguments" ->
esl "(lambda () (return 1))"
..`@equals` "(function () {\n return 1;\n});"
test "assignment expressions" ->
<[ += -= *= /= %=
&= |= ^= >>= <<= >>>= ]> .for-each ~>
esl "(#it x 1)"
..`@equals` "x #it 1;"
test "variable declaration statement" ->
esl "(var f)"
..`@equals` "var f;"
test "variable declaration and assignment" ->
esl "(var f (lambda (x) (return (+ x 1))))"
..`@equals` "var f = function (x) {\n return x + 1;\n};"
test "empty statement" ->
esl "()"
..`@equals` "null;"
test "break statement" ->
esl "(while true (break))"
..`@equals` "while (true) {\n break;\n}"
test "continue statement" ->
esl "(while true (continue))"
..`@equals` "while (true) {\n continue;\n}"
test "break to label" ->
esl "(label x (while true (break x)))"
..`@equals` "x:\n while (true) {\n break x;\n }"
test "continue to label" ->
esl "(label x (while true (continue x)))"
..`@equals` "x:\n while (true) {\n continue x;\n }"
test "stand-alone label" ->
esl "(label x)"
..`@equals` "x:;"
test "labeled statement" ->
esl "(label foo (while (-- n)))"
..`@equals` "foo:\n while (--n) {\n }"
test "labeled expression" ->
esl "(label label (* x 4))"
..`@equals` "label:\n x * 4;"
test "return statement" ->
esl "(lambda () (return \"hello there\"))"
..`@equals` "(function () {\n return 'hello there';\n});"
test "member expression" ->
esl "(. console log)"
..`@equals` "console.log;"
test "explicit block statement" ->
esl "(block a b)"
..`@equals` "{\n a;\n b;\n}"
test "call expression" ->
esl "(f)"
..`@equals` "f();"
test "member, then call with arguments" ->
esl '((. console log) "hi")'
..`@equals` "console.log('hi');"
test "func with member and call in it" ->
esl "(lambda (x) ((. console log) x))"
..`@equals` "(function (x) {\n console.log(x);\n});"
test "switch statement" ->
esl '''
(switch (y)
((== x 5) ((. console log) "hi") (break))
(default (yes)))
'''
..`@equals` """
switch (y()) {
case x == 5:
console.log('hi');
break;
default:
yes();
}
"""
test "if-statement with blocks" ->
esl '(if (+ 1 0) (block ((. console log) "yes") (x)) (block 0))'
..`@equals` """
if (1 + 0) {
console.log(\'yes\');
x();
} else {
0;
}
"""
test "if-statement with expressions" ->
esl '(if (+ 1 0) (x) 0)'
..`@equals` """
if (1 + 0)
x();
else
0;
"""
test "if-statement without alternate" ->
esl '(if (+ 1 0) (block ((. console log) "yes") (x)))'
..`@equals` """
if (1 + 0) {
console.log(\'yes\');
x();
}
"""
test "ternary expression" ->
esl '(?: "something" 0 1)'
..`@equals` "'something' ? 0 : 1;"
test "while loop with explicit body" ->
esl '(while (-- n) (block
((. console log) "ok")
((. console log) "still ok")))'
..`@equals` "while (--n) {\n console.log('ok');\n console.log('still ok');\n}"
test "while loop with explicit body that contains a block" ->
esl '(while (-- n) (block
(block a)))'
..`@equals` "while (--n) {\n {\n a;\n }\n}"
test "while loop with implicit body" ->
esl '(while (-- n) ((. console log) "ok")
((. console log) "still ok"))'
..`@equals` "while (--n) {\n console.log('ok');\n console.log('still ok');\n}"
test "do/while loop with implicit body" ->
esl '(dowhile (-- n) ((. console log) "ok")
((. console log) "still ok"))'
..`@equals` "do {\n console.log('ok');\n console.log('still ok');\n} while (--n);"
test "do/while loop with explicit body" ->
esl '(dowhile (-- n) (block
((. console log) "ok")
((. console log) "still ok")))'
..`@equals` "do {\n console.log('ok');\n console.log('still ok');\n} while (--n);"
test "for loop with implicit body" ->
esl '(for (var x 1) (< x 10) (++ x) ((. console log) "ok")
((. console log) "still ok"))'
..`@equals` "for (var x = 1; x < 10; ++x) {\n console.log('ok');\n console.log('still ok');\n}"
test "for loop with explicit body" ->
esl '(for (var x 1) (< x 10) (++ x) (block ((. console log) "ok")
((. console log) "still ok")))'
..`@equals` "for (var x = 1; x < 10; ++x) {\n console.log('ok');\n console.log('still ok');\n}"
test "for loop with no body" ->
esl '(for (var x 1) (< x 10) (++ x))'
..`@equals` "for (var x = 1; x < 10; ++x) {\n}"
test "for loop with null update" ->
esl '(for (var x 1) (< x 10) () ((. console log) "ok")
((. console log) "still ok"))'
..`@equals` "for (var x = 1; x < 10;) {\n console.log('ok');\n console.log('still ok');\n}"
test "for loop with null init, update and test" ->
esl '(for () () () ((. console log) "ok")
((. console log) "still ok"))'
..`@equals` "for (;;) {\n console.log('ok');\n console.log('still ok');\n}"
test "for-in loop with implicit body" ->
esl '(forin (var x) xs ((. console log) x))'
..`@equals` "for (var x in xs) {\n console.log(x);\n}"
test "for-in loop with explicit body" ->
esl '(forin (var x) xs (block ((. console log) x)))'
..`@equals` "for (var x in xs) {\n console.log(x);\n}"
test "multiple statements in program" ->
esl '((. console log) "hello") ((. console log) "world")'
..`@equals` "console.log('hello');\nconsole.log('world');"
test "function with implicit block body" ->
esl '(lambda (x) ((. console log) "hello") \
((. console log) "world"))'
..`@equals` "(function (x) {\n console.log(\'hello\');\n console.log(\'world\');\n});"
test "function with explicit block body" ->
esl '(lambda (x) (block
((. console log) "hello") \
((. console log) "world")))'
..`@equals` "(function (x) {\n console.log(\'hello\');\n console.log(\'world\');\n});"
test "new statement" ->
esl '(new Error "hi") (new x)'
..`@equals` "new Error('hi');\nnew x();"
test "debugger statement" ->
esl '(debugger)'
..`@equals` "debugger;"
test "throw statement" ->
esl '(throw e)'
..`@equals` "throw e;"
test "try-catch (with `catch` and `finally`)" ->
esl '''
(try (yep)
(nope)
(catch err
(a err)
(b err))
(finally (x)
(y)))
'''
..`@equals` """
try {
yep();
nope();
} catch (err) {
a(err);
b(err);
} finally {
x();
y();
}
"""
test "try-catch (with plain atom in body)" ->
esl '''
(try foo
(catch e
bar)
(finally baz))
'''
..`@equals` """
try {
foo;
} catch (e) {
bar;
} finally {
baz;
}
"""
test "try-catch (with empty body, `catch` and `finally`)" ->
esl '''
(try (catch err)
(finally))
'''
..`@equals` """
try {
} catch (err) {
} finally {
}
"""
test "try-catch (with `catch` and `finally` as explicit blocks)" ->
esl '''
(try (yep)
(nope)
(catch err (block (a err) (b err)))
(finally (block (x) (y))))
'''
..`@equals` """
try {
yep();
nope();
} catch (err) {
a(err);
b(err);
} finally {
x();
y();
}
"""
test "try-catch (with `catch` and `finally` in opposite order)" ->
esl '''
(try (yep)
(nope)
(finally (x) (y))
(catch err (a err) (b err)))
'''
..`@equals` """
try {
yep();
nope();
} catch (err) {
a(err);
b(err);
} finally {
x();
y();
}
"""
test "try-catch (`catch`; no `finally`)" ->
esl '''
(try (yep)
(nope)
(catch err (a err) (b err)))
'''
..`@equals` """
try {
yep();
nope();
} catch (err) {
a(err);
b(err);
}
"""
test "try-catch (`finally`; no `catch`)" ->
esl '''
(try (yep)
(nope)
(finally (x) (y)))
'''
..`@equals` """
try {
yep();
nope();
} finally {
x();
y();
}
"""
test "quoting a list produces array" ->
esl "'(1 2 3)"
eval ..
..type `@equals` "list"
..values
..length `@equals` 3
..0
..type `@equals` "atom"
..value `@equals` "1"
..1
..type `@equals` "atom"
..value `@equals` "2"
..2
..type `@equals` "atom"
..value `@equals` "3"
test "quoting strings produces string AST object" ->
esl "'\"hi\""
eval ..
..type `@equals` "string"
..value `@equals` "hi"
test "quoting atoms produces an object representing it" ->
esl "'fun"
eval ..
..type `@equals` "atom"
..value `@equals` "fun"
test "simple quoting macro" ->
esl "(macro random (lambda () (return '((. Math random)))))
(+ (random) (random))"
..`@equals` "Math.random() + Math.random();"
test "macro constructor given object imports properties as macros" ->
esl '''
(macro (object a (lambda () (return '"hi a"))
b (lambda () (return '"hi b"))))
(a) (b)
'''
..`@equals` "'hi a';\n'hi b';"
test "simple unquoting macro" ->
esl "(macro call (lambda (x) (return `(,x))))
(call three)"
..`@equals` "three();"
test "empty-list-returning macro" ->
esl "(macro shouldbenull (lambda () (return '())))
(shouldbenull)"
..`@equals` "null;"
test "empty-list-returning macro (using quasiquote)" ->
esl "(macro shouldbenull (lambda () (return `())))
(shouldbenull)"
..`@equals` "null;"
test "nothing-returning macro" ->
esl "(macro nothing (lambda () (return undefined)))
(nothing)"
..`@equals` ""
test "macros mask others defined before with the same name" ->
esl "(macro m (lambda () (return ())))
(macro m (lambda () (return '((. console log) \"hi\"))))
(m)"
..`@equals` "console.log('hi');"
test "macros can be masked in the current scope by assigning null" ->
esl "(macro array)
(array 1 2)"
..`@equals` "array(1, 2);"
test "macros can be defined inside function bodies" ->
esl "(var f (lambda (x)
(macro x (lambda () (return '5)))
(return (x))))"
..`@equals` "var f = function (x) {\n return 5;\n};"
test "macros go out of scope at the end of the nesting level" ->
esl "(var f (lambda (x)
(macro x (lambda () (return '5)))
(return (x))))
(x)"
..`@equals` "var f = function (x) {\n return 5;\n};\nx();"
test "macro constructor given 2 atoms aliases the second to the first" ->
esl "(macro list array)
(list a 1 b 2)"
..`@equals` "[\n a,\n 1,\n b,\n 2\n];"
test "dead simple quasiquote" ->
esl "(macro q (lambda () (return `(+ 2 3))))
(q)"
..`@equals` "2 + 3;"
test "quasiquote is like quote if no unquotes contained" ->
esl "(macro rand (lambda ()
(return `(* 5
((. Math random))))))
(rand)"
..`@equals` "5 * Math.random();"
test "macros can quasiquote to unquote arguments into output" ->
esl "(macro rand (lambda (upper)
(return `(* ,upper
((. Math random))))))
(rand 5)"
..`@equals` "5 * Math.random();"
test "macro env can create atoms out of strings or numbers" ->
esl """
(macro m (lambda () (return ((. this atom) 42))))
(m)"""
..`@equals` "42;"
test "macro env can create sexpr AST nodes equivalently to quoting" ->
with-quote =
esl """
(macro m (lambda () (return '(a \"b\"))))
(m)"""
with-construct =
esl """
(macro m (lambda ()
(return ((. this list)
((. this atom) "a")
((. this string) "b")))))
(m)"""
with-quote `@equals` with-construct
test "macros can evaluate number arguments to JS and convert them back again" ->
esl """
(macro incrementedTimesTwo (lambda (x)
(var y (+ 1 ((. this evaluate) x)))
(var xAsSexpr ((. this atom) ((. y toString))))
(return `(* ,xAsSexpr 2))))
(incrementedTimesTwo 5)
"""
..`@equals` "6 * 2;"
test "macros can evaluate object arguments" ->
# This macro uses this.evaluate to compile and evaluate a list that expands
# to an object, then stringifies it.
esl """
(macro objectAsString (lambda (input)
(= obj ((. this evaluate) input))
(return ((. this string) ((. JSON stringify) obj)))))
(objectAsString (object a 1))
"""
..`@equals` "'{\"a\":1}';"
test "macros can evaluate statements" ->
# This macro uses this.evaluate to compile and run an if-statement. A
# statement does not evaluate to a value, so we check for undefined.
esl """
(macro evalThis (lambda (input)
(= obj ((. this evaluate) input))
(if (=== obj undefined)
(return ((. this atom) "yep"))
(return ((. this atom) "nope")))))
(evalThis (if 1 (block) (block)))
"""
..`@equals` "yep;"
test "macros can unquote arrays into quasiquoted lists (non-splicing)" ->
esl "(macro what (lambda (x)
(return `(,x))))
(what (+ 2 3))"
..`@equals` "(2 + 3)();"
test "macros can splice arrays into quasiquoted lists" ->
esl "(macro sumOf (lambda (xs) (return `(+ ,@xs))))
(sumOf (1 2 3))"
..`@equals` "1 + (2 + 3);"
test "macros can splice in empty arrays" ->
esl "(macro sumOf (lambda (xs) (return `(+ 1 2 ,@xs))))
(sumOf ())"
..`@equals` "1 + 2;"
test "quasiquote can contain nested lists" ->
esl '''
(macro mean
(lambda ()
; Convert arguments into array
(var args
((. this list apply) null ((. Array prototype slice call) arguments 0)))
(var total ((. this atom) ((. (. args values length) toString))))
(return `(/ (+ ,@args) ,total))))
(mean 1 2 3)
'''
..`@equals` "(1 + (2 + 3)) / 3;"
test "array macro produces array expression" ->
esl "(array 1 2 3)"
..`@equals` "[\n 1,\n 2,\n 3\n];"
test "array macro can be empty" ->
esl "(array)"
..`@equals` "[];"
test "object macro produces object expression" ->
esl "(object a 1 b 2)"
..`@equals` "({\n a: 1,\n b: 2\n});"
test "object macro can be passed strings as keys too" ->
esl '(object "a" 1 "b" 2)'
..`@equals` "({\n 'a': 1,\n 'b': 2\n});"
test "object macro's value parts can be expressions" ->
esl '(object "a" (+ 1 2) "b" (f x))'
..`@equals` "({\n 'a': 1 + 2,\n 'b': f(x)\n});"
# dynamic *keys* would be ES6
test "macro producing an object literal" ->
esl "(macro obj (lambda () (return '(object a 1))))
(obj)"
..`@equals` "({ a: 1 });"
test "macro producing a function" ->
esl "(macro increase (lambda (n)
(return `(lambda (x) (return (+ x ,n))))))
(increase 3)"
..`@equals` "(function (x) {\n return x + 3;\n});"
test "property access (dotting) chains identifiers" ->
esl "(. a b c)"
..`@equals` "a.b.c;"
test "property access (dotting) chains literals" ->
esl "(. a 1 2)"
..`@equals` "a[1][2];"
test "property access (dotting) can be nested" ->
esl "(. a (. a (. b name)))"
..`@equals` "a[a[b.name]];"
test "property access (dotting) chains mixed literals and identifiers" ->
esl "(. a b 2 a)"
..`@equals` "a.b[2].a;"
test "property access (dotting) treats strings as literals, not identifiers" ->
esl "(. a \"hi\")"
..`@equals` "a['hi'];"
test "computed member expression (\"square brackets\")" ->
esl "(get a b 5)"
..`@equals` "a[b][5];"
test "regex literal" ->
esl '(regex ".*")'
..`@equals` "/.*/;"
test "regex literal with flags" ->
esl '(regex ".*" "gm")'
..`@equals` "/.*/gm;"
test "regex literals are escaped" ->
esl '(regex "/.\\"*")'
..`@equals` "/\\/.\"*/;"
test "regex literals can be derived from atoms too" ->
esl '(regex abc.* g)'
..`@equals` "/abc.*/g;"
test "regex can be given atoms with escaped spaces and slashes" ->
esl '(regex abc\\ */ g)'
..`@equals` "/abc *\\//g;"
test "macro deliberately breaking hygiene for function argument anaphora" ->
esl "(macro : (lambda (body)
(return `(lambda (it) ,body))))
(: (return (. it x)))"
..`@equals` "(function (it) {\n return it.x;\n});"
test "macro given nothing produces no output" ->
esl "(macro null)"
..`@equals` ""
esl "(macro undefined)"
..`@equals` ""
test "when returned from an IIFE, macros can share state" ->
esl """
(macro
((lambda () (var x 0)
(return (object
plusPrev (lambda (n)
(+= x ((. this evaluate) n))
(return ((. this atom) ((. x toString)))))
timesPrev (lambda (n)
(*= x ((. this evaluate) n))
(return ((. this atom) ((. x toString))))))))))
(plusPrev 2) (timesPrev 2)
"""
..`@equals` "2;\n4;"
test "error thrown by macro is caught with a descriptive message" ->
tests =
* code : """
(macro x (lambda () (throw (Error "aaah"))))
(x)
"""
desired-error : "Error evaluating macro `x` (called at line 2, column 0): aaah"
* code : """
(macro x (lambda () (m)))
(x)
"""
desired-error : "Error evaluating macro `x` (called at line 2, column 0): m is not defined"
tests.for-each (it, i) ~>
caught = false
try
esl it.code
catch e
caught := true
if e.message isnt it.desired-error
@fail "Code #i produced wrong error message"
finally
if not caught
@fail "Code #i did not throw an error (expected it to)"
@pass!
test "macro constructor called with no arguments is an error" ->
-> esl "(macro)"
..`@throws` Error
test "macro constructor loading from IIFE can load nothing" ->
esl """
(macro ((lambda ())))
"""
..`@equals` ""
test "macro can return multiple statements by returning an array" ->
esl "(macro declareTwo (lambda () (return (array '(var x 0) '(var y 1)))))
(declareTwo)"
..`@equals` "var x = 0;\nvar y = 1;"
test "macro can check argument type and get its value" ->
esl '''
(macro stringy (lambda (x)
(if (== (. x type) "atom")
(return ((. this string) (+ "atom:" (. x value))))
(block
(if (== (. x type) "string")
(return x)
(return "An unexpected development!"))))))
(stringy a)
(stringy "b")
'''
..`@equals` "'atom:a';\n'b';"
test "macro returning atom with empty or null name fails" ->
self = this
<[ "" null undefined ]>.for-each ->
self.throws do
-> esl """
(macro mac (lambda () (return ((. this atom) #it))))
(mac)
"""
Error
test "require in macros is relative to the eslisp file" ->
{ exec-sync } = require \child_process
{ name : root-dir, fd } = tmp.dir-sync!
# Create simple module in the root to import as a macro
module-basename = "#{uuid.v4!}.js"
module-path = path.join root-dir, module-basename
module-fd = fs.open-sync module-path, \a+
fs.write-sync module-fd, '''
module.exports = function() {
return this.string("BOOM SHAKALAKA")
}
'''
# Create an eslisp file in the root that requires the root directory module
# as a macro
main-basename = "#{uuid.v4!}.js"
main-path = path.join root-dir, main-basename
main-fd = fs.open-sync main-path, \a+
fs.write-sync main-fd, """
(macro (object x (require "./#module-basename")))
(x)
"""
# Create a subdirectory
subdir-basename = "subdir"
subdir-path = path.join root-dir, subdir-basename
fs.mkdir-sync subdir-path
# Create an eslisp file in the sub-directory that requires the module in the
# root directory (its parent directory) as a macro
main2-basename = "#{uuid.v4!}.js"
main2-path = path.join subdir-path, main2-basename
main2-fd = fs.open-sync main2-path, \a+
fs.write-sync main2-fd, """
(macro (object x (require "../#module-basename")))
(x)
"""
eslc-path = path.join process.cwd!, "bin/eslc"
try
# Call the eslisp compiler with current working directory set to the root
# directory, with both eslisp files in turn, and expect neither to error.
exec-sync "#eslc-path #main-basename", cwd : root-dir
..to-string! `@equals` "'BOOM SHAKALAKA';\n"
exec-sync "#eslc-path #main2-path", cwd : root-dir
..to-string! `@equals` "'BOOM SHAKALAKA';\n"
# This second one will only succeed if `require` within macros works
# relative to the eslisp file being compiled.
finally
e <~ rimraf root-dir
@equals e, null
test "macros can be required from node_modules relative to root directory" ->
# Create dummy temporary package
module-name = "test-#{uuid.v4!}"
dir = "./node_modules/#module-name"
fs.mkdir-sync dir
fd = fs.open-sync "#dir/index.js" \a+
fs.write-sync fd, "module.exports = function() { }"
fd = fs.open-sync "#dir/package.json" \a+
fs.write-sync fd, """
{
"name": "#module-name",
"version": "0.1.0",
"description": "test-generated module; safe to delete",
"main": "index.js",
"dependencies": {
}
}
"""
# Attempt to require it and use it as a macro
esl """
(macro (object x (require "#module-name")))
(x)
"""
..`@equals` ""
e <- rimraf dir
test "macros required from separate modules can access complation env" ->
# To set up, create a temporary file with the appropriate macro contents
{ name, fd } = tmp.file-sync!
fs.write-sync fd, """
module.exports = function() {
// Return two statements: a string and a generated symbol
return [
this.atom("ok"),
this.atom("ok2")
];
};
"""
code = esl """
(macro (object x (require "#name")))
(x)
"""
code.split "\n"
..length `@equals` 2
..0 `@equals` "ok;"
..1 `@equals` "ok2;"
fs.unlink-sync name # clean up
test "macro function can be returned from IIFE" ->
# IIFE = immediately-invoked function expression
#