1
|
|
|
package validation_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"strings" |
5
|
|
|
"testing" |
6
|
|
|
|
7
|
|
|
"github.com/muonsoft/validation" |
8
|
|
|
"github.com/stretchr/testify/assert" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
func TestPropertyPath_String(t *testing.T) { |
12
|
|
|
tests := []struct { |
13
|
|
|
path *validation.PropertyPath |
14
|
|
|
want string |
15
|
|
|
}{ |
16
|
|
|
{path: nil, want: ""}, |
17
|
|
|
{path: validation.NewPropertyPath(), want: ""}, |
18
|
|
|
{ |
19
|
|
|
path: validation.NewPropertyPath().WithProperty("array").WithIndex(1).WithProperty("property"), |
20
|
|
|
want: "array[1].property", |
21
|
|
|
}, |
22
|
|
|
{ |
23
|
|
|
path: validation.NewPropertyPath().WithProperty("@foo").WithProperty("bar"), |
24
|
|
|
want: "['@foo'].bar", |
25
|
|
|
}, |
26
|
|
|
{ |
27
|
|
|
path: validation.NewPropertyPath().WithProperty("@foo").WithIndex(0), |
28
|
|
|
want: "['@foo'][0]", |
29
|
|
|
}, |
30
|
|
|
{ |
31
|
|
|
path: validation.NewPropertyPath().WithProperty("foo.bar").WithProperty("baz"), |
32
|
|
|
want: "['foo.bar'].baz", |
33
|
|
|
}, |
34
|
|
|
{ |
35
|
|
|
path: validation.NewPropertyPath().WithProperty("foo.'bar'").WithProperty("baz"), |
36
|
|
|
want: `['foo.\'bar\''].baz`, |
37
|
|
|
}, |
38
|
|
|
{ |
39
|
|
|
path: validation.NewPropertyPath().WithProperty(`0`).WithProperty("baz"), |
40
|
|
|
want: `['0'].baz`, |
41
|
|
|
}, |
42
|
|
|
{ |
43
|
|
|
path: validation.NewPropertyPath().WithProperty(`foo[0]`).WithProperty("baz"), |
44
|
|
|
want: `['foo[0]'].baz`, |
45
|
|
|
}, |
46
|
|
|
{ |
47
|
|
|
path: validation.NewPropertyPath().WithProperty(``).WithProperty("baz"), |
48
|
|
|
want: `[''].baz`, |
49
|
|
|
}, |
50
|
|
|
{ |
51
|
|
|
path: validation.NewPropertyPath().WithProperty("foo").WithProperty(""), |
52
|
|
|
want: `foo['']`, |
53
|
|
|
}, |
54
|
|
|
{ |
55
|
|
|
path: validation.NewPropertyPath().WithProperty(`'`).WithProperty("baz"), |
56
|
|
|
want: `['\''].baz`, |
57
|
|
|
}, |
58
|
|
|
{ |
59
|
|
|
path: validation.NewPropertyPath().WithProperty(`\`).WithProperty("baz"), |
60
|
|
|
want: `['\\'].baz`, |
61
|
|
|
}, |
62
|
|
|
{ |
63
|
|
|
path: validation.NewPropertyPath().WithProperty(`\'foo`).WithProperty("baz"), |
64
|
|
|
want: `['\\\'foo'].baz`, |
65
|
|
|
}, |
66
|
|
|
{ |
67
|
|
|
path: validation.NewPropertyPath().WithProperty(`фу`).WithProperty("baz"), |
68
|
|
|
want: `фу.baz`, |
69
|
|
|
}, |
70
|
|
|
} |
71
|
|
|
for _, test := range tests { |
72
|
|
|
t.Run(test.want, func(t *testing.T) { |
73
|
|
|
got := test.path.String() |
74
|
|
|
|
75
|
|
|
assert.Equal(t, test.want, got) |
76
|
|
|
}) |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
func TestPropertyPath_With(t *testing.T) { |
81
|
|
|
path := validation.NewPropertyPath(validation.PropertyName("top"), validation.ArrayIndex(0)) |
82
|
|
|
|
83
|
|
|
path = path.With( |
84
|
|
|
validation.PropertyName("low"), |
85
|
|
|
validation.ArrayIndex(1), |
86
|
|
|
validation.PropertyName("property"), |
87
|
|
|
) |
88
|
|
|
|
89
|
|
|
assert.Equal(t, "top[0].low[1].property", path.String()) |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
func BenchmarkPropertyPath_String(b *testing.B) { |
93
|
|
|
// cpu: Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz |
94
|
|
|
// BenchmarkPropertyPath_String |
95
|
|
|
// BenchmarkPropertyPath_String-16 217718 7428 ns/op 4384 B/op 5 allocs/op |
96
|
|
|
path := validation.NewPropertyPath( |
97
|
|
|
validation.PropertyName("array"), |
98
|
|
|
validation.ArrayIndex(1234567890), |
99
|
|
|
validation.PropertyName("@foo"), |
100
|
|
|
validation.ArrayIndex(1234567890), |
101
|
|
|
validation.PropertyName("foo.bar"), |
102
|
|
|
validation.PropertyName("foo.'bar'"), |
103
|
|
|
validation.PropertyName(`0123456789`), |
104
|
|
|
validation.PropertyName(`foo[0][1][2][3][4][5][6][7][8][9]`), |
105
|
|
|
validation.PropertyName(``), |
106
|
|
|
validation.PropertyName(`'`), |
107
|
|
|
validation.PropertyName(`\`), |
108
|
|
|
validation.PropertyName(`\'foo`), |
109
|
|
|
validation.PropertyName(`фу`), |
110
|
|
|
validation.PropertyName(strings.Repeat(`@foo.'bar'.[baz]`, 100)), |
111
|
|
|
) |
112
|
|
|
|
113
|
|
|
b.ReportAllocs() |
114
|
|
|
b.ResetTimer() |
115
|
|
|
for i := 0; i < b.N; i++ { |
116
|
|
|
_ = path.String() |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|