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.
7 // These tests are too simple.
17 Rdate
= `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
18 Rtime
= `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
19 Rmicroseconds
= `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
20 Rline
= `(54|56):` // must update if the calls to l.Printf / l.Print below move
21 Rlongfile
= `.*/[A-Za-z0-9_\-]+\.go:` + Rline
22 Rshortfile
= `[A-Za-z0-9_\-]+\.go:` + Rline
28 pattern
string // regexp that log output must match; we add ^ and expected_text$ always
35 {Ldate
, "", Rdate
+ " "},
36 {Ltime
, "", Rtime
+ " "},
37 {Ltime | Lmicroseconds
, "", Rtime
+ Rmicroseconds
+ " "},
38 {Lmicroseconds
, "", Rtime
+ Rmicroseconds
+ " "}, // microsec implies time
39 {Llongfile
, "", Rlongfile
+ " "},
40 {Lshortfile
, "", Rshortfile
+ " "},
41 {Llongfile | Lshortfile
, "", Rshortfile
+ " "}, // shortfile overrides longfile
42 // everything at once:
43 {Ldate | Ltime | Lmicroseconds | Llongfile
, "XXX", "XXX" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rlongfile
+ " "},
44 {Ldate | Ltime | Lmicroseconds | Lshortfile
, "XXX", "XXX" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rshortfile
+ " "},
47 // Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
48 func testPrint(t
*testing
.T
, flag
int, prefix
string, pattern
string, useFormat
bool) {
49 buf
:= new(bytes
.Buffer
)
54 Printf("hello %d world", 23)
56 Println("hello", 23, "world")
59 line
= line
[0 : len(line
)-1]
60 pattern
= "^" + pattern
+ "hello 23 world$"
61 matched
, err4
:= regexp
.MatchString(pattern
, line
)
63 t
.Fatal("pattern did not compile:", err4
)
66 t
.Errorf("log output should match %q is %q", pattern
, line
)
71 func TestAll(t
*testing
.T
) {
72 for _
, testcase
:= range tests
{
73 testPrint(t
, testcase
.flag
, testcase
.prefix
, testcase
.pattern
, false)
74 testPrint(t
, testcase
.flag
, testcase
.prefix
, testcase
.pattern
, true)
78 func TestOutput(t
*testing
.T
) {
79 const testString
= "test"
83 if expect
:= testString
+ "\n"; b
.String() != expect
{
84 t
.Errorf("log output should match %q is %q", expect
, b
.String())
88 func TestFlagAndPrefixSetting(t
*testing
.T
) {
90 l
:= New(&b
, "Test:", LstdFlags
)
93 t
.Errorf("Flags 1: expected %x got %x", LstdFlags
, f
)
95 l
.SetFlags(f | Lmicroseconds
)
97 if f
!= LstdFlags|Lmicroseconds
{
98 t
.Errorf("Flags 2: expected %x got %x", LstdFlags|Lmicroseconds
, f
)
102 t
.Errorf(`Prefix: expected "Test:" got %q`, p
)
104 l
.SetPrefix("Reality:")
107 t
.Errorf(`Prefix: expected "Reality:" got %q`, p
)
109 // Verify a log message looks right, with our prefix and microseconds present.
111 pattern
:= "^Reality:" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " hello\n"
112 matched
, err
:= regexp
.Match(pattern
, b
.Bytes())
114 t
.Fatalf("pattern %q did not compile: %s", pattern
, err
)
117 t
.Error("message did not match pattern")