OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [log/] [log_test.go] - Blame information for rev 750

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
 
5
package log
6
 
7
// These tests are too simple.
8
 
9
import (
10
        "bytes"
11
        "os"
12
        "regexp"
13
        "testing"
14
)
15
 
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
23
)
24
 
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
29
}
30
 
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 + " "},
45
}
46
 
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")
57
        }
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)
64
        }
65
        if !matched {
66
                t.Errorf("log output should match %q is %q", pattern, line)
67
        }
68
        SetOutput(os.Stderr)
69
}
70
 
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)
75
        }
76
}
77
 
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())
85
        }
86
}
87
 
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)
94
        }
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)
99
        }
100
        p := l.Prefix()
101
        if p != "Test:" {
102
                t.Errorf(`Prefix: expected "Test:" got %q`, p)
103
        }
104
        l.SetPrefix("Reality:")
105
        p = l.Prefix()
106
        if p != "Reality:" {
107
                t.Errorf(`Prefix: expected "Reality:" got %q`, p)
108
        }
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)
115
        }
116
        if !matched {
117
                t.Error("message did not match pattern")
118
        }
119
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.