-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogcoe_test.cpp
More file actions
195 lines (147 loc) · 5.29 KB
/
logcoe_test.cpp
File metadata and controls
195 lines (147 loc) · 5.29 KB
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
#include <gtest/gtest.h>
#include <logcoe.hpp>
#include <fstream>
#include <sstream>
#include <filesystem>
#include <string>
#include <regex>
class LogcoeTest : public ::testing::Test
{
protected:
std::stringstream testStream;
std::string testFilename;
void SetUp() override
{
testFilename = "test_logfile_" + std::to_string(std::chrono::system_clock::now().time_since_epoch().count()) + ".log";
while(logcoe::isInitialized()) { logcoe::shutdown(); }
}
void TearDown() override
{
while(logcoe::isInitialized()) { logcoe::shutdown(); }
if (std::filesystem::exists(testFilename))
std::filesystem::remove(testFilename);
}
std::string readLogFile(const std::string &filename)
{
std::ifstream file(filename);
if (!file.is_open())
return "";
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
bool matchesLogPattern(const std::string &text, logcoe::LogLevel level,
const std::string &message, const std::string &source = "")
{
std::string levelStr;
switch (level)
{
case logcoe::LogLevel::DEBUG:
levelStr = "DEBUG";
break;
case logcoe::LogLevel::INFO:
levelStr = "INFO";
break;
case logcoe::LogLevel::WARNING:
levelStr = "WARNING";
break;
case logcoe::LogLevel::ERROR:
levelStr = "ERROR";
break;
default:
levelStr = "NONE";
break;
}
std::string pattern = "\\[.*?\\] \\[" + levelStr + "\\]";
if (!source.empty())
pattern += " \\[" + source + "\\]";
pattern += ": " + message;
std::regex re(pattern);
return std::regex_search(text, re);
}
};
TEST_F(LogcoeTest, DefaultInitialization)
{
logcoe::initialize();
EXPECT_EQ(logcoe::getLogLevel(), logcoe::LogLevel::DEBUG);
logcoe::info("Test info message");
}
TEST_F(LogcoeTest, CustomInitialization)
{
logcoe::initialize(logcoe::LogLevel::INFO, "", true, true, testFilename);
EXPECT_EQ(logcoe::getLogLevel(), logcoe::LogLevel::INFO);
logcoe::info("Test debug message");
EXPECT_TRUE(std::filesystem::exists(testFilename));
std::string fileContent = readLogFile(testFilename);
EXPECT_TRUE(matchesLogPattern(fileContent, logcoe::LogLevel::INFO, "Test debug message"));
}
TEST_F(LogcoeTest, LogLevelFiltering)
{
logcoe::initialize(logcoe::LogLevel::WARNING);
logcoe::setConsoleOutput(testStream);
logcoe::debug("Debug message");
logcoe::info("Info message");
logcoe::warning("Warning message");
logcoe::error("Error message");
std::string output = testStream.str();
EXPECT_FALSE(matchesLogPattern(output, logcoe::LogLevel::DEBUG, "Debug message"));
EXPECT_FALSE(matchesLogPattern(output, logcoe::LogLevel::INFO, "Info message"));
EXPECT_TRUE(matchesLogPattern(output, logcoe::LogLevel::WARNING, "Warning message"));
EXPECT_TRUE(matchesLogPattern(output, logcoe::LogLevel::ERROR, "Error message"));
}
TEST_F(LogcoeTest, ChangeLogLevel)
{
logcoe::initialize(logcoe::LogLevel::ERROR);
EXPECT_EQ(logcoe::getLogLevel(), logcoe::LogLevel::ERROR);
logcoe::setLogLevel(logcoe::LogLevel::DEBUG);
EXPECT_EQ(logcoe::getLogLevel(), logcoe::LogLevel::DEBUG);
}
TEST_F(LogcoeTest, ConsoleRedirection)
{
logcoe::initialize();
logcoe::setConsoleOutput(testStream);
logcoe::info("Test message");
std::string output = testStream.str();
EXPECT_TRUE(matchesLogPattern(output, logcoe::LogLevel::INFO, "Test message"));
}
TEST_F(LogcoeTest, FileOutput)
{
logcoe::initialize();
EXPECT_TRUE(logcoe::setFileOutput(testFilename));
logcoe::info("File test message");
std::string fileContent = readLogFile(testFilename);
EXPECT_TRUE(matchesLogPattern(fileContent, logcoe::LogLevel::INFO, "File test message"));
logcoe::disableFileOutput();
logcoe::info("This shouldn't be in the file");
fileContent = readLogFile(testFilename);
EXPECT_FALSE(matchesLogPattern(fileContent, logcoe::LogLevel::INFO, "This shouldn't be in the file"));
}
TEST_F(LogcoeTest, DisableConsole)
{
logcoe::initialize();
logcoe::setConsoleOutput(testStream);
logcoe::info("Before disable");
logcoe::disableConsoleOutput();
logcoe::info("After disable");
std::string output = testStream.str();
EXPECT_TRUE(matchesLogPattern(output, logcoe::LogLevel::INFO, "Before disable"));
EXPECT_FALSE(matchesLogPattern(output, logcoe::LogLevel::INFO, "After disable"));
}
TEST_F(LogcoeTest, SourceField)
{
logcoe::initialize();
logcoe::setConsoleOutput(testStream);
logcoe::info("Message with source", "TestSource");
std::string output = testStream.str();
EXPECT_TRUE(matchesLogPattern(output, logcoe::LogLevel::INFO, "Message with source", "TestSource"));
}
TEST_F(LogcoeTest, TimeFormat)
{
logcoe::initialize();
logcoe::setConsoleOutput(testStream);
logcoe::setTimeFormat("%H:%M:%S");
logcoe::info("Custom time format");
std::string output = testStream.str();
std::regex timePattern("\\[\\d{2}:\\d{2}:\\d{2}\\]");
EXPECT_TRUE(std::regex_search(output, timePattern));
}