-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_test.go
More file actions
125 lines (112 loc) · 5.93 KB
/
copy_test.go
File metadata and controls
125 lines (112 loc) · 5.93 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
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
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 TestCopy(t *testing.T) {
t.Parallel()
tests := []struct {
name string
base proto.Message
targetPath string
replacementPath string
opts []protopatch.Option
want proto.Message
wantErr error
}{
{
name: "scalar/from/message/scalar",
base: &protopatchv1.TestMessage{String_: "aaa", Message: &protopatchv1.TestMessage{String_: "bbb"}},
targetPath: "string",
replacementPath: "message.string",
want: &protopatchv1.TestMessage{String_: "bbb", Message: &protopatchv1.TestMessage{String_: "bbb"}},
},
{
name: "scalar-list/from/message/scalar-list",
base: &protopatchv1.TestMessage{List: &protopatchv1.TestList{String_: []string{"aaa"}}, Message: &protopatchv1.TestMessage{List: &protopatchv1.TestList{String_: []string{"bbb"}}}},
targetPath: "list.string",
replacementPath: "message.list.string",
want: &protopatchv1.TestMessage{List: &protopatchv1.TestList{String_: []string{"bbb"}}, Message: &protopatchv1.TestMessage{List: &protopatchv1.TestList{String_: []string{"bbb"}}}},
},
{
name: "scalar-list/item/from/scalar-list/item",
base: &protopatchv1.TestList{String_: []string{"aaa", "bbb"}},
targetPath: "string.0",
replacementPath: "string.1",
want: &protopatchv1.TestList{String_: []string{"bbb", "bbb"}},
},
{
name: "scalar-map/from/message/scalar-map",
base: &protopatchv1.TestMessage{Map: &protopatchv1.TestMap{StringToString: map[string]string{"key": "aaa"}}, Message: &protopatchv1.TestMessage{Map: &protopatchv1.TestMap{StringToString: map[string]string{"key": "bbb"}}}},
targetPath: "map.stringToString",
replacementPath: "message.map.stringToString",
want: &protopatchv1.TestMessage{Map: &protopatchv1.TestMap{StringToString: map[string]string{"key": "bbb"}}, Message: &protopatchv1.TestMessage{Map: &protopatchv1.TestMap{StringToString: map[string]string{"key": "bbb"}}}},
},
{
name: "scalar-map/item/from/scalar-map/item",
base: &protopatchv1.TestMap{StringToString: map[string]string{"key0": "aaa", "key1": "bbb"}},
targetPath: "stringToString.key0",
replacementPath: "stringToString.key1",
want: &protopatchv1.TestMap{StringToString: map[string]string{"key0": "bbb", "key1": "bbb"}},
},
{
name: "base/from/message",
base: &protopatchv1.TestMessage{String_: "aaa", Message: &protopatchv1.TestMessage{String_: "bbb"}},
targetPath: "",
replacementPath: "message",
want: &protopatchv1.TestMessage{String_: "bbb"},
},
{
name: "message/from/message/message",
base: &protopatchv1.TestMessage{Message: &protopatchv1.TestMessage{String_: "aaa", Message: &protopatchv1.TestMessage{String_: "bbb"}}},
targetPath: "message",
replacementPath: "message.message",
want: &protopatchv1.TestMessage{Message: &protopatchv1.TestMessage{String_: "bbb"}},
},
{
name: "message-list/from/message/message-list",
base: &protopatchv1.TestMessage{List: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "aaa"}}}, Message: &protopatchv1.TestMessage{List: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "bbb"}}}}},
targetPath: "list.message",
replacementPath: "message.list.message",
want: &protopatchv1.TestMessage{List: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "bbb"}}}, Message: &protopatchv1.TestMessage{List: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "bbb"}}}}},
},
{
name: "message-list/item/from/message-list/item",
base: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "aaa"}, {String_: "bbb"}}},
targetPath: "message.0",
replacementPath: "message.1",
want: &protopatchv1.TestList{Message: []*protopatchv1.TestMessage{{String_: "bbb"}, {String_: "bbb"}}},
},
{
name: "message-map/from/message/message-map",
base: &protopatchv1.TestMessage{Map: &protopatchv1.TestMap{StringToMessage: map[string]*protopatchv1.TestMessage{"key": {String_: "aaa"}}}, Message: &protopatchv1.TestMessage{Map: &protopatchv1.TestMap{StringToMessage: map[string]*protopatchv1.TestMessage{"key": {String_: "bbb"}}}}},
targetPath: "map.stringToMessage",
replacementPath: "message.map.stringToMessage",
want: &protopatchv1.TestMessage{Map: &protopatchv1.TestMap{StringToMessage: map[string]*protopatchv1.TestMessage{"key": {String_: "bbb"}}}, Message: &protopatchv1.TestMessage{Map: &protopatchv1.TestMap{StringToMessage: map[string]*protopatchv1.TestMessage{"key": {String_: "bbb"}}}}},
},
{
name: "message-map/item/from/message-map/item",
base: &protopatchv1.TestMap{StringToMessage: map[string]*protopatchv1.TestMessage{"key0": {String_: "aaa"}, "key1": {String_: "bbb"}}},
targetPath: "stringToMessage.key0",
replacementPath: "stringToMessage.key1",
want: &protopatchv1.TestMap{StringToMessage: map[string]*protopatchv1.TestMessage{"key0": {String_: "bbb"}, "key1": {String_: "bbb"}}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
base := proto.Clone(test.base)
err := protopatch.Copy(base, test.targetPath, test.replacementPath, test.opts...)
if test.wantErr != nil {
require.Equal(t, test.wantErr, err)
return
}
require.NoError(t, err)
patchtest.RequireEqual(t, test.want, base, "copy value mismatch")
})
}
}