Kea 3.1.0
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
20namespace isc {
21namespace test {
22
26void
28
29void
31
32void
34
35void
38
47std::string
48generateDiff(std::string left, std::string right);
49
55template <typename Cfg>
56void
57runToElementTest(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
80template<typename Cfg>
81void 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");
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
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
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
Definition data.cc:1544
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
Definition data.cc:1549
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.
void assertEqWithDiff(ConstElementPtr const &a, ConstElementPtr const &b)
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.