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.
This commit is contained in:
Alexander Grund 2023-01-03 18:44:24 +01:00
parent 700ceb9f4d
commit 6fecabcad5
No known key found for this signature in database
GPG key ID: AA48A0760367A42B

View file

@ -10,6 +10,7 @@
#include <turtle/matcher.hpp> #include <turtle/matcher.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <functional> #include <functional>
#include <string>
namespace { namespace {
template<typename Expected, typename Actual> template<typename Expected, typename Actual>
@ -33,35 +34,49 @@ BOOST_AUTO_TEST_CASE(ref_to_int_and_int_can_be_compared)
BOOST_CHECK(!match(4, std::cref(i))); 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* expected = "same text";
const char* actual = "same text"; BOOST_REQUIRE(expected != actual);
BOOST_REQUIRE(expected != actual); // Different pointer values BOOST_REQUIRE(std::string(expected) == actual);
BOOST_CHECK(match(expected, actual)); BOOST_CHECK(match(expected, actual));
const char* unexpected = "different text"; 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("same text", actual));
BOOST_CHECK(!match("different 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"; const char expected[] = "same text";
BOOST_REQUIRE(expected != actual);
BOOST_CHECK(match(expected, actual)); BOOST_CHECK(match(expected, actual));
const char unexpected[] = "different text"; const char unexpected[] = "different text";
BOOST_CHECK(!match(unexpected, actual)); 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(std::string("same text"), actual));
BOOST_CHECK(match(actual, std::string("same text"))); BOOST_CHECK(match(actual, std::string("same text")));
BOOST_CHECK(!match(std::string("different text"), actual)); BOOST_CHECK(!match(std::string("different text"), actual));