]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3198] add expectEqWithDiff to testutils
authorAndrei Pavel <andrei@isc.org>
Fri, 26 Jan 2024 09:24:51 +0000 (11:24 +0200)
committerAndrei Pavel <andrei@isc.org>
Fri, 26 Jan 2024 10:48:18 +0000 (12:48 +0200)
src/bin/agent/tests/parser_unittests.cc
src/bin/d2/tests/parser_unittest.cc
src/bin/dhcp4/tests/config_parser_unittest.cc
src/bin/dhcp4/tests/parser_unittest.cc
src/bin/dhcp6/tests/config_parser_unittest.cc
src/bin/dhcp6/tests/parser_unittest.cc
src/bin/netconf/tests/parser_unittests.cc
src/lib/testutils/test_to_element.cc
src/lib/testutils/test_to_element.h
src/lib/yang/tests/config_unittests.cc
src/lib/yang/tests/translator_option_data_unittests.cc

index 6d86b1152da46e46e1f0484e1839883649f02a25..2585e88d01ab14ff7ab95cbe0019de8c0fc03348 100644 (file)
@@ -36,13 +36,7 @@ namespace test {
 /// @param a first to be compared
 /// @param b second to be compared
 void compareJSON(ConstElementPtr a, ConstElementPtr b) {
-    ASSERT_TRUE(a);
-    ASSERT_TRUE(b);
-    EXPECT_EQ(a->str(), b->str())
-#ifdef HAVE_CREATE_UNIFIED_DIFF
-        << "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
-#endif
-    ;
+    expectEqWithDiff(a, b);
 }
 
 /// @brief Tests if the input string can be parsed with specific parser
index 71bbba5457729dda65c7890f54e42eac2bd7f64a..489551fd0d203431b3cd6ddf9a7cb0ed06227498 100644 (file)
@@ -37,13 +37,7 @@ namespace test {
 /// @param a first to be compared
 /// @param b second to be compared
 void compareJSON(ConstElementPtr a, ConstElementPtr b) {
-    ASSERT_TRUE(a);
-    ASSERT_TRUE(b);
-    EXPECT_EQ(a->str(), b->str())
-#ifdef HAVE_CREATE_UNIFIED_DIFF
-        << "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
-#endif
-    ;
+    expectEqWithDiff(a, b);
 }
 
 /// @brief Tests if the input string can be parsed with specific parser
index bd5f2b361ffca553bb1d696c84cfc87ddf41db94..8417a99964a344b2be7724dea83b91c77389985a 100644 (file)
@@ -56,6 +56,7 @@ using namespace isc::data;
 using namespace isc::dhcp;
 using namespace isc::dhcp::test;
 using namespace isc::hooks;
+using namespace isc::test;
 using namespace std;
 
 namespace {
@@ -7465,11 +7466,7 @@ TEST_F(Dhcp4ParserTest, dhcpQueueControl) {
             }
 
             // Verify that the staged queue control equals the expected queue control.
-            EXPECT_TRUE(staged_control->equals(*exp_control))
-#ifdef HAVE_CREATE_UNIFIED_DIFF
-                << "\nDiff:\n" << isc::test::generateDiff(prettyPrint(staged_control), prettyPrint(exp_control)) << "\n"
-#endif
-            ;
+            expectEqWithDiff(staged_control, exp_control);
         }
     }
 }
index c3a88cb53e696a6d98f87b6eaa612351f460b2ea..80bf5049453d4204d7f9a885242294d0b468214f 100644 (file)
@@ -36,13 +36,7 @@ namespace test {
 /// @param a first to be compared
 /// @param b second to be compared
 void compareJSON(ConstElementPtr a, ConstElementPtr b) {
-    ASSERT_TRUE(a);
-    ASSERT_TRUE(b);
-    EXPECT_EQ(a->str(), b->str())
-#ifdef HAVE_CREATE_UNIFIED_DIFF
-        << "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
-#endif
-    ;
+    expectEqWithDiff(a, b);
 }
 
 /// @brief Tests if the input string can be parsed with specific parser
index 6f571d52aba429d6c514a98ef1ed4d3b5e2a5ee6..5cfb96fd5016fd3bffd7981bca8e4590ee29a5aa 100644 (file)
@@ -60,6 +60,7 @@ using namespace isc::data;
 using namespace isc::dhcp;
 using namespace isc::dhcp::test;
 using namespace isc::hooks;
+using namespace isc::test;
 using namespace std;
 
 namespace {
@@ -8039,11 +8040,7 @@ TEST_F(Dhcp6ParserTest, dhcpQueueControl) {
             }
 
             // Verify that the staged queue control equals the expected queue control.
-            EXPECT_TRUE(staged_control->equals(*exp_control))
-#ifdef HAVE_CREATE_UNIFIED_DIFF
-                << "\nDiff:\n" << isc::test::generateDiff(prettyPrint(staged_control), prettyPrint(exp_control)) << "\n"
-#endif
-            ;
+            expectEqWithDiff(staged_control, exp_control);
         }
     }
 }
index b0218233af5a6c39be7c9626a9c918ec87f574d3..61bbbfc4977f196b384e831eaeae0cc5680c143f 100644 (file)
@@ -36,13 +36,7 @@ namespace test {
 /// @param a first to be compared
 /// @param b second to be compared
 void compareJSON(ConstElementPtr a, ConstElementPtr b) {
-    ASSERT_TRUE(a);
-    ASSERT_TRUE(b);
-    EXPECT_EQ(a->str(), b->str())
-#ifdef HAVE_CREATE_UNIFIED_DIFF
-        << "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
-#endif
-    ;
+    expectEqWithDiff(a, b);
 }
 
 /// @brief Tests if the input string can be parsed with specific parser
index 852b5acbfa960953253b61622965847a1216da34..a7d08b1d00e84a149f7bee7d18fd281712387e18 100644 (file)
@@ -38,13 +38,7 @@ namespace test {
 /// @param a first to be compared
 /// @param b second to be compared
 void compareJSON(ConstElementPtr a, ConstElementPtr b) {
-    ASSERT_TRUE(a);
-    ASSERT_TRUE(b);
-    EXPECT_EQ(a->str(), b->str())
-#ifdef HAVE_CREATE_UNIFIED_DIFF
-        << "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
-#endif
-    ;
+    expectEqWithDiff(a, b);
 }
 
 /// @brief Tests if the input string can be parsed with specific parser
index 115757a247ccce6609e9cc8e222d47c27fc89ed0..9a333ede37b6a460715d68554d04c6cb0df3d5fe 100644 (file)
 #include <string>
 #include <vector>
 
+using namespace isc::data;
+using namespace std;
+
 namespace isc {
 namespace test {
 
+void expectEqWithDiff(ConstElementPtr const& a, ConstElementPtr const& b) {
+    ASSERT_TRUE(a);
+    ASSERT_TRUE(b);
+    string const pretty_print_a(prettyPrint(a));
+    string const pretty_print_b(prettyPrint(b));
+    EXPECT_EQ(pretty_print_a, pretty_print_b)
+        << endl
+        << "Diff:" << endl
+        << generateDiff(pretty_print_a, pretty_print_b) << endl;
+}
+
+void expectEqWithDiff(ElementPtr const& a, ElementPtr const& b) {
+    ASSERT_TRUE(a);
+    ASSERT_TRUE(b);
+    string const pretty_print_a(prettyPrint(a));
+    string const pretty_print_b(prettyPrint(b));
+    EXPECT_EQ(pretty_print_a, pretty_print_b)
+        << endl
+        << "Diff:" << endl
+        << generateDiff(pretty_print_a, pretty_print_b) << endl;
+}
+
 #ifdef HAVE_CREATE_UNIFIED_DIFF
-std::string generateDiff(std::string left, std::string right) {
-    std::vector<std::string> left_lines;
+string generateDiff(string left, string right) {
+    vector<string> left_lines;
     boost::split(left_lines, left, boost::is_any_of("\n"));
-    std::vector<std::string> right_lines;
+    vector<string> right_lines;
     boost::split(right_lines, right, boost::is_any_of("\n"));
     using namespace testing::internal;
     return (edit_distance::CreateUnifiedDiff(left_lines, right_lines));
 }
 #else
 std::string generateDiff(std::string, std::string) {
-    return ("");
+    return ("N/A: !HAVE_CREATE_UNIFIED_DIFF");
 }
 #endif
 
-}; // end of isc::test namespace
-}; // end of isc namespace
+}  // namespace test
+}  // namespace isc
index 1aa88fc0017b948ab9179b8316f14ce7aa89cd6b..a9af056ff3cd70ba04423232d1b41fec387f7269 100644 (file)
 namespace isc {
 namespace test {
 
+/// @brief Expect two element pointers to be equal. Order of elements
+/// in a list or a map is also checked by default.
+/// @{
+void
+expectEqWithDiff(isc::data::ConstElementPtr const& left, isc::data::ConstElementPtr const& right);
+
+void
+expectEqWithDiff(isc::data::ElementPtr const& left, isc::data::ElementPtr const& right);
+/// @}
+
 /// @brief Return the difference between two strings
 ///
 /// Use the gtest >= 1.8.0 tool which builds the difference between
@@ -30,15 +40,17 @@ namespace test {
 /// @param left left string
 /// @param right right string
 /// @return the unified diff between left and right
-std::string generateDiff(std::string left, std::string right);
+std::string
+generateDiff(std::string left, std::string right);
 
 /// @brief Run a test using toElement() method with a string
 ///
 /// @tparam Cfg the class implementing the toElement() method
 /// @param expected the expected textual value
 /// @param cfg an instance of the Cfg class
-template<typename Cfg>
-void runToElementTest(const std::string& expected, const Cfg& cfg) {
+template <typename Cfg>
+void
+runToElementTest(const std::string& expected, const Cfg& cfg) {
     using namespace isc::data;
 #ifdef HAVE_IS_BASE_OF
     static_assert(std::is_base_of<CfgToElement, Cfg>::value,
@@ -53,9 +65,7 @@ void runToElementTest(const std::string& expected, const Cfg& cfg) {
         std::string got = prettyPrint(unparsed);
         ADD_FAILURE() << "Expected:\n" << wanted << "\n"
                       << "Actual:\n" << got
-#ifdef HAVE_CREATE_UNIFIED_DIFF
                       << "\nDiff:\n" << generateDiff(wanted, got)
-#endif
                       << "\n";
     }
 }
@@ -78,14 +88,12 @@ void runToElementTest(isc::data::ConstElementPtr expected, const Cfg& cfg) {
         std::string got = prettyPrint(unparsed);
         ADD_FAILURE() << "Expected:\n" << wanted << "\n"
                       << "Actual:\n" << got
-#ifdef HAVE_CREATE_UNIFIED_DIFF
                       << "\nDiff:\n" << generateDiff(wanted, got)
-#endif
                       << "\n";
     }
 }
 
-}; // end of isc::test namespace
-}; // end of isc namespace
+}  // namespace test
+}  // namespace isc
 
 #endif // TEST_TO_ELEMENT_H
index a25bf8e8025cbe5f72026d78700759ca2d8907eb..ad2badd1b625dc0e66a890a201110c12619daa65 100644 (file)
@@ -9,6 +9,7 @@
 #include <gtest/gtest.h>
 
 #include <testutils/io_utils.h>
+#include <testutils/test_to_element.h>
 #include <testutils/user_context_utils.h>
 #include <yang/tests/json_configs.h>
 #include <yang/tests/sysrepo_setup.h>
 using namespace std;
 using namespace isc;
 using namespace isc::data;
+using namespace isc::test;
 using namespace isc::yang;
 using namespace isc::yang::test;
 using namespace sysrepo;
 
 namespace {
 
-/// @brief Return the difference between two strings
-///
-/// Use the gtest >= 1.8.0 tool which builds the difference between
-/// two vectors of lines.
-///
-/// @param left left string
-/// @param right right string
-/// @return the unified diff between left and right
-#ifdef HAVE_CREATE_UNIFIED_DIFF
-string generateDiff(string left, string right) {
-    vector<string> left_lines;
-    boost::split(left_lines, left, boost::is_any_of("\n"));
-    vector<string> right_lines;
-    boost::split(right_lines, right, boost::is_any_of("\n"));
-    using namespace testing::internal;
-    return (edit_distance::CreateUnifiedDiff(left_lines, right_lines));
-}
-#else
-string generateDiff(string, string) {
-    return ("");
-}
-#endif
-
 /// @brief Test Fixture class for Yang <-> JSON configs.
 class ConfigTest : public ::testing::Test {
 public:
@@ -159,9 +138,7 @@ public:
         string got = prettyPrint(content);
         cerr << "Expected:\n" << wanted << "\n"
              << "Actual:\n" << got
-#ifdef HAVE_CREATE_UNIFIED_DIFF
              << "\nDiff:\n" << generateDiff(wanted, got)
-#endif
              << "\n";
         return (false);
     }
index ae69616607d397b1d220ff63902e3adc4763a28b..9dd0f04531a5c3ea42277a3b14c003c3570f1f0e 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <gtest/gtest.h>
 
+#include <testutils/test_to_element.h>
 #include <yang/tests/sysrepo_setup.h>
 #include <yang/translator_option_data.h>
 #include <yang/yang_models.h>
@@ -17,6 +18,7 @@
 using namespace std;
 using namespace isc;
 using namespace isc::data;
+using namespace isc::test;
 using namespace isc::yang;
 using namespace isc::yang::test;
 using namespace sysrepo;
@@ -100,7 +102,7 @@ TEST_F(TranslatorOptionDataListTestv4, get) {
     ASSERT_TRUE(options);
     ASSERT_EQ(Element::list, options->getType());
     EXPECT_EQ(1, options->size());
-    EXPECT_TRUE(option->equals(*options->get(0)));
+    expectEqWithDiff(option, options->get(0));
 }
 
 // This test verifies that one option data can be properly translated
@@ -138,7 +140,7 @@ TEST_F(TranslatorOptionDataListTestv6, get) {
     ASSERT_TRUE(options);
     ASSERT_EQ(Element::list, options->getType());
     EXPECT_EQ(1, options->size());
-    EXPECT_TRUE(option->equals(*options->get(0)));
+    expectEqWithDiff(option, options->get(0));
 }
 
 // This test verifies that an empty option data list can be properly
@@ -211,7 +213,7 @@ TEST_F(TranslatorOptionDataListTestv4, set) {
     EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
     ASSERT_TRUE(got);
     ASSERT_EQ(1, got->size());
-    EXPECT_TRUE(option->equals(*got->get(0)));
+    expectEqWithDiff(option, options->get(0));
 }
 
 // This test verifies that one option data can be properly translated
@@ -256,10 +258,10 @@ TEST_F(TranslatorOptionDataListTestv6, set) {
     EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
     ASSERT_TRUE(got);
     ASSERT_EQ(1, got->size());
-    EXPECT_TRUE(option->equals(*got->get(0)));
+    expectEqWithDiff(option, options->get(0));
 }
 
-// This test verifies that multiple options of smae code and space but different data can be
+// This test verifies that multiple options of same code and space but different data can be
 // configured for v4.
 TEST_F(TranslatorOptionDataListTestv4, optionsSameCodeAndSpace) {
     string const xpath("/kea-dhcp4-server:config");
@@ -289,7 +291,7 @@ TEST_F(TranslatorOptionDataListTestv4, optionsSameCodeAndSpace) {
     EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
     ASSERT_TRUE(got);
     EXPECT_EQ(2, got->size());
-    EXPECT_TRUE(options->equals(*got));
+    expectEqWithDiff(options, got);
 
     // Now with keys only.
     options = Element::createList();
@@ -309,11 +311,11 @@ TEST_F(TranslatorOptionDataListTestv4, optionsSameCodeAndSpace) {
     EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
     ASSERT_TRUE(got);
     EXPECT_EQ(4, got->size());
-    EXPECT_TRUE(options->get(0)->equals(*got->get(2)));
-    EXPECT_TRUE(options->get(1)->equals(*got->get(3)));
+    expectEqWithDiff(options->get(0), got->get(2));
+    expectEqWithDiff(options->get(1), got->get(3));
 }
 
-// This test verifies that multiple options of smae code and space but different data can be
+// This test verifies that multiple options of same code and space but different data can be
 // configured for v6.
 TEST_F(TranslatorOptionDataListTestv6, optionsSameCodeAndSpace) {
     string const xpath("/kea-dhcp6-server:config");
@@ -343,7 +345,7 @@ TEST_F(TranslatorOptionDataListTestv6, optionsSameCodeAndSpace) {
     EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
     ASSERT_TRUE(got);
     EXPECT_EQ(2, got->size());
-    EXPECT_TRUE(options->equals(*got));
+    expectEqWithDiff(options, got);
 
     // Now with keys only.
     options = Element::createList();
@@ -363,8 +365,8 @@ TEST_F(TranslatorOptionDataListTestv6, optionsSameCodeAndSpace) {
     EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
     ASSERT_TRUE(got);
     EXPECT_EQ(4, got->size());
-    EXPECT_TRUE(options->get(0)->equals(*got->get(2)));
-    EXPECT_TRUE(options->get(1)->equals(*got->get(3)));
+    expectEqWithDiff(options->get(0), got->get(2));
+    expectEqWithDiff(options->get(1), got->get(3));
 }
 
 }  // namespace