-
Notifications
You must be signed in to change notification settings - Fork 1
/
yoctog_test.go
183 lines (168 loc) · 3.1 KB
/
yoctog_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package yoctog
import (
"bytes"
"log"
"os"
"testing"
)
type TestCase struct {
Level Level
ExpectValue string
}
var output = new(bytes.Buffer)
func TestDebug(t *testing.T) {
debugInput := "こんにちは"
debugExpect := "[DEBUG] こんにちは\n"
debugSuppressExpect := ""
testCase := []TestCase{
{
Level: LevelDebug,
ExpectValue: debugExpect,
},
{
Level: LevelInfo,
ExpectValue: debugSuppressExpect,
},
{
Level: LevelWarn,
ExpectValue: debugSuppressExpect,
},
{
Level: LevelError,
ExpectValue: debugSuppressExpect,
},
{
Level: LevelAllSuppress,
ExpectValue: debugSuppressExpect,
},
}
for _, v := range testCase {
SetLogLevel(v.Level)
Debug(debugInput)
if output.String() != v.ExpectValue {
t.Error("unexpected value")
}
output.Reset()
}
}
func TestInfo(t *testing.T) {
infoInput := "こんにちは"
infoExpect := "[INFO] こんにちは\n"
infoSuppressExpect := ""
testCase := []TestCase{
{
Level: LevelDebug,
ExpectValue: infoExpect,
},
{
Level: LevelInfo,
ExpectValue: infoExpect,
},
{
Level: LevelWarn,
ExpectValue: infoSuppressExpect,
},
{
Level: LevelError,
ExpectValue: infoSuppressExpect,
},
{
Level: LevelAllSuppress,
ExpectValue: infoSuppressExpect,
},
}
for _, v := range testCase {
SetLogLevel(v.Level)
Info(infoInput)
if output.String() != v.ExpectValue {
t.Error("unexpected value")
}
output.Reset()
}
}
func TestWarn(t *testing.T) {
warnInput := "こんにちは"
warnExpect := "[WARN] こんにちは\n"
warnSuppressExpect := ""
testCase := []TestCase{
{
Level: LevelDebug,
ExpectValue: warnExpect,
},
{
Level: LevelInfo,
ExpectValue: warnExpect,
},
{
Level: LevelWarn,
ExpectValue: warnExpect,
},
{
Level: LevelError,
ExpectValue: warnSuppressExpect,
},
{
Level: LevelAllSuppress,
ExpectValue: warnSuppressExpect,
},
}
for _, v := range testCase {
SetLogLevel(v.Level)
Warn(warnInput)
if output.String() != v.ExpectValue {
t.Error("unexpected value")
}
output.Reset()
}
}
func TestError(t *testing.T) {
errorInput := "こんにちは"
errorExpect := "[ERROR] こんにちは\n"
errorSuppressExpect := ""
testCase := []TestCase{
{
Level: LevelDebug,
ExpectValue: errorExpect,
},
{
Level: LevelInfo,
ExpectValue: errorExpect,
},
{
Level: LevelWarn,
ExpectValue: errorExpect,
},
{
Level: LevelError,
ExpectValue: errorExpect,
},
{
Level: LevelAllSuppress,
ExpectValue: errorSuppressExpect,
},
}
for _, v := range testCase {
SetLogLevel(v.Level)
Error(errorInput)
if output.String() != v.ExpectValue {
t.Error("unexpected value")
}
output.Reset()
}
}
// Testing setup/teardown https://gist.github.com/atotto/88ed1be361976807c171
func setup() {
log.SetFlags(0)
log.SetOutput(output)
}
func teardown() {
log.SetOutput(os.Stdout)
}
func TestMain(m *testing.M) {
setup()
ret := m.Run()
if ret == 0 {
teardown()
}
os.Exit(ret)
}