-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_test.go
133 lines (100 loc) · 3.14 KB
/
event_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package recording
import (
"errors"
"testing"
"github.com/echocat/slf4g/level"
"github.com/echocat/slf4g/fields"
"github.com/echocat/slf4g/internal/test/assert"
)
func Test_eventImpl_ForEach(t *testing.T) {
instance := &event{fields: fields.
With("a", 1).
With("b", 2).
With("c", 3)}
actual := entries{}
actualErr := instance.ForEach(actual.consumer())
assert.ToBeNil(t, actualErr)
assert.ToBeEqual(t, entries{{"c", 3}, {"b", 2}, {"a", 1}}, actual)
}
func Test_eventImpl_Get(t *testing.T) {
instance := &event{fields: fields.
With("a", 1).
With("b", 2)}
actual1, actualExists1 := instance.Get("a")
assert.ToBeEqual(t, true, actualExists1)
assert.ToBeEqual(t, 1, actual1)
actual2, actualExists2 := instance.Get("b")
assert.ToBeEqual(t, true, actualExists2)
assert.ToBeEqual(t, 2, actual2)
actual3, actualExists3 := instance.Get("c")
assert.ToBeEqual(t, false, actualExists3)
assert.ToBeEqual(t, nil, actual3)
}
func Test_eventImpl_Len(t *testing.T) {
instance := &event{fields: fields.
With("a", 1).
With("b", 2)}
actual := instance.Len()
assert.ToBeEqual(t, 2, actual)
}
func Test_eventImpl_GetLevel(t *testing.T) {
instance := &event{level: level.Error}
actual := instance.GetLevel()
assert.ToBeEqual(t, level.Error, actual)
}
func Test_eventImpl_With(t *testing.T) {
expected := fields.With("a", 1).With("b", 2)
instance := &event{fields: fields.With("a", 1)}
actual := instance.With("b", 2)
assert.ToBeEqualUsing(t, expected, actual.(*event).fields, fields.AreEqual)
}
func Test_eventImpl_Withf(t *testing.T) {
expected := fields.With("a", 1).Withf("b", "%d", 2)
instance := &event{fields: fields.With("a", 1)}
actual := instance.Withf("b", "%d", 2)
assert.ToBeEqualUsing(t, expected, actual.(*event).fields, fields.AreEqual)
}
func Test_eventImpl_WithError(t *testing.T) {
givenError := errors.New("expected")
expected := fields.With("a", 1).With("anErrorKey", givenError)
instance := &event{
fields: fields.With("a", 1),
provider: &Provider{FieldKeysSpec: fields.KeysSpecImpl{Error: "anErrorKey"}},
}
actual := instance.WithError(givenError)
assert.ToBeEqualUsing(t, expected, actual.(*event).fields, fields.AreEqual)
}
func Test_eventImpl_WithAll(t *testing.T) {
givenMap := map[string]interface{}{
"b": 2,
"c": 3,
}
expected := fields.With("a", 1).WithAll(givenMap)
instance := &event{fields: fields.With("a", 1)}
actual := instance.WithAll(givenMap)
assert.ToBeEqualUsing(t, expected, actual.(*event).fields, fields.AreEqual)
}
func Test_eventImpl_Without(t *testing.T) {
expected := fields.With("b", 2).With("d", 4)
instance := &event{fields: fields.
With("a", 1).
With("b", 2).
With("c", 3).
With("d", 4)}
actual := instance.Without("a", "c")
assert.ToBeEqualUsing(t, expected, actual.(*event).fields, fields.AreEqual)
}
type entries []entry
func (instance *entries) add(key string, value interface{}) {
*instance = append(*instance, entry{key, value})
}
func (instance *entries) consumer() func(key string, value interface{}) error {
return func(key string, value interface{}) error {
instance.add(key, value)
return nil
}
}
type entry struct {
key string
value interface{}
}