31
31
32
32
#include < cassert>
33
33
#include < cstdlib>
34
+ #include < iostream>
34
35
#include < regex>
35
36
#include < sstream>
37
+ #include < stdexcept>
36
38
37
39
namespace juzzlin ::StreamTest {
38
40
41
+ void assertString (std::stringstream & stream, const std::string & message)
42
+ {
43
+ if (stream.str ().find (message) == std::string::npos) {
44
+ throw std::runtime_error (" ERROR!!: '" + message + " ' not found in '" + stream.str () + " '" );
45
+ }
46
+ }
47
+
39
48
void assertMessage (std::stringstream & stream, const std::string & message, const std::string & timestampSeparator)
40
49
{
41
- assert (stream.str ().find (message) != std::string::npos);
42
- assert (stream.str ().find (timestampSeparator) != std::string::npos);
50
+ assertString (stream, message);
51
+ assertString (stream, timestampSeparator);
52
+ }
53
+
54
+ void assertNotString (std::stringstream & stream, const std::string & message)
55
+ {
56
+ if (stream.str ().find (message) != std::string::npos) {
57
+ throw std::runtime_error (" ERROR!!: '" + message + " ' was found in '" + stream.str () + " '" );
58
+ }
43
59
}
44
60
45
61
void assertNotMessage (std::stringstream & stream, const std::string & message, const std::string & timestampSeparator)
46
62
{
47
- assert (stream. str (). find (message) == std::string::npos );
48
- assert (stream. str (). find (timestampSeparator) == std::string::npos );
63
+ assertNotString (stream, message );
64
+ assertNotString (stream, timestampSeparator );
49
65
}
50
66
51
67
void testFatal_noneLoggingLevel_shouldNotPrintMessage (const std::string & message, const std::string & timestampSeparator)
@@ -156,6 +172,66 @@ void testTrace_traceLoggingLevel_shouldPrintMessage(const std::string & message,
156
172
assertMessage (ss, message, timestampSeparator);
157
173
}
158
174
175
+ void testTag_fatalLevel_shouldPrintTag (const std::string & message, const std::string & timestampSeparator)
176
+ {
177
+ std::stringstream ss;
178
+ L::setStream (L::Level::Fatal, ss);
179
+ L::setLoggingLevel (L::Level::Fatal);
180
+ const std::string tag = " TAG" ;
181
+ L (tag).fatal () << message;
182
+ assertMessage (ss, tag + " : " + message, timestampSeparator);
183
+ }
184
+
185
+ void testTag_errorLevel_shouldPrintTag (const std::string & message, const std::string & timestampSeparator)
186
+ {
187
+ std::stringstream ss;
188
+ L::setStream (L::Level::Error, ss);
189
+ L::setLoggingLevel (L::Level::Error);
190
+ const std::string tag = " TAG" ;
191
+ L (tag).fatal () << message;
192
+ assertMessage (ss, tag + " : " + message, timestampSeparator);
193
+ }
194
+
195
+ void testTag_warningLevel_shouldPrintTag (const std::string & message, const std::string & timestampSeparator)
196
+ {
197
+ std::stringstream ss;
198
+ L::setStream (L::Level::Warning, ss);
199
+ L::setLoggingLevel (L::Level::Warning);
200
+ const std::string tag = " TAG" ;
201
+ L (tag).fatal () << message;
202
+ assertMessage (ss, tag + " : " + message, timestampSeparator);
203
+ }
204
+
205
+ void testTag_infoLevel_shouldPrintTag (const std::string & message, const std::string & timestampSeparator)
206
+ {
207
+ std::stringstream ss;
208
+ L::setStream (L::Level::Info, ss);
209
+ L::setLoggingLevel (L::Level::Info);
210
+ const std::string tag = " TAG" ;
211
+ L (tag).fatal () << message;
212
+ assertMessage (ss, tag + " : " + message, timestampSeparator);
213
+ }
214
+
215
+ void testTag_debugLevel_shouldPrintTag (const std::string & message, const std::string & timestampSeparator)
216
+ {
217
+ std::stringstream ss;
218
+ L::setStream (L::Level::Debug, ss);
219
+ L::setLoggingLevel (L::Level::Debug);
220
+ const std::string tag = " TAG" ;
221
+ L (tag).fatal () << message;
222
+ assertMessage (ss, tag + " : " + message, timestampSeparator);
223
+ }
224
+
225
+ void testTag_traceLevel_shouldPrintTag (const std::string & message, const std::string & timestampSeparator)
226
+ {
227
+ std::stringstream ss;
228
+ L::setStream (L::Level::Trace, ss);
229
+ L::setLoggingLevel (L::Level::Trace);
230
+ const std::string tag = " TAG" ;
231
+ L (tag).fatal () << message;
232
+ assertMessage (ss, tag + " : " + message, timestampSeparator);
233
+ }
234
+
159
235
void initializeLogger (const std::string & timestampSeparator)
160
236
{
161
237
L::enableEchoMode (true );
@@ -168,9 +244,10 @@ void testTimestampMode_none_shouldNotPrintTimestamp(const std::string & message)
168
244
L::setTimestampMode (L::TimestampMode::None);
169
245
std::stringstream ss;
170
246
L::setStream (L::Level::Info, ss);
247
+ L::setLoggingLevel (L::Level::Info);
171
248
L ().info () << message;
172
- assert (ss. str (). find (message) != std::string::npos );
173
- assert (ss. str (). find ( " ## " ) == std::string::npos );
249
+ assertString (ss, message );
250
+ assertNotString (ss, " ## " );
174
251
}
175
252
176
253
void testTimestampMode_dateTime_shouldPrintDateTimeTimestamp (const std::string & message)
@@ -284,6 +361,18 @@ void runTests()
284
361
285
362
testTrace_traceLoggingLevel_shouldPrintMessage (message, timestampSeparator);
286
363
364
+ testTag_fatalLevel_shouldPrintTag (message, timestampSeparator);
365
+
366
+ testTag_errorLevel_shouldPrintTag (message, timestampSeparator);
367
+
368
+ testTag_warningLevel_shouldPrintTag (message, timestampSeparator);
369
+
370
+ testTag_infoLevel_shouldPrintTag (message, timestampSeparator);
371
+
372
+ testTag_debugLevel_shouldPrintTag (message, timestampSeparator);
373
+
374
+ testTag_traceLevel_shouldPrintTag (message, timestampSeparator);
375
+
287
376
testTimestampMode_none_shouldNotPrintTimestamp (message);
288
377
289
378
testTimestampMode_dateTime_shouldPrintDateTimeTimestamp (message);
0 commit comments