| 1 | #include <cgreen/boxed_double.h> |
| 2 | #include <cgreen/cgreen.h> |
| 3 | #include <cgreen/mocks.h> |
| 4 | #include <cgreen/unit.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #ifdef __cplusplus |
| 8 | using namespace cgreen; |
| 9 | #endif |
| 10 | |
| 11 | Describe(Mocks); |
| 12 | BeforeEach(Mocks) {} |
| 13 | AfterEach(Mocks) {} |
| 14 | |
| 15 | static int integer_out(void) { |
| 16 | return (int)mock(); |
| 17 | } |
| 18 | |
| 19 | Ensure(Mocks, default_return_value_when_no_presets_for_loose_mock) { |
| 20 | cgreen_mocks_are(loose_mocks); |
| 21 | assert_that(integer_out(), is_equal_to(0)); |
| 22 | } |
| 23 | |
| 24 | Ensure(Mocks, can_stub_an_integer_return) { |
| 25 | expect(integer_out, |
| 26 | will_return(3) |
| 27 | ); |
| 28 | |
| 29 | assert_that(integer_out(), is_equal_to(3)); |
| 30 | } |
| 31 | |
| 32 | Ensure(Mocks, repeats_return_value_when_set_to_always) { |
| 33 | always_expect(integer_out, |
| 34 | will_return(3) |
| 35 | ); |
| 36 | |
| 37 | assert_that(integer_out(), is_equal_to(3)); |
| 38 | assert_that(integer_out(), is_equal_to(3)); |
| 39 | } |
| 40 | |
| 41 | Ensure(Mocks, can_stub_an_integer_return_sequence) { |
| 42 | expect(integer_out, |
| 43 | will_return(1) |
| 44 | ); |
| 45 | |
| 46 | expect(integer_out, |
| 47 | will_return(2) |
| 48 | ); |
| 49 | |
| 50 | expect(integer_out, |
| 51 | will_return(3) |
| 52 | ); |
| 53 | |
| 54 | assert_that(integer_out(), is_equal_to(1)); |
| 55 | assert_that(integer_out(), is_equal_to(2)); |
| 56 | assert_that(integer_out(), is_equal_to(3)); |
| 57 | } |
| 58 | |
| 59 | Ensure(Mocks, expectations_are_reset_between_tests_with_loose_mocks) { |
| 60 | cgreen_mocks_are(loose_mocks); |
| 61 | assert_that(integer_out(), is_equal_to(0)); |
| 62 | } |
| 63 | |
| 64 | static char *string_out(int p1) { |
| 65 | return (char *)mock(p1); |
| 66 | } |
| 67 | |
| 68 | Ensure(Mocks, can_stub_a_string_return) { |
| 69 | expect(string_out, will_return("hello")); |
| 70 | assert_that(string_out(1), is_equal_to_string("hello")); |
| 71 | } |
| 72 | |
| 73 | Ensure(Mocks, can_stub_a_string_sequence) { |
| 74 | expect(string_out, will_return("hello")); |
| 75 | expect(string_out, will_return("goodbye")); |
| 76 | |
| 77 | assert_that(string_out(2), is_equal_to_string("hello")); |
| 78 | assert_that(string_out(3), is_equal_to_string("goodbye")); |
| 79 | } |
| 80 | |
| 81 | static void integer_in(int i) { |
| 82 | mock(i); |
| 83 | } |
| 84 | |
| 85 | Ensure(Mocks, expecting_once_with_any_parameters) { |
| 86 | expect(integer_in); |
| 87 | integer_in(3); |
| 88 | } |
| 89 | |
| 90 | Ensure(Mocks, expecting_once_with_parameter_checks_that_parameter) { |
| 91 | expect(integer_in, when(i, is_equal_to(3))); |
| 92 | integer_in(3); |
| 93 | } |
| 94 | |
| 95 | Ensure(Mocks, always_expect_keeps_affirming_parameter) { |
| 96 | always_expect(integer_in, when(i, is_equal_to(3))); |
| 97 | integer_in(3); |
| 98 | integer_in(3); |
| 99 | integer_in(3); |
| 100 | } |
| 101 | |
| 102 | Ensure(Mocks, expect_a_sequence) { |
| 103 | expect(integer_in, when(i, is_equal_to(1))); |
| 104 | expect(integer_in, when(i, is_equal_to(2))); |
| 105 | expect(integer_in, when(i, is_equal_to(3))); |
| 106 | integer_in(1); |
| 107 | integer_in(2); |
| 108 | integer_in(3); |
| 109 | } |
| 110 | |
| 111 | static void string_in(const char *s) { |
| 112 | mock(s); |
| 113 | } |
| 114 | |
| 115 | Ensure(Mocks, string_expect_is_confirmed) { |
| 116 | expect(string_in, when(s, is_equal_to_string("hello"))); |
| 117 | string_in("hello"); |
| 118 | } |
| 119 | |
| 120 | Ensure(Mocks, string_contains_expectation_is_confirmed) { |
| 121 | expect(string_in, when(s, contains_string("hello"))); |
| 122 | string_in("alice, hello"); |
| 123 | } |
| 124 | |
| 125 | Ensure(Mocks, string_expect_is_confirmed_even_when_null) { |
| 126 | expect(string_in, when(s, is_equal_to_string((char *)NULL))); |
| 127 | string_in(NULL); |
| 128 | } |
| 129 | |
| 130 | Ensure(Mocks, string_expect_sequence) { |
| 131 | expect(string_in, when(s, is_equal_to_string("hello"))); |
| 132 | expect(string_in, when(s, is_equal_to_string("goodbye"))); |
| 133 | string_in("hello"); |
| 134 | string_in("goodbye"); |
| 135 | } |
| 136 | |
| 137 | Ensure(Mocks, expecting_once_with_non_null_parameter_checks_that_parameter) { |
| 138 | expect(string_in, when(s, is_non_null)); |
| 139 | string_in("anything"); |
| 140 | } |
| 141 | |
| 142 | static void string_in2(const char *s2) { |
| 143 | mock(s2); |
| 144 | } |
| 145 | |
| 146 | Ensure(Mocks, expecting_twice_with_non_null_constraint_should_not_mix_them_up) { |
| 147 | expect(string_in, when(s, is_not_null)); |
| 148 | expect(string_in2, when(s2, is_not_null)); |
| 149 | string_in("anything"); |
| 150 | string_in2("anything"); |
| 151 | } |
| 152 | |
| 153 | Ensure(Mocks, expecting_twice_with_is_null_constraint_should_not_mix_them_up) { |
| 154 | expect(string_in, when(s, is_null)); |
| 155 | expect(string_in2, when(s2, is_null)); |
| 156 | string_in(NULL); |
| 157 | string_in2(NULL); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | static void bool_in(bool b) { |
| 162 | mock(b); |
| 163 | } |
| 164 | static void bool_in2(bool b2) { |
| 165 | mock(b2); |
| 166 | } |
| 167 | |
| 168 | Ensure(Mocks, expecting_twice_with_is_true_constraint_should_not_mix_them_up) { |
| 169 | expect(bool_in, when(b, is_true)); |
| 170 | expect(bool_in2, when(b2, is_true)); |
| 171 | bool_in(true); |
| 172 | bool_in2(true); |
| 173 | } |
| 174 | |
| 175 | Ensure(Mocks, expecting_twice_with_is_false_constraint_should_not_mix_them_up) { |
| 176 | expect(bool_in, when(b, is_false)); |
| 177 | expect(bool_in2, when(b2, is_false)); |
| 178 | bool_in(false); |
| 179 | bool_in2(false); |
| 180 | } |
| 181 | |
| 182 | static void double_in(double d) { |
| 183 | mock(box_double(d)); |
| 184 | } |
| 185 | |
| 186 | Ensure(Mocks, double_expect_is_confirmed) { |
| 187 | expect(double_in, when(d, is_equal_to_double(3.14))); |
| 188 | double_in(3.14); |
| 189 | } |
| 190 | |
| 191 | Ensure(Mocks, double_expect_sequence) { |
| 192 | expect(double_in, when(d, is_equal_to_double(1.0))); |
| 193 | expect(double_in, when(d, is_equal_to_double(2.0))); |
| 194 | double_in(1.0); |
| 195 | double_in(2.0); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* non-static to avoid "unused" warning in C++ as long as |
| 200 | 'can_always_return_double' does not work in C++ */ |
| 201 | double double_out(void) { |
| 202 | /* TODO: For v2 this should change to "return mock_double();" */ |
| 203 | return unbox_double(mock()); |
| 204 | } |
| 205 | |
| 206 | #ifndef __cplusplus |
| 207 | /* TODO: This does not work in C++ because of the overloading of |
| 208 | assert_that() which currently has no match for double */ |
| 209 | Ensure(Mocks, can_return_double) { |
| 210 | expect(double_out, will_return_double(4.23)); |
| 211 | assert_that_double(double_out(), is_equal_to_double(4.23)); |
| 212 | } |
| 213 | |
| 214 | Ensure(Mocks, can_always_return_double) { |
| 215 | always_expect(double_out, will_return_double(4.23)); |
| 216 | assert_that_double(double_out(), is_equal_to_double(4.23)); |
| 217 | assert_that_double(double_out(), is_equal_to_double(4.23)); |
| 218 | assert_that_double(double_out(), is_equal_to_double(4.23)); |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | |
| 223 | typedef struct { |
| 224 | int field; |
| 225 | } S; |
| 226 | |
| 227 | /* The mock function */ |
| 228 | S func(void) { |
| 229 | S s; |
| 230 | S *sp; |
| 231 | |
| 232 | sp = (S *)mock(); |
| 233 | |
| 234 | /* Copy content of struct pointed to by returned value */ |
| 235 | memcpy(&s, sp, sizeof(S)); |
| 236 | |
| 237 | /* Return it */ |
| 238 | return s; |
| 239 | } |
| 240 | |
| 241 | Ensure(Mocks, can_return_struct) { |
| 242 | /* Allocate a struct */ |
| 243 | S *sp = (S *)malloc(sizeof(S)); |
| 244 | |
| 245 | /* Prime one field */ |
| 246 | sp->field = 42; |
| 247 | |
| 248 | /* Expect a call and return a pointer to the struct */ |
| 249 | expect(func, will_return(sp)); |
| 250 | |
| 251 | /* Call our mock function which returns a struct */ |
| 252 | S ret = func(); |
| 253 | |
| 254 | /* And assert that the field in the returned struct is propagated */ |
| 255 | assert_that(ret.field, is_equal_to(42)); |
| 256 | |
| 257 | free(sp); |
| 258 | } |
| 259 | |
| 260 | static void mixed_parameters(int i, const char *s) { |
| 261 | mock(i, s); |
| 262 | } |
| 263 | |
| 264 | Ensure(Mocks, confirming_multiple_parameters_multiple_times) { |
| 265 | expect(mixed_parameters, |
| 266 | when(i, is_equal_to(1)), |
| 267 | when(s, is_equal_to_string("Hello")) |
| 268 | ); |
| 269 | expect(mixed_parameters, |
| 270 | when(i, is_equal_to(2)), |
| 271 | when(s, is_equal_to_string("Goodbye")) |
| 272 | ); |
| 273 | |
| 274 | mixed_parameters(1, "Hello"); |
| 275 | mixed_parameters(2, "Goodbye"); |
| 276 | } |
| 277 | |
| 278 | static int sample_mock(int i, const char *s) { |
| 279 | return (int)mock(i, s); |
| 280 | } |
| 281 | |
| 282 | Ensure(Mocks, can_mock_full_function_call) { |
| 283 | expect(sample_mock, |
| 284 | will_return(5), |
| 285 | when(i, is_equal_to(666)), |
| 286 | when(s, is_equal_to_string("devil")) |
| 287 | ); |
| 288 | |
| 289 | assert_that(sample_mock(666, "devil"), is_equal_to(5)); |
| 290 | } |
| 291 | |
| 292 | Ensure(Mocks, when_called_with_always_should_not_tally_counts) { |
| 293 | always_expect(string_out, |
| 294 | will_return(5), |
| 295 | when(i, is_equal_to(666)), |
| 296 | when(s, is_equal_to_string("devil")) |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | Ensure(Mocks, can_mock_full_sequence) { |
| 301 | expect(sample_mock, |
| 302 | will_return(5), |
| 303 | when(i, is_equal_to(666)), |
| 304 | when(s, is_equal_to_string("devil")) |
| 305 | ); |
| 306 | |
| 307 | expect(sample_mock, |
| 308 | will_return(6), |
| 309 | when(i, is_equal_to(667)), |
| 310 | when(s, is_equal_to_string("beastie")) |
| 311 | ); |
| 312 | |
| 313 | assert_that(sample_mock(666, "devil"), is_equal_to(5)); |
| 314 | assert_that(sample_mock(667, "beastie"), is_equal_to(6)); |
| 315 | } |
| 316 | |
| 317 | Ensure(Mocks, can_always_mock_full_function_call) { |
| 318 | always_expect(sample_mock, |
| 319 | will_return(5), |
| 320 | when(i, is_equal_to(666)), |
| 321 | when(s, is_equal_to_string("devil")) |
| 322 | ); |
| 323 | |
| 324 | assert_that(sample_mock(666, "devil"), is_equal_to(5)); |
| 325 | assert_that(sample_mock(666, "devil"), is_equal_to(5)); |
| 326 | assert_that(sample_mock(666, "devil"), is_equal_to(5)); |
| 327 | } |
| 328 | |
| 329 | static void sample_mock_with_parameters_without_space_between_them(int i, const char *s) { |
| 330 | mock(i,s); |
| 331 | } |
| 332 | |
| 333 | Ensure(Mocks, can_mock_full_function_call_when_there_is_no_space_between_parameters) { |
| 334 | expect(sample_mock_with_parameters_without_space_between_them, |
| 335 | when(i, is_equal_to(666)), |
| 336 | when(s, is_equal_to_string("devil")) |
| 337 | ); |
| 338 | |
| 339 | sample_mock_with_parameters_without_space_between_them(666, "devil"); |
| 340 | } |
| 341 | |
| 342 | typedef struct { |
| 343 | double a; |
| 344 | double b; |
| 345 | const char *name; |
| 346 | } LargerThanIntptr; |
| 347 | |
| 348 | static void out_param_mock(LargerThanIntptr* result) { |
| 349 | mock(result); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | Ensure(Mocks, can_stub_an_out_parameter) { |
| 354 | LargerThanIntptr actual = { 3.14, 6.66, "bob" }; |
| 355 | LargerThanIntptr local = { 4.13, 7.89, "alice" }; |
| 356 | |
| 357 | expect(out_param_mock, |
| 358 | will_set_contents_of_output_parameter(result, &actual, sizeof(LargerThanIntptr)) |
| 359 | ); |
| 360 | |
| 361 | out_param_mock(&local); |
| 362 | assert_that_double(actual.a, is_equal_to_double(local.a)); |
| 363 | assert_that_double(actual.b, is_equal_to_double(local.b)); |
| 364 | assert_that(actual.name, is_equal_to_string(local.name)); |
| 365 | assert_that(&local, is_equal_to_contents_of(&actual, sizeof(LargerThanIntptr))); |
| 366 | } |
| 367 | |
| 368 | static void mocked_read(char *ch) { |
| 369 | mock(ch); |
| 370 | } |
| 371 | |
| 372 | Ensure(Mocks, can_stub_a_char_out_parameter) { |
| 373 | char stubbed_char = 'a'; |
| 374 | char returned_char; |
| 375 | expect(mocked_read, |
| 376 | will_set_contents_of_output_parameter(ch, &stubbed_char, 1)); |
| 377 | mocked_read(&returned_char); |
| 378 | assert_that(returned_char, is_equal_to(stubbed_char)); |
| 379 | } |
| 380 | |
| 381 | static void int_out_mock(int *out) { |
| 382 | mock(out); |
| 383 | } |
| 384 | |
| 385 | /* Issue #337: the source value must be copied at expect() time, not |
| 386 | referenced at mock-call time. Here the source is a local in a helper |
| 387 | that has already returned -- and whose stack slot is reused by the |
| 388 | second call -- before the mock runs. */ |
| 389 | static void queue_int_output(int value) { |
| 390 | int local = value; |
| 391 | expect(int_out_mock, will_set_contents_of_output_parameter(out, &local, sizeof(local))); |
| 392 | } |
| 393 | |
| 394 | Ensure(Mocks, set_contents_copies_source_value_at_expect_time) { |
| 395 | queue_int_output(111); |
| 396 | queue_int_output(222); |
| 397 | |
| 398 | int first = 0, second = 0; |
| 399 | int_out_mock(&first); |
| 400 | int_out_mock(&second); |
| 401 | |
| 402 | assert_that(first, is_equal_to(111)); |
| 403 | assert_that(second, is_equal_to(222)); |
| 404 | } |
| 405 | |
| 406 | Ensure(Mocks, can_set_a_scalar_int_output_parameter_from_a_literal) { |
| 407 | int result = 0; |
| 408 | expect(int_out_mock, will_set_scalar_output_parameter(out, 42)); |
| 409 | int_out_mock(&result); |
| 410 | assert_that(result, is_equal_to(42)); |
| 411 | } |
| 412 | |
| 413 | static void short_out_mock(short *out) { |
| 414 | mock(out); |
| 415 | } |
| 416 | |
| 417 | Ensure(Mocks, can_set_a_scalar_short_output_parameter) { |
| 418 | short result = 0; |
| 419 | expect(short_out_mock, will_set_scalar_output_parameter(out, (short)0x1234)); |
| 420 | short_out_mock(&result); |
| 421 | assert_that(result, is_equal_to(0x1234)); |
| 422 | } |
| 423 | |
| 424 | static void long_long_out_mock(long long *out) { |
| 425 | mock(out); |
| 426 | } |
| 427 | |
| 428 | /* The width follows the value's own type via sizeof(value): a >32-bit value |
| 429 | through a long long out-parameter must survive whole (not truncated to int). */ |
| 430 | Ensure(Mocks, scalar_output_parameter_width_follows_the_value_type) { |
| 431 | long long result = 0; |
| 432 | expect(long_long_out_mock, will_set_scalar_output_parameter(out, 0x1122334455LL)); |
| 433 | long_long_out_mock(&result); |
| 434 | assert_that(result, is_equal_to(0x1122334455LL)); |
| 435 | } |
| 436 | |
| 437 | /* Issue #202: a mock called with a NULL out-parameter must not write into it. */ |
| 438 | Ensure(Mocks, set_contents_does_not_write_to_a_null_out_parameter) { |
| 439 | int value = 42; |
| 440 | expect(int_out_mock, will_set_contents_of_output_parameter(out, &value, sizeof(value))); |
| 441 | int_out_mock(NULL); /* used to segfault by writing into NULL */ |
| 442 | assert_that(true); /* reaching here means it did not crash */ |
| 443 | } |
| 444 | |
| 445 | // function which when mocked will be referred to by preprocessor macro |
| 446 | static void function_macro_mock(void) { |
| 447 | mock(); |
| 448 | } |
| 449 | |
| 450 | #define FUNCTION_MACRO function_macro_mock |
| 451 | |
| 452 | Ensure(Mocks, can_mock_a_function_macro) { |
| 453 | |
| 454 | // expect to mock by real function name |
| 455 | expect(function_macro_mock); |
| 456 | |
| 457 | function_macro_mock(); |
| 458 | |
| 459 | // expect to mock by macro function name |
| 460 | expect(FUNCTION_MACRO); |
| 461 | |
| 462 | FUNCTION_MACRO(); |
| 463 | } |
| 464 | |
| 465 | #undef FUNCTION_MACRO |
| 466 | |
| 467 | static void simple_mocked_function(int first, int second) { |
| 468 | mock(first, second); |
| 469 | } |
| 470 | |
| 471 | Ensure(Mocks, constraint_number_of_calls_when_no_when_is_present) { |
| 472 | expect(simple_mocked_function, times(2)); |
| 473 | simple_mocked_function(1, 2); |
| 474 | simple_mocked_function(1, 2); |
| 475 | } |
| 476 | |
| 477 | Ensure(Mocks, constraint_number_of_calls_when_is_present) { |
| 478 | expect(simple_mocked_function, when(first, is_equal_to(1)), times(2)); |
| 479 | simple_mocked_function(1, 2); |
| 480 | simple_mocked_function(1, 2); |
| 481 | } |
| 482 | |
| 483 | Ensure(Mocks, constraint_number_of_calls_when_multiple_expectations_are_present) { |
| 484 | expect(simple_mocked_function, when(first, is_equal_to(1)), times(2)); |
| 485 | expect(simple_mocked_function, when(first, is_equal_to(2)), times(1)); |
| 486 | simple_mocked_function(1, 2); |
| 487 | simple_mocked_function(1, 2); |
| 488 | simple_mocked_function(2, 2); |
| 489 | } |
| 490 | |
| 491 | Ensure(Mocks, constraint_number_of_calls_order_of_expectations_matter) { |
| 492 | expect(simple_mocked_function, when(first, is_equal_to(1)), times(1)); |
| 493 | expect(simple_mocked_function, when(first, is_equal_to(2)), times(1)); |
| 494 | expect(simple_mocked_function, when(first, is_equal_to(1)), times(1)); |
| 495 | simple_mocked_function(1, 2); |
| 496 | simple_mocked_function(2, 2); |
| 497 | simple_mocked_function(1, 2); |
| 498 | } |
| 499 | |
| 500 | Ensure(Mocks, can_capture_parameter) { |
| 501 | int captured_first = 0; |
| 502 | int captured_second = 0; |
| 503 | |
| 504 | expect(simple_mocked_function, |
| 505 | will_capture_parameter(first, captured_first), |
| 506 | will_capture_parameter(second, captured_second)); |
| 507 | simple_mocked_function(0x12345678, 0x76543210); |
| 508 | assert_that(captured_first, is_equal_to_hex(0x12345678)); |
| 509 | assert_that(captured_second, is_equal_to_hex(0x76543210)); |
| 510 | } |
| 511 | |
| 512 | static int changed_by_sideeffect = 1; |
| 513 | static int mock_with_side_effect(void) { |
| 514 | return (int)mock(); |
| 515 | } |
| 516 | static void the_sideeffect(void * data) { |
| 517 | assert_that(*(int*)data, is_equal_to(99)); |
| 518 | changed_by_sideeffect = 2; |
| 519 | } |
| 520 | |
| 521 | Ensure(Mocks, mock_expect_with_side_effect) { |
| 522 | int data_passed_to_sideeffect = 99; |
| 523 | expect(mock_with_side_effect, |
| 524 | with_side_effect(&the_sideeffect, &data_passed_to_sideeffect), |
| 525 | will_return(22)); |
| 526 | |
| 527 | assert_that(mock_with_side_effect(), is_equal_to(22)); |
| 528 | |
| 529 | assert_that(changed_by_sideeffect, is_equal_to(2)); |
| 530 | } |
| 531 | |
| 532 | |
| 533 | typedef struct Struct { |
| 534 | int height; |
| 535 | int weight; |
| 536 | } Struct; |
| 537 | |
| 538 | Struct retrieveStruct(void) { |
| 539 | Struct *struct_p = (Struct *)mock(); |
| 540 | Struct the_struct = *struct_p; |
| 541 | free(struct_p); |
| 542 | return the_struct; |
| 543 | } |
| 544 | |
| 545 | Ensure(Mocks, can_return_by_value) { |
| 546 | Struct someStruct = {.height = 10, .weight = 20}; |
| 547 | expect(retrieveStruct, will_return_by_value(someStruct, sizeof(Struct))); |
| 548 | someStruct.height = 30; |
| 549 | |
| 550 | Struct actualStruct = retrieveStruct(); |
| 551 | assert_that(actualStruct.weight, is_equal_to(20)); |
| 552 | assert_that(actualStruct.height, is_equal_to(10)); |
| 553 | } |
| 554 | |
| 555 | Ensure(Mocks, can_return_by_value_without_a_size) { |
| 556 | Struct someStruct = {.height = 10, .weight = 20}; |
| 557 | expect(retrieveStruct, will_return_by_value(someStruct)); /* size taken from sizeof(someStruct) */ |
| 558 | someStruct.height = 30; |
| 559 | |
| 560 | Struct actualStruct = retrieveStruct(); |
| 561 | assert_that(actualStruct.weight, is_equal_to(20)); |
| 562 | assert_that(actualStruct.height, is_equal_to(10)); |
| 563 | } |
| 564 | |
| 565 | Struct retrieveSpecialStruct(int structNumber) { |
| 566 | Struct *struct_p = (Struct *)mock(structNumber); |
| 567 | Struct the_struct = *struct_p; |
| 568 | free(struct_p); |
| 569 | return the_struct; |
| 570 | } |
| 571 | |
| 572 | Ensure(Mocks, can_return_by_value_depending_on_input_parameter) { |
| 573 | Struct struct1 = {.height = 10, .weight = 20}; |
| 574 | Struct struct2 = {.height = 5, .weight = 33}; |
| 575 | expect(retrieveSpecialStruct, will_return_by_value(struct1, sizeof(Struct)), |
| 576 | when(structNumber, is_equal_to(1))); |
| 577 | expect(retrieveSpecialStruct, will_return_by_value(struct2, sizeof(Struct)), |
| 578 | when(structNumber, is_equal_to(2))); |
| 579 | struct1.height = 30; |
| 580 | |
| 581 | Struct retrievedStruct1 = retrieveSpecialStruct(1); |
| 582 | assert_that(retrievedStruct1.weight, is_equal_to(20)); |
| 583 | assert_that(retrievedStruct1.height, is_equal_to(10)); |
| 584 | Struct retrievedStruct2 = retrieveSpecialStruct(2); |
| 585 | assert_that(retrievedStruct2.weight, is_equal_to(33)); |
| 586 | assert_that(retrievedStruct2.height, is_equal_to(5)); |
| 587 | } |
| 588 | |
| 589 | TestSuite *mock_tests(void) { |
| 590 | TestSuite *suite = create_test_suite(); |
| 591 | add_test_with_context(suite, Mocks, default_return_value_when_no_presets_for_loose_mock); |
| 592 | add_test_with_context(suite, Mocks, can_stub_an_integer_return); |
| 593 | add_test_with_context(suite, Mocks, repeats_return_value_when_set_to_always); |
| 594 | add_test_with_context(suite, Mocks, can_stub_an_integer_return_sequence); |
| 595 | add_test_with_context(suite, Mocks, expectations_are_reset_between_tests_with_loose_mocks); |
| 596 | add_test_with_context(suite, Mocks, can_stub_a_string_return); |
| 597 | add_test_with_context(suite, Mocks, can_stub_a_string_sequence); |
| 598 | add_test_with_context(suite, Mocks, expecting_once_with_any_parameters); |
| 599 | add_test_with_context(suite, Mocks, expecting_once_with_parameter_checks_that_parameter); |
| 600 | add_test_with_context(suite, Mocks, always_expect_keeps_affirming_parameter); |
| 601 | add_test_with_context(suite, Mocks, expect_a_sequence); |
| 602 | add_test_with_context(suite, Mocks, string_expect_is_confirmed); |
| 603 | add_test_with_context(suite, Mocks, string_expect_is_confirmed_even_when_null); |
| 604 | add_test_with_context(suite, Mocks, string_expect_sequence); |
| 605 | add_test_with_context(suite, Mocks, expecting_once_with_non_null_parameter_checks_that_parameter); |
| 606 | add_test_with_context(suite, Mocks, double_expect_is_confirmed); |
| 607 | add_test_with_context(suite, Mocks, double_expect_sequence); |
| 608 | add_test_with_context(suite, Mocks, confirming_multiple_parameters_multiple_times); |
| 609 | add_test_with_context(suite, Mocks, can_mock_full_function_call); |
| 610 | add_test_with_context(suite, Mocks, when_called_with_always_should_not_tally_counts); |
| 611 | add_test_with_context(suite, Mocks, can_mock_full_sequence); |
| 612 | add_test_with_context(suite, Mocks, can_always_mock_full_function_call); |
| 613 | add_test_with_context(suite, Mocks, can_mock_full_function_call_when_there_is_no_space_between_parameters); |
| 614 | add_test_with_context(suite, Mocks, can_stub_an_out_parameter); |
| 615 | add_test_with_context(suite, Mocks, set_contents_copies_source_value_at_expect_time); |
| 616 | add_test_with_context(suite, Mocks, can_set_a_scalar_int_output_parameter_from_a_literal); |
| 617 | add_test_with_context(suite, Mocks, can_set_a_scalar_short_output_parameter); |
| 618 | add_test_with_context(suite, Mocks, scalar_output_parameter_width_follows_the_value_type); |
| 619 | add_test_with_context(suite, Mocks, set_contents_does_not_write_to_a_null_out_parameter); |
| 620 | add_test_with_context(suite, Mocks, string_contains_expectation_is_confirmed); |
| 621 | add_test_with_context(suite, Mocks, can_mock_a_function_macro); |
| 622 | add_test_with_context(suite, Mocks, constraint_number_of_calls_when_no_when_is_present); |
| 623 | add_test_with_context(suite, Mocks, constraint_number_of_calls_when_is_present); |
| 624 | add_test_with_context(suite, Mocks, constraint_number_of_calls_when_multiple_expectations_are_present); |
| 625 | add_test_with_context(suite, Mocks, constraint_number_of_calls_order_of_expectations_matter); |
| 626 | add_test_with_context(suite, Mocks, mock_expect_with_side_effect); |
| 627 | add_test_with_context(suite, Mocks, can_return_by_value); |
| 628 | add_test_with_context(suite, Mocks, can_return_by_value_without_a_size); |
| 629 | add_test_with_context(suite, Mocks, can_return_by_value_depending_on_input_parameter); |
| 630 | |
| 631 | return suite; |
| 632 | } |