| 1 | --- |
| 2 | :cmock: |
| 3 | :plugins: |
| 4 | - :cexception |
| 5 | |
| 6 | :systest: |
| 7 | :types: | |
| 8 | #define UINT32 unsigned int |
| 9 | typedef signed int custom_type; |
| 10 | |
| 11 | :mockable: | |
| 12 | #include "CException.h" |
| 13 | UINT32 foo(custom_type a); |
| 14 | UINT32 bar(custom_type b); |
| 15 | UINT32 foo_varargs(custom_type a, ...); |
| 16 | |
| 17 | :source: |
| 18 | :header: | |
| 19 | #include "CException.h" |
| 20 | UINT32 function_a(int a); |
| 21 | void function_b(char a); |
| 22 | |
| 23 | :code: | |
| 24 | UINT32 function_a(int a) |
| 25 | { |
| 26 | UINT32 r = 0; |
| 27 | CEXCEPTION_T e; |
| 28 | |
| 29 | Try |
| 30 | { |
| 31 | r = (UINT32)foo((custom_type)a); |
| 32 | } |
| 33 | Catch(e) |
| 34 | { |
| 35 | r = (UINT32)e*2; |
| 36 | } |
| 37 | return r; |
| 38 | } |
| 39 | |
| 40 | void function_b(char a) |
| 41 | { |
| 42 | if (a) |
| 43 | { |
| 44 | Throw((CEXCEPTION_T)a); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | :tests: |
| 49 | :common: | |
| 50 | #include "CException.h" |
| 51 | void setUp(void) {} |
| 52 | void tearDown(void) {} |
| 53 | |
| 54 | :units: |
| 55 | - :pass: TRUE |
| 56 | :should: 'successfully exercise a simple ExpectAndReturn mock calls' |
| 57 | :code: | |
| 58 | test() |
| 59 | { |
| 60 | foo_ExpectAndReturn((custom_type)1, 10); |
| 61 | TEST_ASSERT_EQUAL(10, function_a(1)); |
| 62 | } |
| 63 | |
| 64 | - :pass: TRUE |
| 65 | :should: 'successfully throw an error on first call' |
| 66 | :code: | |
| 67 | test() |
| 68 | { |
| 69 | foo_ExpectAndThrow((custom_type)1, 55); |
| 70 | TEST_ASSERT_EQUAL(110, function_a(1)); |
| 71 | } |
| 72 | |
| 73 | - :pass: TRUE |
| 74 | :should: 'successfully throw an error on later calls' |
| 75 | :code: | |
| 76 | test() |
| 77 | { |
| 78 | foo_ExpectAndReturn((custom_type)1, 10); |
| 79 | foo_ExpectAndReturn((custom_type)2, 20); |
| 80 | foo_ExpectAndThrow((custom_type)3, 15); |
| 81 | foo_ExpectAndReturn((custom_type)4, 40); |
| 82 | TEST_ASSERT_EQUAL(10, function_a(1)); |
| 83 | TEST_ASSERT_EQUAL(20, function_a(2)); |
| 84 | TEST_ASSERT_EQUAL(30, function_a(3)); |
| 85 | TEST_ASSERT_EQUAL(40, function_a(4)); |
| 86 | } |
| 87 | |
| 88 | - :pass: TRUE |
| 89 | :should: 'pass because we nothing happens' |
| 90 | :code: | |
| 91 | test() |
| 92 | { |
| 93 | function_b(0); |
| 94 | } |
| 95 | |
| 96 | - :pass: FALSE |
| 97 | :should: 'fail because we did not expect function B to throw' |
| 98 | :code: | |
| 99 | test() |
| 100 | { |
| 101 | function_b(1); |
| 102 | } |
| 103 | |
| 104 | - :pass: TRUE |
| 105 | :should: 'fail because we expect function B to throw' |
| 106 | :code: | |
| 107 | test() |
| 108 | { |
| 109 | CEXCEPTION_T e; |
| 110 | Try |
| 111 | { |
| 112 | function_b(3); |
| 113 | TEST_FAIL_MESSAGE("Should Have Thrown"); |
| 114 | } |
| 115 | Catch(e) |
| 116 | { |
| 117 | TEST_ASSERT_EQUAL(3, e); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | - :pass: TRUE |
| 122 | :should: 'successfully throw an error on consecutive calls' |
| 123 | :code: | |
| 124 | test() |
| 125 | { |
| 126 | foo_ExpectAndReturn((custom_type)1, 10); |
| 127 | foo_ExpectAndReturn((custom_type)1, 20); |
| 128 | foo_ExpectAndThrow((custom_type)1, 15); |
| 129 | foo_ExpectAndThrow((custom_type)3, 40); |
| 130 | TEST_ASSERT_EQUAL(10, function_a(1)); |
| 131 | TEST_ASSERT_EQUAL(20, function_a(1)); |
| 132 | TEST_ASSERT_EQUAL(30, function_a(1)); |
| 133 | TEST_ASSERT_EQUAL(80, function_a(3)); |
| 134 | } |
| 135 | |
| 136 | - :pass: TRUE |
| 137 | :should: 'successfully throw an error on later calls and after a previous mock call' |
| 138 | :code: | |
| 139 | test() |
| 140 | { |
| 141 | foo_ExpectAndReturn((custom_type)1, 10); |
| 142 | foo_ExpectAndReturn((custom_type)1, 20); |
| 143 | foo_ExpectAndThrow((custom_type)1, 15); |
| 144 | TEST_ASSERT_EQUAL(10, function_a(1)); |
| 145 | TEST_ASSERT_EQUAL(20, function_a(1)); |
| 146 | TEST_ASSERT_EQUAL(30, function_a(1)); |
| 147 | |
| 148 | foo_ExpectAndReturn((custom_type)2, 20); |
| 149 | foo_ExpectAndThrow((custom_type)3, 40); |
| 150 | TEST_ASSERT_EQUAL(20, function_a(2)); |
| 151 | TEST_ASSERT_EQUAL(80, function_a(3)); |
| 152 | } |
| 153 | |
| 154 | - :pass: TRUE |
| 155 | :should: 'successfully throw an error if expects and mocks called before it' |
| 156 | :code: | |
| 157 | test() |
| 158 | { |
| 159 | foo_ExpectAndReturn((custom_type)1, 10); |
| 160 | foo_ExpectAndReturn((custom_type)1, 20); |
| 161 | TEST_ASSERT_EQUAL(10, function_a(1)); |
| 162 | TEST_ASSERT_EQUAL(20, function_a(1)); |
| 163 | |
| 164 | foo_ExpectAndReturn((custom_type)2, 20); |
| 165 | foo_ExpectAndThrow((custom_type)3, 40); |
| 166 | TEST_ASSERT_EQUAL(20, function_a(2)); |
| 167 | TEST_ASSERT_EQUAL(80, function_a(3)); |
| 168 | } |
| 169 | |
| 170 | ... |