-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_test.go
71 lines (67 loc) · 1.52 KB
/
metadata_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
package jsonpb
import (
"testing"
)
func TestMessage_noIndex(t *testing.T) {
m := &Message{
Fields: []Field{
{Name: "a", Tag: 1},
{Name: "b", Tag: 10},
{Name: "c", Tag: 11},
{Name: "d", Tag: 20},
},
}
type args struct {
tag uint32
name string
}
tests := []struct {
name string
args args
}{
{name: "a", args: args{tag: 1, name: "a"}},
{name: "b", args: args{tag: 10, name: "b"}},
{name: "c", args: args{tag: 11, name: "c"}},
{name: "d", args: args{tag: 20, name: "d"}},
{name: "not_found", args: args{tag: 12, name: "e"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if byTag, byName := m.FieldByTag(tt.args.tag), m.FieldByName(tt.args.name); byTag != byName {
t.Errorf("byTag = %p, byName = %p", byTag, byName)
}
})
}
}
func TestMessage_sparseTagIndex(t *testing.T) {
m := &Message{
Fields: []Field{
{Name: "a", Tag: 1},
{Name: "b", Tag: 10},
{Name: "c", Tag: 11},
{Name: "d", Tag: 20},
},
}
m.BakeTagIndex()
type args struct {
tag uint32
}
tests := []struct {
name string
args args
want int
}{
{name: "a", args: args{tag: 1}, want: 0},
{name: "b", args: args{tag: 10}, want: 1},
{name: "c", args: args{tag: 11}, want: 2},
{name: "d", args: args{tag: 20}, want: 3},
{name: "not_found", args: args{tag: 12}, want: -1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := m.FieldIndexByTag(tt.args.tag); got != tt.want {
t.Errorf("Message.FieldIndexByTag() = %v, want %v", got, tt.want)
}
})
}
}