Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / log / log_test.go
blobbd4d1a9c583b2a1b659f84545021f599fde70ce3
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 = `[0-9]+:` // 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())