logger_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package logger
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. func TestAPI(t *testing.T) {
  10. l := New()
  11. l.SetFlags(0)
  12. l.SetPrefix("testing")
  13. debug := 0
  14. l.AddHandler(LevelDebug, checkFunc(t, LevelDebug, "test 0", &debug))
  15. info := 0
  16. l.AddHandler(LevelInfo, checkFunc(t, LevelInfo, "test 1", &info))
  17. warn := 0
  18. l.AddHandler(LevelWarn, checkFunc(t, LevelWarn, "test 2", &warn))
  19. ok := 0
  20. l.AddHandler(LevelOK, checkFunc(t, LevelOK, "test 3", &ok))
  21. l.Debugf("test %d", 0)
  22. l.Debugln("test", 0)
  23. l.Infof("test %d", 1)
  24. l.Infoln("test", 1)
  25. l.Warnf("test %d", 2)
  26. l.Warnln("test", 2)
  27. l.Okf("test %d", 3)
  28. l.Okln("test", 3)
  29. if debug != 2 {
  30. t.Errorf("Debug handler called %d != 2 times", debug)
  31. }
  32. if info != 2 {
  33. t.Errorf("Info handler called %d != 2 times", info)
  34. }
  35. if warn != 2 {
  36. t.Errorf("Warn handler called %d != 2 times", warn)
  37. }
  38. if ok != 2 {
  39. t.Errorf("Ok handler called %d != 2 times", ok)
  40. }
  41. }
  42. func checkFunc(t *testing.T, expectl LogLevel, expectmsg string, counter *int) func(LogLevel, string) {
  43. return func(l LogLevel, msg string) {
  44. *counter++
  45. if l != expectl {
  46. t.Errorf("Incorrect message level %d != %d", l, expectl)
  47. }
  48. if !strings.HasSuffix(msg, expectmsg) {
  49. t.Errorf("%q does not end with %q", msg, expectmsg)
  50. }
  51. }
  52. }