-
Notifications
You must be signed in to change notification settings - Fork 3
/
entries.go
204 lines (177 loc) · 5.75 KB
/
entries.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package zltest
import (
"time"
"github.com/rs/zerolog"
)
// Entries represents collection of zerolog log entries.
type Entries struct {
e []*Entry // Log entries.
t T // Test manager.
}
// Get returns the list of Entry in Entries
func (ets Entries) Get() []*Entry {
return ets.e
}
// ExpEntry returns nth logged entry.
func (ets Entries) ExpEntry(n int) *Entry {
ets.t.Helper()
if n < len(ets.e) {
return ets.e[n]
}
ets.t.Fatalf("expected %d%s logged entry to exist", n, ordinal(n))
return nil
}
// ordinal returns English ordinal for a whole number.
func ordinal(n int) string {
switch n {
case 1:
return "st"
case 2:
return "nd"
case 3:
return "rd"
default:
return "th"
}
}
// ExpLen tests that there is want number of entries.
func (ets Entries) ExpLen(want int) {
ets.t.Helper()
have := len(ets.e)
if have != want {
ets.t.Errorf("expected %d entries got %d", want, have)
}
}
// ExpStr tests that at least one log entry has a field key, its value is
// a string, and it's equal to exp.
func (ets Entries) ExpStr(key string, exp string) {
ets.t.Helper()
ets.exp(func(e *Entry) string { return e.expStr(key, exp) })
}
// ExpStrContains tests that at least one log entry has a field key, its value
// is a string, and it contains exp.
func (ets Entries) ExpStrContains(key string, exp string) {
ets.t.Helper()
ets.exp(func(e *Entry) string { return e.expStrContains(key, exp) })
}
// NotExpStr tests that no log entry has a field key, its value is a
// string, and it's equal to exp.
func (ets Entries) NotExpStr(key string, exp string) {
ets.t.Helper()
ets.notExp(func(e *Entry) string { return e.expStr(key, exp) })
}
// ExpTime tests that at least one log entry has a field key, its value is a
// string representing time in zerolog.TimeFieldFormat and it's equal
// to exp.
func (ets Entries) ExpTime(key string, exp time.Time) {
ets.t.Helper()
ets.exp(func(e *Entry) string { return e.expTime(key, exp) })
}
// NotExpTime tests that no one log entry has a field key, its value is a
// string representing time in zerolog.TimeFieldFormat and it's equal
// to exp.
func (ets Entries) NotExpTime(key string, exp time.Time) {
ets.t.Helper()
ets.notExp(func(e *Entry) string { return e.expTime(key, exp) })
}
// ExpDur tests that at least one log entry has a field key and its value is
// equal to exp time.Duration. The duration vale in the entry is
// multiplied by zerolog.DurationFieldUnit before the comparison.
func (ets Entries) ExpDur(key string, exp time.Duration) {
ets.t.Helper()
ets.exp(func(e *Entry) string { return e.expDur(key, exp) })
}
// NotExpDur tests that no log entry has a field key and its value is
// equal to exp time.Duration. The duration vale in the entry is
// multiplied by zerolog.DurationFieldUnit before the comparison.
func (ets Entries) NotExpDur(key string, exp time.Duration) {
ets.t.Helper()
ets.notExp(func(e *Entry) string { return e.expDur(key, exp) })
}
// ExpBool tests that at lest one entry has a field key, its value is
// boolean and equal to exp.
func (ets Entries) ExpBool(key string, exp bool) {
ets.t.Helper()
ets.exp(func(e *Entry) string { return e.expBool(key, exp) })
}
// NotExpBool tests that no log entry has a field key, its value is
// boolean and equal to exp.
func (ets Entries) NotExpBool(key string, exp bool) {
ets.t.Helper()
ets.notExp(func(e *Entry) string { return e.expBool(key, exp) })
}
// ExpMsg tests that at least one log entry message field
// (zerolog.MessageFieldName) is equal to exp.
func (ets Entries) ExpMsg(exp string) {
ets.t.Helper()
ets.exp(func(e *Entry) string { return e.expStr(zerolog.MessageFieldName, exp) })
}
// NotExpMsg tests that none of the log entry message fields
// (zerolog.MessageFieldName) are equal to exp.
func (ets Entries) NotExpMsg(exp string) {
ets.t.Helper()
ets.notExp(func(e *Entry) string { return e.expStr(zerolog.MessageFieldName, exp) })
}
// ExpError tests that at least one log entry error field
// (zerolog.ErrorFieldName) is equal to exp.
func (ets Entries) ExpError(exp string) {
ets.t.Helper()
ets.exp(func(e *Entry) string { return e.expStr(zerolog.ErrorFieldName, exp) })
}
// NotExpError tests that none of the log entry error fields
// (zerolog.ErrorFieldName) are equal to exp.
func (ets Entries) NotExpError(exp string) {
ets.t.Helper()
ets.notExp(func(e *Entry) string { return e.expStr(zerolog.ErrorFieldName, exp) })
}
// ExpErr tests that at least one log entry error field
// (zerolog.ErrorFieldName) is equal to exp error message.
func (ets Entries) ExpErr(exp error) {
ets.t.Helper()
ets.ExpError(exp.Error())
}
// NotExpErr tests that none of the log entry error fields
// (zerolog.ErrorFieldName) are equal to exp error message.
func (ets Entries) NotExpErr(exp error) {
ets.t.Helper()
ets.NotExpError(exp.Error())
}
// ExpNum tests that at least one log entry has a field key and its numerical
// value is equal to exp.
func (ets Entries) ExpNum(key string, exp float64) {
ets.t.Helper()
ets.exp(func(e *Entry) string { return e.expNum(key, exp) })
}
// NotExpNum tests that at least one log entry has a field key and its
// numerical value is equal to exp.
func (ets Entries) NotExpNum(key string, exp float64) {
ets.t.Helper()
ets.notExp(func(e *Entry) string { return e.expNum(key, exp) })
}
func (ets Entries) exp(f func(*Entry) string) {
ets.t.Helper()
e := ets.Get()
for ent := range e {
if f(e[ent]) == "" {
return
}
}
ets.t.Error("no matching log entry found")
}
func (ets Entries) notExp(f func(*Entry) string) {
ets.t.Helper()
e := ets.Get()
for ent := range e {
if f(e[ent]) == "" {
ets.t.Error("matching log entry found")
}
}
}
// Print prints all zerolog log entries.
func (ets Entries) Print() {
ets.t.Helper()
ets.t.Log("entries logged so far:")
for _, e := range ets.e {
ets.t.Log(" " + e.raw)
}
}