Repository navigation
Expand file tree
/
Copy pathQuick.Log.Tests.pas
More file actions
240 lines (207 loc) · 7.77 KB
/
Copy pathQuick.Log.Tests.pas
File metadata and controls
240 lines (207 loc) · 7.77 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
{ ***************************************************************************
Copyright (c) 2015-2026 Kike Pérez
Unit : Quick.Log.Tests
Description : TQuickLog / TMemoryLog unit tests
Author : Kike Pérez
Version : 1.0
Created : 27/02/2026
Modified : 28/02/2026
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Log.Tests;
{$i QuickLib.inc}
interface
uses
DUnitX.TestFramework,
System.SysUtils,
System.IOUtils,
System.Classes,
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
Quick.Commons,
Quick.Log;
type
[TestFixture]
TQuickLogTests = class(TObject)
private
fLog : TQuickLog;
fLogFile : string;
public
[Setup]
procedure SetUp;
[TearDown]
procedure TearDown;
// TMemoryLog tests
[Test]
procedure Test_MemoryLog_Create_DefaultDisabled;
[Test]
procedure Test_MemoryLog_EnableAndCapture;
[Test]
procedure Test_MemoryLog_Text_ContainsLoggedMessage;
[Test]
procedure Test_MemoryLog_Lines_Count;
// TQuickLog – basic creation
[Test]
procedure Test_Create_DefaultVerbose;
[Test]
procedure Test_Create_MemoryLogAssigned;
// TQuickLog – SetLog
[Test]
procedure Test_SetLog_ReturnsTrue;
[Test]
procedure Test_SetLog_LogFileNameSet;
// TQuickLog – Add to MemoryLog
[Test]
procedure Test_Add_String_AppearsInMemoryLog;
[Test]
procedure Test_Add_Format_AppearsInMemoryLog;
[Test]
procedure Test_Add_MultipleMessages_AllAppear;
// TQuickLog – Verbose filtering
[Test]
procedure Test_Verbose_None_FiltersAllMessages;
[Test]
procedure Test_Verbose_OnlyErrors_FiltersInfo;
end;
implementation
procedure TQuickLogTests.SetUp;
begin
fLog := TQuickLog.Create;
fLogFile := TPath.Combine(TPath.GetTempPath,
'quicklib_log_test_' + IntToStr(TThread.CurrentThread.ThreadID) + '.log');
// SetLog must be called so WriteLog has a valid filename to write to
fLog.ShowHeaderInfo := False;
fLog.SetLog(fLogFile, False, 0);
// Enable MemoryLog for all tests
fLog.MemoryLog.Enabled := True;
end;
procedure TQuickLogTests.TearDown;
begin
fLog.Free;
if TFile.Exists(fLogFile) then TFile.Delete(fLogFile);
end;
// ── TMemoryLog ─────────────────────────────────────────────────────────────
procedure TQuickLogTests.Test_MemoryLog_Create_DefaultDisabled;
var
ml : TMemoryLog;
begin
ml := TMemoryLog.Create;
try
Assert.IsFalse(ml.Enabled, 'MemoryLog should be disabled by default');
finally
ml.Free;
end;
end;
procedure TQuickLogTests.Test_MemoryLog_EnableAndCapture;
begin
fLog.MemoryLog.Enabled := True;
fLog.Add('hello memlog', etInfo);
Assert.IsTrue(fLog.MemoryLog.Lines.Count > 0, 'MemoryLog should capture when enabled');
end;
procedure TQuickLogTests.Test_MemoryLog_Text_ContainsLoggedMessage;
begin
fLog.MemoryLog.Enabled := True;
fLog.Add('unique_token_xyz', etInfo);
Assert.IsTrue(fLog.MemoryLog.Text.Contains('unique_token_xyz'),
'MemoryLog.Text should contain the logged message');
end;
procedure TQuickLogTests.Test_MemoryLog_Lines_Count;
begin
fLog.MemoryLog.Enabled := True;
fLog.MemoryLog.Lines.Clear;
fLog.Add('msg1', etInfo);
fLog.Add('msg2', etInfo);
fLog.Add('msg3', etInfo);
Assert.AreEqual(3, Integer(fLog.MemoryLog.Lines.Count),
'MemoryLog should have 3 lines after 3 Add calls');
end;
// ── TQuickLog creation ──────────────────────────────────────────────────────
procedure TQuickLogTests.Test_Create_DefaultVerbose;
begin
// LOG_ALL means all event types are captured
Assert.IsTrue(fLog.Verbose = LOG_ALL,
'Default Verbose should be LOG_ALL');
end;
procedure TQuickLogTests.Test_Create_MemoryLogAssigned;
begin
Assert.IsNotNull(fLog.MemoryLog, 'MemoryLog should be assigned after Create');
end;
// ── SetLog ──────────────────────────────────────────────────────────────────
procedure TQuickLogTests.Test_SetLog_ReturnsTrue;
begin
Assert.IsTrue(fLog.SetLog(fLogFile, False, 0),
'SetLog should return True for a valid path');
end;
procedure TQuickLogTests.Test_SetLog_LogFileNameSet;
begin
fLog.ShowHeaderInfo := False; // avoid writing header during test
fLog.SetLog(fLogFile, False, 0);
Assert.AreEqual(fLogFile, fLog.LogFileName,
'LogFileName should match the name passed to SetLog');
end;
// ── Add ─────────────────────────────────────────────────────────────────────
procedure TQuickLogTests.Test_Add_String_AppearsInMemoryLog;
begin
fLog.MemoryLog.Enabled := True;
fLog.Add('TestMessage_ABC', etInfo);
Assert.IsTrue(fLog.MemoryLog.Text.Contains('TestMessage_ABC'),
'Logged string should appear in MemoryLog');
end;
procedure TQuickLogTests.Test_Add_Format_AppearsInMemoryLog;
begin
fLog.MemoryLog.Enabled := True;
fLog.Add('Value=%d Name=%s', [42, 'Alice'], etInfo);
Assert.IsTrue(fLog.MemoryLog.Text.Contains('Value=42'),
'Formatted log message should appear in MemoryLog');
Assert.IsTrue(fLog.MemoryLog.Text.Contains('Name=Alice'),
'Formatted log message name part should appear in MemoryLog');
end;
procedure TQuickLogTests.Test_Add_MultipleMessages_AllAppear;
begin
fLog.MemoryLog.Enabled := True;
fLog.MemoryLog.Lines.Clear;
fLog.Add('alpha', etInfo);
fLog.Add('beta', etWarning);
fLog.Add('gamma', etError);
Assert.IsTrue(fLog.MemoryLog.Text.Contains('alpha'), 'alpha missing from MemoryLog');
Assert.IsTrue(fLog.MemoryLog.Text.Contains('beta'), 'beta missing from MemoryLog');
Assert.IsTrue(fLog.MemoryLog.Text.Contains('gamma'), 'gamma missing from MemoryLog');
end;
// ── Verbose filtering ───────────────────────────────────────────────────────
procedure TQuickLogTests.Test_Verbose_None_FiltersAllMessages;
begin
fLog.MemoryLog.Enabled := True;
fLog.MemoryLog.Lines.Clear;
fLog.Verbose := [];
fLog.Add('should_be_filtered', etInfo);
Assert.AreEqual(0, Integer(fLog.MemoryLog.Lines.Count),
'With Verbose=LOG_NONE no messages should be captured');
end;
procedure TQuickLogTests.Test_Verbose_OnlyErrors_FiltersInfo;
begin
fLog.MemoryLog.Enabled := True;
fLog.MemoryLog.Lines.Clear;
// Use [etError] (just errors) so that etInfo is excluded
fLog.Verbose := [etError];
fLog.Add('info_msg', etInfo);
fLog.Add('error_msg', etError);
// info should be filtered, error should pass
Assert.IsFalse(fLog.MemoryLog.Text.Contains('info_msg'),
'Info message should be filtered when Verbose=[etError]');
Assert.IsTrue(fLog.MemoryLog.Text.Contains('error_msg'),
'Error message should appear when Verbose=[etError]');
end;
initialization
TDUnitX.RegisterTestFixture(TQuickLogTests);
end.