-
Notifications
You must be signed in to change notification settings - Fork 77
/
enum_test.go
218 lines (166 loc) · 3.89 KB
/
enum_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package pgs
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)
func TestEnum_Name(t *testing.T) {
t.Parallel()
e := &enum{desc: &descriptor.EnumDescriptorProto{Name: proto.String("foo")}}
assert.Equal(t, "foo", e.Name().String())
}
func TestEnum_FullyQualifiedName(t *testing.T) {
t.Parallel()
e := &enum{fqn: "enum"}
assert.Equal(t, e.fqn, e.FullyQualifiedName())
}
func TestEnum_Syntax(t *testing.T) {
t.Parallel()
e := &enum{}
f := dummyFile()
f.addEnum(e)
assert.Equal(t, f.Syntax(), e.Syntax())
}
func TestEnum_Package(t *testing.T) {
t.Parallel()
e := &enum{}
f := dummyFile()
f.addEnum(e)
assert.NotNil(t, e.Package())
assert.Equal(t, f.Package(), e.Package())
}
func TestEnum_File(t *testing.T) {
t.Parallel()
e := &enum{}
m := dummyMsg()
m.addEnum(e)
assert.NotNil(t, e.File())
assert.Equal(t, m.File(), e.File())
}
func TestEnum_BuildTarget(t *testing.T) {
t.Parallel()
e := &enum{}
f := dummyFile()
f.addEnum(e)
assert.False(t, e.BuildTarget())
f.buildTarget = true
assert.True(t, e.BuildTarget())
}
func TestEnum_Descriptor(t *testing.T) {
t.Parallel()
e := &enum{desc: &descriptor.EnumDescriptorProto{}}
assert.Equal(t, e.desc, e.Descriptor())
}
func TestEnum_Parent(t *testing.T) {
t.Parallel()
e := &enum{}
f := dummyFile()
f.addEnum(e)
assert.Equal(t, f, e.Parent())
}
func TestEnum_Imports(t *testing.T) {
t.Parallel()
assert.Nil(t, (&enum{}).Imports())
}
func TestEnum_Values(t *testing.T) {
t.Parallel()
e := &enum{}
assert.Empty(t, e.Values())
e.addValue(&enumVal{})
assert.Len(t, e.Values(), 1)
}
func TestEnum_Dependents(t *testing.T) {
t.Parallel()
t.Run("enum in file", func(t *testing.T) {
t.Parallel()
e := &enum{}
f := dummyFile()
f.addEnum(e)
assert.Empty(t, e.Dependents())
})
t.Run("enum in message", func(t *testing.T) {
t.Parallel()
e := &enum{}
m := dummyMsg()
m.addEnum(e)
assert.Empty(t, e.Dependents())
})
t.Run("external dependents", func(t *testing.T) {
t.Parallel()
pkg := dummyPkg()
f := &file{
pkg: pkg,
desc: &descriptor.FileDescriptorProto{
Package: proto.String(pkg.ProtoName().String()),
Syntax: proto.String(string(Proto3)),
Name: proto.String("test_file.proto"),
},
}
e := &enum{}
e.fqn = fullyQualifiedName(f, e)
m := dummyMsg()
m.fqn = fullyQualifiedName(f, m)
e.addDependent(m)
deps := e.Dependents()
assert.Len(t, deps, 1)
assert.Contains(t, deps, m)
})
}
func TestEnum_Extension(t *testing.T) {
// cannot be parallel
e := &enum{desc: &descriptor.EnumDescriptorProto{}}
assert.NotPanics(t, func() { e.Extension(nil, nil) })
}
func TestEnum_Accept(t *testing.T) {
t.Parallel()
e := &enum{}
e.addValue(&enumVal{})
assert.NoError(t, e.accept(nil))
v := &mockVisitor{}
assert.NoError(t, e.accept(v))
assert.Equal(t, 1, v.enum)
assert.Zero(t, v.enumvalue)
v.Reset()
v.err = errors.New("")
v.v = v
assert.Error(t, e.accept(v))
assert.Equal(t, 1, v.enum)
assert.Zero(t, v.enumvalue)
v.Reset()
assert.NoError(t, e.accept(v))
assert.Equal(t, 1, v.enum)
assert.Equal(t, 1, v.enumvalue)
v.Reset()
e.addValue(&mockEnumValue{err: errors.New("")})
assert.Error(t, e.accept(v))
assert.Equal(t, 1, v.enum)
assert.Equal(t, 2, v.enumvalue)
}
func TestEnum_ChildAtPath(t *testing.T) {
t.Parallel()
e := &enum{}
assert.Equal(t, e, e.childAtPath(nil))
assert.Nil(t, e.childAtPath([]int32{1}))
assert.Nil(t, e.childAtPath([]int32{999, 123}))
}
type mockEnum struct {
Enum
p ParentEntity
err error
}
func (e *mockEnum) setParent(p ParentEntity) { e.p = p }
func (e *mockEnum) accept(v Visitor) error {
_, err := v.VisitEnum(e)
if e.err != nil {
return e.err
}
return err
}
func dummyEnum() *enum {
f := dummyFile()
e := &enum{desc: &descriptor.EnumDescriptorProto{Name: proto.String("enum")}}
f.addEnum(e)
return e
}