From 700ceb9f4d2659e686f2d5be2a2d742cf08aadf2 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Tue, 3 Jan 2023 18:13:00 +0100 Subject: [PATCH 1/4] 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) From 6fecabcad5d53d455330634ee362fa33763a011d Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Tue, 3 Jan 2023 18:44:24 +0100 Subject: [PATCH 2/4] 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)); From 9f5a8131aed466598654db25f9a31fdda93569be Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Tue, 3 Jan 2023 23:00:20 +0100 Subject: [PATCH 3/4] Download docbook DTD/XSL --- appveyor.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/appveyor.yml b/appveyor.yml index 40f4560..eb4515a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -33,6 +33,10 @@ environment: CMAKE: true install: + - appveyor-retry powershell Invoke-WebRequest http://www.oasis-open.org/docbook/xml/4.2/docbook-xml-4.2.zip -OutFile docbook-xml.zip + - appveyor-retry powershell Invoke-WebRequest https://github.com/docbook/xslt10-stylesheets/releases/download/release/1.79.2/docbook-xsl-1.79.2.zip -OutFile docbook-xsl.zip + - 7z x -oC:\Boost\share\docbook-xml docbook-xml.zip + - 7z x -oC:\Boost\share docbook-xsl.zip - mkdir %APPVEYOR_BUILD_FOLDER%\bin - cd %APPVEYOR_BUILD_FOLDER%\bin - appveyor-retry powershell Invoke-WebRequest ftp://ftp.zlatkovic.com/libxml/iconv-1.9.2.win32.zip -OutFile iconv.zip From f191de5a5aa79746a1b56502e3721c8435a9b27d Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Sat, 7 Jan 2023 12:06:44 +0100 Subject: [PATCH 4/4] Add test for serializing a string or functor matcher --- test/test_matcher.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/test_matcher.cpp b/test/test_matcher.cpp index 5f1746d..5a53cb3 100644 --- a/test/test_matcher.cpp +++ b/test/test_matcher.cpp @@ -121,6 +121,11 @@ struct custom_constraint } bool operator()(int actual) { return actual == expected_; } }; + +bool custom_constraint_func(int) +{ + return false; +} } // namespace BOOST_AUTO_TEST_CASE(single_matcher_serializes) @@ -133,6 +138,30 @@ BOOST_AUTO_TEST_CASE(single_matcher_serializes) 1, 2, custom_constraint(), 4, 5)) == "1, 2, custom42, 4, 5"); } +BOOST_AUTO_TEST_CASE(string_matcher_serializes) +{ + using mock::detail::single_matcher; + BOOST_TEST(serialize(single_matcher("foo")) == "\"foo\""); + BOOST_TEST(serialize(single_matcher("foo")) == "\"foo\""); + BOOST_TEST(serialize(single_matcher("foo")) == "\"foo\""); + BOOST_TEST(serialize(single_matcher("foo")) == "\"foo\""); + BOOST_TEST(serialize(single_matcher("foo")) == "\"foo\""); + BOOST_TEST(serialize(single_matcher("foo")) == "\"foo\""); + BOOST_TEST(serialize(single_matcher("foo")) == "\"foo\""); + // Mixed types + BOOST_TEST(serialize(single_matcher("bar", 2)) == "\"bar\", 2"); +} + +BOOST_AUTO_TEST_CASE(functor_matcher_serializes) +{ + using mock::detail::single_matcher; + using Functor = decltype(custom_constraint_func); + BOOST_TEST(serialize(single_matcher(custom_constraint_func)) == "?"); + BOOST_TEST(serialize(single_matcher(custom_constraint_func)) == "?"); + // Mixed types + BOOST_TEST(serialize(single_matcher(custom_constraint_func, 2)) == "?, 2"); +} + BOOST_AUTO_TEST_CASE(multi_matcher_serializes) { using mock::detail::multi_matcher;