-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
86 lines (81 loc) · 2.61 KB
/
main_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
package log
import (
"bytes"
"fmt"
"strings"
"testing"
)
func Test_Prefix(t *testing.T) {
// make io.Writer buffers for stdout and stderr:
stdoutBuffer := bytes.NewBuffer(nil)
stderrBuffer := bytes.NewBuffer(nil)
// Test that we can initialize the logger with a prefix
l := NewWithPrefix(stdoutBuffer, stderrBuffer, "myprefix")
// Test that the prefix is used in the output
l.Info("test")
// check that stdout buffer is not empty:
if stdoutBuffer.Len() == 0 {
t.Errorf("expected stdout buffer to not be empty")
}
logBuffer := stdoutBuffer.String()
fmt.Println("logBuffer:", logBuffer)
if !strings.Contains(logBuffer, "myprefix") {
t.Errorf("expected prefix to be used in output")
}
// test that the message is correct:
if !strings.Contains(logBuffer, "test") {
t.Errorf("expected logline to contain 'test'")
}
// test that stderr buffer is empty:
if stderrBuffer.Len() != 0 {
t.Errorf("expected stderr buffer to be empty")
}
}
func Test_Loglevel(t *testing.T) {
// make io.Writer buffers for stdout and stderr:
stdoutBuffer := bytes.NewBuffer(nil)
stderrBuffer := bytes.NewBuffer(nil)
// Test that we can initialize the logger with a prefix
l := NewWithPrefix(stdoutBuffer, stderrBuffer, "myprefix")
l.Trace("trace-message")
// make sure that stdout and stderr are empty:
if stdoutBuffer.Len() != 0 {
t.Errorf("trace level: expected stdout buffer to be empty")
}
if stderrBuffer.Len() != 0 {
t.Errorf("trace level: expected stderr buffer to be empty")
}
l.Debug("debug-message")
if stdoutBuffer.Len() != 0 {
t.Errorf("debug level: expected stdout buffer to be empty")
}
if stderrBuffer.Len() != 0 {
t.Errorf("debug level: expected stderr buffer to be empty")
}
// now we output info and we expect it to be in stdout:
l.Info("info-message")
if stdoutBuffer.Len() == 0 {
t.Errorf("info level: expected stdout buffer to not be empty")
}
currentStdoutSize := stdoutBuffer.Len()
if stderrBuffer.Len() != 0 {
t.Errorf("info level: expected stderr buffer to be empty")
}
// now we output warn and we expect it to be in stderr:
l.Warn("warn-message")
if stdoutBuffer.Len() != currentStdoutSize {
t.Errorf("warn level: expected stdout buffer to not change")
}
if stderrBuffer.Len() == 0 {
t.Errorf("warn level: expected stderr buffer to not be empty")
}
currentStderrSize := stderrBuffer.Len()
// now we output error and we expect it to be in stderr:
l.Error("error-message")
if stdoutBuffer.Len() != currentStdoutSize {
t.Errorf("error level: expected stdout buffer to not change")
}
if stderrBuffer.Len() == currentStderrSize {
t.Errorf("error level: expected stderr buffer to change")
}
}