forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_test.go
193 lines (151 loc) · 3.9 KB
/
method_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package pgs
import (
"testing"
"errors"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/stretchr/testify/assert"
)
func TestMethod_Name(t *testing.T) {
t.Parallel()
m := &method{desc: &descriptor.MethodDescriptorProto{Name: proto.String("foo")}}
assert.Equal(t, "foo", m.Name().String())
}
func TestMethod_FullyQualifiedName(t *testing.T) {
t.Parallel()
m := &method{fqn: "fizz"}
assert.Equal(t, m.fqn, m.FullyQualifiedName())
}
func TestMethod_Syntax(t *testing.T) {
t.Parallel()
m := &method{}
s := dummyService()
s.addMethod(m)
assert.Equal(t, s.Syntax(), m.Syntax())
}
func TestMethod_Package(t *testing.T) {
t.Parallel()
m := &method{}
s := dummyService()
s.addMethod(m)
assert.NotNil(t, m.Package())
assert.Equal(t, s.Package(), m.Package())
}
func TestMethod_File(t *testing.T) {
t.Parallel()
m := &method{}
s := dummyService()
s.addMethod(m)
assert.NotNil(t, m.File())
assert.Equal(t, s.File(), m.File())
}
func TestMethod_BuildTarget(t *testing.T) {
t.Parallel()
m := &method{}
s := dummyService()
s.addMethod(m)
assert.False(t, m.BuildTarget())
s.setFile(&file{buildTarget: true})
assert.True(t, m.BuildTarget())
}
func TestMethod_Descriptor(t *testing.T) {
t.Parallel()
m := &method{desc: &descriptor.MethodDescriptorProto{}}
assert.Equal(t, m.desc, m.Descriptor())
}
func TestMethod_Service(t *testing.T) {
t.Parallel()
m := &method{}
s := dummyService()
s.addMethod(m)
assert.Equal(t, s, m.Service())
}
func TestMethod_Input(t *testing.T) {
t.Parallel()
m := &method{in: &msg{}}
assert.Equal(t, m.in, m.Input())
}
func TestMethod_Output(t *testing.T) {
t.Parallel()
m := &method{out: &msg{}}
assert.Equal(t, m.out, m.Output())
}
func TestMethod_ClientStreaming(t *testing.T) {
t.Parallel()
m := &method{desc: &descriptor.MethodDescriptorProto{}}
assert.False(t, m.ClientStreaming())
m.desc.ClientStreaming = proto.Bool(true)
assert.True(t, m.ClientStreaming())
}
func TestMethod_ServerStreaming(t *testing.T) {
t.Parallel()
m := &method{desc: &descriptor.MethodDescriptorProto{}}
assert.False(t, m.ServerStreaming())
m.desc.ServerStreaming = proto.Bool(true)
assert.True(t, m.ServerStreaming())
}
func TestMethod_BiDirStreaming(t *testing.T) {
t.Parallel()
m := &method{desc: &descriptor.MethodDescriptorProto{}}
assert.False(t, m.BiDirStreaming())
m.desc.ServerStreaming = proto.Bool(true)
assert.False(t, m.BiDirStreaming())
m.desc.ServerStreaming = proto.Bool(false)
m.desc.ClientStreaming = proto.Bool(true)
assert.False(t, m.BiDirStreaming())
m.desc.ServerStreaming = proto.Bool(true)
assert.True(t, m.BiDirStreaming())
}
func TestMethod_Imports(t *testing.T) {
t.Parallel()
s := dummyService()
m := &method{
in: dummyMsg(),
out: dummyMsg(),
}
s.addMethod(m)
f := &file{desc: &descriptor.FileDescriptorProto{
Name: proto.String("foobar"),
}}
assert.Empty(t, m.Imports())
m.in = &msg{parent: f}
assert.Len(t, m.Imports(), 1)
m.out = &msg{parent: &file{}}
assert.Len(t, m.Imports(), 2)
m.out = &msg{parent: f}
assert.Len(t, m.Imports(), 1)
}
func TestMethod_Extension(t *testing.T) {
// cannot be parallel
m := &method{desc: &descriptor.MethodDescriptorProto{}}
assert.NotPanics(t, func() { m.Extension(nil, nil) })
}
func TestMethod_Accept(t *testing.T) {
t.Parallel()
m := &method{}
assert.Nil(t, m.accept(nil))
v := &mockVisitor{err: errors.New("foo")}
assert.Error(t, m.accept(v))
assert.Equal(t, 1, v.method)
}
func TestMethod_ChildAtPath(t *testing.T) {
t.Parallel()
m := &method{}
assert.Equal(t, m, m.childAtPath(nil))
assert.Nil(t, m.childAtPath([]int32{1}))
}
type mockMethod struct {
Method
i []File
s Service
err error
}
func (m *mockMethod) Imports() []File { return m.i }
func (m *mockMethod) setService(s Service) { m.s = s }
func (m *mockMethod) accept(v Visitor) error {
_, err := v.VisitMethod(m)
if m.err != nil {
return m.err
}
return err
}