package accessctl import ( "errors" "testing" ) func TestStringMatcher(t *testing.T) { tests := []struct { descr string matcher StringMatcher str string match bool }{ // Pattern { descr: "pattern exact match", matcher: StringMatcher{ Pattern: "foo", }, str: "foo", match: true, }, { descr: "pattern exact no match", matcher: StringMatcher{ Pattern: "foo", }, str: "bar", match: false, }, { descr: "pattern single star match", matcher: StringMatcher{ Pattern: "foo/*", }, str: "foo/bar", match: true, }, { descr: "pattern single star no match 1", matcher: StringMatcher{ Pattern: "foo/*", }, str: "foo", match: false, }, { descr: "pattern single star no match 2", matcher: StringMatcher{ Pattern: "foo/*", }, str: "foo/bar/baz", match: false, }, { descr: "pattern double star match 1", matcher: StringMatcher{ Pattern: "foo/**", }, str: "foo/bar", match: true, }, { descr: "pattern double star match 2", matcher: StringMatcher{ Pattern: "foo/**", }, str: "foo/bar/baz", match: true, }, { descr: "pattern double star no match", matcher: StringMatcher{ Pattern: "foo/**", }, str: "foo", match: false, }, // Patterns, assumes individual pattern matching works correctly { descr: "patterns single match", matcher: StringMatcher{ Patterns: []string{"foo"}, }, str: "foo", match: true, }, { descr: "patterns single no match", matcher: StringMatcher{ Patterns: []string{"foo"}, }, str: "bar", match: false, }, { descr: "patterns multi first match", matcher: StringMatcher{ Patterns: []string{"foo", "bar"}, }, str: "foo", match: true, }, { descr: "patterns multi second match", matcher: StringMatcher{ Patterns: []string{"foo", "bar"}, }, str: "bar", match: true, }, { descr: "patterns multi no match", matcher: StringMatcher{ Patterns: []string{"foo", "bar"}, }, str: "baz", match: false, }, } for _, test := range tests { t.Run(test.descr, func(t *testing.T) { err := test.matcher.Match(test.str) if test.match && err != nil { t.Fatalf("expected to match, got %v", err) } else if !test.match && !errors.As(err, new(ErrFilterNoMatch)) { t.Fatalf("expected ErrFilterNoMatch, got %#v", err) } }) } }