ubmockobject.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. $Id$
  5. This file is part of the Free Component Library (FCL)
  6. Copyright (c) 2005 by Uberto Barbini
  7. Ultra basic implementation of mock objects for endo-testing
  8. with fpcunit.
  9. See the file COPYING.FPC, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. **********************************************************************}
  15. unit ubmockobject;
  16. interface
  17. uses
  18. Classes, SysUtils, fpcunit;
  19. type
  20. TUbMockObject = class(TObject)
  21. protected
  22. FSetUpMode: Boolean;
  23. FSetUpList: TStringList;
  24. FCallsList: TStringList;
  25. public
  26. constructor Create; virtual;
  27. destructor Destroy; override;
  28. procedure AddExpectation(const ASignatureCall: string);
  29. property SetUpMode: Boolean read FSetUpMode;
  30. procedure Verify;
  31. procedure StartSetUp;
  32. procedure EndSetUp;
  33. function UncoveredExpectations: integer;
  34. end;
  35. implementation
  36. { TUbMockObject }
  37. procedure TUbMockObject.AddExpectation(const ASignatureCall: string);
  38. begin
  39. if SetUpMode then
  40. FSetUpList.Add(ASignatureCall)
  41. else
  42. FCallsList.Add(ASignatureCall);
  43. end;
  44. function TUbMockObject.UncoveredExpectations: integer;
  45. begin
  46. Result := FSetUpList.Count - FCallsList.Count;
  47. end;
  48. constructor TUbMockObject.Create;
  49. begin
  50. FSetUpList := TStringList.Create;
  51. FCallsList := TStringList.Create;
  52. FSetUpMode := True;
  53. end;
  54. destructor TUbMockObject.Destroy;
  55. begin
  56. FSetUpList.Free;
  57. FCallsList.Free;
  58. inherited;
  59. end;
  60. procedure TUbMockObject.EndSetUp;
  61. begin
  62. FSetUpMode := False;
  63. FCallsList.Clear;
  64. end;
  65. procedure TUbMockObject.StartSetUp;
  66. begin
  67. FSetUpMode := True;
  68. FSetUpList.Clear;
  69. end;
  70. procedure TUbMockObject.Verify;
  71. var
  72. i: integer;
  73. s1, s2: string;
  74. begin
  75. TAssert.AssertEquals('Wrong number of expectations!', FSetUpList.Count, FCallsList.Count);
  76. for i := 0 to FSetUpList.Count - 1 do
  77. begin
  78. s1 := FSetUpList[i];
  79. s2 := FCallsList[i];
  80. TAssert.AssertEquals(s1, s2);
  81. end;
  82. FSetUpList.Clear;
  83. FCallsList.Clear;
  84. end;
  85. end.