ubmockobject.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. unit ubmockobject;
  15. interface
  16. uses
  17. Classes, SysUtils, fpcunit;
  18. type
  19. TUbMockObject = class(TObject)
  20. protected
  21. FSetUpMode: Boolean;
  22. FSetUpList: TStringList;
  23. FCallsList: TStringList;
  24. public
  25. constructor Create; virtual;
  26. destructor Destroy; override;
  27. procedure AddExpectation(const ASignatureCall: string);
  28. property SetUpMode: Boolean read FSetUpMode;
  29. procedure Verify;
  30. procedure StartSetUp;
  31. procedure EndSetUp;
  32. function UncoveredExpectations: integer;
  33. end;
  34. implementation
  35. { TUbMockObject }
  36. procedure TUbMockObject.AddExpectation(const ASignatureCall: string);
  37. begin
  38. if SetUpMode then
  39. FSetUpList.Add(ASignatureCall)
  40. else
  41. FCallsList.Add(ASignatureCall);
  42. end;
  43. function TUbMockObject.UncoveredExpectations: integer;
  44. begin
  45. Result := FSetUpList.Count - FCallsList.Count;
  46. end;
  47. constructor TUbMockObject.Create;
  48. begin
  49. FSetUpList := TStringList.Create;
  50. FCallsList := TStringList.Create;
  51. FSetUpMode := True;
  52. end;
  53. destructor TUbMockObject.Destroy;
  54. begin
  55. FSetUpList.Free;
  56. FCallsList.Free;
  57. inherited;
  58. end;
  59. procedure TUbMockObject.EndSetUp;
  60. begin
  61. FSetUpMode := False;
  62. FCallsList.Clear;
  63. end;
  64. procedure TUbMockObject.StartSetUp;
  65. begin
  66. FSetUpMode := True;
  67. FSetUpList.Clear;
  68. end;
  69. procedure TUbMockObject.Verify;
  70. var
  71. i: integer;
  72. s1, s2: string;
  73. begin
  74. TAssert.AssertEquals('Wrong number of expectations!', FSetUpList.Count, FCallsList.Count);
  75. for i := 0 to FSetUpList.Count - 1 do
  76. begin
  77. s1 := FSetUpList[i];
  78. s2 := FCallsList[i];
  79. TAssert.AssertEquals(s1, s2);
  80. end;
  81. FSetUpList.Clear;
  82. FCallsList.Clear;
  83. end;
  84. end.