jsonconftest.pp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. unit jsonconftest;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, testregistry, jsonconf;
  6. type
  7. { TTestJSONConfig }
  8. TTestJSONConfig= class(TTestCase)
  9. Private
  10. procedure AssertStrings(Msg: String; L: TStrings;
  11. const Values: array of string);
  12. Function CreateConf(AFileName : String) : TJSONCOnfig;
  13. Procedure DeleteConf(C : TJSONConfig; DeleteConfFile : Boolean = true);
  14. published
  15. procedure TestDataTypes;
  16. procedure TestSubNodes;
  17. procedure TestEnumSubkeys;
  18. procedure TestEnumValues;
  19. procedure TestClear;
  20. procedure TestKey;
  21. procedure TestStrings;
  22. end;
  23. implementation
  24. function TTestJSONConfig.CreateConf(AFileName: String): TJSONCOnfig;
  25. begin
  26. Result:=TJSONConfig.Create(Nil);
  27. Result.FileName:=AFileName;
  28. end;
  29. procedure TTestJSONConfig.DeleteConf(C: TJSONConfig; DeleteConfFile: Boolean);
  30. Var
  31. FN : String;
  32. begin
  33. If DeleteConfFile then
  34. FN:=C.FileName;
  35. FreeAndNil(C);
  36. If DeleteConfFile then
  37. DeleteFile(FN);
  38. end;
  39. procedure TTestJSONConfig.TestDataTypes;
  40. Const
  41. A = Integer(1);
  42. B = 'A string';
  43. C = 1.23;
  44. D = True;
  45. E = Int64($FFFFFFFFFFFFF);
  46. Var
  47. Co : TJSONCOnfig;
  48. begin
  49. Co:=CreateConf('test.json');
  50. try
  51. Co.SetValue('a',a);
  52. AssertEquals('Integer read/Write',a,Co.GetValue('a',0));
  53. Co.SetValue('b',b);
  54. AssertEquals('String read/Write',b,Co.GetValue('b',''));
  55. Co.SetValue('c',C);
  56. AssertEquals('Float read/Write',c,Co.GetValue('c',0.0),0.01);
  57. Co.SetValue('d',d);
  58. AssertEquals('Boolean read/Write',d,Co.GetValue('d',False));
  59. Co.SetValue('e',E);
  60. AssertEquals('Int64 read/Write',e,Co.GetValue('e',Int64(0)));
  61. Co.Flush;
  62. finally
  63. DeleteConf(Co,True);
  64. end;
  65. end;
  66. procedure TTestJSONConfig.TestSubNodes;
  67. Var
  68. C : TJSONCOnfig;
  69. begin
  70. C:=CreateConf('test.json');
  71. try
  72. C.SetValue('a',1);
  73. AssertEquals('Read at root',1,C.GetValue('a',0));
  74. C.SetValue('b/a',2);
  75. AssertEquals('Read at root',2,C.GetValue('b/a',2));
  76. C.SetValue('b/c/a',3);
  77. AssertEquals('Read at root',3,C.GetValue('b/c/a',3));
  78. finally
  79. DeleteConf(C,True);
  80. end;
  81. end;
  82. procedure TTestJSONConfig.TestEnumSubkeys;
  83. Var
  84. C : TJSONCOnfig;
  85. L : TStringList;
  86. begin
  87. C:=CreateConf('test.json');
  88. try
  89. C.SetValue('/a',1);
  90. C.SetValue('/b/a',2);
  91. C.SetValue('/b/b',2);
  92. C.SetValue('/c/a',3);
  93. C.SetValue('/c/b/a',4);
  94. C.SetValue('/c/c/a',4);
  95. C.SetValue('/c/d/d',4);
  96. L:=TStringList.Create;
  97. try
  98. C.EnumSubKeys('/',L);
  99. If (L.Count<>2) then
  100. Fail('EnumSubkeys count');
  101. If (L[0]<>'b') then
  102. Fail('EnumSubkeys first element');
  103. If (L[1]<>'c') then
  104. Fail('EnumSubkeys second element');
  105. finally
  106. L.Free;
  107. end;
  108. finally
  109. DeleteConf(C,True);
  110. end;
  111. end;
  112. procedure TTestJSONConfig.TestEnumValues;
  113. Var
  114. C : TJSONCOnfig;
  115. L : TStringList;
  116. begin
  117. C:=CreateConf('test.json');
  118. try
  119. C.SetValue('/a',1);
  120. C.SetValue('/b/a',2);
  121. C.SetValue('/b/b',2);
  122. C.SetValue('/c/a',3);
  123. C.SetValue('/c/b/a',4);
  124. C.SetValue('/c/c/a',4);
  125. C.SetValue('/c/d/d',4);
  126. L:=TStringList.Create;
  127. try
  128. C.EnumValues('/',L);
  129. If (L.Count<>1) then
  130. Fail('EnumValues count');
  131. If (L[0]<>'a') then
  132. Fail('EnumValues first element');
  133. L.Clear;
  134. C.EnumValues('/b',L);
  135. If (L.Count<>2) then
  136. Fail('EnumValues subkey count');
  137. If (L[0]<>'a') then
  138. Fail('EnumValues subkey first element');
  139. If (L[1]<>'b') then
  140. Fail('EnumValues subkey second element');
  141. finally
  142. L.Free;
  143. end;
  144. finally
  145. DeleteConf(C,True);
  146. end;
  147. end;
  148. procedure TTestJSONConfig.TestClear;
  149. Var
  150. C : TJSONCOnfig;
  151. begin
  152. C:=CreateConf('test.json');
  153. try
  154. C.SetValue('a',1);
  155. C.DeleteValue('a');
  156. AssertEquals('Delete value',0,C.GetValue('a',0));
  157. C.SetValue('b/a',1);
  158. C.SetValue('b/c',2);
  159. C.DeleteValue('b/a');
  160. AssertEquals('Delete value in subkey',0,C.GetValue('a',0));
  161. AssertEquals('Delete value only clears deleted value',2,C.GetValue('b/c',0));
  162. C.SetValue('b/a',1);
  163. C.DeletePath('b');
  164. AssertEquals('Delete path',0,C.GetValue('b/a',0));
  165. AssertEquals('Delete path deletes all values',0,C.GetValue('b/c',0));
  166. C.Clear;
  167. AssertEquals('Clear',0,C.GetValue('/a',0));
  168. finally
  169. DeleteConf(C,True);
  170. end;
  171. end;
  172. procedure TTestJSONConfig.TestKey;
  173. Var
  174. C : TJSONCOnfig;
  175. L : TStrings;
  176. begin
  177. C:=CreateConf('test.json');
  178. try
  179. C.SetValue('a',1);
  180. C.SetValue('b/a',2);
  181. C.SetValue('b/b',2);
  182. C.SetValue('b/c/a',3);
  183. C.SetValue('b/c/b',3);
  184. C.OpenKey('/b',False);
  185. AssertEquals('Read relative to key a',2,C.GetValue('a',0));
  186. AssertEquals('Read relative to key b',2,C.GetValue('b',0));
  187. AssertEquals('Read in subkey relative to key a',3,C.GetValue('c/a',0));
  188. AssertEquals('Read in subkey relative to key b',3,C.GetValue('c/b',0));
  189. AssertEquals('Read absolute, disregarding key',1,C.GetValue('/a',0));
  190. AssertEquals('Read absolute in subkey, disregarding key',2,C.GetValue('/b/a',0));
  191. AssertEquals('Read absolute in subkeys, disregarding key',3,C.GetValue('/b/c/a',0));
  192. C.CloseKey;
  193. AssertEquals('Closekey',1,C.GetValue('a',0));
  194. C.OpenKey('b',False);
  195. C.OpenKey('c',False);
  196. AssertEquals('Open relative key',3,C.GetValue('a',0));
  197. C.ResetKey;
  198. AssertEquals('ResetKey',1,C.GetValue('a',0));
  199. C.Clear;
  200. L:=TStringList.Create;
  201. try
  202. C.EnumSubKeys('/',L);
  203. If (L.Count<>0) then
  204. Fail('clear failed');
  205. C.OpenKey('/a/b/c/d',true);
  206. C.EnumSubKeys('/a',L);
  207. If (L.Count<>1) then
  208. Fail('Open key with allowcreate, level 1');
  209. If (L[0]<>'b') then
  210. Fail('Open key with allowcreate, level 1');
  211. L.Clear;
  212. C.EnumSubKeys('/a/b',L);
  213. If (L.Count<>1) then
  214. Fail('Open key with allowcreate, level 2');
  215. If (L[0]<>'c') then
  216. Fail('Open key with allowcreate, level 2');
  217. L.Clear;
  218. C.EnumSubKeys('/a/b/c',L);
  219. If (L.Count<>1) then
  220. Fail('Open key with allowcreate, level 3');
  221. If (L[0]<>'d') then
  222. Fail('Open key with allowcreate, level 3');
  223. finally
  224. L.Free;
  225. end;
  226. finally
  227. DeleteConf(C,True);
  228. end;
  229. end;
  230. procedure TTestJSONConfig.AssertStrings(Msg : String; L : TStrings; Const Values : Array of string);
  231. Var
  232. I : Integer;
  233. begin
  234. Msg:=Msg+': ';
  235. AssertNotNull(Msg+'Have strings',L);
  236. AssertEquals(Msg+'Correct element count',Length(Values),L.Count);
  237. For I:=0 to L.Count-1 do
  238. AssertEquals(Msg+'element '+IntToStr(i),Values[i],l[i]);
  239. end;
  240. procedure TTestJSONConfig.TestStrings;
  241. Var
  242. C : TJSONCOnfig;
  243. L,LD : TStrings;
  244. begin
  245. L:=Nil;
  246. LD:=Nil;
  247. C:=CreateConf('test.json');
  248. try
  249. L:=TStringList.Create;
  250. LD:=TStringList.Create;
  251. L.Add('abc');
  252. C.GetValue('list',L,'');
  253. AssertStrings('Clear, no default.',L,[]);
  254. C.GetValue('list',L,'text');
  255. AssertStrings('Use default.',L,['text']);
  256. L.Clear;
  257. L.Add('abc');
  258. L.Add('def');
  259. C.SetValue('a',L);
  260. C.GetValue('a',LD,'');
  261. AssertStrings('List',LD,['abc','def']);
  262. L.Clear;
  263. L.Add('abc=1');
  264. L.Add('def=2');
  265. C.SetValue('a',L,True);
  266. LD.Clear;
  267. C.GetValue('a',LD,'');
  268. AssertStrings('List',LD,['abc=1','def=2']);
  269. C.SetValue('a','abc');
  270. C.GetValue('a',L,'');
  271. AssertStrings('String',L,['abc']);
  272. C.SetValue('a',Integer(1));
  273. C.GetValue('a',L,'');
  274. AssertStrings('integer',L,['1']);
  275. C.SetValue('a',True);
  276. C.GetValue('a',L,'');
  277. AssertStrings('integer',L,['True']);
  278. C.SetValue('a',Int64(1));
  279. C.GetValue('a',L,'');
  280. AssertStrings('int64',L,['1']);
  281. finally
  282. L.Free;
  283. C.Free;
  284. end;
  285. end;
  286. initialization
  287. RegisterTest(TTestJSONConfig);
  288. end.