-
Notifications
You must be signed in to change notification settings - Fork 1
/
starlarkproto_test.go
80 lines (68 loc) · 1.81 KB
/
starlarkproto_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
package starlarkproto
import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
"go.starlark.net/starlarktest"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
_ "github.com/emcfarlane/starlarkproto/testpb" // import side effect
)
func load(thread *starlark.Thread, module string) (starlark.StringDict, error) {
if module == "assert.star" {
return starlarktest.LoadAssertModule()
}
return nil, fmt.Errorf("unknown module %s", module)
}
func TestExecFile(t *testing.T) {
protoregistry.GlobalFiles.RangeFiles(func(p protoreflect.FileDescriptor) bool {
fmt.Println("file!", p.FullName())
fmt.Println(" ", p.Path())
fmt.Println(" ", p.Name())
fmt.Println(" ", p.Package())
return true
})
resolve.AllowGlobalReassign = true
thread := &starlark.Thread{Load: load}
starlarktest.SetReporter(thread, t)
globals := starlark.StringDict{
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
"proto": NewModule(protoregistry.GlobalFiles),
}
files, err := filepath.Glob("testdata/*.star")
if err != nil {
t.Fatal(err)
}
for _, filename := range files {
src, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
_, err = starlark.ExecFile(thread, filename, src, globals)
switch err := err.(type) {
case *starlark.EvalError:
var found bool
for i := range err.CallStack {
posn := err.CallStack.At(i).Pos
if posn.Filename() == filename {
linenum := int(posn.Line)
msg := err.Error()
t.Errorf("\n%s:%d: unexpected error: %v", filename, linenum, msg)
found = true
break
}
}
if !found {
t.Error(err.Backtrace())
}
case nil:
// success
default:
t.Errorf("\n%s", err)
}
}
}