-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLogTest.Mod
More file actions
289 lines (248 loc) · 8.48 KB
/
Copy pathLogTest.Mod
File metadata and controls
289 lines (248 loc) · 8.48 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
(** LogTest.Mod - Test module for Log
Copyright (C) 2025 Artemis Project
Released under The 3-Clause BSD License.
See https://opensource.org/licenses/BSD-3-Clause
This module tests the Log module functionality and demonstrates
proper usage of the opaque Logger type.
*)
MODULE LogTest;
IMPORT T := Tests, Log, Chars, Files;
CONST
OVERLONG_MSG_LEN = Chars.MAXSTR + 10;
VAR
ts: T.TestSet;
PROCEDURE TestLoggerCreation(): BOOLEAN;
VAR
test: BOOLEAN;
logger: Log.Logger;
level, destination: INTEGER;
timestampEnabled: BOOLEAN;
BEGIN
test := TRUE;
(* Test creating a console logger *)
logger := Log.New(Log.INFO, Log.CONSOLE, "");
T.ExpectedBool(logger # NIL, TRUE, "Logger created successfully", test);
(* Test accessor procedures - information hiding in action *)
level := Log.GetLevel(logger);
T.ExpectedInt(Log.INFO, level, "GetLevel returns correct value", test);
destination := Log.GetDestination(logger);
T.ExpectedInt(Log.CONSOLE, destination, "GetDestination returns correct value", test);
timestampEnabled := Log.GetTimestamp(logger);
T.ExpectedBool(TRUE, timestampEnabled, "Timestamp enabled by default", test);
(* Test setting properties *)
Log.SetLevel(logger, Log.WARNING);
level := Log.GetLevel(logger);
T.ExpectedInt(Log.WARNING, level, "SetLevel updates correctly", test);
Log.SetTimestamp(logger, FALSE);
timestampEnabled := Log.GetTimestamp(logger);
T.ExpectedBool(FALSE, timestampEnabled, "SetTimestamp updates correctly", test);
Log.Close(logger);
RETURN test
END TestLoggerCreation;
PROCEDURE TestLogLevels(): BOOLEAN;
VAR
test: BOOLEAN;
logger: Log.Logger;
BEGIN
test := TRUE;
(* Create logger with DEBUG level *)
logger := Log.New(Log.DEBUG, Log.CONSOLE, "");
(* Test that all levels work *)
Log.Debug(logger, "Debug message");
Log.Info(logger, "Info message");
Log.Warning(logger, "Warning message");
Log.Error(logger, "Error message");
(* Set higher level and test filtering *)
Log.SetLevel(logger, Log.ERROR);
(* These should be filtered out (not cause crashes) *)
Log.Debug(logger, "Should not appear");
Log.Info(logger, "Should not appear");
Log.Warning(logger, "Should not appear");
(* This should appear *)
Log.Error(logger, "This should appear");
Log.Close(logger);
RETURN test
END TestLogLevels;
PROCEDURE TestFileLogging(): BOOLEAN;
VAR
test: BOOLEAN;
logger: Log.Logger;
res : INTEGER;
BEGIN
test := TRUE;
(* Test file logging - should fallback to console if file creation fails *)
logger := Log.New(Log.INFO, Log.FILE, "test.log");
T.ExpectedBool(logger # NIL, TRUE, "File logger created", test);
Log.Debug(logger, "This won't show up");
Log.Info(logger, "Test file logging message");
Log.Warning(logger, "Test file logging message");
Log.Error(logger, "Test file logging message");
Log.Close(logger);
Files.Delete("test.log", res);
RETURN test
END TestFileLogging;
PROCEDURE TestTruncation(): BOOLEAN;
VAR
test: BOOLEAN;
logger: Log.Logger;
longMsg: ARRAY OVERLONG_MSG_LEN OF CHAR; (* Longer than MAX_MESSAGE_LEN *)
i: INTEGER;
BEGIN
test := TRUE;
FOR i := 0 TO LEN(longMsg)-2 DO
longMsg[i] := "A"
END;
longMsg[LEN(longMsg)-1] := 0X;
logger := Log.New(Log.INFO, Log.CONSOLE, "");
Log.Info(logger, longMsg);
Log.Close(logger);
(* Manual check: No crash, output is truncated, string is valid *)
RETURN test
END TestTruncation;
(* Corner cases and extended tests *)
(* Test passing NIL as logger to all exported procedures *)
PROCEDURE TestNilLogger(): BOOLEAN;
VAR test: BOOLEAN;
nilLogger: Log.Logger;
level, dest: INTEGER;
tsEnabled: BOOLEAN;
BEGIN
test := TRUE;
nilLogger := NIL;
(* Should not crash or output *)
Log.Debug(nilLogger, "Should not crash");
Log.Info(nilLogger, "Should not crash");
Log.Warning(nilLogger, "Should not crash");
Log.Error(nilLogger, "Should not crash");
Log.Close(nilLogger);
level := Log.GetLevel(nilLogger);
dest := Log.GetDestination(nilLogger);
tsEnabled := Log.GetTimestamp(nilLogger);
T.ExpectedInt(-1, level, "GetLevel(NIL) returns -1", test);
T.ExpectedInt(-1, dest, "GetDestination(NIL) returns -1", test);
T.ExpectedBool(FALSE, tsEnabled, "GetTimestamp(NIL) returns FALSE", test);
RETURN test
END TestNilLogger;
(* Test invalid log level *)
PROCEDURE TestInvalidLogLevel(): BOOLEAN;
VAR test: BOOLEAN; logger: Log.Logger;
BEGIN
test := TRUE;
logger := Log.New(-1, Log.CONSOLE, "");
T.ExpectedBool(logger = NIL, TRUE, "Logger creation fails for invalid level (low)", test);
logger := Log.New(99, Log.CONSOLE, "");
T.ExpectedBool(logger = NIL, TRUE, "Logger creation fails for invalid level (high)", test);
RETURN test
END TestInvalidLogLevel;
(* Test invalid destination *)
PROCEDURE TestInvalidDestination(): BOOLEAN;
VAR test: BOOLEAN; logger: Log.Logger;
BEGIN
test := TRUE;
logger := Log.New(Log.INFO, -1, "");
T.ExpectedBool(logger = NIL, TRUE, "Logger creation fails for invalid destination (low)", test);
logger := Log.New(Log.INFO, 99, "");
T.ExpectedBool(logger = NIL, TRUE, "Logger creation fails for invalid destination (high)", test);
RETURN test
END TestInvalidDestination;
(* Test file creation failure (simulate with invalid filename) *)
PROCEDURE TestFileCreationFailure(): BOOLEAN;
VAR test: BOOLEAN; logger: Log.Logger;
BEGIN
test := TRUE;
logger := Log.New(Log.INFO, Log.FILE, "/invalid/path/shouldfail.log");
T.ExpectedBool(logger # NIL, TRUE, "Logger fallback to console on file creation failure", test);
Log.Info(logger, "Should fallback to console");
Log.Close(logger);
RETURN test
END TestFileCreationFailure;
(* Test filename truncation *)
PROCEDURE TestFilenameTruncation(): BOOLEAN;
CONST LONGNAME_LEN = Chars.MAXSTR + 10;
VAR test: BOOLEAN; logger: Log.Logger;
longName: ARRAY LONGNAME_LEN OF CHAR; i: INTEGER;
BEGIN
test := TRUE;
FOR i := 0 TO LEN(longName)-2 DO longName[i] := "X" END;
longName[LEN(longName)-1] := 0X;
logger := Log.New(Log.INFO, Log.FILE, longName);
T.ExpectedBool(logger # NIL, TRUE, "Logger created with long filename (should truncate)", test);
Log.Info(logger, "Filename truncation test");
Log.Close(logger);
RETURN test
END TestFilenameTruncation;
(* Test toggling timestamp on and off *)
PROCEDURE TestTimestampToggle(): BOOLEAN;
VAR test: BOOLEAN; logger: Log.Logger;
BEGIN
test := TRUE;
logger := Log.New(Log.INFO, Log.CONSOLE, "");
Log.Info(logger, "Timestamp should be present");
Log.SetTimestamp(logger, FALSE);
Log.Info(logger, "Timestamp should be absent");
Log.SetTimestamp(logger, TRUE);
Log.Info(logger, "Timestamp should be present again");
Log.Close(logger);
RETURN test
END TestTimestampToggle;
(* Test multiple loggers with different settings *)
PROCEDURE TestMultipleLoggers(): BOOLEAN;
VAR test: BOOLEAN;
logger1, logger2: Log.Logger;
res: INTEGER;
BEGIN
test := TRUE;
logger1 := Log.New(Log.INFO, Log.CONSOLE, "");
logger2 := Log.New(Log.ERROR, Log.FILE, "multi.log");
Log.Info(logger1, "Logger1 info");
Log.Error(logger1, "Logger1 error");
Log.Info(logger2, "Logger2 info (should not appear)");
Log.Error(logger2, "Logger2 error");
Log.Close(logger1);
Log.Close(logger2);
Files.Delete("multi.log", res);
RETURN test
END TestMultipleLoggers;
(* Test logger reuse after close *)
PROCEDURE TestReuseAfterClose(): BOOLEAN;
VAR test: BOOLEAN; logger: Log.Logger;
BEGIN
test := TRUE;
logger := Log.New(Log.INFO, Log.CONSOLE, "");
Log.Info(logger, "Before close");
Log.Close(logger);
Log.Info(logger, "After close (should not crash)");
RETURN test
END TestReuseAfterClose;
(* Test empty message and exact buffer size message *)
PROCEDURE TestEmptyAndExactBuffer(): BOOLEAN;
VAR test: BOOLEAN; logger: Log.Logger;
exactMsg: ARRAY Log.MAX_MESSAGE_LEN OF CHAR;
i: INTEGER;
BEGIN
test := TRUE;
logger := Log.New(Log.INFO, Log.CONSOLE, "");
Log.Info(logger, "");
FOR i := 0 TO LEN(exactMsg)-2 DO exactMsg[i] := "B" END;
exactMsg[LEN(exactMsg)-1] := 0X;
Log.Info(logger, exactMsg);
Log.Close(logger);
RETURN test
END TestEmptyAndExactBuffer;
BEGIN
T.Init(ts, "Log Tests");
T.Add(ts, TestLoggerCreation);
T.Add(ts, TestLogLevels);
T.Add(ts, TestFileLogging);
T.Add(ts, TestTruncation);
T.Add(ts, TestNilLogger);
T.Add(ts, TestInvalidLogLevel);
T.Add(ts, TestInvalidDestination);
T.Add(ts, TestFileCreationFailure);
T.Add(ts, TestFilenameTruncation);
T.Add(ts, TestTimestampToggle);
T.Add(ts, TestMultipleLoggers);
T.Add(ts, TestReuseAfterClose);
T.Add(ts, TestEmptyAndExactBuffer);
ASSERT(T.Run(ts))
END LogTest.