1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef TEST_TO_ELEMENT_H
#define TEST_TO_ELEMENT_H

#include <cc/data.h>
#include <cc/cfg_to_element.h>
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#ifdef HAVE_IS_BASE_OF
#include <type_traits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#endif

#ifndef CONFIG_H_WAS_INCLUDED
#error config.h must be included before test_to_element.h
#endif

namespace isc {
namespace test {

/// @brief Expect two element pointers to be equal. Order of elements
/// in a list or a map is also checked by default.
/// @{
void
expectEqWithDiff(isc::data::ConstElementPtr const& left, isc::data::ConstElementPtr const& right);

void
expectEqWithDiff(isc::data::ElementPtr const& left, isc::data::ElementPtr const& right);
/// @}

/// @brief Return the difference between two strings
///
/// Use the gtest >= 1.8.0 tool which builds the difference between
/// two vectors of lines.
///
/// @param left left string
/// @param right right string
/// @return the unified diff between left and right
std::string
generateDiff(std::string left, std::string right);

/// @brief Run a test using toElement() method with a string
///
/// @tparam Cfg the class implementing the toElement() method
/// @param expected the expected textual value
/// @param cfg an instance of the Cfg class
template <typename Cfg>
void
runToElementTest(const std::string& expected, const Cfg& cfg) {
    using namespace isc::data;
#ifdef HAVE_IS_BASE_OF
    static_assert(std::is_base_of<CfgToElement, Cfg>::value,
                  "CfgToElement is not a base of the template parameter");
#endif
    ConstElementPtr json;
    ASSERT_NO_THROW(json = Element::fromJSON(expected)) << expected;
    ConstElementPtr unparsed;
    ASSERT_NO_THROW(unparsed = cfg.toElement());
    if (!isEquivalent(json, unparsed)) {
        std::string wanted = prettyPrint(json);
        std::string got = prettyPrint(unparsed);
        ADD_FAILURE() << "Expected:\n" << wanted << "\n"
                      << "Actual:\n" << got
                      << "\nDiff:\n" << generateDiff(wanted, got)
                      << "\n";
    }
}

/// @brief Run a test using toElement() method with an Element
///
/// @tparam Cfg the class implementing the toElement() method
/// @param expected the expected element value
/// @param cfg an instance of the Cfg class
template<typename Cfg>
void runToElementTest(isc::data::ConstElementPtr expected, const Cfg& cfg) {
#ifdef HAVE_IS_BASE_OF
    static_assert(std::is_base_of<isc::data::CfgToElement, Cfg>::value,
                  "CfgToElement is not a base of the template parameter");
#endif
    isc::data::ConstElementPtr unparsed;
    ASSERT_NO_THROW(unparsed = cfg.toElement());
    if (!isEquivalent(expected, unparsed)) {
        std::string wanted = prettyPrint(expected);
        std::string got = prettyPrint(unparsed);
        ADD_FAILURE() << "Expected:\n" << wanted << "\n"
                      << "Actual:\n" << got
                      << "\nDiff:\n" << generateDiff(wanted, got)
                      << "\n";
    }
}

}  // namespace test
}  // namespace isc

#endif // TEST_TO_ELEMENT_H