Skip to content

Commit 571bc78

Browse files
committed
Fix tests for PatternMap
1 parent 5e4e259 commit 571bc78

File tree

1 file changed

+118
-139
lines changed

1 file changed

+118
-139
lines changed

x/config/config_test.go

Lines changed: 118 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package config_test
22

33
import (
44
"fmt"
5-
"reflect"
65
"testing"
76

87
"github.com/flowdev/spaghetti-cutter/x/config"
@@ -170,145 +169,125 @@ func TestParse(t *testing.T) {
170169
}
171170
}
172171

173-
func TestPatternMap(t *testing.T) {
172+
func TestPatternMapHasKeyValue(t *testing.T) {
174173
specs := []struct {
175-
name string
176-
givenJSON string
177-
givenLeftPattern string
178-
expectedLeftMatch bool
179-
expectedRightString string
174+
name string
175+
givenJSON string
176+
givenKey string
177+
givenStrictKey string
178+
givenValue string
179+
givenStrictValue string
180+
expectedHasKey bool
181+
expectedHasValue bool
180182
}{
181183
{
182-
name: "simple-pair",
183-
givenJSON: `"a": ["b"]`,
184-
givenLeftPattern: "a",
185-
expectedLeftMatch: true,
186-
expectedRightString: "`b`",
184+
name: "simple-pair-full-match",
185+
givenJSON: `"a": ["b"]`,
186+
givenKey: "a",
187+
givenStrictKey: "",
188+
givenValue: "b",
189+
givenStrictValue: "",
190+
expectedHasKey: true,
191+
expectedHasValue: true,
187192
}, {
188-
name: "multiple-pairs",
189-
givenJSON: `"a": ["b", "c", "do", "foo"]`,
190-
givenLeftPattern: "a",
191-
expectedLeftMatch: true,
192-
expectedRightString: "`b`, `c`, `do`, `foo`",
193+
name: "simple-pair-half-match",
194+
givenJSON: `"a": ["b"]`,
195+
givenKey: "a",
196+
givenStrictKey: "",
197+
givenValue: "c",
198+
givenStrictValue: "",
199+
expectedHasKey: true,
200+
expectedHasValue: false,
193201
}, {
194-
name: "one-pair-many-stars",
195-
givenJSON: `"a/*/b/**": ["c/*/d/**"]`,
196-
givenLeftPattern: "a/foo/b/bar/doo",
197-
expectedLeftMatch: true,
198-
expectedRightString: "`c/*/d/**`",
202+
name: "simple-pair-no-match",
203+
givenJSON: `"a": ["b"]`,
204+
givenKey: "c",
205+
givenStrictKey: "",
206+
givenValue: "d",
207+
givenStrictValue: "",
208+
expectedHasKey: false,
209+
expectedHasValue: false,
199210
}, {
200-
name: "no-match",
201-
givenJSON: `"a/*/b/**": ["c"]`,
202-
givenLeftPattern: "a/ahoi/b",
203-
expectedLeftMatch: false,
204-
expectedRightString: "",
211+
name: "multiple-pairs-full-match",
212+
givenJSON: `"a": ["b", "c", "do", "foo"]`,
213+
givenKey: "",
214+
givenStrictKey: "a",
215+
givenValue: "do",
216+
givenStrictValue: "",
217+
expectedHasKey: true,
218+
expectedHasValue: true,
205219
}, {
206-
name: "dollars",
207-
givenJSON: `"a/$*/b/$**/c": ["d/$2/e/$1/f"]`,
208-
givenLeftPattern: "a/foo/b/bar/car/c",
209-
expectedLeftMatch: true,
210-
expectedRightString: "`d/$2/e/$1/f`",
220+
name: "one-pair-many-stars-full-match",
221+
givenJSON: `"a/*/b/**": ["c/*/d/**"]`,
222+
givenKey: "a/foo/b/bar/doo",
223+
givenStrictKey: "",
224+
givenValue: "c/fox/d/baz/dox",
225+
givenStrictValue: "",
226+
expectedHasKey: true,
227+
expectedHasValue: true,
211228
}, {
212-
name: "all-complexity",
213-
givenJSON: `"foo/bar/**": ["b"], "*/*a/**": ["*/*b/**", "b*/c*d/**"]`,
214-
givenLeftPattern: "foo/bara/doo/ey",
215-
expectedLeftMatch: true,
216-
expectedRightString: "`*/*b/**`, `b*/c*d/**`",
217-
},
218-
}
219-
220-
for _, spec := range specs {
221-
t.Run(spec.name, func(t *testing.T) {
222-
cfgBytes := []byte(`{ "allowOnlyIn": { ` + spec.givenJSON + ` } }`)
223-
cfg, err := config.Parse(cfgBytes, spec.name)
224-
if err != nil {
225-
t.Fatalf("got unexpected error: %v", err)
226-
}
227-
pm := cfg.AllowOnlyIn
228-
229-
pl, _ := pm.MatchingList(spec.givenLeftPattern)
230-
231-
if spec.expectedLeftMatch && pl == nil {
232-
t.Fatalf("expected left match for pattern %q in map %v", spec.givenLeftPattern, pm)
233-
} else if !spec.expectedLeftMatch && pl != nil {
234-
t.Fatalf("expected NO left match for pattern %q in map %v but got: %v", spec.givenLeftPattern, pm, pl)
235-
}
236-
if !spec.expectedLeftMatch {
237-
return
238-
}
239-
240-
if spec.expectedRightString != pl.String() {
241-
t.Errorf("expected right string representation %q but got: %q", spec.expectedRightString, pl.String())
242-
}
243-
})
244-
}
245-
}
246-
247-
func TestDollars(t *testing.T) {
248-
specs := []struct {
249-
name string
250-
givenJSON string
251-
givenLeftPattern string
252-
givenRightPattern string
253-
expectedKeyDollars []string
254-
expectedRightString string
255-
}{
256-
{
257-
name: "simple-star",
258-
givenJSON: `"a/$*/b": ["c/$1/d"]`,
259-
givenLeftPattern: "a/foo/b",
260-
givenRightPattern: "c/foo/d",
261-
expectedKeyDollars: []string{"foo"},
262-
expectedRightString: "`c/$1/d`",
229+
name: "one-pair-many-stars-half-match",
230+
givenJSON: `"a/*/b/**": ["c/*/d/**"]`,
231+
givenKey: "a/foo/b/bar/doo",
232+
givenStrictKey: "",
233+
givenValue: "c/fox/d",
234+
givenStrictValue: "",
235+
expectedHasKey: true,
236+
expectedHasValue: false,
263237
}, {
264-
name: "double-star",
265-
givenJSON: `"a/$**/b": ["c/$1/d"]`,
266-
givenLeftPattern: "a/foo/bar/b",
267-
givenRightPattern: "c/foo/bar/d",
268-
expectedKeyDollars: []string{"foo/bar"},
269-
expectedRightString: "`c/$1/d`",
238+
name: "one-pair-many-stars-no-match",
239+
givenJSON: `"a/*/b/**": ["c"]`,
240+
givenKey: "a/ahoi/b",
241+
givenStrictKey: "",
242+
givenValue: "c",
243+
givenStrictValue: "",
244+
expectedHasKey: false,
245+
expectedHasValue: false,
270246
}, {
271-
name: "many-dollars",
272-
givenJSON: `"a/$*/b/$**/c": ["d/$1/e/$2/f"]`,
273-
givenLeftPattern: "a/foo/b/bar/car/c",
274-
givenRightPattern: "d/foo/e/bar/car/f",
275-
expectedKeyDollars: []string{"foo", "bar/car"},
276-
expectedRightString: "`d/$1/e/$2/f`",
247+
name: "shuffled-dollars-full-match",
248+
givenJSON: `"a/$*/b/$**/c": ["d/$2/e/$1/f"]`,
249+
givenKey: "a/foo/b/bar/car/c",
250+
givenStrictKey: "ahoi",
251+
givenValue: "d/bar/car/e/foo/f",
252+
givenStrictValue: "car",
253+
expectedHasKey: true,
254+
expectedHasValue: true,
277255
}, {
278-
name: "shuffled-dollars",
279-
givenJSON: `"a/$*/b/$**/c": ["d/$2/e/$1/f"]`,
280-
givenLeftPattern: "a/foo/b/bar/car/c",
281-
givenRightPattern: "d/bar/car/e/foo/f",
282-
expectedKeyDollars: []string{"foo", "bar/car"},
283-
expectedRightString: "`d/$2/e/$1/f`",
256+
name: "use-not-all-dollars-full-match",
257+
givenJSON: `"a/$*/b/$**/c": ["d/$2/e"]`,
258+
givenKey: "a/foo/b/bar/car/c",
259+
givenStrictKey: "",
260+
givenValue: "d/bar/car/e",
261+
givenStrictValue: "",
262+
expectedHasKey: true,
263+
expectedHasValue: true,
284264
}, {
285-
name: "use-not-all-dollars",
286-
givenJSON: `"a/$*/b/$**/c": ["d/$2/e"]`,
287-
givenLeftPattern: "a/foo/b/bar/car/c",
288-
givenRightPattern: "d/bar/car/e",
289-
expectedKeyDollars: []string{"foo", "bar/car"},
290-
expectedRightString: "`d/$2/e`",
265+
name: "use-no-dollars-full-match",
266+
givenJSON: `"a/$*/b/$**/c": ["d/e/f"]`,
267+
givenKey: "ahoi",
268+
givenStrictKey: "a/foo/b/bar/car/c",
269+
givenValue: "car",
270+
givenStrictValue: "d/e/f",
271+
expectedHasKey: true,
272+
expectedHasValue: true,
291273
}, {
292-
name: "use-no-dollars",
293-
givenJSON: `"a/$*/b/$**/c": ["d/e/f"]`,
294-
givenLeftPattern: "a/foo/b/bar/car/c",
295-
givenRightPattern: "d/e/f",
296-
expectedKeyDollars: []string{"foo", "bar/car"},
297-
expectedRightString: "`d/e/f`",
274+
name: "double-use-dollar-full-match",
275+
givenJSON: `"a/$**/b": ["c/$1/d/$1/e"]`,
276+
givenKey: "a/foo/bar/b",
277+
givenStrictKey: "",
278+
givenValue: "c/foo/bar/d/foo/bar/e",
279+
givenStrictValue: "",
280+
expectedHasKey: true,
281+
expectedHasValue: true,
298282
}, {
299-
name: "double-use-dollar",
300-
givenJSON: `"a/$**/b": ["c/$1/d/$1/e"]`,
301-
givenLeftPattern: "a/foo/bar/b",
302-
givenRightPattern: "c/foo/bar/d/foo/bar/e",
303-
expectedKeyDollars: []string{"foo/bar"},
304-
expectedRightString: "`c/$1/d/$1/e`",
305-
}, {
306-
name: "all-at-once",
307-
givenJSON: `"a/$*/b/$**/c": ["d/$2/e/$1/f/$1/g/$2/h"]`,
308-
givenLeftPattern: "a/foo/b/bar/car/c",
309-
givenRightPattern: "d/bar/car/e/foo/f/foo/g/bar/car/h",
310-
expectedKeyDollars: []string{"foo", "bar/car"},
311-
expectedRightString: "`d/$2/e/$1/f/$1/g/$2/h`",
283+
name: "competing-keys-full-match",
284+
givenJSON: `"foo/bar/**": ["b"], "$*/$*ar/b$*": ["$1/$3b/$2", "fo*/c*d/**"]`,
285+
givenKey: "foo/bar/by",
286+
givenStrictKey: "",
287+
givenValue: "foo/yb/b",
288+
givenStrictValue: "",
289+
expectedHasKey: true,
290+
expectedHasValue: true,
312291
},
313292
}
314293

@@ -319,23 +298,23 @@ func TestDollars(t *testing.T) {
319298
if err != nil {
320299
t.Fatalf("got unexpected error: %v", err)
321300
}
322-
pm := cfg.AllowOnlyIn
323301

324-
pl, actualKeyDollars := pm.MatchingList(spec.givenLeftPattern)
302+
actualHasKey, actualHasValue := cfg.AllowOnlyIn.HasKeyValue(
303+
spec.givenKey,
304+
spec.givenStrictKey,
305+
spec.givenValue,
306+
spec.givenStrictValue,
307+
)
325308

326-
if pl == nil {
327-
t.Fatalf("expected left match for pattern %q in map %v", spec.givenLeftPattern, pm)
309+
if spec.expectedHasKey != actualHasKey {
310+
t.Errorf("expected hasKey for keys %q/%q in map %v to be %t, got %t",
311+
spec.givenKey, spec.givenStrictKey, cfg.AllowOnlyIn, spec.expectedHasKey, actualHasKey)
328312
}
329313

330-
if !reflect.DeepEqual(actualKeyDollars, spec.expectedKeyDollars) {
331-
t.Errorf("expected dollar matches in key to be %q, got %q", spec.expectedKeyDollars, actualKeyDollars)
332-
}
333-
if spec.expectedRightString != pl.String() {
334-
t.Errorf("expected right string representation %q but got: %q", spec.expectedRightString, pl.String())
335-
}
336-
if _, fullmatch := pl.MatchString(spec.givenRightPattern, spec.expectedKeyDollars); !fullmatch {
337-
t.Errorf("right pattern %q didn't match with dollars %q",
338-
spec.givenRightPattern, spec.expectedKeyDollars)
314+
if spec.expectedHasValue != actualHasValue {
315+
t.Errorf("expected hasValue for keys %q/%q and values %q/%q in map %v to be %t, got %t",
316+
spec.givenKey, spec.givenStrictKey, spec.givenValue, spec.givenStrictValue,
317+
cfg.AllowOnlyIn, spec.expectedHasValue, actualHasValue)
339318
}
340319
})
341320
}

0 commit comments

Comments
 (0)