-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestlib.go
More file actions
55 lines (50 loc) · 1.23 KB
/
testlib.go
File metadata and controls
55 lines (50 loc) · 1.23 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
package testlib
import (
"context"
"embed"
"log"
"testing"
"github.com/jig/lisp"
"github.com/jig/lisp/env"
"github.com/jig/lisp/types"
)
type PackageDecl []struct {
Name string
Load func(tenv types.EnvType) error
}
func Directory(t *testing.T, directory embed.FS, packages PackageDecl) error {
d, err := directory.ReadDir(".")
if err != nil {
return err
}
for _, entry := range d {
t.Run(entry.Name(), func(t *testing.T) {
testFile, err := directory.ReadFile(entry.Name())
if err != nil {
t.Fatalf("%s/ReadFile Error: %s", entry.Name(), err)
}
File(t, entry.Name(), string(testFile), packages)
})
}
return nil
}
func File(t *testing.T, entryName, testFile string, packages PackageDecl) {
tenv := env.NewEnv()
for _, library := range packages {
if err := library.Load(tenv); err != nil {
log.Fatalf("Library Load Error: %v\n", err)
}
}
expr, err := lisp.READ(string(testFile), types.NewCursorFile(entryName), tenv)
if err != nil {
t.Fatalf("%s/READ Error: %s", entryName, err)
}
ctx := context.Background()
res, err := lisp.EVAL(ctx, expr, tenv)
if err != nil {
t.Fatalf("%s/EVAL Error: %s", entryName, err)
}
if r, ok := res.(bool); !r || !ok {
t.Fatalf("%s/TEST failed: %v", entryName, res)
}
}