| 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 "CppUTestExt/MockExpectedCallsList.h" |
| 30 | #include "CppUTestExt/MockCheckedExpectedCall.h" |
| 31 | #include "CppUTestExt/MockFailure.h" |
| 32 | #include "MockFailureReporterForTest.h" |
| 33 | |
| 34 | TEST_GROUP(MockExpectedCallsList) |
| 35 | { |
| 36 | MockExpectedCallsList * list; |
| 37 | MockCheckedExpectedCall* call1; |
| 38 | MockCheckedExpectedCall* call2; |
| 39 | MockCheckedExpectedCall* call3; |
| 40 | MockCheckedExpectedCall* call4; |
| 41 | void setup() |
| 42 | { |
| 43 | list = new MockExpectedCallsList; |
| 44 | call1 = new MockCheckedExpectedCall; |
| 45 | call2 = new MockCheckedExpectedCall; |
| 46 | call3 = new MockCheckedExpectedCall; |
| 47 | call4 = new MockCheckedExpectedCall; |
| 48 | call1->withName("foo"); |
| 49 | call2->withName("bar"); |
| 50 | call3->withName("boo"); |
| 51 | } |
| 52 | void teardown() |
| 53 | { |
| 54 | delete call1; |
| 55 | delete call2; |
| 56 | delete call3; |
| 57 | delete call4; |
| 58 | delete list; |
| 59 | CHECK_NO_MOCK_FAILURE(); |
| 60 | MockFailureReporterForTest::clearReporter(); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | TEST(MockExpectedCallsList, emptyList) |
| 65 | { |
| 66 | CHECK(! list->hasUnfulfilledExpectations()); |
| 67 | CHECK(! list->hasFinalizedMatchingExpectations()); |
| 68 | LONGS_EQUAL(0, list->size()); |
| 69 | } |
| 70 | |
| 71 | TEST(MockExpectedCallsList, addingCalls) |
| 72 | { |
| 73 | list->addExpectedCall(call1); |
| 74 | list->addExpectedCall(call2); |
| 75 | LONGS_EQUAL(2, list->size()); |
| 76 | } |
| 77 | |
| 78 | TEST(MockExpectedCallsList, listWithFulfilledExpectationHasNoUnfulfilledOnes) |
| 79 | { |
| 80 | call1->callWasMade(1); |
| 81 | call2->callWasMade(2); |
| 82 | list->addExpectedCall(call1); |
| 83 | list->addExpectedCall(call2); |
| 84 | CHECK(! list->hasUnfulfilledExpectations()); |
| 85 | } |
| 86 | |
| 87 | TEST(MockExpectedCallsList, listWithFulfilledExpectationButOutOfOrder) |
| 88 | { |
| 89 | call1->withCallOrder(1); |
| 90 | call2->withCallOrder(2); |
| 91 | list->addExpectedCall(call1); |
| 92 | list->addExpectedCall(call2); |
| 93 | call2->callWasMade(1); |
| 94 | call1->callWasMade(2); |
| 95 | CHECK(! list->hasUnfulfilledExpectations()); |
| 96 | CHECK(list->hasCallsOutOfOrder()); |
| 97 | } |
| 98 | |
| 99 | TEST(MockExpectedCallsList, listWithUnFulfilledExpectationHasNoUnfillfilledOnes) |
| 100 | { |
| 101 | call1->callWasMade(1); |
| 102 | call3->callWasMade(2); |
| 103 | list->addExpectedCall(call1); |
| 104 | list->addExpectedCall(call2); |
| 105 | list->addExpectedCall(call3); |
| 106 | CHECK(list->hasUnfulfilledExpectations()); |
| 107 | } |
| 108 | |
| 109 | TEST(MockExpectedCallsList, deleteAllExpectationsAndClearList) |
| 110 | { |
| 111 | list->addExpectedCall(new MockCheckedExpectedCall); |
| 112 | list->addExpectedCall(new MockCheckedExpectedCall); |
| 113 | list->deleteAllExpectationsAndClearList(); |
| 114 | } |
| 115 | |
| 116 | TEST(MockExpectedCallsList, onlyKeepUnmatchingExpectations) |
| 117 | { |
| 118 | call1->withName("relate"); |
| 119 | call2->withName("unrelate"); |
| 120 | call3->withName("relate").withParameter("param",1); |
| 121 | list->addExpectedCall(call1); |
| 122 | list->addExpectedCall(call2); |
| 123 | list->addExpectedCall(call3); |
| 124 | list->onlyKeepUnmatchingExpectations(); |
| 125 | LONGS_EQUAL(1, list->size()); |
| 126 | } |
| 127 | |
| 128 | TEST(MockExpectedCallsList, onlyKeepExpectationsRelatedTo) |
| 129 | { |
| 130 | call1->withName("relate"); |
| 131 | call2->withName("unrelate"); |
| 132 | call3->withName("unrelate"); |
| 133 | list->addExpectedCall(call1); |
| 134 | list->addExpectedCall(call2); |
| 135 | list->addExpectedCall(call3); |
| 136 | list->onlyKeepExpectationsRelatedTo("relate"); |
| 137 | LONGS_EQUAL(1, list->size()); |
| 138 | } |
| 139 | |
| 140 | TEST(MockExpectedCallsList, removeAllExpectationsExceptThisThatRelateToTheWoleList) |
| 141 | { |
| 142 | call1->withName("relate"); |
| 143 | call2->withName("relate"); |
| 144 | call3->withName("relate"); |
| 145 | list->addExpectedCall(call1); |
| 146 | list->addExpectedCall(call2); |
| 147 | list->addExpectedCall(call3); |
| 148 | list->onlyKeepExpectationsRelatedTo("unrelate"); |
| 149 | LONGS_EQUAL(0, list->size()); |
| 150 | } |
| 151 | |
| 152 | TEST(MockExpectedCallsList, removeAllExpectationsExceptThisThatRelateToFirstOne) |
| 153 | { |
| 154 | call1->withName("relate"); |
| 155 | call2->withName("unrelate"); |
| 156 | list->addExpectedCall(call1); |
| 157 | list->addExpectedCall(call2); |
| 158 | list->onlyKeepExpectationsRelatedTo("unrelate"); |
| 159 | LONGS_EQUAL(1, list->size()); |
| 160 | } |
| 161 | |
| 162 | TEST(MockExpectedCallsList, removeAllExpectationsExceptThisThatRelateToLastOne) |
| 163 | { |
| 164 | call1->withName("unrelate"); |
| 165 | call2->withName("relate"); |
| 166 | list->addExpectedCall(call1); |
| 167 | list->addExpectedCall(call2); |
| 168 | list->onlyKeepExpectationsRelatedTo("unrelate"); |
| 169 | LONGS_EQUAL(1, list->size()); |
| 170 | } |
| 171 | |
| 172 | TEST(MockExpectedCallsList, onlyKeepExpectationsWithInputParameterName) |
| 173 | { |
| 174 | call1->withName("func").withParameter("param", 1); |
| 175 | call2->withName("func").withParameter("diffname", 1); |
| 176 | call3->withName("func").withParameter("diffname", 1); |
| 177 | list->addExpectedCall(call1); |
| 178 | list->addExpectedCall(call2); |
| 179 | list->addExpectedCall(call3); |
| 180 | list->onlyKeepExpectationsWithInputParameterName("diffname"); |
| 181 | LONGS_EQUAL(2, list->size()); |
| 182 | } |
| 183 | |
| 184 | TEST(MockExpectedCallsList, onlyKeepExpectationsWithInputParameter) |
| 185 | { |
| 186 | MockNamedValue parameter("diffname"); |
| 187 | parameter.setValue(1); |
| 188 | call1->withName("func").withParameter("param", 1); |
| 189 | call2->withName("func").withParameter("diffname", 1); |
| 190 | call3->withName("func").withParameter("diffname", 1); |
| 191 | call4->withName("func").withParameter("diffname", 2); |
| 192 | call3->callWasMade(1); |
| 193 | call3->inputParameterWasPassed("diffname"); |
| 194 | list->addExpectedCall(call1); |
| 195 | list->addExpectedCall(call2); |
| 196 | list->addExpectedCall(call3); |
| 197 | list->addExpectedCall(call4); |
| 198 | list->onlyKeepExpectationsWithInputParameter(parameter); |
| 199 | LONGS_EQUAL(2, list->size()); |
| 200 | } |
| 201 | |
| 202 | TEST(MockExpectedCallsList, addPotentiallyMatchingExpectationsWithEmptyList) |
| 203 | { |
| 204 | MockExpectedCallsList newList; |
| 205 | newList.addPotentiallyMatchingExpectations(*list); |
| 206 | LONGS_EQUAL(0, newList.size()); |
| 207 | } |
| 208 | |
| 209 | TEST(MockExpectedCallsList, addPotentiallyMatchingExpectationsMultipleUnmatchedExpectations) |
| 210 | { |
| 211 | call2->callWasMade(1); |
| 212 | list->addExpectedCall(call1); |
| 213 | list->addExpectedCall(call2); |
| 214 | list->addExpectedCall(call3); |
| 215 | MockExpectedCallsList newList; |
| 216 | newList.addPotentiallyMatchingExpectations(*list); |
| 217 | LONGS_EQUAL(2, newList.size()); |
| 218 | } |
| 219 | |
| 220 | TEST(MockExpectedCallsList, amountOfActualCallsFulfilledFor_HasOneRelated) |
| 221 | { |
| 222 | call1->withName("foo"); |
| 223 | call1->callWasMade(1); |
| 224 | call2->withName("bar"); |
| 225 | call2->callWasMade(2); |
| 226 | list->addExpectedCall(call1); |
| 227 | list->addExpectedCall(call2); |
| 228 | LONGS_EQUAL(1, list->amountOfActualCallsFulfilledFor("bar")); |
| 229 | } |
| 230 | |
| 231 | TEST(MockExpectedCallsList, amountOfActualCallsFulfilledFor_HasNone) |
| 232 | { |
| 233 | call1->withName("foo"); |
| 234 | call1->callWasMade(1); |
| 235 | call2->withName("bar"); |
| 236 | list->addExpectedCall(call1); |
| 237 | LONGS_EQUAL(0, list->amountOfActualCallsFulfilledFor("bar")); |
| 238 | } |
| 239 | |
| 240 | TEST(MockExpectedCallsList, callToStringForUnfulfilledFunctions) |
| 241 | { |
| 242 | call1->withName("foo"); |
| 243 | call2->withName("bar"); |
| 244 | call3->withName("blah"); |
| 245 | call3->callWasMade(1); |
| 246 | |
| 247 | list->addExpectedCall(call1); |
| 248 | list->addExpectedCall(call2); |
| 249 | list->addExpectedCall(call3); |
| 250 | |
| 251 | SimpleString expectedString; |
| 252 | expectedString = StringFromFormat("%s\n%s", call1->callToString().asCharString(), call2->callToString().asCharString()); |
| 253 | STRCMP_EQUAL(expectedString.asCharString(), list->unfulfilledCallsToString().asCharString()); |
| 254 | } |
| 255 | |
| 256 | TEST(MockExpectedCallsList, callToStringForFulfilledFunctions) |
| 257 | { |
| 258 | call1->withName("foo"); |
| 259 | call2->withName("bar"); |
| 260 | |
| 261 | call2->callWasMade(1); |
| 262 | call1->callWasMade(2); |
| 263 | |
| 264 | list->addExpectedCall(call1); |
| 265 | list->addExpectedCall(call2); |
| 266 | |
| 267 | SimpleString expectedString; |
| 268 | expectedString = StringFromFormat("%s\n%s", call1->callToString().asCharString(), call2->callToString().asCharString()); |
| 269 | STRCMP_EQUAL(expectedString.asCharString(), list->fulfilledCallsToString().asCharString()); |
| 270 | } |
| 271 | |
| 272 | TEST(MockExpectedCallsList, removeOneFinalizedMatchingExpectationFromEmptyList) |
| 273 | { |
| 274 | POINTERS_EQUAL(NULLPTR, list->removeFirstFinalizedMatchingExpectation()); |
| 275 | } |
| 276 | |
| 277 | TEST(MockExpectedCallsList, getOneMatchingExpectationFromEmptyList) |
| 278 | { |
| 279 | POINTERS_EQUAL(NULLPTR, list->getFirstMatchingExpectation()); |
| 280 | } |
| 281 | |
| 282 | TEST(MockExpectedCallsList, toStringOnEmptyList) |
| 283 | { |
| 284 | STRCMP_EQUAL("<none>", list->unfulfilledCallsToString().asCharString()); |
| 285 | } |
| 286 | |
| 287 | TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_emptyList) |
| 288 | { |
| 289 | CHECK(! list->hasFinalizedMatchingExpectations()); |
| 290 | } |
| 291 | |
| 292 | TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_listHasNonMatchingCalls) |
| 293 | { |
| 294 | call1->withParameter("param", 0); |
| 295 | call2->withParameter("param", 0); |
| 296 | call3->withParameter("param", 0); |
| 297 | list->addExpectedCall(call1); |
| 298 | list->addExpectedCall(call2); |
| 299 | list->addExpectedCall(call3); |
| 300 | |
| 301 | CHECK(! list->hasFinalizedMatchingExpectations()); |
| 302 | } |
| 303 | |
| 304 | TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_listHasMatchingButNotFinalizedCall) |
| 305 | { |
| 306 | list->addExpectedCall(call1); |
| 307 | list->addExpectedCall(call2); |
| 308 | call1->ignoreOtherParameters(); |
| 309 | call2->withParameter("param", 0); |
| 310 | |
| 311 | CHECK(! list->hasFinalizedMatchingExpectations()); |
| 312 | } |
| 313 | |
| 314 | TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_listHasFinalizedCallThatIgnoresParameters) |
| 315 | { |
| 316 | list->addExpectedCall(call1); |
| 317 | list->addExpectedCall(call2); |
| 318 | call1->ignoreOtherParameters(); |
| 319 | call2->withParameter("param", 0); |
| 320 | |
| 321 | call1->finalizeActualCallMatch(); |
| 322 | |
| 323 | CHECK(list->hasFinalizedMatchingExpectations()); |
| 324 | } |
| 325 | |
| 326 | TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations_listHasFinalizedCallThatDoesntIgnoreParameters) |
| 327 | { |
| 328 | list->addExpectedCall(call1); |
| 329 | list->addExpectedCall(call2); |
| 330 | call1->withParameter("param", 1); |
| 331 | call2->withParameter("param", 0); |
| 332 | |
| 333 | call1->inputParameterWasPassed("param"); |
| 334 | |
| 335 | CHECK(list->hasFinalizedMatchingExpectations()); |
| 336 | } |