-
Notifications
You must be signed in to change notification settings - Fork 5
/
debug_test.go
152 lines (119 loc) · 2.53 KB
/
debug_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package debug
import "testing"
import "strings"
import "bytes"
import "time"
func assertContains(t *testing.T, str, substr string) {
if !strings.Contains(str, substr) {
t.Fatalf("expected %q to contain %q", str, substr)
}
}
func assertNotContains(t *testing.T, str, substr string) {
if strings.Contains(str, substr) {
t.Fatalf("expected %q to not contain %q", str, substr)
}
}
func TestDefault(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)
debug := Debug("foo")
debug("something")
debug("here")
debug("whoop")
if buf.Len() != 0 {
t.Fatalf("buffer should be empty")
}
}
func TestEnable(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)
Enable("foo")
debug := Debug("foo")
debug("something")
debug("here")
debug("whoop")
if buf.Len() == 0 {
t.Fatalf("buffer should have output")
}
str := string(buf.Bytes())
assertContains(t, str, "something")
assertContains(t, str, "here")
assertContains(t, str, "whoop")
}
func TestMultipleOneEnabled(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)
Enable("foo")
foo := Debug("foo")
foo("foo")
bar := Debug("bar")
bar("bar")
if buf.Len() == 0 {
t.Fatalf("buffer should have output")
}
str := string(buf.Bytes())
assertContains(t, str, "foo")
assertNotContains(t, str, "bar")
}
func TestMultipleEnabled(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)
Enable("foo,bar")
foo := Debug("foo")
foo("foo")
bar := Debug("bar")
bar("bar")
if buf.Len() == 0 {
t.Fatalf("buffer should have output")
}
str := string(buf.Bytes())
assertContains(t, str, "foo")
assertContains(t, str, "bar")
}
func TestEnableDisable(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)
Enable("foo,bar")
Disable()
foo := Debug("foo")
foo("foo")
bar := Debug("bar")
bar("bar")
if buf.Len() != 0 {
t.Fatalf("buffer should not have output")
}
}
func ExampleEnable() {
Enable("mongo:connection")
Enable("mongo:*")
Enable("foo,bar,baz")
Enable("*")
}
func ExampleDebug() {
var debug = Debug("single")
for {
debug("sending mail")
debug("send email to %s", "[email protected]")
debug("send email to %s", "[email protected]")
debug("send email to %s", "[email protected]")
time.Sleep(500 * time.Millisecond)
}
}
func BenchmarkDisabled(b *testing.B) {
debug := Debug("something")
for i := 0; i < b.N; i++ {
debug("stuff")
}
}
func BenchmarkNonMatch(b *testing.B) {
debug := Debug("something")
Enable("nonmatch")
for i := 0; i < b.N; i++ {
debug("stuff")
}
}