log_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package log
  5. // These tests are too simple.
  6. import (
  7. "bytes"
  8. "os"
  9. "regexp"
  10. "testing"
  11. )
  12. const (
  13. Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
  14. Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
  15. Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
  16. Rline = `(54|56):` // must update if the calls to l.Printf / l.Print below move
  17. Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline
  18. Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline
  19. )
  20. type tester struct {
  21. flag int
  22. prefix string
  23. pattern string // regexp that log output must match; we add ^ and expected_text$ always
  24. }
  25. var tests = []tester{
  26. // individual pieces:
  27. {0, "", ""},
  28. {0, "XXX", "XXX"},
  29. {Ldate, "", Rdate + " "},
  30. {Ltime, "", Rtime + " "},
  31. {Ltime | Lmicroseconds, "", Rtime + Rmicroseconds + " "},
  32. {Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time
  33. {Llongfile, "", Rlongfile + " "},
  34. {Lshortfile, "", Rshortfile + " "},
  35. {Llongfile | Lshortfile, "", Rshortfile + " "}, // shortfile overrides longfile
  36. // everything at once:
  37. {Ldate | Ltime | Lmicroseconds | Llongfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " "},
  38. {Ldate | Ltime | Lmicroseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " "},
  39. }
  40. // Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
  41. func testPrint(t *testing.T, flag int, prefix string, pattern string, useFormat bool) {
  42. buf := new(bytes.Buffer)
  43. SetOutput(buf)
  44. SetFlags(flag)
  45. SetPrefix(prefix)
  46. if useFormat {
  47. Printf("hello %d world", 23)
  48. } else {
  49. Println("hello", 23, "world")
  50. }
  51. line := buf.String()
  52. line = line[0 : len(line)-1]
  53. pattern = "^" + pattern + "hello 23 world$"
  54. matched, err4 := regexp.MatchString(pattern, line)
  55. if err4 != nil {
  56. t.Fatal("pattern did not compile:", err4)
  57. }
  58. if !matched {
  59. t.Errorf("log output should match %q is %q", pattern, line)
  60. }
  61. SetOutput(os.Stderr)
  62. }
  63. func TestAll(t *testing.T) {
  64. for _, testcase := range tests {
  65. testPrint(t, testcase.flag, testcase.prefix, testcase.pattern, false)
  66. testPrint(t, testcase.flag, testcase.prefix, testcase.pattern, true)
  67. }
  68. }
  69. func TestOutput(t *testing.T) {
  70. const testString = "test"
  71. var b bytes.Buffer
  72. l := New(&b, "", 0)
  73. l.Println(testString)
  74. if expect := testString + "\n"; b.String() != expect {
  75. t.Errorf("log output should match %q is %q", expect, b.String())
  76. }
  77. }
  78. func TestFlagAndPrefixSetting(t *testing.T) {
  79. var b bytes.Buffer
  80. l := New(&b, "Test:", LstdFlags)
  81. f := l.Flags()
  82. if f != LstdFlags {
  83. t.Errorf("Flags 1: expected %x got %x", LstdFlags, f)
  84. }
  85. l.SetFlags(f | Lmicroseconds)
  86. f = l.Flags()
  87. if f != LstdFlags|Lmicroseconds {
  88. t.Errorf("Flags 2: expected %x got %x", LstdFlags|Lmicroseconds, f)
  89. }
  90. p := l.Prefix()
  91. if p != "Test:" {
  92. t.Errorf(`Prefix: expected "Test:" got %q`, p)
  93. }
  94. l.SetPrefix("Reality:")
  95. p = l.Prefix()
  96. if p != "Reality:" {
  97. t.Errorf(`Prefix: expected "Reality:" got %q`, p)
  98. }
  99. // Verify a log message looks right, with our prefix and microseconds present.
  100. l.Print("hello")
  101. pattern := "^Reality:" + Rdate + " " + Rtime + Rmicroseconds + " hello\n"
  102. matched, err := regexp.Match(pattern, b.Bytes())
  103. if err != nil {
  104. t.Fatalf("pattern %q did not compile: %s", pattern, err)
  105. }
  106. if !matched {
  107. t.Error("message did not match pattern")
  108. }
  109. }