Rebase.
[official-gcc.git] / libgo / go / log / log_test.go
blob158c3d93c7e2fb8262579d855767cabf381517f0
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.
5 package log
7 // These tests are too simple.
9 import (
10 "bytes"
11 "os"
12 "regexp"
13 "testing"
16 const (
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
25 type tester struct {
26 flag int
27 prefix string
28 pattern string // regexp that log output must match; we add ^ and expected_text$ always
31 var tests = []tester{
32 // individual pieces:
33 {0, "", ""},
34 {0, "XXX", "XXX"},
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)
50 SetOutput(buf)
51 SetFlags(flag)
52 SetPrefix(prefix)
53 if useFormat {
54 Printf("hello %d world", 23)
55 } else {
56 Println("hello", 23, "world")
58 line := buf.String()
59 line = line[0 : len(line)-1]
60 pattern = "^" + pattern + "hello 23 world$"
61 matched, err4 := regexp.MatchString(pattern, line)
62 if err4 != nil {
63 t.Fatal("pattern did not compile:", err4)
65 if !matched {
66 t.Errorf("log output should match %q is %q", pattern, line)
68 SetOutput(os.Stderr)
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"
80 var b bytes.Buffer
81 l := New(&b, "", 0)
82 l.Println(testString)
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) {
89 var b bytes.Buffer
90 l := New(&b, "Test:", LstdFlags)
91 f := l.Flags()
92 if f != LstdFlags {
93 t.Errorf("Flags 1: expected %x got %x", LstdFlags, f)
95 l.SetFlags(f | Lmicroseconds)
96 f = l.Flags()
97 if f != LstdFlags|Lmicroseconds {
98 t.Errorf("Flags 2: expected %x got %x", LstdFlags|Lmicroseconds, f)
100 p := l.Prefix()
101 if p != "Test:" {
102 t.Errorf(`Prefix: expected "Test:" got %q`, p)
104 l.SetPrefix("Reality:")
105 p = l.Prefix()
106 if p != "Reality:" {
107 t.Errorf(`Prefix: expected "Reality:" got %q`, p)
109 // Verify a log message looks right, with our prefix and microseconds present.
110 l.Print("hello")
111 pattern := "^Reality:" + Rdate + " " + Rtime + Rmicroseconds + " hello\n"
112 matched, err := regexp.Match(pattern, b.Bytes())
113 if err != nil {
114 t.Fatalf("pattern %q did not compile: %s", pattern, err)
116 if !matched {
117 t.Error("message did not match pattern")