| Kea 3.0.2-git

test_to_element.h
Go to the documentation of this file.
1// Copyright (C) 2017-2025 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#include <type_traits>
15
16#ifndef CONFIG_H_WAS_INCLUDED
17#error config.h must be included before test_to_element.h
18#endif
19
20 namespace isc {
21 namespace test {
22
26 void
27 expectEqWithDiff(isc::data::ConstElementPtr const& left, isc::data::ConstElementPtr const& right);
28
29 void
30 expectEqWithDiff(isc::data::ElementPtr const& left, isc::data::ElementPtr const& right);
31
32 void
33 assertEqWithDiff(isc::data::ConstElementPtr const& left, isc::data::ConstElementPtr const& right);
34
35 void
36 assertEqWithDiff(isc::data::ElementPtr const& left, isc::data::ConstElementPtr const& right);
38
47std::string
48 generateDiff(std::string left, std::string right);
49
55 template <typename Cfg>
56 void
57 runToElementTest(const std::string& expected, const Cfg& cfg) {
58 using namespace isc::data;
59 static_assert(std::is_base_of<CfgToElement, Cfg>::value,
60 "CfgToElement is not a base of the template parameter");
61 ConstElementPtr json;
62 ASSERT_NO_THROW(json = Element::fromJSON(expected)) << expected;
63 ConstElementPtr unparsed;
64 ASSERT_NO_THROW(unparsed = cfg.toElement());
65 if (!isEquivalent(json, unparsed)) {
66 std::string wanted = prettyPrint(json);
67 std::string got = prettyPrint(unparsed);
68 ADD_FAILURE() << "Expected:\n" << wanted << "\n"
69 << "Actual:\n" << got
70 << "\nDiff:\n" << generateDiff(wanted, got)
71 << "\n";
72 }
73}
74
80 template<typename Cfg>
81 void runToElementTest(isc::data::ConstElementPtr expected, const Cfg& cfg) {
82 static_assert(std::is_base_of<isc::data::CfgToElement, Cfg>::value,
83 "CfgToElement is not a base of the template parameter");
84 isc::data::ConstElementPtr unparsed;
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
cfg_to_element.h
Element::fromJSON
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition data.cc:798
ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
data.h
isc::data
Definition base_stamped_element.cc:12
isc::data::isEquivalent
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
Definition data.cc:1542
isc::data::prettyPrint
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
Definition data.cc:1547
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::test
Definition env_var_wrapper.cc:12
isc::test::expectEqWithDiff
void expectEqWithDiff(ConstElementPtr const &a, ConstElementPtr const &b)
Expect two element pointers to be equal.
Definition test_to_element.cc:21
isc::test::runToElementTest
void runToElementTest(const std::string &expected, const Cfg &cfg)
Run a test using toElement() method with a string.
Definition test_to_element.h:57
isc::test::assertEqWithDiff
void assertEqWithDiff(ConstElementPtr const &a, ConstElementPtr const &b)
Definition test_to_element.cc:43
isc::test::generateDiff
std::string generateDiff(std::string, std::string)
Return the difference between two strings.
Definition test_to_element.cc:75
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148