firmware  v0.1.2
Chromation Spectrometer Dev-Kit
test-boiler-plate.h
1 #include "FakeAvr/interrupt.h"
2 #include <unity.h>
3 #include <Mock.h>
4 #include "StatusCodes.h" // Python-to-Firmware communication status codes
5 #include "HardwareFake.h" // Fake hardware (registers, pins)
6 /* =====[ Test Helpers ]===== */
7 static void _SilentAssertCall(uint16_t num, char const * name)
8 {
10  // Put num and name in the message displayed if test fails
11  GString *message = g_string_new(NULL);
12  g_string_append_printf(message, "`%s` is not call %d", name, num);
13  // Perform the test
14  TEST_ASSERT_TRUE_MESSAGE(
15  SilentAssertCall(mock, num, name),
16  message->str
17  );
18  // Free memory used by GString
19  g_string_free(message, true);
20 }
21 static void _AssertCall(uint16_t num, char const * name)
22 {
24  // Put num and name in the message displayed if test fails
25  GString *message = g_string_new(NULL);
26  g_string_append_printf(message, "`%s` is not call %d", name, num);
27  // Perform the test
28  TEST_ASSERT_TRUE_MESSAGE(
29  AssertCall(mock, num, name),
30  message->str
31  );
32  // Free memory used by GString
33  g_string_free(message, true);
34 }
35 static void _AssertArg(uint16_t call_n, uint8_t arg_n, void *pval)
36 {
38 
75  GString *msg = g_string_new(NULL);
76  g_string_printf(msg, "Expect different value for call %d arg %d.", call_n, arg_n);
77  // I cannot print the expected value without asking the
78  // caller to include the type as a string. Better to keep the
79  // arg list short. Call and arg number are good enough.
80  TEST_ASSERT_TRUE_MESSAGE(
81  AssertArg(mock, call_n, arg_n, pval),
82  msg->str
83  );
84  g_string_free(msg, true);
85 }
86 static void _AssertArgByteVal(uint16_t call_n, uint8_t arg_n, uint8_t byte_val) { _AssertArg(call_n, arg_n, &byte_val); }
87 static void _test_call_count_is(uint16_t num)
88 {
89  // Print this message if test fails
90  // Note: msg is appended if I use a TEST_ASSERT_EQUAL macro,
91  // so use TEST_FAIL_MESSAGE macro instead.
92  GString *msg = g_string_new(NULL);
93  g_string_printf(msg,
94  "Expect call count is %d, but was %d.",
95  num, NumberOfActualCalls(mock)
96  );
97  // Run test
98  if (num == NumberOfActualCalls(mock)) TEST_PASS();
99  else TEST_FAIL_MESSAGE(msg->str);
100  // Free string-object memory
101  g_string_free(msg, true);
102 }
103 
104 
See StatusCode.h for context.