-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_cache_test.go
109 lines (84 loc) · 3.23 KB
/
logger_cache_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
package log
import (
"sort"
"testing"
"github.com/echocat/slf4g/internal/test/assert"
)
func Test_NewLoggerCache(t *testing.T) {
givenRootLogger := newMockLogger("aRootOne")
givenRootFactory := func() Logger { return givenRootLogger }
givenFactory := func(name string) Logger { return newMockLogger(name) }
actual := NewLoggerCache(givenRootFactory, givenFactory)
assert.ToBeOfType(t, &loggerCache{}, actual)
assert.ToBeSame(t, givenRootLogger, actual.(*loggerCache).root)
assert.ToBeSame(t, givenFactory, actual.(*loggerCache).factory)
assert.ToBeNotNil(t, actual.(*loggerCache).loggers)
}
func Test_NewLoggerCache_panicsIfRootFactoryReturnsNil(t *testing.T) {
givenRootFactory := func() Logger { return nil }
givenFactory := func(name string) Logger { return newMockLogger(name) }
assert.Execution(t, func() {
NewLoggerCache(givenRootFactory, givenFactory)
}).WillPanicWith("Root factory returned a nil root logger.")
}
func Test_loggerCache_GetRootLogger(t *testing.T) {
givenRootLogger := newMockLogger("aRootOne")
instance := &loggerCache{root: givenRootLogger}
actual1 := instance.GetRootLogger()
actual2 := instance.GetRootLogger()
assert.ToBeSame(t, givenRootLogger, actual1)
assert.ToBeSame(t, givenRootLogger, actual2)
}
func Test_loggerCache_GetLogger(t *testing.T) {
instance := &loggerCache{
loggers: make(map[string]Logger),
factory: func(name string) Logger { return newMockLogger(name) },
}
actualFoo1 := instance.GetLogger("foo")
actualFoo2 := instance.GetLogger("foo")
actualBar1 := instance.GetLogger("bar")
actualBar2 := instance.GetLogger("bar")
assert.ToBeNotNil(t, actualFoo1)
assert.ToBeNotNil(t, actualFoo2)
assert.ToBeNotNil(t, actualBar1)
assert.ToBeNotNil(t, actualBar2)
assert.ToBeEqual(t, "foo", actualFoo1.GetName())
assert.ToBeEqual(t, "foo", actualFoo2.GetName())
assert.ToBeEqual(t, "bar", actualBar1.GetName())
assert.ToBeEqual(t, "bar", actualBar2.GetName())
assert.ToBeSame(t, actualFoo1, actualFoo2)
assert.ToBeSame(t, actualBar1, actualBar2)
assert.ToBeNotSame(t, actualFoo1, actualBar1)
assert.ToBeNotSame(t, actualFoo2, actualBar2)
}
func Test_loggerCache_GetLogger_returnsRootIfFactoryReturnsNil(t *testing.T) {
givenRootLogger := newMockLogger("root")
instance := &loggerCache{
root: givenRootLogger,
loggers: make(map[string]Logger),
factory: func(name string) Logger { return nil },
}
actual1 := instance.GetLogger("foo")
actual2 := instance.GetLogger("bar")
assert.ToBeNotNil(t, actual1)
assert.ToBeNotNil(t, actual2)
assert.ToBeEqual(t, "root", actual1.GetName())
assert.ToBeEqual(t, "root", actual2.GetName())
assert.ToBeSame(t, givenRootLogger, actual1)
assert.ToBeSame(t, givenRootLogger, actual2)
}
func Test_loggerCache_GetNames(t *testing.T) {
instance := &loggerCache{
root: newMockLogger("root"),
loggers: make(map[string]Logger),
factory: func(name string) Logger { return newMockLogger(name) },
}
actualBefore := instance.GetNames()
assert.ToBeEqual(t, []string{}, actualBefore)
assert.ToBeNotNil(t, instance.GetRootLogger())
assert.ToBeNotNil(t, instance.GetLogger("foo"))
assert.ToBeNotNil(t, instance.GetLogger("bar"))
actualAfter := instance.GetNames()
sort.Strings(actualAfter)
assert.ToBeEqual(t, []string{"bar", "foo"}, actualAfter)
}