logging_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package util
  11. import (
  12. "fmt"
  13. "testing"
  14. )
  15. func TestLogging(t *testing.T) {
  16. ml := NewMemoryLogger(5)
  17. ml.LogDebug("test")
  18. ml.LogInfo("test")
  19. if ml.String() != `debug: test
  20. test` {
  21. t.Error("Unexpected result:", ml.String())
  22. return
  23. }
  24. if res := fmt.Sprint(ml.Slice()); res != "[debug: test test]" {
  25. t.Error("Unexpected result:", res)
  26. return
  27. }
  28. ml.Reset()
  29. ml.LogError("test1")
  30. if res := fmt.Sprint(ml.Slice()); res != "[error: test1]" {
  31. t.Error("Unexpected result:", res)
  32. return
  33. }
  34. if res := ml.Size(); res != 1 {
  35. t.Error("Unexpected result:", res)
  36. return
  37. }
  38. // Test that the functions can be called
  39. nl := NewNullLogger()
  40. nl.LogDebug(nil, "test")
  41. nl.LogInfo(nil, "test")
  42. nl.LogError(nil, "test")
  43. sol := NewStdOutLogger()
  44. sol.stdlog = func(v ...interface{}) {}
  45. sol.LogDebug(nil, "test")
  46. sol.LogInfo(nil, "test")
  47. sol.LogError(nil, "test")
  48. }