-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlogger_internal_test.go
166 lines (124 loc) · 4.25 KB
/
logger_internal_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
package log
import (
"bytes"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFormatMessage(t *testing.T) {
input := []interface{}{"Hello %s\r\n%d 100%\r\n", "world", 1}
expected := "Hello %s\r\n%d 100%\r\n world 1"
actual := formatMessage(input...)
assert.Equal(t, expected, actual)
}
func TestFormatSeparator(t *testing.T) {
type test struct {
name string
message string
separator string
length int
expected string
}
var tests = []test{
{"no-message", "", "=", 12, "============"},
{"short-length", "", "=", 2, "=="},
{"short-length-with-message", "hello", "=", 2, "====[ hello ]"},
{"short-message-1", "hello", "=", 12, "====[ hello ]"},
{"short-message-2", "hello", "=", 20, "====[ hello ]======="},
{"long-message-1", "hello world, how are you doing?", "=", 12, "====[ hello world, how are you doing? ]"},
{"long-message-2", "hello world, how are you doing?", "=", 50, "====[ hello world, how are you doing? ]==========="},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actual := formatSeparator(tc.message, tc.separator, tc.length)
assert.Equal(t, tc.expected, actual)
})
}
}
func TestPrintMessage_NoColor(t *testing.T) {
type test struct {
name string
level string
message string
printTimestamp bool
expectedStdout string
expectedStderr string
}
var tests = []test{
{"debug-1", "debug", "message", false, "message\n", ""},
{"debug-2", "debug", "message", true, TestingTimeFormat + " | DEBUG | message\n", ""},
{"info-1", "info ", "message", false, "message\n", ""},
{"info-2", "info ", "message", true, TestingTimeFormat + " | INFO | message\n", ""},
{"warn-1", "warn ", "message", false, "message\n", ""},
{"warn-2", "warn ", "message", true, TestingTimeFormat + " | WARN | message\n", ""},
{"error-1", "error", "message", false, "", "message\n"},
{"error-2", "error", "message", true, "", TestingTimeFormat + " | ERROR | message\n"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
resetLogConfig()
stdout, stderr := redirectOutput()
defer resetLogOutput()
PrintTimestamp = tc.printTimestamp
PrintColors = false
printMessage(tc.level, tc.message)
actualStdOut := stdout.String()
actualStdErr := stderr.String()
assert.Equal(t, tc.expectedStdout, actualStdOut, "stdout")
assert.Equal(t, tc.expectedStderr, actualStdErr, "stderr")
})
}
}
func TestPrintMessage_Color(t *testing.T) {
type test struct {
name string
level string
message string
printTimestamp bool
expectedStdout string
expectedStderr string
}
var tests = []test{
{"debug-1", "debug", "message", false, "\x1b[90mmessage\x1b[0m\n", ""},
{"debug-2", "debug", "message", true, "\x1b[90m" + TestingTimeFormat + " | DEBUG | message\x1b[0m\n", ""},
{"info-1", "info ", "message", false, "\x1b[92mmessage\x1b[0m\n", ""},
{"info-2", "info ", "message", true, "\x1b[92m" + TestingTimeFormat + " | INFO | message\x1b[0m\n", ""},
{"warn-1", "warn ", "message", false, "\x1b[93mmessage\x1b[0m\n", ""},
{"warn-2", "warn ", "message", true, "\x1b[93m" + TestingTimeFormat + " | WARN | message\x1b[0m\n", ""},
{"error-1", "error", "message", false, "", "\x1b[91mmessage\x1b[0m\n"},
{"error-2", "error", "message", true, "", "\x1b[91m" + TestingTimeFormat + " | ERROR | message\x1b[0m\n"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
resetLogConfig()
stdout, stderr := redirectOutput()
defer resetLogOutput()
PrintTimestamp = tc.printTimestamp
PrintColors = true
printMessage(tc.level, tc.message)
actualStdOut := stdout.String()
actualStdErr := stderr.String()
assert.Equal(t, tc.expectedStdout, actualStdOut, "stdout")
assert.Equal(t, tc.expectedStderr, actualStdErr, "stderr")
})
}
}
func resetLogConfig() {
PrintTimestamp = false
DebugMode = false
TimeZone, _ = time.LoadLocation("Europe/Brussels")
TimeFormat = TestingTimeFormat
}
func redirectOutput() (*bytes.Buffer, *bytes.Buffer) {
stdout := bytes.NewBufferString("")
stderr := bytes.NewBufferString("")
Stdout = stdout
Stderr = stderr
return stdout, stderr
}
func resetLogOutput() {
Stdout = os.Stdout
Stderr = os.Stderr
TimeFormat = DefaultTimeFormat
}