tregistry2.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. {
  2. This unit tests mostly TRegIniFile to work properly and be Delphi compatible.
  3. Please keep this unit Delphi compatible.
  4. }
  5. {$ifdef FPC} {$mode delphi} {$endif}
  6. unit tregistry2;
  7. interface
  8. procedure DoRegTest2;
  9. implementation
  10. uses Windows, SysUtils, Classes, registry;
  11. {$ifdef FPC}
  12. {$WARN implicit_string_cast_loss off}
  13. {$WARN symbol_deprecated off}
  14. {$endif FPC}
  15. const
  16. STestRegPath = 'Software\FPC-RegTest';
  17. procedure TestFailed(ErrCode: integer);
  18. begin
  19. raise Exception.Create('Test FAILED. Error code: ' + IntToStr(ErrCode));
  20. end;
  21. procedure ClearReg(const KeyName: string = '');
  22. begin
  23. with TRegistry.Create do
  24. try
  25. DeleteKey(STestRegPath);
  26. finally
  27. Free;
  28. end;
  29. end;
  30. function NormPath(const s: string): string;
  31. begin
  32. Result:=StringReplace(s, '/', '\', [rfReplaceAll]);
  33. end;
  34. procedure DoRegTest2;
  35. var
  36. reg: TRegistry;
  37. ri: TRegIniFile;
  38. rini: TRegistryIniFile;
  39. sl: TStringList;
  40. begin
  41. ClearReg;
  42. try
  43. reg:=TRegistry.Create;
  44. try
  45. { The test key must be deleted by ClearReg() }
  46. if reg.KeyExists(STestRegPath) then
  47. TestFailed(1);
  48. if reg.OpenKey(STestRegPath, False) then
  49. TestFailed(2);
  50. if not reg.OpenKey(STestRegPath, True) then
  51. TestFailed(5);
  52. if NormPath(reg.CurrentPath) <> STestRegPath then
  53. TestFailed(6);
  54. reg.WriteString('Item1', '1');
  55. if not reg.OpenKey('\' + STestRegPath + '\1', True) then
  56. TestFailed(10);
  57. reg.WriteString('Item2', '2');
  58. if NormPath(reg.CurrentPath) <> STestRegPath + '\1' then
  59. TestFailed(15);
  60. reg.CloseKey;
  61. if NormPath(reg.CurrentPath) <> '' then
  62. TestFailed(20);
  63. if reg.KeyExists(STestRegPath + '\' + STestRegPath) then
  64. TestFailed(21);
  65. finally
  66. reg.Free;
  67. end;
  68. ri:=TRegIniFile.Create(STestRegPath);
  69. with ri do
  70. try
  71. if ReadString('', 'Item1', '') <> '1' then
  72. TestFailed(101);
  73. if ReadString('1', 'Item2', '') <> '2' then
  74. TestFailed(105);
  75. if NormPath(ri.CurrentPath) <> STestRegPath then
  76. TestFailed(110);
  77. if ReadString('', 'Item1', '') <> '1' then
  78. TestFailed(115);
  79. if not ValueExists('Item1') then
  80. TestFailed(120);
  81. WriteInteger('1', 'Item3', 3);
  82. sl:=TStringList.Create;
  83. try
  84. ReadSectionValues('1', sl);
  85. if sl.Count <> 2 then
  86. TestFailed(125);
  87. if sl.Values['Item2'] <> '2' then
  88. TestFailed(130);
  89. if sl.Values['Item3'] <> '3' then
  90. TestFailed(135);
  91. finally
  92. sl.Free;
  93. end;
  94. WriteInteger('', 'Item4', 4);
  95. WriteInteger('', 'Item41', 41);
  96. WriteInteger('', 'Item42', 42);
  97. if GetDataType('Item4') <> rdString then
  98. TestFailed(140);
  99. if ReadString('', 'Item41', '') <> '41' then
  100. TestFailed(141);
  101. if ReadString('', 'Item42', '') <> '42' then
  102. TestFailed(142);
  103. finally
  104. Free;
  105. end;
  106. { \ at the beginning of the path must be accepted }
  107. ri:=TRegIniFile.Create('\' + STestRegPath);
  108. with ri do
  109. try
  110. if ReadString('', 'Item1', '') <> '1' then
  111. TestFailed(145);
  112. finally
  113. Free;
  114. end;
  115. { Write to non-existing key must work }
  116. ri:=TRegIniFile.Create(STestRegPath + '\2\3\4');
  117. with ri do
  118. try
  119. if FileName <> NormPath(CurrentPath) then
  120. TestFailed(147);
  121. if CurrentKey = 0 then
  122. TestFailed(148);
  123. WriteInteger('', 'Item5', 5);
  124. WriteInteger('5', 'Item6', 6);
  125. if ReadInteger('', 'Item5', 0) <> 5 then
  126. TestFailed(150);
  127. if ReadInteger('5', 'Item6', 0) <> 6 then
  128. TestFailed(160);
  129. finally
  130. Free;
  131. end;
  132. rini:=TRegistryIniFile.Create(STestRegPath);
  133. with rini do
  134. try
  135. if ReadString('', 'Item1', '') <> '1' then
  136. TestFailed(201);
  137. { \ is not allowed as a section name }
  138. if ReadString('\', 'Item1', '') = '1' then
  139. TestFailed(202);
  140. if ReadString('1', 'Item2', '') <> '2' then
  141. TestFailed(205);
  142. { Trailing \ is allowed }
  143. if ReadString('1\', 'Item2', '') <> '2' then
  144. TestFailed(206);
  145. if ReadString('', 'Item1', '') <> '1' then
  146. TestFailed(210);
  147. if not ValueExists('', 'Item4') then
  148. TestFailed(215);
  149. if not ValueExists('1', 'Item2') then
  150. TestFailed(220);
  151. if ReadInteger('2\3\4\5', 'Item6', 0) <> 6 then
  152. TestFailed(225);
  153. if ReadInteger('2\3\4', 'Item5', 0) <> 5 then
  154. TestFailed(230);
  155. EraseSection('2');
  156. if SectionExists('2\3') then
  157. TestFailed(245);
  158. if ValueExists('2\3\4', 'Item5') then
  159. TestFailed(240);
  160. WriteString('2\3\4', 'Item10', '10');
  161. if ReadInteger('2\3\4', 'Item10', 0) <> 10 then
  162. TestFailed(245);
  163. { Check access via a full path }
  164. if not SectionExists('\' + STestRegPath) then
  165. TestFailed(250);
  166. if ReadInteger('\2\3\4', 'Item10', 0) = 10 then
  167. TestFailed(255);
  168. if ReadInteger('\' + STestRegPath + '\2\3\4', 'Item10', 0) <> 10 then
  169. TestFailed(260);
  170. finally
  171. Free;
  172. end;
  173. finally
  174. ClearReg;
  175. end;
  176. { Test if all test keys have been deleted by ClearReg() }
  177. reg:=TRegistry.Create;
  178. try
  179. if reg.KeyExists(STestRegPath) then
  180. TestFailed(501);
  181. if reg.OpenKey(STestRegPath, False) then
  182. TestFailed(502);
  183. if reg.OpenKey(STestRegPath + '\2', False) then
  184. TestFailed(503);
  185. finally
  186. reg.Free;
  187. end;
  188. end;
  189. end.