$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 
24 #include <iostream> // for cerr
25 
26 #include <gtest/gtest.h> // for ASSERT_THROW, ASSERT_EQ
27 
28 #include <fable/fable_fwd.hpp>
29 #include <fable/conf.hpp> // for Conf, Json
30 #include <fable/confable.hpp> // for Confable
31 #include <fable/schema.hpp> // for Schema
32 #include <fable/error.hpp> // for SchemaError
33 #include <fable/utility.hpp> // for pretty_print
34 
35 namespace fable {
36 
43 inline void assert_eq(const Json& j, const Json& k) {
44  ASSERT_EQ(std::string(j.dump(2)), std::string(k.dump(2)));
45 }
46 
55 inline void assert_eq(const Json& j, const char expect[]) {
56  assert_eq(j, parse_json(expect));
57 }
58 
62 inline void assert_ne(const Json& j, const Json& k) {
63  ASSERT_NE(std::string(j.dump(2)), std::string(k.dump(2)));
64 }
65 
71 inline void assert_ne(const Json& j, const char expect[]) {
72  assert_ne(j, parse_json(expect));
73 }
74 
75 inline void assert_schema_eq(const Schema& s, const Json& expect) {
76  assert_eq(s.json_schema(), expect);
77 }
78 
79 inline void assert_schema_eq(const Schema& s, const char expect[]) {
80  assert_eq(s.json_schema(), parse_json(expect));
81 }
82 
83 inline void assert_schema_eq(const Confable& x, const Json& expect) {
84  assert_eq(x.schema().json_schema(), expect);
85 }
86 
87 inline void assert_schema_eq(const Confable& x, const char expect[]) {
88  assert_eq(x.schema().json_schema(), parse_json(expect));
89 }
90 
91 inline void assert_validate(const Schema& s, const Conf& input) {
92  try {
93  s.validate_or_throw(input);
94  } catch (SchemaError& e) {
95  pretty_print(e, std::cerr);
96  throw;
97  };
98 }
99 
100 inline void assert_validate(const Schema& s, const char json_input[]) {
101  assert_validate(s, Conf{parse_json(json_input)});
102 }
103 
104 inline void assert_validate(const Confable& x, const Conf& input) {
105  assert_validate(x.schema(), input);
106 }
107 
108 inline void assert_validate(const Confable& x, const char json_input[]) {
109  assert_validate(x.schema(), Conf{parse_json(json_input)});
110 }
111 
112 inline void assert_invalidate(const Schema& s, const Conf& input) {
113  ASSERT_THROW(s.validate_or_throw(input), SchemaError);
114 }
115 
116 inline void assert_invalidate(const Schema& s, const char json_input[]) {
117  ASSERT_THROW(s.validate_or_throw(Conf{parse_json(json_input)}), SchemaError);
118 }
119 
120 inline void assert_invalidate(const Confable& x, const Conf& input) {
121  assert_invalidate(x.schema(), input);
122 }
123 
124 inline void assert_invalidate(const Confable& x, const char json_input[]) {
125  assert_invalidate(x, Conf{parse_json(json_input)});
126 }
127 
131 template <typename T>
132 inline void assert_to_json(const T& x, const Json& expect) {
133  assert_eq(x.to_json(), expect);
134 }
135 
141 template <typename T>
142 inline void assert_to_json(const T& x, const char json_expect[]) {
143  assert_to_json(x, parse_json(json_expect));
144 }
145 
146 inline void assert_from_conf_throw(Confable& x, const Conf& input) {
147  Json before = x.to_json();
148  ASSERT_THROW(x.from_conf(input), SchemaError);
149  assert_eq(x.to_json(), before);
150 }
151 
152 inline void assert_from_conf_throw(Confable& x, const char json_input[]) {
153  assert_from_conf_throw(x, Conf{parse_json(json_input)});
154 }
155 
156 template <typename T>
157 inline void assert_from_conf(T& x, const Conf& input) {
158  x.from_conf(input);
159 }
160 
161 template <typename T>
162 inline void assert_from_conf(T& x, const char json_input[]) {
163  assert_from_conf(x, Conf{parse_json(json_input)});
164 }
165 
172 template <typename T>
173 inline void assert_from_eq_to(T& x, const Json& identity) {
174  assert_from_conf(x, Conf{identity});
175  assert_to_json(x, identity);
176 }
177 
184 template <typename T>
185 inline void assert_from_eq_to(T& x, const char json_input[]) {
186  assert_from_eq_to(x, parse_json(json_input));
187 }
188 
189 } // namespace fable
Definition: conf.hpp:82
nlohmann::json Json
Definition: fable_fwd.hpp:35
void assert_ne(const Json &j, const Json &k)
Definition: gtest.hpp:62
void assert_eq(const Json &j, const Json &k)
Definition: gtest.hpp:43
void assert_to_json(const T &x, const Json &expect)
Definition: gtest.hpp:132
void assert_from_eq_to(T &x, const Json &identity)
Definition: gtest.hpp:173
Json parse_json(InputType &&input)
Definition: json.hpp:105