| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include "stdafx.h" |
| 5 |
|
| 6 |
using namespace std; |
| 7 |
|
| 8 |
static int tstring_Compare(const std::tstring& a, const std::tstring&b) |
| 9 |
{ |
| 10 |
return strcmp(a.c_str(), b.c_str()); |
| 11 |
} |
| 12 |
static void tstring_ToString(char* string, size_t bufferSize, std::tstring val) |
| 13 |
{ |
| 14 |
size_t val_size = strlen(val.c_str()); |
| 15 |
strncpy(string, val.c_str(), (val_size >= bufferSize) ? (bufferSize - 1) : val_size); |
| 16 |
} |
| 17 |
|
| 18 |
TYPED_MOCK_CLASS(CTestArgBufferMock, CMock) |
| 19 |
{ |
| 20 |
public: |
| 21 |
MOCK_METHOD_1(, void, Test1ArgFunction, UINT8*, bufferArg); |
| 22 |
MOCK_VOID_METHOD_END() |
| 23 |
|
| 24 |
MOCK_METHOD_2(, void, Test2ArgsFunction, UINT8*, buffer1Arg, UINT8*, buffer2Arg); |
| 25 |
MOCK_VOID_METHOD_END() |
| 26 |
}; |
| 27 |
|
| 28 |
static MICROMOCK_MUTEX_HANDLE g_testByTest; |
| 29 |
static MICROMOCK_GLOBAL_SEMAPHORE_HANDLE g_dllByDll; |
| 30 |
|
| 31 |
BEGIN_TEST_SUITE(MicroMockValidateArgumentBufferTests) |
| 32 |
|
| 33 |
TEST_SUITE_INITIALIZE(TestClassInitialize) |
| 34 |
{ |
| 35 |
g_testByTest = MicroMockCreateMutex(); |
| 36 |
ASSERT_IS_NOT_NULL(g_testByTest); |
| 37 |
} |
| 38 |
|
| 39 |
TEST_SUITE_CLEANUP(TestClassCleanup) |
| 40 |
{ |
| 41 |
MicroMockDestroyMutex(g_testByTest); |
| 42 |
} |
| 43 |
|
| 44 |
TEST_FUNCTION_INITIALIZE(TestMethodInitialize) |
| 45 |
{ |
| 46 |
if (!MicroMockAcquireMutex(g_testByTest)) |
| 47 |
{ |
| 48 |
ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework"); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
TEST_FUNCTION_CLEANUP(TestMethodCleanup) |
| 53 |
{ |
| 54 |
if (!MicroMockReleaseMutex(g_testByTest)) |
| 55 |
{ |
| 56 |
ASSERT_FAIL("failure in test framework at ReleaseMutex"); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_Zero_Argument_Index_Throws) |
| 61 |
{ |
| 62 |
// arrange |
| 63 |
CTestArgBufferMock testMock; |
| 64 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 65 |
|
| 66 |
UINT8 expectedArgumentBuffer[1] = {42}; |
| 67 |
MICROMOCK_EXCEPTION exceptionCode = MICROMOCK_EXCEPTION_INTERNAL_ERROR; // anything that is different than the expected code |
| 68 |
|
| 69 |
try |
| 70 |
{ |
| 71 |
// act |
| 72 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 73 |
.ValidateArgumentBuffer(0, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 74 |
} |
| 75 |
catch (CMicroMockException e) |
| 76 |
{ |
| 77 |
exceptionCode = e.GetMicroMockExceptionCode(); |
| 78 |
} |
| 79 |
|
| 80 |
// assert |
| 81 |
ASSERT_ARE_EQUAL_WITH_MSG(int, (int)MICROMOCK_EXCEPTION_INVALID_ARGUMENT, (int)exceptionCode, |
| 82 |
_T("Expected an invalid argument exception")); |
| 83 |
} |
| 84 |
|
| 85 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_Argument_Index_Out_Of_Bounds_Throws) |
| 86 |
{ |
| 87 |
// arrange |
| 88 |
CTestArgBufferMock testMock; |
| 89 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 90 |
|
| 91 |
UINT8 expectedArgumentBuffer[1] = {42}; |
| 92 |
MICROMOCK_EXCEPTION exceptionCode = MICROMOCK_EXCEPTION_INTERNAL_ERROR; // anything that is different than the expected code |
| 93 |
|
| 94 |
try |
| 95 |
{ |
| 96 |
// act |
| 97 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 98 |
.ValidateArgumentBuffer(2, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 99 |
} |
| 100 |
catch (CMicroMockException e) |
| 101 |
{ |
| 102 |
exceptionCode = e.GetMicroMockExceptionCode(); |
| 103 |
} |
| 104 |
|
| 105 |
// assert |
| 106 |
ASSERT_ARE_EQUAL_WITH_MSG(int, (int)MICROMOCK_EXCEPTION_INVALID_ARGUMENT, (int)exceptionCode, |
| 107 |
_T("Expected an invalid argument exception")); |
| 108 |
} |
| 109 |
|
| 110 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_When_Expected_Memory_Matches_Indicates_No_Call_Mismatch) |
| 111 |
{ |
| 112 |
// arrange |
| 113 |
CTestArgBufferMock testMock; |
| 114 |
UINT8 expectedArgumentBuffer[1] = {42}; |
| 115 |
UINT8 actualArgumentBuffer[1] = {42}; |
| 116 |
|
| 117 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 118 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 119 |
|
| 120 |
// act |
| 121 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 122 |
|
| 123 |
// assert |
| 124 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("")), testMock.CompareActualAndExpectedCalls(), |
| 125 |
_T("Expected and actual calls do not match")); |
| 126 |
} |
| 127 |
|
| 128 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_When_Expected_Memory_Matches_And_Expetced_Call_Argument_Value_Is_NULL_Indicates_No_Call_Mismatch) |
| 129 |
{ |
| 130 |
// arrange |
| 131 |
CTestArgBufferMock testMock; |
| 132 |
UINT8 expectedArgumentBuffer[1] = {42}; |
| 133 |
UINT8 actualArgumentBuffer[1] = {42}; |
| 134 |
|
| 135 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 136 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 137 |
|
| 138 |
// act |
| 139 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 140 |
|
| 141 |
// assert |
| 142 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("")), testMock.CompareActualAndExpectedCalls(), |
| 143 |
_T("Expected and actual calls do not match")); |
| 144 |
} |
| 145 |
|
| 146 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_When_Expected_Memory_Does_Not_Match_Indicates_A_Call_Mismatch) |
| 147 |
{ |
| 148 |
// arrange |
| 149 |
CTestArgBufferMock testMock; |
| 150 |
UINT8 expectedArgumentBuffer[1] = { 0x43 }; |
| 151 |
UINT8 actualArgumentBuffer[1] = { 0x42 }; |
| 152 |
|
| 153 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 154 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 155 |
|
| 156 |
// act |
| 157 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 158 |
|
| 159 |
// assert |
| 160 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([43])][Actual:Test1ArgFunction([42])]")), testMock.CompareActualAndExpectedCalls(), |
| 161 |
_T("Expected and actual calls do not match")); |
| 162 |
} |
| 163 |
|
| 164 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_When_Expected_Buffer_Is_NULL_Throws) |
| 165 |
{ |
| 166 |
// arrange |
| 167 |
CTestArgBufferMock testMock; |
| 168 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 169 |
|
| 170 |
UINT8 expectedArgumentBuffer[1] = {42}; |
| 171 |
MICROMOCK_EXCEPTION exceptionCode = MICROMOCK_EXCEPTION_INTERNAL_ERROR; // anything that is different than the expected code |
| 172 |
|
| 173 |
try |
| 174 |
{ |
| 175 |
// act |
| 176 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 177 |
.ValidateArgumentBuffer(1, NULL, sizeof(expectedArgumentBuffer)); |
| 178 |
} |
| 179 |
catch (CMicroMockException e) |
| 180 |
{ |
| 181 |
exceptionCode = e.GetMicroMockExceptionCode(); |
| 182 |
} |
| 183 |
|
| 184 |
// assert |
| 185 |
ASSERT_ARE_EQUAL_WITH_MSG(int, (int)MICROMOCK_EXCEPTION_INVALID_ARGUMENT, (int)exceptionCode, |
| 186 |
_T("Expected an invalid argument exception")); |
| 187 |
} |
| 188 |
|
| 189 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_0_Size_Throws) |
| 190 |
{ |
| 191 |
// arrange |
| 192 |
CTestArgBufferMock testMock; |
| 193 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 194 |
|
| 195 |
UINT8 expectedArgumentBuffer[1] = {42}; |
| 196 |
MICROMOCK_EXCEPTION exceptionCode = MICROMOCK_EXCEPTION_INTERNAL_ERROR; // anything that is different than the expected code |
| 197 |
|
| 198 |
try |
| 199 |
{ |
| 200 |
// act |
| 201 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 202 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, 0); |
| 203 |
} |
| 204 |
catch (CMicroMockException e) |
| 205 |
{ |
| 206 |
exceptionCode = e.GetMicroMockExceptionCode(); |
| 207 |
} |
| 208 |
|
| 209 |
// assert |
| 210 |
ASSERT_ARE_EQUAL_WITH_MSG(int, (int)MICROMOCK_EXCEPTION_INVALID_ARGUMENT, (int)exceptionCode, |
| 211 |
_T("Expected an invalid argument exception")); |
| 212 |
} |
| 213 |
|
| 214 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Only_Byte_1_Of_2_Checked_When_1st_Byte_Matches) |
| 215 |
{ |
| 216 |
// arrange |
| 217 |
CTestArgBufferMock testMock; |
| 218 |
UINT8 expectedArgumentBuffer[2] = {42, 43}; |
| 219 |
UINT8 actualArgumentBuffer[2] = {42, 42}; |
| 220 |
|
| 221 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 222 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, 1); |
| 223 |
|
| 224 |
// act |
| 225 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 226 |
|
| 227 |
// assert |
| 228 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("")), testMock.CompareActualAndExpectedCalls(), |
| 229 |
_T("Expected and actual calls do not match")); |
| 230 |
} |
| 231 |
|
| 232 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Only_Byte_1_Of_2_Checked_When_1st_Byte_Does_Not_Match) |
| 233 |
{ |
| 234 |
// arrange |
| 235 |
CTestArgBufferMock testMock; |
| 236 |
UINT8 expectedArgumentBuffer[2] = {0x42, 0x42}; |
| 237 |
UINT8 actualArgumentBuffer[2] = {0x43, 0x42}; |
| 238 |
|
| 239 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 240 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, 1); |
| 241 |
|
| 242 |
// act |
| 243 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 244 |
|
| 245 |
// assert |
| 246 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([42])][Actual:Test1ArgFunction([43])]")), testMock.CompareActualAndExpectedCalls(), |
| 247 |
_T("Expected and actual calls do not match")); |
| 248 |
} |
| 249 |
|
| 250 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Only_Byte_2_Of_2_Checked_When_2nd_Byte_Matches) |
| 251 |
{ |
| 252 |
// arrange |
| 253 |
CTestArgBufferMock testMock; |
| 254 |
UINT8 expectedArgumentBuffer[] = {42}; |
| 255 |
UINT8 actualArgumentBuffer[2] = {43, 42}; |
| 256 |
|
| 257 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 258 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, 1, 1); |
| 259 |
|
| 260 |
// act |
| 261 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 262 |
|
| 263 |
// assert |
| 264 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("")), testMock.CompareActualAndExpectedCalls(), |
| 265 |
_T("Expected and actual calls do not match")); |
| 266 |
} |
| 267 |
|
| 268 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Only_Byte_2_Of_2_Checked_When_2nd_Byte_Does_Not_Match) |
| 269 |
{ |
| 270 |
// arrange |
| 271 |
CTestArgBufferMock testMock; |
| 272 |
UINT8 expectedArgumentBuffer[] = {0x42}; |
| 273 |
UINT8 actualArgumentBuffer[2] = {0x42, 0x43}; |
| 274 |
|
| 275 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 276 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, 1, 1); |
| 277 |
|
| 278 |
// act |
| 279 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 280 |
|
| 281 |
// assert |
| 282 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([.. 42])][Actual:Test1ArgFunction([.. 43])]")), testMock.CompareActualAndExpectedCalls(), |
| 283 |
_T("Expected and actual calls do not match")); |
| 284 |
} |
| 285 |
|
| 286 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Makes_A_Copy_Of_The_Expected_Buffer) |
| 287 |
{ |
| 288 |
// arrange |
| 289 |
CTestArgBufferMock testMock; |
| 290 |
UINT8 expectedArgumentBuffer[1] = {42}; |
| 291 |
UINT8 actualArgumentBuffer[1] = {42}; |
| 292 |
|
| 293 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 294 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 295 |
|
| 296 |
expectedArgumentBuffer[0] = 41; |
| 297 |
|
| 298 |
// act |
| 299 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 300 |
|
| 301 |
// assert |
| 302 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("")), testMock.CompareActualAndExpectedCalls(), |
| 303 |
_T("Expected and actual calls do not match")); |
| 304 |
} |
| 305 |
|
| 306 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_For_2_Bytes_Formats_Both_Bytes_Into_The_Assert_String) |
| 307 |
{ |
| 308 |
// arrange |
| 309 |
CTestArgBufferMock testMock; |
| 310 |
UINT8 expectedArgumentBuffer[2] = {0x42, 0x42}; |
| 311 |
UINT8 actualArgumentBuffer[2] = {0x42, 0x43}; |
| 312 |
|
| 313 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 314 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, 2); |
| 315 |
|
| 316 |
// act |
| 317 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 318 |
|
| 319 |
// assert |
| 320 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([42 42])][Actual:Test1ArgFunction([42 43])]")), testMock.CompareActualAndExpectedCalls(), |
| 321 |
_T("Expected and actual calls do not match")); |
| 322 |
} |
| 323 |
|
| 324 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_For_1_Digit_Values_Works) |
| 325 |
{ |
| 326 |
// arrange |
| 327 |
CTestArgBufferMock testMock; |
| 328 |
UINT8 expectedArgumentBuffer[] = {0x2}; |
| 329 |
UINT8 actualArgumentBuffer[] = {0x3}; |
| 330 |
|
| 331 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 332 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 333 |
|
| 334 |
// act |
| 335 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 336 |
|
| 337 |
// assert |
| 338 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([02])][Actual:Test1ArgFunction([03])]")), testMock.CompareActualAndExpectedCalls(), |
| 339 |
_T("Expected and actual calls do not match")); |
| 340 |
} |
| 341 |
|
| 342 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_Expected_0x0F_Formats_The_Argument_Buffer_Correctly) |
| 343 |
{ |
| 344 |
// arrange |
| 345 |
CTestArgBufferMock testMock; |
| 346 |
UINT8 expectedArgumentBuffer[] = {0xF}; |
| 347 |
UINT8 actualArgumentBuffer[] = {0x0}; |
| 348 |
|
| 349 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 350 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 351 |
|
| 352 |
// act |
| 353 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 354 |
|
| 355 |
// assert |
| 356 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([0F])][Actual:Test1ArgFunction([00])]")), testMock.CompareActualAndExpectedCalls(), |
| 357 |
_T("Expected and actual calls do not match")); |
| 358 |
} |
| 359 |
|
| 360 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_Expected_0xFF_Formats_The_Argument_Buffer_Correctly) |
| 361 |
{ |
| 362 |
// arrange |
| 363 |
CTestArgBufferMock testMock; |
| 364 |
UINT8 expectedArgumentBuffer[] = {0xFF}; |
| 365 |
UINT8 actualArgumentBuffer[] = {0x0}; |
| 366 |
|
| 367 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 368 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 369 |
|
| 370 |
// act |
| 371 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 372 |
|
| 373 |
// assert |
| 374 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([FF])][Actual:Test1ArgFunction([00])]")), testMock.CompareActualAndExpectedCalls(), |
| 375 |
_T("Expected and actual calls do not match")); |
| 376 |
} |
| 377 |
|
| 378 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_Actual_0x0F_Formats_The_Argument_Buffer_Correctly) |
| 379 |
{ |
| 380 |
// arrange |
| 381 |
CTestArgBufferMock testMock; |
| 382 |
UINT8 expectedArgumentBuffer[] = {0x0}; |
| 383 |
UINT8 actualArgumentBuffer[] = {0xF}; |
| 384 |
|
| 385 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 386 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 387 |
|
| 388 |
// act |
| 389 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 390 |
|
| 391 |
// assert |
| 392 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([00])][Actual:Test1ArgFunction([0F])]")), testMock.CompareActualAndExpectedCalls(), |
| 393 |
_T("Expected and actual calls do not match")); |
| 394 |
} |
| 395 |
|
| 396 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_Actual_0xFF_Formats_The_Argument_Buffer_Correctly) |
| 397 |
{ |
| 398 |
// arrange |
| 399 |
CTestArgBufferMock testMock; |
| 400 |
UINT8 expectedArgumentBuffer[] = {0x00}; |
| 401 |
UINT8 actualArgumentBuffer[] = {0xFF}; |
| 402 |
|
| 403 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 404 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 405 |
|
| 406 |
// act |
| 407 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 408 |
|
| 409 |
// assert |
| 410 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([00])][Actual:Test1ArgFunction([FF])]")), testMock.CompareActualAndExpectedCalls(), |
| 411 |
_T("Expected and actual calls do not match")); |
| 412 |
} |
| 413 |
|
| 414 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_For_2_Bytes_With_1_Digit_Values_Works) |
| 415 |
{ |
| 416 |
// arrange |
| 417 |
CTestArgBufferMock testMock; |
| 418 |
UINT8 expectedArgumentBuffer[] = {0x2, 0x3}; |
| 419 |
UINT8 actualArgumentBuffer[] = {0x3, 0x2}; |
| 420 |
|
| 421 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 422 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 423 |
|
| 424 |
// act |
| 425 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 426 |
|
| 427 |
// assert |
| 428 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([02 03])][Actual:Test1ArgFunction([03 02])]")), testMock.CompareActualAndExpectedCalls(), |
| 429 |
_T("Expected and actual calls do not match")); |
| 430 |
} |
| 431 |
|
| 432 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_For_2_Bytes_Containing_0x0F_And_0xFF_Outputs_The_Correct_Formatted_Buffer) |
| 433 |
{ |
| 434 |
// arrange |
| 435 |
CTestArgBufferMock testMock; |
| 436 |
UINT8 expectedArgumentBuffer[] = {0xF, 0xFF}; |
| 437 |
UINT8 actualArgumentBuffer[] = {0x0, 0x0}; |
| 438 |
|
| 439 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 440 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, sizeof(expectedArgumentBuffer)); |
| 441 |
|
| 442 |
// act |
| 443 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 444 |
|
| 445 |
// assert |
| 446 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([0F FF])][Actual:Test1ArgFunction([00 00])]")), testMock.CompareActualAndExpectedCalls(), |
| 447 |
_T("Expected and actual calls do not match")); |
| 448 |
} |
| 449 |
|
| 450 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_2_Arguments_Formats_Property_Both_Arguments) |
| 451 |
{ |
| 452 |
// arrange |
| 453 |
CTestArgBufferMock testMock; |
| 454 |
UINT8 expectedArgument1Buffer[] = {0x2}; |
| 455 |
UINT8 actualArgument1Buffer[] = {0x3}; |
| 456 |
UINT8 expectedArgument2Buffer[] = {0x4, 0x5}; |
| 457 |
UINT8 actualArgument2Buffer[] = {0x5, 0x4}; |
| 458 |
|
| 459 |
STRICT_EXPECTED_CALL(testMock, Test2ArgsFunction(expectedArgument1Buffer, expectedArgument2Buffer)) |
| 460 |
.ValidateArgumentBuffer(1, expectedArgument1Buffer, sizeof(expectedArgument1Buffer)) |
| 461 |
.ValidateArgumentBuffer(2, expectedArgument2Buffer, sizeof(expectedArgument2Buffer)); |
| 462 |
|
| 463 |
// act |
| 464 |
testMock.Test2ArgsFunction(actualArgument1Buffer, actualArgument2Buffer); |
| 465 |
|
| 466 |
// assert |
| 467 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test2ArgsFunction([02],[04 05])][Actual:Test2ArgsFunction([03],[05 04])]")), testMock.CompareActualAndExpectedCalls(), |
| 468 |
_T("Expected and actual calls do not match")); |
| 469 |
} |
| 470 |
|
| 471 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_For_The_3rd_Byte_Issues_Two_Placeholders_In_The_Formatted_Argument) |
| 472 |
{ |
| 473 |
// arrange |
| 474 |
CTestArgBufferMock testMock; |
| 475 |
UINT8 expectedArgumentBuffer[] = {0x42}; |
| 476 |
UINT8 actualArgumentBuffer[] = {0x42, 0x43, 0x44}; |
| 477 |
|
| 478 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(expectedArgumentBuffer)) |
| 479 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer, 1, 2); |
| 480 |
|
| 481 |
// act |
| 482 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 483 |
|
| 484 |
// assert |
| 485 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([.. .. 42])][Actual:Test1ArgFunction([.. .. 44])]")), testMock.CompareActualAndExpectedCalls(), |
| 486 |
_T("Something is wrong with argument buffer validations")); |
| 487 |
} |
| 488 |
|
| 489 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_2_Validation_Entries_When_Expected_And_Actual_Values_Match_Does_Not_Yield_Any_Call_Difference) |
| 490 |
{ |
| 491 |
// arrange |
| 492 |
CTestArgBufferMock testMock; |
| 493 |
UINT8 expectedArgumentBuffer1[] = {0x42}; |
| 494 |
UINT8 expectedArgumentBuffer2[] = {0x43}; |
| 495 |
UINT8 actualArgumentBuffer[] = {0x42, 0x43}; |
| 496 |
|
| 497 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 498 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1)) |
| 499 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2), 1); |
| 500 |
|
| 501 |
// act |
| 502 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 503 |
|
| 504 |
// assert |
| 505 |
} |
| 506 |
|
| 507 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_2_Validation_Entries_When_First_Byte_Is_Different_Than_Expected_Yields_A_Call_Difference) |
| 508 |
{ |
| 509 |
// arrange |
| 510 |
CTestArgBufferMock testMock; |
| 511 |
UINT8 expectedArgumentBuffer1[] = {0x43}; |
| 512 |
UINT8 expectedArgumentBuffer2[] = {0x42}; |
| 513 |
UINT8 actualArgumentBuffer[] = {0x42, 0x42}; |
| 514 |
|
| 515 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 516 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1)) |
| 517 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2), 1); |
| 518 |
|
| 519 |
// act |
| 520 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 521 |
|
| 522 |
// assert |
| 523 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([43 42])][Actual:Test1ArgFunction([42 42])]")), testMock.CompareActualAndExpectedCalls(), |
| 524 |
_T("Something is wrong with argument buffer validations")); |
| 525 |
} |
| 526 |
|
| 527 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_With_2_Validation_Entries_When_Second_Byte_Is_Different_Than_Expected_Yields_A_Call_Difference) |
| 528 |
{ |
| 529 |
// arrange |
| 530 |
CTestArgBufferMock testMock; |
| 531 |
UINT8 expectedArgumentBuffer1[] = {0x42}; |
| 532 |
UINT8 expectedArgumentBuffer2[] = {0x43}; |
| 533 |
UINT8 actualArgumentBuffer[] = {0x42, 0x42}; |
| 534 |
|
| 535 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 536 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1)) |
| 537 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2), 1); |
| 538 |
|
| 539 |
// act |
| 540 |
testMock.Test1ArgFunction(actualArgumentBuffer); |
| 541 |
|
| 542 |
// assert |
| 543 |
ASSERT_ARE_EQUAL_WITH_MSG(tstring, tstring(_T("[Expected:Test1ArgFunction([42 43])][Actual:Test1ArgFunction([42 42])]")), testMock.CompareActualAndExpectedCalls(), |
| 544 |
_T("Something is wrong with argument buffer validations")); |
| 545 |
} |
| 546 |
|
| 547 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_2_Times_On_The_Same_Byte_With_Different_Content_Throws) |
| 548 |
{ |
| 549 |
// arrange |
| 550 |
CTestArgBufferMock testMock; |
| 551 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 552 |
|
| 553 |
UINT8 expectedArgumentBuffer1[] = {0x42}; |
| 554 |
UINT8 expectedArgumentBuffer2[] = {0x43}; |
| 555 |
bool exceptionThrown = false; |
| 556 |
|
| 557 |
try |
| 558 |
{ |
| 559 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 560 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1)) |
| 561 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2)); |
| 562 |
} |
| 563 |
catch (CMicroMockException e) |
| 564 |
{ |
| 565 |
exceptionThrown = (e.GetMicroMockExceptionCode() == MICROMOCK_EXCEPTION_INVALID_VALIDATE_BUFFERS); |
| 566 |
} |
| 567 |
|
| 568 |
// act |
| 569 |
|
| 570 |
// assert |
| 571 |
ASSERT_IS_TRUE_WITH_MSG(exceptionThrown, _T("ValidateArgumentBuffer should assert/throw")); |
| 572 |
} |
| 573 |
|
| 574 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_On_The_Same_Byte_With_Same_Byte_Content_Does_Not_Throws) |
| 575 |
{ |
| 576 |
// arrange |
| 577 |
CTestArgBufferMock testMock; |
| 578 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 579 |
|
| 580 |
UINT8 expectedArgumentBuffer1[] = {0x42}; |
| 581 |
UINT8 expectedArgumentBuffer2[] = {0x42}; |
| 582 |
|
| 583 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 584 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1)) |
| 585 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2)); |
| 586 |
|
| 587 |
// act |
| 588 |
|
| 589 |
// assert |
| 590 |
// no exception expected |
| 591 |
} |
| 592 |
|
| 593 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Overlapping_Validation_Buffers_Left_Side_The_Same_Throws_When_Bytes_Are_Different) |
| 594 |
{ |
| 595 |
// arrange |
| 596 |
CTestArgBufferMock testMock; |
| 597 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 598 |
|
| 599 |
UINT8 expectedArgumentBuffer1[] = {0x42}; |
| 600 |
UINT8 expectedArgumentBuffer2[] = {0x43, 0x42}; |
| 601 |
bool exceptionThrown = false; |
| 602 |
|
| 603 |
try |
| 604 |
{ |
| 605 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 606 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1), 1) |
| 607 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2), 1); |
| 608 |
} |
| 609 |
catch (CMicroMockException e) |
| 610 |
{ |
| 611 |
exceptionThrown = (e.GetMicroMockExceptionCode() == MICROMOCK_EXCEPTION_INVALID_VALIDATE_BUFFERS); |
| 612 |
} |
| 613 |
|
| 614 |
// act |
| 615 |
|
| 616 |
// assert |
| 617 |
ASSERT_IS_TRUE_WITH_MSG(exceptionThrown, _T("ValidateArgumentBuffer should assert/throw")); |
| 618 |
} |
| 619 |
|
| 620 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Overlapping_Validation_Buffers_Right_Side_The_Same_Throws_When_Bytes_Are_Different) |
| 621 |
{ |
| 622 |
// arrange |
| 623 |
CTestArgBufferMock testMock; |
| 624 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 625 |
|
| 626 |
UINT8 expectedArgumentBuffer1[] = {0x42}; |
| 627 |
UINT8 expectedArgumentBuffer2[] = {0x42, 0x43}; |
| 628 |
bool exceptionThrown = false; |
| 629 |
|
| 630 |
try |
| 631 |
{ |
| 632 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 633 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1), 1) |
| 634 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2), 0); |
| 635 |
} |
| 636 |
catch (CMicroMockException e) |
| 637 |
{ |
| 638 |
exceptionThrown = (e.GetMicroMockExceptionCode() == MICROMOCK_EXCEPTION_INVALID_VALIDATE_BUFFERS); |
| 639 |
} |
| 640 |
|
| 641 |
// act |
| 642 |
|
| 643 |
// assert |
| 644 |
ASSERT_IS_TRUE_WITH_MSG(exceptionThrown, _T("ValidateArgumentBuffer should assert/throw")); |
| 645 |
} |
| 646 |
|
| 647 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Overlapping_Validation_Buffer_Left_Side_The_Same_Does_Not_Thrown_When_Bytes_Are_The_Same) |
| 648 |
{ |
| 649 |
// arrange |
| 650 |
CTestArgBufferMock testMock; |
| 651 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 652 |
|
| 653 |
UINT8 expectedArgumentBuffer1[] = {0x42}; |
| 654 |
UINT8 expectedArgumentBuffer2[] = {0x42, 0x42}; |
| 655 |
|
| 656 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 657 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1), 1) |
| 658 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2), 1); |
| 659 |
|
| 660 |
// act |
| 661 |
|
| 662 |
// assert |
| 663 |
// no exception expected |
| 664 |
} |
| 665 |
|
| 666 |
TEST_FUNCTION(MicroMock_ValidateArgumentBuffer_Overlapping_Validation_Buffer_Right_Side_The_Same_Does_Not_Thrown_When_Bytes_Are_The_Same) |
| 667 |
{ |
| 668 |
// arrange |
| 669 |
CTestArgBufferMock testMock; |
| 670 |
testMock.SetPerformAutomaticCallComparison(AUTOMATIC_CALL_COMPARISON_OFF); |
| 671 |
|
| 672 |
UINT8 expectedArgumentBuffer1[] = {0x42}; |
| 673 |
UINT8 expectedArgumentBuffer2[] = {0x42, 0x42}; |
| 674 |
|
| 675 |
STRICT_EXPECTED_CALL(testMock, Test1ArgFunction(NULL)) |
| 676 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer1, sizeof(expectedArgumentBuffer1), 1) |
| 677 |
.ValidateArgumentBuffer(1, expectedArgumentBuffer2, sizeof(expectedArgumentBuffer2), 0); |
| 678 |
|
| 679 |
// act |
| 680 |
|
| 681 |
// assert |
| 682 |
// no exception expected |
| 683 |
} |
| 684 |
END_TEST_SUITE(MicroMockValidateArgumentBufferTests) |
| 685 |
|