Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sfJoystick tests #245

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(CATCH_CONFIG_FAST_COMPILE ON CACHE BOOL "")
set(CATCH_CONFIG_NO_EXPERIMENTAL_STATIC_ANALYSIS_SUPPORT ON CACHE BOOL "")
FetchContent_Declare(Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.5.3
GIT_TAG v3.6.0
GIT_SHALLOW ON)
FetchContent_MakeAvailable(Catch2)
include(Catch)
Expand All @@ -30,7 +30,15 @@ target_link_libraries(test-csfml-system PRIVATE csfml-system Catch2::Catch2WithM
set_target_warnings(test-csfml-system)
catch_discover_tests(test-csfml-system)

add_executable(test-csfml-window
Window/Joystick.test.cpp
)
target_link_libraries(test-csfml-window PRIVATE csfml-window Catch2::Catch2WithMain)
set_target_warnings(test-csfml-window)
catch_discover_tests(test-csfml-window)

# Copy DLLs into the same directory
if(SFML_OS_WINDOWS AND NOT CSFML_LINK_SFML_STATICALLY)
add_custom_command(TARGET test-csfml-system PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:test-csfml-system> $<TARGET_FILE_DIR:test-csfml-system> COMMAND_EXPAND_LISTS)
add_custom_command(TARGET test-csfml-window PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:test-csfml-window> $<TARGET_FILE_DIR:test-csfml-window> COMMAND_EXPAND_LISTS)
endif()
68 changes: 68 additions & 0 deletions test/Window/Joystick.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <SFML/Window/Joystick.h>

#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators_range.hpp>

TEST_CASE("[Window] sfJoystick")
{
SECTION("Constants")
{
STATIC_CHECK(sfJoystickCount == 8);
STATIC_CHECK(sfJoystickButtonCount == 32);
STATIC_CHECK(sfJoystickAxisCount == 8);
}

// By avoiding calling sfJoystick_update() we can guarantee that
// no joysticks will be detected. This is how we can ensure these
// tests are portable and reliable.

const auto joystick = GENERATE(range(0u, static_cast<unsigned int>(sfJoystickCount - 1)));

SECTION("sfJoystick_isConnected()")
{
CHECK(!sfJoystick_isConnected(joystick));
}

SECTION("sfJoystick_getButtonCount()")
{
CHECK(sfJoystick_getButtonCount(joystick) == 0);
}

SECTION("sfJoystick_hasAxis()")
{
CHECK(!sfJoystick_hasAxis(joystick, sfJoystickX));
CHECK(!sfJoystick_hasAxis(joystick, sfJoystickY));
CHECK(!sfJoystick_hasAxis(joystick, sfJoystickZ));
CHECK(!sfJoystick_hasAxis(joystick, sfJoystickR));
CHECK(!sfJoystick_hasAxis(joystick, sfJoystickU));
CHECK(!sfJoystick_hasAxis(joystick, sfJoystickV));
CHECK(!sfJoystick_hasAxis(joystick, sfJoystickPovX));
CHECK(!sfJoystick_hasAxis(joystick, sfJoystickPovY));
}

SECTION("sfJoystick_isButtonPressed()")
{
const auto button = GENERATE(range(0u, static_cast<unsigned int>(sfJoystickButtonCount - 1)));
CHECK(!sfJoystick_isButtonPressed(joystick, button));
}

SECTION("sfJoystick_getAxisPosition")
{
CHECK(sfJoystick_getAxisPosition(joystick, sfJoystickX) == 0);
CHECK(sfJoystick_getAxisPosition(joystick, sfJoystickY) == 0);
CHECK(sfJoystick_getAxisPosition(joystick, sfJoystickZ) == 0);
CHECK(sfJoystick_getAxisPosition(joystick, sfJoystickR) == 0);
CHECK(sfJoystick_getAxisPosition(joystick, sfJoystickU) == 0);
CHECK(sfJoystick_getAxisPosition(joystick, sfJoystickV) == 0);
CHECK(sfJoystick_getAxisPosition(joystick, sfJoystickPovX) == 0);
CHECK(sfJoystick_getAxisPosition(joystick, sfJoystickPovY) == 0);
}

SECTION("getIdentification()")
{
const auto identification = sfJoystick_getIdentification(joystick);
CHECK(identification.name == std::string("No Joystick"));
CHECK(identification.vendorId == 0);
CHECK(identification.productId == 0);
}
}