summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-fpcunit/src/exampletests/testmockobject.pp
blob: dcea5715d67dcba05f417776b6fb61a44513f757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{$mode objfpc}
{$h+}
{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 2005 Uberto Barbini

    mock object tests

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit testmockobject;

interface

uses
  Classes, SysUtils, fpcunit, testutils, testregistry, ubmockobject;

type

  TTestMock= class(TTestCase)
  protected
    _MockObj: TUbMockObject;
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestRightCalls;
    procedure TestWrongCallCount;
    procedure TestWrongOrderOfCalls;
    procedure TestWrongSignatures;
    procedure TestExpectations;
    procedure TestResetExpectations;
  end;

implementation

{ TTestMock }

procedure TTestMock.SetUp;
begin
  _MockObj := TUbMockObject.Create;
end;

procedure TTestMock.TearDown;
begin
  _MockObj.Free;
end;

procedure TTestMock.TestRightCalls;
begin
  _MockObj.StartSetUp;
  _MockObj.AddExpectation('abc');
  _MockObj.AddExpectation('123');
  _MockObj.AddExpectation('The quick brown fox jumped over the lazy dog.');
  AssertEquals(3, _MockObj.UncoveredExpectations);
  _MockObj.EndSetUp;
  _MockObj.AddExpectation('abc');
  _MockObj.AddExpectation('123');
  _MockObj.AddExpectation('The quick brown fox jumped over the lazy dog.');
  _MockObj.Verify;
  AssertEquals(0, _MockObj.UncoveredExpectations);
end;

procedure TTestMock.TestWrongCallCount;
begin
  _MockObj.StartSetUp;
  _MockObj.EndSetUp;
  _MockObj.AddExpectation('abc');
  AssertException(EAssertionFailedError, @_MockObj.Verify);
  AssertEquals(-1, _MockObj.UncoveredExpectations);

  _MockObj.StartSetUp;
  _MockObj.AddExpectation('abc');
  _MockObj.EndSetUp;
  AssertException(EAssertionFailedError, @_MockObj.Verify);
  AssertEquals(1, _MockObj.UncoveredExpectations);
end;

procedure TTestMock.TestWrongSignatures;
begin
  _MockObj.StartSetUp;
  _MockObj.AddExpectation('aaa');
  _MockObj.EndSetUp;
  _MockObj.AddExpectation('bbb');
  AssertException(EAssertionFailedError, @_MockObj.Verify);
end;

procedure TTestMock.TestWrongOrderOfCalls;
begin
  _MockObj.StartSetUp;
  _MockObj.AddExpectation('abc');
  _MockObj.AddExpectation('123');
  _MockObj.AddExpectation('the quick brown fox jumped over the lazy dog');
  _MockObj.EndSetUp;
  _MockObj.AddExpectation('abc');
  _MockObj.AddExpectation('the quick brown fox jumped over the lazy dog');
  _MockObj.AddExpectation('123');
  AssertException(EAssertionFailedError, @_MockObj.Verify);
end;

procedure TTestMock.TestExpectations;
begin
  AssertTrue(_MockObj.SetUpMode);
  _MockObj.StartSetUp;
  _MockObj.EndSetUp;
  AssertTrue(not _MockObj.SetUpMode);
  _MockObj.Verify;
end;

procedure TTestMock.TestResetExpectations;
begin
  _MockObj.AddExpectation('abc');
  _MockObj.StartSetUp;
  _MockObj.AddExpectation('123');
  _MockObj.EndSetUp;
  _MockObj.AddExpectation('123');
  _MockObj.Verify;
end;

initialization
  RegisterTests([TTestMock]);
end.