From 6fecabcad5d53d455330634ee362fa33763a011d Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Tue, 3 Jan 2023 18:44:24 +0100 Subject: [PATCH] Bring back the fixture Without using a `std::string` the compiler may put the `const char*` C-Strings to the same memory address so our test might succeed when it should not. Add a short descriptive comment and check the property where it is used. --- test/test_matcher.cpp | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/test/test_matcher.cpp b/test/test_matcher.cpp index f772182..5f1746d 100644 --- a/test/test_matcher.cpp +++ b/test/test_matcher.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace { template @@ -33,35 +34,49 @@ BOOST_AUTO_TEST_CASE(ref_to_int_and_int_can_be_compared) BOOST_CHECK(!match(4, std::cref(i))); } -BOOST_AUTO_TEST_CASE(const_char_pointer_and_const_char_pointer_can_be_compared) +namespace { +struct fixture +{ + fixture() : text("same text") + { + // Get a pointer to unique memory containing "same text" + actual = text.c_str(); + } + const char* actual; + +private: + std::string text; +}; +} // namespace + +BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_const_char_pointer_can_be_compared, fixture) { const char* expected = "same text"; - const char* actual = "same text"; - BOOST_REQUIRE(expected != actual); // Different pointer values + BOOST_REQUIRE(expected != actual); + BOOST_REQUIRE(std::string(expected) == actual); + BOOST_CHECK(match(expected, actual)); const char* unexpected = "different text"; - BOOST_CHECK(!match(actual, unexpected)); + BOOST_CHECK(!match(unexpected, actual)); } -BOOST_AUTO_TEST_CASE(const_char_pointer_and_string_literal_can_be_compared) +BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_string_literal_can_be_compared, fixture) { - const char* actual = "same text"; BOOST_CHECK(match("same text", actual)); BOOST_CHECK(!match("different text", actual)); } -BOOST_AUTO_TEST_CASE(const_char_pointer_and_const_char_array_can_be_compared) +BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_const_char_array_can_be_compared, fixture) { - const char* actual = "same text"; const char expected[] = "same text"; + BOOST_REQUIRE(expected != actual); BOOST_CHECK(match(expected, actual)); const char unexpected[] = "different text"; BOOST_CHECK(!match(unexpected, actual)); } -BOOST_AUTO_TEST_CASE(const_char_pointer_and_std_string_can_be_compared) +BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_std_string_can_be_compared, fixture) { - 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));