testjsondata.pp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. {
  2. This file is part of the Free Component Library
  3. JSON FPCUNit test for data structures
  4. Copyright (c) 2007 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. unit testjsondata;
  14. interface
  15. uses
  16. Classes, SysUtils, fpcunit, testutils, testregistry, fpjson;
  17. type
  18. { TTestJSONString }
  19. TTestJSONString = Class(TTestCase)
  20. Private
  21. Procedure TestTo(Const Src,Dest : String);
  22. Procedure TestFrom(Const Src,Dest : String);
  23. Published
  24. Procedure TestJSONStringToString;
  25. Procedure TestStringToJSONString;
  26. end;
  27. { TTestJSON }
  28. TTestJSON = Class(TTestCase)
  29. Protected
  30. Procedure TestItemCount(J : TJSONData;Expected : Integer);
  31. Procedure TestJSONType(J : TJSONData;Expected : TJSONType);
  32. Procedure TestJSON(J : TJSONData;Expected : String);
  33. Procedure TestIsNull(J : TJSONData;Expected : Boolean);
  34. Procedure TestAsBoolean(J : TJSONData;Expected : Boolean; ExpectError : boolean = False);
  35. Procedure TestAsInteger(J : TJSONData; Expected : Integer; ExpectError : boolean = False);
  36. Procedure TestAsInt64(J : TJSONData; Expected : Int64; ExpectError : boolean = False);
  37. Procedure TestAsString(J : TJSONData; Expected : String; ExpectError : boolean = False);
  38. Procedure TestAsFloat(J : TJSONData; Expected : TJSONFloat; ExpectError : boolean = False);
  39. end;
  40. { TTestNull }
  41. TTestNull = class(TTestJSON)
  42. published
  43. procedure TestNull;
  44. end;
  45. { TTestBoolean }
  46. TTestBoolean = class(TTestJSON)
  47. published
  48. procedure TestTrue;
  49. procedure TestFalse;
  50. end;
  51. { TTestInteger }
  52. TTestInteger = class(TTestJSON)
  53. Private
  54. Procedure DoTest(I : Integer);
  55. published
  56. procedure TestPositive;
  57. procedure TestNegative;
  58. procedure TestZero;
  59. end;
  60. { TTestInt64 }
  61. TTestInt64 = class(TTestJSON)
  62. Private
  63. Procedure DoTest(I : Int64);
  64. published
  65. procedure TestPositive;
  66. procedure TestNegative;
  67. procedure TestZero;
  68. end;
  69. { TTestFloat }
  70. TTestFloat = class(TTestJSON)
  71. Private
  72. Procedure DoTest(F : TJSONFloat);
  73. published
  74. procedure TestPositive;
  75. procedure TestNegative;
  76. procedure TestZero;
  77. end;
  78. { TTestString }
  79. TTestString = class(TTestJSON)
  80. private
  81. procedure DoTestFloat(F: TJSOnFloat; S: String; OK: Boolean);
  82. published
  83. procedure TestString;
  84. procedure TestInteger;
  85. procedure TestNegativeInteger;
  86. procedure TestFloat;
  87. procedure TestNegativeFloat;
  88. Procedure TestBooleanTrue;
  89. Procedure TestBooleanFalse;
  90. end;
  91. { TTestArray }
  92. TTestArray = class(TTestJSON)
  93. private
  94. procedure TestAddBoolean(B : Boolean);
  95. published
  96. Procedure TestCreate;
  97. Procedure TestCreateString;
  98. Procedure TestCreatePchar;
  99. procedure TestCreateStrings;
  100. procedure TestCreateInteger;
  101. procedure TestCreateInt64;
  102. procedure TestCreateFloat;
  103. procedure TestCreateBoolean;
  104. procedure TestCreateObject;
  105. procedure TestCreateJSONString;
  106. procedure TestCreateJSONObject;
  107. procedure TestCreateNilPointer;
  108. procedure TestCreatePointer;
  109. procedure TestAddInteger;
  110. procedure TestAddInt64;
  111. procedure TestAddFloat;
  112. procedure TestAddBooleanTrue;
  113. procedure TestAddBooleanFalse;
  114. procedure TestAddString;
  115. procedure TestAddNull;
  116. procedure TestAddObject;
  117. procedure TestAddArray;
  118. procedure TestDelete;
  119. procedure TestRemove;
  120. end;
  121. { TTestObject }
  122. TTestObject = class(TTestJSON)
  123. private
  124. procedure TestAddBoolean(B : Boolean);
  125. published
  126. Procedure TestCreate;
  127. Procedure TestCreateString;
  128. Procedure TestCreatePchar;
  129. procedure TestCreateStrings;
  130. procedure TestCreateInteger;
  131. procedure TestCreateInt64;
  132. procedure TestCreateFloat;
  133. procedure TestCreateBoolean;
  134. procedure TestCreateObject;
  135. procedure TestCreateJSONString;
  136. procedure TestCreateJSONObject;
  137. procedure TestCreateNilPointer;
  138. procedure TestCreatePointer;
  139. procedure TestAddInteger;
  140. procedure TestAddInt64;
  141. procedure TestAddFloat;
  142. procedure TestAddBooleanTrue;
  143. procedure TestAddBooleanFalse;
  144. procedure TestAddString;
  145. procedure TestAddNull;
  146. procedure TestAddObject;
  147. procedure TestAddArray;
  148. procedure TestDelete;
  149. procedure TestRemove;
  150. end;
  151. implementation
  152. { TTestJSON }
  153. procedure TTestJSON.TestItemCount(J: TJSONData; Expected: Integer);
  154. begin
  155. AssertEquals(J.ClassName+'.ItemCount',Expected,J.Count);
  156. end;
  157. procedure TTestJSON.TestJSONType(J: TJSONData; Expected: TJSONType);
  158. begin
  159. AssertEquals(J.ClassName+'.JSONType',Ord(Expected),Ord(J.JSONType));
  160. end;
  161. procedure TTestJSON.TestJSON(J: TJSONData; Expected: String);
  162. begin
  163. AssertEquals(J.ClassName+'.AsJSON',Expected,J.AsJSON);
  164. end;
  165. procedure TTestJSON.TestIsNull(J: TJSONData; Expected: Boolean);
  166. begin
  167. AssertEquals(J.ClassName+'.IsNull',Expected,J.IsNull);
  168. end;
  169. procedure TTestJSON.TestAsBoolean(J: TJSONData; Expected: Boolean; ExpectError: boolean = False);
  170. Var
  171. B : Boolean;
  172. AssignOK : Boolean;
  173. Msg : String;
  174. begin
  175. AssignOK:=False;
  176. Try
  177. B:=J.AsBoolean;
  178. AssignOK:=True;
  179. If Not ExpectError then
  180. AssertEquals(J.Classname+'.AsBoolean',Expected,B);
  181. except
  182. On E : Exception do
  183. begin
  184. AssignOK:=False;
  185. Msg:=E.Message;
  186. end;
  187. end;
  188. If ExpectError then
  189. begin
  190. If AssignOK then
  191. Fail(J.ClassName+'.AsBoolean must raise error');
  192. end
  193. else
  194. begin
  195. If not AssignOK then
  196. Fail(J.ClassName+'.AsBoolean raised unexpected exception: '+Msg)
  197. end;
  198. end;
  199. procedure TTestJSON.TestAsInteger(J: TJSONData; Expected: Integer;
  200. ExpectError: boolean);
  201. Var
  202. I : Integer;
  203. AssignOK : Boolean;
  204. Msg : String;
  205. begin
  206. AssignOK:=False;
  207. Try
  208. I:=J.AsInteger;
  209. AssignOK:=True;
  210. If Not ExpectError then
  211. AssertEquals(J.Classname+'.AsInteger',Expected,I);
  212. except
  213. On E : Exception do
  214. begin
  215. AssignOK:=False;
  216. Msg:=E.Message;
  217. end;
  218. end;
  219. If ExpectError then
  220. begin
  221. If AssignOK then
  222. Fail(J.ClassName+'.AsInteger must raise error');
  223. end
  224. else
  225. begin
  226. If not AssignOK then
  227. Fail(J.ClassName+'.AsInteger raised unexpected exception: '+Msg)
  228. end;
  229. end;
  230. procedure TTestJSON.TestAsInt64(J: TJSONData; Expected: Int64;
  231. ExpectError: boolean);
  232. Var
  233. I : Int64;
  234. AssignOK : Boolean;
  235. Msg : String;
  236. begin
  237. AssignOK:=False;
  238. Try
  239. I:=J.AsInt64;
  240. AssignOK:=True;
  241. If Not ExpectError then
  242. AssertEquals(J.Classname+'.AsInt64',Expected,I);
  243. except
  244. On E : Exception do
  245. begin
  246. AssignOK:=False;
  247. Msg:=E.Message;
  248. end;
  249. end;
  250. If ExpectError then
  251. begin
  252. If AssignOK then
  253. Fail(J.ClassName+'.AsInt64 must raise error');
  254. end
  255. else
  256. begin
  257. If not AssignOK then
  258. Fail(J.ClassName+'.AsInt64 raised unexpected exception: '+Msg)
  259. end;
  260. end;
  261. procedure TTestJSON.TestAsString(J: TJSONData; Expected: String;
  262. ExpectError: boolean);
  263. Var
  264. S : String;
  265. AssignOK : Boolean;
  266. Msg : String;
  267. begin
  268. AssignOK:=False;
  269. Try
  270. S:=J.AsString;
  271. AssignOK:=True;
  272. If Not ExpectError then
  273. AssertEquals(J.Classname+'.AsString',Expected,S);
  274. except
  275. On E : Exception do
  276. begin
  277. AssignOK:=False;
  278. Msg:=E.Message;
  279. end;
  280. end;
  281. If ExpectError then
  282. begin
  283. If AssignOK then
  284. Fail(J.ClassName+'.AsString must raise error');
  285. end
  286. else
  287. begin
  288. If not AssignOK then
  289. Fail(J.ClassName+'.AsString raised unexpected exception: '+Msg)
  290. end;
  291. end;
  292. procedure TTestJSON.TestAsFloat(J: TJSONData; Expected: TJSONFloat;
  293. ExpectError: boolean);
  294. Var
  295. F : TJSONFloat;
  296. AssignOK : Boolean;
  297. Msg : String;
  298. begin
  299. AssignOK:=False;
  300. Try
  301. F:=J.AsFloat;
  302. AssignOK:=True;
  303. If Not ExpectError then
  304. AssertEquals(J.Classname+'.AsFloat',Expected,F);
  305. except
  306. On E : Exception do
  307. begin
  308. AssignOK:=False;
  309. Msg:=E.Message;
  310. end;
  311. end;
  312. If ExpectError then
  313. begin
  314. If AssignOK then
  315. Fail(J.ClassName+'.AsFloat must raise error');
  316. end
  317. else
  318. begin
  319. If not AssignOK then
  320. Fail(J.ClassName+'.AsFloat raised unexpected exception: '+Msg)
  321. end;
  322. end;
  323. { TTestBoolean }
  324. procedure TTestBoolean.TestTrue;
  325. Var
  326. J : TJSONBoolean;
  327. begin
  328. J:=TJSONBoolean.Create(True);
  329. try
  330. TestJSONType(J,jtBoolean);
  331. TestItemCount(J,0);
  332. TestJSON(J,'True');
  333. TestIsNull(J,False);
  334. TestAsBoolean(J,True);
  335. TestAsInteger(J,1);
  336. TestAsInt64(J,1);
  337. TestAsString(J,BoolToStr(True));
  338. TestAsFloat(J,1.0);
  339. finally
  340. FreeAndNil(J);
  341. end;
  342. end;
  343. procedure TTestBoolean.TestFalse;
  344. Var
  345. J : TJSONBoolean;
  346. begin
  347. J:=TJSONBoolean.Create(False);
  348. try
  349. TestJSONType(J,jtBoolean);
  350. TestItemCount(J,0);
  351. TestJSON(J,'False');
  352. TestIsNull(J,False);
  353. TestAsBoolean(J,False);
  354. TestAsInteger(J,0);
  355. TestAsInt64(J,0);
  356. TestAsString(J,BoolToStr(False));
  357. TestAsFloat(J,0.0);
  358. finally
  359. FreeAndNil(J);
  360. end;
  361. end;
  362. { TTestNull }
  363. procedure TTestNull.TestNull;
  364. Var
  365. J : TJSONNull;
  366. begin
  367. J:=TJSONNull.Create;
  368. try
  369. TestJSONType(J,jtNull);
  370. TestItemCount(J,0);
  371. TestJSON(J,'Null');
  372. TestIsNull(J,True);
  373. TestAsBoolean(J,False,True);
  374. TestAsInteger(J,0,true);
  375. TestAsInt64(J,0,true);
  376. TestAsString(J,BoolToStr(False),true);
  377. TestAsFloat(J,0.0,true);
  378. finally
  379. FreeAndNil(J);
  380. end;
  381. end;
  382. { TTestString }
  383. procedure TTestString.TestString;
  384. Const
  385. S = 'A string';
  386. Var
  387. J : TJSONString;
  388. begin
  389. J:=TJSONString.Create(S);
  390. try
  391. TestJSONType(J,jtString);
  392. TestItemCount(J,0);
  393. TestJSON(J,'"'+S+'"');
  394. TestIsNull(J,False);
  395. TestAsBoolean(J,False,True);
  396. TestAsInteger(J,0,true);
  397. TestAsInt64(J,0,true);
  398. TestAsString(J,S);
  399. TestAsFloat(J,0.0,true);
  400. finally
  401. FreeAndNil(J);
  402. end;
  403. end;
  404. procedure TTestString.TestInteger;
  405. Const
  406. S = '1';
  407. Var
  408. J : TJSONString;
  409. begin
  410. J:=TJSONString.Create(S);
  411. try
  412. TestJSONType(J,jtString);
  413. TestItemCount(J,0);
  414. TestJSON(J,'"'+S+'"');
  415. TestIsNull(J,False);
  416. TestAsBoolean(J,True,False);
  417. TestAsInteger(J,1,False);
  418. TestAsInt64(J,1,False);
  419. TestAsString(J,S);
  420. TestAsFloat(J,1.0,False);
  421. finally
  422. FreeAndNil(J);
  423. end;
  424. end;
  425. procedure TTestString.TestNegativeInteger;
  426. Const
  427. S = '-1';
  428. Var
  429. J : TJSONString;
  430. begin
  431. J:=TJSONString.Create(S);
  432. try
  433. TestJSONType(J,jtString);
  434. TestItemCount(J,0);
  435. TestJSON(J,'"'+S+'"');
  436. TestIsNull(J,False);
  437. TestAsBoolean(J,True,False);
  438. TestAsInteger(J,-1,False);
  439. TestAsInt64(J,-1,False);
  440. TestAsString(J,S);
  441. TestAsFloat(J,-1.0,False);
  442. finally
  443. FreeAndNil(J);
  444. end;
  445. end;
  446. procedure TTestString.TestFloat;
  447. begin
  448. DoTestFloat(1.0,'1.0',True);
  449. DoTestFloat(1.0,'1',True);
  450. DoTestFloat(1.0,'1e0',True);
  451. DoTestFloat(1.2,'1.2',True);
  452. DoTestFloat(12.0,'1.2e1',True);
  453. end;
  454. procedure TTestString.TestNegativeFloat;
  455. begin
  456. DoTestFloat(-1.0,'-1.0',True);
  457. DoTestFloat(-1.0,'-1',True);
  458. DoTestFloat(-1.0,'-1e0',True);
  459. DoTestFloat(-1.2,'-1.2',True);
  460. DoTestFloat(-12.0,'-1.2e1',True);
  461. end;
  462. procedure TTestString.TestBooleanTrue;
  463. Const
  464. S = 'True';
  465. Var
  466. J : TJSONString;
  467. begin
  468. J:=TJSONString.Create(S);
  469. try
  470. TestJSONType(J,jtString);
  471. TestItemCount(J,0);
  472. TestJSON(J,'"'+S+'"');
  473. TestIsNull(J,False);
  474. TestAsBoolean(J,True,False);
  475. TestAsInteger(J,-1,True);
  476. TestAsInt64(J,-1,True);
  477. TestAsString(J,S);
  478. TestAsFloat(J,-1.0,True);
  479. finally
  480. FreeAndNil(J);
  481. end;
  482. end;
  483. procedure TTestString.TestBooleanFalse;
  484. Const
  485. S = 'False';
  486. Var
  487. J : TJSONString;
  488. begin
  489. J:=TJSONString.Create(S);
  490. try
  491. TestJSONType(J,jtString);
  492. TestItemCount(J,0);
  493. TestJSON(J,'"'+S+'"');
  494. TestIsNull(J,False);
  495. TestAsBoolean(J,False,False);
  496. TestAsInteger(J,0,True);
  497. TestAsInt64(J,0,True);
  498. TestAsString(J,S);
  499. TestAsFloat(J,0,True);
  500. finally
  501. FreeAndNil(J);
  502. end;
  503. end;
  504. procedure TTestString.DoTestFloat(F : TJSOnFloat;S : String; OK : Boolean);
  505. Var
  506. J : TJSONString;
  507. begin
  508. J:=TJSONString.Create(S);
  509. try
  510. TestJSONType(J,jtString);
  511. TestItemCount(J,0);
  512. TestJSON(J,'"'+S+'"');
  513. TestIsNull(J,False);
  514. TestAsBoolean(J,(F<>0),Not OK);
  515. TestAsInteger(J,Round(F),(Pos('.',S)<>0) or (Pos('E',UpperCase(S))<>0));
  516. TestAsInt64(J,Round(F),(Pos('.',S)<>0) or (Pos('E',UpperCase(S))<>0));
  517. TestAsString(J,S);
  518. TestAsFloat(J,F,Not OK);
  519. finally
  520. FreeAndNil(J);
  521. end;
  522. end;
  523. { TTestInteger }
  524. procedure TTestInteger.DoTest(I: Integer);
  525. Var
  526. J : TJSONIntegerNumber;
  527. begin
  528. J:=TJSONIntegerNumber.Create(I);
  529. try
  530. TestJSONType(J,jtNumber);
  531. TestItemCount(J,0);
  532. AssertEquals('Numbertype is ntInteger',ord(ntInteger),Ord(J.NumberType));
  533. TestJSON(J,IntToStr(i));
  534. TestIsNull(J,False);
  535. TestAsBoolean(J,(I<>0));
  536. TestAsInteger(J,I);
  537. TestAsInt64(J,I);
  538. TestAsString(J,IntToStr(I));
  539. TestAsFloat(J,I);
  540. finally
  541. FreeAndNil(J);
  542. end;
  543. end;
  544. procedure TTestInteger.TestPositive;
  545. begin
  546. DoTest(1);
  547. end;
  548. procedure TTestInteger.TestNegative;
  549. begin
  550. DoTest(-1);
  551. end;
  552. procedure TTestInteger.TestZero;
  553. begin
  554. DoTest(0);
  555. end;
  556. { TTestInt64 }
  557. procedure TTestInt64.DoTest(I: Int64);
  558. Var
  559. J : TJSONInt64Number;
  560. begin
  561. J:=TJSONInt64Number.Create(I);
  562. try
  563. TestJSONType(J,jtNumber);
  564. TestItemCount(J,0);
  565. AssertEquals('Numbertype is ntInt64',ord(ntInt64),Ord(J.NumberType));
  566. TestJSON(J,IntToStr(i));
  567. TestIsNull(J,False);
  568. TestAsBoolean(J,(I<>0));
  569. TestAsInteger(J,I);
  570. TestAsInt64(J,I);
  571. TestAsString(J,IntToStr(I));
  572. TestAsFloat(J,I);
  573. finally
  574. FreeAndNil(J);
  575. end;
  576. end;
  577. procedure TTestInt64.TestPositive;
  578. begin
  579. DoTest(1);
  580. end;
  581. procedure TTestInt64.TestNegative;
  582. begin
  583. DoTest(-1);
  584. end;
  585. procedure TTestInt64.TestZero;
  586. begin
  587. DoTest(0);
  588. end;
  589. { TTestFloat }
  590. procedure TTestFloat.DoTest(F: TJSONFloat);
  591. Var
  592. J : TJSONFloatNumber;
  593. S : String;
  594. begin
  595. Str(F,S);
  596. J:=TJSONFloatNumber.Create(F);
  597. try
  598. TestJSONType(J,jtNumber);
  599. TestItemCount(J,0);
  600. AssertEquals('Numbertype is ntFloat',ord(ntFloat),Ord(J.NumberType));
  601. TestJSON(J,S);
  602. TestIsNull(J,False);
  603. TestAsBoolean(J,(F<>0));
  604. TestAsInteger(J,Round(F));
  605. TestAsInt64(J,Round(F));
  606. TestAsString(J,S);
  607. TestAsFloat(J,F);
  608. finally
  609. FreeAndNil(J);
  610. end;
  611. end;
  612. procedure TTestFloat.TestPositive;
  613. begin
  614. DoTest(1.0);
  615. DoTest(1.2);
  616. DoTest(1.2e1);
  617. DoTest(1.2e-1);
  618. DoTest(1.2e10);
  619. DoTest(1.2e-10);
  620. end;
  621. procedure TTestFloat.TestNegative;
  622. begin
  623. DoTest(-1.0);
  624. DoTest(-1.2);
  625. DoTest(-1.2e1);
  626. DoTest(-1.2e-1);
  627. DoTest(-1.2e10);
  628. DoTest(-1.2e-10);
  629. end;
  630. procedure TTestFloat.TestZero;
  631. begin
  632. DoTest(0.0);
  633. end;
  634. { TTestArray }
  635. procedure TTestArray.TestCreate;
  636. Var
  637. J : TJSONArray;
  638. begin
  639. J:=TJSonArray.Create;
  640. try
  641. TestJSONType(J,jtArray);
  642. TestItemCount(J,0);
  643. TestJSON(J,'[]');
  644. TestIsNull(J,False);
  645. TestAsBoolean(J,False,True);
  646. TestAsInteger(J,1,True);
  647. TestAsInt64(J,1,True);
  648. TestAsString(J,'',True);
  649. TestAsFloat(J,0.0,True);
  650. finally
  651. FreeAndNil(J);
  652. end;
  653. end;
  654. procedure TTestArray.TestCreateString;
  655. Const
  656. S = 'A string';
  657. Var
  658. J : TJSONArray;
  659. begin
  660. J:=TJSonArray.Create([S]);
  661. try
  662. TestJSONType(J,jtArray);
  663. TestItemCount(J,1);
  664. TestJSONType(J[0],jtString);
  665. TestJSON(J,'["'+S+'"]');
  666. TestIsNull(J,False);
  667. finally
  668. FreeAndNil(J);
  669. end;
  670. end;
  671. procedure TTestArray.TestCreatePChar;
  672. Const
  673. S = 'A string';
  674. Var
  675. J : TJSONArray;
  676. begin
  677. J:=TJSonArray.Create([Pchar(S)]);
  678. try
  679. TestJSONType(J,jtArray);
  680. TestItemCount(J,1);
  681. TestJSONType(J[0],jtString);
  682. TestJSON(J,'["'+S+'"]');
  683. TestIsNull(J,False);
  684. finally
  685. FreeAndNil(J);
  686. end;
  687. end;
  688. procedure TTestArray.TestCreateStrings;
  689. Const
  690. S = 'A string';
  691. T = 'B string';
  692. Var
  693. J : TJSONArray;
  694. begin
  695. J:=TJSONArray.Create([S,T]);
  696. try
  697. TestJSONType(J,jtArray);
  698. TestItemCount(J,2);
  699. TestJSONType(J[0],jtString);
  700. TestJSONType(J[1],jtString);
  701. TestJSON(J,'["'+S+'", "'+T+'"]');
  702. TestIsNull(J,False);
  703. finally
  704. FreeAndNil(J);
  705. end;
  706. end;
  707. procedure TTestArray.TestCreateInteger;
  708. Const
  709. S = 3;
  710. Var
  711. J : TJSONArray;
  712. begin
  713. J:=TJSonArray.Create([S]);
  714. try
  715. TestJSONType(J,jtArray);
  716. TestItemCount(J,1);
  717. TestJSONType(J[0],jtNumber);
  718. TestJSON(J,'[3]');
  719. finally
  720. FreeAndNil(J);
  721. end;
  722. end;
  723. procedure TTestArray.TestCreateFloat;
  724. Const
  725. S = 1.2;
  726. Var
  727. J : TJSONArray;
  728. r : String;
  729. begin
  730. J:=TJSonArray.Create([S]);
  731. try
  732. TestJSONType(J,jtArray);
  733. TestItemCount(J,1);
  734. TestJSONType(J[0],jtNumber);
  735. Str(S,R);
  736. TestJSON(J,'['+R+']');
  737. finally
  738. FreeAndNil(J);
  739. end;
  740. end;
  741. procedure TTestArray.TestCreateInt64;
  742. Const
  743. S : Int64 = $FFFFFFFFFFFFF;
  744. Var
  745. J : TJSONArray;
  746. begin
  747. J:=TJSonArray.Create([S]);
  748. try
  749. TestJSONType(J,jtArray);
  750. TestItemCount(J,1);
  751. TestJSONType(J[0],jtNumber);
  752. TestJSON(J,'['+IntToStr(S)+']');
  753. finally
  754. FreeAndNil(J);
  755. end;
  756. end;
  757. procedure TTestArray.TestCreateBoolean;
  758. Const
  759. S = True;
  760. Var
  761. J : TJSONArray;
  762. begin
  763. J:=TJSonArray.Create([S]);
  764. try
  765. TestJSONType(J,jtArray);
  766. TestItemCount(J,1);
  767. TestJSONType(J[0],jtBoolean);
  768. TestJSON(J,'[True]');
  769. finally
  770. FreeAndNil(J);
  771. end;
  772. end;
  773. procedure TTestArray.TestCreateJSONObject;
  774. Var
  775. J : TJSONArray;
  776. begin
  777. J:=TJSonArray.Create([TJSONObject.Create]);
  778. try
  779. TestItemCount(J,1);
  780. TestJSONType(J[0],jtObject);
  781. TestJSON(J,'[{}]');
  782. finally
  783. FreeAndNil(J);
  784. end;
  785. end;
  786. procedure TTestArray.TestCreateJSONString;
  787. Const
  788. S = 'A string';
  789. Var
  790. J : TJSONArray;
  791. begin
  792. J:=TJSonArray.Create([TJSONString.Create(S)]);
  793. try
  794. TestItemCount(J,1);
  795. TestJSONType(J[0],jtString);
  796. TestJSON(J,'["'+S+'"]');
  797. finally
  798. FreeAndNil(J);
  799. end;
  800. end;
  801. procedure TTestArray.TestCreateObject;
  802. Var
  803. J : TJSONArray;
  804. begin
  805. J:=Nil;
  806. try
  807. Try
  808. J:=TJSONArray.Create([TObject.Create]);
  809. Fail('Array constructor accepts only TJSONData');
  810. finally
  811. FreeAndNil(J);
  812. end;
  813. except
  814. // Should be OK.
  815. end;
  816. end;
  817. procedure TTestArray.TestCreateNilPointer;
  818. Var
  819. J : TJSONArray;
  820. P : Pointer;
  821. begin
  822. J:=Nil;
  823. P:=Nil;
  824. Try
  825. J:=TJSONArray.Create([P]);
  826. TestJSONType(J[0],jtNull);
  827. finally
  828. FreeAndNil(J);
  829. end;
  830. end;
  831. procedure TTestArray.TestCreatePointer;
  832. Var
  833. J : TJSONArray;
  834. P : Pointer;
  835. begin
  836. J:=Nil;
  837. P:=@Self;
  838. try
  839. Try
  840. J:=TJSONArray.Create([P]);
  841. Fail('Array constructor accepts only NIL pointers');
  842. finally
  843. FreeAndNil(J);
  844. end;
  845. except
  846. // Should be OK.
  847. end;
  848. end;
  849. procedure TTestArray.TestAddInteger;
  850. Var
  851. J : TJSONArray;
  852. begin
  853. J:=TJSonArray.Create;
  854. try
  855. J.Add(Integer(0));
  856. TestItemCount(J,1);
  857. TestJSONType(J[0],jtNumber);
  858. AssertEquals('J[0] is TJSONIntegerNumber',J[0].ClassType,TJSONIntegerNumber);
  859. AssertEquals('j.Types[0]=jtNumber',ord(J.Types[0]),Ord(jtNumber));
  860. AssertEquals('J.Integers[0]=0',0,J.integers[0]);
  861. TestAsInteger(J[0],0);
  862. TestAsInt64(J[0],0);
  863. TestJSON(J,'[0]');
  864. finally
  865. FreeAndNil(J);
  866. end;
  867. end;
  868. procedure TTestArray.TestAddInt64;
  869. Var
  870. J : TJSONArray;
  871. begin
  872. J:=TJSonArray.Create;
  873. try
  874. J.Add(Int64(0));
  875. TestItemCount(J,1);
  876. TestJSONType(J[0],jtNumber);
  877. AssertEquals('J[0] is TJSONInt64Number',J[0].ClassType,TJSONInt64Number);
  878. AssertEquals('j.Types[0]=jtNumber',ord(J.Types[0]),Ord(jtNumber));
  879. AssertEquals('J.Int64s[0]=0',0,J.Int64s[0]);
  880. TestAsInteger(J[0],0);
  881. TestAsInt64(J[0],0);
  882. TestJSON(J,'[0]');
  883. finally
  884. FreeAndNil(J);
  885. end;
  886. end;
  887. procedure TTestArray.TestAddFloat;
  888. Var
  889. J : TJSONArray;
  890. S : String;
  891. F : TJSONFloat;
  892. begin
  893. F:=1.2;
  894. J:=TJSonArray.Create;
  895. try
  896. J.Add(F);
  897. TestItemCount(J,1);
  898. TestJSONType(J[0],jtNumber);
  899. AssertEquals('J[0] is TJSONFloatNumber',TJSONfloatNumber,J[0].ClassType);
  900. AssertEquals('j.Types[0]=jtNumber',Ord(jtNumber),ord(J.Types[0]));
  901. AssertEquals('J.Floats[0]='+FloatToStr(F),F,J.Floats[0]);
  902. TestAsFloat(J[0],F);
  903. Str(F,S);
  904. TestJSON(J,'['+S+']');
  905. finally
  906. FreeAndNil(J);
  907. end;
  908. end;
  909. procedure TTestArray.TestAddBoolean(B : Boolean);
  910. Var
  911. J : TJSONArray;
  912. begin
  913. B:=True;
  914. J:=TJSonArray.Create;
  915. try
  916. J.Add(B);
  917. TestItemCount(J,1);
  918. TestJSONType(J[0],jtBoolean);
  919. AssertEquals('J[0] is TJSONBoolean',TJSONBoolean,J[0].ClassType);
  920. TestAsBoolean(J[0],B);
  921. AssertEquals('J.Booleans[0]='+BoolToStr(B)+'"',B,J.Booleans[0]);
  922. If B then
  923. TestJSON(J,'[True]')
  924. else
  925. TestJSON(J,'[False]');
  926. finally
  927. FreeAndNil(J);
  928. end;
  929. end;
  930. procedure TTestArray.TestAddBooleanTrue;
  931. begin
  932. TestAddBoolean(True);
  933. end;
  934. procedure TTestArray.TestAddBooleanFalse;
  935. begin
  936. TestAddBoolean(False);
  937. end;
  938. procedure TTestArray.TestAddString;
  939. Var
  940. J : TJSONArray;
  941. S : String;
  942. F : TJSONFloat;
  943. begin
  944. S:='A string';
  945. J:=TJSonArray.Create;
  946. try
  947. J.Add(S);
  948. TestItemCount(J,1);
  949. TestJSONType(J[0],jtString);
  950. AssertEquals('J[0] is TJSONString',TJSONString,J[0].ClassType);
  951. TestAsString(J[0],S);
  952. AssertEquals('J.Strings[0]="'+S+'"',S,J.Strings[0]);
  953. TestJSON(J,'["'+StringToJSONString(S)+'"]');
  954. finally
  955. FreeAndNil(J);
  956. end;
  957. end;
  958. procedure TTestArray.TestAddNull;
  959. Var
  960. J : TJSONArray;
  961. S : String;
  962. F : TJSONFloat;
  963. begin
  964. J:=TJSonArray.Create;
  965. try
  966. J.Add;
  967. TestItemCount(J,1);
  968. TestJSONType(J[0],jtNull);
  969. AssertEquals('J[0] is TJSONNull',TJSONNull,J[0].ClassType);
  970. AssertEquals('J.Nulls[0]=True',True,J.Nulls[0]);
  971. TestIsNull(J[0],true);
  972. TestJSON(J,'[Null]');
  973. finally
  974. FreeAndNil(J);
  975. end;
  976. end;
  977. procedure TTestArray.TestAddArray;
  978. Var
  979. J,J2 : TJSONArray;
  980. begin
  981. J:=TJSonArray.Create;
  982. try
  983. J2:=TJSonArray.Create;
  984. J2.Add(0);
  985. J2.Add(1);
  986. J.Add(J2);
  987. TestItemCount(J,1);
  988. TestJSONType(J[0],jtArray);
  989. AssertEquals('J[0] is TJSONArray',TJSONArray,J[0].ClassType);
  990. AssertEquals('J.Arrays[0] is TJSONArray',TJSONArray,J.Arrays[0].ClassType);
  991. TestAsInteger(J.Arrays[0][0],0);
  992. TestAsInteger(J.Arrays[0][1],1);
  993. TestAsInt64(J.Arrays[0][0],0);
  994. TestAsInt64(J.Arrays[0][1],1);
  995. TestJSON(J,'[[0, 1]]');
  996. finally
  997. FreeAndNil(J);
  998. end;
  999. end;
  1000. procedure TTestArray.TestAddObject;
  1001. Const
  1002. A = 'a';
  1003. B = 'b';
  1004. Var
  1005. J : TJSONArray;
  1006. J2 : TJSONObject;
  1007. begin
  1008. J:=TJSonArray.Create;
  1009. try
  1010. J2:=TJSonObject.Create;
  1011. J2.Add(A,0);
  1012. J2.Add(B,1);
  1013. J.Add(J2);
  1014. TestItemCount(J,1);
  1015. TestJSONType(J[0],jtObject);
  1016. AssertEquals('J[0] is TJSONObject',TJSONObject,J[0].ClassType);
  1017. AssertEquals('J.Objects[0] is TJSONObject',TJSONObject,J.Objects[0].ClassType);
  1018. TestAsInteger(J.Objects[0][A],0);
  1019. TestAsInteger(J.Objects[0][B],1);
  1020. TestAsInt64(J.Objects[0][A],0);
  1021. TestAsInt64(J.Objects[0][B],1);
  1022. TestJSON(J,'[{ "a" : 0, "b" : 1 }]');
  1023. finally
  1024. FreeAndNil(J);
  1025. end;
  1026. end;
  1027. procedure TTestArray.TestDelete;
  1028. Var
  1029. J : TJSONArray;
  1030. begin
  1031. J:=TJSonArray.Create;
  1032. try
  1033. J.Add(0);
  1034. J.Add(1);
  1035. TestItemCount(J,2);
  1036. TestJSONType(J[0],jtNumber);
  1037. TestJSONType(J[1],jtNumber);
  1038. TestJSON(J,'[0, 1]');
  1039. J.Delete(1);
  1040. TestItemCount(J,1);
  1041. J.Delete(0);
  1042. TestItemCount(J,0);
  1043. finally
  1044. FreeAndNil(J);
  1045. end;
  1046. end;
  1047. procedure TTestArray.TestRemove;
  1048. Var
  1049. J : TJSONArray;
  1050. I : TJSONData;
  1051. begin
  1052. J:=TJSonArray.Create;
  1053. try
  1054. J.Add(0);
  1055. J.Add(1);
  1056. J.Add(2);
  1057. TestItemCount(J,3);
  1058. TestJSONType(J[0],jtNumber);
  1059. TestJSONType(J[1],jtNumber);
  1060. TestJSONType(J[2],jtNumber);
  1061. TestJSON(J,'[0, 1, 2]');
  1062. I:=J[1];
  1063. J.Remove(I);
  1064. TestItemCount(J,2);
  1065. TestAsInteger(J[0],0);
  1066. TestAsInteger(J[1],2);
  1067. TestAsInt64(J[0],0);
  1068. TestAsInt64(J[1],2);
  1069. finally
  1070. FreeAndNil(J);
  1071. end;
  1072. end;
  1073. { TTestObject }
  1074. procedure TTestObject.TestCreate;
  1075. Var
  1076. J : TJSONObject;
  1077. begin
  1078. J:=TJSONObject.Create;
  1079. try
  1080. TestJSONType(J,jtObject);
  1081. TestItemCount(J,0);
  1082. TestJSON(J,'{}');
  1083. TestIsNull(J,False);
  1084. TestAsBoolean(J,False,True);
  1085. TestAsInteger(J,1,True);
  1086. TestAsInt64(J,1,True);
  1087. TestAsString(J,'',True);
  1088. TestAsFloat(J,0.0,True);
  1089. finally
  1090. FreeAndNil(J);
  1091. end;
  1092. end;
  1093. procedure TTestObject.TestAddInteger;
  1094. Const
  1095. A = 'a';
  1096. Var
  1097. J : TJSONObject;
  1098. begin
  1099. J:=TJSonObject.Create;
  1100. try
  1101. J.Add(A,Integer(0));
  1102. TestItemCount(J,1);
  1103. TestJSONType(J[A],jtNumber);
  1104. AssertEquals('J[''a''] is TJSONIntegerNumber',J[A].ClassType,TJSONIntegerNumber);
  1105. AssertEquals('j.Types[''a'']=jtNumber',ord(J.Types[A]),Ord(jtNumber));
  1106. AssertEquals('J.Integers[''a'']=0',0,J.integers[A]);
  1107. TestAsInteger(J[A],0);
  1108. TestAsInt64(J[A],0);
  1109. TestJSON(J,'{ "'+A+'" : 0 }');
  1110. finally
  1111. FreeAndNil(J);
  1112. end;
  1113. end;
  1114. procedure TTestObject.TestAddInt64;
  1115. Const
  1116. A = 'a';
  1117. Var
  1118. J : TJSONObject;
  1119. begin
  1120. J:=TJSonObject.Create;
  1121. try
  1122. J.Add(A,Int64(0));
  1123. TestItemCount(J,1);
  1124. TestJSONType(J[A],jtNumber);
  1125. AssertEquals('J[''a''] is TJSONInt64Number',J[A].ClassType,TJSONInt64Number);
  1126. AssertEquals('j.Types[''a'']=jtNumber',ord(J.Types[A]),Ord(jtNumber));
  1127. AssertEquals('J.Int64s[''a'']=0',0,J.Int64s[A]);
  1128. TestAsInteger(J[A],0);
  1129. TestAsInt64(J[A],0);
  1130. TestJSON(J,'{ "'+A+'" : 0 }');
  1131. finally
  1132. FreeAndNil(J);
  1133. end;
  1134. end;
  1135. procedure TTestObject.TestAddFloat;
  1136. Const
  1137. A = 'a';
  1138. Var
  1139. J : TJSONObject;
  1140. S : String;
  1141. F : TJSONFloat;
  1142. begin
  1143. F:=1.2;
  1144. J:=TJSonObject.Create;
  1145. try
  1146. J.Add(A,F);
  1147. TestItemCount(J,1);
  1148. TestJSONType(J[A],jtNumber);
  1149. AssertEquals('J[''a''] is TJSONFloatNumber',TJSONfloatNumber,J[a].ClassType);
  1150. AssertEquals('j.Types[''a'']=jtNumber',Ord(jtNumber),ord(J.Types[a]));
  1151. AssertEquals('J.Floats[''a'']='+FloatToStr(F),F,J.Floats[a]);
  1152. TestAsFloat(J[A],F);
  1153. Str(F,S);
  1154. TestJSON(J,'{ "'+a+'" : '+S+' }');
  1155. finally
  1156. FreeAndNil(J);
  1157. end;
  1158. end;
  1159. procedure TTestObject.TestAddBoolean(B : Boolean);
  1160. Const
  1161. A = 'a';
  1162. Var
  1163. J : TJSONObject;
  1164. begin
  1165. B:=True;
  1166. J:=TJSonObject.Create;
  1167. try
  1168. J.Add(A,B);
  1169. TestItemCount(J,1);
  1170. TestJSONType(J[A],jtBoolean);
  1171. AssertEquals('J[''a''] is TJSONBoolean',TJSONBoolean,J[a].ClassType);
  1172. TestAsBoolean(J[a],B);
  1173. AssertEquals('J.Booleans[''a'']='+BoolToStr(B)+'"',B,J.Booleans[a]);
  1174. If B then
  1175. TestJSON(J,'{ "'+a+'" : True }')
  1176. else
  1177. TestJSON(J,'{ "'+a+'" : False }');
  1178. finally
  1179. FreeAndNil(J);
  1180. end;
  1181. end;
  1182. procedure TTestObject.TestAddBooleanTrue;
  1183. begin
  1184. TestAddBoolean(True);
  1185. end;
  1186. procedure TTestObject.TestAddBooleanFalse;
  1187. begin
  1188. TestAddBoolean(False);
  1189. end;
  1190. procedure TTestObject.TestAddString;
  1191. Const
  1192. A = 'a';
  1193. Var
  1194. J : TJSONObject;
  1195. S : String;
  1196. F : TJSONFloat;
  1197. begin
  1198. S:='A string';
  1199. J:=TJSonObject.Create;
  1200. try
  1201. J.Add(A,S);
  1202. TestItemCount(J,1);
  1203. TestJSONType(J[a],jtString);
  1204. AssertEquals('J[''a''] is TJSONString',TJSONString,J[A].ClassType);
  1205. TestAsString(J[a],S);
  1206. AssertEquals('J.Strings[''a'']="'+S+'"',S,J.Strings[A]);
  1207. TestJSON(J,'{ "'+a+'" : "'+StringToJSONString(S)+'" }');
  1208. finally
  1209. FreeAndNil(J);
  1210. end;
  1211. end;
  1212. procedure TTestObject.TestAddNull;
  1213. Const
  1214. A = 'a';
  1215. Var
  1216. J : TJSONObject;
  1217. S : String;
  1218. F : TJSONFloat;
  1219. begin
  1220. J:=TJSonObject.Create;
  1221. try
  1222. J.Add(a);
  1223. TestItemCount(J,1);
  1224. TestJSONType(J[a],jtNull);
  1225. AssertEquals('J[''a''] is TJSONNull',TJSONNull,J[A].ClassType);
  1226. AssertEquals('J.Nulls[''a'']=True',True,J.Nulls[A]);
  1227. TestIsNull(J[a],true);
  1228. TestJSON(J,'{ "'+a+'" : Null }');
  1229. finally
  1230. FreeAndNil(J);
  1231. end;
  1232. end;
  1233. procedure TTestObject.TestAddObject;
  1234. Const
  1235. A = 'a';
  1236. B = 'b';
  1237. C = 'c';
  1238. Var
  1239. J,J2 : TJSONObject;
  1240. begin
  1241. J:=TJSonObject.Create;
  1242. try
  1243. J2:=TJSonObject.Create;
  1244. J2.Add(B,0);
  1245. J2.Add(C,1);
  1246. J.Add(A,J2);
  1247. TestItemCount(J,1);
  1248. TestJSONType(J[A],jtObject);
  1249. AssertEquals('J[''a''] is TJSONObject',TJSONObject,J[A].ClassType);
  1250. AssertEquals('J.Objects[''a''] is TJSONObject',TJSONObject,J.Objects[A].ClassType);
  1251. TestAsInteger(J.Objects[A][B],0);
  1252. TestAsInteger(J.Objects[A][C],1);
  1253. TestAsInt64(J.Objects[A][B],0);
  1254. TestAsInt64(J.Objects[A][C],1);
  1255. TestJSON(J,'{ "a" : { "b" : 0, "c" : 1 } }');
  1256. finally
  1257. FreeAndNil(J);
  1258. end;
  1259. end;
  1260. procedure TTestObject.TestAddArray;
  1261. Const
  1262. A = 'a';
  1263. Var
  1264. J : TJSONObject;
  1265. J2 : TJSONArray;
  1266. begin
  1267. J:=TJSONObject.Create;
  1268. try
  1269. J2:=TJSonArray.Create;
  1270. J2.Add(0);
  1271. J2.Add(1);
  1272. J.Add(A,J2);
  1273. TestItemCount(J,1);
  1274. TestJSONType(J[A],jtArray);
  1275. AssertEquals('J[''a''] is TJSONArray',TJSONArray,J[A].ClassType);
  1276. AssertEquals('J.Arrays[0] is TJSONArray',TJSONArray,J.Arrays[A].ClassType);
  1277. TestAsInteger(J.Arrays[A][0],0);
  1278. TestAsInteger(J.Arrays[A][1],1);
  1279. TestAsInt64(J.Arrays[A][0],0);
  1280. TestAsInt64(J.Arrays[A][1],1);
  1281. TestJSON(J,'{ "a" : [0, 1] }');
  1282. finally
  1283. FreeAndNil(J);
  1284. end;
  1285. end;
  1286. procedure TTestObject.TestDelete;
  1287. Const
  1288. A = 'a';
  1289. B = 'b';
  1290. Var
  1291. J : TJSONObject;
  1292. begin
  1293. J:=TJSonObject.Create;
  1294. try
  1295. J.Add(A,0);
  1296. J.Add(B,1);
  1297. TestItemCount(J,2);
  1298. TestJSONType(J[A],jtNumber);
  1299. TestJSONType(J[A],jtNumber);
  1300. TestJSON(J,'{ "a" : 0, "b" : 1 }');
  1301. J.Delete(1);
  1302. TestItemCount(J,1);
  1303. J.Delete(0);
  1304. TestItemCount(J,0);
  1305. finally
  1306. FreeAndNil(J);
  1307. end;
  1308. end;
  1309. procedure TTestObject.TestRemove;
  1310. Const
  1311. A = 'a';
  1312. B = 'b';
  1313. C = 'c';
  1314. Var
  1315. J : TJSONObject;
  1316. I : TJSONData;
  1317. begin
  1318. J:=TJSonObject.Create;
  1319. try
  1320. J.Add(A,1);
  1321. J.Add(B,2);
  1322. J.Add(C,3);
  1323. TestItemCount(J,3);
  1324. TestJSONType(J[A],jtNumber);
  1325. TestJSONType(J[B],jtNumber);
  1326. TestJSONType(J[C],jtNumber);
  1327. TestJSON(J,'{ "a" : 1, "b" : 2, "c" : 3 }');
  1328. I:=J[b];
  1329. J.Remove(I);
  1330. TestItemCount(J,2);
  1331. TestAsInteger(J[a],1);
  1332. TestAsInteger(J[c],3);
  1333. TestAsInt64(J[a],1);
  1334. TestAsInt64(J[c],3);
  1335. finally
  1336. FreeAndNil(J);
  1337. end;
  1338. end;
  1339. procedure TTestObject.TestCreateString;
  1340. Const
  1341. A = 'A';
  1342. S = 'A string';
  1343. Var
  1344. J : TJSONObject;
  1345. begin
  1346. J:=TJSONObject.Create([A,S]);
  1347. try
  1348. TestJSONType(J,jtObject);
  1349. TestItemCount(J,1);
  1350. TestJSONType(J[A],jtString);
  1351. TestJSON(J,'{ "A" : "'+S+'" }');
  1352. TestIsNull(J,False);
  1353. finally
  1354. FreeAndNil(J);
  1355. end;
  1356. end;
  1357. procedure TTestObject.TestCreatePChar;
  1358. Const
  1359. A = 'A';
  1360. S = 'A string';
  1361. Var
  1362. J : TJSONObject;
  1363. begin
  1364. J:=TJSONObject.Create([A,Pchar(S)]);
  1365. try
  1366. TestJSONType(J,jtObject);
  1367. TestItemCount(J,1);
  1368. TestJSONType(J[A],jtString);
  1369. TestJSON(J,'{ "A" : "'+S+'" }');
  1370. TestIsNull(J,False);
  1371. finally
  1372. FreeAndNil(J);
  1373. end;
  1374. end;
  1375. procedure TTestObject.TestCreateStrings;
  1376. Const
  1377. A = 'A';
  1378. B = 'B';
  1379. S = 'A string';
  1380. T = 'B string';
  1381. Var
  1382. J : TJSONObject;
  1383. begin
  1384. J:=TJSONObject.Create([A,S,B,T]);
  1385. try
  1386. TestJSONType(J,jtObject);
  1387. TestItemCount(J,2);
  1388. TestJSONType(J[A],jtString);
  1389. TestJSONType(J[B],jtString);
  1390. TestJSON(J,'{ "A" : "'+S+'", "B" : "'+T+'" }');
  1391. TestIsNull(J,False);
  1392. finally
  1393. FreeAndNil(J);
  1394. end;
  1395. end;
  1396. procedure TTestObject.TestCreateInteger;
  1397. Const
  1398. A = 'A';
  1399. S = 3;
  1400. Var
  1401. J : TJSONObject;
  1402. begin
  1403. J:=TJSONObject.Create([A,S]);
  1404. try
  1405. TestJSONType(J,jtObject);
  1406. TestItemCount(J,1);
  1407. TestJSONType(J[A],jtNumber);
  1408. TestJSON(J,'{ "A" : 3 }');
  1409. finally
  1410. FreeAndNil(J);
  1411. end;
  1412. end;
  1413. procedure TTestObject.TestCreateFloat;
  1414. Const
  1415. A = 'A';
  1416. S = 1.2;
  1417. Var
  1418. J : TJSONObject;
  1419. r : String;
  1420. begin
  1421. J:=TJSONObject.Create([A,S]);
  1422. try
  1423. TestJSONType(J,jtObject);
  1424. TestItemCount(J,1);
  1425. TestJSONType(J[A],jtNumber);
  1426. Str(S,R);
  1427. TestJSON(J,'{ "A" : '+R+' }');
  1428. finally
  1429. FreeAndNil(J);
  1430. end;
  1431. end;
  1432. procedure TTestObject.TestCreateInt64;
  1433. Const
  1434. A = 'A';
  1435. S : Int64 = $FFFFFFFFFFFFF;
  1436. Var
  1437. J : TJSONObject;
  1438. begin
  1439. J:=TJSONObject.Create([A,S]);
  1440. try
  1441. TestJSONType(J,jtObject);
  1442. TestItemCount(J,1);
  1443. TestJSONType(J[A],jtNumber);
  1444. TestJSON(J,'{ "A" : '+IntToStr(S)+' }');
  1445. finally
  1446. FreeAndNil(J);
  1447. end;
  1448. end;
  1449. procedure TTestObject.TestCreateBoolean;
  1450. Const
  1451. A = 'A';
  1452. S = True;
  1453. Var
  1454. J : TJSONObject;
  1455. begin
  1456. J:=TJSONObject.Create([A,S]);
  1457. try
  1458. TestJSONType(J,jtObject);
  1459. TestItemCount(J,1);
  1460. TestJSONType(J[A],jtBoolean);
  1461. TestJSON(J,'{ "A" : True }');
  1462. finally
  1463. FreeAndNil(J);
  1464. end;
  1465. end;
  1466. procedure TTestObject.TestCreateJSONObject;
  1467. Const
  1468. A = 'A';
  1469. Var
  1470. J : TJSONObject;
  1471. begin
  1472. J:=TJSONObject.Create([A,TJSONObject.Create]);
  1473. try
  1474. TestItemCount(J,1);
  1475. TestJSONType(J[A],jtObject);
  1476. TestJSON(J,'{ "A" : {} }');
  1477. finally
  1478. FreeAndNil(J);
  1479. end;
  1480. end;
  1481. procedure TTestObject.TestCreateJSONString;
  1482. Const
  1483. A = 'A';
  1484. S = 'A string';
  1485. Var
  1486. J : TJSONObject;
  1487. begin
  1488. J:=TJSONObject.Create([A,TJSONString.Create(S)]);
  1489. try
  1490. TestItemCount(J,1);
  1491. TestJSONType(J[A],jtString);
  1492. TestJSON(J,'{ "A" : "'+S+'" }');
  1493. finally
  1494. FreeAndNil(J);
  1495. end;
  1496. end;
  1497. procedure TTestObject.TestCreateObject;
  1498. Const
  1499. A = 'A';
  1500. Var
  1501. J : TJSONObject;
  1502. begin
  1503. J:=Nil;
  1504. try
  1505. Try
  1506. J:=TJSONObject.Create([A,TObject.Create]);
  1507. Fail('Array constructor accepts only TJSONData');
  1508. finally
  1509. FreeAndNil(J);
  1510. end;
  1511. except
  1512. // Should be OK.
  1513. end;
  1514. end;
  1515. procedure TTestObject.TestCreateNilPointer;
  1516. Const
  1517. A = 'A';
  1518. Var
  1519. J : TJSONObject;
  1520. P : Pointer;
  1521. begin
  1522. J:=Nil;
  1523. P:=Nil;
  1524. Try
  1525. J:=TJSONObject.Create([A,P]);
  1526. TestJSONType(J[A],jtNull);
  1527. finally
  1528. FreeAndNil(J);
  1529. end;
  1530. end;
  1531. procedure TTestObject.TestCreatePointer;
  1532. Const
  1533. A = 'A';
  1534. Var
  1535. J : TJSONObject;
  1536. P : Pointer;
  1537. begin
  1538. J:=Nil;
  1539. P:=@Self;
  1540. try
  1541. Try
  1542. J:=TJSONObject.Create([A,P]);
  1543. Fail('Array constructor accepts only NIL pointers');
  1544. finally
  1545. FreeAndNil(J);
  1546. end;
  1547. except
  1548. // Should be OK.
  1549. end;
  1550. end;
  1551. { TTestJSONString }
  1552. procedure TTestJSONString.TestTo(const Src, Dest: String);
  1553. Var
  1554. S : String;
  1555. begin
  1556. S:='StringToJSONString('''+Src+''')='''+Dest+'''';
  1557. AssertEquals(S,Dest,StringToJSONString(Src));
  1558. end;
  1559. procedure TTestJSONString.TestFrom(const Src, Dest: String);
  1560. Var
  1561. S : String;
  1562. begin
  1563. S:='JSONStringToString('''+Src+''')='''+Dest+'''';
  1564. AssertEquals(S,Dest,JSONStringToString(Src));
  1565. end;
  1566. procedure TTestJSONString.TestJSONStringToString;
  1567. begin
  1568. TestFrom('','');
  1569. TestFrom('A','A');
  1570. TestFrom('AB','AB');
  1571. TestFrom('ABC','ABC');
  1572. TestFrom('\\','\');
  1573. TestFrom('\/','/');
  1574. TestFrom('\"','"');
  1575. TestFrom('\b',#8);
  1576. TestFrom('\t',#9);
  1577. TestFrom('\n',#10);
  1578. TestFrom('\f',#12);
  1579. TestFrom('\r',#13);
  1580. TestFrom('\bBC',#8'BC');
  1581. TestFrom('\tBC',#9'BC');
  1582. TestFrom('\nBC',#10'BC');
  1583. TestFrom('\fBC',#12'BC');
  1584. TestFrom('\rBC',#13'BC');
  1585. TestFrom('A\b','A'#8);
  1586. TestFrom('A\t','A'#9);
  1587. TestFrom('A\n','A'#10);
  1588. TestFrom('A\f','A'#12);
  1589. TestFrom('A\r','A'#13);
  1590. TestFrom('A\bBC','A'#8'BC');
  1591. TestFrom('A\tBC','A'#9'BC');
  1592. TestFrom('A\nBC','A'#10'BC');
  1593. TestFrom('A\fBC','A'#12'BC');
  1594. TestFrom('A\rBC','A'#13'BC');
  1595. TestFrom('\\\\','\\');
  1596. TestFrom('\/\/','//');
  1597. TestFrom('\"\"','""');
  1598. TestFrom('\b\b',#8#8);
  1599. TestFrom('\t\t',#9#9);
  1600. TestFrom('\n\n',#10#10);
  1601. TestFrom('\f\f',#12#12);
  1602. TestFrom('\r\r',#13#13);
  1603. end;
  1604. procedure TTestJSONString.TestStringToJSONString;
  1605. begin
  1606. TestTo('','');
  1607. TestTo('A','A');
  1608. TestTo('AB','AB');
  1609. TestTo('ABC','ABC');
  1610. TestTo('\','\\');
  1611. TestTo('/','\/');
  1612. TestTo('"','\"');
  1613. TestTo(#8,'\b');
  1614. TestTo(#9,'\t');
  1615. TestTo(#10,'\n');
  1616. TestTo(#12,'\f');
  1617. TestTo(#13,'\r');
  1618. TestTo(#8'BC','\bBC');
  1619. TestTo(#9'BC','\tBC');
  1620. TestTo(#10'BC','\nBC');
  1621. TestTo(#12'BC','\fBC');
  1622. TestTo(#13'BC','\rBC');
  1623. TestTo('A'#8,'A\b');
  1624. TestTo('A'#9,'A\t');
  1625. TestTo('A'#10,'A\n');
  1626. TestTo('A'#12,'A\f');
  1627. TestTo('A'#13,'A\r');
  1628. TestTo('A'#8'BC','A\bBC');
  1629. TestTo('A'#9'BC','A\tBC');
  1630. TestTo('A'#10'BC','A\nBC');
  1631. TestTo('A'#12'BC','A\fBC');
  1632. TestTo('A'#13'BC','A\rBC');
  1633. TestTo('\\','\\\\');
  1634. TestTo('//','\/\/');
  1635. TestTo('""','\"\"');
  1636. TestTo(#8#8,'\b\b');
  1637. TestTo(#9#9,'\t\t');
  1638. TestTo(#10#10,'\n\n');
  1639. TestTo(#12#12,'\f\f');
  1640. TestTo(#13#13,'\r\r');
  1641. end;
  1642. initialization
  1643. RegisterTest(TTestJSONString);
  1644. RegisterTest(TTestNull);
  1645. RegisterTest(TTestBoolean);
  1646. RegisterTest(TTestInteger);
  1647. RegisterTest(TTestInt64);
  1648. RegisterTest(TTestFloat);
  1649. RegisterTest(TTestString);
  1650. RegisterTest(TTestArray);
  1651. RegisterTest(TTestObject);
  1652. end.