From 22abcdfa7be592e415a253aea568400d189d780a Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Tue, 15 Oct 2024 23:12:26 -0400 Subject: [PATCH] add test framework --- .github/workflows/ci.yml | 3 +-- .gitmodules | 4 +++ CMakeLists.txt | 19 +++++++++++++- tests/CMakeLists.txt | 53 ++++++++++++++++++++++++++++++++++++++++ third-party/googletest | 1 + 5 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 160000 third-party/googletest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7c045cc..a746424 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -156,10 +156,9 @@ jobs: - name: Run tests id: test - if: false working-directory: build/tests run: | - ./test_moonlight-xboxog --gtest_color=yes + ./test_moonlight --gtest_color=yes - name: Generate gcov report # any except canceled or skipped diff --git a/.gitmodules b/.gitmodules index 05645c4..57b4e9b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,7 @@ +[submodule "third-party/googletest"] + path = third-party/googletest + url = https://github.com/google/googletest.git + branch = main [submodule "third-party/moonlight-common-c"] path = third-party/moonlight-common-c url = https://github.com/moonlight-stream/moonlight-common-c.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 8dec9cd..489ec9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,11 @@ set(XBOX_XBE_DIR "${CMAKE_CURRENT_BINARY_DIR}/xbe") set(XBOX_ISO "${CMAKE_PROJECT_NAME}.iso") message(STATUS "XBOX_ISO: ${XBOX_ISO}") +# +# Options +# +option(BUILD_TESTS "Build tests" ON) + # create the xbe directory if it doesn't exist file(MAKE_DIRECTORY ${XBOX_XBE_DIR}) @@ -30,15 +35,27 @@ add_definitions(-DXBOX -DNXDK) set(MOONLIGHT_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp" ) +set(MOONLIGHT_EXTERNAL_LIBRARIES + "${NXDK_DIR}/lib/libpbkit.lib" +) add_executable(${CMAKE_PROJECT_NAME} ${MOONLIGHT_SOURCES} ) include_directories("${NXDK_DIR}/lib") -target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC ${NXDK_DIR}/lib/libpbkit.lib) +target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC + ${MOONLIGHT_EXTERNAL_LIBRARIES} +) target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE "") +# +# tests +# +if(BUILD_TESTS) + add_subdirectory(tests) +endif() + # Post-build exe to xbe conversion add_custom_target(cxbe_convert ALL VERBATIM COMMAND "${CMAKE_COMMAND}" -E env ./tools/cxbe/cxbe -OUT:${XBOX_XBE_DIR}/default.xbe -TITLE:${XBE_TITLE} ${CMAKE_CURRENT_BINARY_DIR}/${XBE_TITLE}.exe diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..899f4d3 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,53 @@ +cmake_minimum_required(VERSION 3.13) +# https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md#foundational-c-support + +project(test_moonlight) + +include_directories("${CMAKE_SOURCE_DIR}") + +enable_testing() + +# Add GoogleTest directory to the project +set(GTEST_SOURCE_DIR "${CMAKE_SOURCE_DIR}/third-party/googletest") +set(INSTALL_GTEST OFF) +set(INSTALL_GMOCK OFF) +add_subdirectory("${GTEST_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/googletest") +include_directories("${GTEST_SOURCE_DIR}/googletest/include" "${GTEST_SOURCE_DIR}") + +# coverage +# https://gcovr.com/en/stable/guide/compiling.html#compiler-options +set(CMAKE_CXX_FLAGS "-fprofile-arcs -ftest-coverage -ggdb -O0") +set(CMAKE_C_FLAGS "-fprofile-arcs -ftest-coverage -ggdb -O0") + +# if windows +if (WIN32) + # For Windows: Prevent overriding the parent project's compiler/linker settings + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # cmake-lint: disable=C0103 +endif () + +file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS + ${CMAKE_SOURCE_DIR}/tests/*.h + ${CMAKE_SOURCE_DIR}/tests/*.cpp) + +# remove main.cpp from the list of sources +# TODO +# list(REMOVE_ITEM MOONLIGHT_SOURCES ${CMAKE_SOURCE_DIR}/src/main.cpp) + +add_executable(${PROJECT_NAME} + ${TEST_SOURCES} + ${MOONLIGHT_SOURCES}) + +foreach(dep ${MOONLIGHT_TARGET_DEPENDENCIES}) + add_dependencies(${PROJECT_NAME} ${dep}) # compile these before Moonlight +endforeach() + +set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20) +target_link_libraries(${PROJECT_NAME} + ${MOONLIGHT_EXTERNAL_LIBRARIES} + gtest + gtest_main) # if we use this we don't need our own main function +target_compile_definitions(${PROJECT_NAME} PUBLIC ${MOONLIGHT_DEFINITIONS} ${TEST_DEFINITIONS}) +target_compile_options(${PROJECT_NAME} PRIVATE $<$:${MOONLIGHT_COMPILE_OPTIONS}>) +target_link_options(${PROJECT_NAME} PRIVATE) + +add_test(NAME ${PROJECT_NAME} COMMAND moonlight_test) diff --git a/third-party/googletest b/third-party/googletest new file mode 160000 index 0000000..b514bdc --- /dev/null +++ b/third-party/googletest @@ -0,0 +1 @@ +Subproject commit b514bdc898e2951020cbdca1304b75f5950d1f59