~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/log/log_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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         = `(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
 
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
}