diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e986b1204bfeee612867454ea81211e662c5c476..065e7df1cf5f6da4ccecd9511610104bf60c8cb6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,7 +10,7 @@ target_link_libraries(catch_main PUBLIC CONAN_PKG::catch2) target_link_libraries(catch_main PRIVATE project_options) add_executable(tests tests.cpp) -target_link_libraries(tests PRIVATE project_warnings project_options catch_main) +target_link_libraries(tests PRIVATE project_warnings project_options catch_main state game) # automatically discover tests that are defined in catch based test files you can modify the unittests. TEST_PREFIX to # whatever you want, or use different for different binaries diff --git a/test/tests.cpp b/test/tests.cpp index 9c40b2835013ac62ce609e530446371915c59095..a4b01d209d9828c69308fbfa4d09a008006e777e 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,14 +1,87 @@ #include <catch2/catch.hpp> +#include <string> -unsigned int Factorial(unsigned int number) +#include "state/state.h" +#include "state/shooter.h" +#include "game/game.h" +#include "state/level.h" + +TEST_CASE("Testing state history storing", "[state]") { - return number <= 1 ? number : Factorial(number - 1) * number; + state cs; + REQUIRE(cs.score == 0); + REQUIRE(cs.player.position.get() == 240); + + cs.score = 100; + cs.push(); + + REQUIRE(cs.score == 100); + + cs.player.position.set(120); + REQUIRE(cs.player.position.get() == 120); + cs.push(); + + cs.player.position.set(0); + REQUIRE(cs.player.position.get() == 0); + + cs = state::pop(); + + REQUIRE(cs.player.position.get() == 120); + REQUIRE(cs.score == 100); + + cs = state::pop(); + + REQUIRE(cs.player.position.get() == 240); + REQUIRE(cs.score == 100); + + try { + cs = state::pop(); + REQUIRE(false); + } catch (const std::exception&) { } } -TEST_CASE("Factorials are computed", "[factorial]") -{ - REQUIRE(Factorial(1) == 1); - REQUIRE(Factorial(2) == 2); - REQUIRE(Factorial(3) == 6); - REQUIRE(Factorial(10) == 3628800); +TEST_CASE("Testing ranged_value structure", "[ranged_value]") { + ranged_value<int> val(5, 0, 10); + + REQUIRE(val.get() == 5); + + val.set(3); + + REQUIRE(val.get() == 3); + + val.set(-5); + + REQUIRE(val.get() == 0); + + val.set(15); + + REQUIRE(val.get() == 10); +} + +TEST_CASE("Testing game initialization", "[game]") { + try { + game g(30); + REQUIRE(true); + } catch (const std::exception& e) { + std::string exception_string = e.what(); + REQUIRE(exception_string == ""); + } +} + +TEST_CASE("Testing level loading", "[level]") { + try { + level::create().from_file("levels/01.txt"); + level l1 = level::get("start"); + REQUIRE(true); + } catch (const std::exception& e) { + std::string exception_string = e.what(); + REQUIRE(exception_string == ""); + } + + try { + level l2 = level::get("gkfhdfgkdfghj"); + REQUIRE(false); + } catch (const std::exception&) { + REQUIRE(true); + } }