Kea  1.9.9-git
test_to_element.h
Go to the documentation of this file.
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 
33 std::string generateDiff(std::string left, std::string right);
34 
40 template<typename 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 
68 template<typename 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
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
void runToElementTest(const std::string &expected, const Cfg &cfg)
Run a test using toElement() method with a string.
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
Definition: data.cc:1224
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
Definition: data.cc:1219
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
std::string generateDiff(std::string, std::string)
Return the difference between two strings.
Defines the logger used by the top-level component of kea-dhcp-ddns.