testdecorator.pp 2.5 KB

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