fprime/Utils/TestUtils.hpp
2021-09-16 12:06:59 -07:00

138 lines
4.1 KiB
C++

// ======================================================================
// \title TestUtils.hpp
// \author vwong
// \brief hpp file for unit test utility macros
//
// \copyright
//
// Copyright (C) 2009-2020 California Institute of Technology.
//
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
// ======================================================================
#ifndef TESTUTILS_HPP
#define TESTUTILS_HPP
// HOW TO USE:
//
// 1) in Tester.cpp, include this file
// e.g.: #include <Utils/TestUtils.hpp>
//
// 2) in Tester.cpp, set your component name in TEST_COMP macro
// e.g.: #define TEST_COMP PwrSwitchManagerComponentImpl
//
// 3) make sure INSTANCE and CMD_SEQ are also defined in Tester.cpp (they
// should be autogenerated)
//
// List of macros:
//
// - SEND_CMD(cmd, status, ...)
// - SEND_CMD_NO_EXPECT(cmd, ...)
// - ASSERT_LAST_CMD(cmd, status)
// - ASSERT_LAST_TLM(name, value)
// - ASSERT_LAST_EVENT(name, ...)
// - ASSERT_LAST_PORT_OUT(port, ...)
//
// See below for detailed descriptions
// SEND_CMD
//
// Send a command and expect a response status. This command essentially calls
// sendCmd, doDispatch, and asserts a command response. The last command
// response received must be for the command sent here for it to validate, i.e.
// it may not work well if your component interleaves command responses.
//
// Example:
//
// SEND_CMD(PWR_SW_MGR_PWR_ON, Fw::CmdResponse::OK, channel);
// SEND_CMD(PWR_SW_MGR_SET_DUTY_CYCLE, Fw::CmdResponse::OK, channel, dutyCycle);
// SEND_CMD(PWR_SW_MGR_PWR_ON, Fw::COMMAND_EXECUTION_ERROR, illegalChannel);
//
#define SEND_CMD(cmd, status, ...) \
SEND_CMD_COMP(TEST_COMP, cmd, status, ## __VA_ARGS__)
#define SEND_CMD_COMP(comp, cmd, status, ...) \
this->sendCmd_ ## cmd(INSTANCE, CMD_SEQ, ## __VA_ARGS__); \
this->component.doDispatch(); \
ASSERT_LAST_CMD(cmd, status);
// SEND_CMD_NO_EXPECT
//
// Send a command and performs dispatch, without asserting any command response.
//
// Example:
//
// SEND_CMD_NO_EXPECT(FILE_DWN_SEND_APID, 100, 0, 0, 0);
// // ...
//
#define SEND_CMD_NO_EXPECT(cmd, ...) \
SEND_CMD_COMP_NO_EXPECT(TEST_COMP, cmd, ## __VA_ARGS__)
#define SEND_CMD_COMP_NO_EXPECT(comp, cmd, ...) \
this->sendCmd_ ## cmd(INSTANCE, CMD_SEQ, ## __VA_ARGS__); \
this->component.doDispatch();
// ASSERT_LAST_CMD
//
// Assert response status of command. This macro checks both that there was a
// response and that the response is as expected and is for the command
// specified.
//
// Example:
//
// SEND_CMD_NO_EXPECT(FILE_DWN_SEND_APID, 100, 0, 0, 0);
// // ...
// ASSERT_LAST_CMD(FILE_DWN_SEND_APID, Fw::CmdResponse::OK);
//
#define ASSERT_LAST_CMD(cmd, status) \
ASSERT_LAST_CMD_COMP(TEST_COMP, cmd, status)
#define ASSERT_LAST_CMD_COMP(comp, cmd, status) \
ASSERT_GT(this->cmdResponseHistory->size(), 0); \
ASSERT_CMD_RESPONSE(this->cmdResponseHistory->size()-1, comp::OPCODE_ ## cmd, CMD_SEQ, status);
// ASSERT_LAST_TLM
//
// Assert the value last received in a given channel.
//
// Example:
//
// ASSERT_LAST_TLM(NeaCamManager_ImageDataSize, dataSize);
// ASSERT_LAST_TLM(NeaCamManager_PatternDataSize, 0);
//
#define ASSERT_LAST_TLM(name, value) \
ASSERT_GT(this->tlmHistory_ ## name->size(), 0); \
ASSERT_TLM_ ## name(this->tlmHistory_ ## name->size()-1, value);
// ASSERT_LAST_EVENT
//
// Assert the arguments in the last received EVR of a given name.
//
// Example:
//
// SEND_CMD(PWR_SW_MGR_SET_DUTY_CYCLE, Fw::COMMAND_VALIDATION_ERROR, 0, 0);
// ASSERT_LAST_EVENT(PwrSwitchManager_DutyCyclingNotEnabled, i);
//
#define ASSERT_LAST_EVENT(name, ...) \
ASSERT_GT(this->eventHistory_ ## name->size(), 0); \
ASSERT_EVENTS_ ## name(this->eventHistory_ ## name->size()-1, ## __VA_ARGS__);
// ASSERT_LAST_PORT_OUT
//
// Assert the arguments in the last output port call of a given port.
//
// Example:
//
// this->invoke_to_PingRecv(0, 0xDEADBEEF);
// this->component.doDispatch();
// ASSERT_LAST_PORT_OUT(PingResponse, 0, 0xDEADBEEF);
//
#define ASSERT_LAST_PORT_OUT(port, ...) \
ASSERT_GT(this->fromPortHistory_ ## port->size(), 0); \
ASSERT_from_ ## port(__VA_ARGS__);
#endif