| 1 | #include <cgreen/cgreen.h> |
| 2 | #include <cgreen/constraint.h> |
| 3 | #include <cgreen/constraint_syntax_helpers.h> |
| 4 | #include <cgreen/mocks.h> |
| 5 | #include <stdint.h> |
| 6 | #include <cgreen/cgreen_value.h> |
| 7 | |
| 8 | #ifdef __cplusplus |
| 9 | using namespace cgreen; |
| 10 | |
| 11 | namespace cgreen { |
| 12 | extern "C" { |
| 13 | extern CgreenValue make_cgreen_pointer_value(void *ptr); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | #else |
| 18 | extern CgreenValue make_cgreen_pointer_value(void *ptr); |
| 19 | #endif |
| 20 | |
| 21 | Describe(MockStruct); |
| 22 | BeforeEach(MockStruct) {} |
| 23 | AfterEach(MockStruct) {} |
| 24 | |
| 25 | typedef struct { |
| 26 | int i; |
| 27 | const char *string; |
| 28 | } Struct; |
| 29 | |
| 30 | |
| 31 | /* If you are only interested in a single, or few, fields: */ |
| 32 | void function_mocking_field(Struct s) { |
| 33 | mock(s.i); |
| 34 | } |
| 35 | |
| 36 | Ensure(MockStruct, can_mock_a_struct_parameters_field) { |
| 37 | Struct struct_to_send = { .i = 12, .string = "hello" }; |
| 38 | |
| 39 | expect(function_mocking_field, when(s.i, is_equal_to(12))); |
| 40 | |
| 41 | function_mocking_field(struct_to_send); |
| 42 | } |
| 43 | |
| 44 | void *cgreen_memdup(void *s, size_t size) { |
| 45 | void *p = malloc(size); |
| 46 | memcpy(p, s, size); |
| 47 | return p; |
| 48 | } |
| 49 | |
| 50 | #define memdup(s) cgreen_memdup(&s, sizeof(s)) |
| 51 | |
| 52 | /* If you need to get the whole struct: */ |
| 53 | void function_mocking_the_whole_struct(Struct s) { |
| 54 | Struct *sP = (Struct *)memdup(s); |
| 55 | mock(sP); |
| 56 | } |
| 57 | |
| 58 | Ensure(MockStruct, can_mock_a_struct_parameter) { |
| 59 | Struct struct_to_send = { .i = 13, .string = "hello" }; |
| 60 | Struct *p; |
| 61 | |
| 62 | expect(function_mocking_the_whole_struct, will_capture_parameter(sP, p)); |
| 63 | |
| 64 | function_mocking_the_whole_struct(struct_to_send); |
| 65 | |
| 66 | assert_that(p->i, is_equal_to(13)); |
| 67 | } |