forked from mennanov/fieldmask-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mask_test.go
119 lines (112 loc) · 2.96 KB
/
mask_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
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
package fieldmask_utils_test
import (
"testing"
"github.com/gogo/protobuf/types"
"github.com/golang/protobuf/protoc-gen-go/generator"
"github.com/propertechnologies/fieldmask-utils"
"github.com/stretchr/testify/assert"
)
func TestMask_String(t *testing.T) {
mask := fieldmask_utils.MaskFromString("a{b{c}}")
assert.Equal(t, "a{b{c}}", mask.String())
}
func TestMaskFromProtoFieldMaskSuccess(t *testing.T) {
testCases := []struct {
mask *types.FieldMask
expectedTree string
}{
{
&types.FieldMask{Paths: []string{
"a", // overwritten by the paths below (a.*)
"a.b.c",
"a.b.d",
"a.c.d",
"b.c.d",
"a", // has no effect, since more strict rules are applied above
"c",
}},
"a{b{c,d},c{d}},b{c{d}},c",
},
{
&types.FieldMask{Paths: []string{"a", "b", "b", "a"}},
"a,b",
},
{
&types.FieldMask{Paths: []string{}},
"",
},
}
for _, testCase := range testCases {
mask, err := fieldmask_utils.MaskFromProtoFieldMask(testCase.mask, func(s string) string { return s })
assert.Nil(t, err)
assert.Equal(t, fieldmask_utils.MaskFromString(testCase.expectedTree), mask)
}
}
func TestMaskFromProtoFieldMask_Failure(t *testing.T) {
testCases := []*types.FieldMask{
{Paths: []string{"a", ".a"}},
{Paths: []string{"."}},
{Paths: []string{"a.b.c.d.e", "a.b."}},
}
for _, fieldMask := range testCases {
_, err := fieldmask_utils.MaskFromProtoFieldMask(fieldMask, generator.CamelCase)
assert.NotNil(t, err)
}
}
func TestMaskFromString(t *testing.T) {
testCases := []struct {
input string
expectedMask fieldmask_utils.Mask
length int
}{
{
"foo,bar{c{d,e{f,g,h}}}",
fieldmask_utils.Mask{
"foo": fieldmask_utils.Mask{},
"bar": fieldmask_utils.Mask{
"c": fieldmask_utils.Mask{
"d": fieldmask_utils.Mask{},
"e": fieldmask_utils.Mask{
"f": fieldmask_utils.Mask{},
"g": fieldmask_utils.Mask{},
"h": fieldmask_utils.Mask{},
},
},
},
}, 2,
},
{"foo, bar{c {d,e{f,\ng,h}}},t", fieldmask_utils.Mask{
"foo": fieldmask_utils.Mask{},
"bar": fieldmask_utils.Mask{
"c": fieldmask_utils.Mask{
"d": fieldmask_utils.Mask{},
"e": fieldmask_utils.Mask{
"f": fieldmask_utils.Mask{},
"g": fieldmask_utils.Mask{},
"h": fieldmask_utils.Mask{},
},
},
},
"t": fieldmask_utils.Mask{},
}, 3},
{"foo", fieldmask_utils.Mask{"foo": fieldmask_utils.Mask{}}, 1},
{"foo,bar", fieldmask_utils.Mask{
"foo": fieldmask_utils.Mask{},
"bar": fieldmask_utils.Mask{},
}, 2},
{"foo,bar{c},d,e", fieldmask_utils.Mask{
"foo": fieldmask_utils.Mask{},
"bar": fieldmask_utils.Mask{
"c": fieldmask_utils.Mask{},
},
"d": fieldmask_utils.Mask{},
"e": fieldmask_utils.Mask{},
}, 4},
{"", fieldmask_utils.Mask{}, 0},
}
for _, testCase := range testCases {
mask := fieldmask_utils.MaskFromString(testCase.input)
assert.Equal(t, testCase.expectedMask, mask)
assert.Equal(t, testCase.length, len(mask))
}
}