logger_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package logger
  16. import (
  17. "strings"
  18. "testing"
  19. )
  20. func TestAPI(t *testing.T) {
  21. l := New()
  22. l.SetFlags(0)
  23. l.SetPrefix("testing")
  24. debug := 0
  25. l.AddHandler(LevelDebug, checkFunc(t, LevelDebug, "test 0", &debug))
  26. info := 0
  27. l.AddHandler(LevelInfo, checkFunc(t, LevelInfo, "test 1", &info))
  28. warn := 0
  29. l.AddHandler(LevelWarn, checkFunc(t, LevelWarn, "test 2", &warn))
  30. ok := 0
  31. l.AddHandler(LevelOK, checkFunc(t, LevelOK, "test 3", &ok))
  32. l.Debugf("test %d", 0)
  33. l.Debugln("test", 0)
  34. l.Infof("test %d", 1)
  35. l.Infoln("test", 1)
  36. l.Warnf("test %d", 2)
  37. l.Warnln("test", 2)
  38. l.Okf("test %d", 3)
  39. l.Okln("test", 3)
  40. if debug != 2 {
  41. t.Errorf("Debug handler called %d != 2 times", debug)
  42. }
  43. if info != 2 {
  44. t.Errorf("Info handler called %d != 2 times", info)
  45. }
  46. if warn != 2 {
  47. t.Errorf("Warn handler called %d != 2 times", warn)
  48. }
  49. if ok != 2 {
  50. t.Errorf("Ok handler called %d != 2 times", ok)
  51. }
  52. }
  53. func checkFunc(t *testing.T, expectl LogLevel, expectmsg string, counter *int) func(LogLevel, string) {
  54. return func(l LogLevel, msg string) {
  55. *counter++
  56. if l != expectl {
  57. t.Errorf("Incorrect message level %d != %d", l, expectl)
  58. }
  59. if !strings.HasSuffix(msg, expectmsg) {
  60. t.Errorf("%q does not end with %q", msg, expectmsg)
  61. }
  62. }
  63. }