diff --git a/context_test.go b/context_test.go index 2ef872c..cc1c452 100644 --- a/context_test.go +++ b/context_test.go @@ -73,7 +73,7 @@ func TestContextReset(t *testing.T) { assert.Empty(t, c.Errors.Errors()) assert.Empty(t, c.Errors.ByType(ErrorTypeAny)) assert.Len(t, c.Params, 0) - assert.Equal(t, c.index, -1) + assert.EqualValues(t, c.index, -1) assert.Equal(t, c.Writer.(*responseWriter), &c.writermem) } diff --git a/gin_test.go b/gin_test.go index 24a192e..ddecc3e 100644 --- a/gin_test.go +++ b/gin_test.go @@ -5,6 +5,7 @@ package gin import ( + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -37,8 +38,8 @@ func TestCreateDefaultRouter(t *testing.T) { } func TestNoRouteWithoutGlobalHandlers(t *testing.T) { - middleware0 := func(c *Context) {} - middleware1 := func(c *Context) {} + var middleware0 HandlerFunc = func(c *Context) {} + var middleware1 HandlerFunc = func(c *Context) {} router := New() @@ -46,22 +47,22 @@ func TestNoRouteWithoutGlobalHandlers(t *testing.T) { assert.Nil(t, router.Handlers) assert.Len(t, router.noRoute, 1) assert.Len(t, router.allNoRoute, 1) - assert.Equal(t, router.noRoute[0], middleware0) - assert.Equal(t, router.allNoRoute[0], middleware0) + compareFunc(t, router.noRoute[0], middleware0) + compareFunc(t, router.allNoRoute[0], middleware0) router.NoRoute(middleware1, middleware0) assert.Len(t, router.noRoute, 2) assert.Len(t, router.allNoRoute, 2) - assert.Equal(t, router.noRoute[0], middleware1) - assert.Equal(t, router.allNoRoute[0], middleware1) - assert.Equal(t, router.noRoute[1], middleware0) - assert.Equal(t, router.allNoRoute[1], middleware0) + compareFunc(t, router.noRoute[0], middleware1) + compareFunc(t, router.allNoRoute[0], middleware1) + compareFunc(t, router.noRoute[1], middleware0) + compareFunc(t, router.allNoRoute[1], middleware0) } func TestNoRouteWithGlobalHandlers(t *testing.T) { - middleware0 := func(c *Context) {} - middleware1 := func(c *Context) {} - middleware2 := func(c *Context) {} + var middleware0 HandlerFunc = func(c *Context) {} + var middleware1 HandlerFunc = func(c *Context) {} + var middleware2 HandlerFunc = func(c *Context) {} router := New() router.Use(middleware2) @@ -71,27 +72,27 @@ func TestNoRouteWithGlobalHandlers(t *testing.T) { assert.Len(t, router.Handlers, 1) assert.Len(t, router.noRoute, 1) - assert.Equal(t, router.Handlers[0], middleware2) - assert.Equal(t, router.noRoute[0], middleware0) - assert.Equal(t, router.allNoRoute[0], middleware2) - assert.Equal(t, router.allNoRoute[1], middleware0) + compareFunc(t, router.Handlers[0], middleware2) + compareFunc(t, router.noRoute[0], middleware0) + compareFunc(t, router.allNoRoute[0], middleware2) + compareFunc(t, router.allNoRoute[1], middleware0) router.Use(middleware1) assert.Len(t, router.allNoRoute, 3) assert.Len(t, router.Handlers, 2) assert.Len(t, router.noRoute, 1) - assert.Equal(t, router.Handlers[0], middleware2) - assert.Equal(t, router.Handlers[1], middleware1) - assert.Equal(t, router.noRoute[0], middleware0) - assert.Equal(t, router.allNoRoute[0], middleware2) - assert.Equal(t, router.allNoRoute[1], middleware1) - assert.Equal(t, router.allNoRoute[2], middleware0) + compareFunc(t, router.Handlers[0], middleware2) + compareFunc(t, router.Handlers[1], middleware1) + compareFunc(t, router.noRoute[0], middleware0) + compareFunc(t, router.allNoRoute[0], middleware2) + compareFunc(t, router.allNoRoute[1], middleware1) + compareFunc(t, router.allNoRoute[2], middleware0) } func TestNoMethodWithoutGlobalHandlers(t *testing.T) { - middleware0 := func(c *Context) {} - middleware1 := func(c *Context) {} + var middleware0 HandlerFunc = func(c *Context) {} + var middleware1 HandlerFunc = func(c *Context) {} router := New() @@ -99,16 +100,16 @@ func TestNoMethodWithoutGlobalHandlers(t *testing.T) { assert.Empty(t, router.Handlers) assert.Len(t, router.noMethod, 1) assert.Len(t, router.allNoMethod, 1) - assert.Equal(t, router.noMethod[0], middleware0) - assert.Equal(t, router.allNoMethod[0], middleware0) + compareFunc(t, router.noMethod[0], middleware0) + compareFunc(t, router.allNoMethod[0], middleware0) router.NoMethod(middleware1, middleware0) assert.Len(t, router.noMethod, 2) assert.Len(t, router.allNoMethod, 2) - assert.Equal(t, router.noMethod[0], middleware1) - assert.Equal(t, router.allNoMethod[0], middleware1) - assert.Equal(t, router.noMethod[1], middleware0) - assert.Equal(t, router.allNoMethod[1], middleware0) + compareFunc(t, router.noMethod[0], middleware1) + compareFunc(t, router.allNoMethod[0], middleware1) + compareFunc(t, router.noMethod[1], middleware0) + compareFunc(t, router.allNoMethod[1], middleware0) } func TestRebuild404Handlers(t *testing.T) { @@ -116,9 +117,9 @@ func TestRebuild404Handlers(t *testing.T) { } func TestNoMethodWithGlobalHandlers(t *testing.T) { - middleware0 := func(c *Context) {} - middleware1 := func(c *Context) {} - middleware2 := func(c *Context) {} + var middleware0 HandlerFunc = func(c *Context) {} + var middleware1 HandlerFunc = func(c *Context) {} + var middleware2 HandlerFunc = func(c *Context) {} router := New() router.Use(middleware2) @@ -128,20 +129,28 @@ func TestNoMethodWithGlobalHandlers(t *testing.T) { assert.Len(t, router.Handlers, 1) assert.Len(t, router.noMethod, 1) - assert.Equal(t, router.Handlers[0], middleware2) - assert.Equal(t, router.noMethod[0], middleware0) - assert.Equal(t, router.allNoMethod[0], middleware2) - assert.Equal(t, router.allNoMethod[1], middleware0) + compareFunc(t, router.Handlers[0], middleware2) + compareFunc(t, router.noMethod[0], middleware0) + compareFunc(t, router.allNoMethod[0], middleware2) + compareFunc(t, router.allNoMethod[1], middleware0) router.Use(middleware1) assert.Len(t, router.allNoMethod, 3) assert.Len(t, router.Handlers, 2) assert.Len(t, router.noMethod, 1) - assert.Equal(t, router.Handlers[0], middleware2) - assert.Equal(t, router.Handlers[1], middleware1) - assert.Equal(t, router.noMethod[0], middleware0) - assert.Equal(t, router.allNoMethod[0], middleware2) - assert.Equal(t, router.allNoMethod[1], middleware1) - assert.Equal(t, router.allNoMethod[2], middleware0) + compareFunc(t, router.Handlers[0], middleware2) + compareFunc(t, router.Handlers[1], middleware1) + compareFunc(t, router.noMethod[0], middleware0) + compareFunc(t, router.allNoMethod[0], middleware2) + compareFunc(t, router.allNoMethod[1], middleware1) + compareFunc(t, router.allNoMethod[2], middleware0) +} + +func compareFunc(t *testing.T, a, b interface{}) { + sf1 := reflect.ValueOf(a) + sf2 := reflect.ValueOf(b) + if sf1.Pointer() != sf2.Pointer() { + t.Error("different functions") + } } diff --git a/path_test.go b/path_test.go index 3bf2799..01cb758 100644 --- a/path_test.go +++ b/path_test.go @@ -83,6 +83,6 @@ func TestPathCleanMallocs(t *testing.T) { for _, test := range cleanTests { allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) }) - assert.Equal(t, allocs, 0) + assert.EqualValues(t, allocs, 0) } }