tcreportgenerator.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. unit tcreportgenerator;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes,
  6. SysUtils,
  7. fpcunit,
  8. testregistry,
  9. fpreport,
  10. udapp,
  11. fpTTF,
  12. fpjson,
  13. {demos}
  14. rptsimplelist,
  15. rptexpressions,
  16. rptgrouping,
  17. rptgrouping2,
  18. rptframes,
  19. rptimages,
  20. rptttf,
  21. rptshapes,
  22. rptdataset,
  23. rptcolumns,
  24. rptmasterdetail,
  25. rptjson,
  26. rptcontnr,
  27. rptnestedgroups,
  28. rptBarcode,
  29. rptQRcode;
  30. type
  31. { TTestDemos }
  32. TTestDemos = class(TTestCase)
  33. private
  34. FFilePath: String;
  35. procedure SaveJSON(pFileName: String; pJSON: TJSONData);
  36. protected
  37. procedure SetUp; override;
  38. procedure TearDown; override;
  39. procedure TestDemo(pName: String; pDemoAppClass: TReportDemoAppClass);
  40. published
  41. procedure SimpleList;
  42. procedure ExpressionDemo;
  43. procedure GroupingDemo;
  44. procedure Grouping2Demo;
  45. procedure FramesDemo;
  46. procedure ImagesDemo;
  47. procedure TTFDemo;
  48. procedure ShapesDemo;
  49. procedure DatasetDemo;
  50. procedure ColumnsDemo;
  51. procedure MasterDetailDemo;
  52. procedure JSONDemo;
  53. procedure CollectionDemo;
  54. procedure ObjectListDemo;
  55. procedure TestNestedGroupDemo;
  56. procedure BarcodeDemo;
  57. procedure QRCodeDemo;
  58. end;
  59. implementation
  60. uses
  61. fpjsonreport,
  62. jsonscanner,
  63. jsonparser;
  64. { TTestDemos }
  65. procedure TTestDemos.SaveJSON(pFileName: String; pJSON: TJSONData);
  66. var
  67. S: TFileStream;
  68. J: TJSONStringType;
  69. begin
  70. S:=TFileStream.Create(pFileName,fmCreate);
  71. try
  72. J:=pJSON.FormatJSON;
  73. S.WriteBuffer(J[1],Length(J));
  74. finally
  75. S.Free;
  76. end;
  77. end;
  78. procedure TTestDemos.SetUp;
  79. begin
  80. inherited SetUp;
  81. FFilePath:=ExtractFilePath(ParamStr(0));
  82. if not ForceDirectories(FFilePath+'rendered') then
  83. Fail('Could not create directory for rendered JSON');
  84. gTTFontCache.Clear;
  85. gTTFontCache.SearchPath.Clear;
  86. gTTFontCache.SearchPath.Add(FFilePath+'fonts/');
  87. gTTFontCache.SearchPath.Add(FFilePath+'../demos/fonts/');
  88. {$IFDEF UNIX}
  89. gTTFontCache.SearchPath.Add(GetUserDir + '.fonts/');
  90. gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/ubuntu-font-family/');
  91. gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/ubuntu/');
  92. gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/dejavu/');
  93. {$ENDIF}
  94. // ask to generate the font cache
  95. gTTFontCache.BuildFontCache;
  96. end;
  97. procedure TTestDemos.TearDown;
  98. begin
  99. inherited TearDown;
  100. end;
  101. procedure TTestDemos.TestDemo(pName: String; pDemoAppClass: TReportDemoAppClass);
  102. var
  103. lApp: TReportDemoApp;
  104. lSetJSON: TJSONData;
  105. lActualJSON: TJSONObject;
  106. S: TFileStream;
  107. P: TJSONParser;
  108. J: TJSONStringType;
  109. lEqual: Boolean;
  110. lSetFile, lActualFile: String;
  111. begin
  112. lSetFile:=FFilePath+'rendered'+PathDelim+pName+'.set.json';
  113. lActualFile:=FFilePath+'rendered'+PathDelim+pName+'.actual.json';
  114. lApp:=pDemoAppClass.Create(Nil);
  115. lActualJSON := TJSONObject.Create;
  116. try
  117. // delete old actual
  118. DeleteFile(lActualFile);
  119. // create Report
  120. lApp.TestInit;
  121. // run first time
  122. lApp.rpt.RunReport;
  123. lApp.rpt.SaveRenderToJSON(lActualJSON);
  124. // delete DateCreated
  125. lActualJSON.GetPath('Report.DateCreated').AsString := '';
  126. //SaveJSON(lSetFile, lActualJSON); // uncomment for regeneration after changes
  127. if Not FileExists(lSetFile) then
  128. begin
  129. SaveJSON(lSetFile, lActualJSON);
  130. Ignore('No previous test result available, saved result for reference');
  131. end;
  132. // load set report
  133. S:=TFileStream.Create(lSetFile,fmOpenRead);
  134. try
  135. P:=TJSONParser.Create(S, []);
  136. try
  137. lSetJSON:=TJSONObject(P.Parse);
  138. // compare reports
  139. lEqual := lSetJSON.AsJSON = lActualJSON.AsJSON;
  140. if not lEqual then
  141. SaveJSON(lActualFile, lActualJSON);
  142. AssertTrue('equal renders', lEqual);
  143. // run a second time
  144. lApp.rpt.RunReport;
  145. lActualJSON.Clear;
  146. lApp.rpt.SaveRenderToJSON(lActualJSON);
  147. // delete DateCreated
  148. lActualJSON.GetPath('Report.DateCreated').AsString := '';
  149. // compare reports
  150. lEqual := lSetJSON.AsJSON = lActualJSON.AsJSON;
  151. if not lEqual then
  152. SaveJSON(lActualFile, lActualJSON);
  153. AssertTrue('equal second renders', lEqual);
  154. finally
  155. lSetJSON.Free;
  156. P.Free;
  157. end;
  158. finally
  159. S.Free;
  160. end;
  161. finally
  162. lActualJSON.Free;
  163. lApp.Free;
  164. end;
  165. end;
  166. procedure TTestDemos.SimpleList;
  167. begin
  168. TestDemo('simplelist', TSimpleListDemo);
  169. end;
  170. procedure TTestDemos.ExpressionDemo;
  171. begin
  172. TestDemo('expression', TExpressionsDemo);
  173. end;
  174. procedure TTestDemos.GroupingDemo;
  175. begin
  176. TestDemo('grouping', TGroupingDemo);
  177. end;
  178. procedure TTestDemos.Grouping2Demo;
  179. begin
  180. TestDemo('grouping2', TGrouping2Demo);
  181. end;
  182. procedure TTestDemos.FramesDemo;
  183. begin
  184. TestDemo('frames', TFramesDemo);
  185. end;
  186. procedure TTestDemos.ImagesDemo;
  187. var
  188. cd: String;
  189. begin
  190. cd := GetCurrentDir;
  191. SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
  192. try
  193. TestDemo('images', TImagesDemo);
  194. finally
  195. SetCurrentDir(cd);
  196. end;
  197. end;
  198. procedure TTestDemos.TTFDemo;
  199. var
  200. cd: String;
  201. begin
  202. cd := GetCurrentDir;
  203. SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
  204. try
  205. TestDemo('ttf', TTTFDemo);
  206. finally
  207. SetCurrentDir(cd);
  208. end;
  209. end;
  210. procedure TTestDemos.ShapesDemo;
  211. begin
  212. TestDemo('shapes', TShapesDemo);
  213. end;
  214. procedure TTestDemos.DatasetDemo;
  215. var
  216. cd: String;
  217. begin
  218. cd := GetCurrentDir;
  219. SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
  220. try
  221. TestDemo('dataset', TDatasetDemo);
  222. finally
  223. SetCurrentDir(cd);
  224. end;
  225. end;
  226. procedure TTestDemos.ColumnsDemo;
  227. begin
  228. TestDemo('columns', TColumnsDemo)
  229. end;
  230. procedure TTestDemos.MasterDetailDemo;
  231. begin
  232. TestDemo('masterdetail', TMasterDetailDemo);
  233. end;
  234. procedure TTestDemos.JSONDemo;
  235. var
  236. cd: String;
  237. begin
  238. cd := GetCurrentDir;
  239. SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
  240. try
  241. TestDemo('json', TJSONDemo);
  242. finally
  243. SetCurrentDir(cd);
  244. end;
  245. end;
  246. procedure TTestDemos.CollectionDemo;
  247. begin
  248. TestDemo('collection', TCollectionDemo);
  249. end;
  250. procedure TTestDemos.ObjectListDemo;
  251. begin
  252. TestDemo('objectlist', TObjectListDemo);
  253. end;
  254. procedure TTestDemos.BarcodeDemo;
  255. begin
  256. TestDemo('barcode', TBarcodeDemo);
  257. end;
  258. procedure TTestDemos.QRCodeDemo;
  259. begin
  260. TestDemo('qrcode', TQRCodeDemo);
  261. end;
  262. procedure TTestDemos.TestNestedGroupDemo;
  263. begin
  264. TestDemo('nestedgroups', TNestedGroupsDemo);
  265. end;
  266. initialization
  267. RegisterTests(
  268. [TTestDemos
  269. ]);
  270. end.