jsonconftest.pp 8.2 KB

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