-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_test.go
95 lines (89 loc) · 2.44 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
87
88
89
90
91
92
93
94
95
package logger
import (
"context"
"fmt"
"testing"
)
func TestNoFieldsMutation(t *testing.T) {
testCases := map[string]struct {
apply func(e *Entry) *Entry
expectedEntries int
}{
"WithError": {
apply: func(e *Entry) *Entry {
return e.WithError(fmt.Errorf("some error"))
},
expectedEntries: 1,
},
"WithField": {
apply: func(e *Entry) *Entry {
return e.WithField("single", "value")
},
expectedEntries: 1,
},
"WithFields": {
apply: func(e *Entry) *Entry {
return e.WithFields(Fields{"multiple1": "value", "multiple2": "value"})
},
expectedEntries: 2,
},
"WithField+WithFields": {
apply: func(e *Entry) *Entry {
return e.WithField("single", "value").WithFields(Fields{"multiple1": "value", "multiple2": "value"})
},
expectedEntries: 3,
},
"WithField+WithError": {
apply: func(e *Entry) *Entry {
return e.WithField("single", "value").WithError(fmt.Errorf("some error"))
},
expectedEntries: 2,
},
"Override WithField": {
apply: func(e *Entry) *Entry {
return e.WithField("single", "value").WithField("single", "overridden-value")
},
expectedEntries: 1,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
entry := &Entry{}
if len(entry.fields) != 0 {
t.Fatalf("A blank entry should not contain any fields. Found %d", len(entry.fields))
}
newEntry := tc.apply(entry)
if len(entry.fields) != 0 {
t.Fatalf("Applying a func mutated the original entry. Found %d entries in original entry.", len(entry.fields))
}
if len(newEntry.fields) != tc.expectedEntries {
t.Fatalf("Applying a func did not add fields to the new entry. Expected %d, found %d", tc.expectedEntries, len(newEntry.fields))
}
})
}
}
func TestNoContextMutation(t *testing.T) {
entry1 := &Entry{}
if entry1.context != nil {
t.Fatalf("A blank entry should not contain a context.")
}
ctx1 := context.Background()
ctx2 := context.TODO()
entry2 := entry1.WithContext(ctx1)
if entry1.context != nil {
t.Fatalf("WithContext mutated the original entry1.")
}
if entry2.context != ctx1 {
t.Fatalf("WithContext did not set context in entry2.")
}
entry3 := entry2.WithContext(ctx2)
if entry1.context != nil {
t.Fatalf("WithContext mutated the original entry1.")
}
if entry2.context != ctx1 {
t.Fatalf("The second WithContext mutated entry2.")
}
if entry3.context != ctx2 {
t.Fatalf("The second WithContext did not set context in entry3.")
}
}