18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 22:44:59 +01:00
Files
maszyna/tests/unit/CMakeLists.txt
jerrrrycho 5bac4e1cf1 Fix the four issues failing the Sonar quality gate on master (#323)
* fix(scripting): forward the object in push_object of the Lua screens (cpp:S5417)

push_object takes a forwarding reference and called std::move on it, which
SonarCloud reports as a blocker bug. It only worked because every caller
passes an rvalue: with an lvalue Type_ is deduced as a reference, and
new Type_(std::move(Object)) doesn't even compile.

The object is now forwarded, and the userdata is sized and built for
std::remove_cvref_t<Type_>, the type collect<> and check_object<> are used
with: an rvalue is moved in, an lvalue would be copied. Every caller passes
an rvalue of lua_image, bitmap_font or glyph, so they move the object in
exactly as before.

* fix(simulation): turbidity from the overcast without branches of the same value (cpp:S3923)

The chain of conditionals setting Global.fTurbidity had pairs of branches
with the same result (<= 0.10 and <= 0.20 both 4, and so on, the last two
both 9), which SonarCloud reports as a bug. The thresholds that changed
nothing are gone and the mapping is a small function: up to 0.20 gives 4,
up to 0.40 5, up to 0.60 6, up to 0.70 7, up to 0.80 8, anything else 9.
The comparisons are the same ones, in the same order, so every overcast
value gives the turbidity it gave before.

* test: script screen tests keep their files in the build folder, not in the temporary one (cpp:S5443)

The folders of the script screen host tests were made in the system's
temporary directory, and the sandbox test put its "outside.txt" straight
into that directory. SonarCloud reports both uses as vulnerabilities:
the directory is writable by everyone.

The folders are now made in the build folder of the unit tests, passed to
them as EU07_UNIT_TESTS_SCRATCH, the way EU07_UNIT_TESTS_DATA is. The
scripts are one level down in the folder of a test, so the file the sandbox
test puts outside the folder of the scripts is still inside the folder of
the test and goes with it. Nothing depends on the working directory, and
nothing is left behind.
2026-11-11 06:04:31 +01:00

56 lines
2.7 KiB
CMake

# Unit tests of the simulation, built and run without the game data: the test vehicles are JSON
# definitions in data/ (see vehicle.h), loaded from memory by the game's own vehicle loader. The code
# under test is the game's core library (eu07_core), the very objects the game links; the rest of the
# engine it reaches for is replaced with stubs (engine_stubs.cpp). They run as part of the build,
# from the IDE, or with ctest.
set(UNIT_TESTS_NAME eu07_unit_tests)
# a new test_*.cpp file or file in data/ (definitions, real vehicles, golden traces) is picked up on its own
file(GLOB UNIT_TEST_CASES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/test_*.cpp")
file(GLOB_RECURSE UNIT_TEST_DATA CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/data/*")
add_executable(${UNIT_TESTS_NAME}
main.cpp
vehicle.cpp
fiz.cpp
drive.cpp
consist.cpp
scenario.cpp
golden.cpp
invariants.cpp
figures.cpp
engine_stubs.cpp
${UNIT_TEST_CASES})
# the core library brings the game's build settings along
target_link_libraries(${UNIT_TESTS_NAME} PRIVATE eu07_core)
# the script screen host, tested with the real runtimes (test_script_screen_host.cpp)
target_sources(${UNIT_TESTS_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/scripting/scriptscreens.cpp")
if (WITH_LUA)
target_sources(${UNIT_TESTS_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/scripting/luaprogram.cpp" "${CMAKE_SOURCE_DIR}/stb/stb_image.c")
endif()
if (WITH_WASM)
target_sources(${UNIT_TESTS_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/scripting/wasmprogram.cpp")
target_link_libraries(${UNIT_TESTS_NAME} PRIVATE eu07_wamr)
endif()
target_include_directories(${UNIT_TESTS_NAME} PRIVATE "${DEPS_DIR}/doctest")
# the names the tests CAPTURE are often C strings, shown as text rather than pointers
target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_DATA="${CMAKE_CURRENT_SOURCE_DIR}/data" DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING)
# the folders of the tests that write files (the scripts of test_script_screen_host.cpp) are made in the build folder,
# and removed by the test that made them
target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_SCRATCH="${CMAKE_CURRENT_BINARY_DIR}")
if (MSVC)
target_compile_options(${UNIT_TESTS_NAME} PRIVATE /bigobj)
endif()
set_target_properties(${UNIT_TESTS_NAME} PROPERTIES FOLDER "tests")
add_test(NAME ${UNIT_TESTS_NAME} COMMAND ${UNIT_TESTS_NAME})
if (WIN32)
# the libraries of the game (lua51.dll) are next to the game, see run_tests.cmake
set_tests_properties(${UNIT_TESTS_NAME} PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:eu07>")
endif()
# for running the tests at the end of building the game, see the main CMakeLists.txt
set(UNIT_TEST_DATA ${UNIT_TEST_DATA} PARENT_SCOPE)