TestUtils.hpp 4.1 KB

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