forked from caarlos0/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_test.go
86 lines (69 loc) · 1.84 KB
/
entry_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 (
"fmt"
"io"
"testing"
"github.com/matryer/is"
)
func TestEntry_WithFields(t *testing.T) {
is := is.New(t)
a := NewEntry(New(io.Discard))
is.Equal(a.Fields, nil)
b := a.WithFields(Fields{"foo": "bar"})
is.Equal(Fields{}, a.mergedFields())
is.Equal(Fields{"foo": "bar"}, b.mergedFields())
c := a.WithFields(Fields{"foo": "hello", "bar": "world"})
e := c.finalize(InfoLevel, "upload")
is.Equal(e.Message, "upload")
is.Equal(e.Fields, Fields{"foo": "hello", "bar": "world"})
is.Equal(e.Level, InfoLevel)
}
func TestEntry_WithField(t *testing.T) {
is := is.New(t)
a := NewEntry(New(io.Discard))
b := a.WithField("foo", "bar")
is.Equal(Fields{}, a.mergedFields())
is.Equal(Fields{"foo": "bar"}, b.mergedFields())
}
func TestEntry_WithError(t *testing.T) {
is := is.New(t)
a := NewEntry(New(io.Discard))
b := a.WithError(fmt.Errorf("boom"))
is.Equal(Fields{}, a.mergedFields())
is.Equal(Fields{"error": "boom"}, b.mergedFields())
}
func TestEntry_WithError_fields(t *testing.T) {
is := is.New(t)
a := NewEntry(New(io.Discard))
b := a.WithError(errFields("boom"))
is.Equal(Fields{}, a.mergedFields())
is.Equal(Fields{
"error": "boom",
"reason": "timeout",
}, b.mergedFields())
}
func TestEntry_WithError_nil(t *testing.T) {
is := is.New(t)
a := NewEntry(New(io.Discard))
b := a.WithError(nil)
is.Equal(Fields{}, a.mergedFields())
is.Equal(Fields{}, b.mergedFields())
}
func TestEntry_WithoutPadding(t *testing.T) {
is := is.New(t)
log := New(io.Discard)
a := NewEntry(log)
is.Equal(defaultPadding, a.Padding)
log.IncreasePadding()
b := NewEntry(log)
is.Equal(defaultPadding+2, b.Padding)
c := b.WithoutPadding()
is.Equal(defaultPadding, c.Padding)
}
type errFields string
func (ef errFields) Error() string {
return string(ef)
}
func (ef errFields) Fields() Fields {
return Fields{"reason": "timeout"}
}