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 /test/cell_helper_test.cpp | |
parent | 696b97147c2e399be7a95cc5a983294eebc84ff0 (diff) |
test: replace gtest with Catch2 & Trompeloeil
Diffstat (limited to 'test/cell_helper_test.cpp')
-rw-r--r-- | test/cell_helper_test.cpp | 281 |
1 files changed, 194 insertions, 87 deletions
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()}))); + } } - |