From f40d5ac21d7c376a1aa5c2890f9efddfbedbbabb Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sat, 23 Sep 2023 19:22:30 -0600 Subject: [PATCH] Add tests for `sf::Joystick` --- test/CMakeLists.txt | 1 + test/Window/Joystick.test.cpp | 76 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 test/Window/Joystick.test.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a5de41373..9322d61f0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -66,6 +66,7 @@ set(WINDOW_SRC Window/Cursor.test.cpp Window/Event.test.cpp Window/GlResource.test.cpp + Window/Joystick.test.cpp Window/Keyboard.test.cpp Window/VideoMode.test.cpp Window/Window.test.cpp diff --git a/test/Window/Joystick.test.cpp b/test/Window/Joystick.test.cpp new file mode 100644 index 000000000..0ebfeb926 --- /dev/null +++ b/test/Window/Joystick.test.cpp @@ -0,0 +1,76 @@ +#include + +#include +#include + +TEST_CASE("[Window] sf::Joystick") +{ + SECTION("Constants") + { + STATIC_CHECK(sf::Joystick::Count == 8); + STATIC_CHECK(sf::Joystick::ButtonCount == 32); + STATIC_CHECK(sf::Joystick::AxisCount == 8); + } + + SECTION("Identification") + { + const sf::Joystick::Identification identification; + CHECK(identification.name == "No Joystick"); + CHECK(identification.vendorId == 0); + CHECK(identification.productId == 0); + } + + // By avoiding calling sf::Joystick::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, sf::Joystick::Count - 1)); + + SECTION("isConnected()") + { + CHECK(!sf::Joystick::isConnected(joystick)); + } + + SECTION("getButtonCount()") + { + CHECK(sf::Joystick::getButtonCount(joystick) == 0); + } + + SECTION("hasAxis()") + { + CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::X)); + CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Y)); + CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::Z)); + CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::R)); + CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::U)); + CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::V)); + CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::PovX)); + CHECK(!sf::Joystick::hasAxis(joystick, sf::Joystick::PovY)); + } + + SECTION("isButtonPressed()") + { + const auto button = GENERATE(range(0u, sf::Joystick::ButtonCount - 1)); + CHECK(!sf::Joystick::isButtonPressed(joystick, button)); + } + + SECTION("getAxisPosition") + { + CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::X) == 0); + CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Y) == 0); + CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::Z) == 0); + CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::R) == 0); + CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::U) == 0); + CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::V) == 0); + CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::PovX) == 0); + CHECK(sf::Joystick::getAxisPosition(joystick, sf::Joystick::PovY) == 0); + } + + SECTION("getIdentification()") + { + const auto identification = sf::Joystick::getIdentification(joystick); + CHECK(identification.name == "No Joystick"); + CHECK(identification.vendorId == 0); + CHECK(identification.productId == 0); + } +}