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
|
{
This unit tests mostly TRegIniFile to work properly and be Delphi compatible.
Please keep this unit Delphi compatible.
}
{$ifdef FPC} {$mode delphi} {$endif}
uses Windows, SysUtils, Classes, registry;
{$ifdef FPC}
{$WARN implicit_string_cast_loss off}
{$WARN symbol_deprecated off}
{$endif FPC}
const
STestRegPath = 'Software\FPC-RegTest';
procedure TestFailed(ErrCode: integer);
begin
writeln('Test FAILED. Error code: ' + IntToStr(ErrCode));
Halt(ErrCode);
end;
procedure ClearReg(const KeyName: string = '');
begin
with TRegistry.Create do
try
DeleteKey(STestRegPath);
finally
Free;
end;
end;
function NormPath(const s: string): string;
begin
Result:=StringReplace(s, '/', '\', [rfReplaceAll]);
end;
procedure DoRegTest2;
var
reg: TRegistry;
ri: TRegIniFile;
rini: TRegistryIniFile;
sl: TStringList;
begin
ClearReg;
try
reg:=TRegistry.Create;
try
{ The test key must be deleted by ClearReg() }
if reg.KeyExists(STestRegPath) then
TestFailed(1);
if reg.OpenKey(STestRegPath, False) then
TestFailed(2);
if not reg.OpenKey(STestRegPath, True) then
TestFailed(5);
if NormPath(reg.CurrentPath) <> STestRegPath then
TestFailed(6);
reg.WriteString('Item1', '1');
if not reg.OpenKey('\' + STestRegPath + '\1', True) then
TestFailed(10);
reg.WriteString('Item2', '2');
if NormPath(reg.CurrentPath) <> STestRegPath + '\1' then
TestFailed(15);
reg.CloseKey;
if NormPath(reg.CurrentPath) <> '' then
TestFailed(20);
if reg.KeyExists(STestRegPath + '\' + STestRegPath) then
TestFailed(21);
finally
reg.Free;
end;
ri:=TRegIniFile.Create(STestRegPath);
with ri do
try
if ReadString('', 'Item1', '') <> '1' then
TestFailed(101);
if ReadString('1', 'Item2', '') <> '2' then
TestFailed(105);
if NormPath(ri.CurrentPath) <> STestRegPath then
TestFailed(110);
if ReadString('', 'Item1', '') <> '1' then
TestFailed(115);
if not ValueExists('Item1') then
TestFailed(120);
WriteInteger('1', 'Item3', 3);
sl:=TStringList.Create;
try
ReadSectionValues('1', sl);
if sl.Count <> 2 then
TestFailed(125);
if sl.Values['Item2'] <> '2' then
TestFailed(130);
if sl.Values['Item3'] <> '3' then
TestFailed(135);
finally
sl.Free;
end;
WriteInteger('', 'Item4', 4);
WriteInteger('', 'Item41', 41);
WriteInteger('', 'Item42', 42);
if GetDataType('Item4') <> rdString then
TestFailed(140);
if ReadString('', 'Item41', '') <> '41' then
TestFailed(141);
if ReadString('', 'Item42', '') <> '42' then
TestFailed(142);
finally
Free;
end;
{ \ at the beginning of the path must be accepted }
ri:=TRegIniFile.Create('\' + STestRegPath);
with ri do
try
if ReadString('', 'Item1', '') <> '1' then
TestFailed(145);
finally
Free;
end;
{ Write to non-existing key must work }
ri:=TRegIniFile.Create(STestRegPath + '\2\3\4');
with ri do
try
if FileName <> NormPath(CurrentPath) then
TestFailed(147);
if CurrentKey = 0 then
TestFailed(148);
WriteInteger('', 'Item5', 5);
WriteInteger('5', 'Item6', 6);
if ReadInteger('', 'Item5', 0) <> 5 then
TestFailed(150);
if ReadInteger('5', 'Item6', 0) <> 6 then
TestFailed(160);
finally
Free;
end;
rini:=TRegistryIniFile.Create(STestRegPath);
with rini do
try
if ReadString('', 'Item1', '') <> '1' then
TestFailed(201);
{ \ is not allowed as a section name }
if ReadString('\', 'Item1', '') = '1' then
TestFailed(202);
if ReadString('1', 'Item2', '') <> '2' then
TestFailed(205);
{ Trailing \ is allowed }
if ReadString('1\', 'Item2', '') <> '2' then
TestFailed(206);
if ReadString('', 'Item1', '') <> '1' then
TestFailed(210);
if not ValueExists('', 'Item4') then
TestFailed(215);
if not ValueExists('1', 'Item2') then
TestFailed(220);
if ReadInteger('2\3\4\5', 'Item6', 0) <> 6 then
TestFailed(225);
if ReadInteger('2\3\4', 'Item5', 0) <> 5 then
TestFailed(230);
EraseSection('2');
if SectionExists('2\3') then
TestFailed(245);
if ValueExists('2\3\4', 'Item5') then
TestFailed(240);
WriteString('2\3\4', 'Item10', '10');
if ReadInteger('2\3\4', 'Item10', 0) <> 10 then
TestFailed(245);
{ Check access via a full path }
if not SectionExists('\' + STestRegPath) then
TestFailed(250);
if ReadInteger('\2\3\4', 'Item10', 0) = 10 then
TestFailed(255);
if ReadInteger('\' + STestRegPath + '\2\3\4', 'Item10', 0) <> 10 then
TestFailed(260);
finally
Free;
end;
finally
ClearReg;
end;
{ Test if all test keys have been deleted by ClearReg() }
reg:=TRegistry.Create;
try
if reg.KeyExists(STestRegPath) then
TestFailed(501);
if reg.OpenKey(STestRegPath, False) then
TestFailed(502);
if reg.OpenKey(STestRegPath + '\2', False) then
TestFailed(503);
finally
reg.Free;
end;
end;
begin
DoRegTest2;
end.
|