Skip to content

Commit

Permalink
tests: introduce helpers to reduce repetitiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
GwnDaan committed Dec 16, 2023
1 parent 97e6b36 commit 2b775ac
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 658 deletions.
66 changes: 1 addition & 65 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,71 +65,7 @@ if(BUILD_EXAMPLES)
endif()

if(BUILD_TESTING)
find_package(GTest QUIET)
if(NOT GTest_FOUND)
# Find GoogleTest
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1)

# For Windows: Prevent overriding the parent project's compiler/linker
# settings
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# For before CMAKE 3.20, GTest::gtest_main didn't exist, so add it in All
# linking to gtest should be done using the new GTest::gtest_main library
# for forward compatability
if(NOT TARGET GTest::gtest_main)
add_library(GTest::gtest_main ALIAS GTest::Main)
endif()
endif()

# For cmake<3.20 and older GTest
if(NOT TARGET GTest::gtest_main)
set_target_properties(GTest::Main PROPERTIES IMPORTED_GLOBAL TRUE)
add_library(GTest::gtest_main ALIAS GTest::Main)
endif()

# Set test source files
set(TEST_SRC
test/identifier_tests.cpp
test/diagnostic_protocol_tests.cpp
test/core_network_management_tests.cpp
test/virtual_can_plugin_tests.cpp
test/address_claim_tests.cpp
test/can_name_tests.cpp
test/hardware_interface_tests.cpp
test/vt_client_tests.cpp
test/language_command_interface_tests.cpp
test/tc_client_tests.cpp
test/ddop_tests.cpp
test/event_dispatcher_tests.cpp
test/isb_tests.cpp
test/cf_functionalities_tests.cpp
test/guidance_tests.cpp
test/speed_distance_message_tests.cpp
test/maintain_power_tests.cpp
test/vt_object_tests.cpp
test/nmea2000_message_tests.cpp)

add_executable(unit_tests ${TEST_SRC})
set_target_properties(
unit_tests
PROPERTIES CXX_STANDARD 11
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)
target_link_libraries(
unit_tests
PRIVATE GTest::gtest_main ${PROJECT_NAME}::Isobus
${PROJECT_NAME}::HardwareIntegration ${PROJECT_NAME}::Utility)

include(GoogleTest)
gtest_discover_tests(unit_tests name_tests identifier_tests)
add_subdirectory("test")
endif()

install(
Expand Down
5 changes: 5 additions & 0 deletions isobus/include/isobus/isobus/can_network_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ namespace isobus
/// @returns A list of all the partnered control functions
const std::list<std::shared_ptr<PartneredControlFunction>> &get_partnered_control_functions() const;

/// @brief Gets all the control functions that are known to the network manager
/// @param[in] includingOffline If true, all control functions are returned, otherwise only online control functions are returned
/// @returns A list of all the control functions
std::list<std::shared_ptr<ControlFunction>> get_control_functions(bool includingOffline) const;

/// @brief Returns the class instance of the NMEA2k fast packet protocol.
/// Use this to register for FP multipacket messages
/// @returns The class instance of the NMEA2k fast packet protocol.
Expand Down
23 changes: 23 additions & 0 deletions isobus/src/can_network_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,29 @@ namespace isobus
return partneredControlFunctions;
}

std::list<std::shared_ptr<ControlFunction>> isobus::CANNetworkManager::get_control_functions(bool includingOffline) const
{
std::list<std::shared_ptr<ControlFunction>> retVal;

for (std::uint8_t channelIndex = 0; channelIndex < CAN_PORT_MAXIMUM; channelIndex++)
{
for (std::uint8_t address = 0; address < NULL_CAN_ADDRESS; address++)
{
if (nullptr != controlFunctionTable[channelIndex][address])
{
retVal.push_back(controlFunctionTable[channelIndex][address]);
}
}
}

if (includingOffline)
{
retVal.insert(retVal.end(), inactiveControlFunctions.begin(), inactiveControlFunctions.end());
}

return retVal;
}

FastPacketProtocol &CANNetworkManager::get_fast_packet_protocol()
{
return fastPacketProtocol;
Expand Down
71 changes: 71 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
cmake_minimum_required(VERSION 3.16)

find_package(GTest QUIET)
if(NOT GTest_FOUND)
# Find GoogleTest
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1)

# For Windows: Prevent overriding the parent project's compiler/linker
# settings
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# For before CMAKE 3.20, GTest::gtest_main didn't exist, so add it in All
# linking to gtest should be done using the new GTest::gtest_main library for
# forward compatability
if(NOT TARGET GTest::gtest_main)
add_library(GTest::gtest_main ALIAS GTest::Main)
endif()
endif()

# For cmake<3.20 and older GTest
if(NOT TARGET GTest::gtest_main)
set_target_properties(GTest::Main PROPERTIES IMPORTED_GLOBAL TRUE)
add_library(GTest::gtest_main ALIAS GTest::Main)
endif()

# Set test include files
set(TEST_INCLUDE helpers/control_function_helpers.hpp)

# Set test source files
set(TEST_SRC
identifier_tests.cpp
diagnostic_protocol_tests.cpp
core_network_management_tests.cpp
virtual_can_plugin_tests.cpp
address_claim_tests.cpp
can_name_tests.cpp
hardware_interface_tests.cpp
vt_client_tests.cpp
language_command_interface_tests.cpp
tc_client_tests.cpp
ddop_tests.cpp
event_dispatcher_tests.cpp
isb_tests.cpp
cf_functionalities_tests.cpp
guidance_tests.cpp
speed_distance_message_tests.cpp
maintain_power_tests.cpp
vt_object_tests.cpp
nmea2000_message_tests.cpp
helpers/control_function_helpers.cpp)

add_executable(unit_tests ${TEST_SRC} ${TEST_INCLUDE})
set_target_properties(
unit_tests
PROPERTIES CXX_STANDARD 11
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON)
target_link_libraries(
unit_tests
PRIVATE GTest::gtest_main ${PROJECT_NAME}::Isobus
${PROJECT_NAME}::HardwareIntegration ${PROJECT_NAME}::Utility)

include(GoogleTest)
gtest_discover_tests(unit_tests name_tests identifier_tests)
24 changes: 7 additions & 17 deletions test/cf_functionalities_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "isobus/isobus/isobus_functionalities.hpp"
#include "isobus/utility/system_timing.hpp"

#include "helpers/control_function_helpers.hpp"

using namespace isobus;

class TestControlFunctionFunctionalities : public ControlFunctionFunctionalities
Expand All @@ -31,23 +33,8 @@ TEST(CONTROL_FUNCTION_FUNCTIONALITIES_TESTS, CFFunctionalitiesTest)
CANHardwareInterface::assign_can_channel_frame_handler(0, std::make_shared<VirtualCANPlugin>());
CANHardwareInterface::start();

NAME clientNAME(0);
clientNAME.set_industry_group(2);
clientNAME.set_function_instance(3);
clientNAME.set_function_code(static_cast<std::uint8_t>(NAME::Function::TirePressureControl));
auto internalECU = InternalControlFunction::create(clientNAME, 0x50, 0);

CANMessageFrame testFrame;

std::uint32_t waitingTimestamp_ms = SystemTiming::get_timestamp_ms();

while ((!internalECU->get_address_valid()) &&
(!SystemTiming::time_expired_ms(waitingTimestamp_ms, 2000)))
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}

ASSERT_TRUE(internalECU->get_address_valid());
auto internalECU = test_helpers::claim_internal_control_function(0x01, 0);
auto otherECU = test_helpers::force_claim_partnered_control_function(0x12, 0);

TestControlFunctionFunctionalities cfFunctionalitiesUnderTest(internalECU);

Expand Down Expand Up @@ -511,6 +498,7 @@ TEST(CONTROL_FUNCTION_FUNCTIONALITIES_TESTS, CFFunctionalitiesTest)
cfFunctionalitiesUnderTest.set_functionality_is_supported(ControlFunctionFunctionalities::Functionalities::TractorImplementManagementServer, 1, false);

// Get the virtual CAN plugin back to a known state
CANMessageFrame testFrame = {};
while (!requesterPlugin.get_queue_empty())
{
requesterPlugin.read_frame(testFrame);
Expand All @@ -519,6 +507,7 @@ TEST(CONTROL_FUNCTION_FUNCTIONALITIES_TESTS, CFFunctionalitiesTest)

// Simulate a request for the message
testFrame.identifier = 0x18EA50F7;
testFrame.identifier = test_helpers::create_extended_can_id(6, 0xEA00, otherECU, internalECU);
testFrame.data[0] = 0x8E;
testFrame.data[1] = 0xFC;
testFrame.data[2] = 0x00;
Expand Down Expand Up @@ -615,4 +604,5 @@ TEST(CONTROL_FUNCTION_FUNCTIONALITIES_TESTS, CFFunctionalitiesTest)

//! @todo try to reduce the reference count, such that that we don't use destroyed control functions later on
ASSERT_TRUE(internalECU->destroy(2));
ASSERT_TRUE(otherECU->destroy());
}
Loading

0 comments on commit 2b775ac

Please sign in to comment.