-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfield_test.go
58 lines (51 loc) · 1.28 KB
/
field_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
package protobuf
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type TestNestedOuter struct {
A int32
*TestNestedInner
}
type TestNestedInner struct {
A int32
B int32 `protobuf:"10"`
C int32 `protobuf:"renamed"`
}
func TestEncodeNested(t *testing.T) {
s := &TestNestedOuter{
A: 13,
TestNestedInner: &TestNestedInner{
A: 12,
B: 14,
C: 15,
},
}
v := reflect.ValueOf(s).Elem()
actual := ProtoFields(v.Type())
for _, f := range actual {
f.Field = reflect.StructField{}
}
expected := []*ProtoField{
{1, TagNone, "", []int{0}, reflect.StructField{}},
{2, TagNone, "", []int{1, 0}, reflect.StructField{}},
{10, TagNone, "", []int{1, 1}, reflect.StructField{}},
{11, TagNone, "renamed", []int{1, 2}, reflect.StructField{}},
}
assert.Equal(t, expected, actual)
assert.Equal(t, v.FieldByIndex(actual[0].Index).Int(), int64(13))
assert.Equal(t, v.FieldByIndex(actual[1].Index).Int(), int64(12))
assert.Equal(t, v.FieldByIndex(actual[2].Index).Int(), int64(14))
assert.Equal(t, v.FieldByIndex(actual[3].Index).Int(), int64(15))
}
type TestDuplicateID struct {
A int32 `protobuf:"1"`
B int32 `protobuf:"1"`
}
func TestDuplicateIDNotAllowed(t *testing.T) {
assert.Panics(t, func() {
v := reflect.TypeOf(&TestDuplicateID{})
ProtoFields(v)
})
}