testjsonparser.pp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. {
  2. This file is part of the Free Component Library
  3. JSON FPCUNit test for parser
  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 testjsonparser;
  14. interface
  15. uses
  16. Classes, SysUtils, fpcunit, testutils, testregistry,fpjson,
  17. jsonParser,testjsondata;
  18. type
  19. { TTestParser }
  20. TTestParser= class(TTestJSON)
  21. private
  22. procedure DoTestError(S: String);
  23. procedure DoTestFloat(F: TJSONFloat); overload;
  24. procedure DoTestFloat(F: TJSONFloat; S: String); overload;
  25. procedure DoTestObject(S: String; const ElNames: array of String; DoJSONTest : Boolean = True);
  26. procedure DoTestString(S : String);
  27. procedure DoTestArray(S: String; ACount: Integer);
  28. published
  29. procedure TestEmpty;
  30. procedure TestNull;
  31. procedure TestTrue;
  32. procedure TestFalse;
  33. procedure TestFloat;
  34. procedure TestInteger;
  35. procedure TestInt64;
  36. procedure TestString;
  37. procedure TestArray;
  38. procedure TestObject;
  39. procedure TestMixed;
  40. procedure TestErrors;
  41. end;
  42. implementation
  43. procedure TTestParser.TestEmpty;
  44. Var
  45. P : TJSONParser;
  46. J : TJSONData;
  47. begin
  48. P:=TJSONParser.Create('');
  49. Try
  50. J:=P.Parse;
  51. If (J<>Nil) then
  52. Fail('Empty returns Nil');
  53. Finally
  54. FreeAndNil(J);
  55. FreeAndNil(P);
  56. end;
  57. end;
  58. procedure TTestParser.TestInteger;
  59. Var
  60. P : TJSONParser;
  61. J : TJSONData;
  62. begin
  63. P:=TJSONParser.Create('1');
  64. Try
  65. J:=P.Parse;
  66. If (J=Nil) then
  67. Fail('Parse of 1 fails');
  68. TestJSONType(J,jtNumber);
  69. TestAsInteger(J,1);
  70. Finally
  71. FreeAndNil(J);
  72. FreeAndNil(P);
  73. end;
  74. end;
  75. procedure TTestParser.TestInt64;
  76. Var
  77. P : TJSONParser;
  78. J : TJSONData;
  79. begin
  80. P:=TJSONParser.Create('123456789012345');
  81. Try
  82. J:=P.Parse;
  83. If (J=Nil) then
  84. Fail('Parse of 123456789012345 fails');
  85. TestJSONType(J,jtNumber);
  86. TestAsInt64(J,123456789012345);
  87. Finally
  88. FreeAndNil(J);
  89. FreeAndNil(P);
  90. end;
  91. end;
  92. procedure TTestParser.TestNull;
  93. Var
  94. P : TJSONParser;
  95. J : TJSONData;
  96. begin
  97. P:=TJSONParser.Create('null');
  98. Try
  99. J:=P.Parse;
  100. If (J=Nil) then
  101. Fail('Parse of null fails');
  102. TestJSONType(J,jtNull);
  103. Finally
  104. FreeAndNil(J);
  105. FreeAndNil(P);
  106. end;
  107. end;
  108. procedure TTestParser.TestTrue;
  109. Var
  110. P : TJSONParser;
  111. J : TJSONData;
  112. begin
  113. P:=TJSONParser.Create('True');
  114. Try
  115. J:=P.Parse;
  116. If (J=Nil) then
  117. Fail('Parse of True fails');
  118. TestJSONType(J,jtBoolean);
  119. TestAsBoolean(J,True);
  120. Finally
  121. FreeAndNil(J);
  122. FreeAndNil(P);
  123. end;
  124. end;
  125. procedure TTestParser.TestFalse;
  126. Var
  127. P : TJSONParser;
  128. J : TJSONData;
  129. begin
  130. P:=TJSONParser.Create('False');
  131. Try
  132. J:=P.Parse;
  133. If (J=Nil) then
  134. Fail('Parse of False fails');
  135. TestJSONType(J,jtBoolean);
  136. TestAsBoolean(J,False);
  137. Finally
  138. FreeAndNil(J);
  139. FreeAndNil(P);
  140. end;
  141. end;
  142. procedure TTestParser.TestFloat;
  143. begin
  144. DoTestFloat(1.2);
  145. DoTestFloat(-1.2);
  146. DoTestFloat(0);
  147. DoTestFloat(1.2e1);
  148. DoTestFloat(-1.2e1);
  149. DoTestFloat(0);
  150. DoTestFloat(1.2,'1.2');
  151. DoTestFloat(-1.2,'-1.2');
  152. DoTestFloat(0,'0.0');
  153. end;
  154. procedure TTestParser.TestString;
  155. begin
  156. DoTestString('A string');
  157. DoTestString('');
  158. DoTestString('\"');
  159. end;
  160. procedure TTestParser.TestArray;
  161. Var
  162. S1,S2,S3 : String;
  163. begin
  164. DoTestArray('[]',0);
  165. DoTestArray('[null]',1);
  166. DoTestArray('[true]',1);
  167. DoTestArray('[false]',1);
  168. DoTestArray('[1]',1);
  169. DoTestArray('[1, 2]',2);
  170. DoTestArray('[1, 2, 3]',3);
  171. DoTestArray('[1234567890123456]',1);
  172. DoTestArray('[1234567890123456, 2234567890123456]',2);
  173. DoTestArray('[1234567890123456, 2234567890123456, 3234567890123456]',3);
  174. Str(1.2,S1);
  175. Str(2.3,S2);
  176. Str(3.4,S3);
  177. DoTestArray('['+S1+']',1);
  178. DoTestArray('['+S1+', '+S2+']',2);
  179. DoTestArray('['+S1+', '+S2+', '+S3+']',3);
  180. DoTestArray('["A string"]',1);
  181. DoTestArray('["A string", "Another string"]',2);
  182. DoTestArray('["A string", "Another string", "Yet another string"]',3);
  183. DoTestArray('[null, false]',2);
  184. DoTestArray('[true, false]',2);
  185. DoTestArray('[null, 1]',2);
  186. DoTestArray('[1, "A string"]',2);
  187. DoTestArray('[1, []]',2);
  188. DoTestArray('[1, [1, 2]]',2);
  189. end;
  190. procedure TTestParser.TestMixed;
  191. Const
  192. SAddr ='{ "addressbook": { "name": "Mary Lebow", '+
  193. ' "address": {'+
  194. ' "street": "5 Main Street",'+LineEnding+
  195. ' "city": "San Diego, CA",'+LineEnding+
  196. ' "zip": 91912,'+LineEnding+
  197. ' },'+LineEnding+
  198. ' "phoneNumbers": [ '+LineEnding+
  199. ' "619 332-3452",'+LineEnding+
  200. ' "664 223-4667"'+LineEnding+
  201. ' ]'+LineEnding+
  202. ' }'+LineEnding+
  203. '}';
  204. begin
  205. DoTestArray('[1, {}]',2);
  206. DoTestArray('[1, { "a" : 1 }]',2);
  207. DoTestArray('[1, { "a" : 1 }, 1]',3);
  208. DoTestObject('{ "a" : [1, 2] }',['a']);
  209. DoTestObject('{ "a" : [1, 2], "B" : { "c" : "d" } }',['a','B']);
  210. DoTestObject(SAddr,['addressbook'],False);
  211. end;
  212. procedure TTestParser.TestObject;
  213. begin
  214. DoTestObject('{}',[]);
  215. DoTestObject('{ "a" : 1 }',['a']);
  216. DoTestObject('{ "a" : 1, "B" : "String" }',['a','B']);
  217. DoTestObject('{ "a" : 1, "B" : {} }',['a','B']);
  218. DoTestObject('{ "a" : 1, "B" : { "c" : "d" } }',['a','B']);
  219. end;
  220. procedure TTestParser.DoTestObject(S : String; Const ElNames : Array of String; DoJSONTest : Boolean = True);
  221. Var
  222. P : TJSONParser;
  223. J : TJSONData;
  224. O : TJSONObject;
  225. I : Integer;
  226. begin
  227. P:=TJSONParser.Create(S);
  228. Try
  229. J:=P.Parse;
  230. If (J=Nil) then
  231. Fail('Parse of object "'+S+'" fails');
  232. TestJSONType(J,jtObject);
  233. TestItemCount(J,High(ElNames)-Low(ElNames)+1);
  234. O:=TJSONObject(J);
  235. For I:=Low(ElNames) to High(ElNames) do
  236. AssertEquals(Format('Element %d name',[I-Low(Elnames)])
  237. ,ElNames[i], O.Names[I-Low(ElNames)]);
  238. If DoJSONTest then
  239. self.TestJSON(J,S);
  240. Finally
  241. FreeAndNil(J);
  242. FreeAndNil(P);
  243. end;
  244. end;
  245. procedure TTestParser.DoTestArray(S : String; ACount : Integer);
  246. Var
  247. P : TJSONParser;
  248. J : TJSONData;
  249. begin
  250. P:=TJSONParser.Create(S);
  251. Try
  252. J:=P.Parse;
  253. If (J=Nil) then
  254. Fail('Parse of array "'+S+'" fails');
  255. TestJSONType(J,jtArray);
  256. TestItemCount(J,ACount);
  257. TestJSON(J,S);
  258. Finally
  259. FreeAndNil(J);
  260. FreeAndNil(P);
  261. end;
  262. end;
  263. procedure TTestParser.TestErrors;
  264. begin
  265. DoTestError('a');
  266. DoTestError('"b');
  267. DoTestError('1Tru');
  268. DoTestError('b"');
  269. DoTestError('{"a" : }');
  270. DoTestError('{"a" : ""');
  271. DoTestError('{"a : ""');
  272. DoTestError('[1,]');
  273. DoTestError('[,]');
  274. DoTestError('[,,]');
  275. DoTestError('[1,,]');
  276. end;
  277. procedure TTestParser.DoTestError(S : String);
  278. Var
  279. P : TJSONParser;
  280. J : TJSONData;
  281. ParseOK : Boolean;
  282. N : String;
  283. begin
  284. ParseOK:=False;
  285. P:=TJSONParser.Create(S);
  286. J:=Nil;
  287. Try
  288. Try
  289. Repeat
  290. FreeAndNil(J);
  291. J:=P.Parse;
  292. ParseOK:=True;
  293. If (J<>Nil) then
  294. N:=J.ClassName;
  295. Until (J=Nil)
  296. Finally
  297. FreeAndNil(J);
  298. FreeAndNil(P);
  299. end;
  300. except
  301. ParseOk:=False;
  302. end;
  303. If ParseOK then
  304. Fail('Parse of JSON string "'+S+'" should fail, but returned '+N);
  305. end;
  306. procedure TTestParser.DoTestString(S: String);
  307. Var
  308. P : TJSONParser;
  309. J : TJSONData;
  310. begin
  311. P:=TJSONParser.Create('"'+S+'"');
  312. Try
  313. J:=P.Parse;
  314. If (J=Nil) then
  315. Fail('Parse of string "'+S+'" fails');
  316. TestJSONType(J,jtString);
  317. TestAsString(J,JSONStringToString(S));
  318. TestJSON(J,'"'+S+'"');
  319. Finally
  320. FreeAndNil(J);
  321. FreeAndNil(P);
  322. end;
  323. end;
  324. procedure TTestParser.DoTestFloat(F : TJSONFloat);
  325. Var
  326. S : String;
  327. begin
  328. Str(F,S);
  329. DoTestFloat(F,S);
  330. end;
  331. procedure TTestParser.DoTestFloat(F : TJSONFloat; S : String);
  332. Var
  333. P : TJSONParser;
  334. J : TJSONData;
  335. begin
  336. P:=TJSONParser.Create(S);
  337. Try
  338. J:=P.Parse;
  339. If (J=Nil) then
  340. Fail('Parse of float '+S+' fails');
  341. TestJSONType(J,jtNumber);
  342. TestAsFloat(J,F);
  343. Finally
  344. FreeAndNil(J);
  345. FreeAndNil(P);
  346. end;
  347. end;
  348. initialization
  349. RegisterTest(TTestParser);
  350. end.