-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
106 lines (91 loc) · 2.92 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
func Test_main(t *testing.T) {
var suffix = "Out"
ass := assert.New(t)
binary, err := os.ReadFile("./test/stdin")
ass.NoError(err)
req := &pluginpb.CodeGeneratorRequest{}
err = proto.Unmarshal(binary, req)
ass.NoError(err)
plugin, err := protogen.Options{}.New(req)
ass.NoError(err)
err = func(plugin *protogen.Plugin, t *testing.T) error {
for _, file := range plugin.Files {
for _, message := range file.Proto.GetMessageType() {
ass.Equal(message.GetName(), "OutgoingMessageOut")
if strings.HasSuffix(message.GetName(), suffix) {
ass.Equal(&gen{
ModelNamePrivate: strings.ToLower(message.GetName()),
ModelName: message.GetName(),
PackageName: string(file.GoPackageName),
PathFile: file.Desc.Path(),
}, &gen{
ModelNamePrivate: "outgoingmessageout",
ModelName: "OutgoingMessageOut",
PackageName: "outgoingv1",
PathFile: "outgoing_topic/v1/outgoing.proto",
})
tmpl, err := parseTemplates(&gen{
ModelNamePrivate: strings.ToLower(message.GetName()),
ModelName: message.GetName(),
PackageName: string(file.GoPackageName),
PathFile: file.Desc.Path(),
})
if err != nil {
ass.NoError(err)
plugin.Error(err)
continue
}
msgName := strings.ToLower(strings.Replace(message.GetName(), suffix, "", 2))
filename := fmt.Sprintf("%s_%s.kafka.go", file.GeneratedFilenamePrefix, msgName)
genFile := plugin.NewGeneratedFile(filename, file.GoImportPath)
if _, err = genFile.Write([]byte(tmpl)); err != nil {
ass.NoError(err)
plugin.Error(err)
continue
}
}
}
}
return nil
}(plugin, t)
ass.NoError(err)
}
func Test_parseTemplates(t *testing.T) {
var crateFileTable = []struct {
label string
tmplBody string
tmplData *gen
wantContent string
}{
{
tmplBody: `templates GEN-TEST-1 {{.ModelNamePrivate}} {{.ModelName}} {{.PackageName}} {{.PathFile}}`,
tmplData: &gen{ModelNamePrivate: "test1", ModelName: "Test1", PackageName: "testv1", PathFile: "/to.proto"},
wantContent: `templates GEN-TEST-1 test1 Test1 testv1 /to.proto`,
},
{
tmplBody: `templates GEN-TEST-2 {{.ModelNamePrivate}} {{.ModelName}} {{.PackageName}} {{.PathFile}}`,
tmplData: &gen{ModelNamePrivate: "abc", ModelName: "ABC", PackageName: "abcv1", PathFile: "/abc.proto"},
wantContent: `templates GEN-TEST-2 abc ABC abcv1 /abc.proto`,
},
}
ass := assert.New(t)
for _, tt := range crateFileTable {
t.Run(tt.label, func(t *testing.T) {
handlerKafkaTmpl = tt.tmplBody
result, err := parseTemplates(tt.tmplData)
ass.NoError(err)
ass.Equal(result, tt.wantContent)
})
}
}