-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
643 lines (508 loc) · 16.8 KB
/
tests.cpp
File metadata and controls
643 lines (508 loc) · 16.8 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
* Ledger - Unit Tests
* https://github.com/Interlaced-Pixel/Ledger
*
* Licensed under the PolyForm Noncommercial License 1.0.0
* https://polyformproject.org/licenses/noncommercial/1.0.0
*
* Required Notice: Copyright (c) 2025 Interlaced Pixel
*/
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "vendors/doctest.h"
#include "ledger.h"
#include <sstream>
#include <thread>
#include <fstream>
#include <filesystem>
using namespace ledger;
TEST_SUITE("LogLevel")
{
TEST_CASE("log_level_to_string returns correct string representations")
{
CHECK(std::string(log_level_to_string(LOG_TRACE)) == "TRACE");
CHECK(std::string(log_level_to_string(LOG_DEBUG)) == "DEBUG");
CHECK(std::string(log_level_to_string(LOG_INFO)) == "INFO");
CHECK(std::string(log_level_to_string(LOG_WARNING)) == "WARNING");
CHECK(std::string(log_level_to_string(LOG_ERROR)) == "ERROR");
CHECK(std::string(log_level_to_string(LOG_FATAL)) == "FATAL");
}
TEST_CASE("log_level_to_string handles unknown levels")
{
CHECK(std::string(log_level_to_string(static_cast<LogLevel>(999))) == "UNKNOWN");
}
}
TEST_SUITE("StreamSink")
{
TEST_CASE("StreamSink writes messages to output stream")
{
std::ostringstream oss;
StreamSink sink(oss);
sink.write("Test message");
CHECK(oss.str() == "Test message\n");
}
TEST_CASE("StreamSink handles multiple writes")
{
std::ostringstream oss;
StreamSink sink(oss);
sink.write("First message");
sink.write("Second message");
std::string output = oss.str();
CHECK(output.find("First message") != std::string::npos);
CHECK(output.find("Second message") != std::string::npos);
}
}
TEST_SUITE("RotatingFileLogger")
{
TEST_CASE("RotatingFileLogger creates and writes to file")
{
std::string filename = "test_log.txt";
{
RotatingFileLogger logger(filename, 1000000, 5);
logger.write("Test log message");
}
// Verify file was created and contains the message
std::ifstream file(filename);
CHECK(file.is_open());
std::string line;
bool found = false;
while (std::getline(file, line))
{
if (line.find("Test log message") != std::string::npos)
{
found = true;
break;
}
}
file.close();
CHECK(found);
// Cleanup
std::filesystem::remove(filename);
}
TEST_CASE("RotatingFileLogger handles multiple writes")
{
std::string filename = "test_log_multi.txt";
{
RotatingFileLogger logger(filename, 1000000, 5);
logger.write("Message 1");
logger.write("Message 2");
logger.write("Message 3");
}
std::ifstream file(filename);
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
CHECK(content.find("Message 1") != std::string::npos);
CHECK(content.find("Message 2") != std::string::npos);
CHECK(content.find("Message 3") != std::string::npos);
std::filesystem::remove(filename);
}
}
TEST_SUITE("AsyncLogSink")
{
TEST_CASE("AsyncLogSink forwards messages to inner sink")
{
std::ostringstream oss;
auto inner_sink = std::make_unique<StreamSink>(oss);
AsyncLogSink async_sink(std::move(inner_sink), 100, AsyncLogSink::DropPolicy::DROP_NEWEST);
async_sink.write("Test async message");
async_sink.flush();
CHECK(oss.str().find("Test async message") != std::string::npos);
}
TEST_CASE("AsyncLogSink drops oldest messages when full with DROP_OLDEST policy")
{
std::ostringstream oss;
auto inner_sink = std::make_unique<StreamSink>(oss);
AsyncLogSink async_sink(std::move(inner_sink), 2, AsyncLogSink::DropPolicy::DROP_OLDEST);
async_sink.write("Message 1");
async_sink.write("Message 2");
async_sink.write("Message 3"); // Should drop Message 1
async_sink.flush();
CHECK(async_sink.dropped_count() > 0);
}
TEST_CASE("AsyncLogSink correctly reports dropped count")
{
std::ostringstream oss;
auto inner_sink = std::make_unique<StreamSink>(oss);
AsyncLogSink async_sink(std::move(inner_sink), 1, AsyncLogSink::DropPolicy::DROP_NEWEST);
async_sink.write("Message 1");
size_t initial_drops = async_sink.dropped_count();
async_sink.write("Message 2"); // Should be dropped
CHECK(async_sink.dropped_count() > initial_drops);
}
}
TEST_SUITE("LogContext")
{
TEST_CASE("LogContext stores and retrieves context values")
{
LogContextStorage::set("test_key", "test_value");
CHECK(LogContextStorage::get("test_key") == "test_value");
LogContextStorage::remove("test_key");
}
TEST_CASE("LogContext RAII automatically removes context on destruction")
{
CHECK(LogContextStorage::get("temp_key").empty());
{
LogContext ctx;
ctx.add("temp_key", "temp_value");
CHECK(LogContextStorage::get("temp_key") == "temp_value");
}
CHECK(LogContextStorage::get("temp_key").empty());
}
TEST_CASE("LogContext supports multiple key-value pairs")
{
{
LogContext ctx;
ctx.add("key1", "value1");
ctx.add("key2", "value2");
ctx.add("key3", "value3");
CHECK(LogContextStorage::get("key1") == "value1");
CHECK(LogContextStorage::get("key2") == "value2");
CHECK(LogContextStorage::get("key3") == "value3");
}
CHECK(LogContextStorage::get("key1").empty());
CHECK(LogContextStorage::get("key2").empty());
CHECK(LogContextStorage::get("key3").empty());
}
TEST_CASE("LogContext can add numeric values")
{
{
LogContext ctx;
ctx.add("user_id", 12345);
ctx.add("port", 8080);
CHECK(LogContextStorage::get("user_id") == "12345");
CHECK(LogContextStorage::get("port") == "8080");
}
}
}
TEST_SUITE("DefaultLogFormatter")
{
TEST_CASE("DefaultLogFormatter produces standard format")
{
DefaultLogFormatter formatter;
std::tm time_info = {};
time_info.tm_year = 125; // 2025
time_info.tm_mon = 1; // February
time_info.tm_mday = 13;
std::string result = formatter.format(LOG_INFO, "Test message", time_info);
CHECK(result.find("Test message") != std::string::npos);
CHECK(result.find("[INFO]") != std::string::npos);
CHECK(result.find("2025-02-13") != std::string::npos);
}
TEST_CASE("DefaultLogFormatter includes file and line information")
{
DefaultLogFormatter formatter;
std::tm time_info = {};
std::string result = formatter.format(LOG_DEBUG, "Test", time_info, "test.cpp", 42);
CHECK(result.find("test.cpp:42") != std::string::npos);
}
TEST_CASE("DefaultLogFormatter includes prefix when set")
{
DefaultLogFormatter formatter(TimestampFormat::STANDARD, "MyApp");
std::tm time_info = {};
std::string result = formatter.format(LOG_INFO, "Test", time_info);
CHECK(result.find("MyApp") != std::string::npos);
}
TEST_CASE("DefaultLogFormatter supports ISO8601 timestamp format")
{
DefaultLogFormatter formatter(TimestampFormat::ISO8601);
std::tm time_info = {};
time_info.tm_year = 125;
time_info.tm_mon = 1;
time_info.tm_mday = 13;
std::string result = formatter.format(LOG_INFO, "Test", time_info);
CHECK(result.find("2025-02-13T") != std::string::npos);
CHECK(result.find("Z") != std::string::npos);
}
TEST_CASE("DefaultLogFormatter supports NONE timestamp format")
{
DefaultLogFormatter formatter(TimestampFormat::NONE);
std::tm time_info = {};
std::string result = formatter.format(LOG_INFO, "Test message", time_info);
CHECK(result.find("Test message") != std::string::npos);
CHECK(result.find("[INFO]") != std::string::npos);
// Should not have timestamp
CHECK(result.find("[") == result.find("[INFO]"));
}
}
TEST_SUITE("JSONLogFormatter")
{
TEST_CASE("JSONLogFormatter produces valid JSON format")
{
JSONLogFormatter formatter;
std::tm time_info = {};
std::string result = formatter.format(LOG_INFO, "Test message", time_info);
CHECK(result.find("{") != std::string::npos);
CHECK(result.find("}") != std::string::npos);
CHECK(result.find("\"timestamp\"") != std::string::npos);
CHECK(result.find("\"level\"") != std::string::npos);
CHECK(result.find("\"message\"") != std::string::npos);
}
TEST_CASE("JSONLogFormatter escapes JSON special characters")
{
JSONLogFormatter formatter;
std::tm time_info = {};
std::string result = formatter.format(LOG_INFO, "Message with \"quotes\" and \\backslash", time_info);
// Should contain escaped quotes and backslashes
bool escaped_quotes_found = result.find("\\\"") != std::string::npos;
bool quoted_quotes_not_found = result.find("\"quotes\"") == std::string::npos;
CHECK(escaped_quotes_found);
CHECK(quoted_quotes_not_found);
}
TEST_CASE("JSONLogFormatter includes file and line in JSON")
{
JSONLogFormatter formatter;
std::tm time_info = {};
std::string result = formatter.format(LOG_ERROR, "Error", time_info, "error.cpp", 100);
CHECK(result.find("\"file\"") != std::string::npos);
CHECK(result.find("\"line\"") != std::string::npos);
CHECK(result.find("100") != std::string::npos);
}
}
TEST_SUITE("Logger - Basic Functionality")
{
TEST_CASE("Logger sets log level")
{
Ledger::set_level(LOG_WARNING);
// Note: We can't directly verify the level, but we can test that it affects logging
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
Ledger::info("Should not appear");
Ledger::warning("Should appear");
std::string output = oss.str();
CHECK(output.find("Should appear") != std::string::npos);
}
TEST_CASE("Logger logs info messages")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
Ledger::set_level(LOG_INFO);
Ledger::info("Test info message");
std::string output = oss.str();
CHECK(output.find("Test info message") != std::string::npos);
CHECK(output.find("[INFO]") != std::string::npos);
}
TEST_CASE("Logger logs warning messages")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
Ledger::warning("Test warning message");
std::string output = oss.str();
CHECK(output.find("Test warning message") != std::string::npos);
CHECK(output.find("[WARNING]") != std::string::npos);
}
TEST_CASE("Logger logs error messages")
{
std::ostringstream oss;
Ledger::set_output_streams(std::cout, oss);
Ledger::error("Test error message");
std::string output = oss.str();
CHECK(output.find("Test error message") != std::string::npos);
CHECK(output.find("[ERROR]") != std::string::npos);
}
}
TEST_SUITE("Logger - Structured Logging")
{
TEST_CASE("Logger supports key-value pairs")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
Ledger::info("User action", "user_id", 123, "action", "login");
std::string output = oss.str();
CHECK(output.find("User action") != std::string::npos);
CHECK(output.find("user_id=123") != std::string::npos);
CHECK(output.find("action=login") != std::string::npos);
}
TEST_CASE("Logger context is included in messages")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
{
LogContext ctx;
ctx.add("request_id", "req-123");
Ledger::info("Processing request");
}
std::string output = oss.str();
CHECK(output.find("request_id=req-123") != std::string::npos);
}
}
TEST_SUITE("Logger - Thread Safety")
{
TEST_CASE("Logger handles concurrent logging from multiple threads")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
std::vector<std::thread> threads;
for (int i = 0; i < 5; ++i)
{
threads.emplace_back([i]() {
for (int j = 0; j < 10; ++j)
{
Ledger::info("Thread message", "thread_id", i, "iteration", j);
}
});
}
for (auto &t : threads)
{
t.join();
}
std::string output = oss.str();
// Should have logged 50 messages without crashing
CHECK(output.find("Thread message") != std::string::npos);
}
}
TEST_SUITE("Logger - File Logging")
{
TEST_CASE("Logger can write to file")
{
std::string filename = "test_logger.log";
Ledger::set_file_logging(filename, 1000000, 5);
Ledger::info("File logging test message");
std::ifstream file(filename);
std::string line;
bool found = false;
while (std::getline(file, line))
{
if (line.find("File logging test message") != std::string::npos)
{
found = true;
break;
}
}
file.close();
CHECK(found);
Ledger::set_file_logging(nullptr);
std::filesystem::remove(filename);
}
}
TEST_SUITE("Logger - Formatters")
{
TEST_CASE("Logger can use custom formatter")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
auto json_formatter = std::make_unique<JSONLogFormatter>();
Ledger::set_formatter(std::move(json_formatter));
Ledger::info("JSON formatted message");
std::string output = oss.str();
CHECK(output.find("{") != std::string::npos);
CHECK(output.find("\"message\"") != std::string::npos);
Ledger::set_formatter(nullptr);
}
TEST_CASE("Logger can use custom formatter with prefix")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
auto formatter = std::make_unique<DefaultLogFormatter>(TimestampFormat::STANDARD, "APP");
Ledger::set_formatter(std::move(formatter));
Ledger::info("Prefixed message");
std::string output = oss.str();
CHECK(output.find("APP") != std::string::npos);
Ledger::set_formatter(nullptr);
}
}
TEST_SUITE("Logger - Configuration")
{
TEST_CASE("LoggerConfigBuilder creates valid configuration")
{
auto config = Ledger::LoggerConfigBuilder()
.set_level(LOG_DEBUG)
.add_stream_sink(std::cout)
.set_formatter(std::make_unique<JSONLogFormatter>())
.build();
CHECK(config.level == LOG_DEBUG);
CHECK(config.sinks.size() > 0);
CHECK(config.formatter != nullptr);
}
TEST_CASE("Logger can be configured with fluent API")
{
std::ostringstream oss;
auto config = Ledger::LoggerConfigBuilder()
.set_level(LOG_INFO)
.add_stream_sink(oss)
.build();
Ledger::configure(std::move(config));
Ledger::info("Configured logger test");
CHECK(oss.str().find("Configured logger test") != std::string::npos);
}
}
TEST_SUITE("Logger - Category Loggers")
{
TEST_CASE("CategoryLogger can be obtained and used")
{
auto logger = Ledger::get("database");
// Note: We test that it doesn't crash and is functional
CHECK(true);
}
TEST_CASE("Different categories can have different configurations")
{
auto db_logger = Ledger::get("database");
auto api_logger = Ledger::get("api");
// Configure database logger
Ledger::LoggerRegistry::set_config(
"database",
Ledger::LoggerConfigBuilder()
.set_level(LOG_DEBUG)
.build());
CHECK(Ledger::LoggerRegistry::has_config("database"));
}
}
TEST_SUITE("Logger - Edge Cases")
{
TEST_CASE("Logger handles empty messages")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
Ledger::info("");
// Should not crash and should log something
CHECK(oss.str().find("[INFO]") != std::string::npos);
}
TEST_CASE("Logger handles very long messages")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
std::string long_message(10000, 'x');
Ledger::info(long_message);
CHECK(oss.str().find("xxx") != std::string::npos);
}
TEST_CASE("Logger handles special characters in messages")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
Ledger::info("Message with special chars: \n\t\r\\\"");
CHECK(oss.str().find("special") != std::string::npos);
}
}
TEST_SUITE("Macros")
{
TEST_CASE("LOG_INFO macro works")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
LOG_INFO("Macro test message");
std::string output = oss.str();
CHECK(output.find("Macro test message") != std::string::npos);
}
TEST_CASE("LOG_WARNING macro works")
{
std::ostringstream oss;
Ledger::set_output_streams(oss, oss);
LOG_WARNING("Warning via macro");
std::string output = oss.str();
CHECK(output.find("Warning via macro") != std::string::npos);
}
TEST_CASE("LOG_ERROR macro works")
{
std::ostringstream oss;
Ledger::set_output_streams(std::cout, oss);
LOG_ERROR("Error via macro");
std::string output = oss.str();
CHECK(output.find("Error via macro") != std::string::npos);
}
TEST_CASE("LOG_FATAL macro works")
{
std::ostringstream oss;
Ledger::set_output_streams(std::cout, oss);
LOG_FATAL("Fatal via macro");
std::string output = oss.str();
CHECK(output.find("Fatal via macro") != std::string::npos);
}
}