tcjsonini.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. unit tcjsonini;
  2. {$mode objfpc}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, fpjson, inifiles, jsonini;
  6. Type
  7. { TJSONIniTest }
  8. TJSONIniTest = Class(TTestCase)
  9. private
  10. FFileContent: TJSONData;
  11. Fini: TJSONIniFile;
  12. FStrings: TStrings;
  13. FTestFile: String;
  14. procedure AssertValue(const aSection, Akey, avalue: string);
  15. procedure CreateIni;
  16. function GetIni: TJSONIniFile;
  17. function GetO: TJSONObject;
  18. Protected
  19. procedure HaveFile;
  20. Procedure ReLoad;
  21. procedure NoFileYet;
  22. procedure RemoveFile;
  23. Procedure Setup; override;
  24. Procedure TearDown; override;
  25. Procedure ReadFile;
  26. Procedure WriteFile;
  27. Procedure SampleFile;
  28. Property TestFile : String Read FTestFile;
  29. Property FileContent : TJSONData Read FFileContent Write FFileContent;
  30. Property ObjFileContent : TJSONObject Read GetO;
  31. Property Ini : TJSONIniFile Read GetIni;
  32. Property Strings : TStrings Read FStrings;
  33. Published
  34. Procedure TestEmpty;
  35. Procedure TestReadEmpty;
  36. Procedure TestReadEmptyValue;
  37. Procedure TestReadEmptyObject;
  38. Procedure TestRead1EmptySection;
  39. Procedure TestReadSections;
  40. procedure TestReadSection;
  41. procedure TestReadSectionValues;
  42. Procedure TestReadString;
  43. Procedure TestReadInteger;
  44. Procedure TestReadInt64;
  45. Procedure TestReadFloat;
  46. Procedure TestReadBoolean;
  47. Procedure TestReadDate;
  48. Procedure TestReadTime;
  49. Procedure TestReadDateTime;
  50. Procedure TestEraseSection;
  51. Procedure TestEraseSectionCaseMismatch;
  52. Procedure TestDeleteKey;
  53. Procedure TestDeleteKeySectionCaseMismatch;
  54. Procedure TestDeleteKeyKeyCaseMismatch;
  55. Procedure TestWriteString;
  56. Procedure TestWriteInteger;
  57. Procedure TestWriteBoolean;
  58. Procedure TestWriteDate;
  59. Procedure TestWriteDateTime;
  60. Procedure TestWriteTime;
  61. Procedure TestConvertIni;
  62. Procedure TestConvertIniString;
  63. end;
  64. implementation
  65. { TJSONIniTest }
  66. function TJSONIniTest.GetIni: TJSONIniFile;
  67. begin
  68. If FIni=Nil then
  69. begin
  70. Fini:=TJSONIniFile.Create(TestFile);
  71. end;
  72. Result:=FIni;
  73. end;
  74. function TJSONIniTest.GetO: TJSONObject;
  75. begin
  76. Result:=FFileContent as TJSONObject;
  77. end;
  78. procedure TJSONIniTest.Setup;
  79. begin
  80. Inherited;
  81. FTestFile:=TestName+'.json';
  82. If FileExists(FTestFile) then
  83. DeleteFile(FTestFile);
  84. FStrings:=TStringList.Create;
  85. // Do nothing
  86. end;
  87. procedure TJSONIniTest.TearDown;
  88. begin
  89. If FileExists(FTestFile) then
  90. DeleteFile(FTestFile);
  91. FreeAndNil(FFileContent);
  92. FreeAndNil(FIni);
  93. FreeAndNil(FStrings);
  94. Inherited;
  95. end;
  96. procedure TJSONIniTest.ReadFile;
  97. Var
  98. F : TFileStream;
  99. begin
  100. FreeAndNil(FFileContent);
  101. AssertTrue('Test File '+TestFile+' exists.',FileExists(TestFile));
  102. F:=TFileStream.Create(TestFile,fmOpenRead or fmShareDenyWrite);
  103. try
  104. FileContent:=GetJSON(F);
  105. finally
  106. F.Free;
  107. end;
  108. end;
  109. procedure TJSONIniTest.WriteFile;
  110. Var
  111. F : TFileStream;
  112. S : TJSONStringType;
  113. begin
  114. F:=TFileStream.Create(TestFile,fmCreate);
  115. try
  116. S:=FFileContent.AsJSON;
  117. F.WriteBuffer(S[1],Length(S));
  118. finally
  119. F.Free;
  120. end;
  121. end;
  122. procedure TJSONIniTest.SampleFile;
  123. begin
  124. FileContent:=TJSONObject.Create([
  125. 'a',TJSONObject.Create([
  126. 'i',1,
  127. 'i6',TJSONInt64Number.Create(Maxint*2),
  128. 'f',1.2,
  129. 's','test',
  130. 'si','1',
  131. 'si6',IntToStr(int64(MaxInt*2)),
  132. 'sf','1.2',
  133. 'dt','2001-05-06T23:24:25.678',
  134. 'id',Round(EncodeDate(2001,05,06)),
  135. 'fd',EncodeDate(2001,05,06),
  136. 't','0000-00-00T12:13:14.567',
  137. 'ft',Frac(EncodeTime(12,13,14,567)),
  138. 'fdt',EncodeDate(2001,05,06)+EncodeTime(23,24,25,678),
  139. 'd','2001-05-06',
  140. 'b',true,
  141. 'n',Nil,
  142. 'o',TJSONObject.Create
  143. ]),
  144. 'B',TJSONObject.Create([
  145. 'I',1,
  146. 'F',1.2,
  147. 'S','test',
  148. 'SI','1',
  149. 'SF','1.2',
  150. 'DT','2001-05-06T23:24:25.678',
  151. 'T','0000-00-00T12:13:14.567',
  152. 'D','2001-05-06',
  153. 'B',true,
  154. 'N',Nil,
  155. 'O',TJSONObject.Create
  156. ]),
  157. 'NO','not'
  158. ]);
  159. WriteFile;
  160. end;
  161. procedure TJSONIniTest.TestEmpty;
  162. begin
  163. AssertFalse('No test file',FileExists(testfile));
  164. AssertNull('No ini',Fini);
  165. AssertNull('No file content',FFileContent);
  166. AssertNotNull('Have strings',Strings);
  167. AssertEquals('Have empty strings',0,Strings.Count);
  168. end;
  169. procedure TJSONIniTest.TestReadEmpty;
  170. begin
  171. Ini.ReadSections(Strings);
  172. AssertEquals('No sections',0,Strings.Count);
  173. end;
  174. procedure TJSONIniTest.TestReadEmptyValue;
  175. begin
  176. FileContent:=TJSONString.Create('me');
  177. WriteFile;
  178. Ini.ReadSections(Strings);
  179. AssertEquals('No sections',0,Strings.Count);
  180. end;
  181. procedure TJSONIniTest.TestReadEmptyObject;
  182. begin
  183. FileContent:=TJSONObject.Create();
  184. WriteFile;
  185. Ini.ReadSections(Strings);
  186. AssertEquals('No sections',0,Strings.Count);
  187. end;
  188. procedure TJSONIniTest.TestRead1EmptySection;
  189. begin
  190. FileContent:=TJSONObject.Create(['empty',TJSONOBject.Create]);
  191. WriteFile;
  192. Ini.ReadSections(Strings);
  193. AssertEquals('1 sections',1,Strings.Count);
  194. AssertEquals('Section name','empty',Strings[0]);
  195. end;
  196. procedure TJSONIniTest.TestReadSections;
  197. begin
  198. SampleFile;
  199. Ini.ReadSections(Strings);
  200. AssertEquals('2 sections',2,Strings.Count);
  201. AssertEquals('Section name 0','a',Strings[0]);
  202. AssertEquals('Section name 1','B',Strings[1]);
  203. end;
  204. procedure TJSONIniTest.TestReadSection;
  205. begin
  206. SampleFile;
  207. Ini.ReadSection('a',Strings);
  208. // Only valid values are reported
  209. AssertEquals('value count',(FileContent as TJSONObject).Objects['a'].Count-2,Strings.Count);
  210. AssertEquals('value names','i,i6,f,s,si,si6,sf,dt,id,fd,t,ft,fdt,d,b',Strings.CommaText);
  211. end;
  212. procedure TJSONIniTest.TestReadSectionValues;
  213. Var
  214. D : TJSONEnum;
  215. begin
  216. SampleFile;
  217. Ini.ReadSectionValues('a',Strings,[]);
  218. // Only valid values are reported
  219. AssertEquals('value count',(FileContent as TJSONObject).Objects['a'].Count-2,Strings.Count);
  220. for D in (FileContent as TJSONObject).Objects['a'] do
  221. if D.Value.JSONType in ActualValueJSONTypes then
  222. AssertEquals('value '+D.key,D.Value.AsString,Strings.Values[D.Key]);
  223. Strings.Clear;
  224. Ini.ReadSectionValues('a',Strings);
  225. // All valid values are reported
  226. AssertEquals('value count',(FileContent as TJSONObject).Objects['a'].Count,Strings.Count);
  227. end;
  228. procedure TJSONIniTest.TestReadString;
  229. begin
  230. SampleFile;
  231. AssertEquals('Value, case OK','test',Ini.ReadString('a','s','nono'));
  232. AssertEquals('Value, key case not OK','test',Ini.ReadString('a','S','nono'));
  233. AssertEquals('Value, section case not OK','test',Ini.ReadString('A','s','nono'));
  234. AssertEquals('Value, section not exist','nono',Ini.ReadString('C','s','nono'));
  235. AssertEquals('Value, key not exist','nono',Ini.ReadString('a','Z','nono'));
  236. AssertEquals('Value, key not string','1',Ini.ReadString('a','i','nono'));
  237. AssertEquals('Value, key not valid value','nono',Ini.ReadString('a','o','nono'));
  238. end;
  239. procedure TJSONIniTest.TestReadInteger;
  240. begin
  241. SampleFile;
  242. AssertEquals('Value, case OK',1,Ini.ReadInteger('a','i',2));
  243. AssertEquals('Value, key case not OK',1,Ini.ReadInteger('a','I',2));
  244. AssertEquals('Value, section case not OK',1,Ini.ReadInteger('A','i',2));
  245. AssertEquals('Value, section not exist',2,Ini.ReadInteger('C','i',2));
  246. AssertEquals('Value, key not exist',2,Ini.ReadInteger('a','Z',2));
  247. AssertEquals('Value, key not integer',2,Ini.ReadInteger('a','s',2));
  248. AssertEquals('Value, key not integer, but convertable to integer',1,Ini.ReadInteger('a','si',2));
  249. end;
  250. procedure TJSONIniTest.TestReadInt64;
  251. Var
  252. I6 : Int64;
  253. begin
  254. I6:=MaxInt*2;
  255. SampleFile;
  256. AssertEquals('Value, case OK',i6,Ini.ReadInt64('a','i6',2));
  257. AssertEquals('Value, key case not OK',i6,Ini.ReadInt64('a','I6',2));
  258. AssertEquals('Value, section case not OK',i6,Ini.ReadInt64('A','i6',2));
  259. AssertEquals('Value, section not exist',2,Ini.ReadInt64('C','i',2));
  260. AssertEquals('Value, key not exist',2,Ini.ReadInt64('a','Z',2));
  261. AssertEquals('Value, key not integer',2,Ini.ReadInt64('a','s',2));
  262. AssertEquals('Value, key not integer, but convertable to int64',I6,Ini.ReadInt64('a','si6',2));
  263. end;
  264. procedure TJSONIniTest.TestReadFloat;
  265. begin
  266. SampleFile;
  267. AssertEquals('Value, case OK',1.2,Ini.ReadFloat('a','f',2.3));
  268. AssertEquals('Value, key case not OK',1.2,Ini.ReadFloat('a','F',2.3));
  269. AssertEquals('Value, section case not OK',1.2,Ini.ReadFloat('A','f',2.3));
  270. AssertEquals('Value, section not exist',2.3,Ini.ReadFloat('C','f',2.3));
  271. AssertEquals('Value, key not exist',2.3,Ini.ReadFloat('a','Z',2.3));
  272. AssertEquals('Value, key not float',2.3,Ini.ReadFloat('a','s',2.3));
  273. AssertEquals('Value, key not float, but convertable to float',1.2,Ini.ReadFloat('a','sf',2.3));
  274. end;
  275. procedure TJSONIniTest.TestReadBoolean;
  276. begin
  277. SampleFile;
  278. AssertEquals('Value, case OK',True,Ini.ReadBool('a','b',False));
  279. AssertEquals('Value, key case not OK',True,Ini.ReadBool('a','B',False));
  280. AssertEquals('Value, section case not OK',True,Ini.ReadBool('A','b',False));
  281. AssertEquals('Value, section not exist',True,Ini.ReadBool('C','b',True));
  282. AssertEquals('Value, key not exist',True,Ini.ReadBool('a','Z',True));
  283. AssertEquals('Value, key not bool but integer',True,Ini.ReadBool('a','i',false));
  284. end;
  285. procedure TJSONIniTest.TestReadDate;
  286. Var
  287. D,DD : TDateTime;
  288. begin
  289. D:=EncodeDate(2001,05,06);
  290. DD:=EncodeDate(1999,11,12);
  291. SampleFile;
  292. AssertEquals('Value, case OK',D,Ini.ReadDate('a','d',DD));
  293. AssertEquals('Value, key case not OK',D,Ini.ReadDate('a','D',DD));
  294. AssertEquals('Value, section case not OK',D,Ini.ReadDate('A','d',DD));
  295. AssertEquals('Value, section not exist',DD,Ini.ReadDate('C','d',DD));
  296. AssertEquals('Value, date as integer',D,Ini.ReadDate('a','id',DD));
  297. AssertEquals('Value, date as float',D,Ini.ReadDate('a','fd',DD));
  298. end;
  299. procedure TJSONIniTest.TestReadTime;
  300. Var
  301. T,DT : TDateTime;
  302. begin
  303. T:=EncodeTime(12,13,14,567);
  304. DT:=EncodeTime(1,2,3,4);
  305. SampleFile;
  306. AssertEquals('Value, case OK',T,Ini.ReadTime('a','t',DT));
  307. AssertEquals('Value, key case not OK',T,Ini.ReadTime('a','T',DT));
  308. AssertEquals('Value, section case not OK',T,Ini.ReadTime('A','t',DT));
  309. AssertEquals('Value, section not exist',DT,Ini.ReadTime('C','t',DT));
  310. AssertEquals('Value, key exist as float',T,Ini.ReadTime('a','ft',DT));
  311. end;
  312. procedure TJSONIniTest.TestReadDateTime;
  313. Var
  314. DT,DDT : TDateTime;
  315. begin
  316. DT:=EncodeDate(2001,05,06)+EncodeTime(23,24,25,678);
  317. DDT:=EncodeDate(1999,11,12)+EncodeTime(1,2,3,4);
  318. SampleFile;
  319. AssertEquals('Value, case OK',DT,Ini.ReadDateTime('a','dt',DDT));
  320. AssertEquals('Value, key case not OK',DT,Ini.ReadDateTime('a','DT',DDT));
  321. AssertEquals('Value, section case not OK',DT,Ini.ReadDateTime('A','dt',DDT));
  322. AssertEquals('Value, section not exist',DDT,Ini.ReadDateTime('C','dt',DDT));
  323. AssertEquals('Value, key exist as float',DT,Ini.ReadDateTime('a','fdt',DDT));
  324. end;
  325. procedure TJSONIniTest.TestEraseSection;
  326. begin
  327. SampleFile;
  328. Ini.EraseSection('B');
  329. Ini.UpdateFile;
  330. ReadFile;
  331. AssertEquals('No more section',-1,ObjFileContent.IndexOfName('B'));
  332. end;
  333. procedure TJSONIniTest.TestEraseSectionCaseMismatch;
  334. begin
  335. SampleFile;
  336. Ini.EraseSection('b');
  337. Ini.UpdateFile;
  338. ReadFile;
  339. AssertEquals('No more section',-1,ObjFileContent.IndexOfName('B'));
  340. end;
  341. procedure TJSONIniTest.TestDeleteKey;
  342. begin
  343. SampleFile;
  344. Ini.DeleteKey('a','i');
  345. Ini.UpdateFile;
  346. ReadFile;
  347. AssertEquals('No more key',-1,ObjFileContent.Objects['a'].IndexOfName('i'));
  348. end;
  349. procedure TJSONIniTest.TestDeleteKeySectionCaseMismatch;
  350. begin
  351. SampleFile;
  352. Ini.DeleteKey('A','i');
  353. Ini.UpdateFile;
  354. ReadFile;
  355. AssertEquals('No more key',-1,ObjFileContent.Objects['a'].IndexOfName('i'));
  356. end;
  357. procedure TJSONIniTest.TestDeleteKeyKeyCaseMismatch;
  358. begin
  359. SampleFile;
  360. Ini.DeleteKey('a','I');
  361. Ini.UpdateFile;
  362. ReadFile;
  363. AssertEquals('No more key',-1,ObjFileContent.Objects['a'].IndexOfName('i'));
  364. end;
  365. procedure TJSONIniTest.AssertValue(const aSection,Akey,avalue : string);
  366. Var
  367. D : TJSONData;
  368. begin
  369. ini.UpdateFile;
  370. ReadFile;
  371. D:=ObjFileContent.FindPath(asection+'.'+akey);
  372. AssertNotNull('Have value at '+asection+'.'+akey,D);
  373. AssertEquals('Correct value at '+asection+'.'+akey,AValue,D.AsString);
  374. end;
  375. procedure TJSONIniTest.NoFileYet;
  376. begin
  377. AssertFalse('File not exist yet',FileExists(TestFile));
  378. end;
  379. procedure TJSONIniTest.HaveFile;
  380. begin
  381. AssertTrue('Test file exists',FileExists(TestFile));
  382. end;
  383. procedure TJSONIniTest.ReLoad;
  384. begin
  385. FreeAndNil(Fini);
  386. AssertNotNull(Ini);
  387. end;
  388. procedure TJSONIniTest.RemoveFile;
  389. begin
  390. if FileExists(TestFile) then
  391. AssertTrue('Deleted file',DeleteFile(TestFile));
  392. end;
  393. procedure TJSONIniTest.TestWriteString;
  394. begin
  395. Ini.WriteString('a','i','string');
  396. NoFileYet;
  397. AssertValue('a','i','string');
  398. Ini.CacheUpdates:=False;
  399. Ini.WriteString('a','i','string2');
  400. HaveFile;
  401. AssertValue('a','i','string2');
  402. Reload;
  403. AssertEquals('Can read value','string2',Ini.ReadString('a','i',''));
  404. end;
  405. procedure TJSONIniTest.TestWriteInteger;
  406. begin
  407. Ini.Writeinteger('a','i',2);
  408. NoFileYet;
  409. AssertValue('a','i','2');
  410. Ini.CacheUpdates:=False;
  411. Ini.WriteInteger('a','i',3);
  412. HaveFile;
  413. AssertValue('a','i','3');
  414. Reload;
  415. AssertEquals('Can read value',3,Ini.ReadInteger('a','i',0));
  416. end;
  417. procedure TJSONIniTest.TestWriteBoolean;
  418. begin
  419. Ini.WriteBool('a','i',true);
  420. NoFileYet;
  421. AssertValue('a','i','True');
  422. Ini.CacheUpdates:=False;
  423. Ini.WriteBool('a','i2',true);
  424. HaveFile;
  425. AssertValue('a','i2','True');
  426. Reload;
  427. AssertEquals('Can read value',True,Ini.ReadBool('a','i2',false));
  428. end;
  429. procedure TJSONIniTest.TestWriteDate;
  430. Var
  431. D : TDateTime;
  432. begin
  433. D:=EncodeDate(2001,2,3);
  434. Ini.WriteDate('a','i',D);
  435. NoFileYet;
  436. AssertValue('a','i','2001-02-03T00:00:00.000');
  437. Ini.CacheUpdates:=False;
  438. Ini.WriteDate('a','i',D+1);
  439. HaveFile;
  440. AssertValue('a','i','2001-02-04T00:00:00.000');
  441. Reload;
  442. AssertEquals('Can read value',D+1,Ini.ReadDate('a','i',0));
  443. end;
  444. procedure TJSONIniTest.TestWriteDateTime;
  445. Var
  446. D : TDateTime;
  447. begin
  448. D:=EncodeDate(2001,2,3)+EncodeTime(12,13,14,567);
  449. Ini.WriteDateTime('a','i',D);
  450. NoFileYet;
  451. AssertValue('a','i','2001-02-03T12:13:14.567');
  452. Ini.CacheUpdates:=False;
  453. Ini.WriteDateTime('a','i',D+1);
  454. HaveFile;
  455. AssertValue('a','i','2001-02-04T12:13:14.567');
  456. Reload;
  457. AssertEquals('Can read value',D+1,Ini.ReadDateTime('a','i',0));
  458. end;
  459. procedure TJSONIniTest.TestWriteTime;
  460. Var
  461. D,D2 : TDateTime;
  462. begin
  463. D:=EncodeTime(12,13,14,567);
  464. D2:=EncodeTime(13,14,15,678);
  465. Ini.WriteTime('a','i',D);
  466. NoFileYet;
  467. AssertValue('a','i','0000-00-00T12:13:14.567');
  468. Ini.CacheUpdates:=False;
  469. Ini.WriteTime('a','i',D2);
  470. HaveFile;
  471. AssertValue('a','i','0000-00-00T13:14:15.678');
  472. Reload;
  473. AssertEquals('Can read value',D2,Ini.ReadTime('a','i',0));
  474. end;
  475. procedure TJSONIniTest.CreateIni;
  476. Var
  477. M : TMemIniFile;
  478. D,DT,T : TDateTime;
  479. begin
  480. D:=EncodeDate(2001,2,3);
  481. T:=EncodeTime(12,13,14,567);
  482. DT:=D+T;
  483. if FileExists(TestName+'.ini') then
  484. DeleteFile(TestName+'.ini');
  485. M:=TMemIniFile.Create(TestName+'.ini');
  486. try
  487. M.WriteString('a','s','c');
  488. M.WriteInteger('a','i',2);
  489. M.WriteBool('a','b',True);
  490. M.WriteInt64('a','i6',Maxint*2);
  491. M.WriteDate('a','d',D);
  492. M.WriteTime('a','t',T);
  493. M.WriteDateTime('a','dt',DT);
  494. M.WriteFloat('a','f',1.23);
  495. M.UpdateFile;
  496. finally
  497. M.Free;
  498. end;
  499. end;
  500. procedure TJSONIniTest.TestConvertIni;
  501. Var
  502. D,DT,T : TDateTime;
  503. begin
  504. D:=EncodeDate(2001,2,3);
  505. T:=EncodeTime(12,13,14,567);
  506. DT:=D+T;
  507. CreateIni;
  508. TJSONIniFile.ConvertIni(TestName+'.ini',TestFile,False);
  509. AssertEquals('String','c',Ini.ReadString('a','s',''));
  510. AssertEquals('Integer',2,Ini.ReadInteger('a','i',1));
  511. AssertEquals('Bool',True,Ini.ReadBool('a','b',False));
  512. AssertEquals('Int64',Int64(Maxint*2),Ini.ReadInt64('a','i6',Maxint*2));
  513. AssertEquals('Date',D, Ini.ReadDate('a','d',0));
  514. AssertEquals('Time',T,Ini.ReadTime('a','t',0));
  515. AssertEquals('DateTime',DT,Ini.ReadDateTime('a','dt',0));
  516. AssertEquals('Float',1.23,Ini.ReadFloat('a','f',0));
  517. if FileExists(TestName+'.ini') then
  518. DeleteFile(TestName+'.ini');
  519. end;
  520. procedure TJSONIniTest.TestConvertIniString;
  521. Var
  522. D,DT,T : TDateTime;
  523. begin
  524. D:=EncodeDate(2001,2,3);
  525. T:=EncodeTime(12,13,14,567);
  526. DT:=D+T;
  527. CreateIni;
  528. TJSONIniFile.ConvertIni(TestName+'.ini',TestFile,True);
  529. AssertEquals('String','c',Ini.ReadString('a','s',''));
  530. AssertEquals('Integer',2,Ini.ReadInteger('a','i',1));
  531. AssertEquals('Bool',True,Ini.ReadBool('a','b',False));
  532. AssertEquals('Int64',Int64(Maxint*2),Ini.ReadInt64('a','i6',Maxint*2));
  533. AssertEquals('Date',DateToStr(D), Ini.ReadString('a','d',''));
  534. AssertEquals('Time',TimeToStr(T),Ini.ReadString('a','t',''));
  535. AssertEquals('DateTime',DateTimeToStr(DT),Ini.ReadString('a','dt',''));
  536. AssertEquals('Float',1.23,Ini.ReadFloat('a','f',0));
  537. if FileExists(TestName+'.ini') then
  538. DeleteFile(TestName+'.ini');
  539. end;
  540. initialization
  541. RegisterTest(TJSONIniTest);
  542. end.