testdecorator.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 TestDecorator;
  13. {$mode objfpc}
  14. interface
  15. uses
  16. Classes, SysUtils, FPCUnit;
  17. type
  18. { TTestDecorator }
  19. TTestDecorator = class(TAssert)
  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 CountTestCases: integer; override;
  30. constructor Create(aTest: TTest); reintroduce; overload;
  31. destructor Destroy; override;
  32. procedure BasicRun(AResult: TTestResult); virtual;
  33. procedure Run(AResult: TTestResult); override;
  34. property Test: TTest read FTest;
  35. end;
  36. { TTestSetup }
  37. TTestSetup = class(TTestDecorator)
  38. protected
  39. procedure OneTimeSetup; virtual; abstract;
  40. procedure OneTimeTearDown; virtual; abstract;
  41. public
  42. procedure Run(AResult: TTestResult); override;
  43. end;
  44. implementation
  45. { TTestDecorator }
  46. function TTestDecorator.GetTestName: string;
  47. begin
  48. Result := FTest.TestName;
  49. end;
  50. function TTestDecorator.GetTestSuiteName: string;
  51. begin
  52. Result := FTest.TestSuiteName;
  53. end;
  54. procedure TTestDecorator.SetTestSuiteName(const aName: string);
  55. begin
  56. FTest.TestSuiteName := aName;
  57. end;
  58. function TTestDecorator.GetEnableIgnores: boolean;
  59. begin
  60. Result := FTest.EnableIgnores;
  61. end;
  62. procedure TTestDecorator.SetEnableIgnores(Value: boolean);
  63. begin
  64. FTest.EnableIgnores := Value;
  65. end;
  66. function TTestDecorator.CountTestCases: integer;
  67. begin
  68. Result := FTest.CountTestCases;
  69. end;
  70. constructor TTestDecorator.Create(aTest: TTest);
  71. begin
  72. inherited Create;
  73. FTest := aTest;
  74. end;
  75. destructor TTestDecorator.Destroy;
  76. begin
  77. FreeAndNil(FTest);
  78. inherited Destroy;
  79. end;
  80. procedure TTestDecorator.BasicRun(AResult: TTestResult);
  81. begin
  82. FTest.Run(AResult);
  83. end;
  84. procedure TTestDecorator.Run(AResult: TTestResult);
  85. begin
  86. BasicRun(AResult);
  87. end;
  88. procedure OneTimeProtect(aTest: TTest; aResult: TTestResult);
  89. begin
  90. if aTest is TTestSetup then
  91. begin
  92. TTestSetup(aTest).OneTimeSetup;
  93. TTestSetup(aTest).BasicRun(aResult);
  94. TTestSetup(aTest).OneTimeTearDown;
  95. end;
  96. end;
  97. { TTestSetup }
  98. procedure TTestSetup.Run(AResult: TTestResult);
  99. begin
  100. AResult.RunProtected(Self, @OneTimeProtect);
  101. end;
  102. end.