F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
TestUtils.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TestUtils.hpp
3 // \author vwong
4 // \brief hpp file for unit test utility macros
5 //
6 // \copyright
7 //
8 // Copyright (C) 2009-2020 California Institute of Technology.
9 //
10 // ALL RIGHTS RESERVED. United States Government Sponsorship
11 // acknowledged.
12 // ======================================================================
13 
14 #ifndef TESTUTILS_HPP
15 #define TESTUTILS_HPP
16 
17 // HOW TO USE:
18 //
19 // 1) in Tester.cpp, include this file
20 // e.g.: #include <Utils/TestUtils.hpp>
21 //
22 // 2) in Tester.cpp, set your component name in TEST_COMP macro
23 // e.g.: #define TEST_COMP PwrSwitchManagerComponentImpl
24 //
25 // 3) make sure INSTANCE and CMD_SEQ are also defined in Tester.cpp (they
26 // should be autogenerated)
27 //
28 // List of macros:
29 //
30 // - SEND_CMD(cmd, status, ...)
31 // - SEND_CMD_NO_EXPECT(cmd, ...)
32 // - ASSERT_LAST_CMD(cmd, status)
33 // - ASSERT_LAST_TLM(name, value)
34 // - ASSERT_LAST_EVENT(name, ...)
35 // - ASSERT_LAST_PORT_OUT(port, ...)
36 //
37 // See below for detailed descriptions
38 
39 // SEND_CMD
40 //
41 // Send a command and expect a response status. This command essentially calls
42 // sendCmd, doDispatch, and asserts a command response. The last command
43 // response received must be for the command sent here for it to validate, i.e.
44 // it may not work well if your component interleaves command responses.
45 //
46 // Example:
47 //
48 // SEND_CMD(PWR_SW_MGR_PWR_ON, Fw::CmdResponse::OK, channel);
49 // SEND_CMD(PWR_SW_MGR_SET_DUTY_CYCLE, Fw::CmdResponse::OK, channel, dutyCycle);
50 // SEND_CMD(PWR_SW_MGR_PWR_ON, Fw::COMMAND_EXECUTION_ERROR, illegalChannel);
51 //
52 #define SEND_CMD(cmd, status, ...) SEND_CMD_COMP(TEST_COMP, cmd, status, ##__VA_ARGS__)
53 
54 #define SEND_CMD_COMP(comp, cmd, status, ...) \
55  this->sendCmd_##cmd(INSTANCE, CMD_SEQ, ##__VA_ARGS__); \
56  this->component.doDispatch(); \
57  ASSERT_LAST_CMD(cmd, status);
58 
59 // SEND_CMD_NO_EXPECT
60 //
61 // Send a command and performs dispatch, without asserting any command response.
62 //
63 // Example:
64 //
65 // SEND_CMD_NO_EXPECT(FILE_DWN_SEND_APID, 100, 0, 0, 0);
66 // // ...
67 //
68 #define SEND_CMD_NO_EXPECT(cmd, ...) SEND_CMD_COMP_NO_EXPECT(TEST_COMP, cmd, ##__VA_ARGS__)
69 
70 #define SEND_CMD_COMP_NO_EXPECT(comp, cmd, ...) \
71  this->sendCmd_##cmd(INSTANCE, CMD_SEQ, ##__VA_ARGS__); \
72  this->component.doDispatch();
73 
74 // ASSERT_LAST_CMD
75 //
76 // Assert response status of command. This macro checks both that there was a
77 // response and that the response is as expected and is for the command
78 // specified.
79 //
80 // Example:
81 //
82 // SEND_CMD_NO_EXPECT(FILE_DWN_SEND_APID, 100, 0, 0, 0);
83 // // ...
84 // ASSERT_LAST_CMD(FILE_DWN_SEND_APID, Fw::CmdResponse::OK);
85 //
86 #define ASSERT_LAST_CMD(cmd, status) ASSERT_LAST_CMD_COMP(TEST_COMP, cmd, status)
87 
88 #define ASSERT_LAST_CMD_COMP(comp, cmd, status) \
89  ASSERT_GT(this->cmdResponseHistory->size(), 0); \
90  ASSERT_CMD_RESPONSE(this->cmdResponseHistory->size() - 1, comp::OPCODE_##cmd, CMD_SEQ, status);
91 
92 // ASSERT_LAST_TLM
93 //
94 // Assert the value last received in a given channel.
95 //
96 // Example:
97 //
98 // ASSERT_LAST_TLM(NeaCamManager_ImageDataSize, dataSize);
99 // ASSERT_LAST_TLM(NeaCamManager_PatternDataSize, 0);
100 //
101 #define ASSERT_LAST_TLM(name, value) \
102  ASSERT_GT(this->tlmHistory_##name->size(), 0); \
103  ASSERT_TLM_##name(this->tlmHistory_##name->size() - 1, value);
104 
105 // ASSERT_LAST_EVENT
106 //
107 // Assert the arguments in the last received EVR of a given name.
108 //
109 // Example:
110 //
111 // SEND_CMD(PWR_SW_MGR_SET_DUTY_CYCLE, Fw::COMMAND_VALIDATION_ERROR, 0, 0);
112 // ASSERT_LAST_EVENT(PwrSwitchManager_DutyCyclingNotEnabled, i);
113 //
114 #define ASSERT_LAST_EVENT(name, ...) \
115  ASSERT_GT(this->eventHistory_##name->size(), 0); \
116  ASSERT_EVENTS_##name(this->eventHistory_##name->size() - 1, ##__VA_ARGS__);
117 
118 // ASSERT_LAST_PORT_OUT
119 //
120 // Assert the arguments in the last output port call of a given port.
121 //
122 // Example:
123 //
124 // this->invoke_to_PingRecv(0, 0xDEADBEEF);
125 // this->component.doDispatch();
126 // ASSERT_LAST_PORT_OUT(PingResponse, 0, 0xDEADBEEF);
127 //
128 #define ASSERT_LAST_PORT_OUT(port, ...) \
129  ASSERT_GT(this->fromPortHistory_##port->size(), 0); \
130  ASSERT_from_##port(__VA_ARGS__);
131 
132 #endif