-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslang_test.go
161 lines (140 loc) · 2.7 KB
/
slang_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
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
package slang_test
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/spy16/sabre"
"github.com/spy16/slang"
)
const (
testDir = "./lib"
libDir = "./lib"
)
var _ sabre.Scope = (*slang.Slang)(nil)
func TestSlang_Bind(t *testing.T) {
sl := slang.New()
tests := []struct {
name string
symbol string
ns string
wantErr bool
}{
{
name: "CrossNamespaceBindingValidation",
symbol: "core/not",
ns: "user",
wantErr: true,
},
{
name: "BindingInCurrentNS",
symbol: "hello",
ns: "user",
wantErr: false,
},
{
name: "UserBinding",
symbol: "user/hello",
ns: "user",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := sl.Bind(tt.symbol, sabre.Nil{})
if (err != nil) != tt.wantErr {
t.Errorf("Bind() error = %#v, wantErr %#v", err, tt.wantErr)
return
}
})
}
}
func TestSlang_Resolve(t *testing.T) {
sl := slang.New()
sl.Bind("pi", sabre.Float64(3.1412))
tests := []struct {
name string
symbol string
wantErr bool
}{
{
name: "CoreBinding",
symbol: "core/impl?",
},
{
name: "UserBinding",
symbol: "hello",
wantErr: true,
},
{
name: "MissingUserBinding",
symbol: "hello",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := sl.Resolve(tt.symbol)
if (err != nil) != tt.wantErr {
t.Errorf("Resolve() error = %#v, wantErr %#v", err, tt.wantErr)
return
}
})
}
}
func TestSlang(t *testing.T) {
if testing.Short() {
return
}
t.Parallel()
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Fatalf("failed to read dir: %v", err)
}
for _, fi := range files {
if !strings.HasSuffix(fi.Name(), "_test.lisp") {
continue
}
t.Run(fi.Name(), func(t *testing.T) {
testFile(t, filepath.Join(testDir, fi.Name()))
})
}
}
func testFile(t *testing.T, file string) {
fh, err := os.Open(file)
if err != nil {
t.Fatalf("failed to open file: %v", err)
}
defer fh.Close()
sl, err := initSlang()
if err != nil {
t.Fatalf("failed to init slang: %v", err)
}
_, err = sl.ReadEval(fh)
if err != nil {
t.Errorf("execution failed for '%s': %v", file, err)
}
}
func initSlang() (*slang.Slang, error) {
di, err := ioutil.ReadDir(testDir)
if err != nil {
return nil, err
}
sl := slang.New()
for _, fi := range di {
if !strings.HasSuffix(fi.Name(), ".lisp") ||
strings.HasSuffix(fi.Name(), "_test.lisp") {
continue
}
fh, err := os.Open(filepath.Join(testDir, fi.Name()))
if err != nil {
return nil, err
}
defer fh.Close()
if _, err := sl.ReadEval(fh); err != nil {
return nil, err
}
}
return sl, nil
}