firmware  v0.1.2
Chromation Spectrometer Dev-Kit
test_Mock.c
1 #include "test_Mock.h"
2 #include "ExampleCalls.h"
3 #include <unity.h>
4 #include <Mock.h>
5 
6 //=====[ List of tests: specifications of RanAsHoped ]=====
7  // [x] Pass if call lists are the same length and all values match.
8  // RanAsHoped returns true if:
9  // [x] call lists are the same length and all values match
10  // RanAsHoped returns false if:
11  // [x] there are more expected calls than actual calls
12  // [x] there are more actual calls than expected calls
13  // RanAsHoped returns false if a call has:
14  // [x] an expected call name that does not match the actual call name
15  // [x] more expected inputs than actual inputs
16  // [x] more actual inputs than expected inputs
17  // [x] an expected input value that does not match the actual input value
18  //
19  // WhyDidItFail reports
20  // [x] Unexpected calls
21  // [x] Calls made out of order
22  // [x] first unexpected actual call after exhausting a shorter list of expected calls
23  // [x] first missed call after exhausting a shorter list of actual calls
24  //
25 
26 /* =====[ New functionality I added 2019 October ]===== */
27 // Define what is recorded when the following unit tests fake recording an
28 // actual call.
29 static RecordedCall * Mock_TakesTwoArgs(uint8_t b1, uint8_t b2)
30 {
31  char const *call_name = "TakesTwoArgs";
32  RecordedCall *record_of_call = RecordedCall_new(call_name);
33  RecordedArg *record_of_arg1 = RecordedArg_new(SetupRecord_uint8_t);
34  *((uint8_t *)record_of_arg1->pArg) = b1;
35  RecordedArg *record_of_arg2 = RecordedArg_new(SetupRecord_uint8_t);
36  *((uint8_t *)record_of_arg2->pArg) = b2;
37  RecordArg(record_of_call, record_of_arg1);
38  RecordArg(record_of_call, record_of_arg2);
39  return record_of_call;
40 }
41 void SetUp_AssertCall(void){
42  mock = Mock_new();
43  //
44  uint8_t call1_arg1 = 11; uint8_t call1_arg2 = 12;
45  uint8_t call2_arg1 = 21; uint8_t call2_arg2 = 22;
46  RecordActualCall(mock, Mock_TakesTwoArgs(call1_arg1, call1_arg2));
47  RecordActualCall(mock, Mock_TakesTwoArgs(call2_arg1, call2_arg2));
48 }
49 void TearDown_AssertCall(void){
50  //
51  Mock_destroy(mock);
52  mock = NULL;
53 }
54 
55 void AssertCall_returns_false_if_call_number_exceeds_call_list(void){
56  /* =====[ Operate and Test ]===== */
57  TEST_ASSERT_FALSE(AssertCall(mock,100,"TakesTwoArgs"));
58 }
59 void AssertCall_returns_false_if_call_n_does_not_match_name(void)
60 {
61  /* =====[ Operate and Test ]===== */
62  TEST_ASSERT_FALSE(AssertCall(mock,1,"WrongCallName"));
63 }
64 void AssertCall_returns_true_if_call_n_matches_name(void)
65 {
66  /* =====[ Operate and Test ]===== */
67  TEST_ASSERT_TRUE(AssertCall(mock,1,"TakesTwoArgs"));
68 }
69 void AssertArg_returns_false_if_call_number_exceeds_call_list(void)
70 {
71  /* =====[ Operate and Test ]===== */
72  uint8_t arg_n = 1; uint8_t assert_val = 11;
73  TEST_ASSERT_FALSE(AssertArg(mock, 100, arg_n, &assert_val));
74 }
75 void AssertArg_returns_false_if_arg_number_exceeds_arg_list(void)
76 {
77  /* =====[ Operate and Test ]===== */
78  uint8_t call_n = 1; uint8_t assert_val = 11;
79  TEST_ASSERT_FALSE(AssertArg(mock, call_n, 100, &assert_val));
80 }
81 void AssertArg_returns_true_if_arg_1_in_call_1_matches_uint8_arg_value(void)
82 {
83  /* =====[ Operate and Test ]===== */
84  uint8_t call_n = 1; uint8_t arg_n = 1; uint8_t assert_val = 11;
85  TEST_ASSERT_TRUE(AssertArg(mock, call_n, arg_n, &assert_val));
86 }
87 void AssertArg_returns_true_if_arg_2_in_call_1_matches_uint8_arg_value(void)
88 {
89  /* =====[ Operate and Test ]===== */
90  uint8_t call_n = 1; uint8_t arg_n = 2; uint8_t assert_val = 12;
91  TEST_ASSERT_TRUE(AssertArg(mock, call_n, arg_n, &assert_val));
92 }
93 void AssertArg_returns_true_if_arg_1_in_call_2_matches_uint8_arg_value(void)
94 {
95  /* =====[ Operate and Test ]===== */
96  uint8_t call_n = 2; uint8_t arg_n = 1; uint8_t assert_val = 21;
97  TEST_ASSERT_TRUE(AssertArg(mock, call_n, arg_n, &assert_val));
98 }
99 void AssertArg_returns_true_if_arg_2_in_call_2_matches_uint8_arg_value(void)
100 {
101  /* =====[ Operate and Test ]===== */
102  uint8_t call_n = 2; uint8_t arg_n = 2; uint8_t assert_val = 22;
103  /* printf("\n"); */
104  TEST_ASSERT_TRUE(AssertArg(mock, call_n, arg_n, &assert_val));
105 }
106 void NumberOfActualCalls_returns_number_of_actual_calls_recorded_in_mock(void)
107 {
108  /* =====[ Operate and Test ]===== */
109  TEST_ASSERT_EQUAL_UINT8(2, NumberOfActualCalls(mock));
110 }
111 
112 void SetUp_libMock(void){
113  mock = Mock_new();
114  //
115 }
116 void TearDown_libMock(void){
117  //
118  Mock_destroy(mock);
119  mock = NULL;
120 }
121 
122 void RanAsHoped_returns_true_if_call_lists_match(void)
123 { // [x] call lists are the same length and all values match
124  Expect_TakesNoArg(); // test sets an expectation that a DOF is called
125  TakesNoArg_Stubbed(); // simulate the FUT calling the stubbed DOF
126  TEST_ASSERT_TRUE_MESSAGE(
127  RanAsHoped(mock), // If this is false,
128  WhyDidItFail(mock) // print this message.
129  );
130 }
131 void RanAsHoped_returns_false_if_more_expected_calls_than_actual_calls(void)
132 { // [x] there are more expected calls than actual calls
133  Expect_TakesNoArg(); // test sets an expectation that a DOF is called
134  // simulate the FUT missing the call to the stubbed DOF
135  TEST_ASSERT_FALSE(RanAsHoped(mock));
136 }
137 void RanAsHoped_returns_false_if_more_actual_calls_than_expected_calls(void)
138 { // [x] there are more actual calls than expected calls
139  SpanishInquisition_Stubbed(); // simulate the FUT making an unexpected call
140  TEST_ASSERT_FALSE(RanAsHoped(mock));
141 }
142 void RanAsHoped_returns_false_if_call_names_do_not_match(void)
143 { // [x] an expected call name that does not match the actual call name
144  Expect_TakesNoArg(); // test sets an expectation that TakesNoArg is called
145  SpanishInquisition_Stubbed(); // simulate the FUT calling something else
146  TEST_ASSERT_FALSE(RanAsHoped(mock));
147 }
148 void RanAsHoped_returns_false_if_a_call_expected_more_inputs(void)
149 { // [x] more expected inputs than actual inputs
150  Expect_TakesTwoArgs(0xa1, 0xa2); // test sets an expectation this DOF takes two args
151  TakesOneArg_StubbedWithCallName_TakesTwoArgs(0xa1);
152  TEST_ASSERT_FALSE(RanAsHoped(mock));
153 }
154 void RanAsHoped_returns_false_if_a_call_expected_less_inputs(void)
155 { // [x] more actual inputs than expected inputs
156  Expect_TakesOneArg(0xb1); // test sets an expectation this DOF takes one arg
157  TakesTwoArgs_StubbedWithCallName_TakesOneArg(0xb1, 0xb2);
158  TEST_ASSERT_FALSE(RanAsHoped(mock));
159 }
160 void RanAsHoped_returns_false_if_a_call_has_the_wrong_input_value(void)
161 { // [x] an expected input value that does not match the actual input value
162  Expect_TakesOneArg(0xAA); // test set an expectation this DOF receives 0xAA
163  TakesOneArg_Stubbed(0xFF); // simulate FUT calling DOF with 0xFF instead
164  TEST_ASSERT_FALSE(RanAsHoped(mock));
165 }
166 void WhyDidItFail_reports_unexpected_calls(void)
167 { // [x] Unexpected calls
168  Expect_TakesNoArg(); // test expects the FUT to call this DOF
169  SpanishInquisition_Stubbed(); // simulate the FUT making an unexpected call
170  TEST_ASSERT_FALSE(RanAsHoped(mock));
171  TEST_ASSERT_EQUAL_STRING(
172  " Call #1: expected 'TakesNoArg',"
173  " was 'SpanishInquisition'."
174  " ",
175  WhyDidItFail(mock)
176  );
177 }
178 void WhyDidItFail_reports_when_calls_are_out_of_order(void)
179 { // [x] Calls made out of order
180  Expect_TakesNoArg();
181  Expect_TakesOneArg(0x01);
182  Expect_TakesTwoArgs(0x02,0x03);
183  Expect_TakesOneArg(0xAA);
184  TakesNoArg_Stubbed(); // simulate the FUT calling the expected DOF
185  TakesTwoArgs_Stubbed(0x02,0x03);// simulate the FUT calling DOF out of order
186  TakesOneArg_Stubbed(0x01); // simulate the FUT calling DOF out of order
187  TakesOneArg_Stubbed(0xAA); // simulate the FUT calling the expected DOF
188  TEST_ASSERT_FALSE(RanAsHoped(mock));
189  TEST_ASSERT_EQUAL_STRING(
190  " Wrong number of args in call #2 'TakesOneArg',"
191  " expected 1, was 2."
192  " Wrong number of args in call #3 'TakesTwoArgs',"
193  " expected 2, was 1."
194  " Call #2: expected 'TakesOneArg',"
195  " was 'TakesTwoArgs'."
196  " Call #2: 'TakesOneArg' expected input '(uint8_t)0x01',"
197  " but was '(uint8_t)0x02'."
198  " Call #3: expected 'TakesTwoArgs',"
199  " was 'TakesOneArg'."
200  " Call #3: 'TakesTwoArgs' expected input '(uint8_t)0x02',"
201  " but was '(uint8_t)0x01'."
202  " ",
203  WhyDidItFail(mock)
204  );
205 }
206 void WhyDidItFail_reports_first_unexpected_call_after_last_expected_call(void)
207 { // [x] first unexpected actual call after exhausting a shorter list of expected calls
208  Expect_TakesNoArg(); // test expects FUT to call this DOF
209  Expect_TakesOneArg(0x01); // expect this is the last DOF the FUT calls
210  TakesNoArg_Stubbed(); // simulate the FUT calling the expected DOF
211  TakesOneArg_Stubbed(0x01); // simulate the FUT calling the expected DOF
212  TakesTwoArgs_Stubbed(0x02,0x03);// simulate the FUT calling an unexpected DOF
213  TEST_ASSERT_FALSE(RanAsHoped(mock));
214  TEST_ASSERT_EQUAL_STRING(
215  " Expected 2 calls, received 3 calls."
216  " First unexpected call: received #3:'TakesTwoArgs'."
217  " ",
218  WhyDidItFail(mock)
219  );
220 }
221 void WhyDidItFail_reports_first_missed_call_after_last_actual_call(void)
222 { // [x] first missed call after exhausting a shorter list of actual calls
223  Expect_TakesNoArg(); // test expects FUT to call this DOF
224  Expect_TakesOneArg(0x01); // test expects FUT to call this DOF
225  Expect_TakesTwoArgs(0x02,0x03); // expect this is the last DOF the FUT calls
226  TakesNoArg_Stubbed(); // simulate the FUT calling the expected DOF
227  TakesOneArg_Stubbed(0x01); // simulate this is the last DOF the FUT calls
228  TEST_ASSERT_FALSE(RanAsHoped(mock));
229  TEST_ASSERT_EQUAL_STRING(
230  " Expected 3 calls, received 2 calls."
231  " First missed call: expected #3:'TakesTwoArgs'."
232  " ",
233  WhyDidItFail(mock)
234  );
235 }
236