File: logger_test.go

package info (click to toggle)
golang-github-cactus-mlog 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: makefile: 2
file content (198 lines) | stat: -rw-r--r-- 5,512 bytes parent folder | download
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
// Copyright (c) 2012-2016 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package mlog

import (
	"bytes"
	"fmt"
	"io"
	"testing"
	"time"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
	"gotest.tools/v3/assert/opt"
	"gotest.tools/v3/golden"
)

func assertPanic(t *testing.T, f func()) {
	defer func() {
		if r := recover(); r == nil {
			t.Errorf("The code did not panic")
		}
	}()
	f()
}

func TestLoggerMsgs(t *testing.T) {
	var infoTests = map[string]struct {
		flags   FlagSet
		method  string
		message string
		extra   interface{}
	}{
		"infom1":  {Llevel | Lsort, "infom", "test", Map{"x": "y"}},
		"infom2":  {Llevel | Lsort, "infom", "test", Map{"x": "y", "y": "z", "t": "u", "u": "v"}},
		"infom3":  {Llevel | Lsort, "infom", "test", Map{"y": "z", "x": "y", "u": "v", "t": "u"}},
		"infom4":  {Llevel | Lsort, "infom", "test", Map{"x": 1, "y": 2, "z": 3, "haz_string": "such tests"}},
		"debug1":  {Llevel | Lsort | Ldebug, "debugm", "test", nil},
		"debug2":  {Llevel | Lsort | Ldebug, "debugm", "test", nil},
		"infof1":  {Llevel, "infof", "test: %d", 5},
		"infof2":  {Llevel, "infof", "test: %s", "test"},
		"infof3":  {Llevel, "infof", "test: %s %s", []interface{}{"test", "pickles"}},
		"infox1":  {Llevel, "infox", "test", []*Attr{{"x", "y"}}},
		"infox2":  {Llevel, "infox", "test", []*Attr{{"x", "y"}, {"y", "z"}}},
		"infox3":  {Llevel, "infox", "test", nil},
		"debugx1": {Llevel | Ldebug, "debugx", "test", []*Attr{{"x", "y"}}},
		"debugx2": {Llevel | Ldebug, "debugx", "test", []*Attr{{"x", "y"}, {"y", "z"}}},
		"debugx3": {Llevel | Ldebug, "debugx", "test", nil},
	}

	buf := &bytes.Buffer{}
	logger := New(io.Discard, Llevel|Lsort)
	logger.out = buf

	for name, tt := range infoTests {
		buf.Truncate(0)
		logger.flags = uint64(tt.flags)

		switch tt.method {
		case "debugx":
			m, ok := tt.extra.([]*Attr)
			if !ok && tt.extra != nil {
				t.Errorf("%s: failed type assertion", name)
				continue
			}
			logger.Debugx(tt.message, m...)
		case "infox":
			m, ok := tt.extra.([]*Attr)
			if !ok && tt.extra != nil {
				t.Errorf("%s: failed type assertion", name)
				continue
			}
			logger.Infox(tt.message, m...)
		case "debugm":
			m, ok := tt.extra.(Map)
			if !ok && tt.extra != nil {
				t.Errorf("%s: failed type assertion", name)
				continue
			}
			logger.Debugm(tt.message, m)
		case "infom":
			m, ok := tt.extra.(Map)
			if !ok && tt.extra != nil {
				t.Errorf("%s: failed type assertion", name)
				continue
			}
			logger.Infom(tt.message, m)
		case "debug":
			logger.Debug(tt.message)
		case "info":
			logger.Info(tt.message)
		case "debugf":
			if i, ok := tt.extra.([]interface{}); ok {
				logger.Debugf(tt.message, i...)
			} else {
				logger.Debugf(tt.message, tt.extra)
			}
		case "infof":
			if i, ok := tt.extra.([]interface{}); ok {
				logger.Infof(tt.message, i...)
			} else {
				logger.Infof(tt.message, tt.extra)
			}
		default:
			t.Errorf("%s: not sure what to do", name)
			continue
		}

		goldenFixture := fmt.Sprintf("test_logger_msgs.%s.golden", name)
		golden.AssertBytes(t, buf.Bytes(), goldenFixture, "%s: did not match expectation", name)
	}

}

func TestLoggerTimestamp(t *testing.T) {
	buf := &bytes.Buffer{}

	// test nanoseconds
	logger := New(buf, Lstd)
	tnow := time.Now()
	logger.Info("test this")
	ts := bytes.Split(buf.Bytes()[6:], []byte{'"'})[0]
	tlog, err := time.Parse(time.RFC3339Nano, string(ts))
	assert.Check(t, err, "Failed to parse time from log")
	assert.Assert(t, is.DeepEqual(tnow, tlog, opt.TimeWithThreshold(2*time.Second)), "Time not even close")

	buf.Truncate(0)

	// test microeconds
	logger.SetFlags(Lstd)
	tnow = time.Now()
	logger.Info("test this")
	ts = bytes.Split(buf.Bytes()[6:], []byte{'"'})[0]
	tlog, err = time.Parse(time.RFC3339Nano, string(ts))
	assert.Check(t, err, "Failed to parse time from log")
	assert.Assert(t, is.DeepEqual(tnow, tlog, opt.TimeWithThreshold(2*time.Second)), "Time not even close")

	buf.Truncate(0)

	// test standard (seconds)
	logger.SetFlags(Lstd)
	tnow = time.Now()
	logger.Info("test this")
	ts = bytes.Split(buf.Bytes()[6:], []byte{'"'})[0]
	tlog, err = time.Parse(time.RFC3339Nano, string(ts))
	assert.Check(t, err, "Failed to parse time from log")
	assert.Assert(t, is.DeepEqual(tnow, tlog, opt.TimeWithThreshold(2*time.Second)), "Time not even close")
}

func TestPanics(t *testing.T) {
	var infoTests = map[string]struct {
		flags   FlagSet
		method  string
		message string
		extra   interface{}
	}{
		"panic":  {Llevel | Lsort, "panic", "test", nil},
		"panicf": {Llevel | Lsort, "panicf", "test: %d", 5},
		"panicm": {Llevel | Lsort, "panicm", "test", Map{"x": "y"}},
	}

	buf := &bytes.Buffer{}
	logger := New(io.Discard, Llevel|Lsort)
	logger.out = buf

	for name, tt := range infoTests {
		buf.Truncate(0)
		logger.flags = uint64(tt.flags)

		switch tt.method {
		case "panicm":
			m, ok := tt.extra.(Map)
			if !ok && tt.extra != nil {
				t.Errorf("%s: failed type assertion", name)
				continue
			}
			assertPanic(t, func() {
				logger.Panicm(tt.message, m)
			})
		case "panicf":
			assertPanic(t, func() {
				logger.Panicf(tt.message, tt.extra)
			})
		case "panic":
			assertPanic(t, func() {
				logger.Panic(tt.message)
			})
		default:
			t.Errorf("%s: not sure what to do", name)
			continue
		}

		goldenFixture := fmt.Sprintf("test_logger_msgs.%s.golden", name)
		golden.AssertBytes(t, buf.Bytes(), goldenFixture, "%s: did not match expectation", name)
	}
}