Debian Codegraph @

logger_test.go

raw symbols SCIP
1 // Copyright (c) 2012-2016 Eli Janssen
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package mlog
6
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "testing"
12 "time"
13
14 "gotest.tools/v3/assert"
15 is "gotest.tools/v3/assert/cmp"
16 "gotest.tools/v3/assert/opt"
17 "gotest.tools/v3/golden"
18 )
19
20 func assertPanic(t *testing.T, f func()) {
21 defer func() {
22 if r := recover(); r == nil {
23 t.Errorf("The code did not panic")
24 }
25 }()
26 f()
27 }
28
29 func TestLoggerMsgs(t *testing.T) {
30 var infoTests = map[string]struct {
31 flags FlagSet
32 method string
33 message string
34 extra interface{}
35 }{
36 "infom1": {Llevel | Lsort, "infom", "test", Map{"x": "y"}},
37 "infom2": {Llevel | Lsort, "infom", "test", Map{"x": "y", "y": "z", "t": "u", "u": "v"}},
38 "infom3": {Llevel | Lsort, "infom", "test", Map{"y": "z", "x": "y", "u": "v", "t": "u"}},
39 "infom4": {Llevel | Lsort, "infom", "test", Map{"x": 1, "y": 2, "z": 3, "haz_string": "such tests"}},
40 "debug1": {Llevel | Lsort | Ldebug, "debugm", "test", nil},
41 "debug2": {Llevel | Lsort | Ldebug, "debugm", "test", nil},
42 "infof1": {Llevel, "infof", "test: %d", 5},
43 "infof2": {Llevel, "infof", "test: %s", "test"},
44 "infof3": {Llevel, "infof", "test: %s %s", []interface{}{"test", "pickles"}},
45 "infox1": {Llevel, "infox", "test", []*Attr{{"x", "y"}}},
46 "infox2": {Llevel, "infox", "test", []*Attr{{"x", "y"}, {"y", "z"}}},
47 "infox3": {Llevel, "infox", "test", nil},
48 "debugx1": {Llevel | Ldebug, "debugx", "test", []*Attr{{"x", "y"}}},
49 "debugx2": {Llevel | Ldebug, "debugx", "test", []*Attr{{"x", "y"}, {"y", "z"}}},
50 "debugx3": {Llevel | Ldebug, "debugx", "test", nil},
51 }
52
53 buf := &bytes.Buffer{}
54 logger := New(io.Discard, Llevel|Lsort)
55 logger.out = buf
56
57 for name, tt := range infoTests {
58 buf.Truncate(0)
59 logger.flags = uint64(tt.flags)
60
61 switch tt.method {
62 case "debugx":
63 m, ok := tt.extra.([]*Attr)
64 if !ok && tt.extra != nil {
65 t.Errorf("%s: failed type assertion", name)
66 continue
67 }
68 logger.Debugx(tt.message, m...)
69 case "infox":
70 m, ok := tt.extra.([]*Attr)
71 if !ok && tt.extra != nil {
72 t.Errorf("%s: failed type assertion", name)
73 continue
74 }
75 logger.Infox(tt.message, m...)
76 case "debugm":
77 m, ok := tt.extra.(Map)
78 if !ok && tt.extra != nil {
79 t.Errorf("%s: failed type assertion", name)
80 continue
81 }
82 logger.Debugm(tt.message, m)
83 case "infom":
84 m, ok := tt.extra.(Map)
85 if !ok && tt.extra != nil {
86 t.Errorf("%s: failed type assertion", name)
87 continue
88 }
89 logger.Infom(tt.message, m)
90 case "debug":
91 logger.Debug(tt.message)
92 case "info":
93 logger.Info(tt.message)
94 case "debugf":
95 if i, ok := tt.extra.([]interface{}); ok {
96 logger.Debugf(tt.message, i...)
97 } else {
98 logger.Debugf(tt.message, tt.extra)
99 }
100 case "infof":
101 if i, ok := tt.extra.([]interface{}); ok {
102 logger.Infof(tt.message, i...)
103 } else {
104 logger.Infof(tt.message, tt.extra)
105 }
106 default:
107 t.Errorf("%s: not sure what to do", name)
108 continue
109 }
110
111 goldenFixture := fmt.Sprintf("test_logger_msgs.%s.golden", name)
112 golden.AssertBytes(t, buf.Bytes(), goldenFixture, "%s: did not match expectation", name)
113 }
114
115 }
116
117 func TestLoggerTimestamp(t *testing.T) {
118 buf := &bytes.Buffer{}
119
120 // test nanoseconds
121 logger := New(buf, Lstd)
122 tnow := time.Now()
123 logger.Info("test this")
124 ts := bytes.Split(buf.Bytes()[6:], []byte{'"'})[0]
125 tlog, err := time.Parse(time.RFC3339Nano, string(ts))
126 assert.Check(t, err, "Failed to parse time from log")
127 assert.Assert(t, is.DeepEqual(tnow, tlog, opt.TimeWithThreshold(2*time.Second)), "Time not even close")
128
129 buf.Truncate(0)
130
131 // test microeconds
132 logger.SetFlags(Lstd)
133 tnow = time.Now()
134 logger.Info("test this")
135 ts = bytes.Split(buf.Bytes()[6:], []byte{'"'})[0]
136 tlog, err = time.Parse(time.RFC3339Nano, string(ts))
137 assert.Check(t, err, "Failed to parse time from log")
138 assert.Assert(t, is.DeepEqual(tnow, tlog, opt.TimeWithThreshold(2*time.Second)), "Time not even close")
139
140 buf.Truncate(0)
141
142 // test standard (seconds)
143 logger.SetFlags(Lstd)
144 tnow = time.Now()
145 logger.Info("test this")
146 ts = bytes.Split(buf.Bytes()[6:], []byte{'"'})[0]
147 tlog, err = time.Parse(time.RFC3339Nano, string(ts))
148 assert.Check(t, err, "Failed to parse time from log")
149 assert.Assert(t, is.DeepEqual(tnow, tlog, opt.TimeWithThreshold(2*time.Second)), "Time not even close")
150 }
151
152 func TestPanics(t *testing.T) {
153 var infoTests = map[string]struct {
154 flags FlagSet
155 method string
156 message string
157 extra interface{}
158 }{
159 "panic": {Llevel | Lsort, "panic", "test", nil},
160 "panicf": {Llevel | Lsort, "panicf", "test: %d", 5},
161 "panicm": {Llevel | Lsort, "panicm", "test", Map{"x": "y"}},
162 }
163
164 buf := &bytes.Buffer{}
165 logger := New(io.Discard, Llevel|Lsort)
166 logger.out = buf
167
168 for name, tt := range infoTests {
169 buf.Truncate(0)
170 logger.flags = uint64(tt.flags)
171
172 switch tt.method {
173 case "panicm":
174 m, ok := tt.extra.(Map)
175 if !ok && tt.extra != nil {
176 t.Errorf("%s: failed type assertion", name)
177 continue
178 }
179 assertPanic(t, func() {
180 logger.Panicm(tt.message, m)
181 })
182 case "panicf":
183 assertPanic(t, func() {
184 logger.Panicf(tt.message, tt.extra)
185 })
186 case "panic":
187 assertPanic(t, func() {
188 logger.Panic(tt.message)
189 })
190 default:
191 t.Errorf("%s: not sure what to do", name)
192 continue
193 }
194
195 goldenFixture := fmt.Sprintf("test_logger_msgs.%s.golden", name)
196 golden.AssertBytes(t, buf.Bytes(), goldenFixture, "%s: did not match expectation", name)
197 }
198 }