$darkmode
gtest.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
22 #pragma once
23 #ifndef FABLE_UTILITY_GTEST_HPP_
24 #define FABLE_UTILITY_GTEST_HPP_
25 
26 #include <iostream> // for cerr
27 
28 #include <gtest/gtest.h> // for ASSERT_THROW, ASSERT_EQ
29 
30 #include <fable/conf.hpp> // for Conf, Json
31 #include <fable/confable.hpp> // for Confable
32 #include <fable/schema.hpp> // for Schema
33 #include <fable/error.hpp> // for SchemaError
34 #include <fable/utility.hpp> // for pretty_print
35 
36 namespace fable {
37 
44 inline void assert_eq(const Json& j, const Json& k) {
45  ASSERT_EQ(std::string(j.dump(2)), std::string(k.dump(2)));
46 }
47 
56 inline void assert_eq(const Json& j, const char expect[]) {
57  assert_eq(j, parse_json(expect));
58 }
59 
63 inline void assert_ne(const Json& j, const Json& k) {
64  ASSERT_NE(std::string(j.dump(2)), std::string(k.dump(2)));
65 }
66 
72 inline void assert_ne(const Json& j, const char expect[]) {
73  assert_ne(j, parse_json(expect));
74 }
75 
76 inline void assert_schema_eq(const Schema& s, const Json& expect) {
77  assert_eq(s.json_schema(), expect);
78 }
79 
80 inline void assert_schema_eq(const Schema& s, const char expect[]) {
81  assert_eq(s.json_schema(), parse_json(expect));
82 }
83 
84 inline void assert_schema_eq(const Confable& x, const Json& expect) {
85  assert_eq(x.schema().json_schema(), expect);
86 }
87 
88 inline void assert_schema_eq(const Confable& x, const char expect[]) {
89  assert_eq(x.schema().json_schema(), parse_json(expect));
90 }
91 
92 inline void assert_validate(const Schema& s, const Conf& input) {
93  try {
94  s.validate(input);
95  } catch (SchemaError& e) {
96  pretty_print(e, std::cerr);
97  throw;
98  };
99 }
100 
101 inline void assert_validate(const Schema& s, const char json_input[]) {
102  assert_validate(s, Conf{parse_json(json_input)});
103 }
104 
105 inline void assert_validate(const Confable& x, const Conf& input) {
106  assert_validate(x.schema(), input);
107 }
108 
109 inline void assert_validate(const Confable& x, const char json_input[]) {
110  assert_validate(x.schema(), Conf{parse_json(json_input)});
111 }
112 
113 inline void assert_invalidate(const Schema& s, const Conf& input) {
114  ASSERT_THROW(s.validate(input), SchemaError);
115 }
116 
117 inline void assert_invalidate(const Schema& s, const char json_input[]) {
118  ASSERT_THROW(s.validate(Conf{parse_json(json_input)}), SchemaError);
119 }
120 
121 inline void assert_invalidate(const Confable& x, const Conf& input) {
122  assert_invalidate(x.schema(), input);
123 }
124 
125 inline void assert_invalidate(const Confable& x, const char json_input[]) {
126  assert_invalidate(x, Conf{parse_json(json_input)});
127 }
128 
132 template <typename T>
133 inline void assert_to_json(const T& x, const Json& expect) {
134  assert_eq(x.to_json(), expect);
135 }
136 
142 template <typename T>
143 inline void assert_to_json(const T& x, const char json_expect[]) {
144  assert_to_json(x, parse_json(json_expect));
145 }
146 
147 inline void assert_from_conf_throw(Confable& x, const Conf& input) {
148  Json before = x.to_json();
149  ASSERT_THROW(x.from_conf(input), SchemaError);
150  assert_eq(x.to_json(), before);
151 }
152 
153 inline void assert_from_conf_throw(Confable& x, const char json_input[]) {
154  assert_from_conf_throw(x, Conf{parse_json(json_input)});
155 }
156 
157 template <typename T>
158 inline void assert_from_conf(T& x, const Conf& input) {
159  x.from_conf(input);
160 }
161 
162 template <typename T>
163 inline void assert_from_conf(T& x, const char json_input[]) {
164  assert_from_conf(x, Conf{parse_json(json_input)});
165 }
166 
173 template <typename T>
174 inline void assert_from_eq_to(T& x, const Json& identity) {
175  assert_from_conf(x, Conf{identity});
176  assert_to_json(x, identity);
177 }
178 
185 template <typename T>
186 inline void assert_from_eq_to(T& x, const char json_input[]) {
187  assert_from_eq_to(x, parse_json(json_input));
188 }
189 
190 } // namespace fable
191 
192 #endif // FABLE_UTILITY_GTEST_HPP_
Definition: conf.hpp:74
Definition: confable.hpp:46
void assert_from_eq_to(T &x, const Json &identity)
Definition: gtest.hpp:174
void assert_to_json(const T &x, const Json &expect)
Definition: gtest.hpp:133
void validate(const Conf &c) const override
Definition: schema.hpp:262
nlohmann::json Json
Definition: json.hpp:62
Definition: error.hpp:120
virtual void to_json(Json &j) const
Definition: confable.cpp:72
Definition: conf.hpp:70
Definition: schema.hpp:178
Schema & schema()
Definition: confable.cpp:55
virtual void from_conf(const Conf &c)
Definition: confable.cpp:66
void assert_ne(const Json &j, const Json &k)
Definition: gtest.hpp:63
Json json_schema() const override
Definition: schema.hpp:261
void assert_eq(const Json &j, const Json &k)
Definition: gtest.hpp:44
Json parse_json(InputType &&input)
Definition: json.hpp:105