This commit is contained in:
Antranig Vartanian 2025-06-15 03:48:15 +04:00
commit 7b4a1a6c1f
No known key found for this signature in database
GPG key ID: DE3998662D59F21C
8 changed files with 296 additions and 0 deletions

37
tests/LoggerTest.Mod Normal file
View file

@ -0,0 +1,37 @@
MODULE LoggerTest;
IMPORT Logger;
VAR
log: Logger.Logger;
BEGIN
(* Initialize logger instance *)
log := Logger.New();
(* Show default level (INFO) in action *)
log.Info("Logger initialized");
log.InfoInt("Connected users: ", 42);
(* Prefix usage *)
log.SetPrefix("unit-test");
log.Warn("Warning with prefix");
log.WarnInt("Sessions: ", 5);
log.Error("Something went wrong");
log.ErrorInt("Error code: ", -7);
(* Switch to DEBUG level *)
log.SetLevel(Logger.DEBUG);
log.Debug("This is a debug message");
log.DebugInt("File descriptor: ", 3);
(* Clear prefix *)
log.ClearPrefix();
log.Info("Prefix cleared");
(* Suppress all output *)
log.SetLevel(-1);
log.Error("This should NOT print");
log.Debug("This should NOT print");
END LoggerTest.

9
tests/expected.txt Normal file
View file

@ -0,0 +1,9 @@
[2025-06-15T03:27:27Z] [INFO] Logger initialized
[2025-06-15T03:27:27Z] [INFO] Connected users: 42
[2025-06-15T03:27:27Z] [WARN] [unit-test] Warning with prefix
[2025-06-15T03:27:27Z] [WARN] [unit-test] Sessions: 5
[2025-06-15T03:27:27Z] [ERROR] [unit-test] Something went wrong
[2025-06-15T03:27:27Z] [ERROR] [unit-test] Error code: -7
[2025-06-15T03:27:27Z] [DEBUG] [unit-test] This is a debug message
[2025-06-15T03:27:27Z] [DEBUG] [unit-test] File descriptor: 3
[2025-06-15T03:27:27Z] [INFO] Prefix cleared

52
tests/test.awk Normal file
View file

@ -0,0 +1,52 @@
#!/usr/bin/awk -f
# test.awk: Compare actual.txt with expected.txt, allowing [TIME] as a timestamp placeholder
BEGIN {
passed = 1
line = 0
# Load expected lines from second file (expected.txt)
while ((getline expectedLine < ARGV[2]) > 0) {
line++
expected[line] = expectedLine
}
delete ARGV[2] # remove expected.txt so AWK processes only actual.txt
}
{
line++
actual = $0
expectedLine = expected[line]
# Match timestamp in the form [YYYY-MM-DDTHH:MM:SS]
if (match(actual, /\[([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})Z\] (.*)/)) {
ts_len = RLENGTH
msg = substr(actual, ts_len + 1)
} else {
printf("FAIL: Line %d: Invalid or missing timestamp: %s\n", line, actual)
passed = 0
next
}
# If expected line begins with [TIME], strip it
if (index(expectedLine, "[TIME] ") == 1) {
expectedLine = substr(expectedLine, 8)
}
if (msg != expectedLine) {
printf("FAIL: Line %d:\n expected: %s\n actual: %s\n", line, expectedLine, msg)
passed = 0
}
}
END {
if (passed) {
print "PASS: all lines matched with valid timestamps"
exit 0
} else {
print "FAIL: differences found"
exit 1
}
}