testregistry.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2004 by Dean Zobec, Michael Van Canneyt
  4. Port to Free Pascal of the JUnit framework.
  5. Port to Pas2JS by Mattias Gaertner in 2017.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit TestRegistry;
  13. {$mode objfpc}
  14. interface
  15. uses
  16. Classes, FPCUnit, TestDecorator;
  17. type
  18. TTestDecoratorClass = class of TTestDecorator;
  19. procedure RegisterTest(ATestClass: TTestCaseClass); overload;
  20. procedure RegisterTest(ASuitePath: String; ATestClass: TTestCaseClass); overload;
  21. procedure RegisterTest(ASuitePath: String; ATest: TTest); overload;
  22. procedure RegisterTests(const ATests: Array of TTestCaseClass);
  23. procedure RegisterTests(ASuitePath: String; const ATests: Array of TTestCaseClass);
  24. procedure RegisterTestDecorator(ADecoratorClass: TTestDecoratorClass; ATestClass: TTestCaseClass);
  25. function NumberOfRegisteredTests: longint;
  26. function GetTestRegistry: TTestSuite;
  27. implementation
  28. var
  29. FTestRegistry: TTestSuite;
  30. procedure RegisterTestInSuite(ARootSuite: TTestSuite; APath: string; ATest: TTest);
  31. var
  32. i: Integer;
  33. lTargetSuite: TTestSuite;
  34. lCurrentTest: TTest;
  35. lSuiteName: String;
  36. lPathRemainder: String;
  37. lDotPos: Integer;
  38. begin
  39. if APath = '' then
  40. begin
  41. // end recursion
  42. ARootSuite.AddTest(ATest);
  43. end
  44. else
  45. begin
  46. // Split the path on the dot (.)
  47. lDotPos := Pos('.', APath);
  48. if (lDotPos <= 0) then lDotPos := Pos('\', APath);
  49. if (lDotPos <= 0) then lDotPos := Pos('/', APath);
  50. if (lDotPos > 0) then
  51. begin
  52. lSuiteName := Copy(APath, 1, lDotPos - 1);
  53. lPathRemainder := Copy(APath, lDotPos + 1, length(APath) - lDotPos);
  54. end
  55. else
  56. begin
  57. lSuiteName := APath;
  58. lPathRemainder := '';
  59. end;
  60. // Check to see if the path already exists
  61. lTargetSuite := nil;
  62. I:=0;
  63. While (lTargetSuite=Nil) and (I<ARootSuite.ChildTestCount) do
  64. begin
  65. lCurrentTest:= ARootSuite.Test[i];
  66. if lCurrentTest is TTestSuite then
  67. if (lCurrentTest.TestName = lSuiteName) then
  68. lTargetSuite := TTestSuite(lCurrentTest);
  69. Inc(I);
  70. end; { if }
  71. if not Assigned(lTargetSuite) then
  72. begin
  73. lTargetSuite := TTestSuite.Create(lSuiteName);
  74. ARootSuite.AddTest(lTargetSuite);
  75. end;
  76. RegisterTestInSuite(lTargetSuite, lPathRemainder, ATest);
  77. end; { if/else }
  78. end;
  79. procedure RegisterTest(ATestClass: TTestCaseClass);
  80. begin
  81. GetTestRegistry.AddTestSuiteFromClass(ATestClass);
  82. end;
  83. procedure RegisterTest(ASuitePath: String; ATestClass: TTestCaseClass);
  84. begin
  85. RegisterTestInSuite(GetTestRegistry, ASuitePath, TTestSuite.Create(ATestClass));
  86. end;
  87. procedure RegisterTest(ASuitePath: String; ATest: TTest);
  88. begin
  89. RegisterTestInSuite(GetTestRegistry, ASuitePath, ATest);
  90. end;
  91. procedure RegisterTests(const ATests: array of TTestCaseClass);
  92. var
  93. i: integer;
  94. begin
  95. for i := Low(ATests) to High(ATests) do
  96. if Assigned(ATests[i]) then
  97. RegisterTest(ATests[i]);
  98. end;
  99. procedure RegisterTests(ASuitePath: String;
  100. const ATests: array of TTestCaseClass);
  101. var
  102. i: integer;
  103. begin
  104. for i := Low(ATests) to High(ATests) do
  105. if Assigned(ATests[i]) then
  106. RegisterTest(ASuitePath,ATests[i]);
  107. end;
  108. procedure RegisterTestDecorator(ADecoratorClass: TTestDecoratorClass;
  109. ATestClass: TTestCaseClass);
  110. begin
  111. GetTestRegistry.AddTest(ADecoratorClass.Create(TTestSuite.Create(ATestClass)));
  112. end;
  113. function NumberOfRegisteredTests: longint;
  114. begin
  115. Result := GetTestRegistry.CountTestCases;
  116. end;
  117. function GetTestRegistry: TTestSuite;
  118. begin
  119. if not Assigned(FTestRegistry) then
  120. FTestRegistry := TTestSuite.Create;
  121. Result := FTestRegistry;
  122. end;
  123. initialization
  124. FTestRegistry := nil;
  125. end.