utcrundirtests.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2025 Michael Van Canneyt ([email protected])
  4. Run wasm consortium WIT testsuite tests
  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. unit utcrundirtests;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes,
  16. SysUtils,
  17. fpcunit,
  18. WIT.Model,
  19. WIT.Scanner,
  20. WIT.Parser;
  21. type
  22. { TSchemaFileTest }
  23. { TFileTest }
  24. TFileTest = class(TAssert)
  25. Private
  26. FIgnores: Boolean;
  27. FFileName : String;
  28. protected
  29. function GetTestName: string; override;
  30. function GetTestSuiteName: string; override;
  31. function GetEnableIgnores: boolean; override;
  32. procedure SetEnableIgnores(Value: boolean); override;
  33. procedure SetTestSuiteName(const aName: string); override;
  34. procedure TestFile; virtual;
  35. Public
  36. constructor Create(const aFileName: String);
  37. function CountTestCases: integer; override;
  38. procedure Run(AResult: TTestResult); override;
  39. property TestFileName : String Read FFileName;
  40. end;
  41. { TDirectoryFileTests }
  42. TDirectoryFileTests = class(TTestSuite)
  43. public
  44. Constructor CreateFromDir(const aDir : String);
  45. end;
  46. implementation
  47. function TFileTest.GetTestName: string;
  48. begin
  49. Result:=ChangeFileExt(ExtractFileName(FFileName),'')
  50. end;
  51. function TFileTest.GetTestSuiteName: string;
  52. begin
  53. Result:='';
  54. end;
  55. function TFileTest.GetEnableIgnores: boolean;
  56. begin
  57. Result:=FIgnores;
  58. end;
  59. procedure TFileTest.SetEnableIgnores(Value: boolean);
  60. begin
  61. FIgnores:=Value;
  62. end;
  63. procedure TFileTest.SetTestSuiteName(const aName: string);
  64. begin
  65. // Nothing
  66. end;
  67. procedure TFileTest.TestFile;
  68. var
  69. lScanner : TWITScanner;
  70. lParser : TWITParser;
  71. lStream : TStream;
  72. lDocument : TWITDocument;
  73. lErr : string;
  74. begin
  75. lScanner:=Nil;
  76. lParser:=Nil;
  77. lDocument:=Nil;
  78. lStream:=TFileStream.Create(FFileName,fmOpenRead or fmShareDenyWrite);
  79. try
  80. lScanner:=TWITScanner.Create(lStream);
  81. lParser:=TWITParser.Create(lScanner,True);
  82. try
  83. lScanner:=Nil;
  84. LDocument:=LParser.ParseDocument;
  85. except
  86. on E : exception do
  87. lErr:=E.ClassName+' : '+E.Message;;
  88. end;
  89. if lErr<>'' then
  90. Fail('Failed to parse '+TestFileName+' : '+lErr);
  91. finally
  92. LDocument.Free;
  93. lScanner.Free;
  94. lParser.Free;
  95. lStream.Free;
  96. end;
  97. end;
  98. constructor TFileTest.Create(const aFileName: String);
  99. begin
  100. FFileName:=aFileName;
  101. end;
  102. function TFileTest.CountTestCases: integer;
  103. begin
  104. Result:=1;
  105. end;
  106. Procedure DoRun(aTest: TTest; aResult: TTestResult);
  107. begin
  108. TFileTest(aTest).TestFile;
  109. end;
  110. procedure TFileTest.Run(AResult: TTestResult);
  111. begin
  112. aResult.StartTest(Self);
  113. FLastStep:=TTestStep.stRunTest;
  114. AResult.RunProtected(Self,@DoRun);
  115. aResult.EndTest(Self);
  116. end;
  117. { TDirectoryFileTests }
  118. constructor TDirectoryFileTests.CreateFromDir(const aDir: String);
  119. var
  120. lInfo : TSearchRec;
  121. lDir : String;
  122. begin
  123. inherited create('TestDir');
  124. lDir:=IncludeTrailingPathDelimiter(aDir);
  125. if FindFirst(lDir+'*.wit',0,lInfo)=0 then
  126. try
  127. Repeat
  128. AddTest(TFileTest.Create(lDir+lInfo.Name));
  129. until FindNext(lInfo)<>0;
  130. finally
  131. FindClose(lInfo);
  132. end;
  133. end;
  134. end.