tests: make path assertions aware of vendoring

The path of a package can change in a situation where
dependency vendoring is in use.
This change modifies gin's unit tests to allow such paths.
This commit is contained in:
Philipp Meinen 2015-08-21 20:57:53 +02:00
parent 704d690ac0
commit 9fd8aff56e
4 changed files with 23 additions and 13 deletions

View File

@ -161,7 +161,7 @@ func TestContextHandlerName(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := createTestContext()
c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest} c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest}
assert.Equal(t, c.HandlerName(), "github.com/gin-gonic/gin.handlerNameTest") assert.Regexp(t, "^(.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest$", c.HandlerName())
} }
func handlerNameTest(c *Context) { func handlerNameTest(c *Context) {

View File

@ -63,7 +63,7 @@ func TestDebugPrintRoutes(t *testing.T) {
defer teardown() defer teardown()
debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest}) debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})
assert.Equal(t, w.String(), "[GIN-debug] GET /path/to/route/:param --> github.com/gin-gonic/gin.handlerNameTest (2 handlers)\n") assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, w.String())
} }
func setup(w io.Writer) { func setup(w io.Writer) {

View File

@ -214,32 +214,42 @@ func TestListOfRoutes(t *testing.T) {
list := router.Routes() list := router.Routes()
assert.Len(t, list, 7) assert.Len(t, list, 7)
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/favicon.ico", Path: "/favicon.ico",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test1$",
}) })
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/", Path: "/",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test1$",
}) })
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/users/", Path: "/users/",
Handler: "github.com/gin-gonic/gin.handler_test2", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test2$",
}) })
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/users/:id", Path: "/users/:id",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test1$",
}) })
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "POST", Method: "POST",
Path: "/users/:id", Path: "/users/:id",
Handler: "github.com/gin-gonic/gin.handler_test2", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test2$",
}) })
} }
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
for _, gotRoute := range gotRoutes {
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
assert.Regexp(t, wantRoute.Path, gotRoute.Path)
return
}
}
t.Errorf("route not found: %v", wantRoute)
}
func handler_test1(c *Context) {} func handler_test1(c *Context) {}
func handler_test2(c *Context) {} func handler_test2(c *Context) {}

View File

@ -78,7 +78,7 @@ func TestFilterFlags(t *testing.T) {
} }
func TestFunctionName(t *testing.T) { func TestFunctionName(t *testing.T) {
assert.Equal(t, nameOfFunction(somefunction), "github.com/gin-gonic/gin.somefunction") assert.Regexp(t, `^(.*/vendor/)?github.com/gin-gonic/gin.somefunction$`, nameOfFunction(somefunction))
} }
func somefunction() { func somefunction() {