-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdr_test.go
91 lines (81 loc) · 2.26 KB
/
stdr_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
package stdlogr
import (
"bytes"
"errors"
"fmt"
"log"
"testing"
"github.com/go-logr/logr"
)
func TestInfo(t *testing.T) {
const testString = "test\ntest"
var b bytes.Buffer
l := New(log.New(&b, "", 0))
l.Info(testString)
if expect := fmt.Sprintf("[verbosity=0] %s\n", testString); b.String() != expect {
t.Errorf("log output should match %q is %q", expect, b.String())
}
}
func TestError(t *testing.T) {
const testString = "test"
var b bytes.Buffer
l := New(log.New(&b, "", 0))
l.Error(errors.New(testString), testString)
if expect := fmt.Sprintf("[Error=%[1]s] [verbosity=0] %[1]s\n", testString); b.String() != expect {
t.Errorf("log output should match %q is %q", expect, b.String())
}
}
func TestName(t *testing.T) {
const testString = "test"
var b bytes.Buffer
l := New(log.New(&b, "", 0))
l.WithName(testString).Info(testString)
if expect := fmt.Sprintf("[name=%[1]s] [verbosity=0] %[1]s\n", testString); b.String() != expect {
t.Errorf("log output should match %q is %q", expect, b.String())
}
}
func TestNameAppend(t *testing.T) {
const testString = "test"
var b bytes.Buffer
l := New(log.New(&b, "", 0))
l.WithName(testString).WithName(testString).Info(testString)
if expect := fmt.Sprintf("[name=%[1]s/%[1]s] [verbosity=0] %[1]s\n", testString); b.String() != expect {
t.Errorf("log output should match %q is %q", expect, b.String())
}
}
func TestInfoKV(t *testing.T) {
const testString = `test
test`
var b bytes.Buffer
var l logr.Logger = Logger{
Std: log.New(&b, "", 0),
Formatter: DefaultFormatter{
ForceQuote: true,
},
}
l.Info(testString, testString, testString)
if expect := fmt.Sprintf("[verbosity=0] [%[1]q=%[1]q] %[1]s\n", testString); b.String() != expect {
t.Errorf("log output should match %q is %q", expect, b.String())
}
}
func TestVerbosity(t *testing.T) {
const testString = "test"
var b bytes.Buffer
l := New(log.New(&b, "", 0))
l.V(1).Info(testString, testString, testString)
if b.String() != "" {
t.Errorf("Expected 0 output but got %q", b.String())
}
}
func TestTimestampFormat(t *testing.T) {
const testString = "test"
var b bytes.Buffer
l := Logger{
Std: log.New(&b, "", 0),
Formatter: DefaultFormatter{
TimestampFormat: "2006-01-02 03:04:05 -0700",
},
}
l.Info(testString)
t.Log(b.String())
}