logger_test.go 1.4 KB

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