-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_test.go
285 lines (220 loc) · 7.37 KB
/
entry_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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package glog
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestEntry_newEntry(t *testing.T) {
type ctxKey struct{}
ctx := context.WithValue(context.Background(), ctxKey{}, "v1")
l := NewDefault().WithContext(ctx)
entry := newEntry(l, DebugLevel)
require.NotNil(t, entry)
require.Equal(t, l, entry.l)
require.NotNil(t, entry.encoder)
require.Equal(t, DebugLevel, entry.level)
}
func TestEntry_Byte_WithText(t *testing.T) {
var eb bytes.Buffer
var b bytes.Buffer
l := NewDefault().WithExporter(StandardExporter(&b)).WithErrorOutput(&eb)
l.Info().Byte("key", '=').Fire()
require.Greater(t, b.Len(), 0)
s := string(b.Bytes()[0 : b.Len()-1])
// parse fields
fields := strings.Split(s, " ")
require.Greater(t, len(fields), 0)
fields = fields[len(fields)-1:]
require.Equal(t, len(fields), 1)
info := fields[0]
require.True(t, strings.HasPrefix(info, "key="), "%q", info)
require.False(t, strings.Contains(info, " "), "%q", info)
info = strings.TrimPrefix(info, "key=")
require.Greater(t, len(info), 0)
i, err := strconv.ParseInt(info, 10, 64)
require.Nil(t, err, "%q", info)
require.Equal(t, byte(i), byte('='))
require.Equal(t, eb.Len(), 0)
}
func TestEntry_Byte_WithJSON(t *testing.T) {
var eb bytes.Buffer
var b bytes.Buffer
l := NewDefault().WithExporter(StandardExporter(&b)).WithEncoderFunc(JSONEncoder).WithErrorOutput(&eb)
l.Info().Byte("key", '=').Fire()
require.Greater(t, b.Len(), 0)
c := b.Bytes()[:b.Len()-1]
m := make(map[string]interface{})
err := json.Unmarshal(c, &m)
require.Nil(t, err, "%q", string(c))
n, ok := m["key"].(float64)
require.True(t, ok, "%+q", m["key"])
require.Equal(t, byte(n), byte('='), "%q", m["key"])
require.Equal(t, eb.Len(), 0)
}
func TestEntry_Duration_WithText(t *testing.T) {
var eb bytes.Buffer
var b bytes.Buffer
l := NewDefault().WithExporter(StandardExporter(&b)).WithErrorOutput(&eb)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
d := time.Second + time.Duration(r.Int63n(int64(time.Second)))
l.Info().
Nanosecond("Nanosecond", d).
Microsecond("Microsecond", d).
Millisecond("Millisecond", d).
Second("Second", d).
Minute("Minute", d).
Hour("Hour", d).
Fire()
require.Greater(t, b.Len(), 0)
s := string(b.Bytes()[0 : b.Len()-1])
fmt.Println(s)
// parse fields
fields := strings.Split(s, " ")
require.Greater(t, len(fields), 6)
fields = fields[len(fields)-6:]
require.Equal(t, len(fields), 6)
// Nanosecond
require.True(t, strings.HasPrefix(fields[0], "Nanosecond="), "%q", fields[0])
require.True(t, strings.HasSuffix(fields[0], nanoSuffix), "%q", fields[0])
require.False(t, strings.Contains(fields[0], " "), "%q", fields[0])
// Microsecond
require.True(t, strings.HasPrefix(fields[1], "Microsecond="), "%q", fields[1])
require.True(t, strings.HasSuffix(fields[1], microSuffix), "%q", fields[1])
require.False(t, strings.Contains(fields[1], " "), "%q", fields[1])
// Millisecond
require.True(t, strings.HasPrefix(fields[2], "Millisecond="), "%q", fields[2])
require.True(t, strings.HasSuffix(fields[2], milliSuffix), "%q", fields[2])
require.False(t, strings.Contains(fields[2], " "), "%q", fields[2])
// Second
require.True(t, strings.HasPrefix(fields[3], "Second="), "%q", fields[3])
require.True(t, strings.HasSuffix(fields[3], secondSuffix), "%q", fields[3])
require.False(t, strings.Contains(fields[3], " "), "%q", fields[3])
// Minute
require.True(t, strings.HasPrefix(fields[4], "Minute="), "%q", fields[4])
require.True(t, strings.HasSuffix(fields[4], minuteSuffix), "%q", fields[4])
require.False(t, strings.Contains(fields[4], " "), "%q", fields[4])
// Hour
require.True(t, strings.HasPrefix(fields[5], "Hour="), "%q", fields[5])
require.True(t, strings.HasSuffix(fields[5], hourSuffix), "%q", fields[5])
require.False(t, strings.Contains(fields[5], " "), "%q", fields[5])
require.Equal(t, eb.Len(), 0)
}
func TestEntry_Duration_WithJSON(t *testing.T) {
var eb bytes.Buffer
var b bytes.Buffer
l := NewDefault().WithExporter(StandardExporter(&b)).WithEncoderFunc(JSONEncoder).WithErrorOutput(&eb)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
d := time.Second + time.Duration(r.Int63n(int64(time.Second)))
l.Info().
Msg("Test for JSON format").
Nanosecond("Nanosecond", d).
Microsecond("Microsecond", d).
Millisecond("Millisecond", d).
Second("Second", d).
Minute("Minute", d).
Hour("Hour", d).
Fire()
require.Greater(t, b.Len(), 0)
c := b.Bytes()[:b.Len()-1]
fmt.Println(string(c))
var m map[string]string
err := json.Unmarshal(c, &m)
require.Nil(t, err, "%+q", string(c))
// Nanosecond
require.NotEqual(t, len(m["Nanosecond"]), 0, "%q", m)
require.True(t, strings.HasSuffix(m["Nanosecond"], nanoSuffix), "%q", m["Nanosecond"])
// Microsecond
require.NotEqual(t, len(m["Microsecond"]), 0, "%q", m)
require.True(t, strings.HasSuffix(m["Microsecond"], microSuffix), "%q", m["Microsecond"])
// Millisecond
require.NotEqual(t, len(m["Millisecond"]), 0, "%q", m)
require.True(t, strings.HasSuffix(m["Millisecond"], milliSuffix), "%q", m["Millisecond"])
// Second
require.NotEqual(t, len(m["Second"]), 0, "%q", m)
require.True(t, strings.HasSuffix(m["Second"], secondSuffix), "%q", m["Second"])
// Minute
require.NotEqual(t, len(m["Minute"]), 0, "%q", m)
require.True(t, strings.HasSuffix(m["Minute"], minuteSuffix), "%q", m["Minute"])
// Hour
require.NotEqual(t, len(m["Hour"]), 0, "%q", m)
require.True(t, strings.HasSuffix(m["Hour"], hourSuffix), "%q", m["Hour"])
require.Equal(t, eb.Len(), 0)
}
func TestEntry_Raw_WithText(t *testing.T) {
var b bytes.Buffer
l := NewDefault().WithExporter(StandardExporter(&b))
data := struct {
Name string
Number int64
}{
Name: "n1",
Number: 1,
}
bd, err := json.Marshal(&data)
require.Nil(t, err)
l.Info().String("key1", string(bd)).Fire()
require.Contains(t, b.String(), "\\")
b.Reset()
l.Info().RawBytes("key2", bd).RawString("key3", string(bd)).Fire()
require.NotContains(t, b.String(), "\\")
}
func TestEntry_Raw_WithJSON(t *testing.T) {
var b bytes.Buffer
l := NewDefault().WithExporter(StandardExporter(&b)).WithEncoderFunc(JSONEncoder)
data := struct {
Name string
Number int64
}{
Name: "n1",
Number: 1,
}
bd, err := json.Marshal(&data)
require.Nil(t, err)
l.Info().String("key1", string(bd)).RawBytes("key2", bd).RawString("key3", string(bd)).Fire()
require.Greater(t, b.Len(), 0)
b1 := b.Bytes()[0 : b.Len()-1]
var m map[string]interface{}
err = json.Unmarshal(b1, &m)
require.Nil(t, err)
// Expected "key1" is string
k1, ok := m["key1"]
require.True(t, ok)
_, ok = k1.(string)
require.True(t, ok)
// Expected "key2" is map
k2, ok := m["key2"]
require.True(t, ok)
_, ok = k2.(map[string]interface{})
require.True(t, ok)
// Expected "key3" is map
k3, ok := m["key3"]
require.True(t, ok)
_, ok = k3.(map[string]interface{})
require.True(t, ok)
}
func TestEntry_Error(t *testing.T) {
l := NewDefault().WithExporter(StandardExporter(ioutil.Discard))
var err error
require.NotPanics(t, func() {
l.Error().Error("error", err).Fire()
})
}
func TestEntry_Errors(t *testing.T) {
l := NewDefault().WithExporter(StandardExporter(ioutil.Discard))
var errors []error
require.NotPanics(t, func() {
l.Error().Errors("error", errors).Fire()
})
errors = append(errors, nil)
require.NotPanics(t, func() {
l.Error().Errors("error", errors).Fire()
})
}