debug_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 setup(w io.Writer) {
  49. SetMode(DebugMode)
  50. log.SetOutput(w)
  51. }
  52. func teardown() {
  53. SetMode(TestMode)
  54. log.SetOutput(os.Stdout)
  55. }