-
Notifications
You must be signed in to change notification settings - Fork 72
/
logger_test.go
131 lines (119 loc) · 3.2 KB
/
logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2016 Ryan Boehning. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package q
import (
"fmt"
"strconv"
"strings"
"testing"
"time"
)
// TestHeader verifies that logger.header() returns a header line with the
// expected filename, function name, and line number.
// nolint: funlen
func TestHeader(t *testing.T) {
testCases := []struct {
lastFile, lastFunc string
currFile, currFunc string
timerExpired bool
wantEmptyString bool
}{
{
lastFile: "foo.go",
lastFunc: "foo.Bar",
currFile: "foo.go",
currFunc: "foo.Bar",
timerExpired: false,
wantEmptyString: true,
},
{
lastFile: "hello.go",
lastFunc: "main.Greeting",
currFile: "hello.go",
currFunc: "main.Farewell",
timerExpired: false,
wantEmptyString: false,
},
{
lastFile: "hello.go",
lastFunc: "main.Greeting",
currFile: "goodbye.go",
currFunc: "main.Greeting",
timerExpired: false,
wantEmptyString: false,
},
{
lastFile: "hello.go",
lastFunc: "main.Greeting",
currFile: "goodbye.go",
currFunc: "main.Farewell",
timerExpired: false,
wantEmptyString: false,
},
{
lastFile: "goodbye.go",
lastFunc: "main.Goodbye",
currFile: "goodbye.go",
currFunc: "main.Goodbye",
timerExpired: false,
wantEmptyString: true,
},
{
lastFile: "goodbye.go",
lastFunc: "main.Goodbye",
currFile: "goodbye.go",
currFunc: "main.Goodbye",
timerExpired: true,
wantEmptyString: false,
},
}
for _, tc := range testCases {
l := logger{
lastFile: tc.lastFile,
lastFunc: tc.lastFunc,
}
if !tc.timerExpired {
l.lastWrite = time.Now()
}
const line = 123
h := l.header(tc.currFunc, tc.currFile, line)
if tc.wantEmptyString {
if h == "" {
continue
}
t.Fatalf("\nl.header(%s, %s, %d)\ngot: %q\nwant: %q", tc.currFunc, tc.lastFile, line, h, "")
}
if !strings.Contains(h, tc.currFunc) {
t.Fatalf("\nl.header(%s, %s, %d)\ngot: %q\nmissing current function name", tc.currFunc, tc.currFile, line, h)
}
if !strings.Contains(h, tc.currFile) {
t.Fatalf("\nl.header(%s, %s, %d)\ngot: %q\nmissing current file name", tc.currFunc, tc.currFile, line, h)
}
if !strings.Contains(h, strconv.Itoa(line)) {
t.Fatalf("\nl.header(%s, %s, %d)\ngot: %q\nmissing line number", tc.currFunc, tc.currFile, line, h)
}
}
}
// TestOutput verifies that logger.output() prints the expected output to the
// log buffer.
func TestOutput(t *testing.T) {
testCases := []struct {
args []string
want string
}{
{
args: []string{fmt.Sprintf("%s=%s", colorize("a", bold), colorize("int(1)", cyan))},
want: fmt.Sprintf("%s %s=%s\n", colorize("0.000s", yellow), colorize("a", bold), colorize("int(1)", cyan)),
},
}
for _, tc := range testCases {
l := logger{start: time.Now().UTC()}
l.output(tc.args...)
got := l.buf.String()
if got != tc.want {
argString := strings.Join(tc.args, ", ")
t.Fatalf("\nlogger.output(%s)\ngot: %swant: %s", argString, got, tc.want)
}
}
}