debug_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "bytes"
  7. "errors"
  8. "io"
  9. "log"
  10. "os"
  11. "testing"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. // TODO
  15. // func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
  16. // func debugPrint(format string, values ...interface{}) {
  17. func TestIsDebugging(t *testing.T) {
  18. SetMode(DebugMode)
  19. assert.True(t, IsDebugging())
  20. SetMode(ReleaseMode)
  21. assert.False(t, IsDebugging())
  22. SetMode(TestMode)
  23. assert.False(t, IsDebugging())
  24. }
  25. func TestDebugPrint(t *testing.T) {
  26. var w bytes.Buffer
  27. setup(&w)
  28. defer teardown()
  29. SetMode(ReleaseMode)
  30. debugPrint("DEBUG this!")
  31. SetMode(TestMode)
  32. debugPrint("DEBUG this!")
  33. assert.Empty(t, w.String())
  34. SetMode(DebugMode)
  35. debugPrint("these are %d %s\n", 2, "error messages")
  36. assert.Equal(t, w.String(), "[GIN-debug] these are 2 error messages\n")
  37. }
  38. func TestDebugPrintError(t *testing.T) {
  39. var w bytes.Buffer
  40. setup(&w)
  41. defer teardown()
  42. SetMode(DebugMode)
  43. debugPrintError(nil)
  44. assert.Empty(t, w.String())
  45. debugPrintError(errors.New("this is an error"))
  46. assert.Equal(t, w.String(), "[GIN-debug] [ERROR] this is an error\n")
  47. }
  48. func TestDebugPrintRoutes(t *testing.T) {
  49. var w bytes.Buffer
  50. setup(&w)
  51. defer teardown()
  52. debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})
  53. assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, w.String())
  54. }
  55. func setup(w io.Writer) {
  56. SetMode(DebugMode)
  57. log.SetOutput(w)
  58. }
  59. func teardown() {
  60. SetMode(TestMode)
  61. log.SetOutput(os.Stdout)
  62. }