woodpecker/vendor/github.com/gin-gonic/gin/debug_test.go

78 lines
1.7 KiB
Go
Raw Normal View History

2015-05-22 18:37:40 +00:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"bytes"
"errors"
"io"
"log"
2015-09-30 01:21:17 +00:00
"os"
2015-05-22 18:37:40 +00:00
"testing"
2015-09-30 01:21:17 +00:00
"github.com/stretchr/testify/assert"
2015-05-22 18:37:40 +00:00
)
// TODO
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
// func debugPrint(format string, values ...interface{}) {
func TestIsDebugging(t *testing.T) {
SetMode(DebugMode)
assert.True(t, IsDebugging())
SetMode(ReleaseMode)
assert.False(t, IsDebugging())
SetMode(TestMode)
assert.False(t, IsDebugging())
}
func TestDebugPrint(t *testing.T) {
var w bytes.Buffer
setup(&w)
defer teardown()
SetMode(ReleaseMode)
debugPrint("DEBUG this!")
SetMode(TestMode)
debugPrint("DEBUG this!")
assert.Empty(t, w.String())
SetMode(DebugMode)
debugPrint("these are %d %s\n", 2, "error messages")
assert.Equal(t, w.String(), "[GIN-debug] these are 2 error messages\n")
}
func TestDebugPrintError(t *testing.T) {
var w bytes.Buffer
setup(&w)
defer teardown()
SetMode(DebugMode)
debugPrintError(nil)
assert.Empty(t, w.String())
debugPrintError(errors.New("this is an error"))
assert.Equal(t, w.String(), "[GIN-debug] [ERROR] this is an error\n")
}
2015-09-30 01:21:17 +00:00
func TestDebugPrintRoutes(t *testing.T) {
var w bytes.Buffer
setup(&w)
defer teardown()
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")
}
2015-05-22 18:37:40 +00:00
func setup(w io.Writer) {
SetMode(DebugMode)
2015-09-30 01:21:17 +00:00
log.SetOutput(w)
2015-05-22 18:37:40 +00:00
}
func teardown() {
SetMode(TestMode)
2015-09-30 01:21:17 +00:00
log.SetOutput(os.Stdout)
2015-05-22 18:37:40 +00:00
}