-
Notifications
You must be signed in to change notification settings - Fork 3
/
funcs_test.go
59 lines (49 loc) · 1.52 KB
/
funcs_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
package logbuch
import (
"bytes"
"strings"
"testing"
)
func TestFuncs(t *testing.T) {
for lvl := 0; lvl < LevelError; lvl++ {
var stdout bytes.Buffer
var stderr bytes.Buffer
SetOutput(&stdout, &stderr)
SetLevel(lvl)
Debug("Debug %s", "message")
Info("Info %s", "message")
Warn("Warning %s", "message")
Error("Error %s", "message")
if strings.Contains(stdout.String(), "Debug message") != (lvl <= LevelDebug) ||
strings.Contains(stdout.String(), "Info message") != (lvl <= LevelInfo) ||
strings.Contains(stdout.String(), "Warning message") != (lvl <= LevelWarning) ||
strings.Contains(stdout.String(), "Error message") {
t.Fatalf("Unexpected standard log output for level %v: %v", logger.level, stdout.String())
}
if !strings.Contains(stderr.String(), "Error message") {
t.Fatalf("Unexpected error log output for level %v: %v", logger.level, stderr.String())
}
}
}
func TestFatal(t *testing.T) {
var stderr bytes.Buffer
defer func() {
if r := recover(); r == nil {
t.Fatalf("Fatal must panic")
}
if !strings.Contains(stderr.String(), "Fatal") ||
!strings.Contains(stderr.String(), "key=value") {
t.Fatalf("Log must contain error message")
}
}()
SetOutput(nil, &stderr)
SetFormatter(NewFieldFormatter(StandardTimeFormat, "\t"))
Fatal("Fatal", Fields{"key": "value"})
}
func TestSetFormatter(t *testing.T) {
formatter := NewFieldFormatter(StandardTimeFormat, "\t")
SetFormatter(formatter)
if logger.GetFormatter() != formatter {
t.Fatal("Formatter must have been set")
}
}