| 1 | --- |
| 2 | :cmock: |
| 3 | :enforce_strict_ordering: 1 |
| 4 | :plugins: |
| 5 | - :ignore |
| 6 | - :cexception |
| 7 | |
| 8 | :systest: |
| 9 | :types: | |
| 10 | #define UINT32 unsigned int |
| 11 | |
| 12 | typedef signed int custom_type; |
| 13 | |
| 14 | :mockable: | |
| 15 | #include "CException.h" |
| 16 | UINT32 foo(custom_type a); |
| 17 | UINT32 bar(custom_type b); |
| 18 | void baz(custom_type c); |
| 19 | |
| 20 | :source: |
| 21 | :header: | |
| 22 | #include "CException.h" |
| 23 | UINT32 function_a(int a, int b); |
| 24 | void function_b(void); |
| 25 | void function_c(void); |
| 26 | void function_d(void); |
| 27 | |
| 28 | :code: | |
| 29 | UINT32 function_a(int a, int b) |
| 30 | { |
| 31 | return foo((custom_type)a) + bar((custom_type)b); |
| 32 | } |
| 33 | |
| 34 | void function_b(void) |
| 35 | { |
| 36 | baz((custom_type)1); |
| 37 | foo((custom_type)2); |
| 38 | bar((custom_type)3); |
| 39 | baz((custom_type)4); |
| 40 | foo((custom_type)5); |
| 41 | bar((custom_type)6); |
| 42 | baz((custom_type)7); |
| 43 | } |
| 44 | |
| 45 | void function_c(void) |
| 46 | { |
| 47 | foo((custom_type)1); |
| 48 | foo((custom_type)2); |
| 49 | bar((custom_type)3); |
| 50 | bar((custom_type)4); |
| 51 | foo((custom_type)5); |
| 52 | } |
| 53 | |
| 54 | void function_d(void) |
| 55 | { |
| 56 | CEXCEPTION_T e; |
| 57 | Try |
| 58 | { |
| 59 | foo((custom_type)1); |
| 60 | } |
| 61 | Catch(e) {} |
| 62 | Try |
| 63 | { |
| 64 | bar((custom_type)2); |
| 65 | } |
| 66 | Catch(e) {} |
| 67 | Try |
| 68 | { |
| 69 | foo((custom_type)3); |
| 70 | } |
| 71 | Catch(e) {} |
| 72 | } |
| 73 | |
| 74 | :tests: |
| 75 | :common: | |
| 76 | #include "CException.h" |
| 77 | void setUp(void) {} |
| 78 | void tearDown(void) {} |
| 79 | |
| 80 | :units: |
| 81 | - :pass: TRUE |
| 82 | :should: 'successfully exercise two simple ExpectAndReturn mock calls' |
| 83 | :code: | |
| 84 | test() |
| 85 | { |
| 86 | foo_ExpectAndReturn((custom_type)1, 10); |
| 87 | bar_ExpectAndReturn((custom_type)2, 20); |
| 88 | TEST_ASSERT_EQUAL(30, function_a(1, 2)); |
| 89 | } |
| 90 | |
| 91 | - :pass: FALSE |
| 92 | :should: 'fail because bar() is called but is not expected' |
| 93 | :verify_error: 'Called more times than expected' |
| 94 | :code: | |
| 95 | test() |
| 96 | { |
| 97 | foo_ExpectAndReturn((custom_type)1, 10); |
| 98 | TEST_ASSERT_EQUAL(30, function_a(1, 2)); |
| 99 | } |
| 100 | |
| 101 | - :pass: FALSE |
| 102 | :should: 'fail because bar() is called twice but is expected once' |
| 103 | :verify_error: 'Called fewer times than expected' |
| 104 | :code: | |
| 105 | test() |
| 106 | { |
| 107 | foo_ExpectAndReturn((custom_type)1, 10); |
| 108 | bar_ExpectAndReturn((custom_type)2, 20); |
| 109 | bar_ExpectAndReturn((custom_type)3, 30); |
| 110 | TEST_ASSERT_EQUAL(30, function_a(1, 2)); |
| 111 | } |
| 112 | |
| 113 | - :pass: FALSE |
| 114 | :should: 'fail because bar and foo called in reverse order' |
| 115 | :verify_error: 'Called earlier than expected' |
| 116 | :code: | |
| 117 | test() |
| 118 | { |
| 119 | bar_ExpectAndReturn((custom_type)2, 20); |
| 120 | foo_ExpectAndReturn((custom_type)1, 10); |
| 121 | TEST_ASSERT_EQUAL(30, function_a(1, 2)); |
| 122 | } |
| 123 | |
| 124 | - :pass: TRUE |
| 125 | :should: 'pass because bar and foo called in order with multiple params' |
| 126 | :code: | |
| 127 | test() |
| 128 | { |
| 129 | foo_ExpectAndReturn((custom_type)1, 10); |
| 130 | foo_ExpectAndReturn((custom_type)2, 10); |
| 131 | bar_ExpectAndReturn((custom_type)3, 20); |
| 132 | bar_ExpectAndReturn((custom_type)4, 10); |
| 133 | foo_ExpectAndReturn((custom_type)5, 10); |
| 134 | function_c(); |
| 135 | } |
| 136 | |
| 137 | - :pass: FALSE |
| 138 | :should: 'fail because bar and foo called out of order at end' |
| 139 | :code: | |
| 140 | test() |
| 141 | { |
| 142 | foo_ExpectAndReturn((custom_type)1, 10); |
| 143 | foo_ExpectAndReturn((custom_type)2, 10); |
| 144 | bar_ExpectAndReturn((custom_type)3, 20); |
| 145 | foo_ExpectAndReturn((custom_type)5, 10); |
| 146 | bar_ExpectAndReturn((custom_type)4, 10); |
| 147 | function_c(); |
| 148 | } |
| 149 | |
| 150 | - :pass: FALSE |
| 151 | :should: 'fail because bar and foo called out of order at start' |
| 152 | :code: | |
| 153 | test() |
| 154 | { |
| 155 | foo_ExpectAndReturn((custom_type)2, 10); |
| 156 | foo_ExpectAndReturn((custom_type)1, 10); |
| 157 | bar_ExpectAndReturn((custom_type)3, 20); |
| 158 | bar_ExpectAndReturn((custom_type)4, 10); |
| 159 | foo_ExpectAndReturn((custom_type)5, 10); |
| 160 | function_c(); |
| 161 | } |
| 162 | |
| 163 | - :pass: TRUE |
| 164 | :should: 'pass because we are properly ignoring baz' |
| 165 | :code: | |
| 166 | test() |
| 167 | { |
| 168 | baz_Ignore(); |
| 169 | foo_ExpectAndReturn((custom_type)2, 10); |
| 170 | bar_ExpectAndReturn((custom_type)3, 20); |
| 171 | foo_ExpectAndReturn((custom_type)5, 10); |
| 172 | bar_ExpectAndReturn((custom_type)6, 10); |
| 173 | function_b(); |
| 174 | } |
| 175 | |
| 176 | - :pass: FALSE |
| 177 | :should: 'fail because bar and foo out of order, even though baz is ignored' |
| 178 | :code: | |
| 179 | test() |
| 180 | { |
| 181 | baz_Ignore(); |
| 182 | foo_ExpectAndReturn((custom_type)2, 10); |
| 183 | foo_ExpectAndReturn((custom_type)5, 10); |
| 184 | bar_ExpectAndReturn((custom_type)3, 20); |
| 185 | bar_ExpectAndReturn((custom_type)6, 10); |
| 186 | function_b(); |
| 187 | } |
| 188 | |
| 189 | - :pass: TRUE |
| 190 | :should: 'pass when using cexception, as long as the order is right' |
| 191 | :code: | |
| 192 | test() |
| 193 | { |
| 194 | foo_ExpectAndThrow((custom_type)1, 10); |
| 195 | bar_ExpectAndReturn((custom_type)2, 20); |
| 196 | foo_ExpectAndReturn((custom_type)3, 10); |
| 197 | function_d(); |
| 198 | } |
| 199 | |
| 200 | - :pass: FALSE |
| 201 | :should: 'fail when an throw call is made out of order' |
| 202 | :code: | |
| 203 | test() |
| 204 | { |
| 205 | bar_ExpectAndReturn((custom_type)2, 20); |
| 206 | foo_ExpectAndThrow((custom_type)1, 10); |
| 207 | foo_ExpectAndReturn((custom_type)3, 10); |
| 208 | function_d(); |
| 209 | } |
| 210 | |
| 211 | - :pass: TRUE |
| 212 | :should: 'successfully handle back to back ExpectAndReturn setup and mock calls' |
| 213 | :code: | |
| 214 | test() |
| 215 | { |
| 216 | foo_ExpectAndReturn((custom_type)1, 10); |
| 217 | bar_ExpectAndReturn((custom_type)2, 20); |
| 218 | TEST_ASSERT_EQUAL(30, function_a(1, 2)); |
| 219 | |
| 220 | foo_ExpectAndReturn((custom_type)3, 30); |
| 221 | bar_ExpectAndReturn((custom_type)4, 40); |
| 222 | TEST_ASSERT_EQUAL(70, function_a(3, 4)); |
| 223 | |
| 224 | foo_ExpectAndReturn((custom_type)1, 50); |
| 225 | bar_ExpectAndReturn((custom_type)9, 60); |
| 226 | TEST_ASSERT_EQUAL(110, function_a(1, 9)); |
| 227 | } |
| 228 | |
| 229 | - :pass: FALSE |
| 230 | :should: 'successfully catch errors during back to back ExpectAndReturn setup and mock calls' |
| 231 | :verify_error: 'Called earlier than expected' |
| 232 | :code: | |
| 233 | test() |
| 234 | { |
| 235 | foo_ExpectAndReturn((custom_type)1, 10); |
| 236 | bar_ExpectAndReturn((custom_type)2, 20); |
| 237 | TEST_ASSERT_EQUAL(30, function_a(1, 2)); |
| 238 | |
| 239 | foo_ExpectAndReturn((custom_type)3, 30); |
| 240 | bar_ExpectAndReturn((custom_type)4, 40); |
| 241 | TEST_ASSERT_EQUAL(70, function_a(3, 4)); |
| 242 | |
| 243 | bar_ExpectAndReturn((custom_type)9, 60); |
| 244 | foo_ExpectAndReturn((custom_type)1, 50); |
| 245 | TEST_ASSERT_EQUAL(110, function_a(1, 9)); |
| 246 | } |
| 247 | ... |