-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
162 lines (126 loc) · 5.07 KB
/
plugin_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"bytes"
"os"
"strings"
"testing"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
)
func Test_SupportNewTimestampFormat(t *testing.T) {
fixture, _ := os.ReadFile("./fixtures/money.pb")
fds := new(descriptor.FileDescriptorSet)
proto.Unmarshal(fixture, fds)
req := new(plugin_go.CodeGeneratorRequest)
req.ProtoFile = fds.GetFile()
req.FileToGenerate = append(req.FileToGenerate, fds.GetFile()[0].GetName())
plugin := &plugin{out: &bytes.Buffer{}}
res, _ := plugin.Generate(req)
content := res.GetFile()[0].GetContent()
if !strings.Contains(content, "scalar Timestamp") {
t.Errorf("Expected the schema to include a scalar definition for Time, %s", content)
}
if !strings.Contains(content, "createdAt: Timestamp") {
t.Errorf("Expected the schema to reflect the ISO8601 json serialization format, %s", content)
}
}
func Test_CanGenerateCamelCaseFieldNames(t *testing.T) {
fixture, _ := os.ReadFile("./fixtures/money.pb")
fds := new(descriptor.FileDescriptorSet)
proto.Unmarshal(fixture, fds)
req := new(plugin_go.CodeGeneratorRequest)
req.ProtoFile = fds.GetFile()
req.FileToGenerate = append(req.FileToGenerate, fds.GetFile()[0].GetName())
plugin := &plugin{out: &bytes.Buffer{}}
res, _ := plugin.Generate(req)
content := res.GetFile()[0].GetContent()
if !strings.Contains(content, "amountCents") {
t.Errorf("Expected generated schema to contain camelCase field names, but got this instead: %v", content)
}
if strings.Contains(content, "amount_cents") {
t.Errorf("Expected generated schema not to contain snake_case field names, but got this instead: \n%v", content)
}
}
func Test_CanGenerate(t *testing.T) {
fixture, _ := os.ReadFile("./fixtures/money.pb")
fds := new(descriptor.FileDescriptorSet)
proto.Unmarshal(fixture, fds)
req := new(plugin_go.CodeGeneratorRequest)
req.ProtoFile = fds.GetFile()
req.FileToGenerate = append(req.FileToGenerate, fds.GetFile()[0].GetName())
plugin := &plugin{out: &bytes.Buffer{}}
res, _ := plugin.Generate(req)
content := res.GetFile()[0].GetContent()
t.Log(content)
}
func Test_CanAddTypeComments(t *testing.T) {
fixture, _ := os.ReadFile("./fixtures/money.pb")
fds := new(descriptor.FileDescriptorSet)
proto.Unmarshal(fixture, fds)
req := new(plugin_go.CodeGeneratorRequest)
req.ProtoFile = fds.GetFile()
req.FileToGenerate = append(req.FileToGenerate, fds.GetFile()[0].GetName())
plugin := &plugin{out: &bytes.Buffer{}}
res, _ := plugin.Generate(req)
content := res.GetFile()[0].GetContent()
if !strings.Contains(content, "foozles are the best") {
t.Errorf("Expected generated schema to include type comments, but got %s", content)
}
}
func Test_CanAddEnumComments(t *testing.T) {
fixture, _ := os.ReadFile("./fixtures/money.pb")
fds := new(descriptor.FileDescriptorSet)
proto.Unmarshal(fixture, fds)
req := new(plugin_go.CodeGeneratorRequest)
req.ProtoFile = fds.GetFile()
req.FileToGenerate = append(req.FileToGenerate, fds.GetFile()[0].GetName())
plugin := &plugin{out: &bytes.Buffer{}}
res, _ := plugin.Generate(req)
content := res.GetFile()[0].GetContent()
if !strings.Contains(content, "Put money in") {
t.Errorf("Expected generated schema to include enum comments, but got %s", content)
}
}
func Test_FieldDeprecated(t *testing.T) {
fixture, _ := os.ReadFile("./fixtures/money.pb")
fds := new(descriptor.FileDescriptorSet)
proto.Unmarshal(fixture, fds)
req := new(plugin_go.CodeGeneratorRequest)
req.ProtoFile = fds.GetFile()
req.FileToGenerate = append(req.FileToGenerate, fds.GetFile()[0].GetName())
plugin := &plugin{out: &bytes.Buffer{}}
res, _ := plugin.Generate(req)
content := res.GetFile()[0].GetContent()
if !strings.Contains(content, "foobar: String @deprecated") {
t.Errorf("Expected generated schema to include deprecation descriptors, but got %s", content)
}
}
func Test_EnumValueDeprecated(t *testing.T) {
fixture, _ := os.ReadFile("./fixtures/money.pb")
fds := new(descriptor.FileDescriptorSet)
proto.Unmarshal(fixture, fds)
req := new(plugin_go.CodeGeneratorRequest)
req.ProtoFile = fds.GetFile()
req.FileToGenerate = append(req.FileToGenerate, fds.GetFile()[0].GetName())
plugin := &plugin{out: &bytes.Buffer{}}
res, _ := plugin.Generate(req)
content := res.GetFile()[0].GetContent()
if !strings.Contains(content, "BARTER @deprecated") {
t.Errorf("Expected generated schema to include an enum deprecation descriptor, but got %s", content)
}
}
func Test_InputDeprecated(t *testing.T) {
fixture, _ := os.ReadFile("./fixtures/money.pb")
fds := new(descriptor.FileDescriptorSet)
proto.Unmarshal(fixture, fds)
req := new(plugin_go.CodeGeneratorRequest)
req.ProtoFile = fds.GetFile()
req.FileToGenerate = append(req.FileToGenerate, fds.GetFile()[0].GetName())
plugin := &plugin{out: &bytes.Buffer{}}
res, _ := plugin.Generate(req)
content := res.GetFile()[0].GetContent()
if strings.Contains(content, "yeOldMoneyBox: Input_core_apimessages_OldMoneyBox @deprecated") {
t.Errorf("Expected generated schema to exclude input deprecation, but got %s", content)
}
}