forked from mocktools/go-smtp-mock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_test.go
153 lines (125 loc) · 4.34 KB
/
logger_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
package smtpmock
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewLogger(t *testing.T) {
t.Run("when log to stdout, server activity enabled", func(t *testing.T) {
isLogToStdoutEnabled, logServerActivity := true, true
logger := &eventLogger{
logToStdout: isLogToStdoutEnabled,
logServerActivity: logServerActivity,
flag: logFlag,
stdout: os.Stdout,
stderr: os.Stderr,
}
assert.EqualValues(t, logger, newLogger(isLogToStdoutEnabled, logServerActivity))
})
t.Run("when log to stdout, server activity disabled", func(t *testing.T) {
isLogToStdoutEnabled, logServerActivity := false, false
logger := &eventLogger{
flag: logFlag,
stdout: os.Stdout,
stderr: os.Stderr,
}
assert.EqualValues(t, logger, newLogger(isLogToStdoutEnabled, logServerActivity))
})
}
func TestEventLoggerInfoActivity(t *testing.T) {
logMessage := "log message"
t.Run("when log to stdout and server activity enabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(true, true)
logger.stdout = &buf
logger.infoActivity(logMessage) // initializes and memoizes INFO logger during first function calling
assert.Regexp(t, loggerMessageRegex(infoLogLevel, logMessage), buf.String())
assert.NotNil(t, logger.eventInfo)
memoizedInfoLogger := logger.eventInfo
logger.infoActivity(logMessage)
assert.Same(t, memoizedInfoLogger, logger.eventInfo)
})
t.Run("when log to stdout disabled, server activity enabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(false, true)
logger.stdout = &buf
logger.infoActivity(logMessage)
assert.Nil(t, logger.eventInfo)
assert.Empty(t, buf.String())
})
t.Run("when log to stdout enabled, server activity disabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(true, false)
logger.stdout = &buf
logger.infoActivity(logMessage)
assert.Nil(t, logger.eventInfo)
assert.Empty(t, buf.String())
})
}
func TestEventLoggerInfo(t *testing.T) {
logMessage := "log message"
t.Run("when log to stdout enabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(true, false)
logger.stdout = &buf
logger.info(logMessage) // initializes and memoizes INFO logger during first function calling
assert.Regexp(t, loggerMessageRegex(infoLogLevel, logMessage), buf.String())
assert.NotNil(t, logger.eventInfo)
memoizedInfoLogger := logger.eventInfo
logger.info(logMessage)
assert.Same(t, memoizedInfoLogger, logger.eventInfo)
})
t.Run("when log to stdout disabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(false, false)
logger.stdout = &buf
logger.info(logMessage)
assert.Nil(t, logger.eventInfo)
assert.Empty(t, buf.String())
})
}
func TestEventLoggerWarning(t *testing.T) {
logMessage := "log message"
t.Run("when log to stdout enabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(true, false)
logger.stdout = &buf
logger.warning(logMessage) // initializes and memoizes WARNING logger during first function calling
assert.Regexp(t, loggerMessageRegex(warningLogLevel, logMessage), buf.String())
assert.NotNil(t, logger.eventWarning)
memoizedWarningLogger := logger.eventWarning
logger.warning(logMessage)
assert.Same(t, memoizedWarningLogger, logger.eventWarning)
})
t.Run("when log to stdout disabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(false, false)
logger.stdout = &buf
logger.warning(logMessage)
assert.Nil(t, logger.eventWarning)
assert.Empty(t, buf.String())
})
}
func TestEventLoggerError(t *testing.T) {
logMessage := "log message"
t.Run("when log to stdout enabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(true, false)
logger.stderr = &buf
logger.error(logMessage) // initializes and memoizes ERROR logger during first function calling
assert.Regexp(t, loggerMessageRegex(errorLogLevel, logMessage), buf.String())
assert.NotNil(t, logger.eventError)
memoizedErrorLogger := logger.eventError
logger.error(logMessage)
assert.Same(t, memoizedErrorLogger, logger.eventError)
})
t.Run("when log to stdout disabled", func(t *testing.T) {
var buf bytes.Buffer
logger := newLogger(false, false)
logger.stderr = &buf
logger.error(logMessage)
assert.Nil(t, logger.eventError)
assert.Empty(t, buf.String())
})
}