testjsonparser.pp 7.7 KB

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