2015-04-08 00:58:35 +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 (
|
2015-05-09 01:34:43 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"log"
|
2015-04-08 00:58:35 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2015-05-09 01:34:43 +00:00
|
|
|
var cachedDebugLogger *log.Logger = nil
|
|
|
|
|
2015-04-09 10:15:02 +00:00
|
|
|
// TODO
|
2015-05-07 09:30:01 +00:00
|
|
|
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
2015-04-09 10:15:02 +00:00
|
|
|
// func debugPrint(format string, values ...interface{}) {
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func TestIsDebugging(t *testing.T) {
|
|
|
|
SetMode(DebugMode)
|
|
|
|
assert.True(t, IsDebugging())
|
|
|
|
SetMode(ReleaseMode)
|
|
|
|
assert.False(t, IsDebugging())
|
|
|
|
SetMode(TestMode)
|
|
|
|
assert.False(t, IsDebugging())
|
|
|
|
}
|
2015-05-09 01:34:43 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup(w io.Writer) {
|
|
|
|
SetMode(DebugMode)
|
|
|
|
if cachedDebugLogger == nil {
|
|
|
|
cachedDebugLogger = debugLogger
|
|
|
|
debugLogger = log.New(w, debugLogger.Prefix(), 0)
|
|
|
|
} else {
|
|
|
|
panic("setup failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func teardown() {
|
|
|
|
SetMode(TestMode)
|
|
|
|
if cachedDebugLogger != nil {
|
|
|
|
debugLogger = cachedDebugLogger
|
|
|
|
cachedDebugLogger = nil
|
|
|
|
} else {
|
|
|
|
panic("teardown failed")
|
|
|
|
}
|
|
|
|
}
|