| 1 | --- |
| 2 | :cmock: |
| 3 | :plugins: |
| 4 | - :callback |
| 5 | :treat_as: |
| 6 | custom_type: INT |
| 7 | |
| 8 | :systest: |
| 9 | :types: | |
| 10 | #define UINT32 unsigned int |
| 11 | |
| 12 | typedef signed int custom_type; |
| 13 | |
| 14 | :mockable: | |
| 15 | UINT32 foo(custom_type* a); |
| 16 | UINT32 bar(custom_type* b); |
| 17 | int baz(void); |
| 18 | void fuz(int* args, int num); |
| 19 | |
| 20 | :source: |
| 21 | :header: | |
| 22 | void function_a(int a, int b); |
| 23 | UINT32 function_b(void); |
| 24 | int function_c(void); |
| 25 | |
| 26 | :code: | |
| 27 | void function_a(int a, int b) |
| 28 | { |
| 29 | int args[6] = {0, 1, 2, 3, 5, 5}; |
| 30 | args[0] = a; |
| 31 | fuz(args, b); |
| 32 | } |
| 33 | |
| 34 | UINT32 function_b(void) |
| 35 | { |
| 36 | UINT32 sum = 0; |
| 37 | custom_type a = 0; |
| 38 | custom_type b = 0; |
| 39 | sum = foo(&a) + bar(&b); |
| 40 | return (UINT32)((custom_type)sum + a + b); |
| 41 | } |
| 42 | |
| 43 | int function_c(void) |
| 44 | { |
| 45 | return (baz() + baz() + baz()); |
| 46 | } |
| 47 | |
| 48 | :tests: |
| 49 | :common: | |
| 50 | void setUp(void) {} |
| 51 | void tearDown(void) {} |
| 52 | |
| 53 | UINT32 FooAndBarHelper(custom_type* data, int num) |
| 54 | { |
| 55 | num++; |
| 56 | *data = (custom_type)(num * 2); |
| 57 | return (UINT32)(*data * 2); |
| 58 | } |
| 59 | |
| 60 | int BazCallbackPointless(int num) |
| 61 | { |
| 62 | return num; |
| 63 | } |
| 64 | |
| 65 | int BazCallbackComplainsIfCalledMoreThanTwice(int num) |
| 66 | { |
| 67 | TEST_ASSERT_MESSAGE(num < 2, "Do Not Call Baz More Than Twice"); |
| 68 | return num; |
| 69 | } |
| 70 | |
| 71 | void FuzVerifier(int* args, int num_args, int num_calls) |
| 72 | { |
| 73 | int i; |
| 74 | TEST_ASSERT_MESSAGE(num_args < 5, "No More Than 5 Args Allowed"); |
| 75 | for (i = 0; i < num_args; i++) |
| 76 | { |
| 77 | TEST_ASSERT_EQUAL(num_calls + i, args[i]); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | :units: |
| 82 | - :pass: TRUE |
| 83 | :should: 'successfully exercise two simple ExpectAndReturn mock calls the normal way' |
| 84 | :code: | |
| 85 | test() |
| 86 | { |
| 87 | custom_type exp = 0; |
| 88 | foo_ExpectAndReturn(&exp, 10); |
| 89 | bar_ExpectAndReturn(&exp, 20); |
| 90 | TEST_ASSERT_EQUAL(30, function_b()); |
| 91 | } |
| 92 | |
| 93 | - :pass: FALSE |
| 94 | :should: 'successfully exercise two simple ExpectAndReturn mock calls and catch failure the normal way' |
| 95 | :code: | |
| 96 | test() |
| 97 | { |
| 98 | custom_type exp = 1; |
| 99 | foo_ExpectAndReturn(&exp, 10); |
| 100 | bar_ExpectAndReturn(&exp, 20); |
| 101 | TEST_ASSERT_EQUAL(30, function_b()); |
| 102 | } |
| 103 | |
| 104 | - :pass: TRUE |
| 105 | :should: 'successfully exercise using some basic callbacks' |
| 106 | :code: | |
| 107 | test() |
| 108 | { |
| 109 | foo_StubWithCallback((CMOCK_foo_CALLBACK)FooAndBarHelper); |
| 110 | bar_StubWithCallback((CMOCK_bar_CALLBACK)FooAndBarHelper); |
| 111 | TEST_ASSERT_EQUAL(12, function_b()); |
| 112 | } |
| 113 | |
| 114 | - :pass: TRUE |
| 115 | :should: 'successfully exercise using some basic callbacks even if there were expects' |
| 116 | :code: | |
| 117 | test() |
| 118 | { |
| 119 | custom_type exp = 500; |
| 120 | foo_ExpectAndReturn(&exp, 10); |
| 121 | foo_StubWithCallback((CMOCK_foo_CALLBACK)FooAndBarHelper); |
| 122 | bar_StubWithCallback((CMOCK_bar_CALLBACK)FooAndBarHelper); |
| 123 | TEST_ASSERT_EQUAL(12, function_b()); |
| 124 | } |
| 125 | |
| 126 | - :pass: FALSE |
| 127 | :should: 'successfully exercise using some basic callbacks and notice failures' |
| 128 | :code: | |
| 129 | test() |
| 130 | { |
| 131 | foo_StubWithCallback((CMOCK_foo_CALLBACK)FooAndBarHelper); |
| 132 | bar_StubWithCallback((CMOCK_bar_CALLBACK)FooAndBarHelper); |
| 133 | TEST_ASSERT_EQUAL(10, function_b()); |
| 134 | } |
| 135 | |
| 136 | - :pass: TRUE |
| 137 | :should: 'successfully exercise a callback with no arguments' |
| 138 | :code: | |
| 139 | test() |
| 140 | { |
| 141 | baz_StubWithCallback((CMOCK_baz_CALLBACK)BazCallbackPointless); |
| 142 | TEST_ASSERT_EQUAL(3, function_c()); |
| 143 | } |
| 144 | |
| 145 | - :pass: FALSE |
| 146 | :should: 'successfully throw a failure from within a callback function' |
| 147 | :code: | |
| 148 | test() |
| 149 | { |
| 150 | baz_StubWithCallback((CMOCK_baz_CALLBACK)BazCallbackComplainsIfCalledMoreThanTwice); |
| 151 | function_c(); |
| 152 | } |
| 153 | |
| 154 | - :pass: TRUE |
| 155 | :should: 'be usable for things like dynamically sized memory checking for passing conditions' |
| 156 | :code: | |
| 157 | test() |
| 158 | { |
| 159 | fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); |
| 160 | function_a(0, 4); |
| 161 | } |
| 162 | |
| 163 | - :pass: FALSE |
| 164 | :should: 'be usable for things like dynamically sized memory checking for failing conditions' |
| 165 | :code: | |
| 166 | test() |
| 167 | { |
| 168 | fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); |
| 169 | function_a(0, 5); |
| 170 | } |
| 171 | |
| 172 | - :pass: FALSE |
| 173 | :should: 'be usable for things like dynamically sized memory checking for failing conditions 2' |
| 174 | :code: | |
| 175 | test() |
| 176 | { |
| 177 | fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); |
| 178 | function_a(1, 4); |
| 179 | } |
| 180 | |
| 181 | - :pass: TRUE |
| 182 | :should: 'run them interlaced' |
| 183 | :code: | |
| 184 | test() |
| 185 | { |
| 186 | custom_type exp = 0; |
| 187 | foo_ExpectAndReturn(&exp, 10); |
| 188 | foo_ExpectAndReturn(&exp, 15); |
| 189 | bar_ExpectAndReturn(&exp, 20); |
| 190 | bar_ExpectAndReturn(&exp, 40); |
| 191 | fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); |
| 192 | baz_StubWithCallback((CMOCK_baz_CALLBACK)BazCallbackPointless); |
| 193 | |
| 194 | TEST_ASSERT_EQUAL(30, function_b()); |
| 195 | TEST_ASSERT_EQUAL(55, function_b()); |
| 196 | function_a(0, 4); |
| 197 | TEST_ASSERT_EQUAL(3, function_c()); |
| 198 | } |
| 199 | |
| 200 | - :pass: TRUE |
| 201 | :should: 'run them back to back' |
| 202 | :code: | |
| 203 | test() |
| 204 | { |
| 205 | custom_type exp = 0; |
| 206 | foo_ExpectAndReturn(&exp, 10); |
| 207 | bar_ExpectAndReturn(&exp, 20); |
| 208 | TEST_ASSERT_EQUAL(30, function_b()); |
| 209 | |
| 210 | foo_ExpectAndReturn(&exp, 15); |
| 211 | bar_ExpectAndReturn(&exp, 40); |
| 212 | TEST_ASSERT_EQUAL(55, function_b()); |
| 213 | |
| 214 | fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); |
| 215 | function_a(0, 4); |
| 216 | |
| 217 | baz_StubWithCallback((CMOCK_baz_CALLBACK)BazCallbackPointless); |
| 218 | TEST_ASSERT_EQUAL(3, function_c()); |
| 219 | } |
| 220 | |
| 221 | ... |