forked from cosmorse/protoc-gen-ego
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_test.go
More file actions
109 lines (92 loc) · 1.89 KB
/
runner_test.go
File metadata and controls
109 lines (92 loc) · 1.89 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
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
package main
import (
"go/ast"
"go/format"
"go/parser"
"go/token"
"io"
"os"
"strings"
"testing"
"github.com/cosmorse/protoc-gen-ego/testdata"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
)
func TestRun(t *testing.T) {
os.Args = []string{"protoc-gen-test"}
camelcaseEnumConstants = true
file, err := os.Open("testdata/message.desc")
if err != nil {
t.Error(err)
return
}
defer file.Close()
opts := &protogen.Options{}
err = run(file, opts)
if err != nil {
t.Error(err)
}
}
func TestParseGoSource(t *testing.T) {
in, err := os.Open("testdata/message.pb.go")
if err != nil {
t.Error(err)
return
}
defer in.Close()
src, err := io.ReadAll(in)
if err != nil {
t.Error(err)
return
}
fileSet := token.NewFileSet()
file, err := parser.ParseFile(fileSet, "", src, parser.ParseComments)
if err != nil {
t.Error(err)
return
}
var objName string
ast.Inspect(file, func(node ast.Node) bool {
switch obj := node.(type) {
case *ast.TypeSpec:
if obj.Name != nil && obj.Name.Name == "Message" {
objName = obj.Name.Name
}
case *ast.StructType:
for _, field := range obj.Fields.List {
t.Log(objName)
if field.Tag != nil && len(field.Names) == 1 && field.Names[0].String() == "Id" {
field.Tag.Value = strings.Replace(field.Tag.Value, "id,omitempty", "_id", 1)
field.Names[0].Name = "TEST_ID"
}
}
}
return true
})
err = format.Node(os.Stdout, fileSet, file)
if err != nil {
t.Error(err)
return
}
}
func TestGeneration(t *testing.T) {
msg := &testdata.Message{
Id: 1,
Quote: []byte("123"),
Role: testdata.GroupRoleCreator,
}
msg.Pet = &testdata.Message_Cat{
Cat: &testdata.Cat{Name: "cat"},
}
data, err := proto.Marshal(msg)
if err != nil {
t.Error(err)
return
}
var msg2 testdata.Message
err = proto.Unmarshal(data, &msg2)
if err != nil {
t.Error(err)
}
t.Log(&msg2)
}