testdecorator.pp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2005 by Dean Zobec
  4. Decorators for fpcunit tests and one-time TTestSetup implementation
  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 testdecorator;
  12. {$mode objfpc}
  13. {$h+}
  14. interface
  15. uses
  16. Classes, SysUtils, fpcunit;
  17. type
  18. { TTestDecorator }
  19. TTestDecorator = class(TTest{Assert})
  20. private
  21. FTest: TTest;
  22. protected
  23. function GetTestName: string; override;
  24. function GetTestSuiteName: string; override;
  25. procedure SetTestSuiteName(const aName: string); override;
  26. function GetEnableIgnores: boolean; override;
  27. procedure SetEnableIgnores(Value: boolean); override;
  28. public
  29. Function GetChildTest(AIndex: Integer): TTest; override;
  30. Function GetChildTestCount : Integer; override;
  31. function CountTestCases: integer; override;
  32. constructor Create(aTest: TTest); reintroduce; overload;
  33. destructor Destroy; override;
  34. procedure BasicRun(AResult: TTestResult); virtual;
  35. procedure Run(AResult: TTestResult); override;
  36. property Test: TTest read FTest;
  37. end;
  38. { TTestSetup }
  39. TTestSetup = class(TTestDecorator)
  40. protected
  41. procedure OneTimeSetup; virtual; abstract;
  42. procedure OneTimeTearDown; virtual; abstract;
  43. public
  44. procedure Run(AResult: TTestResult); override;
  45. end;
  46. implementation
  47. { TTestDecorator }
  48. function TTestDecorator.GetTestName: string;
  49. begin
  50. Result := FTest.TestName;
  51. end;
  52. function TTestDecorator.GetTestSuiteName: string;
  53. begin
  54. Result := FTest.TestSuiteName;
  55. end;
  56. procedure TTestDecorator.SetTestSuiteName(const aName: string);
  57. begin
  58. FTest.TestSuiteName := aName;
  59. end;
  60. function TTestDecorator.GetEnableIgnores: boolean;
  61. begin
  62. result := FTest.EnableIgnores;
  63. end;
  64. procedure TTestDecorator.SetEnableIgnores(Value: boolean);
  65. begin
  66. FTest.EnableIgnores := Value;
  67. end;
  68. function TTestDecorator.GetChildTest(AIndex: Integer): TTest;
  69. begin
  70. Result:=FTest.GetChildTest(AIndex);
  71. end;
  72. function TTestDecorator.GetChildTestCount: Integer;
  73. begin
  74. Result:=FTest.GetChildTestCount;
  75. end;
  76. function TTestDecorator.CountTestCases: integer;
  77. begin
  78. Result := FTest.CountTestCases;
  79. end;
  80. constructor TTestDecorator.Create(aTest: TTest);
  81. begin
  82. inherited Create;
  83. FTest := aTest;
  84. end;
  85. destructor TTestDecorator.Destroy;
  86. begin
  87. FTest.Free;
  88. inherited Destroy;
  89. end;
  90. procedure TTestDecorator.BasicRun(AResult: TTestResult);
  91. begin
  92. FTest.Run(AResult);
  93. end;
  94. procedure TTestDecorator.Run(AResult: TTestResult);
  95. begin
  96. BasicRun(AResult);
  97. end;
  98. procedure OneTimeProtect(aTest: TTest; aResult: TTestResult);
  99. begin
  100. if aTest is TTestSetup then
  101. begin
  102. TTestSetup(aTest).OneTimeSetup;
  103. TTestSetup(aTest).BasicRun(aResult);
  104. TTestSetup(aTest).OneTimeTearDown;
  105. end;
  106. end;
  107. { TTestSetup }
  108. procedure TTestSetup.Run(AResult: TTestResult);
  109. begin
  110. AResult.RunProtected(Self, @OneTimeProtect);
  111. end;
  112. end.