utcjson.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. unit utcjson;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, testregistry, System.JSON;
  6. type
  7. { TTestJSONObject }
  8. TTestJSONObject= class(TTestCase)
  9. private
  10. FPair: TJSONPair;
  11. FValue: TJSONValue;
  12. protected
  13. procedure SetUp; override;
  14. procedure TearDown; override;
  15. Property Value : TJSONValue Read FValue Write FValue;
  16. Property Pair : TJSONPair Read FPair Write FPair;
  17. published
  18. procedure TestHookUp;
  19. Procedure TestNull;
  20. Procedure TestParseNull;
  21. Procedure TestBoolean;
  22. Procedure TestParseBoolean;
  23. Procedure TestBoolTrue;
  24. Procedure TestParseBoolTrue;
  25. Procedure TestBoolFalse;
  26. Procedure TestParseBoolFalse;
  27. Procedure TestString;
  28. Procedure TestStringNull;
  29. Procedure TestParseString;
  30. Procedure TestNumberInt;
  31. Procedure TestParseNumberInt;
  32. Procedure TestNumberInt64;
  33. Procedure TestParseNumberInt64;
  34. Procedure TestNumberFloat;
  35. Procedure TestParseNumberFloat;
  36. Procedure TestJSONPairPair;
  37. Procedure TestJSONPairStringString;
  38. Procedure TestJSONPairStringValue;
  39. Procedure TestJSONPairStringInt;
  40. Procedure TestJSONPairStringBool;
  41. Procedure TestJSONPairStringFloat;
  42. Procedure TestJSONPairStringInt64;
  43. Procedure TestJSONObjectEmpty;
  44. Procedure TestJSONObjectOneElement;
  45. Procedure TestJSONObjectTwoElements;
  46. Procedure TestJSONParseObjectEmpty;
  47. Procedure TestJSONParseObjectOneElement;
  48. Procedure TestJSONParseObjectTwoElements;
  49. Procedure TestJSONArrayEmpty;
  50. Procedure TestJSONArrayOneElement;
  51. Procedure TestJSONArrayTwoElements;
  52. Procedure TestFindEmptyValue;
  53. Procedure TestFindObjectName;
  54. Procedure TestFindObjectIndexName;
  55. Procedure TestFindObjectNameRecurse;
  56. Procedure TestFindArrayIndex;
  57. Procedure TestFindArrayName;
  58. end;
  59. { TTestJSONPathParser }
  60. TTestJSONPathParser = class(TTestCase)
  61. private
  62. FParser: TJSONPathParser;
  63. protected
  64. procedure SetUp; override;
  65. procedure TearDown; override;
  66. Procedure AssertEquals(Msg : string; aExpected, aActual : TJSONPathToken); overload;
  67. Property Parser : TJSONPathParser Read FParser;
  68. published
  69. Procedure TestHookup;
  70. Procedure TestEof;
  71. Procedure TestName;
  72. Procedure TestIndexNumeric;
  73. Procedure TestIndexName;
  74. Procedure TestDotName;
  75. end;
  76. implementation
  77. uses typinfo;
  78. procedure TTestJSONObject.TestHookUp;
  79. begin
  80. AssertNull('No value',value);
  81. end;
  82. procedure TTestJSONObject.TestNull;
  83. begin
  84. Value:=TJSONNull.Create;
  85. AssertEquals('ToString','null',Value.ToString);
  86. AssertEquals('ToJSON','null',Value.ToJSON);
  87. AssertEquals('Format','null',Value.Format);
  88. AssertEquals('Null',True,Value.Null);
  89. end;
  90. procedure TTestJSONObject.TestParseNull;
  91. begin
  92. Value:=TJSONValue.ParseJSONValue('null');
  93. AssertEquals('Class',TJSONNull,Value.ClassType);
  94. end;
  95. procedure TTestJSONObject.TestBoolean;
  96. begin
  97. Value:=TJSONBool.Create(True);
  98. AssertEquals('ToString','true',Value.ToString);
  99. AssertEquals('ToJSON','true',Value.ToJSON);
  100. AssertEquals('Format','true',Value.Format);
  101. AssertEquals('Null',False,Value.Null);
  102. end;
  103. procedure TTestJSONObject.TestParseBoolean;
  104. begin
  105. Value:=TJSONValue.ParseJSONValue('true',False);
  106. AssertEquals('Class',TJSONBool,Value.ClassType);
  107. AssertEquals('ToString','true',Value.ToString);
  108. end;
  109. procedure TTestJSONObject.TestBoolTrue;
  110. begin
  111. Value:=TJSONTrue.Create;
  112. AssertEquals('ToString','true',Value.ToString);
  113. AssertEquals('ToJSON','true',Value.ToJSON);
  114. AssertEquals('Format','true',Value.Format);
  115. AssertEquals('Null',False,Value.Null);
  116. end;
  117. procedure TTestJSONObject.TestParseBoolTrue;
  118. begin
  119. Value:=TJSONValue.ParseJSONValue('true',True);
  120. AssertEquals('Class',TJSONTrue,Value.ClassType);
  121. AssertEquals('ToString','true',Value.ToString);
  122. end;
  123. procedure TTestJSONObject.TestBoolFalse;
  124. begin
  125. Value:=TJSONfalse.Create;
  126. AssertEquals('ToString','false',Value.ToString);
  127. AssertEquals('ToJSON','false',Value.ToJSON);
  128. AssertEquals('Format','false',Value.Format);
  129. AssertEquals('Null',False,Value.Null);
  130. end;
  131. procedure TTestJSONObject.TestParseBoolFalse;
  132. begin
  133. Value:=TJSONValue.ParseJSONValue('false',True);
  134. AssertEquals('Class',TJSONFalse,Value.ClassType);
  135. AssertEquals('ToString','false',Value.ToString);
  136. end;
  137. procedure TTestJSONObject.TestString;
  138. begin
  139. Value:=TJSONString.Create('string');
  140. AssertEquals('ToString','"string"',Value.ToString);
  141. AssertEquals('ToJSON','"string"',Value.ToJSON);
  142. AssertEquals('Format','"string"',Value.Format);
  143. AssertEquals('Null',False,Value.Null);
  144. end;
  145. procedure TTestJSONObject.TestStringNull;
  146. begin
  147. Value:=TJSONString.Create();
  148. AssertEquals('ToString','null',Value.ToString);
  149. AssertEquals('Null',True,Value.Null);
  150. end;
  151. procedure TTestJSONObject.TestParseString;
  152. begin
  153. Value:=TJSONValue.ParseJSONValue('"string"',True);
  154. AssertEquals('Class',TJSONString,Value.ClassType);
  155. AssertEquals('ToString','"string"',Value.ToString);
  156. end;
  157. procedure TTestJSONObject.TestNumberInt;
  158. begin
  159. Value:=TJSONNumber.Create(12);
  160. AssertEquals('ToString','12',Value.ToString);
  161. AssertEquals('ToJSON','12',Value.ToJSON);
  162. AssertEquals('Format','12',Value.Format);
  163. AssertEquals('Null',False,Value.Null);
  164. end;
  165. procedure TTestJSONObject.TestParseNumberInt;
  166. begin
  167. Value:=TJSONValue.ParseJSONValue('12',True);
  168. AssertEquals('Class',TJSONNumber,Value.ClassType);
  169. AssertEquals('ToString','12',Value.ToString);
  170. end;
  171. procedure TTestJSONObject.TestNumberInt64;
  172. begin
  173. Value:=TJSONNumber.Create(Int64(12));
  174. AssertEquals('ToString','12',Value.ToString);
  175. AssertEquals('ToJSON','12',Value.ToJSON);
  176. AssertEquals('Format','12',Value.Format);
  177. AssertEquals('Null',False,Value.Null);
  178. end;
  179. procedure TTestJSONObject.TestParseNumberInt64;
  180. begin
  181. Value:=TJSONValue.ParseJSONValue('1221212121111',True);
  182. AssertEquals('Class',TJSONNumber,Value.ClassType);
  183. AssertEquals('ToString','1221212121111',Value.ToString);
  184. end;
  185. procedure TTestJSONObject.TestNumberFloat;
  186. var
  187. V : String;
  188. begin
  189. V:=FloatToStr(12.34,TFormatSettings.Invariant);
  190. Value:=TJSONNumber.Create(12.34);
  191. AssertEquals('ToString',V,Value.ToString);
  192. AssertEquals('ToJSON',V,Value.ToJSON);
  193. AssertEquals('Format',V,Value.Format);
  194. AssertEquals('Null',False,Value.Null);
  195. end;
  196. procedure TTestJSONObject.TestParseNumberFloat;
  197. var
  198. V : String;
  199. begin
  200. V:=FloatToStr(12.34,TFormatSettings.Invariant);
  201. Value:=TJSONValue.ParseJSONValue('12.34',True);
  202. AssertEquals('Class',TJSONNumber,Value.ClassType);
  203. AssertEquals('ToString',V,Value.ToString);
  204. end;
  205. procedure TTestJSONObject.TestJSONPairPair;
  206. begin
  207. Pair:=TJSONPair.Create(TJSONString.Create('a'),TJSONNumber.Create('12'));
  208. AssertEquals('ToString','"a":12',Pair.ToString);
  209. AssertEquals('ToJSON','"a":12',Pair.ToJSON);
  210. AssertEquals('Format','"a":12',Pair.Format);
  211. AssertEquals('Null',False,Pair.Null);
  212. end;
  213. procedure TTestJSONObject.TestJSONPairStringString;
  214. begin
  215. Pair:=TJSONPair.Create('a','b');
  216. AssertEquals('ToString','"a":"b"',Pair.ToString);
  217. AssertEquals('ToJSON','"a":"b"',Pair.ToJSON);
  218. AssertEquals('Format','"a":"b"',Pair.Format);
  219. AssertEquals('Null',False,Pair.Null);
  220. end;
  221. procedure TTestJSONObject.TestJSONPairStringValue;
  222. begin
  223. Pair:=TJSONPair.Create('a',TJSONString.Create('b'));
  224. AssertEquals('ToString','"a":"b"',Pair.ToString);
  225. AssertEquals('ToJSON','"a":"b"',Pair.ToJSON);
  226. AssertEquals('Format','"a":"b"',Pair.Format);
  227. AssertEquals('Null',False,Pair.Null);
  228. end;
  229. procedure TTestJSONObject.TestJSONPairStringInt;
  230. begin
  231. Pair:=TJSONPair.Create('a',1);
  232. AssertEquals('ToString','"a":1',Pair.ToString);
  233. AssertEquals('ToJSON','"a":1',Pair.ToJSON);
  234. AssertEquals('Format','"a":1',Pair.Format);
  235. AssertEquals('Null',False,Pair.Null);
  236. end;
  237. procedure TTestJSONObject.TestJSONPairStringBool;
  238. begin
  239. Pair:=TJSONPair.Create('a',True);
  240. AssertEquals('ToString','"a":true',Pair.ToString);
  241. AssertEquals('ToJSON','"a":true',Pair.ToJSON);
  242. AssertEquals('Format','"a":true',Pair.Format);
  243. AssertEquals('Null',False,Pair.Null);
  244. end;
  245. procedure TTestJSONObject.TestJSONPairStringFloat;
  246. var
  247. V : String;
  248. begin
  249. V:=FloatToStr(12.34,TFormatSettings.Invariant);
  250. Pair:=TJSONPair.Create('a',12.34);
  251. AssertEquals('ToString','"a":'+V,Pair.ToString);
  252. AssertEquals('ToJSON','"a":'+V,Pair.ToJSON);
  253. AssertEquals('Format','"a":'+V,Pair.Format);
  254. AssertEquals('Null',False,Pair.Null);
  255. end;
  256. procedure TTestJSONObject.TestJSONPairStringInt64;
  257. begin
  258. Pair:=TJSONPair.Create('a',1221212121111);
  259. AssertEquals('ToString','"a":1221212121111',Pair.ToString);
  260. AssertEquals('ToJSON','"a":1221212121111',Pair.ToJSON);
  261. AssertEquals('Format','"a":1221212121111',Pair.Format);
  262. AssertEquals('Null',False,Pair.Null);
  263. end;
  264. procedure TTestJSONObject.TestJSONObjectEmpty;
  265. begin
  266. Value:=TJSONObject.Create;
  267. AssertEquals('ToString','{}',Value.ToString);
  268. AssertEquals('ToJSON','{}',Value.ToJSON);
  269. AssertEquals('Format','{'+sLineBreak+'}',Value.Format);
  270. AssertEquals('Null',False,Value.Null);
  271. end;
  272. procedure TTestJSONObject.TestJSONObjectOneElement;
  273. begin
  274. Value:=TJSONObject.Create(TJSONPair.Create('a','b'));
  275. AssertEquals('ToString','{"a":"b"}',Value.ToString);
  276. AssertEquals('ToJSON','{"a":"b"}',Value.ToJSON);
  277. AssertEquals('Format','{'+sLineBreak+' "a":"b"'+sLineBreak+'}',Value.Format);
  278. AssertEquals('Null',False,Value.Null);
  279. end;
  280. procedure TTestJSONObject.TestJSONObjectTwoElements;
  281. var
  282. O : TJSONObject;
  283. begin
  284. O:=TJSONObject.Create(TJSONPair.Create('a','b'));
  285. O.AddPair('c','d');
  286. AssertEquals('Count',2,O.Count);
  287. Value:=O;
  288. AssertEquals('ToString','{"a":"b","c":"d"}',Value.ToString);
  289. AssertEquals('ToJSON','{"a":"b","c":"d"}',Value.ToJSON);
  290. AssertEquals('Format','{'+sLineBreak+
  291. ' "a":"b",'+sLineBreak+
  292. ' "c":"d"'+sLineBreak+'}',Value.Format);
  293. AssertEquals('Null',False,Value.Null);
  294. end;
  295. procedure TTestJSONObject.TestJSONParseObjectEmpty;
  296. begin
  297. Value:=TJSONValue.ParseJSONValue('{}',True);
  298. AssertEquals('Class',TJSONObject,Value.ClassType);
  299. AssertEquals('ToString','{}',Value.ToString);
  300. end;
  301. procedure TTestJSONObject.TestJSONParseObjectOneElement;
  302. begin
  303. Value:=TJSONValue.ParseJSONValue('{"a":"b"}',True);
  304. AssertEquals('Class',TJSONObject,Value.ClassType);
  305. AssertEquals('ToString','{"a":"b"}',Value.ToString);
  306. end;
  307. procedure TTestJSONObject.TestJSONParseObjectTwoElements;
  308. begin
  309. Value:=TJSONValue.ParseJSONValue('{"a":"b","c":"d"}',True);
  310. AssertEquals('Class',TJSONObject,Value.ClassType);
  311. AssertEquals('ToString','{"a":"b","c":"d"}',Value.ToString);
  312. end;
  313. procedure TTestJSONObject.TestJSONArrayEmpty;
  314. begin
  315. Value:=TJSONArray.Create;
  316. AssertEquals('ToString','[]',Value.ToString);
  317. AssertEquals('ToJSON','[]',Value.ToJSON);
  318. AssertEquals('Format','['+sLineBreak+']',Value.Format);
  319. AssertEquals('Null',False,Value.Null);
  320. end;
  321. procedure TTestJSONObject.TestJSONArrayOneElement;
  322. begin
  323. Value:=TJSONArray.Create(TJSONNumber.Create(1));
  324. AssertEquals('ToString','[1]',Value.ToString);
  325. AssertEquals('ToJSON','[1]',Value.ToJSON);
  326. AssertEquals('Format','['+sLineBreak+' 1'+sLineBreak+']',Value.Format);
  327. AssertEquals('Null',False,Value.Null);
  328. end;
  329. procedure TTestJSONObject.TestJSONArrayTwoElements;
  330. begin
  331. Value:=TJSONArray.Create(TJSONNumber.Create(1),TJSONNumber.Create(2));
  332. AssertEquals('ToString','[1,2]',Value.ToString);
  333. AssertEquals('ToJSON','[1,2]',Value.ToJSON);
  334. AssertEquals('Format','['+sLineBreak+
  335. ' 1,'+sLineBreak+
  336. ' 2'+sLineBreak+
  337. ']',Value.Format);
  338. AssertEquals('Null',False,Value.Null);
  339. end;
  340. procedure TTestJSONObject.TestFindEmptyValue;
  341. begin
  342. FValue:=TJSONString.Create('Name');
  343. AssertSame('Empty returns same',FValue,FValue.FindValue(''));
  344. end;
  345. procedure TTestJSONObject.TestFindObjectName;
  346. var
  347. V : TJSONValue;
  348. begin
  349. FValue:=TJSONObject.Create(TJSONPair.Create('a','b'));
  350. V:=FValue.FindValue('a');
  351. AssertNotNull('Have JSON value',V);
  352. AssertEquals('Have correct value','b',V.Value);
  353. V:=FValue.FindValue('c');
  354. AssertNull('Have no JSON value',V);
  355. end;
  356. procedure TTestJSONObject.TestFindObjectIndexName;
  357. var
  358. V : TJSONValue;
  359. begin
  360. FValue:=TJSONObject.Create(TJSONPair.Create('a','b'));
  361. V:=FValue.FindValue('["a"]');
  362. AssertNotNull('Have JSON value',V);
  363. AssertEquals('Have correct value','b',V.Value);
  364. V:=FValue.FindValue('c');
  365. AssertNull('Have no JSON value',V);
  366. end;
  367. procedure TTestJSONObject.TestFindObjectNameRecurse;
  368. var
  369. V : TJSONValue;
  370. begin
  371. V:=TJSONObject.Create(TJSONPair.Create('b','c'));
  372. FValue:=TJSONObject.Create(TJSONPair.Create('a',v));
  373. V:=FValue.FindValue('a.b');
  374. AssertNotNull('Have JSON value',V);
  375. AssertEquals('Have correct value','c',V.Value);
  376. V:=FValue.FindValue('a.c');
  377. AssertNull('Have no JSON value',V);
  378. end;
  379. procedure TTestJSONObject.TestFindArrayIndex;
  380. var
  381. V : TJSONValue;
  382. begin
  383. FValue:=TJSONArray.Create('a','b');
  384. V:=FValue.FindValue('[0]');
  385. AssertNotNull('Have JSON value',V);
  386. AssertEquals('Have correct value','a',V.Value);
  387. V:=FValue.FindValue('[1]');
  388. AssertNotNull('Have JSON value',V);
  389. AssertEquals('Have correct value','b',V.Value);
  390. V:=FValue.FindValue('[-1]');
  391. AssertNull('Have no JSON value -1',V);
  392. V:=FValue.FindValue('[2]');
  393. AssertNull('Have no JSON value 2',V);
  394. end;
  395. procedure TTestJSONObject.TestFindArrayName;
  396. var
  397. V : TJSONValue;
  398. begin
  399. FValue:=TJSONArray.Create('a','b');
  400. V:=FValue.FindValue('a');
  401. AssertNull('Have no JSON value',V);
  402. end;
  403. procedure TTestJSONObject.SetUp;
  404. begin
  405. FreeAndNil(Fvalue);
  406. FreeAndNil(FPair);
  407. end;
  408. procedure TTestJSONObject.TearDown;
  409. begin
  410. FreeAndNil(FPair);
  411. FreeAndNil(Fvalue);
  412. end;
  413. { TTestJSONPathParser }
  414. procedure TTestJSONPathParser.SetUp;
  415. begin
  416. inherited SetUp;
  417. FParser:=Default(TJSONPathParser);
  418. end;
  419. procedure TTestJSONPathParser.TearDown;
  420. begin
  421. FParser:=Default(TJSONPathParser);
  422. inherited TearDown;
  423. end;
  424. procedure TTestJSONPathParser.AssertEquals(Msg: string; aExpected, aActual: TJSONPathToken);
  425. begin
  426. AssertEquals(Msg,GetEnumName(TypeInfo(TJSONPathParser.TToken),Ord(aExpected)),
  427. GetEnumName(TypeInfo(TJSONPathParser.TToken),Ord(aActual)));
  428. end;
  429. procedure TTestJSONPathParser.TestHookup;
  430. begin
  431. AssertEquals('Default', TJSONPathToken.UnDefined, FParser.Token);
  432. end;
  433. procedure TTestJSONPathParser.TestEof;
  434. begin
  435. FParser:=TJSONPathParser.Create('');
  436. AssertEquals('Empty is eof ',TJSONPathToken.Eof,FParser.NextToken);
  437. AssertTrue('eof',FParser.IsEOF);
  438. end;
  439. procedure TTestJSONPathParser.TestName;
  440. begin
  441. FParser:=TJSONPathParser.Create('name');
  442. AssertEquals('Type',TJSONPathToken.Name,FParser.NextToken);
  443. AssertEquals('Value','name',FParser.TokenName);
  444. AssertTrue('eof',FParser.IsEOF);
  445. end;
  446. procedure TTestJSONPathParser.TestIndexNumeric;
  447. begin
  448. FParser:=TJSONPathParser.Create('[12]');
  449. AssertEquals('Type',TJSONPathToken.ArrayIndex,FParser.NextToken);
  450. AssertEquals('Value',12,FParser.TokenArrayIndex);
  451. AssertTrue('eof',FParser.IsEOF);
  452. end;
  453. procedure TTestJSONPathParser.TestIndexName;
  454. begin
  455. FParser:=TJSONPathParser.Create('["name"]');
  456. AssertEquals('Type',TJSONPathToken.name,FParser.NextToken);
  457. AssertEquals('Value','name',FParser.TokenName);
  458. AssertTrue('eof',FParser.IsEOF);
  459. end;
  460. procedure TTestJSONPathParser.TestDotName;
  461. begin
  462. FParser:=TJSONPathParser.Create('.name');
  463. AssertEquals('Type',TJSONPathToken.Name,FParser.NextToken);
  464. AssertEquals('Value','name',FParser.TokenName);
  465. AssertTrue('eof',FParser.IsEOF);
  466. end;
  467. initialization
  468. RegisterTests([TTestJSONObject,TTestJSONPathParser]);
  469. end.