diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-25 16:20:51 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-25 16:20:51 +0200 |
commit | 9c868601591b60cf066eb576e85b0dbf4e1fd7ae (patch) | |
tree | c04cefa9297000df530865d0662da4a858527385 | |
parent | 696b97147c2e399be7a95cc5a983294eebc84ff0 (diff) |
test: replace gtest with Catch2 & Trompeloeil
-rw-r--r-- | lib/CMakeLists.txt | 16 | ||||
-rw-r--r-- | lib/catch2/CMakeLists.txt | 4 | ||||
-rw-r--r-- | lib/gtest/CMakeLists.txt | 4 | ||||
-rw-r--r-- | lib/trompeloeil/CMakeLists.txt | 4 | ||||
-rw-r--r-- | test/CMakeLists.txt | 14 | ||||
-rw-r--r-- | test/cell_helper_test.cpp | 281 | ||||
-rw-r--r-- | test/mocks/matrix.hpp | 22 |
7 files changed, 230 insertions, 115 deletions
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 6a5d90d..4e2b318 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -43,10 +43,18 @@ add_subdirectory(backward) if(test) FetchContent_Declare( - gtest - GIT_REPOSITORY "https://github.com/google/googletest" - GIT_TAG v1.11.0 + catch2 + GIT_REPOSITORY "https://github.com/catchorg/Catch2" + GIT_TAG v3.0.1 ) - add_subdirectory(gtest) + add_subdirectory(catch2) + + FetchContent_Declare( + trompeloeil + GIT_REPOSITORY "https://github.com/rollbear/trompeloeil" + GIT_TAG v42 + ) + + add_subdirectory(trompeloeil) endif() diff --git a/lib/catch2/CMakeLists.txt b/lib/catch2/CMakeLists.txt new file mode 100644 index 0000000..f9f75e4 --- /dev/null +++ b/lib/catch2/CMakeLists.txt @@ -0,0 +1,4 @@ +message(STATUS "Fetching Catch2...") + +FetchContent_MakeAvailable(catch2) + diff --git a/lib/gtest/CMakeLists.txt b/lib/gtest/CMakeLists.txt deleted file mode 100644 index f28b693..0000000 --- a/lib/gtest/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -message(STATUS "Fetching gtest...") - -FetchContent_MakeAvailable(gtest) - diff --git a/lib/trompeloeil/CMakeLists.txt b/lib/trompeloeil/CMakeLists.txt new file mode 100644 index 0000000..dc3f2e5 --- /dev/null +++ b/lib/trompeloeil/CMakeLists.txt @@ -0,0 +1,4 @@ +message(STATUS "Fetching Trompeloeil...") + +FetchContent_MakeAvailable(trompeloeil) + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0c74bf2..8a92723 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,7 +1,5 @@ project(tests CXX) -enable_testing() - file(GLOB SOURCES cell_helper_test.cpp ${CMAKE_SOURCE_DIR}/src/game/cell_helper.cpp @@ -18,14 +16,14 @@ target_compile_options( PRIVATE -Wall -Wextra -Wpedantic -Wshadow -Wold-style-cast -Wcast-align -Wno-unused - -Wconversion -Wcast-qual -Wctor-dtor-privacy + -Wconversion -Wcast-qual -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Woverloaded-virtual -Wredundant-decls -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -Werror - -pedantic -fsanitize=address -fno-exceptions + -pedantic -fsanitize=address ) target_include_directories( @@ -38,12 +36,10 @@ target_link_options(${PROJECT_NAME} PRIVATE -fsanitize=address) target_link_libraries( ${PROJECT_NAME} + PRIVATE GSL yacppdic - gtest_main - gmock + Catch2::Catch2WithMain + trompeloeil ) -include(GoogleTest) - -gtest_discover_tests(${PROJECT_NAME}) diff --git a/test/cell_helper_test.cpp b/test/cell_helper_test.cpp index b570aba..c9618fd 100644 --- a/test/cell_helper_test.cpp +++ b/test/cell_helper_test.cpp @@ -3,97 +3,204 @@ #include "mocks/matrix.hpp" -#include <gtest/gtest.h> +#include <catch2/catch_test_macros.hpp> +#include <catch2/matchers/catch_matchers_vector.hpp> +#include <trompeloeil.hpp> #include <list> #include <memory> +#include <vector> -class CellHelperTest : public testing::Test +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +TEST_CASE("CellHelper") { -protected: -}; + SECTION("can identify dying cell") + { + auto mock_matrix = MockMatrix<char>(); + + auto cell_helper = CellHelper<char>(mock_matrix); + + constexpr auto MATRIX_ROWS = 8U; + constexpr auto MATRIX_COLUMNS = 6U; + + const auto cell_pos = Vector2({.x = 3, .y = 5}); + + const auto cell_pos_up = cell_pos + Vector2::up(); + const auto cell_pos_down = cell_pos + Vector2::down(); + const auto cell_pos_left = cell_pos + Vector2::left(); + const auto cell_pos_right = cell_pos + Vector2::right(); + + const auto cell_pos_up_left = cell_pos + Vector2::up() + Vector2::left(); + const auto cell_pos_up_right = cell_pos + Vector2::up() + Vector2::right(); + + const auto cell_pos_down_left = cell_pos + Vector2::down() + Vector2::left(); + const auto cell_pos_down_right = cell_pos + Vector2::down() + Vector2::right(); + + // NOLINTNEXTLINE(readability-identifier-length) + REQUIRE_CALL(mock_matrix, get_row_cnt()).RETURN(MATRIX_ROWS).TIMES(AT_LEAST(1)); + + REQUIRE_CALL(mock_matrix, get_column_cnt()) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(MATRIX_COLUMNS) + .TIMES(AT_LEAST(1)); + + auto matrix_call_seq = trompeloeil::sequence(); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN('x') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_down)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN('x') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN('x') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN('x') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_down_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_down_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE(cell_helper.is_cell_dying(cell_pos) == true); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); -TEST_F(CellHelperTest, IsCellDying) -{ - const auto mock_matrix = MockMatrix<char>(); - - const auto rows = 8U; - const auto columns = 6U; - - const auto position = Vector2({.x = 3, .y = 5}); - - EXPECT_CALL(mock_matrix, get_row_cnt()).WillRepeatedly(testing::Return(rows)); - - EXPECT_CALL(mock_matrix, get_column_cnt()).WillRepeatedly(testing::Return(columns)); - - EXPECT_CALL(mock_matrix, get(position + Vector2::up())) - .WillOnce(testing::Return('x')) - .WillOnce(testing::Return(' ')) - .WillOnce(testing::Return(' ')); - - EXPECT_CALL(mock_matrix, get(position + Vector2::down())) - .WillRepeatedly(testing::Return(' ')); - - EXPECT_CALL(mock_matrix, get(position + Vector2::left())) - .WillOnce(testing::Return('x')) - .WillOnce(testing::Return('x')) - .WillOnce(testing::Return(' ')); - - EXPECT_CALL(mock_matrix, get(position + Vector2::right())) - .WillOnce(testing::Return('x')) - .WillOnce(testing::Return(' ')) - .WillOnce(testing::Return(' ')); - - EXPECT_CALL(mock_matrix, get(position + Vector2::up() + Vector2::left())) - .WillRepeatedly(testing::Return(' ')); - - EXPECT_CALL(mock_matrix, get(position + Vector2::up() + Vector2::right())) - .WillRepeatedly(testing::Return('x')); - - EXPECT_CALL(mock_matrix, get(position + Vector2::down() + Vector2::left())) - .WillRepeatedly(testing::Return(' ')); - - EXPECT_CALL(mock_matrix, get(position + Vector2::down() + Vector2::right())) - .WillRepeatedly(testing::Return(' ')); - - auto cell_helper = CellHelper<char>(mock_matrix); - - EXPECT_EQ(cell_helper.is_cell_dying(position), true); - - EXPECT_EQ(cell_helper.is_cell_dying(position), false); - - EXPECT_EQ(cell_helper.is_cell_dying(position), true); -} - -TEST_F(CellHelperTest, FindNeighbours) -{ - const auto mock_matrix = MockMatrix<char>(); - - const auto rows = 8U; - const auto columns = 6U; - - const auto position = Vector2({.x = 3, .y = 5}); - - EXPECT_CALL(mock_matrix, get_row_cnt()).WillOnce(testing::Return(rows)); - - EXPECT_CALL(mock_matrix, get_column_cnt()).WillOnce(testing::Return(columns)); - - auto cell_helper = CellHelper<char>(mock_matrix); - - const auto living_neighbour_cells = cell_helper.find_neighbours(position); - - EXPECT_EQ(living_neighbour_cells.size(), 8U); - - EXPECT_THAT( - living_neighbour_cells, - testing::ContainerEq(std::list<Vector2>( - {position + Vector2::up(), - position + Vector2::down(), - position + Vector2::left(), - position + Vector2::right(), - position + Vector2::up() + Vector2::left(), - position + Vector2::up() + Vector2::right(), - position + Vector2::down() + Vector2::left(), - position + Vector2::down() + Vector2::right()}))); + REQUIRE_CALL(mock_matrix, get(cell_pos_down)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN('x') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_down_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN('x') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_down_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE(cell_helper.is_cell_dying(cell_pos) == false); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_down)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN('x') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_up_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_down_left)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE_CALL(mock_matrix, get(cell_pos_down_right)) + // NOLINTNEXTLINE(readability-identifier-length) + .RETURN(' ') + .IN_SEQUENCE(matrix_call_seq); + + REQUIRE(cell_helper.is_cell_dying(cell_pos) == true); + } + + SECTION("can find neighbours") + { + auto mock_matrix = MockMatrix<char>(); + + auto cell_helper = CellHelper<char>(mock_matrix); + + constexpr auto MATRIX_ROWS = 12U; + constexpr auto MATRIX_COLUMNS = 9U; + + const auto cell_pos = Vector2({.x = 8, .y = 3}); + + // NOLINTNEXTLINE(readability-identifier-length) + REQUIRE_CALL(mock_matrix, get_row_cnt()).RETURN(MATRIX_ROWS); + + // NOLINTNEXTLINE(readability-identifier-length) + REQUIRE_CALL(mock_matrix, get_column_cnt()).RETURN(MATRIX_COLUMNS); + + const auto living_neighbour_cells = cell_helper.find_neighbours(cell_pos); + + REQUIRE(living_neighbour_cells.size() == 5U); + + REQUIRE_THAT( + living_neighbour_cells, + Catch::Matchers::Contains(std::vector<Vector2>( + {cell_pos + Vector2::up(), + cell_pos + Vector2::down(), + cell_pos + Vector2::left(), + cell_pos + Vector2::up() + Vector2::left(), + cell_pos + Vector2::down() + Vector2::left()}))); + } } - diff --git a/test/mocks/matrix.hpp b/test/mocks/matrix.hpp index 1602ebf..4b193b2 100644 --- a/test/mocks/matrix.hpp +++ b/test/mocks/matrix.hpp @@ -2,30 +2,30 @@ #include "interfaces/matrix.hpp" -#include <gmock/gmock.h> +#include <trompeloeil.hpp> template <typename Element> class MockMatrix : public IMatrix<Element> { public: - // NOLINTNEXTLINE(modernize-use-trailing-return-type) - MOCK_METHOD(void, fill, (Element element), (noexcept, override)); + // NOLINTNEXTLINE(modernize-use-trailing-return-type, readability-identifier-length) + MAKE_MOCK1(fill, void(Element element), noexcept override); - // NOLINTNEXTLINE(modernize-use-trailing-return-type) - MOCK_METHOD(Element, get, (const Vector2 &pos), (const, noexcept, override)); + // NOLINTNEXTLINE(modernize-use-trailing-return-type, readability-identifier-length) + MAKE_MOCK1(get, Element(const Vector2 &pos), const noexcept override); - // NOLINTNEXTLINE(modernize-use-trailing-return-type) - MOCK_METHOD(void, set, (const Vector2 &pos, Element element), (noexcept, override)); + // NOLINTNEXTLINE(modernize-use-trailing-return-type, readability-identifier-length) + MAKE_MOCK2(set, void(const Vector2 &pos, Element element), noexcept override); // NOLINTNEXTLINE(modernize-use-trailing-return-type) - MOCK_METHOD(uint32_t, get_row_cnt, (), (const, noexcept, override)); + MAKE_MOCK0(get_row_cnt, uint32_t(), const noexcept override); // NOLINTNEXTLINE(modernize-use-trailing-return-type) - MOCK_METHOD(uint32_t, get_column_cnt, (), (const, noexcept, override)); + MAKE_MOCK0(get_column_cnt, uint32_t(), const noexcept override); // NOLINTNEXTLINE(modernize-use-trailing-return-type) - MOCK_METHOD(MatrixIterator<Element>, begin, (), (const, noexcept, override)); + MAKE_MOCK0(begin, MatrixIterator<Element>(), const noexcept override); // NOLINTNEXTLINE(modernize-use-trailing-return-type) - MOCK_METHOD(MatrixIterator<Element>, end, (), (const, noexcept)); + MAKE_MOCK0(end, MatrixIterator<Element>(), const noexcept override); }; |