Repository navigation
Expand file tree
/
Copy pathsession_test.go
More file actions
697 lines (674 loc) · 23.8 KB
/
Copy pathsession_test.go
File metadata and controls
697 lines (674 loc) · 23.8 KB
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
package ant_test
import (
"context"
"errors"
"fmt"
"io"
"maps"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/ant-src/ant"
"github.com/ant-src/ant/value"
)
func TestSession(t *testing.T) {
var out, errOut strings.Builder
s, err := ant.NewSession(context.Background(), ant.WithStdout(&out), ant.WithStderr(&errOut),
ant.WithSource("lib.ant", "export var counter = 0\nexport func bump(): void { counter += 1 }\nexport type P = { x: int }"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
eval := func(src string) value.Value {
t.Helper()
v, err := s.Eval(src)
if err != nil {
t.Fatalf("%s: %v", src, err)
}
return v
}
if v := eval("Os.program()"); v != "" {
t.Errorf("Os.program() in a session = %v", v)
}
if v := eval("1 + 2"); v != int64(3) {
t.Errorf("1 + 2 = %v", v)
}
if v := eval("var x = 10"); v != value.Void {
t.Errorf("a declaration is void, got %v", v)
}
if v := eval("x * 2"); v != int64(20) {
t.Errorf("x * 2 = %v", v)
}
eval("func double(n: int): int { n * 2 }")
eval("type Point = { x: int, y: int }")
if v := eval("double(x)"); v != int64(20) {
t.Errorf("double(x) = %v", v)
}
if v := value.Render(eval("Point{ x: 1, y: 2 }")); v != "Point{ x: 1, y: 2 }" {
t.Errorf("record: %s", v)
}
eval("var x = \"again\"")
if v := eval("x"); v != "again" {
t.Errorf("redeclared x = %v", v)
}
eval(`println("hello")`)
if !strings.Contains(out.String(), "hello\n") {
t.Errorf("stdout: %q", out.String())
}
// multi-line input
if v := eval("var f = func(a: int): int {\n a + 1\n}\nf(1)"); v != int64(2) {
t.Errorf("multi-line: %v", v)
}
// a panic is reported and the session continues
if _, err := s.Eval("panic(\"boom\")"); err == nil {
t.Error("expected a panic error")
} else if _, ok := err.(*ant.PanicError); !ok {
t.Errorf("expected *PanicError, got %T", err)
}
if !strings.Contains(errOut.String(), "boom") {
t.Errorf("stderr lacks the report: %q", errOut.String())
}
if v := eval("x"); v != "again" {
t.Errorf("after a panic x = %v", v)
}
// a syntax error is a load error
if _, err := s.Eval("var = 1"); err == nil {
t.Error("expected a syntax error")
} else if _, ok := err.(*ant.LoadError); !ok {
t.Errorf("expected *LoadError, got %T", err)
}
// imports stay in force and modules initialize once
eval(`import "lib.ant" { counter, bump, P }`)
eval("bump()")
eval("bump()")
if v := eval("counter"); v != int64(2) {
t.Errorf("counter = %v", v)
}
if v := value.Render(eval("P{ x: 7 }")); v != "P{ x: 7 }" {
t.Errorf("imported type: %s", v)
}
// an input after an import keeps its own positions, in a panic's trace and in a load error
eval("func deep(): int {\n [1][3]\n}")
if _, err := s.Eval("deep()"); err == nil {
t.Error("expected a panic error")
} else if pe, ok := err.(*ant.PanicError); !ok || len(pe.Trace) == 0 || pe.Trace[0].Pos.Line != 2 {
t.Errorf("trace after an import: %v", err)
}
if _, err := s.Eval("var a = 1\nvar = 2"); err == nil {
t.Error("expected a load error")
} else if le, ok := err.(*ant.LoadError); !ok || le.Diagnostics[0].Pos.Line != 2 {
t.Errorf("load error after an import: %v", err)
}
// incomplete inputs
for _, src := range []string{"func f() {", "var s = \"open", "[1,", "\"${",
"f(1,\n", "/* open\n", "`a\n", "var s = \"open\n", "<ul>\n <li>a</li>", "<>\n <p>a</p>"} {
if !ant.Incomplete(src) {
t.Errorf("%q should be incomplete", src)
}
}
for _, src := range []string{"1 + 2", "func f() {}", "var x = = 1",
"var r = /ab\n", "if true\n", "var x = = 1\n", "<ul>\n <li>a</li>\n</ul>"} {
if ant.Incomplete(src) {
t.Errorf("%q should not be incomplete", src)
}
}
}
// TestSessionContext checks that a session's context ends an input that never waits.
func TestSessionContext(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
s, err := ant.NewSession(ctx)
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Eval("var n = 0\nwhile true { n += 1 }"); err == nil {
t.Fatal("an endless loop returned without an error")
}
}
// TestSessionGrowsUnderGoroutines: an input that declares new names grows the session's module
// while a goroutine of an earlier input stores into it, reads an import and calls a function;
// no store is lost and nothing races.
func TestSessionGrowsUnderGoroutines(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithStdout(&strings.Builder{}),
ant.WithSource("lib.ant", "export var step = 1"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Eval(`import "./lib.ant" L
var n = 0
var done: Channel[bool] = Channel.new(1)
func add(): void { n += L.step }
go { for i in Iter.range(0, 100000) { add() }; Channel.send(done, true) }`); err != nil {
t.Fatal(err)
}
for i := 0; i < 200; i++ {
if _, err := s.Eval(fmt.Sprintf("var v%d = %d", i, i)); err != nil {
t.Fatal(err)
}
}
v, err := s.Eval("Channel.receive(done)\nn")
if err != nil || v != value.Value(int64(100000)) {
t.Errorf("n = %v, %v; want 100000", v, err)
}
}
// TestSessionImportInFailedInput checks that an import stays in force when the rest of its
// input panics, as the input's declarations do.
func TestSessionImportInFailedInput(t *testing.T) {
var errOut strings.Builder
s, err := ant.NewSession(context.Background(), ant.WithStderr(&errOut),
ant.WithSource("lib.ant", "export var counter = 0\nexport func bump(): int { counter += 1; counter }"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Eval("import \"lib.ant\" L\nvar a = L.bump()\npanic(\"boom\")"); err == nil {
t.Fatal("expected a panic error")
}
if v, err := s.Eval("L.bump()"); err != nil || v != int64(2) {
t.Errorf("L.bump() = %v, %v", v, err)
}
}
// TestSessionTransitiveImport checks that a module an input imports may import another, in the
// input that imports it and in the inputs after it, which take the import by its absolute path.
func TestSessionTransitiveImport(t *testing.T) {
dir := t.TempDir()
for name, src := range map[string]string{
"b.ant": "export func b(): int { 2 }",
"a.ant": "import \"./b.ant\" { b }\nexport func a(): int { b() + 1 }",
} {
if err := os.WriteFile(filepath.Join(dir, name), []byte(src), 0o644); err != nil {
t.Fatal(err)
}
}
t.Chdir(dir)
s, err := ant.NewSession(context.Background())
if err != nil {
t.Fatal(err)
}
defer s.Close()
if v, err := s.Eval("import \"./a.ant\" { a }\na()"); err != nil || v != int64(3) {
t.Fatalf("a() = %v, %v", v, err)
}
if v, err := s.Eval("a()"); err != nil || v != int64(3) {
t.Errorf("a() in a later input = %v, %v", v, err)
}
}
// TestSessionTypeFuncLater checks that a function may be declared on a type an earlier input
// declared, and not on a name an earlier input redeclared as a variable.
func TestSessionTypeFuncLater(t *testing.T) {
s, err := ant.NewSession(context.Background())
if err != nil {
t.Fatal(err)
}
defer s.Close()
for _, src := range []string{"type P = { x: int }", "func P.get(p: P): int { p.x * 2 }"} {
if _, err := s.Eval(src); err != nil {
t.Fatalf("%s: %v", src, err)
}
}
if v, err := s.Eval("P.get(P{ x: 4 })"); err != nil || v != int64(8) {
t.Errorf("P.get = %v, %v", v, err)
}
if _, err := s.Eval("var P = 1"); err != nil {
t.Fatal(err)
}
if _, err := s.Eval("func P.other(): int { 1 }"); err == nil {
t.Error("expected a load error for a function on a variable")
}
// a type whose input failed to load is not a type of the session
if _, err := s.Eval("type Z = { x: int }; var print = 1"); err == nil {
t.Fatal("expected a load error")
}
if _, err := s.Eval("func Z.get(): int { 1 }"); err == nil {
t.Error("expected a load error for a function on a type that failed to load")
}
}
// TestSessionFieldDefaults: a default reads the session's variables at each literal, a later
// input's literal fills in a type an earlier input declared, and a redeclared type takes its new
// defaults.
func TestSessionFieldDefaults(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithSource("lib.ant", "var hidden = 3\nexport type L = { n: int = hidden }"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
render := func(src string) string {
t.Helper()
v, err := s.Eval(src)
if err != nil {
t.Fatalf("%s: %v", src, err)
}
return value.Render(v)
}
render("var base = 5")
render("type C = { n: int = base, tags: Array[string] = [] }")
if got := render("C{}"); got != "C{ n: 5, tags: [] }" {
t.Errorf("C{} = %s", got)
}
render("base = 7")
if got := render("C{ tags: [\"a\"] }"); got != `C{ n: 7, tags: ["a"] }` {
t.Errorf("after base = 7: %s", got)
}
render("var one = C{}")
render("Array.push(one.tags, \"x\")")
if got := render("C{}.tags"); got != "[]" {
t.Errorf("a default shared its array: %s", got)
}
render("type C = { n: int = 1, m: int = 2 }")
if got := render("C{ m: 3 }"); got != "C{ n: 1, m: 3 }" {
t.Errorf("redeclared C = %s", got)
}
render(`import "lib.ant" { L }`)
if got := render("L{}"); got != "L{ n: 3 }" {
t.Errorf("imported L{} = %s", got)
}
if got := render("C{ n: 4 }"); got != "C{ n: 4, m: 2 }" {
t.Errorf("C{ n: 4 } = %s", got)
}
if _, err := s.Eval("type D = { a: int, b: int = 1 }\nD{ b: 2 }"); err == nil || !strings.Contains(err.Error(), "missing the field a") {
t.Errorf("D{ b: 2 } = %v, want missing the field a", err)
}
}
// TestSessionInterrupts: a session honors WithInterrupts as a run does, so an interrupt ends
// an input that never waits.
func TestSessionInterrupts(t *testing.T) {
interrupts := make(chan struct{}, 1)
// The deadline only bounds the test: without the interrupt the input would never end.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s, err := ant.NewSession(ctx, ant.WithInterrupts(interrupts))
if err != nil {
t.Fatal(err)
}
defer s.Close()
time.AfterFunc(50*time.Millisecond, func() { interrupts <- struct{}{} })
if _, err := s.Eval("var n = 0\nwhile true { n += 1 }"); !errors.Is(err, ant.ErrInterrupted) {
t.Fatalf("an interrupted input ended with %v, want ErrInterrupted", err)
}
}
// TestSessionComplete checks that completion sees what earlier inputs declared, by their latest
// types, and what the position allows.
func TestSessionComplete(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithStdout(io.Discard), ant.WithStderr(io.Discard))
if err != nil {
t.Fatal(err)
}
defer s.Close()
for _, src := range []string{"type Point = { x: int, y: int }", "var p = Point{ x: 1, y: 2 }", "var n = 1", "var n = \"a\"", "func twice(k: int): int { k * 2 }", "p.x + 1"} {
if _, err := s.Eval(src); err != nil {
t.Fatalf("%s: %v", src, err)
}
}
labels := func(src string) map[string]ant.Completion {
out := map[string]ant.Completion{}
for _, c := range s.Complete(src, len(src)) {
out[c.Label] = c
}
return out
}
if got := labels("p."); got["x"].Kind != "field" || got["y"].Detail != "int" {
t.Errorf("p.: %v", got)
}
if got := labels("n | String."); got["length"].Kind != "function" {
t.Errorf("n | String.: %v", got)
}
if got := labels("tw"); got["twice"].Kind != "function" || !strings.Contains(got["twice"].Detail, "k: int") {
t.Errorf("tw: %v", got["twice"])
}
if got := labels("twice("); !got["k:"].Preferred || got["k:"].Insert != "k: " {
t.Errorf("twice(: %v", got["k:"])
}
// the parameters of a call come in their order
var params []string
for _, c := range s.Complete("Http.get(", len("Http.get(")) {
if c.Preferred {
params = append(params, c.Label)
}
}
if strings.Join(params, " ") != "url: headers: timeout: ctx:" {
t.Errorf("Http.get( parameters: %q", params)
}
// after a named argument, the parameters not given yet, in their order
for _, src := range []string{`Http.get(url: "x", `, `var a = try Http.get(url: "x", `, `Http.get(url: "x", ti`, `Http.get("x", timeout: null, `} {
params = nil
for _, c := range s.Complete(src, len(src)) {
if c.Preferred {
params = append(params, c.Label)
}
}
want := "headers: timeout: ctx:"
if strings.Contains(src, "timeout: null") {
want = "headers: ctx:"
}
if strings.Join(params, " ") != want {
t.Errorf("%s: parameters %q, want %q", src, params, want)
}
}
if got := labels(""); got["Array"].Kind != "namespace" || got["var"].Kind != "keyword" || got["n"].Detail != "string" {
t.Errorf("empty: Array %v, var %v, n %v", got["Array"], got["var"], got["n"])
}
if got := s.Complete("x", 5); got != nil {
t.Errorf("offset past the end: %v", got)
}
// a Result is an Ok or an Err, which share no field, so the type promises none; the
// value at runtime is an Ok, whose value is read through, and the Result functions that
// take it first are offered on it (section 5, Calls through .)
if _, err := s.Eval("var r: Result[Point, Error] = Ok{ value: p }"); err != nil {
t.Fatal(err)
}
if got := labels("r."); got["value"].Kind != "field" || got["value"].Detail != "Point" || got["unwrap"].Kind != "function" {
t.Errorf("r.: %v", got)
} else if names := slices.Sorted(maps.Keys(got)); strings.Join(names, " ") != "err isOk map mapErr ok unwrap unwrapOr value wrapErr" {
t.Errorf("r.: %q", names)
}
if got := labels("r.va"); got["value"].Label != "value" {
t.Errorf("r.va: %v", got)
}
if got := labels("r.value."); got["x"].Detail != "int" || got["y"].Detail != "int" {
t.Errorf("r.value.: %v", got)
}
if got := labels("r?.value?."); got["x"].Detail != "int" {
t.Errorf("r?.value?.: %v", got)
}
// not through an expression, and not a field that is not there
if got := labels("twice(1).r."); len(got) != 0 {
t.Errorf("twice(1).r.: %v", got)
}
if got := labels("r.nope."); len(got) != 0 {
t.Errorf("r.nope.: %v", got)
}
}
// BenchmarkSessionComplete measures one completion on a session with an import and many
// declarations: what a keystroke costs.
func BenchmarkSessionComplete(b *testing.B) {
s, err := ant.NewSession(context.Background(), ant.WithStdout(io.Discard), ant.WithStderr(io.Discard))
if err != nil {
b.Fatal(err)
}
defer s.Close()
if _, err := s.Eval(`import "tests/run/lib/geometry.ant" { distance, Point }`); err != nil {
b.Fatal(err)
}
for i := 0; i < 200; i++ {
if _, err := s.Eval(fmt.Sprintf("func f%d(n: int): int { n + %d }", i, i)); err != nil {
b.Fatal(err)
}
}
s.Eval("var xs = [1, 2, 3]")
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Complete("xs | Array.", len("xs | Array."))
}
}
// TestSessionTry checks that an input is a script body of its own: try unwraps an Ok and
// returns an Err from the input, which is reported and leaves the session going; return gives
// the input's result; defer runs when the input ends.
func TestSessionTry(t *testing.T) {
var out, errOut strings.Builder
s, err := ant.NewSession(context.Background(), ant.WithStdout(&out), ant.WithStderr(&errOut))
if err != nil {
t.Fatal(err)
}
defer s.Close()
eval := func(src string) (value.Value, error) {
t.Helper()
v, err := s.Eval(src)
if _, ok := err.(*ant.LoadError); ok {
t.Fatalf("%s: %v", src, err)
}
return v, err
}
if _, err := eval("var a = try Ok{ value: 1 }"); err != nil {
t.Fatal(err)
}
if v, _ := eval("a + 1"); v != int64(2) {
t.Errorf("a + 1 = %v", v)
}
_, err = eval("var b = try Err{ error: Error{ message: \"no page\" } }\nprintln(\"unreached\")")
if _, ok := err.(*ant.ResultError); !ok || err.Error() != "no page" {
t.Errorf("try on Err: %v", err)
}
if !strings.Contains(errOut.String(), "no page\n") || strings.Contains(out.String(), "unreached") {
t.Errorf("stdout %q, stderr %q", out.String(), errOut.String())
}
if v, err := eval("return a * 10\n99"); err != nil || v != int64(10) {
t.Errorf("return: %v, %v", v, err)
}
if v, err := eval("var x: int | null = null\nx ?? return \"none\""); err != nil || v != "none" {
t.Errorf("?? return: %v, %v", v, err)
}
out.Reset()
if _, err := eval("defer { println(\"deferred\") }\nprintln(\"body\")"); err != nil {
t.Fatal(err)
}
if out.String() != "body\ndeferred\n" {
t.Errorf("defer: %q", out.String())
}
// the session goes on after an Err
if v, _ := eval("a"); v != int64(1) {
t.Errorf("a = %v", v)
}
}
// TestSessionOutputBetweenInputs checks that a goroutine's output reaches stdout while the session
// waits for its next input.
func TestSessionOutputBetweenInputs(t *testing.T) {
out := &syncBuffer{}
s, err := ant.NewSession(context.Background(), ant.WithStdout(out), ant.WithStderr(io.Discard))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Eval("go { Time.sleep(Time.duration(milliseconds: 20)); print(\"late\") }"); err != nil {
t.Fatal(err)
}
deadline := time.Now().Add(5 * time.Second)
for out.String() != "late" {
if time.Now().After(deadline) {
t.Fatalf("stdout %q", out.String())
}
time.Sleep(5 * time.Millisecond)
}
}
type syncBuffer struct {
mu sync.Mutex
sb strings.Builder
}
func (b *syncBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.sb.Write(p)
}
func (b *syncBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.sb.String()
}
// TestSessionTypeOf checks the types :type prints, over what the session declared.
// TestSessionInterface checks that an interface declared in one input stays declared for the
// analysis of later inputs (section 4, Interfaces).
func TestSessionInterface(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithStdout(io.Discard), ant.WithStderr(io.Discard))
if err != nil {
t.Fatal(err)
}
defer s.Close()
for _, src := range []string{"interface Shape { area(s: Self): float64 }", "type Circle = { radius: float64 }",
"func Circle.area(c: Circle): float64 { c.radius }", "var s: Shape = Circle{ radius: 2.0 }"} {
if _, err := s.Eval(src); err != nil {
t.Fatal(err)
}
}
for src, want := range map[string]string{"s": "Shape", "s.area()": "float64"} {
if got, err := s.TypeOf(src); err != nil || got != want {
t.Errorf("TypeOf(%q) = %q, %v; want %q", src, got, err, want)
}
}
offered := false
for _, it := range s.Complete("s.", 2) {
if it.Label == "area" {
offered = true
}
}
if !offered {
t.Errorf("s. does not offer area")
}
// a later input may declare the name again, as any session declaration may, and a variable
// declared over an interface gets a field of its own
for _, src := range []string{"interface Shape { area(s: Self): float64; name(s: Self): string }", "var Shape = 1", "var t = Shape + 1", "interface Circle { r(c: Self): float64 }", "var c2 = 2"} {
if _, err := s.Eval(src); err != nil {
t.Fatalf("%s: %v", src, err)
}
}
for src, want := range map[string]string{"Shape": "int", "t": "int", "c2": "int"} {
if got, err := s.TypeOf(src); err != nil || got != want {
t.Errorf("TypeOf(%q) = %q, %v; want %q", src, got, err, want)
}
}
if v, ok := s.Value("t"); !ok || v != int64(2) {
t.Errorf("t = %v, %v", v, ok)
}
}
func TestSessionTypeOf(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithStdout(io.Discard), ant.WithStderr(io.Discard))
if err != nil {
t.Fatal(err)
}
defer s.Close()
for _, src := range []string{"type Point = { x: int, y: int }", "var p = Point{ x: 1, y: 2 }", "func twice(k: int): int { k * 2 }", "var xs = [1, 2]"} {
if _, err := s.Eval(src); err != nil {
t.Fatal(err)
}
}
for src, want := range map[string]string{
"p": "Point",
"p.x + 1": "int",
"twice": "func(int): int",
"xs | Array.map({ \"\" })": "Array[string]",
"Http.get": "func(string, Map[string, string] = ..., Time.Duration | null = ..., Context | null = ...): Result[Http.Response, Error]",
"Http.get(\"u\")": "Result[Http.Response, Error]",
} {
got, err := s.TypeOf(src)
if err != nil || got != want {
t.Errorf("TypeOf(%q) = %q, %v; want %q", src, got, err, want)
}
}
for _, src := range []string{"p.z", "nope", "1 +", ""} {
if got, err := s.TypeOf(src); err == nil {
t.Errorf("TypeOf(%q) = %q, want an error", src, got)
} else if le, ok := err.(*ant.LoadError); ok && len(le.Diagnostics) > 0 && le.Diagnostics[0].Pos.Line > 1 {
t.Errorf("TypeOf(%q): %v is not positioned in the expression", src, err)
}
}
}
// TestSessionLast checks that _ reads the result of the last input that had one, and is still
// written to as a discard.
func TestSessionLast(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithStdout(io.Discard), ant.WithStderr(io.Discard))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Eval("_"); err == nil {
t.Error("_ before any result reads")
}
for _, step := range []struct {
src string
want value.Value
}{
{"40 + 1", int64(41)},
{"_ + 1", int64(42)},
{"var x = 5", value.Void}, // void leaves _ as it was
{"_", int64(42)},
{"_ = 7", value.Void}, // a discard still
{"_", int64(42)},
{"\"s\"", "s"},
{"_ + \"t\"", "st"},
} {
v, err := s.Eval(step.src)
if err != nil || v != step.want {
t.Errorf("%s = %v, %v; want %v", step.src, v, err, step.want)
}
}
if v, ok := s.Value("_"); !ok || v != "st" {
t.Errorf("Value(_) = %v, %v", v, ok)
}
}
// TestSessionLet: a let entered in a session holds for the rest of it. A later input may not
// declare its name again nor assign it, though it may still declare other names again and turn
// a var into a let (section 10, Scripts).
func TestSessionLet(t *testing.T) {
s, err := ant.NewSession(context.Background())
if err != nil {
t.Fatal(err)
}
defer s.Close()
for _, src := range []string{"let limit = 1", "var count = 1", "var count = 2", "let count = 3"} {
if _, err := s.Eval(src); err != nil {
t.Fatalf("%s: %v", src, err)
}
}
for src, want := range map[string]string{
"var limit = 2": "limit is bound by let earlier in the session",
"let limit = 2": "limit is bound by let earlier in the session",
"limit = 2": "assignment to let name limit",
"count += 1": "assignment to let name count",
"func limit() {}": "limit is bound by let earlier in the session",
"let r = 1\nlet r = 2": "r is already declared in this scope",
"var w = 1\nvar w = 2": "w is already declared in this scope",
} {
if _, err := s.Eval(src); err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("%s: error %v, want one containing %q", src, err, want)
}
}
if v, err := s.Eval("limit + count"); err != nil || v != int64(4) {
t.Errorf("limit + count = %v, %v", v, err)
}
}
// TestSessionLetFailures: a let whose initializer panics is not bound, so a later input may
// declare its name again; an import may not replace a let; and a let the analysis keeps as
// written does not break the analysis of later inputs.
func TestSessionLetFailures(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithStderr(io.Discard), ant.WithSource("m.ant", "export var q = 5"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Eval(`let p: int = panic("boom")`); err == nil {
t.Fatal("the panic was not reported")
}
if v, err := s.Eval("let p = 1\np"); err != nil || v != int64(1) {
t.Errorf("let p again after a failed let: %v, %v", v, err)
}
if _, err := s.Eval("let q = 1"); err != nil {
t.Fatal(err)
}
if _, err := s.Eval(`import "./m.ant" { q }`); err == nil || !strings.Contains(err.Error(), "q is bound by let earlier in the session") {
t.Errorf("an import replacing a let: %v", err)
}
for _, src := range []string{"let [a, b] = [[], []]", "var c = 2"} {
if _, err := s.Eval(src); err != nil {
t.Fatalf("%s: %v", src, err)
}
}
if ty, err := s.TypeOf("c"); err != nil || ty != "int" {
t.Errorf("TypeOf(c) after an untyped let destructuring = %q, %v", ty, err)
}
}
// TestSessionTypeOfWarns checks that code that only warns, such as a block with an unused let,
// still has a type.
func TestSessionTypeOfWarns(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithStdout(io.Discard), ant.WithStderr(io.Discard))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if got, err := s.TypeOf("func(): int { let q = 3\n 4 }"); err != nil || got != "func(): int" {
t.Errorf("TypeOf = %q, %v; want func(): int", got, err)
}
}