| 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 | #include "CppUTest/TestHarness.h" |
| 28 | |
| 29 | #include "CppUTest/TestHarness_c.h" |
| 30 | #include "CppUTest/TestTestingFixture.h" |
| 31 | #include "CppUTestExt/MockSupport_c.h" |
| 32 | #include "MockSupport_cTestCFile.h" |
| 33 | #include "CppUTestExt/OrderedTest.h" |
| 34 | |
| 35 | extern "C" { |
| 36 | static void dummy_function_for_mock_c_test() |
| 37 | { |
| 38 | } |
| 39 | static void dummy_function_for_mock_c_test_two() |
| 40 | { |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | TEST_GROUP(MockSupport_c) |
| 45 | { |
| 46 | void teardown() |
| 47 | { |
| 48 | mock_c()->clear(); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | TEST(MockSupport_c, OrderObserved) |
| 53 | { |
| 54 | mock_c()->strictOrder(); |
| 55 | |
| 56 | mock_c()->expectOneCall("foo1"); |
| 57 | mock_c()->expectOneCall("foo2"); |
| 58 | mock_c()->actualCall("foo1"); |
| 59 | mock_c()->actualCall("foo2"); |
| 60 | |
| 61 | mock_c()->checkExpectations(); |
| 62 | } |
| 63 | |
| 64 | TEST(MockSupport_c, hasReturnValue) |
| 65 | { |
| 66 | mock_c()->expectOneCall("foo"); |
| 67 | CHECK(mock_c()->actualCall("foo")->hasReturnValue() == 0); |
| 68 | CHECK(mock_c()->hasReturnValue() == 0); |
| 69 | |
| 70 | mock_c()->expectOneCall("foo2")->andReturnIntValue(1); |
| 71 | CHECK(mock_c()->actualCall("foo2")->hasReturnValue() != 0); |
| 72 | CHECK(mock_c()->hasReturnValue() != 0); |
| 73 | } |
| 74 | |
| 75 | TEST(MockSupport_c, expectAndActualOneCall) |
| 76 | { |
| 77 | mock_c()->expectOneCall("boo"); |
| 78 | mock_c()->actualCall("boo"); |
| 79 | mock_c()->checkExpectations(); |
| 80 | } |
| 81 | |
| 82 | TEST(MockSupport_c, expectAndActualThreeCalls) |
| 83 | { |
| 84 | mock_c()->expectNCalls(3, "boo"); |
| 85 | mock_c()->actualCall("boo"); |
| 86 | mock_c()->actualCall("boo"); |
| 87 | mock_c()->actualCall("boo"); |
| 88 | mock_c()->checkExpectations(); |
| 89 | } |
| 90 | |
| 91 | TEST(MockSupport_c, expectNoCall) |
| 92 | { |
| 93 | mock_c()->expectNoCall("foo"); |
| 94 | mock_c()->expectOneCall("bar"); |
| 95 | mock_c()->actualCall("bar"); |
| 96 | mock_c()->checkExpectations(); |
| 97 | } |
| 98 | |
| 99 | TEST(MockSupport_c, expectAndActualParameters) |
| 100 | { |
| 101 | mock_c()->expectOneCall("boo")->withIntParameters("integer", 1)->withDoubleParameters("double", 1.0)-> |
| 102 | withStringParameters("string", "string")->withPointerParameters("pointer", (void*) 1)-> |
| 103 | withFunctionPointerParameters("functionPointer", dummy_function_for_mock_c_test); |
| 104 | mock_c()->actualCall("boo")->withIntParameters("integer", 1)->withDoubleParameters("double", 1.0)-> |
| 105 | withStringParameters("string", "string")->withPointerParameters("pointer", (void*) 1)-> |
| 106 | withFunctionPointerParameters("functionPointer", dummy_function_for_mock_c_test); |
| 107 | } |
| 108 | |
| 109 | extern "C"{ |
| 110 | |
| 111 | static int typeNameIsEqual(const void* object1, const void* object2) |
| 112 | { |
| 113 | return object1 == object2; |
| 114 | } |
| 115 | |
| 116 | static const char* typeNameValueToString(const void* PUNUSED(object)) |
| 117 | { |
| 118 | return "valueToString"; |
| 119 | } |
| 120 | |
| 121 | static void typeCopy(void* dst, const void* src) |
| 122 | { |
| 123 | *(int*) dst = *(const int*) src; |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | TEST(MockSupport_c, expectAndActualParametersOnObject) |
| 129 | { |
| 130 | mock_c()->installComparator("typeName", typeNameIsEqual, typeNameValueToString); |
| 131 | mock_c()->expectOneCall("boo")->withParameterOfType("typeName", "name", (const void*) 1); |
| 132 | mock_c()->actualCall("boo")->withParameterOfType("typeName", "name", (const void*) 1); |
| 133 | mock_c()->checkExpectations(); |
| 134 | mock_c()->removeAllComparatorsAndCopiers(); |
| 135 | } |
| 136 | |
| 137 | TEST(MockSupport_c, boolParameter) |
| 138 | { |
| 139 | mock_c()->expectOneCall("foo")->withBoolParameters("p", 1); |
| 140 | mock_c()->actualCall("foo")->withBoolParameters("p", 1); |
| 141 | } |
| 142 | |
| 143 | TEST(MockSupport_c, unsignedIntParameter) |
| 144 | { |
| 145 | mock_c()->expectOneCall("foo")->withUnsignedIntParameters("p", 1); |
| 146 | mock_c()->actualCall("foo")->withUnsignedIntParameters("p", 1); |
| 147 | } |
| 148 | |
| 149 | TEST(MockSupport_c, longIntParameter) |
| 150 | { |
| 151 | mock_c()->expectOneCall("foo")->withLongIntParameters("p", 1); |
| 152 | mock_c()->actualCall("foo")->withLongIntParameters("p", 1); |
| 153 | } |
| 154 | |
| 155 | TEST(MockSupport_c, unsignedLongIntParameter) |
| 156 | { |
| 157 | mock_c()->expectOneCall("foo")->withUnsignedLongIntParameters("p", 1); |
| 158 | mock_c()->actualCall("foo")->withUnsignedLongIntParameters("p", 1); |
| 159 | } |
| 160 | |
| 161 | TEST(MockSupport_c, doubleParameterWithTolerance) |
| 162 | { |
| 163 | mock_c( )->expectOneCall("foo")->withDoubleParametersAndTolerance("p", 2.0, 0.2); |
| 164 | mock_c( )->actualCall("foo")->withDoubleParameters("p", 1.9); |
| 165 | } |
| 166 | |
| 167 | #ifdef CPPUTEST_USE_LONG_LONG |
| 168 | |
| 169 | TEST(MockSupport_c, longLongIntParameter) |
| 170 | { |
| 171 | mock_c()->expectOneCall("foo")->withLongLongIntParameters("p", 1); |
| 172 | mock_c()->actualCall("foo")->withLongLongIntParameters("p", 1); |
| 173 | } |
| 174 | |
| 175 | TEST(MockSupport_c, unsignedLongLongIntParameter) |
| 176 | { |
| 177 | mock_c()->expectOneCall("foo")->withUnsignedLongLongIntParameters("p", 1); |
| 178 | mock_c()->actualCall("foo")->withUnsignedLongLongIntParameters("p", 1); |
| 179 | } |
| 180 | |
| 181 | #endif |
| 182 | |
| 183 | TEST(MockSupport_c, memoryBufferParameter) |
| 184 | { |
| 185 | const unsigned char mem_buffer[] = { 1, 2, 3}; |
| 186 | mock_c()->expectOneCall("foo")->withMemoryBufferParameter("out", mem_buffer, sizeof(mem_buffer)); |
| 187 | mock_c()->actualCall("foo")->withMemoryBufferParameter("out", mem_buffer, sizeof(mem_buffer)); |
| 188 | mock_c()->checkExpectations(); |
| 189 | } |
| 190 | |
| 191 | TEST(MockSupport_c, outputParameters) |
| 192 | { |
| 193 | int param = 1; |
| 194 | const int retval = 2; |
| 195 | mock_c()->expectOneCall("foo")->withOutputParameterReturning("out", &retval, sizeof(retval)); |
| 196 | mock_c()->actualCall("foo")->withOutputParameter("out", ¶m); |
| 197 | mock_c()->checkExpectations(); |
| 198 | LONGS_EQUAL(2, param); |
| 199 | LONGS_EQUAL(2, retval); |
| 200 | } |
| 201 | |
| 202 | TEST(MockSupport_c, unmodifiedOutputParameter) |
| 203 | { |
| 204 | int param = 1; |
| 205 | mock_c()->expectOneCall("foo")->withUnmodifiedOutputParameter("out"); |
| 206 | mock_c()->actualCall("foo")->withOutputParameter("out", ¶m); |
| 207 | mock_c()->checkExpectations(); |
| 208 | LONGS_EQUAL(1, param); |
| 209 | } |
| 210 | |
| 211 | TEST(MockSupport_c, outputParameters_differentType) |
| 212 | { |
| 213 | long param = 1; |
| 214 | const long retval = 2; |
| 215 | mock_c()->expectOneCall("foo")->withOutputParameterReturning("out", &retval, sizeof(retval)); |
| 216 | mock_c()->actualCall("foo")->withOutputParameter("out", ¶m); |
| 217 | mock_c()->checkExpectations(); |
| 218 | LONGS_EQUAL(2, param); |
| 219 | LONGS_EQUAL(2, retval); |
| 220 | } |
| 221 | |
| 222 | TEST(MockSupport_c, outputParametersOfType) |
| 223 | { |
| 224 | int param = 1; |
| 225 | const int retval = 2; |
| 226 | mock_c()->installCopier("typeName", typeCopy); |
| 227 | mock_c()->expectOneCall("foo")->withOutputParameterOfTypeReturning("typeName", "out", &retval); |
| 228 | mock_c()->actualCall("foo")->withOutputParameterOfType("typeName", "out", ¶m); |
| 229 | LONGS_EQUAL(2, param); |
| 230 | LONGS_EQUAL(2, retval); |
| 231 | mock_c()->checkExpectations(); |
| 232 | mock_c()->removeAllComparatorsAndCopiers(); |
| 233 | } |
| 234 | |
| 235 | TEST(MockSupport_c, ignoreOtherParameters) |
| 236 | { |
| 237 | mock_c()->expectOneCall("foo")->withIntParameters("int", 1)->ignoreOtherParameters(); |
| 238 | mock_c()->actualCall("foo")->withIntParameters("int", 1)->withDoubleParameters("double", 0.01); |
| 239 | mock_c()->checkExpectations(); |
| 240 | } |
| 241 | |
| 242 | TEST(MockSupport_c, returnBoolValue) |
| 243 | { |
| 244 | int expected_value = 1; |
| 245 | mock_c()->expectOneCall("boo")->andReturnBoolValue(expected_value); |
| 246 | CHECK_EQUAL(expected_value, mock_c()->actualCall("boo")->boolReturnValue()); |
| 247 | CHECK_EQUAL(expected_value, mock_c()->boolReturnValue()); |
| 248 | LONGS_EQUAL(MOCKVALUETYPE_BOOL, mock_c()->returnValue().type); |
| 249 | } |
| 250 | |
| 251 | TEST(MockSupport_c, whenReturnValueIsGivenReturnBoolValueOrDefaultShouldIgnoreTheDefault) |
| 252 | { |
| 253 | int defaultValue = 1; |
| 254 | int expectedValue = 0; |
| 255 | mock_c()->expectOneCall("foo")->andReturnBoolValue(expectedValue); |
| 256 | LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnBoolValueOrDefault(defaultValue)); |
| 257 | LONGS_EQUAL(expectedValue, mock_c()->returnBoolValueOrDefault(defaultValue)); |
| 258 | } |
| 259 | |
| 260 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnBoolValueOrDefaultShouldlUseTheDefaultValue) |
| 261 | { |
| 262 | int defaultValue = 1; |
| 263 | mock_c()->expectOneCall("foo"); |
| 264 | LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnBoolValueOrDefault(defaultValue)); |
| 265 | LONGS_EQUAL(defaultValue, mock_c()->returnBoolValueOrDefault(defaultValue)); |
| 266 | } |
| 267 | |
| 268 | TEST(MockSupport_c, returnIntValue) |
| 269 | { |
| 270 | int expected_value = -10; |
| 271 | mock_c()->expectOneCall("boo")->andReturnIntValue(expected_value); |
| 272 | LONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->intReturnValue()); |
| 273 | LONGS_EQUAL(expected_value, mock_c()->intReturnValue()); |
| 274 | LONGS_EQUAL(MOCKVALUETYPE_INTEGER, mock_c()->returnValue().type); |
| 275 | } |
| 276 | |
| 277 | TEST(MockSupport_c, whenReturnValueIsGivenReturnIntValueOrDefaultShouldIgnoreTheDefault) |
| 278 | { |
| 279 | int defaultValue = -10; |
| 280 | int expectedValue = defaultValue - 1; |
| 281 | mock_c()->expectOneCall("foo")->andReturnIntValue(expectedValue); |
| 282 | LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnIntValueOrDefault(defaultValue)); |
| 283 | LONGS_EQUAL(expectedValue, mock_c()->returnIntValueOrDefault(defaultValue)); |
| 284 | } |
| 285 | |
| 286 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnIntValueOrDefaultShouldlUseTheDefaultValue) |
| 287 | { |
| 288 | int defaultValue = -10; |
| 289 | mock_c()->expectOneCall("foo"); |
| 290 | LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnIntValueOrDefault(defaultValue)); |
| 291 | LONGS_EQUAL(defaultValue, mock_c()->returnIntValueOrDefault(defaultValue)); |
| 292 | } |
| 293 | |
| 294 | TEST(MockSupport_c, returnUnsignedIntValue) |
| 295 | { |
| 296 | unsigned int expected_value = 7; |
| 297 | mock_c()->expectOneCall("boo")->andReturnUnsignedIntValue(expected_value); |
| 298 | LONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->unsignedIntReturnValue()); |
| 299 | LONGS_EQUAL(expected_value, mock_c()->unsignedIntReturnValue()); |
| 300 | LONGS_EQUAL(MOCKVALUETYPE_UNSIGNED_INTEGER, mock_c()->returnValue().type); |
| 301 | } |
| 302 | |
| 303 | TEST(MockSupport_c, whenReturnValueIsGivenReturnUnsignedIntValueOrDefaultShouldIgnoreTheDefault) |
| 304 | { |
| 305 | unsigned int defaultValue = 10; |
| 306 | unsigned int expectedValue = defaultValue + 1; |
| 307 | mock_c()->expectOneCall("foo")->andReturnUnsignedIntValue(expectedValue); |
| 308 | LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnUnsignedIntValueOrDefault(defaultValue)); |
| 309 | LONGS_EQUAL(expectedValue, mock_c()->returnUnsignedIntValueOrDefault(defaultValue)); |
| 310 | } |
| 311 | |
| 312 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnUnsignedIntValueOrDefaultShouldlUseTheDefaultValue) |
| 313 | { |
| 314 | unsigned int defaultValue = 10; |
| 315 | mock_c()->expectOneCall("foo"); |
| 316 | LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnUnsignedIntValueOrDefault(defaultValue)); |
| 317 | LONGS_EQUAL(defaultValue, mock_c()->returnUnsignedIntValueOrDefault(defaultValue)); |
| 318 | } |
| 319 | |
| 320 | TEST(MockSupport_c, returnLongIntValue) |
| 321 | { |
| 322 | long int expected_value = -10L; |
| 323 | mock_c()->expectOneCall("boo")->andReturnLongIntValue(expected_value); |
| 324 | LONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->longIntReturnValue()); |
| 325 | LONGS_EQUAL(expected_value, mock_c()->longIntReturnValue()); |
| 326 | LONGS_EQUAL(MOCKVALUETYPE_LONG_INTEGER, mock_c()->returnValue().type); |
| 327 | } |
| 328 | |
| 329 | TEST(MockSupport_c, whenReturnValueIsGivenReturnLongIntValueOrDefaultShouldIgnoreTheDefault) |
| 330 | { |
| 331 | long int defaultValue = -10L; |
| 332 | long int expectedValue = defaultValue - 1L; |
| 333 | mock_c()->expectOneCall("foo")->andReturnLongIntValue(expectedValue); |
| 334 | LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnLongIntValueOrDefault(defaultValue)); |
| 335 | LONGS_EQUAL(expectedValue, mock_c()->returnLongIntValueOrDefault(defaultValue)); |
| 336 | } |
| 337 | |
| 338 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnLongIntValueOrDefaultShouldlUseTheDefaultValue) |
| 339 | { |
| 340 | long int defaultValue = -10L; |
| 341 | mock_c()->expectOneCall("foo"); |
| 342 | LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnLongIntValueOrDefault(defaultValue)); |
| 343 | LONGS_EQUAL(defaultValue, mock_c()->returnLongIntValueOrDefault(defaultValue)); |
| 344 | } |
| 345 | |
| 346 | TEST(MockSupport_c, returnUnsignedLongIntValue) |
| 347 | { |
| 348 | unsigned long int expected_value = 10; |
| 349 | mock_c()->expectOneCall("boo")->andReturnUnsignedLongIntValue(expected_value); |
| 350 | LONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->unsignedLongIntReturnValue()); |
| 351 | LONGS_EQUAL(expected_value, mock_c()->unsignedLongIntReturnValue()); |
| 352 | LONGS_EQUAL(MOCKVALUETYPE_UNSIGNED_LONG_INTEGER, mock_c()->returnValue().type); |
| 353 | } |
| 354 | |
| 355 | TEST(MockSupport_c, whenReturnValueIsGivenReturnUnsignedLongIntValueOrDefaultShouldIgnoreTheDefault) |
| 356 | { |
| 357 | unsigned long int defaultValue = 10L; |
| 358 | unsigned long int expectedValue = defaultValue + 1L; |
| 359 | mock_c()->expectOneCall("foo")->andReturnUnsignedLongIntValue(expectedValue); |
| 360 | LONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnUnsignedLongIntValueOrDefault(defaultValue)); |
| 361 | LONGS_EQUAL(expectedValue, mock_c()->returnUnsignedLongIntValueOrDefault(defaultValue)); |
| 362 | } |
| 363 | |
| 364 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnUnsignedLongIntValueOrDefaultShouldlUseTheDefaultValue) |
| 365 | { |
| 366 | unsigned long int defaultValue = 10L; |
| 367 | mock_c()->expectOneCall("foo"); |
| 368 | LONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnUnsignedLongIntValueOrDefault(defaultValue)); |
| 369 | LONGS_EQUAL(defaultValue, mock_c()->returnUnsignedLongIntValueOrDefault(defaultValue)); |
| 370 | } |
| 371 | |
| 372 | #ifdef CPPUTEST_USE_LONG_LONG |
| 373 | |
| 374 | TEST(MockSupport_c, returnLongLongIntValue) |
| 375 | { |
| 376 | long long int expected_value = -10L; |
| 377 | mock_c()->expectOneCall("boo")->andReturnLongLongIntValue(expected_value); |
| 378 | LONGLONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->longLongIntReturnValue()); |
| 379 | LONGLONGS_EQUAL(expected_value, mock_c()->longLongIntReturnValue()); |
| 380 | LONGLONGS_EQUAL(MOCKVALUETYPE_LONG_LONG_INTEGER, mock_c()->returnValue().type); |
| 381 | } |
| 382 | |
| 383 | TEST(MockSupport_c, whenReturnValueIsGivenReturnLongLongIntValueOrDefaultShouldIgnoreTheDefault) |
| 384 | { |
| 385 | long long int defaultValue = -10L; |
| 386 | long long int expectedValue = defaultValue - 1L; |
| 387 | mock_c()->expectOneCall("foo")->andReturnLongLongIntValue(expectedValue); |
| 388 | LONGLONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnLongLongIntValueOrDefault(defaultValue)); |
| 389 | LONGLONGS_EQUAL(expectedValue, mock_c()->returnLongLongIntValueOrDefault(defaultValue)); |
| 390 | } |
| 391 | |
| 392 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnLongLongIntValueOrDefaultShouldlUseTheDefaultValue) |
| 393 | { |
| 394 | long long int defaultValue = -10L; |
| 395 | mock_c()->expectOneCall("foo"); |
| 396 | LONGLONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnLongLongIntValueOrDefault(defaultValue)); |
| 397 | LONGLONGS_EQUAL(defaultValue, mock_c()->returnLongLongIntValueOrDefault(defaultValue)); |
| 398 | } |
| 399 | |
| 400 | TEST(MockSupport_c, returnUnsignedLongLongIntValue) |
| 401 | { |
| 402 | unsigned long long int expected_value = 10; |
| 403 | mock_c()->expectOneCall("boo")->andReturnUnsignedLongLongIntValue(expected_value); |
| 404 | UNSIGNED_LONGLONGS_EQUAL(expected_value, mock_c()->actualCall("boo")->unsignedLongLongIntReturnValue()); |
| 405 | UNSIGNED_LONGLONGS_EQUAL(expected_value, mock_c()->unsignedLongLongIntReturnValue()); |
| 406 | UNSIGNED_LONGLONGS_EQUAL(MOCKVALUETYPE_UNSIGNED_LONG_LONG_INTEGER, mock_c()->returnValue().type); |
| 407 | } |
| 408 | |
| 409 | TEST(MockSupport_c, whenReturnValueIsGivenReturnUnsignedLongLongIntValueOrDefaultShouldIgnoreTheDefault) |
| 410 | { |
| 411 | unsigned long long int defaultValue = 10L; |
| 412 | unsigned long long int expectedValue = defaultValue + 1L; |
| 413 | mock_c()->expectOneCall("foo")->andReturnUnsignedLongLongIntValue(expectedValue); |
| 414 | UNSIGNED_LONGLONGS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnUnsignedLongLongIntValueOrDefault(defaultValue)); |
| 415 | UNSIGNED_LONGLONGS_EQUAL(expectedValue, mock_c()->returnUnsignedLongLongIntValueOrDefault(defaultValue)); |
| 416 | } |
| 417 | |
| 418 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnUnsignedLongLongIntValueOrDefaultShouldlUseTheDefaultValue) |
| 419 | { |
| 420 | unsigned long long int defaultValue = 10L; |
| 421 | mock_c()->expectOneCall("foo"); |
| 422 | UNSIGNED_LONGLONGS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnUnsignedLongLongIntValueOrDefault(defaultValue)); |
| 423 | UNSIGNED_LONGLONGS_EQUAL(defaultValue, mock_c()->returnUnsignedLongLongIntValueOrDefault(defaultValue)); |
| 424 | } |
| 425 | |
| 426 | #endif |
| 427 | |
| 428 | TEST(MockSupport_c, returnStringValue) |
| 429 | { |
| 430 | mock_c()->expectOneCall("boo")->andReturnStringValue("hello world"); |
| 431 | STRCMP_EQUAL("hello world", mock_c()->actualCall("boo")->stringReturnValue()); |
| 432 | STRCMP_EQUAL("hello world", mock_c()->stringReturnValue()); |
| 433 | LONGS_EQUAL(MOCKVALUETYPE_STRING, mock_c()->returnValue().type); |
| 434 | } |
| 435 | |
| 436 | TEST(MockSupport_c, whenReturnValueIsGivenReturnStringValueOrDefaultShouldIgnoreTheDefault) |
| 437 | { |
| 438 | const char defaultValue[] = "bar"; |
| 439 | const char expectedValue[] = "bla"; |
| 440 | mock_c()->expectOneCall("foo")->andReturnStringValue(expectedValue); |
| 441 | STRCMP_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnStringValueOrDefault(defaultValue)); |
| 442 | STRCMP_EQUAL(expectedValue, mock_c()->returnStringValueOrDefault(defaultValue)); |
| 443 | } |
| 444 | |
| 445 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnStringValueOrDefaultShouldlUseTheDefaultValue) |
| 446 | { |
| 447 | const char defaultValue[] = "bar"; |
| 448 | mock_c()->expectOneCall("foo"); |
| 449 | STRCMP_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnStringValueOrDefault(defaultValue)); |
| 450 | STRCMP_EQUAL(defaultValue, mock_c()->returnStringValueOrDefault(defaultValue)); |
| 451 | } |
| 452 | |
| 453 | TEST(MockSupport_c, returnDoubleValue) |
| 454 | { |
| 455 | mock_c()->expectOneCall("boo")->andReturnDoubleValue(1.0); |
| 456 | DOUBLES_EQUAL(1.0, mock_c()->actualCall("boo")->doubleReturnValue(), 0.005); |
| 457 | DOUBLES_EQUAL(1.0, mock_c()->doubleReturnValue(), 0.005); |
| 458 | LONGS_EQUAL(MOCKVALUETYPE_DOUBLE, mock_c()->returnValue().type); |
| 459 | } |
| 460 | |
| 461 | TEST(MockSupport_c, whenReturnValueIsGivenReturnDoubleValueOrDefaultShouldIgnoreTheDefault) |
| 462 | { |
| 463 | double defaultValue = 2.2; |
| 464 | double expectedValue = defaultValue + 0.1; |
| 465 | mock_c()->expectOneCall("foo")->andReturnDoubleValue(expectedValue); |
| 466 | DOUBLES_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnDoubleValueOrDefault(defaultValue), 0.005); |
| 467 | DOUBLES_EQUAL(expectedValue, mock_c()->returnDoubleValueOrDefault(defaultValue), 0.005); |
| 468 | } |
| 469 | |
| 470 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnDoubleValueOrDefaultShouldlUseTheDefaultValue) |
| 471 | { |
| 472 | double defaultValue = 2.2; |
| 473 | mock_c()->expectOneCall("foo"); |
| 474 | DOUBLES_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnDoubleValueOrDefault(defaultValue), 0.005); |
| 475 | DOUBLES_EQUAL(defaultValue, mock_c()->returnDoubleValueOrDefault(defaultValue), 0.005); |
| 476 | } |
| 477 | |
| 478 | TEST(MockSupport_c, returnPointerValue) |
| 479 | { |
| 480 | mock_c()->expectOneCall("boo")->andReturnPointerValue((void*) 10); |
| 481 | POINTERS_EQUAL((void*) 10, mock_c()->actualCall("boo")->pointerReturnValue()); |
| 482 | POINTERS_EQUAL((void*) 10, mock_c()->pointerReturnValue()); |
| 483 | LONGS_EQUAL(MOCKVALUETYPE_POINTER, mock_c()->returnValue().type); |
| 484 | } |
| 485 | |
| 486 | TEST(MockSupport_c, whenReturnValueIsGivenReturnPointerValueOrDefaultShouldIgnoreTheDefault) |
| 487 | { |
| 488 | void* defaultValue = (void*) 10; |
| 489 | void* expectedValue = (void*) 27; |
| 490 | mock_c()->expectOneCall("foo")->andReturnPointerValue(expectedValue); |
| 491 | POINTERS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnPointerValueOrDefault(defaultValue)); |
| 492 | POINTERS_EQUAL(expectedValue, mock_c()->returnPointerValueOrDefault(defaultValue)); |
| 493 | } |
| 494 | |
| 495 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnPointerValueOrDefaultShouldlUseTheDefaultValue) |
| 496 | { |
| 497 | void* defaultValue = (void*) 10; |
| 498 | mock_c()->expectOneCall("foo"); |
| 499 | POINTERS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnPointerValueOrDefault(defaultValue)); |
| 500 | POINTERS_EQUAL(defaultValue, mock_c()->returnPointerValueOrDefault(defaultValue)); |
| 501 | } |
| 502 | |
| 503 | TEST(MockSupport_c, returnConstPointerValue) |
| 504 | { |
| 505 | mock_c()->expectOneCall("boo")->andReturnConstPointerValue((const void*) 10); |
| 506 | POINTERS_EQUAL((const void*) 10, mock_c()->actualCall("boo")->constPointerReturnValue()); |
| 507 | POINTERS_EQUAL((const void*) 10, mock_c()->constPointerReturnValue()); |
| 508 | LONGS_EQUAL(MOCKVALUETYPE_CONST_POINTER, mock_c()->returnValue().type); |
| 509 | } |
| 510 | |
| 511 | TEST(MockSupport_c, whenReturnValueIsGivenReturnConstPointerValueOrDefaultShouldIgnoreTheDefault) |
| 512 | { |
| 513 | const void* defaultValue = (void*) 10; |
| 514 | const void* expectedValue = (void*) 27; |
| 515 | mock_c()->expectOneCall("foo")->andReturnConstPointerValue(expectedValue); |
| 516 | POINTERS_EQUAL(expectedValue, mock_c()->actualCall("foo")->returnConstPointerValueOrDefault(defaultValue)); |
| 517 | POINTERS_EQUAL(expectedValue, mock_c()->returnConstPointerValueOrDefault(defaultValue)); |
| 518 | } |
| 519 | |
| 520 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnConstPointerValueOrDefaultShouldlUseTheDefaultValue) |
| 521 | { |
| 522 | const void* defaultValue = (void*) 10; |
| 523 | mock_c()->expectOneCall("foo"); |
| 524 | POINTERS_EQUAL(defaultValue, mock_c()->actualCall("foo")->returnConstPointerValueOrDefault(defaultValue)); |
| 525 | POINTERS_EQUAL(defaultValue, mock_c()->returnConstPointerValueOrDefault(defaultValue)); |
| 526 | } |
| 527 | |
| 528 | TEST(MockSupport_c, returnFunctionPointerValue) |
| 529 | { |
| 530 | mock_c()->expectOneCall("boo")->andReturnFunctionPointerValue(dummy_function_for_mock_c_test); |
| 531 | FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->actualCall("boo")->functionPointerReturnValue()); |
| 532 | FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->functionPointerReturnValue()); |
| 533 | LONGS_EQUAL(MOCKVALUETYPE_FUNCTIONPOINTER, mock_c()->returnValue().type); |
| 534 | } |
| 535 | |
| 536 | TEST(MockSupport_c, whenReturnValueIsGivenReturnFunctionPointerValueOrDefaultShouldIgnoreTheDefault) |
| 537 | { |
| 538 | mock_c()->expectOneCall("foo")->andReturnFunctionPointerValue(dummy_function_for_mock_c_test); |
| 539 | FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->actualCall("foo")->returnFunctionPointerValueOrDefault(dummy_function_for_mock_c_test_two)); |
| 540 | FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->returnFunctionPointerValueOrDefault(dummy_function_for_mock_c_test_two)); |
| 541 | } |
| 542 | |
| 543 | TEST(MockSupport_c, whenNoReturnValueIsGivenReturnFunctionPointerValueOrDefaultShouldlUseTheDefaultValue) |
| 544 | { |
| 545 | mock_c()->expectOneCall("foo"); |
| 546 | FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test_two, mock_c()->actualCall("foo")->returnFunctionPointerValueOrDefault(dummy_function_for_mock_c_test_two)); |
| 547 | FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test_two, mock_c()->returnFunctionPointerValueOrDefault(dummy_function_for_mock_c_test_two)); |
| 548 | } |
| 549 | |
| 550 | TEST(MockSupport_c, MockSupportWithScope) |
| 551 | { |
| 552 | mock_scope_c("scope")->expectOneCall("boo"); |
| 553 | LONGS_EQUAL(0, mock_scope_c("other")->expectedCallsLeft()); |
| 554 | LONGS_EQUAL(1, mock_scope_c("scope")->expectedCallsLeft()); |
| 555 | mock_scope_c("scope")->actualCall("boo"); |
| 556 | } |
| 557 | |
| 558 | TEST(MockSupport_c, MockSupportSetBoolData) |
| 559 | { |
| 560 | mock_c()->setBoolData("boolean", 1); |
| 561 | CHECK_EQUAL(1, mock_c()->getData("boolean").value.boolValue); |
| 562 | } |
| 563 | |
| 564 | TEST(MockSupport_c, MockSupportSetIntData) |
| 565 | { |
| 566 | mock_c()->setIntData("integer", 10); |
| 567 | LONGS_EQUAL(10, mock_c()->getData("integer").value.intValue); |
| 568 | } |
| 569 | |
| 570 | TEST(MockSupport_c, MockSupportSetDoubleData) |
| 571 | { |
| 572 | mock_c()->setDoubleData("double", 1.0); |
| 573 | DOUBLES_EQUAL(1.00, mock_c()->getData("double").value.doubleValue, 0.05); |
| 574 | } |
| 575 | |
| 576 | TEST(MockSupport_c, MockSupportSetStringData) |
| 577 | { |
| 578 | mock_c()->setStringData("string", "hello world"); |
| 579 | STRCMP_EQUAL("hello world", mock_c()->getData("string").value.stringValue); |
| 580 | } |
| 581 | |
| 582 | TEST(MockSupport_c, MockSupportSetPointerData) |
| 583 | { |
| 584 | mock_c()->setPointerData("pointer", (void*) 1); |
| 585 | POINTERS_EQUAL((void*) 1, mock_c()->getData("pointer").value.pointerValue); |
| 586 | } |
| 587 | |
| 588 | TEST(MockSupport_c, MockSupportSetConstPointerData) |
| 589 | { |
| 590 | mock_c()->setConstPointerData("constPointer", (const void*) 1); |
| 591 | POINTERS_EQUAL((const void*) 1, mock_c()->getData("constPointer").value.constPointerValue); |
| 592 | } |
| 593 | |
| 594 | TEST(MockSupport_c, MockSupportMemoryBufferData) |
| 595 | { |
| 596 | mock_c()->setDataObject("name", "const unsigned char*", (void *) 0xDEAD); |
| 597 | POINTERS_EQUAL(0xDEAD, mock_c()->getData("name").value.memoryBufferValue); |
| 598 | LONGS_EQUAL(MOCKVALUETYPE_MEMORYBUFFER, mock_c()->getData("name").type); |
| 599 | } |
| 600 | |
| 601 | TEST(MockSupport_c, MockSupportSetFunctionPointerData) |
| 602 | { |
| 603 | mock_c()->setFunctionPointerData("functionPointer", dummy_function_for_mock_c_test); |
| 604 | FUNCTIONPOINTERS_EQUAL(dummy_function_for_mock_c_test, mock_c()->getData("functionPointer").value.functionPointerValue); |
| 605 | } |
| 606 | |
| 607 | TEST(MockSupport_c, MockSupportSetDataObject) |
| 608 | { |
| 609 | mock_c()->setDataObject("name", "type", (void*) 1); |
| 610 | POINTERS_EQUAL((void*) 1, mock_c()->getData("name").value.objectValue); |
| 611 | } |
| 612 | |
| 613 | TEST(MockSupport_c, MockSupportSetDataConstObject) |
| 614 | { |
| 615 | mock_c()->setDataConstObject("name", "type", (const void*) 5); |
| 616 | POINTERS_EQUAL((void*) 5, mock_c()->getData("name").value.constObjectValue); |
| 617 | } |
| 618 | |
| 619 | TEST(MockSupport_c, WorksInCFile) |
| 620 | { |
| 621 | all_mock_support_c_calls(); |
| 622 | } |
| 623 | |
| 624 | static bool destructorWasCalled = false; |
| 625 | |
| 626 | static void failedCallToMockC() |
| 627 | { |
| 628 | SetBooleanOnDestructorCall setOneDestructor(destructorWasCalled); |
| 629 | mock_c()->actualCall("Not a call"); |
| 630 | } // LCOV_EXCL_LINE |
| 631 | |
| 632 | // Silly wrapper because of a test that only fails in Visual C++ due to different |
| 633 | // destructor behaviors |
| 634 | #ifdef _MSC_VER |
| 635 | #define MSC_SWITCHED_TEST(testGroup, testName) IGNORE_TEST(testGroup, testName) |
| 636 | #else |
| 637 | #define MSC_SWITCHED_TEST(testGroup, testName) TEST(testGroup, testName) |
| 638 | #endif |
| 639 | |
| 640 | MSC_SWITCHED_TEST(MockSupport_c, NoExceptionsAreThrownWhenAMock_cCallFailed) |
| 641 | { |
| 642 | TestTestingFixture fixture; |
| 643 | |
| 644 | fixture.setTestFunction(failedCallToMockC); |
| 645 | fixture.runAllTests(); |
| 646 | |
| 647 | LONGS_EQUAL(1, fixture.getFailureCount()); |
| 648 | // Odd behavior in Visual C++, destructor still gets called here |
| 649 | CHECK(!destructorWasCalled); |
| 650 | } |
| 651 | |
| 652 | static bool cpputestHasCrashed; |
| 653 | |
| 654 | static void crashMethod() |
| 655 | { |
| 656 | cpputestHasCrashed = true; |
| 657 | } |
| 658 | |
| 659 | TEST_ORDERED(MockSupport_c, shouldCrashOnFailure, 21) |
| 660 | { |
| 661 | cpputestHasCrashed = false; |
| 662 | TestTestingFixture fixture; |
| 663 | UtestShell::setCrashMethod(crashMethod); |
| 664 | mock_c()->crashOnFailure(true); |
| 665 | fixture.setTestFunction(failedCallToMockC); |
| 666 | |
| 667 | fixture.runAllTests(); |
| 668 | |
| 669 | CHECK(cpputestHasCrashed); |
| 670 | |
| 671 | UtestShell::resetCrashMethod(); |
| 672 | mock_c()->crashOnFailure(false); |
| 673 | } |
| 674 | |
| 675 | TEST_ORDERED(MockSupport_c, nextTestShouldNotCrashOnFailure, 22) |
| 676 | { |
| 677 | cpputestHasCrashed = false; |
| 678 | TestTestingFixture fixture; |
| 679 | UtestShell::setCrashMethod(crashMethod); |
| 680 | fixture.setTestFunction(failedCallToMockC); |
| 681 | |
| 682 | fixture.runAllTests(); |
| 683 | |
| 684 | CHECK_FALSE(cpputestHasCrashed); |
| 685 | |
| 686 | UtestShell::resetCrashMethod(); |
| 687 | } |
| 688 | |
| 689 | static void failingCallToMockCWithParameterOfType_() |
| 690 | { |
| 691 | mock_c()->expectOneCall("bar")->withParameterOfType("typeName", "name", (const void*) 1); |
| 692 | mock_c()->actualCall("bar")->withParameterOfType("typeName", "name", (const void*) 2); |
| 693 | } // LCOV_EXCL_LINE |
| 694 | |
| 695 | TEST(MockSupport_c, failureWithParameterOfTypeCoversValueToString) |
| 696 | { |
| 697 | TestTestingFixture fixture; |
| 698 | mock_c()->installComparator("typeName", typeNameIsEqual, typeNameValueToString); |
| 699 | fixture.setTestFunction(failingCallToMockCWithParameterOfType_); |
| 700 | fixture.runAllTests(); |
| 701 | fixture.assertPrintContains("typeName name: <valueToString>"); |
| 702 | mock_c()->removeAllComparatorsAndCopiers(); |
| 703 | } |
| 704 | |
| 705 | static void callToMockCWithOutputParameterOfType_() |
| 706 | { |
| 707 | int value1 = 7; |
| 708 | const int value2 = 9; |
| 709 | mock_c()->expectOneCall("bar")->withOutputParameterOfTypeReturning("intType", "bla", &value2); |
| 710 | mock_c()->actualCall("bar")->withOutputParameterOfType("intType", "bla", &value1); |
| 711 | LONGS_EQUAL(value1, value2); |
| 712 | } |
| 713 | |
| 714 | TEST(MockSupport_c, successWithOutputParameterOfType) |
| 715 | { |
| 716 | TestTestingFixture fixture; |
| 717 | mock_c()->installCopier("intType", typeCopy); |
| 718 | fixture.setTestFunction(callToMockCWithOutputParameterOfType_); |
| 719 | fixture.runAllTests(); |
| 720 | LONGS_EQUAL(2, fixture.getCheckCount()); |
| 721 | LONGS_EQUAL(0, fixture.getFailureCount()); |
| 722 | mock_c()->removeAllComparatorsAndCopiers(); |
| 723 | } |
| 724 | |
| 725 | static void failingCallToMockCWithMemoryBuffer_() |
| 726 | { |
| 727 | unsigned char memBuffer1[] = { 0x12, 0x15, 0xFF }; |
| 728 | unsigned char memBuffer2[] = { 0x12, 0x05, 0xFF }; |
| 729 | mock_c()->expectOneCall("bar")->withMemoryBufferParameter("name", memBuffer1, sizeof(memBuffer1)); |
| 730 | mock_c()->actualCall("bar")->withMemoryBufferParameter("name", memBuffer2, sizeof(memBuffer2)); |
| 731 | } // LCOV_EXCL_LINE |
| 732 | |
| 733 | TEST(MockSupport_c, expectOneMemBufferParameterAndValueFailsDueToContents) |
| 734 | { |
| 735 | TestTestingFixture fixture; |
| 736 | fixture.setTestFunction(failingCallToMockCWithMemoryBuffer_); |
| 737 | fixture.runAllTests(); |
| 738 | fixture.assertPrintContains("Unexpected parameter value to parameter \"name\" " |
| 739 | "to function \"bar\": <Size = 3 | HexContents = 12 05 FF>"); |
| 740 | } |
| 741 | |
| 742 | TEST(MockSupport_c, ignoreOtherCalls) |
| 743 | { |
| 744 | mock_c()->expectOneCall("foo"); |
| 745 | mock_c()->ignoreOtherCalls(); |
| 746 | mock_c()->actualCall("foo"); |
| 747 | mock_c()->actualCall("bar"); |
| 748 | mock_c()->checkExpectations(); |
| 749 | } |