1 // Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef TEST_TO_ELEMENT_H
8 #define TEST_TO_ELEMENT_H
9 
10 #include <cc/data.h>
11 #include <cc/cfg_to_element.h>
12 #include <gtest/gtest.h>
13 #include <string>
14 #ifdef HAVE_IS_BASE_OF
15 #include <type_traits>
16 #endif
17 
18 #ifndef CONFIG_H_WAS_INCLUDED
19 #error config.h must be included before test_to_element.h
20 #endif
21 
22 namespace isc {
23 namespace test {
24 
25 /// @brief Return the difference between two strings
26 ///
27 /// Use the gtest >= 1.8.0 tool which builds the difference between
28 /// two vectors of lines.
29 ///
30 /// @param left left string
31 /// @param right right string
32 /// @return the unified diff between left and right
33 std::string generateDiff(std::string left, std::string right);
34 
35 /// @brief Run a test using toElement() method with a string
36 ///
37 /// @tparam Cfg the class implementing the toElement() method
38 /// @param expected the expected textual value
39 /// @param cfg an instance of the Cfg class
40 template<typename Cfg>
runToElementTest(const std::string & expected,const Cfg & cfg)41 void runToElementTest(const std::string& expected, const Cfg& cfg) {
42     using namespace isc::data;
43 #ifdef HAVE_IS_BASE_OF
44     static_assert(std::is_base_of<CfgToElement, Cfg>::value,
45                   "CfgToElement is not a base of the template parameter");
46 #endif
47     ConstElementPtr json;
48     ASSERT_NO_THROW(json = Element::fromJSON(expected)) << expected;
49     ConstElementPtr unparsed;
50     ASSERT_NO_THROW(unparsed = cfg.toElement());
51     if (!isEquivalent(json, unparsed)) {
52         std::string wanted = prettyPrint(json);
53         std::string got = prettyPrint(unparsed);
54         ADD_FAILURE() << "Expected:\n" << wanted << "\n"
55                       << "Actual:\n" << got
56 #ifdef HAVE_CREATE_UNIFIED_DIFF
57                       << "\nDiff:\n" << generateDiff(wanted, got)
58 #endif
59                       << "\n";
60     }
61 }
62 
63 /// @brief Run a test using toElement() method with an Element
64 ///
65 /// @tparam Cfg the class implementing the toElement() method
66 /// @param expected the expected element value
67 /// @param cfg an instance of the Cfg class
68 template<typename Cfg>
runToElementTest(isc::data::ConstElementPtr expected,const Cfg & cfg)69 void runToElementTest(isc::data::ConstElementPtr expected, const Cfg& cfg) {
70 #ifdef HAVE_IS_BASE_OF
71     static_assert(std::is_base_of<isc::data::CfgToElement, Cfg>::value,
72                   "CfgToElement is not a base of the template parameter");
73 #endif
74     isc::data::ConstElementPtr unparsed;
75     ASSERT_NO_THROW(unparsed = cfg.toElement());
76     if (!isEquivalent(expected, unparsed)) {
77         std::string wanted = prettyPrint(expected);
78         std::string got = prettyPrint(unparsed);
79         ADD_FAILURE() << "Expected:\n" << wanted << "\n"
80                       << "Actual:\n" << got
81 #ifdef HAVE_CREATE_UNIFIED_DIFF
82                       << "\nDiff:\n" << generateDiff(wanted, got)
83 #endif
84                       << "\n";
85     }
86 }
87 
88 }; // end of isc::test namespace
89 }; // end of isc namespace
90 
91 #endif // TEST_TO_ELEMENT_H
92