From 528761b180b217e0ccc4d7f4acf3a951ba3e4feb Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Mon, 2 Jan 2023 17:30:54 +0000 Subject: [PATCH] Unit test nullptr C-style strings Added unit tests for the cases of matching and serializing C-style strings (char*) that are nullptr. (As of this revision, these new tests correctly fail, except for the test of mock::equal; fix to follow.) --- test/test_constraints.cpp | 9 +++++++++ test/test_log.cpp | 8 ++++++++ test/test_matcher.cpp | 10 +++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/test_constraints.cpp b/test/test_constraints.cpp index 11c43fd..92157df 100644 --- a/test/test_constraints.cpp +++ b/test/test_constraints.cpp @@ -80,6 +80,15 @@ BOOST_AUTO_TEST_CASE(equal_constraint_deref) } } +BOOST_AUTO_TEST_CASE(equal_null_c_string) +{ + const char* const null_str = nullptr; + + BOOST_CHECK(mock::equal(null_str).c_(null_str)); + BOOST_CHECK(!mock::equal(null_str).c_("non-null string")); + BOOST_CHECK(!mock::equal("non-null-string").c_(null_str)); +} + BOOST_AUTO_TEST_CASE(same_constraint) { { diff --git a/test/test_log.cpp b/test/test_log.cpp index c26d3bb..4c321fe 100644 --- a/test/test_log.cpp +++ b/test/test_log.cpp @@ -89,6 +89,14 @@ BOOST_AUTO_TEST_CASE(strings_are_serialized_with_double_quotes) BOOST_CHECK_EQUAL("\"string\"", to_string(std::string("string"))); } +BOOST_AUTO_TEST_CASE(null_c_strings_are_serialized_to_nullptr) +{ + const char* const null_str = nullptr; + BOOST_CHECK_EQUAL("nullptr", to_string(null_str)); + // Note the lack of double quotes in the output, as opposed to when the + // string "nullptr" is serialized. +} + namespace { struct non_serializable {}; diff --git a/test/test_matcher.cpp b/test/test_matcher.cpp index cc98be0..f7292b8 100644 --- a/test/test_matcher.cpp +++ b/test/test_matcher.cpp @@ -36,7 +36,7 @@ BOOST_AUTO_TEST_CASE(ref_to_int_and_int_can_be_compared) namespace { struct fixture { - fixture() : text("same text"), actual(text.c_str()) + fixture() : text("same text"), actual(text.c_str()), null_str(nullptr) { const char* static_string = "same text"; BOOST_REQUIRE(actual != static_string); @@ -44,6 +44,7 @@ struct fixture } std::string text; const char* actual; + const char* null_str; }; } // namespace @@ -75,6 +76,13 @@ BOOST_FIXTURE_TEST_CASE(const_char_pointer_and_std_string_can_be_compared, fixtu BOOST_CHECK(!match(std::string("different text"), actual)); } +BOOST_FIXTURE_TEST_CASE(null_const_char_pointers_can_be_compared, fixture) +{ + BOOST_CHECK(match(null_str, null_str)); + BOOST_CHECK(!match(null_str, "non-null string")); + BOOST_CHECK(!match("non-null string", null_str)); +} + namespace { template std::string serialize(const T& t)