-
Notifications
You must be signed in to change notification settings - Fork 8
/
node_test.go
56 lines (51 loc) · 1.12 KB
/
node_test.go
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
// Copyright 2020 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package node
import (
"fmt"
"testing"
)
func TestNode(t *testing.T) {
ch := make(chan bool)
vm := New(&Options{
OnEmit: func(arg string) {
if arg != "100" {
t.Fatalf("expected '%v', got '%v'", "100", arg)
} else {
ch <- true
}
},
})
vm.Run("x=0")
N := 100
for i := 0; i < N; i++ {
val := vm.Run("++x")
if err := val.Error(); err != nil {
t.Fatal(err)
}
if val.String() != fmt.Sprint(i+1) {
t.Fatalf("expected '%v', got '%v'", fmt.Sprint(i+1), val)
}
}
val := vm.Run("x")
if err := val.Error(); err != nil {
t.Fatal(err)
}
if val.String() != fmt.Sprint(N) {
t.Fatalf("expected '%v', got '%v'", fmt.Sprint(N), val)
}
vm.Run("emit(100)")
<-ch
v := vm.Run("throw new Error('hello')")
if v.Error() == nil {
t.Fatal("expected an error")
}
err, ok := v.Error().(ErrThrown)
if !ok {
t.Fatal("expected an ErrThrown")
}
if err.Error() != "Error: hello" {
t.Fatalf("expected '%s', got '%s'", "Error: hello", err.Error())
}
}