-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
87 lines (73 loc) · 1.53 KB
/
main_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
package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
"testing"
"github.com/emcfarlane/starlarkproto"
"google.golang.org/protobuf/reflect/protoreflect"
)
var flagGenStar = flag.Bool("genstar", false, "Verbose mode.")
func TestProto(t *testing.T) {
flag.Parse()
var b strings.Builder
b.WriteString("# generated by go generate; DO NOT EDIT\n")
type identifier struct {
protoreflect.FileDescriptor
name string
}
var (
fds []identifier
)
for name, value := range globals {
if fd, ok := value.(*starlarkproto.Descriptor); ok {
fds = append(fds, identifier{
FileDescriptor: fd.Descriptor().(protoreflect.FileDescriptor),
name: name,
})
}
}
sort.Slice(fds, func(i, j int) bool {
return fds[i].Path() < fds[j].Path()
})
for _, fd := range fds {
name := fd.name
path := fd.Path()
fmt.Fprintf(&b, "%s = proto.file(%q)\n", name, path)
}
if *flagGenStar {
if err := os.WriteFile("protos.star", []byte(b.String()), 0644); err != nil {
t.Fatal(err)
}
t.Log("wrote protos.star")
} else {
t.Log(b.String())
}
}
// TODO: easily convert YAML to proto to Starlark
func TestYAMLtoSTAR(t *testing.T) {
tests := []struct {
name string
file string
}{{
name: "init",
file: "testdata/yaml.yaml",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fsrc, err := os.ReadFile(tt.file)
if err != nil {
t.Fatal(err)
}
msgs, err := yamlToProtos(string(fsrc))
if err != nil {
t.Fatal(err)
}
for _, msg := range msgs {
t.Log(msg)
}
})
}
}