Debian Codegraph @

tests/CppUTestExt/MockHierarchyTest.cpp

raw
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "MockFailureReporterForTest.h"
30
31 TEST_GROUP(MockHierarchyTest)
32 {
33 void teardown()
34 {
35 mock().clear();
36 }
37 };
38
39 TEST(MockHierarchyTest, getMockSupportScope)
40 {
41 MockSupport* mock1 = mock().getMockSupportScope("name");
42 MockSupport* mock2 = mock().getMockSupportScope("differentName");
43
44 CHECK(!mock().hasData("name"));
45 CHECK(mock1 != mock2);
46 POINTERS_EQUAL(mock1, mock().getMockSupportScope("name"));
47 CHECK(mock1 != &mock());
48 }
49
50 TEST(MockHierarchyTest, usingTwoMockSupportsByName)
51 {
52 mock("first").expectOneCall("boo");
53
54 LONGS_EQUAL(0, mock("other").expectedCallsLeft());
55 LONGS_EQUAL(1, mock("first").expectedCallsLeft());
56
57 mock("first").clear();
58 }
59
60 TEST(MockHierarchyTest, EnableDisableWorkHierarchically)
61 {
62 mock("first");
63
64 mock().disable();
65 mock("first").expectOneCall("boo");
66 LONGS_EQUAL(0, mock("first").expectedCallsLeft());
67
68 mock().enable();
69 mock("first").expectOneCall("boo");
70 LONGS_EQUAL(1, mock("first").expectedCallsLeft());
71
72 mock("first").clear();
73 }
74
75 TEST(MockHierarchyTest, EnableDisableWorkHierarchicallyWhenSupportIsDynamicallyCreated)
76 {
77 mock().disable();
78 mock("first").expectOneCall("boo");
79 LONGS_EQUAL(0, mock("first").expectedCallsLeft());
80
81 mock().enable();
82 mock("second").expectOneCall("boo");
83 LONGS_EQUAL(1, mock("second").expectedCallsLeft());
84
85 mock().clear();
86 }
87
88 TEST(MockHierarchyTest, ExpectedCallsLeftWorksHierarchically)
89 {
90 mock("first").expectOneCall("foobar");
91 LONGS_EQUAL(1, mock().expectedCallsLeft());
92
93 mock().clear();
94 }
95
96 TEST(MockHierarchyTest, checkExpectationsWorksHierarchically)
97 {
98 MockFailureReporterInstaller failureReporterInstaller;
99
100 MockExpectedCallsListForTest expectations;
101 expectations.addFunction("first::foobar");
102 expectations.addFunction("second::helloworld");
103 MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
104
105 mock("first").expectOneCall("foobar");
106 mock("second").expectOneCall("helloworld");
107
108 mock().checkExpectations();
109 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
110 }
111
112 TEST(MockHierarchyTest, ignoreOtherCallsWorksHierarchically)
113 {
114 mock("first");
115 mock().ignoreOtherCalls();
116 mock("first").actualCall("boo");
117
118 mock().checkExpectations();
119 }
120
121 TEST(MockHierarchyTest, ignoreOtherCallsWorksHierarchicallyWhenDynamicallyCreated)
122 {
123 mock().ignoreOtherCalls();
124 mock("first").actualCall("boo");
125
126 mock().checkExpectations();
127 }
128
129 TEST(MockHierarchyTest, checkExpectationsWorksHierarchicallyForLastCallNotFinished)
130 {
131 MockFailureReporterInstaller failureReporterInstaller;
132
133 MockExpectedCallsListForTest expectations;
134 expectations.addFunction("first::foobar")->withParameter("boo", 1);
135 MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "first::foobar", expectations);
136
137 mock("first").expectOneCall("foobar").withParameter("boo", 1);
138 mock("first").actualCall("foobar");
139
140 mock().checkExpectations();
141 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
142 }
143
144 TEST(MockHierarchyTest, reporterIsInheritedInHierarchicalMocks)
145 {
146 MockFailureReporterInstaller failureReporterInstaller;
147 MockExpectedCallsListForTest expectations;
148
149 mock("differentScope").actualCall("foobar");
150
151 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "differentScope::foobar", expectations);
152 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
153 }
154