This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
130 lines (107 loc) · 2.59 KB
/
logging_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
package logging
import (
"bytes"
"log"
"os"
"strings"
"testing"
)
func TestLoggingWithNilReferenceProducesTraditionalBehavior(t *testing.T) {
defer restore()
out := prepare()
thing := new(ThingUnderTest)
thing.Action()
assertCallDepthAccuracy(t, out)
assertCorrectLogMessage(t, out)
}
func TestLoggingWithLoggerCapturesOutput(t *testing.T) {
defer restore()
out := prepare()
thing := new(ThingUnderTest)
thing.log = Capture()
thing.Action()
assertCallDepthAccuracy(t, thing.log.Log)
assertCorrectLogMessage(t, thing.log.Log)
assertNothingLogged(t, out)
}
func TestLoggingWithLoggerCapturesOutputInAllProvidedWriters(t *testing.T) {
defer restore()
out0 := prepare()
out1 := new(bytes.Buffer)
out2 := new(bytes.Buffer)
thing := new(ThingUnderTest)
thing.log = Capture(out1, out2)
thing.Action()
assertCorrectLogMessage(t, thing.log.Log)
assertCorrectLogMessage(t, out1)
assertCorrectLogMessage(t, out2)
assertNothingLogged(t, out0)
}
func TestLogCallsAreCounted(t *testing.T) {
defer restore()
callCount := 10
thing := new(ThingUnderTest)
thing.log = Capture()
for x := 0; x < callCount; x++ {
thing.Action()
}
assertLogCallsCounted(t, thing.log.Calls, callCount)
}
func TestLoggingWithDiscard(t *testing.T) {
defer restore()
out := prepare()
thing := new(ThingUnderTest)
thing.log = Discard()
thing.Action()
assertNothingLogged(t, thing.log.Log)
assertNothingLogged(t, out)
}
func assertLogCallsCounted(t *testing.T, actual int, expected int) {
t.Helper()
if actual != expected {
t.Errorf("\n"+
"Expected: %d\n"+
"Actual: %d",
expected, actual)
}
}
func assertCorrectLogMessage(t *testing.T, out *bytes.Buffer) {
t.Helper()
expected := "Hello, World!\n"
actual := out.String()
if !strings.HasSuffix(actual, expected) {
t.Errorf("\n"+
"Expected Suffix: %s\n"+
"Actual Log: %s",
expected, actual)
}
}
func assertCallDepthAccuracy(t *testing.T, out *bytes.Buffer) {
t.Helper()
expected := "thing_under_test.go:8"
actual := out.String()
if !strings.HasPrefix(actual, expected) {
t.Errorf("\n"+
"Expected Prefix: %s\n"+
"Actual Log: %s",
expected, actual)
}
}
func assertNothingLogged(t *testing.T, out *bytes.Buffer) {
t.Helper()
length := out.Len()
if length > 0 {
t.Errorf("Expected log to be empty, but it had a length of %d.", length)
}
}
/////////////////////////////////////////////////////////////////////////////////
func restore() {
log.SetOutput(os.Stderr)
log.SetFlags(log.LstdFlags)
}
func prepare() *bytes.Buffer {
out := new(bytes.Buffer)
log.SetOutput(out)
log.SetFlags(log.Lshortfile)
return out
}