123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- {
- This file is part of the Free Pascal run time library.
- Copyright (c) 1999-2022 by Michael van Canneyt and other members of the
- Free Pascal development team
- test report generator
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- unit tcreportgenerator;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes,
- SysUtils,
- fpcunit,
- testregistry,
- fpreport,
- udapp,
- fpTTF,
- fpjson,
- {demos}
- rptsimplelist,
- rptexpressions,
- rptgrouping,
- rptgrouping2,
- rptframes,
- rptimages,
- rptttf,
- rptshapes,
- rptdataset,
- rptcolumns,
- rptmasterdetail,
- rptjson,
- rptcontnr,
- rptnestedgroups,
- rptBarcode,
- rptQRcode;
- type
- { TTestDemos }
- TTestDemos = class(TTestCase)
- private
- FFilePath: String;
- procedure SaveJSON(pFileName: String; pJSON: TJSONData);
- protected
- procedure SetUp; override;
- procedure TearDown; override;
- procedure TestDemo(pName: String; pDemoAppClass: TReportDemoAppClass);
- published
- procedure SimpleList;
- procedure ExpressionDemo;
- procedure GroupingDemo;
- procedure Grouping2Demo;
- procedure FramesDemo;
- procedure ImagesDemo;
- procedure TTFDemo;
- procedure ShapesDemo;
- procedure DatasetDemo;
- procedure ColumnsDemo;
- procedure MasterDetailDemo;
- procedure JSONDemo;
- procedure CollectionDemo;
- procedure ObjectListDemo;
- procedure TestNestedGroupDemo;
- procedure BarcodeDemo;
- procedure QRCodeDemo;
- end;
- implementation
- uses
- fpjsonreport,
- jsonscanner,
- jsonparser;
- { TTestDemos }
- procedure TTestDemos.SaveJSON(pFileName: String; pJSON: TJSONData);
- var
- S: TFileStream;
- J: TJSONStringType;
- begin
- S:=TFileStream.Create(pFileName,fmCreate);
- try
- J:=pJSON.FormatJSON;
- S.WriteBuffer(J[1],Length(J));
- finally
- S.Free;
- end;
- end;
- procedure TTestDemos.SetUp;
- begin
- inherited SetUp;
- FFilePath:=ExtractFilePath(ParamStr(0));
- if not ForceDirectories(FFilePath+'rendered') then
- Fail('Could not create directory for rendered JSON');
- gTTFontCache.Clear;
- gTTFontCache.SearchPath.Clear;
- gTTFontCache.SearchPath.Add(FFilePath+'fonts/');
- gTTFontCache.SearchPath.Add(FFilePath+'../demos/fonts/');
- {$IFDEF UNIX}
- gTTFontCache.SearchPath.Add(GetUserDir + '.fonts/');
- gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/ubuntu-font-family/');
- gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/ubuntu/');
- gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/dejavu/');
- {$ENDIF}
- // ask to generate the font cache
- gTTFontCache.BuildFontCache;
- end;
- procedure TTestDemos.TearDown;
- begin
- inherited TearDown;
- end;
- procedure TTestDemos.TestDemo(pName: String; pDemoAppClass: TReportDemoAppClass);
- var
- lApp: TReportDemoApp;
- lSetJSON: TJSONData;
- lActualJSON: TJSONObject;
- S: TFileStream;
- P: TJSONParser;
- J: TJSONStringType;
- lEqual: Boolean;
- lSetFile, lActualFile: String;
- begin
- lSetFile:=FFilePath+'rendered'+PathDelim+pName+'.set.json';
- lActualFile:=FFilePath+'rendered'+PathDelim+pName+'.actual.json';
- lApp:=pDemoAppClass.Create(Nil);
- lActualJSON := TJSONObject.Create;
- try
- // delete old actual
- DeleteFile(lActualFile);
- // create Report
- lApp.TestInit;
- // run first time
- lApp.rpt.RunReport;
- lApp.rpt.SaveRenderToJSON(lActualJSON);
- // delete DateCreated
- lActualJSON.GetPath('Report.DateCreated').AsString := '';
- //SaveJSON(lSetFile, lActualJSON); // uncomment for regeneration after changes
- if Not FileExists(lSetFile) then
- begin
- SaveJSON(lSetFile, lActualJSON);
- Ignore('No previous test result available, saved result for reference');
- end;
- // load set report
- S:=TFileStream.Create(lSetFile,fmOpenRead);
- try
- P:=TJSONParser.Create(S, []);
- try
- lSetJSON:=TJSONObject(P.Parse);
- // compare reports
- lEqual := lSetJSON.AsJSON = lActualJSON.AsJSON;
- if not lEqual then
- SaveJSON(lActualFile, lActualJSON);
- AssertTrue('equal renders', lEqual);
- // run a second time
- lApp.rpt.RunReport;
- lActualJSON.Clear;
- lApp.rpt.SaveRenderToJSON(lActualJSON);
- // delete DateCreated
- lActualJSON.GetPath('Report.DateCreated').AsString := '';
- // compare reports
- lEqual := lSetJSON.AsJSON = lActualJSON.AsJSON;
- if not lEqual then
- SaveJSON(lActualFile, lActualJSON);
- AssertTrue('equal second renders', lEqual);
- finally
- lSetJSON.Free;
- P.Free;
- end;
- finally
- S.Free;
- end;
- finally
- lActualJSON.Free;
- lApp.Free;
- end;
- end;
- procedure TTestDemos.SimpleList;
- begin
- TestDemo('simplelist', TSimpleListDemo);
- end;
- procedure TTestDemos.ExpressionDemo;
- begin
- TestDemo('expression', TExpressionsDemo);
- end;
- procedure TTestDemos.GroupingDemo;
- begin
- TestDemo('grouping', TGroupingDemo);
- end;
- procedure TTestDemos.Grouping2Demo;
- begin
- TestDemo('grouping2', TGrouping2Demo);
- end;
- procedure TTestDemos.FramesDemo;
- begin
- TestDemo('frames', TFramesDemo);
- end;
- procedure TTestDemos.ImagesDemo;
- var
- cd: String;
- begin
- cd := GetCurrentDir;
- SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
- try
- TestDemo('images', TImagesDemo);
- finally
- SetCurrentDir(cd);
- end;
- end;
- procedure TTestDemos.TTFDemo;
- var
- cd: String;
- begin
- cd := GetCurrentDir;
- SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
- try
- TestDemo('ttf', TTTFDemo);
- finally
- SetCurrentDir(cd);
- end;
- end;
- procedure TTestDemos.ShapesDemo;
- begin
- TestDemo('shapes', TShapesDemo);
- end;
- procedure TTestDemos.DatasetDemo;
- var
- cd: String;
- begin
- cd := GetCurrentDir;
- SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
- try
- TestDemo('dataset', TDatasetDemo);
- finally
- SetCurrentDir(cd);
- end;
- end;
- procedure TTestDemos.ColumnsDemo;
- begin
- TestDemo('columns', TColumnsDemo)
- end;
- procedure TTestDemos.MasterDetailDemo;
- begin
- TestDemo('masterdetail', TMasterDetailDemo);
- end;
- procedure TTestDemos.JSONDemo;
- var
- cd: String;
- begin
- cd := GetCurrentDir;
- SetCurrentDir(cd+PathDelim+'..'+PathDelim+'demos');
- try
- TestDemo('json', TJSONDemo);
- finally
- SetCurrentDir(cd);
- end;
- end;
- procedure TTestDemos.CollectionDemo;
- begin
- TestDemo('collection', TCollectionDemo);
- end;
- procedure TTestDemos.ObjectListDemo;
- begin
- TestDemo('objectlist', TObjectListDemo);
- end;
- procedure TTestDemos.BarcodeDemo;
- begin
- TestDemo('barcode', TBarcodeDemo);
- end;
- procedure TTestDemos.QRCodeDemo;
- begin
- TestDemo('qrcode', TQRCodeDemo);
- end;
- procedure TTestDemos.TestNestedGroupDemo;
- begin
- TestDemo('nestedgroups', TNestedGroupsDemo);
- end;
- initialization
- RegisterTests(
- [TTestDemos
- ]);
- end.
|