ubmockobject.pp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. This file is part of the Free Component Library (FCL)
  5. Copyright (c) 2005 by Uberto Barbini
  6. Ultra basic implementation of mock objects for endo-testing
  7. with fpcunit.
  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. {$IFNDEF FPC_DOTTEDUNITS}
  15. unit ubmockobject;
  16. {$ENDIF FPC_DOTTEDUNITS}
  17. interface
  18. {$IFDEF FPC_DOTTEDUNITS}
  19. uses
  20. System.Classes, System.SysUtils, FpcUnit.Test;
  21. {$ELSE FPC_DOTTEDUNITS}
  22. uses
  23. Classes, SysUtils, fpcunit;
  24. {$ENDIF FPC_DOTTEDUNITS}
  25. type
  26. TUbMockObject = class(TObject)
  27. protected
  28. FSetUpMode: Boolean;
  29. FSetUpList: TStringList;
  30. FCallsList: TStringList;
  31. public
  32. constructor Create; virtual;
  33. destructor Destroy; override;
  34. procedure AddExpectation(const ASignatureCall: string);
  35. property SetUpMode: Boolean read FSetUpMode;
  36. procedure Verify;
  37. procedure StartSetUp;
  38. procedure EndSetUp;
  39. function UncoveredExpectations: integer;
  40. end;
  41. implementation
  42. { TUbMockObject }
  43. procedure TUbMockObject.AddExpectation(const ASignatureCall: string);
  44. begin
  45. if SetUpMode then
  46. FSetUpList.Add(ASignatureCall)
  47. else
  48. FCallsList.Add(ASignatureCall);
  49. end;
  50. function TUbMockObject.UncoveredExpectations: integer;
  51. begin
  52. Result := FSetUpList.Count - FCallsList.Count;
  53. end;
  54. constructor TUbMockObject.Create;
  55. begin
  56. FSetUpList := TStringList.Create;
  57. FCallsList := TStringList.Create;
  58. FSetUpMode := True;
  59. end;
  60. destructor TUbMockObject.Destroy;
  61. begin
  62. FSetUpList.Free;
  63. FCallsList.Free;
  64. inherited;
  65. end;
  66. procedure TUbMockObject.EndSetUp;
  67. begin
  68. FSetUpMode := False;
  69. FCallsList.Clear;
  70. end;
  71. procedure TUbMockObject.StartSetUp;
  72. begin
  73. FSetUpMode := True;
  74. FSetUpList.Clear;
  75. end;
  76. procedure TUbMockObject.Verify;
  77. var
  78. i: integer;
  79. s1, s2: string;
  80. begin
  81. TAssert.AssertEquals('Wrong number of expectations!', FSetUpList.Count, FCallsList.Count);
  82. for i := 0 to FSetUpList.Count - 1 do
  83. begin
  84. s1 := FSetUpList[i];
  85. s2 := FCallsList[i];
  86. TAssert.AssertEquals(s1, s2);
  87. end;
  88. FSetUpList.Clear;
  89. FCallsList.Clear;
  90. end;
  91. end.