-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappend_test.go
More file actions
84 lines (77 loc) · 2.32 KB
/
append_test.go
File metadata and controls
84 lines (77 loc) · 2.32 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package protopatch_test
import (
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/daishe/protopatch"
"github.com/daishe/protopatch/internal/patchtest"
protopatchv1 "github.com/daishe/protopatch/internal/testtypes/protopatch/v1"
)
func TestAppend(t *testing.T) {
t.Parallel()
tests := []struct {
name string
base proto.Message
path string
value any
opts []protopatch.Option
want proto.Message
wantErr error
}{
{
name: "scalar-list-nil/append",
base: &protopatchv1.TestList{String_: []string(nil)},
path: "string",
value: "bbb",
want: &protopatchv1.TestList{String_: []string{"bbb"}},
},
{
name: "scalar-list-empty/append",
base: &protopatchv1.TestList{String_: []string{}},
path: "string",
value: "bbb",
want: &protopatchv1.TestList{String_: []string{"bbb"}},
},
{
name: "scalar-list/append",
base: &protopatchv1.TestList{String_: []string{"aaa"}},
path: "string",
value: "bbb",
want: &protopatchv1.TestList{String_: []string{"aaa", "bbb"}},
},
{
name: "message-list-nil/append",
base: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage(nil)},
path: "message",
value: &protopatchv1.TestMessage{String_: "bbb"},
want: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "bbb"}}},
},
{
name: "message-list-empty/append",
base: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{}},
path: "message",
value: &protopatchv1.TestMessage{String_: "bbb"},
want: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "bbb"}}},
},
{
name: "message-list/append",
base: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "aaa"}}},
path: "message",
value: &protopatchv1.TestMessage{String_: "bbb"},
want: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "aaa"}, {String_: "bbb"}}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
base := proto.Clone(test.base)
err := protopatch.Append(base, test.path, test.value, test.opts...)
if test.wantErr != nil {
require.Equal(t, test.wantErr, err)
return
}
require.NoError(t, err)
patchtest.RequireEqual(t, test.want, base, "append value mismatch")
})
}
}