From 700ceb9f4d2659e686f2d5be2a2d742cf08aadf2 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Tue, 3 Jan 2023 18:13:00 +0100 Subject: [PATCH] Remove fixture of tests in test_matcher The fixture is not really required and can be replaced by definitions in each test which even improves readability. Also the tests are partially redundant due to implicit conversions so add test in test_function for actual usage testing of string comparisons. --- test/detail/test_function.cpp | 50 +++++++++++++++++++++++++++++++++++ test/test_matcher.cpp | 33 +++++++++-------------- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/test/detail/test_function.cpp b/test/detail/test_function.cpp index bd640a2..59ed333 100644 --- a/test/detail/test_function.cpp +++ b/test/detail/test_function.cpp @@ -158,6 +158,56 @@ BOOST_FIXTURE_TEST_CASE(triggering_several_once_expectations_is_valid, mock_erro } } +BOOST_FIXTURE_TEST_CASE(string_likes_are_matched, mock_error_fixture) +{ + const char* c_string = "value"; + const char c_string2[] = "value"; + BOOST_REQUIRE(c_string != c_string2); // Different pointers + const std::string string = c_string; + { + mock::detail::function f; + + f.expect().once().with(string); + f(string); + BOOST_TEST(f.verify()); + + f.reset(); + f.expect().once().with(c_string); + f(string); + BOOST_TEST(f.verify()); + + f.reset(); + f.expect().once().with(c_string2); + f(string); + BOOST_TEST(f.verify()); + + CHECK_CALLS(3); + } + { + mock::detail::function f; + + f.expect().once().with(c_string); + f(c_string); + BOOST_TEST(f.verify()); + + f.reset(); + f.expect().once().with(c_string); + f(c_string2); + BOOST_TEST(f.verify()); + + f.reset(); + f.expect().once().with(c_string2); + f(c_string); + BOOST_TEST(f.verify()); + + f.reset(); + f.expect().once().with(string); + f(c_string); + BOOST_TEST(f.verify()); + CHECK_CALLS(4); + } +} + BOOST_FIXTURE_TEST_CASE(triggering_a_once_expectation_calls_unexpected_call_error_after_one_call, mock_error_fixture) { { diff --git a/test/test_matcher.cpp b/test/test_matcher.cpp index 547ecaa..f772182 100644 --- a/test/test_matcher.cpp +++ b/test/test_matcher.cpp @@ -33,46 +33,39 @@ BOOST_AUTO_TEST_CASE(ref_to_int_and_int_can_be_compared) BOOST_CHECK(!match(4, std::cref(i))); } -namespace { -struct fixture -{ - fixture() : text("same text"), actual(text.c_str()) - { - const char* static_string = "same text"; - BOOST_REQUIRE(actual != static_string); - BOOST_REQUIRE(actual == std::string(static_string)); - } - std::string text; - const char* actual; -}; -} // namespace - -BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_const_char_pointer_can_be_compared, fixture) +BOOST_AUTO_TEST_CASE(const_char_pointer_and_const_char_pointer_can_be_compared) { const char* expected = "same text"; + const char* actual = "same text"; + BOOST_REQUIRE(expected != actual); // Different pointer values BOOST_CHECK(match(expected, actual)); const char* unexpected = "different text"; BOOST_CHECK(!match(actual, unexpected)); } -BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_string_literal_can_be_compared, fixture) +BOOST_AUTO_TEST_CASE(const_char_pointer_and_string_literal_can_be_compared) { + const char* actual = "same text"; BOOST_CHECK(match("same text", actual)); BOOST_CHECK(!match("different text", actual)); } -BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_const_char_array_can_be_compared, fixture) +BOOST_AUTO_TEST_CASE(const_char_pointer_and_const_char_array_can_be_compared) { - const char expected[10] = "same text"; + const char* actual = "same text"; + const char expected[] = "same text"; BOOST_CHECK(match(expected, actual)); - const char unexpected[15] = "different text"; + const char unexpected[] = "different text"; BOOST_CHECK(!match(unexpected, actual)); } -BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_std_string_can_be_compared, fixture) +BOOST_AUTO_TEST_CASE(const_char_pointer_and_std_string_can_be_compared) { + const char* actual = "same text"; BOOST_CHECK(match(std::string("same text"), actual)); + BOOST_CHECK(match(actual, std::string("same text"))); BOOST_CHECK(!match(std::string("different text"), actual)); + BOOST_CHECK(!match(actual, std::string("different text"))); } BOOST_AUTO_TEST_CASE(null_const_char_pointers_can_be_compared)