| 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(MockComparatorCopierTest) |
| 32 | { |
| 33 | void teardown() |
| 34 | { |
| 35 | mock().checkExpectations(); |
| 36 | mock().clear(); |
| 37 | mock().removeAllComparatorsAndCopiers(); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | class MyTypeForTesting |
| 42 | { |
| 43 | public: |
| 44 | MyTypeForTesting(long val) |
| 45 | { |
| 46 | value = new long(val); |
| 47 | } |
| 48 | virtual ~MyTypeForTesting() |
| 49 | { |
| 50 | delete value; |
| 51 | } |
| 52 | long *value; |
| 53 | }; |
| 54 | |
| 55 | class MyTypeForTestingComparator : public MockNamedValueComparator |
| 56 | { |
| 57 | public: |
| 58 | virtual bool isEqual(const void* object1, const void* object2) |
| 59 | { |
| 60 | const MyTypeForTesting* obj1 = (const MyTypeForTesting*) object1; |
| 61 | const MyTypeForTesting* obj2 = (const MyTypeForTesting*) object2; |
| 62 | return *(obj1->value) == *(obj2->value); |
| 63 | } |
| 64 | virtual SimpleString valueToString(const void* object) |
| 65 | { |
| 66 | const MyTypeForTesting* obj = (const MyTypeForTesting*) object; |
| 67 | return StringFrom(*(obj->value)); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | class MyTypeForTestingCopier : public MockNamedValueCopier |
| 72 | { |
| 73 | public: |
| 74 | virtual void copy(void* dst_, const void* src_) |
| 75 | { |
| 76 | MyTypeForTesting* dst = (MyTypeForTesting*) dst_; |
| 77 | const MyTypeForTesting* src = (const MyTypeForTesting*) src_; |
| 78 | *(dst->value) = *(src->value); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | TEST(MockComparatorCopierTest, customObjectParameterFailsWhenNotHavingAComparisonRepository) |
| 83 | { |
| 84 | MockFailureReporterInstaller failureReporterInstaller; |
| 85 | |
| 86 | MyTypeForTesting object(1); |
| 87 | mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 88 | mock().actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 89 | |
| 90 | MockNoWayToCompareCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting"); |
| 91 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 92 | } |
| 93 | |
| 94 | TEST(MockComparatorCopierTest, customObjectParameterFailsWhenNotHavingACopierRepository) |
| 95 | { |
| 96 | MockFailureReporterInstaller failureReporterInstaller; |
| 97 | |
| 98 | MyTypeForTesting object(1); |
| 99 | mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &object); |
| 100 | mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 101 | |
| 102 | MockNoWayToCopyCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting"); |
| 103 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 104 | } |
| 105 | |
| 106 | TEST(MockComparatorCopierTest, customObjectParameterSucceeds) |
| 107 | { |
| 108 | MyTypeForTesting object(1); |
| 109 | MyTypeForTestingComparator comparator; |
| 110 | mock().installComparator("MyTypeForTesting", comparator); |
| 111 | |
| 112 | mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 113 | mock().actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 114 | |
| 115 | mock().checkExpectations(); |
| 116 | |
| 117 | mock().removeAllComparatorsAndCopiers(); |
| 118 | } |
| 119 | |
| 120 | static bool myTypeIsEqual(const void* object1, const void* object2) |
| 121 | { |
| 122 | return ((const MyTypeForTesting*)object1)->value == ((const MyTypeForTesting*)object2)->value; |
| 123 | } |
| 124 | |
| 125 | static SimpleString myTypeValueToString(const void* object) |
| 126 | { |
| 127 | return StringFrom(((const MyTypeForTesting*)object)->value); |
| 128 | } |
| 129 | |
| 130 | TEST(MockComparatorCopierTest, customObjectWithFunctionComparator) |
| 131 | { |
| 132 | MyTypeForTesting object(1); |
| 133 | MockFunctionComparator comparator(myTypeIsEqual, myTypeValueToString); |
| 134 | mock().installComparator("MyTypeForTesting", comparator); |
| 135 | |
| 136 | mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 137 | mock().actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 138 | |
| 139 | mock().checkExpectations(); |
| 140 | |
| 141 | mock().removeAllComparatorsAndCopiers(); |
| 142 | } |
| 143 | |
| 144 | TEST(MockComparatorCopierTest, customObjectWithFunctionComparatorThatFailsCoversValueToString) |
| 145 | { |
| 146 | MockFailureReporterInstaller failureReporterInstaller; |
| 147 | |
| 148 | MyTypeForTesting object(5); |
| 149 | MockFunctionComparator comparator(myTypeIsEqual, myTypeValueToString); |
| 150 | mock().installComparator("MyTypeForTesting", comparator); |
| 151 | |
| 152 | MockExpectedCallsListForTest expectations; |
| 153 | expectations.addFunction("function")->withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 154 | MockExpectedCallsDidntHappenFailure failure(UtestShell::getCurrent(), expectations); |
| 155 | |
| 156 | mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 157 | mock().checkExpectations(); |
| 158 | |
| 159 | CHECK_EXPECTED_MOCK_FAILURE_LOCATION(failure, __FILE__, __LINE__); |
| 160 | } |
| 161 | |
| 162 | TEST(MockComparatorCopierTest, customTypeOutputParameterSucceeds) |
| 163 | { |
| 164 | MyTypeForTesting expectedObject(55); |
| 165 | MyTypeForTesting actualObject(99); |
| 166 | MyTypeForTestingCopier copier; |
| 167 | mock().installCopier("MyTypeForTesting", copier); |
| 168 | |
| 169 | mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject); |
| 170 | mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject); |
| 171 | |
| 172 | mock().checkExpectations(); |
| 173 | CHECK_EQUAL(55, *(expectedObject.value)); |
| 174 | CHECK_EQUAL(55, *(actualObject.value)); |
| 175 | |
| 176 | mock().removeAllComparatorsAndCopiers(); |
| 177 | } |
| 178 | |
| 179 | TEST(MockComparatorCopierTest, noActualCallForCustomTypeOutputParameter) |
| 180 | { |
| 181 | MockFailureReporterInstaller failureReporterInstaller; |
| 182 | |
| 183 | MyTypeForTesting expectedObject(1); |
| 184 | MyTypeForTestingCopier copier; |
| 185 | mock().installCopier("MyTypeForTesting", copier); |
| 186 | |
| 187 | MockExpectedCallsListForTest expectations; |
| 188 | expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); |
| 189 | MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); |
| 190 | |
| 191 | mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); |
| 192 | mock().checkExpectations(); |
| 193 | |
| 194 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 195 | |
| 196 | mock().removeAllComparatorsAndCopiers(); |
| 197 | } |
| 198 | |
| 199 | TEST(MockComparatorCopierTest, unexpectedCustomTypeOutputParameter) |
| 200 | { |
| 201 | MockFailureReporterInstaller failureReporterInstaller; |
| 202 | |
| 203 | MyTypeForTesting actualObject(8834); |
| 204 | MyTypeForTestingCopier copier; |
| 205 | mock().installCopier("MyTypeForTesting", copier); |
| 206 | |
| 207 | MockExpectedCallsListForTest expectations; |
| 208 | expectations.addFunction("foo"); |
| 209 | MockNamedValue parameter("parameterName"); |
| 210 | parameter.setConstObjectPointer("MyTypeForTesting", &actualObject); |
| 211 | MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); |
| 212 | |
| 213 | mock().expectOneCall("foo"); |
| 214 | mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject); |
| 215 | mock().checkExpectations(); |
| 216 | |
| 217 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 218 | |
| 219 | mock().removeAllComparatorsAndCopiers(); |
| 220 | } |
| 221 | |
| 222 | TEST(MockComparatorCopierTest, customTypeOutputParameterMissing) |
| 223 | { |
| 224 | MockFailureReporterInstaller failureReporterInstaller; |
| 225 | |
| 226 | MyTypeForTesting expectedObject(123464); |
| 227 | MyTypeForTestingCopier copier; |
| 228 | mock().installCopier("MyTypeForTesting", copier); |
| 229 | |
| 230 | MockExpectedCallsListForTest expectations; |
| 231 | expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); |
| 232 | MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations); |
| 233 | |
| 234 | mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); |
| 235 | mock().actualCall("foo"); |
| 236 | |
| 237 | mock().checkExpectations(); |
| 238 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 239 | |
| 240 | mock().removeAllComparatorsAndCopiers(); |
| 241 | } |
| 242 | |
| 243 | TEST(MockComparatorCopierTest, customTypeOutputParameterOfWrongType) |
| 244 | { |
| 245 | MockFailureReporterInstaller failureReporterInstaller; |
| 246 | |
| 247 | MyTypeForTesting expectedObject(123464); |
| 248 | MyTypeForTesting actualObject(75646); |
| 249 | MyTypeForTestingCopier copier; |
| 250 | mock().installCopier("MyTypeForTesting", copier); |
| 251 | |
| 252 | MockExpectedCallsListForTest expectations; |
| 253 | expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); |
| 254 | MockNamedValue parameter("output"); |
| 255 | parameter.setConstObjectPointer("OtherTypeForTesting", &actualObject); |
| 256 | MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); |
| 257 | |
| 258 | mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); |
| 259 | mock().actualCall("foo").withOutputParameterOfType("OtherTypeForTesting", "output", &actualObject); |
| 260 | |
| 261 | mock().checkExpectations(); |
| 262 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 263 | |
| 264 | mock().removeAllComparatorsAndCopiers(); |
| 265 | } |
| 266 | |
| 267 | TEST(MockComparatorCopierTest, noCopierForCustomTypeOutputParameter) |
| 268 | { |
| 269 | MockFailureReporterInstaller failureReporterInstaller; |
| 270 | |
| 271 | MyTypeForTesting expectedObject(123464); |
| 272 | MyTypeForTesting actualObject(8834); |
| 273 | |
| 274 | MockExpectedCallsListForTest expectations; |
| 275 | expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); |
| 276 | MockNoWayToCopyCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting"); |
| 277 | |
| 278 | mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); |
| 279 | mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "output", &actualObject); |
| 280 | |
| 281 | mock().checkExpectations(); |
| 282 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 283 | } |
| 284 | |
| 285 | TEST(MockComparatorCopierTest, twoCustomTypeOutputParameters) |
| 286 | { |
| 287 | MyTypeForTesting expectedObject1(545); |
| 288 | MyTypeForTesting actualObject1(979); |
| 289 | MyTypeForTesting expectedObject2(123); |
| 290 | MyTypeForTesting actualObject2(4567); |
| 291 | MyTypeForTestingCopier copier; |
| 292 | mock().installCopier("MyTypeForTesting", copier); |
| 293 | |
| 294 | mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject1).withParameter("id", 1); |
| 295 | mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject2).withParameter("id", 2); |
| 296 | mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject1).withParameter("id", 1); |
| 297 | mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject2).withParameter("id", 2); |
| 298 | |
| 299 | mock().checkExpectations(); |
| 300 | CHECK_EQUAL(545, *(expectedObject1.value)); |
| 301 | CHECK_EQUAL(545, *(actualObject1.value)); |
| 302 | CHECK_EQUAL(123, *(expectedObject2.value)); |
| 303 | CHECK_EQUAL(123, *(actualObject2.value)); |
| 304 | |
| 305 | mock().removeAllComparatorsAndCopiers(); |
| 306 | } |
| 307 | |
| 308 | TEST(MockComparatorCopierTest, twoInterleavedCustomTypeOutputParameters) |
| 309 | { |
| 310 | MyTypeForTesting expectedObject1(9545); |
| 311 | MyTypeForTesting actualObject1(79); |
| 312 | MyTypeForTesting expectedObject2(132); |
| 313 | MyTypeForTesting actualObject2(743); |
| 314 | MyTypeForTestingCopier copier; |
| 315 | |
| 316 | mock().installCopier("MyTypeForTesting", copier); |
| 317 | mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject1).withParameter("id", 1); |
| 318 | mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject2).withParameter("id", 2); |
| 319 | mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject2).withParameter("id", 2); |
| 320 | mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject1).withParameter("id", 1); |
| 321 | |
| 322 | mock().checkExpectations(); |
| 323 | CHECK_EQUAL(9545, *(expectedObject1.value)); |
| 324 | CHECK_EQUAL(9545, *(actualObject1.value)); |
| 325 | CHECK_EQUAL(132, *(expectedObject2.value)); |
| 326 | CHECK_EQUAL(132, *(actualObject2.value)); |
| 327 | |
| 328 | mock().removeAllComparatorsAndCopiers(); |
| 329 | } |
| 330 | |
| 331 | TEST(MockComparatorCopierTest, twoDifferentCustomTypeOutputParametersInSameFunctionCallSucceeds) |
| 332 | { |
| 333 | MyTypeForTesting expectedObject1(11); |
| 334 | MyTypeForTesting actualObject1(22); |
| 335 | MyTypeForTesting expectedObject2(33); |
| 336 | MyTypeForTesting actualObject2(44); |
| 337 | MyTypeForTestingCopier copier; |
| 338 | mock().installCopier("MyTypeForTesting", copier); |
| 339 | |
| 340 | mock().expectOneCall("foo") |
| 341 | .withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject1) |
| 342 | .withOutputParameterOfTypeReturning("MyTypeForTesting", "foobar", &expectedObject2); |
| 343 | mock().actualCall("foo") |
| 344 | .withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject1) |
| 345 | .withOutputParameterOfType("MyTypeForTesting", "foobar", &actualObject2); |
| 346 | |
| 347 | mock().checkExpectations(); |
| 348 | CHECK_EQUAL(11, *(expectedObject1.value)); |
| 349 | CHECK_EQUAL(11, *(actualObject1.value)); |
| 350 | CHECK_EQUAL(33, *(expectedObject2.value)); |
| 351 | CHECK_EQUAL(33, *(actualObject2.value)); |
| 352 | |
| 353 | mock().removeAllComparatorsAndCopiers(); |
| 354 | } |
| 355 | |
| 356 | TEST(MockComparatorCopierTest, customTypeOutputAndInputParametersOfSameNameInDifferentFunctionCallsOfSameFunctionSucceeds) |
| 357 | { |
| 358 | MyTypeForTesting expectedObject1(911); |
| 359 | MyTypeForTesting actualObject1(6576878); |
| 360 | MyTypeForTesting expectedObject2(123); |
| 361 | MyTypeForTesting actualObject2(123); |
| 362 | MyTypeForTestingCopier copier; |
| 363 | MyTypeForTestingComparator comparator; |
| 364 | mock().installCopier("MyTypeForTesting", copier); |
| 365 | mock().installComparator("MyTypeForTesting", comparator); |
| 366 | |
| 367 | mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject1); |
| 368 | mock().expectOneCall("foo").withParameterOfType("MyTypeForTesting", "bar", &expectedObject2); |
| 369 | mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject1); |
| 370 | mock().actualCall("foo").withParameterOfType("MyTypeForTesting", "bar", &actualObject2); |
| 371 | |
| 372 | mock().checkExpectations(); |
| 373 | CHECK_EQUAL(911, *(expectedObject1.value)); |
| 374 | CHECK_EQUAL(911, *(actualObject1.value)); |
| 375 | CHECK_EQUAL(123, *(expectedObject2.value)); |
| 376 | CHECK_EQUAL(123, *(actualObject2.value)); |
| 377 | |
| 378 | mock().removeAllComparatorsAndCopiers(); |
| 379 | } |
| 380 | |
| 381 | TEST(MockComparatorCopierTest, twoCustomTypeOutputParametersOfSameNameInDifferentFunctionsSucceeds) |
| 382 | { |
| 383 | MyTypeForTesting expectedObject1(657); |
| 384 | MyTypeForTesting actualObject1(984465); |
| 385 | MyTypeForTesting expectedObject2(987); |
| 386 | MyTypeForTesting actualObject2(987); |
| 387 | MyTypeForTestingCopier copier; |
| 388 | MyTypeForTestingComparator comparator; |
| 389 | mock().installCopier("MyTypeForTesting", copier); |
| 390 | mock().installComparator("MyTypeForTesting", comparator); |
| 391 | |
| 392 | mock().expectOneCall("foo1").withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject1); |
| 393 | mock().expectOneCall("foo2").withParameterOfType("MyTypeForTesting", "bar", &expectedObject2); |
| 394 | mock().actualCall("foo1").withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject1); |
| 395 | mock().actualCall("foo2").withParameterOfType("MyTypeForTesting", "bar", &actualObject2); |
| 396 | |
| 397 | mock().checkExpectations(); |
| 398 | CHECK_EQUAL(657, *(expectedObject1.value)); |
| 399 | CHECK_EQUAL(657, *(actualObject1.value)); |
| 400 | CHECK_EQUAL(987, *(expectedObject2.value)); |
| 401 | CHECK_EQUAL(987, *(actualObject2.value)); |
| 402 | |
| 403 | mock().removeAllComparatorsAndCopiers(); |
| 404 | } |
| 405 | |
| 406 | TEST(MockComparatorCopierTest, customTypeOutputAndInputParameterOfSameTypeInSameFunctionCall) |
| 407 | { |
| 408 | MyTypeForTesting expectedObject1(45); |
| 409 | MyTypeForTesting actualObject1(45); |
| 410 | MyTypeForTesting expectedObject2(987765443); |
| 411 | MyTypeForTesting actualObject2(0); |
| 412 | MyTypeForTestingCopier copier; |
| 413 | MyTypeForTestingComparator comparator; |
| 414 | mock().installCopier("MyTypeForTesting", copier); |
| 415 | mock().installComparator("MyTypeForTesting", comparator); |
| 416 | |
| 417 | mock().expectOneCall("foo") |
| 418 | .withParameterOfType("MyTypeForTesting", "bar", &expectedObject1) |
| 419 | .withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject2); |
| 420 | mock().actualCall("foo") |
| 421 | .withParameterOfType("MyTypeForTesting", "bar", &actualObject1) |
| 422 | .withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject2); |
| 423 | |
| 424 | mock().checkExpectations(); |
| 425 | CHECK_EQUAL(45, *(expectedObject1.value)); |
| 426 | CHECK_EQUAL(45, *(actualObject1.value)); |
| 427 | CHECK_EQUAL(987765443, *(expectedObject2.value)); |
| 428 | CHECK_EQUAL(987765443, *(actualObject2.value)); |
| 429 | |
| 430 | mock().removeAllComparatorsAndCopiers(); |
| 431 | } |
| 432 | |
| 433 | TEST(MockComparatorCopierTest, customTypeOutputParameterTraced) |
| 434 | { |
| 435 | MyTypeForTesting actualObject(676789); |
| 436 | MyTypeForTestingCopier copier; |
| 437 | mock().installCopier("MyTypeForTesting", copier); |
| 438 | mock().tracing(true); |
| 439 | |
| 440 | mock().actualCall("someFunc").withOutputParameterOfType("MyTypeForTesting", "someParameter", &actualObject); |
| 441 | |
| 442 | mock().checkExpectations(); |
| 443 | STRCMP_CONTAINS("Function name:someFunc MyTypeForTesting someParameter:", mock().getTraceOutput()); |
| 444 | |
| 445 | mock().removeAllComparatorsAndCopiers(); |
| 446 | } |
| 447 | |
| 448 | TEST(MockComparatorCopierTest, customTypeOutputParameterWithIgnoredParameters) |
| 449 | { |
| 450 | MyTypeForTesting expectedObject(444537909); |
| 451 | MyTypeForTesting actualObject(98765); |
| 452 | MyTypeForTestingCopier copier; |
| 453 | mock().installCopier("MyTypeForTesting", copier); |
| 454 | |
| 455 | mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &expectedObject).ignoreOtherParameters(); |
| 456 | mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "bar", &actualObject).withParameter("other", 1); |
| 457 | |
| 458 | mock().checkExpectations(); |
| 459 | CHECK_EQUAL(444537909, *(expectedObject.value)); |
| 460 | CHECK_EQUAL(444537909, *(actualObject.value)); |
| 461 | |
| 462 | mock().removeAllComparatorsAndCopiers(); |
| 463 | } |
| 464 | |
| 465 | static void myTypeCopy(void* dst_, const void* src_) |
| 466 | { |
| 467 | MyTypeForTesting* dst = (MyTypeForTesting*) dst_; |
| 468 | const MyTypeForTesting* src = (const MyTypeForTesting*) src_; |
| 469 | *(dst->value) = *(src->value); |
| 470 | } |
| 471 | |
| 472 | TEST(MockComparatorCopierTest, customObjectWithFunctionCopier) |
| 473 | { |
| 474 | MyTypeForTesting expectedObject(9874452); |
| 475 | MyTypeForTesting actualObject(2034); |
| 476 | MockFunctionCopier copier(myTypeCopy); |
| 477 | mock().installCopier("MyTypeForTesting", copier); |
| 478 | |
| 479 | mock().expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &expectedObject); |
| 480 | mock().actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject); |
| 481 | |
| 482 | mock().checkExpectations(); |
| 483 | CHECK_EQUAL(9874452, *(expectedObject.value)); |
| 484 | CHECK_EQUAL(9874452, *(actualObject.value)); |
| 485 | |
| 486 | mock().removeAllComparatorsAndCopiers(); |
| 487 | } |
| 488 | |
| 489 | TEST(MockComparatorCopierTest, removingComparatorsWorksHierachically) |
| 490 | { |
| 491 | MockFailureReporterInstaller failureReporterInstaller; |
| 492 | |
| 493 | MyTypeForTesting object(1); |
| 494 | MyTypeForTestingComparator comparator; |
| 495 | |
| 496 | mock("scope").installComparator("MyTypeForTesting", comparator); |
| 497 | mock().removeAllComparatorsAndCopiers(); |
| 498 | mock("scope").expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 499 | mock("scope").actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 500 | |
| 501 | MockNoWayToCompareCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting"); |
| 502 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 503 | } |
| 504 | |
| 505 | TEST(MockComparatorCopierTest, removingCopiersWorksHierachically) |
| 506 | { |
| 507 | MockFailureReporterInstaller failureReporterInstaller; |
| 508 | MyTypeForTesting object(1); |
| 509 | |
| 510 | MyTypeForTestingCopier copier; |
| 511 | |
| 512 | mock("scope").installCopier("MyTypeForTesting", copier); |
| 513 | mock().removeAllComparatorsAndCopiers(); |
| 514 | mock("scope").expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "bar", &object); |
| 515 | mock("scope").actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "bar", &object); |
| 516 | |
| 517 | MockNoWayToCopyCustomTypeFailure expectedFailure(mockFailureTest(), "MyTypeForTesting"); |
| 518 | CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); |
| 519 | } |
| 520 | |
| 521 | TEST(MockComparatorCopierTest, installComparatorWorksHierarchicalOnBothExistingAndDynamicallyCreatedMockSupports) |
| 522 | { |
| 523 | MyTypeForTesting object(1); |
| 524 | MyTypeForTestingComparator comparator; |
| 525 | |
| 526 | mock("existing"); |
| 527 | mock().installComparator("MyTypeForTesting", comparator); |
| 528 | mock("existing").expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 529 | mock("existing").actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 530 | mock("dynamic").expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 531 | mock("dynamic").actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 532 | |
| 533 | mock().checkExpectations(); |
| 534 | mock().removeAllComparatorsAndCopiers(); |
| 535 | } |
| 536 | |
| 537 | TEST(MockComparatorCopierTest, installComparatorsWorksHierarchical) |
| 538 | { |
| 539 | MyTypeForTesting object(1); |
| 540 | MyTypeForTestingComparator comparator; |
| 541 | MockNamedValueComparatorsAndCopiersRepository repos; |
| 542 | repos.installComparator("MyTypeForTesting", comparator); |
| 543 | |
| 544 | mock("existing"); |
| 545 | mock().installComparatorsAndCopiers(repos); |
| 546 | mock("existing").expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 547 | mock("existing").actualCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 548 | |
| 549 | mock().checkExpectations(); |
| 550 | mock().removeAllComparatorsAndCopiers(); |
| 551 | } |
| 552 | |
| 553 | TEST(MockComparatorCopierTest, installCopiersWorksHierarchically) |
| 554 | { |
| 555 | MyTypeForTesting object(1); |
| 556 | MyTypeForTestingCopier copier; |
| 557 | |
| 558 | mock("existing"); |
| 559 | mock().installCopier("MyTypeForTesting", copier); |
| 560 | mock("existing").expectOneCall("function").withOutputParameterOfTypeReturning("MyTypeForTesting", "parameterName", &object); |
| 561 | mock("existing").actualCall("function").withOutputParameterOfType("MyTypeForTesting", "parameterName", &object); |
| 562 | |
| 563 | mock().checkExpectations(); |
| 564 | mock().removeAllComparatorsAndCopiers(); |
| 565 | } |
| 566 | |
| 567 | class StubComparator : public MockNamedValueComparator |
| 568 | { |
| 569 | public: |
| 570 | virtual bool isEqual(const void*, const void*) |
| 571 | { |
| 572 | return true; |
| 573 | } |
| 574 | virtual SimpleString valueToString(const void*) |
| 575 | { |
| 576 | return ""; |
| 577 | } |
| 578 | }; |
| 579 | |
| 580 | struct SomeClass |
| 581 | { |
| 582 | int someDummy_; |
| 583 | }; |
| 584 | |
| 585 | static void functionWithConstParam(const SomeClass param) |
| 586 | { |
| 587 | mock().actualCall("functionWithConstParam").withParameterOfType("SomeClass", "param", ¶m); |
| 588 | } |
| 589 | |
| 590 | TEST(MockComparatorCopierTest, shouldSupportConstParameters) |
| 591 | { |
| 592 | StubComparator comparator; |
| 593 | mock().installComparator("SomeClass", comparator); |
| 594 | |
| 595 | SomeClass param; |
| 596 | mock().expectOneCall("functionWithConstParam").withParameterOfType("SomeClass", "param", ¶m); |
| 597 | functionWithConstParam(param); |
| 598 | |
| 599 | mock().checkExpectations(); |
| 600 | } |
| 601 |