-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
127 lines (105 loc) · 3.81 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
package logger
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/fatih/color"
)
// TestFormatSeverity
// Unit test to verify that the formatSeverity method correctly formats strings with the given
// colour.
func TestFormatSeverity(t *testing.T) {
magenta := color.New(color.FgMagenta).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
boldRed := color.New(color.FgHiRed).Add(color.Bold).SprintFunc()
testStrings := []struct {
text string
expected string
}{
{text: "DEBUG", expected: magenta("DEBUG")},
{text: "INFO", expected: green("INFO")},
{text: "WARN", expected: yellow("WARN")},
{text: "ERROR", expected: red("ERROR")},
{text: "FATAL", expected: boldRed("FATAL")},
}
for _, s := range testStrings {
result := formatSeverity(s.text)
if result != s.expected {
t.Errorf(
"Expected formatted severity for Severity(%s) to return '%s', got '%s' instead.",
s.text,
s.expected,
result,
)
}
}
}
// TestIsNullOrEmptyString
// Unit test to verify that the IsNullOrEmptyString method correctly identifies null or empty strings.
func TestIsNullOrEmptyString(t *testing.T) {
str := ""
if !isNullOrEmptyString(str) {
t.Errorf("Expected input string '%s' to result in 'true', got '%v' instead.", str, isNullOrEmptyString(str))
}
str = "Hello world!"
if isNullOrEmptyString(str) {
t.Errorf("Expected input string '%s' to result in 'false', got '%v' instead.", str, isNullOrEmptyString(str))
}
}
// MockLoggerInterface is a mock implementation of LoggerInterface for testing purposes.
type MockLoggerInterface struct {
Prefix string
}
// MockLogMessageBuffer
// Helper function to capture the output of LogMessage.
func (l *MockLoggerInterface) MockLogMessageBuffer(message, severity string) *bytes.Buffer {
var buf bytes.Buffer
l.MockLogMessage(&buf, message, severity)
return &buf
}
// MockLogMessage
// Modified version of the original LogMessage method to write to a specified buffer.
func (l *MockLoggerInterface) MockLogMessage(output *bytes.Buffer, message, severity string) {
if isNullOrEmptyString(message) {
l.MockLogMessage(output, "No message string passed to logger interface.", "ERROR")
}
if isNullOrEmptyString(severity) {
l.MockLogMessage(output, "No severity string passed to logger interface.", "ERROR")
}
logTime := time.Now().Format("2006-01-02 15:04:05")
if severity == "FATAL" {
fmt.Fprintf(output, base, logTime, formatSeverity(severity), l.Prefix, cyan(message))
return
}
fmt.Fprintf(output, base, logTime, formatSeverity(severity), l.Prefix, cyan(message))
}
// TestLogMessage
// Unit test to verify through the implementation of mock functionality that the `LogMessage`
// method correctly prints logs to the standard output.
func TestLogMessage(t *testing.T) {
// Create a MockLoggerInterface instance
mockLogger := &MockLoggerInterface{
Prefix: "TestPrefix",
}
// Test case 1: Log a regular message
message := "Test Message"
severity := "INFO"
expectedOutput := fmt.Sprintf(base, time.Now().Format("2006-01-02 15:04:05"), formatSeverity(severity), mockLogger.Prefix, cyan(message))
outputBuffer := mockLogger.MockLogMessageBuffer(message, severity)
actualOutput := outputBuffer.String()
if actualOutput != expectedOutput {
t.Errorf("Expected output: %s\nActual output: %s", expectedOutput, actualOutput)
}
// Test case 2: Log a FATAL message
message = "Fatal Error"
severity = "FATAL"
expectedOutput = fmt.Sprintf(base, time.Now().Format("2006-01-02 15:04:05"), formatSeverity(severity), mockLogger.Prefix, cyan(message))
outputBuffer = mockLogger.MockLogMessageBuffer(message, severity)
actualOutput = outputBuffer.String()
if actualOutput != expectedOutput {
t.Errorf("Expected output: %s\nActual output: %s", expectedOutput, actualOutput)
}
}