-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_test.go
More file actions
61 lines (57 loc) · 1.33 KB
/
split_test.go
File metadata and controls
61 lines (57 loc) · 1.33 KB
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
package protoparts
import (
"fmt"
"testing"
"github.com/obeattie/protoparts/internal/testproto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/dynamicpb"
)
func TestSplit(t *testing.T) {
md := (&testproto.Person{}).ProtoReflect().Descriptor()
type tc struct {
msg *dynamicpb.Message
expectedPaths []Path
}
cases := []tc{
{
// Empty message
testMsg(t, nil, nil, nil, nil, nil, nil),
[]Path{},
},
{
// Simple, one field populated
testMsg(t, p("Oliver Beattie"), nil, nil, nil, nil, nil),
[]Path{
DecodeSymbolicPath("name", md)},
},
{
// An optional field with empty contents is still present
testMsg(t, p(""), nil, nil, nil, nil, nil),
[]Path{
DecodeSymbolicPath("name", md)},
},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
parts := split(t, c.msg)
var seenPaths []Path
for _, part := range parts {
seenPaths = append(seenPaths, part.Path)
}
assert.ElementsMatch(t, c.expectedPaths, seenPaths)
})
}
}
func BenchmarkSplit(b *testing.B) {
msg := benchmarkMsg(b)
md := msg.Descriptor()
pb, err := proto.Marshal(msg)
require.NoError(b, err)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Split(pb, md)
}
}