-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_test.go
More file actions
262 lines (229 loc) · 6.81 KB
/
array_test.go
File metadata and controls
262 lines (229 loc) · 6.81 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2026 The Gopherly Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package synthra
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// helpers to build test fixtures
func envSlice(envs ...map[string]any) []any {
out := make([]any, 0, len(envs))
for _, e := range envs {
out = append(out, e)
}
return out
}
func mixedSlice() []any {
return []any{
map[string]any{"name": "first"},
"not-a-map",
42,
map[string]any{"name": "last"},
}
}
func TestSliceLen_MissingPath(t *testing.T) {
c := &Configuration{m: map[string]any{}}
assert.Equal(t, 0, c.SliceLen("environments"))
}
func TestSliceLen_NonSlice(t *testing.T) {
c := &Configuration{m: map[string]any{"environments": "not-a-slice"}}
assert.Equal(t, 0, c.SliceLen("environments"))
}
func TestSliceLen_EmptySlice(t *testing.T) {
c := &Configuration{m: map[string]any{"environments": []any{}}}
assert.Equal(t, 0, c.SliceLen("environments"))
}
func TestSliceLen_NonEmpty(t *testing.T) {
c := &Configuration{m: map[string]any{
"environments": envSlice(
map[string]any{"name": "dev"},
map[string]any{"name": "prod"},
),
}}
assert.Equal(t, 2, c.SliceLen("environments"))
}
func TestSliceLen_NilReceiver(t *testing.T) {
var c *Configuration
assert.Equal(t, 0, c.SliceLen("environments"))
}
func TestEachMap_MissingPath(t *testing.T) {
c := &Configuration{m: map[string]any{}}
var count int
for range c.EachMap("environments") {
count++
}
assert.Equal(t, 0, count)
}
func TestEachMap_NonSlice(t *testing.T) {
c := &Configuration{m: map[string]any{"environments": "scalar"}}
var count int
for range c.EachMap("environments") {
count++
}
assert.Equal(t, 0, count)
}
func TestEachMap_EmptySlice(t *testing.T) {
c := &Configuration{m: map[string]any{"environments": []any{}}}
var count int
for range c.EachMap("environments") {
count++
}
assert.Equal(t, 0, count)
}
func TestEachMap_AllMapElements(t *testing.T) {
c := &Configuration{m: map[string]any{
"environments": envSlice(
map[string]any{"name": "dev"},
map[string]any{"name": "prod"},
),
}}
var names []string
for _, e := range c.EachMap("environments") {
names = append(names, e.StringOr("name", ""))
}
assert.Equal(t, []string{"dev", "prod"}, names)
}
func TestEachMap_MixedTypeSlice_SkipsNonMaps(t *testing.T) {
c := &Configuration{m: map[string]any{"items": mixedSlice()}}
var names []string
for _, e := range c.EachMap("items") {
names = append(names, e.StringOr("name", ""))
}
// only map elements yielded
assert.Equal(t, []string{"first", "last"}, names)
}
func TestEachMap_NilReceiver(t *testing.T) {
var c *Configuration
var count int
for range c.EachMap("environments") {
count++
}
assert.Equal(t, 0, count)
}
func TestFind_MissingPath(t *testing.T) {
c := &Configuration{m: map[string]any{}}
assert.Nil(t, c.Find("environments", "name", "prod"))
}
func TestFind_NoMatch(t *testing.T) {
c := &Configuration{m: map[string]any{
"environments": envSlice(map[string]any{"name": "dev"}),
}}
assert.Nil(t, c.Find("environments", "name", "prod"))
}
func TestFind_Match(t *testing.T) {
c := &Configuration{m: map[string]any{
"environments": envSlice(
map[string]any{"name": "dev", "port": 8080},
map[string]any{"name": "prod", "port": 443},
),
}}
got := c.Find("environments", "name", "prod")
require.NotNil(t, got)
assert.Equal(t, 443, got.IntOr("port", 0))
}
func TestFind_CaseInsensitiveField(t *testing.T) {
c := &Configuration{m: map[string]any{
"environments": envSlice(map[string]any{"Name": "Prod"}),
}}
got := c.Find("environments", "name", "Prod")
require.NotNil(t, got)
assert.Equal(t, "Prod", got.StringOr("Name", ""))
}
func TestFind_NilReceiver(t *testing.T) {
var c *Configuration
assert.Nil(t, c.Find("environments", "name", "prod"))
}
func TestFindFunc_PredicateShortCircuit(t *testing.T) {
visited := 0
c := &Configuration{m: map[string]any{
"environments": envSlice(
map[string]any{"name": "dev"},
map[string]any{"name": "staging"},
map[string]any{"name": "prod"},
),
}}
got := c.FindFunc("environments", func(e *Configuration) bool {
visited++
return e.StringOr("name", "") == "staging"
})
require.NotNil(t, got)
// should stop after visiting dev and staging (2 visits)
assert.Equal(t, 2, visited)
assert.Equal(t, "staging", got.StringOr("name", ""))
}
func TestFindFunc_NilReceiver(t *testing.T) {
var c *Configuration
got := c.FindFunc("environments", func(_ *Configuration) bool { return true })
assert.Nil(t, got)
}
func TestConfigurableEachMap_YieldsMutableWrappers(t *testing.T) {
shared := []any{
map[string]any{"name": "dev", "port": 8080},
}
c := newConfigurable(map[string]any{"environments": shared})
for _, e := range c.EachMap("environments") {
require.NoError(t, e.Set("port", 9090))
}
// mutation reaches back into parent's backing slice
slice, ok := c.m["environments"].([]any)
require.True(t, ok)
entry, ok := slice[0].(map[string]any)
require.True(t, ok)
assert.Equal(t, 9090, entry["port"])
}
func TestConfigurableEachMap_MixedTypeSlice_SkipsNonMaps(t *testing.T) {
c := newConfigurable(map[string]any{"items": mixedSlice()})
var names []string
for _, e := range c.EachMap("items") {
names = append(names, e.StringOr("name", ""))
}
assert.Equal(t, []string{"first", "last"}, names)
}
func TestConfigurableFind_MutationVisible(t *testing.T) {
parentMap := map[string]any{
"environments": []any{
map[string]any{"name": "prod", "port": 443},
},
}
c := newConfigurable(parentMap)
env := c.Find("environments", "name", "prod")
require.NotNil(t, env)
require.NoError(t, env.Set("port", 8443))
// mutation is reflected in the parent's backing map
slice, ok := parentMap["environments"].([]any)
require.True(t, ok)
entry, ok := slice[0].(map[string]any)
require.True(t, ok)
assert.Equal(t, 8443, entry["port"])
}
func TestConfigurableFind_NilReceiver(t *testing.T) {
var c *Configurable
assert.Nil(t, c.Find("environments", "name", "prod"))
}
func TestConfigurableFindFunc_PredicateShortCircuit(t *testing.T) {
visited := 0
c := newConfigurable(map[string]any{
"environments": envSlice(
map[string]any{"name": "dev"},
map[string]any{"name": "prod"},
),
})
got := c.FindFunc("environments", func(e *Configurable) bool {
visited++
return e.StringOr("name", "") == "dev"
})
require.NotNil(t, got)
assert.Equal(t, 1, visited)
}