Skip to content

Commit

Permalink
Refactor glob matcher test
Browse files Browse the repository at this point in the history
  • Loading branch information
sharbov committed Mar 6, 2018
1 parent c19bd67 commit 6b7a0ef
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions route/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,29 @@ func TestGlobMatcher(t *testing.T) {
route *Route
}{
// happy flows
{uri: "/foo", matches: true, route: getRoute("/foo")},
{uri: "/fool", matches: true, route: getRoute("/foo?")},
{uri: "/fool", matches: true, route: getRoute("/foo*")},
{uri: "/fools", matches: true, route: getRoute("/foo*")},
{uri: "/fools", matches: true, route: getRoute("/foo*")},
{uri: "/foo/x/bar", matches: true, route: getRoute("/foo/*/bar")},
{uri: "/foo/x/y/z/w/bar", matches: true, route: getRoute("/foo/**")},
{uri: "/foo/x/y/z/w/bar", matches: true, route: getRoute("/foo/**/bar")},
{uri: "/foo", matches: true, route: &Route{Path: "/foo"}},
{uri: "/fool", matches: true, route: &Route{Path: "/foo?"}},
{uri: "/fool", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/foo/x/bar", matches: true, route: &Route{Path: "/foo/*/bar"}},
{uri: "/foo/x/y/z/w/bar", matches: true, route: &Route{Path: "/foo/**"}},
{uri: "/foo/x/y/z/w/bar", matches: true, route: &Route{Path: "/foo/**/bar"}},

// error flows
{uri: "/fo", matches: false, route: getRoute("/foo")},
{uri: "/fools", matches: false, route: getRoute("/foo")},
{uri: "/fo", matches: false, route: getRoute("/foo*")},
{uri: "/fools", matches: false, route: getRoute("/foo.*")},
{uri: "/foo/x/y/z/w/baz", matches: false, route: getRoute("/foo/**/bar")},
{uri: "/fo", matches: false, route: &Route{Path: "/foo"}},
{uri: "/fools", matches: false, route: &Route{Path: "/foo"}},
{uri: "/fo", matches: false, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: false, route: &Route{Path: "/foo.*"}},
{uri: "/foo/x/y/z/w/baz", matches: false, route: &Route{Path: "/foo/**/bar"}},
}

for _, tt := range tests {
t.Run(tt.uri, func(t *testing.T) {
tt.route.Glob = glob.MustCompile(tt.route.Path)
if got, want := globMatcher(tt.uri, tt.route), tt.matches; got != want {
t.Fatalf("got %v want %v", got, want)
}
})
}
}

func getRoute(path string) *Route {
return &Route{Path: path, Glob: glob.MustCompile(path)}
}

0 comments on commit 6b7a0ef

Please sign in to comment.