Skip to content

Commit b37853d

Browse files
committed
[#3198] add expectEqWithDiff to testutils
1 parent 3f7e8d6 commit b37853d

11 files changed

+73
-97
lines changed

src/bin/agent/tests/parser_unittests.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ namespace test {
3636
/// @param a first to be compared
3737
/// @param b second to be compared
3838
void compareJSON(ConstElementPtr a, ConstElementPtr b) {
39-
ASSERT_TRUE(a);
40-
ASSERT_TRUE(b);
41-
EXPECT_EQ(a->str(), b->str())
42-
#ifdef HAVE_CREATE_UNIFIED_DIFF
43-
<< "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
44-
#endif
45-
;
39+
expectEqWithDiff(a, b);
4640
}
4741

4842
/// @brief Tests if the input string can be parsed with specific parser

src/bin/d2/tests/parser_unittest.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ namespace test {
3737
/// @param a first to be compared
3838
/// @param b second to be compared
3939
void compareJSON(ConstElementPtr a, ConstElementPtr b) {
40-
ASSERT_TRUE(a);
41-
ASSERT_TRUE(b);
42-
EXPECT_EQ(a->str(), b->str())
43-
#ifdef HAVE_CREATE_UNIFIED_DIFF
44-
<< "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
45-
#endif
46-
;
40+
expectEqWithDiff(a, b);
4741
}
4842

4943
/// @brief Tests if the input string can be parsed with specific parser

src/bin/dhcp4/tests/config_parser_unittest.cc

+2-5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ using namespace isc::data;
5656
using namespace isc::dhcp;
5757
using namespace isc::dhcp::test;
5858
using namespace isc::hooks;
59+
using namespace isc::test;
5960
using namespace std;
6061

6162
namespace {
@@ -7465,11 +7466,7 @@ TEST_F(Dhcp4ParserTest, dhcpQueueControl) {
74657466
}
74667467

74677468
// Verify that the staged queue control equals the expected queue control.
7468-
EXPECT_TRUE(staged_control->equals(*exp_control))
7469-
#ifdef HAVE_CREATE_UNIFIED_DIFF
7470-
<< "\nDiff:\n" << isc::test::generateDiff(prettyPrint(staged_control), prettyPrint(exp_control)) << "\n"
7471-
#endif
7472-
;
7469+
expectEqWithDiff(staged_control, exp_control);
74737470
}
74747471
}
74757472
}

src/bin/dhcp4/tests/parser_unittest.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ namespace test {
3636
/// @param a first to be compared
3737
/// @param b second to be compared
3838
void compareJSON(ConstElementPtr a, ConstElementPtr b) {
39-
ASSERT_TRUE(a);
40-
ASSERT_TRUE(b);
41-
EXPECT_EQ(a->str(), b->str())
42-
#ifdef HAVE_CREATE_UNIFIED_DIFF
43-
<< "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
44-
#endif
45-
;
39+
expectEqWithDiff(a, b);
4640
}
4741

4842
/// @brief Tests if the input string can be parsed with specific parser

src/bin/dhcp6/tests/config_parser_unittest.cc

+2-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ using namespace isc::data;
6060
using namespace isc::dhcp;
6161
using namespace isc::dhcp::test;
6262
using namespace isc::hooks;
63+
using namespace isc::test;
6364
using namespace std;
6465

6566
namespace {
@@ -8039,11 +8040,7 @@ TEST_F(Dhcp6ParserTest, dhcpQueueControl) {
80398040
}
80408041

80418042
// Verify that the staged queue control equals the expected queue control.
8042-
EXPECT_TRUE(staged_control->equals(*exp_control))
8043-
#ifdef HAVE_CREATE_UNIFIED_DIFF
8044-
<< "\nDiff:\n" << isc::test::generateDiff(prettyPrint(staged_control), prettyPrint(exp_control)) << "\n"
8045-
#endif
8046-
;
8043+
expectEqWithDiff(staged_control, exp_control);
80478044
}
80488045
}
80498046
}

src/bin/dhcp6/tests/parser_unittest.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ namespace test {
3636
/// @param a first to be compared
3737
/// @param b second to be compared
3838
void compareJSON(ConstElementPtr a, ConstElementPtr b) {
39-
ASSERT_TRUE(a);
40-
ASSERT_TRUE(b);
41-
EXPECT_EQ(a->str(), b->str())
42-
#ifdef HAVE_CREATE_UNIFIED_DIFF
43-
<< "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
44-
#endif
45-
;
39+
expectEqWithDiff(a, b);
4640
}
4741

4842
/// @brief Tests if the input string can be parsed with specific parser

src/bin/netconf/tests/parser_unittests.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@ namespace test {
3838
/// @param a first to be compared
3939
/// @param b second to be compared
4040
void compareJSON(ConstElementPtr a, ConstElementPtr b) {
41-
ASSERT_TRUE(a);
42-
ASSERT_TRUE(b);
43-
EXPECT_EQ(a->str(), b->str())
44-
#ifdef HAVE_CREATE_UNIFIED_DIFF
45-
<< "\nDiff:\n" << generateDiff(prettyPrint(a), prettyPrint(b)) << "\n"
46-
#endif
47-
;
41+
expectEqWithDiff(a, b);
4842
}
4943

5044
/// @brief Tests if the input string can be parsed with specific parser

src/lib/testutils/test_to_element.cc

+31-6
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,48 @@
1212
#include <string>
1313
#include <vector>
1414

15+
using namespace isc::data;
16+
using namespace std;
17+
1518
namespace isc {
1619
namespace test {
1720

21+
void expectEqWithDiff(ConstElementPtr const& a, ConstElementPtr const& b) {
22+
ASSERT_TRUE(a);
23+
ASSERT_TRUE(b);
24+
string const pretty_print_a(prettyPrint(a));
25+
string const pretty_print_b(prettyPrint(b));
26+
EXPECT_EQ(pretty_print_a, pretty_print_b)
27+
<< endl
28+
<< "Diff:" << endl
29+
<< generateDiff(pretty_print_a, pretty_print_b) << endl;
30+
}
31+
32+
void expectEqWithDiff(ElementPtr const& a, ElementPtr const& b) {
33+
ASSERT_TRUE(a);
34+
ASSERT_TRUE(b);
35+
string const pretty_print_a(prettyPrint(a));
36+
string const pretty_print_b(prettyPrint(b));
37+
EXPECT_EQ(pretty_print_a, pretty_print_b)
38+
<< endl
39+
<< "Diff:" << endl
40+
<< generateDiff(pretty_print_a, pretty_print_b) << endl;
41+
}
42+
1843
#ifdef HAVE_CREATE_UNIFIED_DIFF
19-
std::string generateDiff(std::string left, std::string right) {
20-
std::vector<std::string> left_lines;
44+
string generateDiff(string left, string right) {
45+
vector<string> left_lines;
2146
boost::split(left_lines, left, boost::is_any_of("\n"));
22-
std::vector<std::string> right_lines;
47+
vector<string> right_lines;
2348
boost::split(right_lines, right, boost::is_any_of("\n"));
2449
using namespace testing::internal;
2550
return (edit_distance::CreateUnifiedDiff(left_lines, right_lines));
2651
}
2752
#else
2853
std::string generateDiff(std::string, std::string) {
29-
return ("");
54+
return ("N/A: !HAVE_CREATE_UNIFIED_DIFF");
3055
}
3156
#endif
3257

33-
}; // end of isc::test namespace
34-
}; // end of isc namespace
58+
} // namespace test
59+
} // namespace isc

src/lib/testutils/test_to_element.h

+17-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
namespace isc {
2323
namespace test {
2424

25+
/// @brief Expect two element pointers to be equal. Order of elements
26+
/// in a list or a map is also checked by default.
27+
/// @{
28+
void
29+
expectEqWithDiff(isc::data::ConstElementPtr const& left, isc::data::ConstElementPtr const& right);
30+
31+
void
32+
expectEqWithDiff(isc::data::ElementPtr const& left, isc::data::ElementPtr const& right);
33+
/// @}
34+
2535
/// @brief Return the difference between two strings
2636
///
2737
/// Use the gtest >= 1.8.0 tool which builds the difference between
@@ -30,15 +40,17 @@ namespace test {
3040
/// @param left left string
3141
/// @param right right string
3242
/// @return the unified diff between left and right
33-
std::string generateDiff(std::string left, std::string right);
43+
std::string
44+
generateDiff(std::string left, std::string right);
3445

3546
/// @brief Run a test using toElement() method with a string
3647
///
3748
/// @tparam Cfg the class implementing the toElement() method
3849
/// @param expected the expected textual value
3950
/// @param cfg an instance of the Cfg class
40-
template<typename Cfg>
41-
void runToElementTest(const std::string& expected, const Cfg& cfg) {
51+
template <typename Cfg>
52+
void
53+
runToElementTest(const std::string& expected, const Cfg& cfg) {
4254
using namespace isc::data;
4355
#ifdef HAVE_IS_BASE_OF
4456
static_assert(std::is_base_of<CfgToElement, Cfg>::value,
@@ -53,9 +65,7 @@ void runToElementTest(const std::string& expected, const Cfg& cfg) {
5365
std::string got = prettyPrint(unparsed);
5466
ADD_FAILURE() << "Expected:\n" << wanted << "\n"
5567
<< "Actual:\n" << got
56-
#ifdef HAVE_CREATE_UNIFIED_DIFF
5768
<< "\nDiff:\n" << generateDiff(wanted, got)
58-
#endif
5969
<< "\n";
6070
}
6171
}
@@ -78,14 +88,12 @@ void runToElementTest(isc::data::ConstElementPtr expected, const Cfg& cfg) {
7888
std::string got = prettyPrint(unparsed);
7989
ADD_FAILURE() << "Expected:\n" << wanted << "\n"
8090
<< "Actual:\n" << got
81-
#ifdef HAVE_CREATE_UNIFIED_DIFF
8291
<< "\nDiff:\n" << generateDiff(wanted, got)
83-
#endif
8492
<< "\n";
8593
}
8694
}
8795

88-
}; // end of isc::test namespace
89-
}; // end of isc namespace
96+
} // namespace test
97+
} // namespace isc
9098

9199
#endif // TEST_TO_ELEMENT_H

src/lib/yang/tests/config_unittests.cc

+2-25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <gtest/gtest.h>
1010

1111
#include <testutils/io_utils.h>
12+
#include <testutils/test_to_element.h>
1213
#include <testutils/user_context_utils.h>
1314
#include <yang/tests/json_configs.h>
1415
#include <yang/tests/sysrepo_setup.h>
@@ -24,35 +25,13 @@
2425
using namespace std;
2526
using namespace isc;
2627
using namespace isc::data;
28+
using namespace isc::test;
2729
using namespace isc::yang;
2830
using namespace isc::yang::test;
2931
using namespace sysrepo;
3032

3133
namespace {
3234

33-
/// @brief Return the difference between two strings
34-
///
35-
/// Use the gtest >= 1.8.0 tool which builds the difference between
36-
/// two vectors of lines.
37-
///
38-
/// @param left left string
39-
/// @param right right string
40-
/// @return the unified diff between left and right
41-
#ifdef HAVE_CREATE_UNIFIED_DIFF
42-
string generateDiff(string left, string right) {
43-
vector<string> left_lines;
44-
boost::split(left_lines, left, boost::is_any_of("\n"));
45-
vector<string> right_lines;
46-
boost::split(right_lines, right, boost::is_any_of("\n"));
47-
using namespace testing::internal;
48-
return (edit_distance::CreateUnifiedDiff(left_lines, right_lines));
49-
}
50-
#else
51-
string generateDiff(string, string) {
52-
return ("");
53-
}
54-
#endif
55-
5635
/// @brief Test Fixture class for Yang <-> JSON configs.
5736
class ConfigTest : public ::testing::Test {
5837
public:
@@ -159,9 +138,7 @@ class ConfigTest : public ::testing::Test {
159138
string got = prettyPrint(content);
160139
cerr << "Expected:\n" << wanted << "\n"
161140
<< "Actual:\n" << got
162-
#ifdef HAVE_CREATE_UNIFIED_DIFF
163141
<< "\nDiff:\n" << generateDiff(wanted, got)
164-
#endif
165142
<< "\n";
166143
return (false);
167144
}

src/lib/yang/tests/translator_option_data_unittests.cc

+14-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <gtest/gtest.h>
1010

11+
#include <testutils/test_to_element.h>
1112
#include <yang/tests/sysrepo_setup.h>
1213
#include <yang/translator_option_data.h>
1314
#include <yang/yang_models.h>
@@ -17,6 +18,7 @@
1718
using namespace std;
1819
using namespace isc;
1920
using namespace isc::data;
21+
using namespace isc::test;
2022
using namespace isc::yang;
2123
using namespace isc::yang::test;
2224
using namespace sysrepo;
@@ -100,7 +102,7 @@ TEST_F(TranslatorOptionDataListTestv4, get) {
100102
ASSERT_TRUE(options);
101103
ASSERT_EQ(Element::list, options->getType());
102104
EXPECT_EQ(1, options->size());
103-
EXPECT_TRUE(option->equals(*options->get(0)));
105+
expectEqWithDiff(option, options->get(0));
104106
}
105107

106108
// This test verifies that one option data can be properly translated
@@ -138,7 +140,7 @@ TEST_F(TranslatorOptionDataListTestv6, get) {
138140
ASSERT_TRUE(options);
139141
ASSERT_EQ(Element::list, options->getType());
140142
EXPECT_EQ(1, options->size());
141-
EXPECT_TRUE(option->equals(*options->get(0)));
143+
expectEqWithDiff(option, options->get(0));
142144
}
143145

144146
// This test verifies that an empty option data list can be properly
@@ -211,7 +213,7 @@ TEST_F(TranslatorOptionDataListTestv4, set) {
211213
EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
212214
ASSERT_TRUE(got);
213215
ASSERT_EQ(1, got->size());
214-
EXPECT_TRUE(option->equals(*got->get(0)));
216+
expectEqWithDiff(option, options->get(0));
215217
}
216218

217219
// This test verifies that one option data can be properly translated
@@ -256,10 +258,10 @@ TEST_F(TranslatorOptionDataListTestv6, set) {
256258
EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
257259
ASSERT_TRUE(got);
258260
ASSERT_EQ(1, got->size());
259-
EXPECT_TRUE(option->equals(*got->get(0)));
261+
expectEqWithDiff(option, options->get(0));
260262
}
261263

262-
// This test verifies that multiple options of smae code and space but different data can be
264+
// This test verifies that multiple options of same code and space but different data can be
263265
// configured for v4.
264266
TEST_F(TranslatorOptionDataListTestv4, optionsSameCodeAndSpace) {
265267
string const xpath("/kea-dhcp4-server:config");
@@ -289,7 +291,7 @@ TEST_F(TranslatorOptionDataListTestv4, optionsSameCodeAndSpace) {
289291
EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
290292
ASSERT_TRUE(got);
291293
EXPECT_EQ(2, got->size());
292-
EXPECT_TRUE(options->equals(*got));
294+
expectEqWithDiff(options, got);
293295

294296
// Now with keys only.
295297
options = Element::createList();
@@ -309,11 +311,11 @@ TEST_F(TranslatorOptionDataListTestv4, optionsSameCodeAndSpace) {
309311
EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
310312
ASSERT_TRUE(got);
311313
EXPECT_EQ(4, got->size());
312-
EXPECT_TRUE(options->get(0)->equals(*got->get(2)));
313-
EXPECT_TRUE(options->get(1)->equals(*got->get(3)));
314+
expectEqWithDiff(options->get(0), got->get(2));
315+
expectEqWithDiff(options->get(1), got->get(3));
314316
}
315317

316-
// This test verifies that multiple options of smae code and space but different data can be
318+
// This test verifies that multiple options of same code and space but different data can be
317319
// configured for v6.
318320
TEST_F(TranslatorOptionDataListTestv6, optionsSameCodeAndSpace) {
319321
string const xpath("/kea-dhcp6-server:config");
@@ -343,7 +345,7 @@ TEST_F(TranslatorOptionDataListTestv6, optionsSameCodeAndSpace) {
343345
EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
344346
ASSERT_TRUE(got);
345347
EXPECT_EQ(2, got->size());
346-
EXPECT_TRUE(options->equals(*got));
348+
expectEqWithDiff(options, got);
347349

348350
// Now with keys only.
349351
options = Element::createList();
@@ -363,8 +365,8 @@ TEST_F(TranslatorOptionDataListTestv6, optionsSameCodeAndSpace) {
363365
EXPECT_NO_THROW_LOG(got = translator_->getOptionDataListFromAbsoluteXpath(xpath));
364366
ASSERT_TRUE(got);
365367
EXPECT_EQ(4, got->size());
366-
EXPECT_TRUE(options->get(0)->equals(*got->get(2)));
367-
EXPECT_TRUE(options->get(1)->equals(*got->get(3)));
368+
expectEqWithDiff(options->get(0), got->get(2));
369+
expectEqWithDiff(options->get(1), got->get(3));
368370
}
369371

370372
} // namespace

0 commit comments

Comments
 (0)