| 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/MockCheckedExpectedCall.h" |
| 30 | #include "CppUTestExt/MockFailure.h" |
| 31 | #include "MockFailureReporterForTest.h" |
| 32 | |
| 33 | class TypeForTestingExpectedFunctionCall |
| 34 | { |
| 35 | public: |
| 36 | TypeForTestingExpectedFunctionCall(int val) |
| 37 | { |
| 38 | value = new int(val); |
| 39 | } |
| 40 | virtual ~TypeForTestingExpectedFunctionCall() |
| 41 | { |
| 42 | delete value; |
| 43 | } |
| 44 | int *value; |
| 45 | }; |
| 46 | |
| 47 | class TypeForTestingExpectedFunctionCallComparator : public MockNamedValueComparator |
| 48 | { |
| 49 | public: |
| 50 | virtual bool isEqual(const void* object1, const void* object2) |
| 51 | { |
| 52 | const TypeForTestingExpectedFunctionCall* obj1 = (const TypeForTestingExpectedFunctionCall*) object1; |
| 53 | const TypeForTestingExpectedFunctionCall* obj2 = (const TypeForTestingExpectedFunctionCall*) object2; |
| 54 | return *(obj1->value) == *(obj2->value); |
| 55 | } |
| 56 | virtual SimpleString valueToString(const void* object) |
| 57 | { |
| 58 | const TypeForTestingExpectedFunctionCall* obj = (const TypeForTestingExpectedFunctionCall*) object; |
| 59 | return StringFrom(*(obj->value)); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | class TypeForTestingExpectedFunctionCallCopier : public MockNamedValueCopier |
| 64 | { |
| 65 | public: |
| 66 | virtual void copy(void* dst_, const void* src_) |
| 67 | { |
| 68 | TypeForTestingExpectedFunctionCall* dst = (TypeForTestingExpectedFunctionCall*) dst_; |
| 69 | const TypeForTestingExpectedFunctionCall* src = (const TypeForTestingExpectedFunctionCall*) src_; |
| 70 | *(dst->value) = *(src->value); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | TEST_GROUP(MockNamedValueHandlerRepository) |
| 75 | { |
| 76 | void teardown() |
| 77 | { |
| 78 | CHECK_NO_MOCK_FAILURE(); |
| 79 | MockFailureReporterForTest::clearReporter(); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | TEST(MockNamedValueHandlerRepository, getComparatorForNonExistingName) |
| 84 | { |
| 85 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 86 | POINTERS_EQUAL(NULLPTR, repository.getComparatorForType("typeName")); |
| 87 | } |
| 88 | |
| 89 | TEST(MockNamedValueHandlerRepository, installComparator) |
| 90 | { |
| 91 | TypeForTestingExpectedFunctionCallComparator comparator; |
| 92 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 93 | repository.installComparator("typeName", comparator); |
| 94 | POINTERS_EQUAL(&comparator, repository.getComparatorForType("typeName")); |
| 95 | } |
| 96 | |
| 97 | TEST(MockNamedValueHandlerRepository, installMultipleComparators) |
| 98 | { |
| 99 | TypeForTestingExpectedFunctionCallComparator comparator1, comparator2, comparator3; |
| 100 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 101 | repository.installComparator("type1", comparator1); |
| 102 | repository.installComparator("type2", comparator2); |
| 103 | repository.installComparator("type3", comparator3); |
| 104 | POINTERS_EQUAL(&comparator3, repository.getComparatorForType("type3")); |
| 105 | POINTERS_EQUAL(&comparator2, repository.getComparatorForType("type2")); |
| 106 | POINTERS_EQUAL(&comparator1, repository.getComparatorForType("type1")); |
| 107 | } |
| 108 | |
| 109 | TEST(MockNamedValueHandlerRepository, getCopierForNonExistingName) |
| 110 | { |
| 111 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 112 | POINTERS_EQUAL(NULLPTR, repository.getCopierForType("typeName")); |
| 113 | } |
| 114 | |
| 115 | TEST(MockNamedValueHandlerRepository, installCopier) |
| 116 | { |
| 117 | TypeForTestingExpectedFunctionCallCopier copier; |
| 118 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 119 | repository.installCopier("typeName", copier); |
| 120 | POINTERS_EQUAL(&copier, repository.getCopierForType("typeName")); |
| 121 | } |
| 122 | |
| 123 | TEST(MockNamedValueHandlerRepository, installMultipleCopiers) |
| 124 | { |
| 125 | TypeForTestingExpectedFunctionCallCopier copier1, copier2, copier3; |
| 126 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 127 | repository.installCopier("type1", copier1); |
| 128 | repository.installCopier("type2", copier2); |
| 129 | repository.installCopier("type3", copier3); |
| 130 | POINTERS_EQUAL(&copier3, repository.getCopierForType("type3")); |
| 131 | POINTERS_EQUAL(&copier2, repository.getCopierForType("type2")); |
| 132 | POINTERS_EQUAL(&copier1, repository.getCopierForType("type1")); |
| 133 | } |
| 134 | |
| 135 | TEST(MockNamedValueHandlerRepository, installMultipleHandlers) |
| 136 | { |
| 137 | TypeForTestingExpectedFunctionCallCopier copier1, copier2, copier3; |
| 138 | TypeForTestingExpectedFunctionCallComparator comparator1, comparator2, comparator3; |
| 139 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 140 | repository.installCopier("type1", copier1); |
| 141 | repository.installComparator("type1", comparator1); |
| 142 | repository.installCopier("type2", copier2); |
| 143 | repository.installCopier("type3", copier3); |
| 144 | repository.installComparator("type2", comparator2); |
| 145 | repository.installComparator("type3", comparator3); |
| 146 | POINTERS_EQUAL(&comparator3, repository.getComparatorForType("type3")); |
| 147 | POINTERS_EQUAL(&comparator2, repository.getComparatorForType("type2")); |
| 148 | POINTERS_EQUAL(&comparator1, repository.getComparatorForType("type1")); |
| 149 | POINTERS_EQUAL(&copier3, repository.getCopierForType("type3")); |
| 150 | POINTERS_EQUAL(&copier2, repository.getCopierForType("type2")); |
| 151 | POINTERS_EQUAL(&copier1, repository.getCopierForType("type1")); |
| 152 | } |
| 153 | |
| 154 | TEST_GROUP(MockExpectedCall) |
| 155 | { |
| 156 | MockCheckedExpectedCall* call; |
| 157 | MockNamedValueComparatorsAndCopiersRepository* originalComparatorRepository; |
| 158 | void setup() |
| 159 | { |
| 160 | originalComparatorRepository = MockNamedValue::getDefaultComparatorsAndCopiersRepository(); |
| 161 | call = new MockCheckedExpectedCall(1); |
| 162 | call->withName("funcName"); |
| 163 | } |
| 164 | void teardown() |
| 165 | { |
| 166 | MockNamedValue::setDefaultComparatorsAndCopiersRepository(originalComparatorRepository); |
| 167 | delete call; |
| 168 | CHECK_NO_MOCK_FAILURE(); |
| 169 | MockFailureReporterForTest::clearReporter(); |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | TEST(MockExpectedCall, callWithoutParameterSetOrNotFound) |
| 174 | { |
| 175 | STRCMP_EQUAL("", call->getInputParameterType("nonexisting").asCharString()); |
| 176 | LONGS_EQUAL(0, call->getInputParameter("nonexisting").getIntValue()); |
| 177 | CHECK(!call->hasInputParameterWithName("nonexisting")); |
| 178 | } |
| 179 | |
| 180 | TEST(MockExpectedCall, callWithUnsignedIntegerParameter) |
| 181 | { |
| 182 | const SimpleString paramName = "paramName"; |
| 183 | unsigned int value = 356; |
| 184 | call->withParameter(paramName, value); |
| 185 | STRCMP_EQUAL("unsigned int", call->getInputParameterType(paramName).asCharString()); |
| 186 | LONGS_EQUAL(value, call->getInputParameter(paramName).getUnsignedIntValue()); |
| 187 | CHECK(call->hasInputParameterWithName(paramName)); |
| 188 | STRCMP_CONTAINS("funcName -> unsigned int paramName: <356 (0x164)>", call->callToString().asCharString()); |
| 189 | } |
| 190 | |
| 191 | TEST(MockExpectedCall, callWithIntegerParameter) |
| 192 | { |
| 193 | const SimpleString paramName = "paramName"; |
| 194 | int value = 2; |
| 195 | call->withParameter(paramName, value); |
| 196 | STRCMP_EQUAL("int", call->getInputParameterType(paramName).asCharString()); |
| 197 | LONGS_EQUAL(value, call->getInputParameter(paramName).getIntValue()); |
| 198 | CHECK(call->hasInputParameterWithName(paramName)); |
| 199 | STRCMP_CONTAINS("funcName -> int paramName: <2 (0x2)>", call->callToString().asCharString()); |
| 200 | } |
| 201 | |
| 202 | TEST(MockExpectedCall, callWithBooleanParameter) |
| 203 | { |
| 204 | const SimpleString paramName = "paramName"; |
| 205 | bool value = true; |
| 206 | call->withParameter(paramName, value); |
| 207 | STRCMP_EQUAL("bool", call->getInputParameterType(paramName).asCharString()); |
| 208 | CHECK_EQUAL(value, call->getInputParameter(paramName).getBoolValue()); |
| 209 | CHECK(call->hasInputParameterWithName(paramName)); |
| 210 | STRCMP_CONTAINS("funcName -> bool paramName: <true>", call->callToString().asCharString()); |
| 211 | } |
| 212 | |
| 213 | TEST(MockExpectedCall, callWithUnsignedLongIntegerParameter) |
| 214 | { |
| 215 | const SimpleString paramName = "paramName"; |
| 216 | unsigned long value = 888; |
| 217 | call->withParameter(paramName, value); |
| 218 | STRCMP_EQUAL("unsigned long int", call->getInputParameterType(paramName).asCharString()); |
| 219 | LONGS_EQUAL(value, call->getInputParameter(paramName).getUnsignedLongIntValue()); |
| 220 | CHECK(call->hasInputParameterWithName(paramName)); |
| 221 | STRCMP_CONTAINS("funcName -> unsigned long int paramName: <888 (0x378)>", call->callToString().asCharString()); |
| 222 | } |
| 223 | |
| 224 | TEST(MockExpectedCall, callWithLongIntegerParameter) |
| 225 | { |
| 226 | const SimpleString paramName = "paramName"; |
| 227 | long value = 777; |
| 228 | call->withParameter(paramName, value); |
| 229 | STRCMP_EQUAL("long int", call->getInputParameterType(paramName).asCharString()); |
| 230 | LONGS_EQUAL(value, call->getInputParameter(paramName).getLongIntValue()); |
| 231 | CHECK(call->hasInputParameterWithName(paramName)); |
| 232 | STRCMP_CONTAINS("funcName -> long int paramName: <777 (0x309)>", call->callToString().asCharString()); |
| 233 | } |
| 234 | |
| 235 | #ifdef CPPUTEST_USE_LONG_LONG |
| 236 | |
| 237 | TEST(MockExpectedCall, callWithUnsignedLongLongIntegerParameter) |
| 238 | { |
| 239 | const SimpleString paramName = "paramName"; |
| 240 | unsigned long long value = 888; |
| 241 | call->withParameter(paramName, value); |
| 242 | STRCMP_EQUAL("unsigned long long int", call->getInputParameterType(paramName).asCharString()); |
| 243 | UNSIGNED_LONGLONGS_EQUAL(value, call->getInputParameter(paramName).getUnsignedLongLongIntValue()); |
| 244 | CHECK(call->hasInputParameterWithName(paramName)); |
| 245 | STRCMP_CONTAINS("funcName -> unsigned long long int paramName: <888 (0x378)>", call->callToString().asCharString()); |
| 246 | } |
| 247 | |
| 248 | TEST(MockExpectedCall, callWithLongLongIntegerParameter) |
| 249 | { |
| 250 | const SimpleString paramName = "paramName"; |
| 251 | long long value = 777; |
| 252 | call->withParameter(paramName, value); |
| 253 | STRCMP_EQUAL("long long int", call->getInputParameterType(paramName).asCharString()); |
| 254 | LONGLONGS_EQUAL(value, call->getInputParameter(paramName).getLongLongIntValue()); |
| 255 | CHECK(call->hasInputParameterWithName(paramName)); |
| 256 | STRCMP_CONTAINS("funcName -> long long int paramName: <777 (0x309)>", call->callToString().asCharString()); |
| 257 | } |
| 258 | |
| 259 | #endif |
| 260 | |
| 261 | TEST(MockExpectedCall, callWithDoubleParameter) |
| 262 | { |
| 263 | const SimpleString paramName = "paramName"; |
| 264 | double value = 1.2; |
| 265 | call->withParameter(paramName, value); |
| 266 | STRCMP_EQUAL("double", call->getInputParameterType(paramName).asCharString()); |
| 267 | DOUBLES_EQUAL(value, call->getInputParameter(paramName).getDoubleValue(), 0.0); |
| 268 | STRCMP_CONTAINS("funcName -> double paramName: <1.2>", call->callToString().asCharString()); |
| 269 | } |
| 270 | |
| 271 | TEST(MockExpectedCall, callWithDoubleParameterAndTolerance) |
| 272 | { |
| 273 | const SimpleString paramName = "paramName"; |
| 274 | double value = 1.2; |
| 275 | double tolerance = 0.2; |
| 276 | call->withParameter(paramName, value, tolerance); |
| 277 | STRCMP_EQUAL("double", call->getInputParameterType(paramName).asCharString()); |
| 278 | DOUBLES_EQUAL(value, call->getInputParameter(paramName).getDoubleValue(), 0.0); |
| 279 | DOUBLES_EQUAL(tolerance, call->getInputParameter(paramName).getDoubleTolerance(), 0.0); |
| 280 | STRCMP_CONTAINS("funcName -> double paramName: <1.2>", call->callToString().asCharString()); |
| 281 | } |
| 282 | |
| 283 | TEST(MockExpectedCall, callWithStringParameter) |
| 284 | { |
| 285 | const SimpleString paramName = "paramName"; |
| 286 | const char* value = "hello world"; |
| 287 | call->withParameter(paramName, value); |
| 288 | STRCMP_EQUAL("const char*", call->getInputParameterType(paramName).asCharString()); |
| 289 | STRCMP_EQUAL(value, call->getInputParameter(paramName).getStringValue()); |
| 290 | STRCMP_CONTAINS("funcName -> const char* paramName: <hello world>", call->callToString().asCharString()); |
| 291 | } |
| 292 | |
| 293 | TEST(MockExpectedCall, callWithPointerParameter) |
| 294 | { |
| 295 | const SimpleString paramName = "paramName"; |
| 296 | void* value = (void*) 0x123; |
| 297 | call->withParameter(paramName, value); |
| 298 | STRCMP_EQUAL("void*", call->getInputParameterType(paramName).asCharString()); |
| 299 | POINTERS_EQUAL(value, call->getInputParameter(paramName).getPointerValue()); |
| 300 | STRCMP_CONTAINS("funcName -> void* paramName: <0x123>", call->callToString().asCharString()); |
| 301 | } |
| 302 | |
| 303 | TEST(MockExpectedCall, callWithConstPointerParameter) |
| 304 | { |
| 305 | const SimpleString paramName = "paramName"; |
| 306 | const void* value = (const void*) 0x345; |
| 307 | call->withParameter(paramName, value); |
| 308 | STRCMP_EQUAL("const void*", call->getInputParameterType(paramName).asCharString()); |
| 309 | POINTERS_EQUAL(value, call->getInputParameter(paramName).getConstPointerValue()); |
| 310 | STRCMP_CONTAINS("funcName -> const void* paramName: <0x345>", call->callToString().asCharString()); |
| 311 | } |
| 312 | |
| 313 | TEST(MockExpectedCall, callWithFunctionPointerParameter) |
| 314 | { |
| 315 | const SimpleString paramName = "paramName"; |
| 316 | void (*value)() = (void (*)()) 0xdead; |
| 317 | call->withParameter(paramName, value); |
| 318 | STRCMP_EQUAL("void (*)()", call->getInputParameterType(paramName).asCharString()); |
| 319 | FUNCTIONPOINTERS_EQUAL(value, call->getInputParameter(paramName).getFunctionPointerValue()); |
| 320 | STRCMP_CONTAINS("funcName -> void (*)() paramName: <0xdead>", call->callToString().asCharString()); |
| 321 | } |
| 322 | |
| 323 | TEST(MockExpectedCall, callWithMemoryBuffer) |
| 324 | { |
| 325 | const SimpleString paramName = "paramName"; |
| 326 | const unsigned char value[] = { 0x12, 0xFE, 0xA1 }; |
| 327 | call->withParameter(paramName, value, sizeof(value)); |
| 328 | STRCMP_EQUAL("const unsigned char*", call->getInputParameterType(paramName).asCharString()); |
| 329 | POINTERS_EQUAL( (void*) value, (void*) call->getInputParameter(paramName).getMemoryBuffer() ); |
| 330 | LONGS_EQUAL(sizeof(value), call->getInputParameter(paramName).getSize()); |
| 331 | STRCMP_CONTAINS("funcName -> const unsigned char* paramName: <Size = 3 | HexContents = 12 FE A1>", call->callToString().asCharString()); |
| 332 | } |
| 333 | |
| 334 | TEST(MockExpectedCall, callWithObjectParameter) |
| 335 | { |
| 336 | const SimpleString paramName = "paramName"; |
| 337 | void* value = (void*) 0x123; |
| 338 | call->withParameterOfType("ClassName", paramName, value); |
| 339 | POINTERS_EQUAL(value, call->getInputParameter(paramName).getConstObjectPointer()); |
| 340 | STRCMP_EQUAL("ClassName", call->getInputParameterType(paramName).asCharString()); |
| 341 | STRCMP_CONTAINS("funcName -> ClassName paramName: <No comparator found for type: \"ClassName\">", call->callToString().asCharString()); |
| 342 | } |
| 343 | |
| 344 | TEST(MockExpectedCall, callWithObjectParameterUnequalComparison) |
| 345 | { |
| 346 | TypeForTestingExpectedFunctionCall type(1), unequalType(2); |
| 347 | MockNamedValue parameter("name"); |
| 348 | parameter.setConstObjectPointer("type", &unequalType); |
| 349 | call->withParameterOfType("type", "name", &type); |
| 350 | CHECK(!call->hasInputParameter(parameter)); |
| 351 | } |
| 352 | |
| 353 | TEST(MockExpectedCall, callWithObjectParameterEqualComparisonButFailsWithoutRepository) |
| 354 | { |
| 355 | TypeForTestingExpectedFunctionCall type(1), equalType(1); |
| 356 | MockNamedValue parameter("name"); |
| 357 | parameter.setConstObjectPointer("type", &equalType); |
| 358 | call->withParameterOfType("type", "name", &type); |
| 359 | CHECK(!call->hasInputParameter(parameter)); |
| 360 | } |
| 361 | |
| 362 | TEST(MockExpectedCall, callWithObjectParameterEqualComparisonButFailsWithoutComparator) |
| 363 | { |
| 364 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 365 | MockNamedValue::setDefaultComparatorsAndCopiersRepository(&repository); |
| 366 | |
| 367 | TypeForTestingExpectedFunctionCall type(1), equalType(1); |
| 368 | MockNamedValue parameter("name"); |
| 369 | parameter.setConstObjectPointer("type", &equalType); |
| 370 | call->withParameterOfType("type", "name", &type); |
| 371 | CHECK(!call->hasInputParameter(parameter)); |
| 372 | } |
| 373 | |
| 374 | TEST(MockExpectedCall, callWithObjectParameterEqualComparison) |
| 375 | { |
| 376 | TypeForTestingExpectedFunctionCallComparator comparator; |
| 377 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 378 | MockNamedValue::setDefaultComparatorsAndCopiersRepository(&repository); |
| 379 | repository.installComparator("type", comparator); |
| 380 | |
| 381 | TypeForTestingExpectedFunctionCall type(1), equalType(1); |
| 382 | MockNamedValue parameter("name"); |
| 383 | parameter.setConstObjectPointer("type", &equalType); |
| 384 | |
| 385 | call->withParameterOfType("type", "name", &type); |
| 386 | CHECK(call->hasInputParameter(parameter)); |
| 387 | } |
| 388 | |
| 389 | TEST(MockExpectedCall, getParameterValueOfObjectType) |
| 390 | { |
| 391 | TypeForTestingExpectedFunctionCallComparator comparator; |
| 392 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 393 | MockNamedValue::setDefaultComparatorsAndCopiersRepository(&repository); |
| 394 | repository.installComparator("type", comparator); |
| 395 | |
| 396 | TypeForTestingExpectedFunctionCall type(1); |
| 397 | call->withParameterOfType("type", "name", &type); |
| 398 | POINTERS_EQUAL(&type, call->getInputParameter("name").getConstObjectPointer()); |
| 399 | STRCMP_EQUAL("1", call->getInputParameterValueString("name").asCharString()); |
| 400 | } |
| 401 | |
| 402 | TEST(MockExpectedCall, getParameterValueOfObjectTypeWithoutRepository) |
| 403 | { |
| 404 | TypeForTestingExpectedFunctionCall type(1); |
| 405 | call->withParameterOfType("type", "name", &type); |
| 406 | STRCMP_EQUAL("No comparator found for type: \"type\"", call->getInputParameterValueString("name").asCharString()); |
| 407 | } |
| 408 | |
| 409 | TEST(MockExpectedCall, getParameterValueOfObjectTypeWithoutComparator) |
| 410 | { |
| 411 | TypeForTestingExpectedFunctionCall type(1); |
| 412 | MockNamedValueComparatorsAndCopiersRepository repository; |
| 413 | MockNamedValue::setDefaultComparatorsAndCopiersRepository(&repository); |
| 414 | call->withParameterOfType("type", "name", &type); |
| 415 | STRCMP_EQUAL("No comparator found for type: \"type\"", call->getInputParameterValueString("name").asCharString()); |
| 416 | } |
| 417 | |
| 418 | TEST(MockExpectedCall, callWithTwoUnsignedIntegerParameter) |
| 419 | { |
| 420 | unsigned int expected_value = 1; |
| 421 | unsigned int another_expected_value = 2; |
| 422 | |
| 423 | call->withParameter("unsigned-integer1", expected_value); |
| 424 | call->withParameter("unsigned-integer2", another_expected_value); |
| 425 | STRCMP_EQUAL("unsigned int", call->getInputParameterType("unsigned-integer1").asCharString()); |
| 426 | STRCMP_EQUAL("unsigned int", call->getInputParameterType("unsigned-integer2").asCharString()); |
| 427 | LONGS_EQUAL(expected_value, call->getInputParameter("unsigned-integer1").getUnsignedIntValue()); |
| 428 | LONGS_EQUAL(another_expected_value, call->getInputParameter("unsigned-integer2").getUnsignedIntValue()); |
| 429 | } |
| 430 | |
| 431 | TEST(MockExpectedCall, callWithTwoIntegerParameter) |
| 432 | { |
| 433 | int expected_value = 1; |
| 434 | int another_expected_value = -1; |
| 435 | |
| 436 | call->withParameter("integer1", expected_value); |
| 437 | call->withParameter("integer2", another_expected_value); |
| 438 | STRCMP_EQUAL("int", call->getInputParameterType("integer1").asCharString()); |
| 439 | STRCMP_EQUAL("int", call->getInputParameterType("integer2").asCharString()); |
| 440 | LONGS_EQUAL(expected_value, call->getInputParameter("integer1").getIntValue()); |
| 441 | LONGS_EQUAL(another_expected_value, call->getInputParameter("integer2").getIntValue()); |
| 442 | } |
| 443 | |
| 444 | TEST(MockExpectedCall, callWithThreeDifferentParameter) |
| 445 | { |
| 446 | call->withParameter("integer", 1); |
| 447 | call->withParameter("string", "hello world"); |
| 448 | call->withParameter("double", 0.12); |
| 449 | STRCMP_EQUAL("int", call->getInputParameterType("integer").asCharString()); |
| 450 | STRCMP_EQUAL("const char*", call->getInputParameterType("string").asCharString()); |
| 451 | STRCMP_EQUAL("double", call->getInputParameterType("double").asCharString()); |
| 452 | LONGS_EQUAL(1, call->getInputParameter("integer").getIntValue()); |
| 453 | STRCMP_EQUAL("hello world", call->getInputParameter("string").getStringValue()); |
| 454 | DOUBLES_EQUAL(0.12, call->getInputParameter("double").getDoubleValue(), 0.05); |
| 455 | } |
| 456 | |
| 457 | TEST(MockExpectedCall, singleCallNotMadeIsNotFulfilledButCanMatchActualCalls) |
| 458 | { |
| 459 | MockCheckedExpectedCall expectedCall(1); |
| 460 | expectedCall.withName("name"); |
| 461 | CHECK(!expectedCall.isFulfilled()); |
| 462 | CHECK(expectedCall.canMatchActualCalls()); |
| 463 | } |
| 464 | |
| 465 | TEST(MockExpectedCall, singleCallMadeIsFulFilledAndCannotMatchActualCalls) |
| 466 | { |
| 467 | MockCheckedExpectedCall expectedCall(1); |
| 468 | expectedCall.callWasMade(1); |
| 469 | CHECK(expectedCall.isFulfilled()); |
| 470 | CHECK(!expectedCall.canMatchActualCalls()); |
| 471 | } |
| 472 | |
| 473 | TEST(MockExpectedCall, multiCallNotMadeIsNotFulfilledButCanMatchActualCalls) |
| 474 | { |
| 475 | MockCheckedExpectedCall expectedCall(2); |
| 476 | expectedCall.withName("name"); |
| 477 | CHECK(!expectedCall.isFulfilled()); |
| 478 | CHECK(expectedCall.canMatchActualCalls()); |
| 479 | } |
| 480 | |
| 481 | TEST(MockExpectedCall, multiCallNotMadeExpectedTimesIsNotFulfilledButCanMatchActualCalls) |
| 482 | { |
| 483 | MockCheckedExpectedCall expectedCall(2); |
| 484 | expectedCall.withName("name"); |
| 485 | expectedCall.callWasMade(1); |
| 486 | CHECK(!expectedCall.isFulfilled()); |
| 487 | CHECK(expectedCall.canMatchActualCalls()); |
| 488 | } |
| 489 | |
| 490 | TEST(MockExpectedCall, multiCallsMadeExpectedTimesIsFulfilledAndCannotMatchActualCalls) |
| 491 | { |
| 492 | MockCheckedExpectedCall expectedCall(3); |
| 493 | expectedCall.withName("name"); |
| 494 | expectedCall.callWasMade(1); |
| 495 | expectedCall.callWasMade(2); |
| 496 | expectedCall.callWasMade(3); |
| 497 | CHECK(expectedCall.isFulfilled()); |
| 498 | CHECK(!expectedCall.canMatchActualCalls()); |
| 499 | } |
| 500 | |
| 501 | TEST(MockExpectedCall, multiCallsMadeMoreThanExpectedTimesIsNotFulfilledAndCannotMatchActualCalls) |
| 502 | { |
| 503 | MockCheckedExpectedCall expectedCall(3); |
| 504 | expectedCall.withName("name"); |
| 505 | expectedCall.callWasMade(1); |
| 506 | expectedCall.callWasMade(2); |
| 507 | expectedCall.callWasMade(3); |
| 508 | expectedCall.callWasMade(4); |
| 509 | CHECK(!expectedCall.isFulfilled()); |
| 510 | CHECK(!expectedCall.canMatchActualCalls()); |
| 511 | } |
| 512 | |
| 513 | TEST(MockExpectedCall, callsWithoutParameterAlwaysMatch) |
| 514 | { |
| 515 | MockCheckedExpectedCall expectedCall(1); |
| 516 | CHECK(expectedCall.isMatchingActualCall()); |
| 517 | } |
| 518 | |
| 519 | TEST(MockExpectedCall, callsWithParameterNotFulfilledDontMatch) |
| 520 | { |
| 521 | MockCheckedExpectedCall expectedCall(1); |
| 522 | expectedCall.withParameter("para", 1); |
| 523 | CHECK(!expectedCall.isMatchingActualCall()); |
| 524 | } |
| 525 | |
| 526 | TEST(MockExpectedCall, callsWithParameterFulfilledDoMatch) |
| 527 | { |
| 528 | MockCheckedExpectedCall expectedCall(1); |
| 529 | expectedCall.withParameter("para", 1); |
| 530 | expectedCall.inputParameterWasPassed("para"); |
| 531 | CHECK(expectedCall.isMatchingActualCall()); |
| 532 | } |
| 533 | |
| 534 | TEST(MockExpectedCall, callsWithSomeParametersNotFulfilledDontMatch) |
| 535 | { |
| 536 | MockCheckedExpectedCall expectedCall(1); |
| 537 | expectedCall.withParameter("para", 1).withParameter("two", 2); |
| 538 | expectedCall.inputParameterWasPassed("para"); |
| 539 | CHECK(!expectedCall.isMatchingActualCall()); |
| 540 | } |
| 541 | |
| 542 | TEST(MockExpectedCall, toStringForNoParametersSingleCallNotCalled) |
| 543 | { |
| 544 | MockCheckedExpectedCall expectedCall(1); |
| 545 | expectedCall.withName("name"); |
| 546 | STRCMP_EQUAL("name -> no parameters (expected 1 call, called 0 times)", expectedCall.callToString().asCharString()); |
| 547 | } |
| 548 | |
| 549 | TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledLessThanExpectedTimes) |
| 550 | { |
| 551 | MockCheckedExpectedCall expectedCall(2); |
| 552 | expectedCall.withName("name"); |
| 553 | expectedCall.callWasMade(1); |
| 554 | STRCMP_EQUAL("name -> no parameters (expected 2 calls, called 1 time)", expectedCall.callToString().asCharString()); |
| 555 | } |
| 556 | |
| 557 | TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledExpectedTimes) |
| 558 | { |
| 559 | MockCheckedExpectedCall expectedCall(2); |
| 560 | expectedCall.withName("name"); |
| 561 | expectedCall.callWasMade(1); |
| 562 | expectedCall.callWasMade(2); |
| 563 | STRCMP_EQUAL("name -> no parameters (expected 2 calls, called 2 times)", expectedCall.callToString().asCharString()); |
| 564 | } |
| 565 | |
| 566 | TEST(MockExpectedCall, toStringForIgnoredParameters) |
| 567 | { |
| 568 | MockCheckedExpectedCall expectedCall(1); |
| 569 | expectedCall.withName("name"); |
| 570 | expectedCall.ignoreOtherParameters(); |
| 571 | STRCMP_EQUAL("name -> all parameters ignored (expected 1 call, called 0 times)", expectedCall.callToString().asCharString()); |
| 572 | } |
| 573 | |
| 574 | TEST(MockExpectedCall, toStringForMultipleInputParameters) |
| 575 | { |
| 576 | int int_value = 10; |
| 577 | unsigned int uint_value = 7; |
| 578 | |
| 579 | MockCheckedExpectedCall expectedCall(1); |
| 580 | expectedCall.withName("name"); |
| 581 | expectedCall.withParameter("string", "value"); |
| 582 | expectedCall.withParameter("integer", int_value); |
| 583 | expectedCall.withParameter("unsigned-integer", uint_value); |
| 584 | expectedCall.callWasMade(1); |
| 585 | STRCMP_EQUAL("name -> const char* string: <value>, int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)> " |
| 586 | "(expected 1 call, called 1 time)", expectedCall.callToString().asCharString()); |
| 587 | } |
| 588 | |
| 589 | TEST(MockExpectedCall, toStringForMultipleInputAndOutputParameters) |
| 590 | { |
| 591 | int int_value = 10; |
| 592 | unsigned int uint_value = 7; |
| 593 | unsigned char buffer_value[3]; |
| 594 | |
| 595 | MockCheckedExpectedCall expectedCall(1); |
| 596 | expectedCall.withName("name"); |
| 597 | expectedCall.withParameter("string", "value"); |
| 598 | expectedCall.withParameter("integer", int_value); |
| 599 | expectedCall.withParameter("unsigned-integer", uint_value); |
| 600 | expectedCall.withOutputParameterReturning("buffer", buffer_value, sizeof(buffer_value)); |
| 601 | expectedCall.callWasMade(1); |
| 602 | STRCMP_EQUAL("name -> const char* string: <value>, int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)>, " |
| 603 | "const void* buffer: <output> (expected 1 call, called 1 time)", expectedCall.callToString().asCharString()); |
| 604 | } |
| 605 | |
| 606 | TEST(MockExpectedCall, toStringForMultipleOutputParameters) |
| 607 | { |
| 608 | unsigned char buffer_value[3]; |
| 609 | |
| 610 | MockCheckedExpectedCall expectedCall(1); |
| 611 | expectedCall.withName("name"); |
| 612 | expectedCall.withOutputParameterReturning("buffer1", buffer_value, sizeof(buffer_value)); |
| 613 | expectedCall.withOutputParameterReturning("buffer2", buffer_value, sizeof(buffer_value)); |
| 614 | expectedCall.callWasMade(1); |
| 615 | STRCMP_EQUAL("name -> const void* buffer1: <output>, const void* buffer2: <output> (expected 1 call, called 1 time)", expectedCall.callToString().asCharString()); |
| 616 | } |
| 617 | |
| 618 | TEST(MockExpectedCall, toStringForUnmodifiedOutputParameter) |
| 619 | { |
| 620 | MockCheckedExpectedCall expectedCall(1); |
| 621 | expectedCall.withName("name"); |
| 622 | expectedCall.withUnmodifiedOutputParameter("buffer1"); |
| 623 | expectedCall.callWasMade(1); |
| 624 | STRCMP_EQUAL("name -> const void* buffer1: <output> (expected 1 call, called 1 time)", expectedCall.callToString().asCharString()); |
| 625 | } |
| 626 | |
| 627 | TEST(MockExpectedCall, toStringForParameterAndIgnored) |
| 628 | { |
| 629 | MockCheckedExpectedCall expectedCall(1); |
| 630 | expectedCall.withName("name"); |
| 631 | expectedCall.withParameter("string", "value"); |
| 632 | expectedCall.ignoreOtherParameters(); |
| 633 | expectedCall.callWasMade(1); |
| 634 | STRCMP_EQUAL("name -> const char* string: <value>, other parameters are ignored (expected 1 call, called 1 time)", expectedCall.callToString().asCharString()); |
| 635 | } |
| 636 | |
| 637 | TEST(MockExpectedCall, toStringForCallOrderSingle) |
| 638 | { |
| 639 | MockCheckedExpectedCall expectedCall(1); |
| 640 | expectedCall.withName("name"); |
| 641 | expectedCall.withCallOrder(2); |
| 642 | expectedCall.callWasMade(1); |
| 643 | STRCMP_EQUAL("name -> expected call order: <2> -> no parameters (expected 1 call, called 1 time)", expectedCall.callToString().asCharString()); |
| 644 | } |
| 645 | |
| 646 | TEST(MockExpectedCall, toStringForCallOrderMultiple) |
| 647 | { |
| 648 | MockCheckedExpectedCall expectedCall(5); |
| 649 | expectedCall.withName("name"); |
| 650 | expectedCall.withCallOrder(5, 9); |
| 651 | expectedCall.callWasMade(5); |
| 652 | expectedCall.callWasMade(6); |
| 653 | expectedCall.callWasMade(7); |
| 654 | expectedCall.callWasMade(8); |
| 655 | expectedCall.callWasMade(9); |
| 656 | STRCMP_EQUAL("name -> expected calls order: <5..9> -> no parameters (expected 5 calls, called 5 times)", expectedCall.callToString().asCharString()); |
| 657 | } |
| 658 | |
| 659 | TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderSingle) |
| 660 | { |
| 661 | call->withName("name"); |
| 662 | call->withCallOrder(2); |
| 663 | call->callWasMade(1); |
| 664 | CHECK(call->isFulfilled()); |
| 665 | CHECK(call->isOutOfOrder()); |
| 666 | } |
| 667 | |
| 668 | TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderMultipleTooEarly) |
| 669 | { |
| 670 | MockCheckedExpectedCall expectedCall(3); |
| 671 | expectedCall.withName("name"); |
| 672 | expectedCall.withCallOrder(10, 12); |
| 673 | expectedCall.callWasMade(9); |
| 674 | expectedCall.callWasMade(10); |
| 675 | expectedCall.callWasMade(11); |
| 676 | CHECK(expectedCall.isFulfilled()); |
| 677 | CHECK(expectedCall.isOutOfOrder()); |
| 678 | } |
| 679 | |
| 680 | TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderMultipleTooLate) |
| 681 | { |
| 682 | MockCheckedExpectedCall expectedCall(3); |
| 683 | expectedCall.withName("name"); |
| 684 | expectedCall.withCallOrder(10, 12); |
| 685 | expectedCall.callWasMade(11); |
| 686 | expectedCall.callWasMade(12); |
| 687 | expectedCall.callWasMade(13); |
| 688 | CHECK(expectedCall.isFulfilled()); |
| 689 | CHECK(expectedCall.isOutOfOrder()); |
| 690 | } |
| 691 | |
| 692 | TEST(MockExpectedCall, callOrderIsFulfilledSingle) |
| 693 | { |
| 694 | call->withName("name"); |
| 695 | call->withCallOrder(1); |
| 696 | call->callWasMade(1); |
| 697 | CHECK(call->isFulfilled()); |
| 698 | CHECK_FALSE(call->isOutOfOrder()); |
| 699 | } |
| 700 | |
| 701 | TEST(MockExpectedCall, callOrderIsFulfilledMultiple) |
| 702 | { |
| 703 | MockCheckedExpectedCall expectedCall(4); |
| 704 | expectedCall.withName("name"); |
| 705 | expectedCall.withCallOrder(150, 153); |
| 706 | expectedCall.callWasMade(150); |
| 707 | expectedCall.callWasMade(151); |
| 708 | expectedCall.callWasMade(152); |
| 709 | expectedCall.callWasMade(153); |
| 710 | CHECK(expectedCall.isFulfilled()); |
| 711 | CHECK_FALSE(expectedCall.isOutOfOrder()); |
| 712 | } |
| 713 | |
| 714 | TEST(MockExpectedCall, hasOutputParameter) |
| 715 | { |
| 716 | const int value = 1; |
| 717 | call->withOutputParameterReturning("foo", &value, sizeof(value)); |
| 718 | MockNamedValue foo("foo"); |
| 719 | foo.setValue(&value); |
| 720 | CHECK(call->hasOutputParameter(foo)); |
| 721 | } |
| 722 | |
| 723 | TEST(MockExpectedCall, hasUnmodifiedOutputParameter) |
| 724 | { |
| 725 | call->withUnmodifiedOutputParameter("foo"); |
| 726 | MockNamedValue foo("foo"); |
| 727 | foo.setValue((const void *)NULLPTR); |
| 728 | foo.setSize(0); |
| 729 | CHECK(call->hasOutputParameter(foo)); |
| 730 | } |
| 731 | |
| 732 | TEST(MockExpectedCall, hasNoOutputParameter) |
| 733 | { |
| 734 | call->withIntParameter("foo", (int)1); |
| 735 | MockNamedValue foo("foo"); |
| 736 | foo.setValue((int)1); |
| 737 | CHECK_FALSE(call->hasOutputParameter(foo)); |
| 738 | } |
| 739 | |
| 740 | TEST(MockExpectedCall, hasOutputParameterOfType) |
| 741 | { |
| 742 | TypeForTestingExpectedFunctionCall object(6789); |
| 743 | call->withOutputParameterOfTypeReturning("TypeForTestingExpectedFunctionCall", "foo", &object); |
| 744 | MockNamedValue foo("foo"); |
| 745 | foo.setConstObjectPointer("TypeForTestingExpectedFunctionCall", &object); |
| 746 | CHECK(call->hasOutputParameter(foo)); |
| 747 | } |
| 748 | |
| 749 | TEST(MockExpectedCall, hasNoOutputParameterOfTypeSameTypeButInput) |
| 750 | { |
| 751 | TypeForTestingExpectedFunctionCall object(543); |
| 752 | call->withParameterOfType("TypeForTestingExpectedFunctionCall", "foo", &object); |
| 753 | MockNamedValue foo("foo"); |
| 754 | foo.setConstObjectPointer("TypeForTestingExpectedFunctionCall", &object); |
| 755 | CHECK_FALSE(call->hasOutputParameter(foo)); |
| 756 | } |
| 757 | |
| 758 | TEST(MockExpectedCall, hasNoOutputParameterOfTypeDifferentType) |
| 759 | { |
| 760 | TypeForTestingExpectedFunctionCall object(543); |
| 761 | call->withOutputParameterOfTypeReturning("TypeForTestingExpectedFunctionCall", "foo", &object); |
| 762 | MockNamedValue foo("foo"); |
| 763 | foo.setConstObjectPointer("OtherTypeForTestingExpectedFunctionCall", &object); |
| 764 | CHECK_FALSE(call->hasOutputParameter(foo)); |
| 765 | } |
| 766 | |
| 767 | TEST_GROUP(MockIgnoredExpectedCall) |
| 768 | { |
| 769 | MockIgnoredExpectedCall ignored; |
| 770 | }; |
| 771 | |
| 772 | TEST(MockIgnoredExpectedCall, worksAsItShould) |
| 773 | { |
| 774 | ignored.withName("func"); |
| 775 | ignored.withCallOrder(1); |
| 776 | ignored.withCallOrder(1, 1); |
| 777 | ignored.onObject(NULLPTR); |
| 778 | ignored.withBoolParameter("umm", true); |
| 779 | ignored.withIntParameter("bla", (int) 1); |
| 780 | ignored.withUnsignedIntParameter("foo", (unsigned int) 1); |
| 781 | ignored.withLongIntParameter("hey", (long int) 1); |
| 782 | ignored.withUnsignedLongIntParameter("bah", (unsigned long int) 1); |
| 783 | #ifdef CPPUTEST_USE_LONG_LONG |
| 784 | ignored.withLongLongIntParameter("yo", (long long int) 1); |
| 785 | ignored.withUnsignedLongLongIntParameter("grr", (unsigned long long int) 1); |
| 786 | #endif |
| 787 | ignored.withDoubleParameter("hah", (double) 1.1f); |
| 788 | ignored.withDoubleParameter("gah", 2.1, 0.3); |
| 789 | ignored.withStringParameter("goo", "hello"); |
| 790 | ignored.withPointerParameter("pie", (void*) NULLPTR); |
| 791 | ignored.withConstPointerParameter("woo", (const void*) NULLPTR); |
| 792 | ignored.withFunctionPointerParameter("fop", (void(*)()) NULLPTR); |
| 793 | ignored.withMemoryBufferParameter("waa", (const unsigned char*) NULLPTR, 0); |
| 794 | ignored.withParameterOfType( "mytype", "top", (const void*) NULLPTR); |
| 795 | ignored.withOutputParameterReturning("bar", (void*) NULLPTR, 1); |
| 796 | ignored.withOutputParameterOfTypeReturning("mytype", "bar", (const void*) NULLPTR); |
| 797 | ignored.withUnmodifiedOutputParameter("unmod"); |
| 798 | ignored.ignoreOtherParameters(); |
| 799 | ignored.andReturnValue(true); |
| 800 | ignored.andReturnValue((double) 1.0f); |
| 801 | ignored.andReturnValue((unsigned int) 1); |
| 802 | ignored.andReturnValue((int) 1); |
| 803 | ignored.andReturnValue((unsigned long int) 1); |
| 804 | ignored.andReturnValue((long int) 1); |
| 805 | #ifdef CPPUTEST_USE_LONG_LONG |
| 806 | ignored.andReturnValue((unsigned long long int) 1); |
| 807 | ignored.andReturnValue((long long int) 1); |
| 808 | #endif |
| 809 | ignored.andReturnValue("boo"); |
| 810 | ignored.andReturnValue((void*) NULLPTR); |
| 811 | ignored.andReturnValue((const void*) NULLPTR); |
| 812 | ignored.andReturnValue((void(*)()) NULLPTR); |
| 813 | } |