testregistry.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. 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 testregistry;
  12. {$mode objfpc}
  13. {$h+}
  14. interface
  15. uses
  16. 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(ATests: Array of TTestCaseClass);
  23. procedure RegisterTestDecorator(ADecoratorClass: TTestDecoratorClass; ATestClass: TTestCaseClass);
  24. function NumberOfRegisteredTests: longint;
  25. function GetTestRegistry: TTestSuite;
  26. implementation
  27. uses
  28. Classes
  29. ;
  30. var
  31. FTestRegistry: TTestSuite;
  32. function GetTestRegistry: TTestSuite;
  33. begin
  34. if not Assigned(FTestRegistry) then
  35. FTestRegistry := TTestSuite.Create;
  36. Result := FTestRegistry;
  37. end;
  38. procedure RegisterTestInSuite(ARootSuite: TTestSuite; APath: string; ATest: TTest);
  39. var
  40. i: Integer;
  41. lTargetSuite: TTestSuite;
  42. lCurrentTest: TTest;
  43. lSuiteName: String;
  44. lPathRemainder: String;
  45. lDotPos: Integer;
  46. lTests: TFPList;
  47. begin
  48. if APath = '' then
  49. begin
  50. // end recursion
  51. ARootSuite.AddTest(ATest);
  52. end
  53. else
  54. begin
  55. // Split the path on the dot (.)
  56. lDotPos := Pos('.', APath);
  57. if (lDotPos <= 0) then lDotPos := Pos('\', APath);
  58. if (lDotPos <= 0) then lDotPos := Pos('/', APath);
  59. if (lDotPos > 0) then
  60. begin
  61. lSuiteName := Copy(APath, 1, lDotPos - 1);
  62. lPathRemainder := Copy(APath, lDotPos + 1, length(APath) - lDotPos);
  63. end
  64. else
  65. begin
  66. lSuiteName := APath;
  67. lPathRemainder := '';
  68. end;
  69. // Check to see if the path already exists
  70. lTargetSuite := nil;
  71. lTests := ARootSuite.Tests;
  72. for i := 0 to lTests.Count -1 do
  73. begin
  74. lCurrentTest := TTest(lTests[i]);
  75. if lCurrentTest is TTestSuite then
  76. begin
  77. if (lCurrentTest.TestName = lSuiteName) then
  78. begin
  79. lTargetSuite := TTestSuite(lCurrentTest);
  80. break;
  81. end;
  82. end; { if }
  83. end; { for }
  84. if not Assigned(lTargetSuite) then
  85. begin
  86. lTargetSuite := TTestSuite.Create(lSuiteName);
  87. ARootSuite.AddTest(lTargetSuite);
  88. end;
  89. RegisterTestInSuite(lTargetSuite, lPathRemainder, ATest);
  90. end; { if/else }
  91. end;
  92. procedure RegisterTest(ATestClass: TTestCaseClass);
  93. begin
  94. GetTestRegistry.AddTestSuiteFromClass(ATestClass);
  95. end;
  96. procedure RegisterTest(ASuitePath: String; ATestClass: TTestCaseClass);
  97. begin
  98. RegisterTestInSuite(GetTestRegistry, ASuitePath, TTestSuite.Create(ATestClass));
  99. end;
  100. procedure RegisterTest(ASuitePath: String; ATest: TTest);
  101. begin
  102. RegisterTestInSuite(GetTestRegistry, ASuitePath, ATest);
  103. end;
  104. procedure RegisterTestDecorator(ADecoratorClass: TTestDecoratorClass; ATestClass: TTestCaseClass);
  105. begin
  106. GetTestRegistry.AddTest(ADecoratorClass.Create(TTestSuite.Create(ATestClass)));
  107. end;
  108. procedure RegisterTests(ATests: Array of TTestCaseClass);
  109. var
  110. i: integer;
  111. begin
  112. for i := Low(ATests) to High(ATests) do
  113. if Assigned(ATests[i]) then
  114. begin
  115. RegisterTest(ATests[i]);
  116. end;
  117. end;
  118. function NumberOfRegisteredTests: longint;
  119. begin
  120. Result := GetTestRegistry.CountTestCases;
  121. end;
  122. initialization
  123. FTestRegistry := nil;
  124. finalization
  125. FTestRegistry.Free;
  126. end.