Skip to content

Commit

Permalink
refactor matcher tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Feb 11, 2018
1 parent f2bcc0f commit a8e3641
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions route/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,52 @@ import (
)

func TestPrefixMatcher(t *testing.T) {
routeFoo := &Route{Host: "www.example.com", Path: "/foo"}

tests := []struct {
uri string
want bool
route *Route
uri string
matches bool
route *Route
}{
{"/fo", false, routeFoo},
{"/foo", true, routeFoo},
{"/fools", true, routeFoo},
{"/bar", false, routeFoo},
{uri: "/foo", matches: true, route: &Route{Path: "/foo"}},
{uri: "/fools", matches: true, route: &Route{Path: "/foo"}},
{uri: "/fo", matches: false, route: &Route{Path: "/foo"}},
{uri: "/bar", matches: false, route: &Route{Path: "/foo"}},
}

for _, tt := range tests {
if got := prefixMatcher(tt.uri, tt.route); got != tt.want {
t.Errorf("%s: got %v want %v", tt.uri, got, tt.want)
}
t.Run(tt.uri, func(t *testing.T) {
if got, want := prefixMatcher(tt.uri, tt.route), tt.matches; got != want {
t.Fatalf("got %v want %v", got, want)
}
})
}
}

func TestGlobMatcher(t *testing.T) {
routeFoo := &Route{Host: "www.example.com", Path: "/foo"}
routeFooWild := &Route{Host: "www.example.com", Path: "/foo.*"}

tests := []struct {
uri string
want bool
route *Route
uri string
matches bool
route *Route
}{
{"/fo", false, routeFoo},
{"/foo", true, routeFoo},
{"/fools", false, routeFoo},
{"/bar", false, routeFoo},

{"/fo", false, routeFooWild},
{"/foo", false, routeFooWild},
{"/fools", false, routeFooWild},
{"/foo.", true, routeFooWild},
{"/foo.a", true, routeFooWild},
{"/foo.bar", true, routeFooWild},
{"/foo.bar.baz", true, routeFooWild},
// happy flows
{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"}},

// error flows
{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.*"}},
}

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

0 comments on commit a8e3641

Please sign in to comment.