testdecorator.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. This file is part of the Free Component Library (FCL)
  5. Copyright (c) 2005 by Dean Zobec
  6. Decorators for fpcunit tests and one-time TTestSetup implementation
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit testdecorator;
  14. interface
  15. uses
  16. Classes, SysUtils, fpcunit;
  17. type
  18. { TTestDecorator }
  19. TTestDecorator = class(TAssert)
  20. private
  21. FTest: TTest;
  22. function GetTestName: string; override;
  23. function GetTestSuiteName: string; override;
  24. procedure SetTestSuiteName(const aName: string); override;
  25. public
  26. function CountTestCases: integer; override;
  27. constructor Create(aTest: TTest); reintroduce; overload;
  28. destructor Destroy; override;
  29. procedure BasicRun(AResult: TTestResult); virtual;
  30. procedure Run(AResult: TTestResult); override;
  31. property Test: TTest read FTest;
  32. end;
  33. { TTestSetup }
  34. TTestSetup = class(TTestDecorator)
  35. protected
  36. procedure OneTimeSetup; virtual; abstract;
  37. procedure OneTimeTearDown; virtual; abstract;
  38. public
  39. procedure Run(AResult: TTestResult); override;
  40. end;
  41. implementation
  42. { TTestDecorator }
  43. function TTestDecorator.GetTestName: string;
  44. begin
  45. Result := FTest.TestName;
  46. end;
  47. function TTestDecorator.GetTestSuiteName: string;
  48. begin
  49. Result := FTest.TestSuiteName;
  50. end;
  51. procedure TTestDecorator.SetTestSuiteName(const aName: string);
  52. begin
  53. FTest.TestSuiteName := aName;
  54. end;
  55. function TTestDecorator.CountTestCases: integer;
  56. begin
  57. Result := FTest.CountTestCases;
  58. end;
  59. constructor TTestDecorator.Create(aTest: TTest);
  60. begin
  61. inherited Create;
  62. FTest := aTest;
  63. end;
  64. destructor TTestDecorator.Destroy;
  65. begin
  66. FTest.Free;
  67. inherited Destroy;
  68. end;
  69. procedure TTestDecorator.BasicRun(AResult: TTestResult);
  70. begin
  71. FTest.Run(AResult);
  72. end;
  73. procedure TTestDecorator.Run(AResult: TTestResult);
  74. begin
  75. BasicRun(AResult);
  76. end;
  77. procedure OneTimeProtect(aTest: TTest; aResult: TTestResult);
  78. begin
  79. if aTest is TTestSetup then
  80. begin
  81. TTestSetup(aTest).OneTimeSetup;
  82. TTestSetup(aTest).BasicRun(aResult);
  83. TTestSetup(aTest).OneTimeTearDown;
  84. end;
  85. end;
  86. { TTestSetup }
  87. procedure TTestSetup.Run(AResult: TTestResult);
  88. begin
  89. AResult.RunProtected(Self, @OneTimeProtect);
  90. end;
  91. end.