Kea 2.5.6
test_to_element.h
Go to the documentation of this file.
1// Copyright (C) 2017-2024 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
22namespace isc {
23namespace test {
24
28void
30
31void
34
43std::string
44generateDiff(std::string left, std::string right);
45
51template <typename Cfg>
52void
53runToElementTest(const std::string& expected, const Cfg& cfg) {
54 using namespace isc::data;
55#ifdef HAVE_IS_BASE_OF
56 static_assert(std::is_base_of<CfgToElement, Cfg>::value,
57 "CfgToElement is not a base of the template parameter");
58#endif
59 ConstElementPtr json;
60 ASSERT_NO_THROW(json = Element::fromJSON(expected)) << expected;
61 ConstElementPtr unparsed;
62 ASSERT_NO_THROW(unparsed = cfg.toElement());
63 if (!isEquivalent(json, unparsed)) {
64 std::string wanted = prettyPrint(json);
65 std::string got = prettyPrint(unparsed);
66 ADD_FAILURE() << "Expected:\n" << wanted << "\n"
67 << "Actual:\n" << got
68 << "\nDiff:\n" << generateDiff(wanted, got)
69 << "\n";
70 }
71}
72
78template<typename Cfg>
79void runToElementTest(isc::data::ConstElementPtr expected, const Cfg& cfg) {
80#ifdef HAVE_IS_BASE_OF
81 static_assert(std::is_base_of<isc::data::CfgToElement, Cfg>::value,
82 "CfgToElement is not a base of the template parameter");
83#endif
85 ASSERT_NO_THROW(unparsed = cfg.toElement());
86 if (!isEquivalent(expected, unparsed)) {
87 std::string wanted = prettyPrint(expected);
88 std::string got = prettyPrint(unparsed);
89 ADD_FAILURE() << "Expected:\n" << wanted << "\n"
90 << "Actual:\n" << got
91 << "\nDiff:\n" << generateDiff(wanted, got)
92 << "\n";
93 }
94}
95
96} // namespace test
97} // namespace isc
98
99#endif // TEST_TO_ELEMENT_H
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
void expectEqWithDiff(ConstElementPtr const &a, ConstElementPtr const &b)
Expect two element pointers to be equal.
void runToElementTest(const std::string &expected, const Cfg &cfg)
Run a test using toElement() method with a string.
std::string generateDiff(std::string, std::string)
Return the difference between two strings.
Defines the logger used by the top-level component of kea-lfc.