testjsondata.pp 39 KB

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