Skip to content

Commit 902dfc9

Browse files
committed
fable: Add and use gtest utility functions
1 parent b0ae81b commit 902dfc9

File tree

6 files changed

+271
-225
lines changed

6 files changed

+271
-225
lines changed

fable/include/fable/utility/gtest.hpp

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
*/
18+
/**
19+
* \file fable/utility/gtest.hpp
20+
*/
21+
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/error.hpp> // for SchemaError
33+
#include <fable/utility.hpp> // for pretty_print
34+
35+
namespace fable {
36+
37+
inline void assert_eq(const Json& j, const Json& k) {
38+
ASSERT_EQ(std::string(j.dump(2)), std::string(k.dump(2)));
39+
}
40+
41+
inline void assert_ne(const Json& j, const Json& k) {
42+
ASSERT_NE(std::string(j.dump(2)), std::string(k.dump(2)));
43+
}
44+
45+
inline void assert_schema_eq(const Confable& x, const Json& expect) {
46+
assert_eq(x.schema().json_schema(), expect);
47+
}
48+
49+
inline void assert_schema_eq(const Confable& x, const char expect[]) {
50+
assert_eq(x.schema().json_schema(), Json::parse(expect));
51+
}
52+
53+
inline void assert_validate(const Confable& x, const Conf& input) {
54+
try {
55+
x.schema().validate(input);
56+
} catch (SchemaError& e) {
57+
pretty_print(e, std::cerr);
58+
throw;
59+
};
60+
}
61+
62+
inline void assert_validate(const Confable& x, const char json_input[]) {
63+
assert_validate(x, Conf{Json::parse(json_input)});
64+
}
65+
66+
inline void assert_invalidate(const Confable& x, const Conf& input) {
67+
ASSERT_THROW(x.schema().validate(input), SchemaError);
68+
}
69+
70+
inline void assert_invalidate(const Confable& x, const char json_input[]) {
71+
assert_invalidate(x, Conf{Json::parse(json_input)});
72+
}
73+
74+
template <typename T>
75+
inline void assert_to_json(const T& x, const Json& expect) {
76+
assert_eq(x.to_json(), expect);
77+
}
78+
79+
template <typename T>
80+
inline void assert_to_json(const T& x, const char json_expect[]) {
81+
assert_to_json(x, Json::parse(json_expect));
82+
}
83+
84+
inline void assert_from_conf_throw(Confable& x, const Conf& input) {
85+
Json before = x.to_json();
86+
ASSERT_THROW(x.from_conf(input), SchemaError);
87+
assert_eq(x.to_json(), before);
88+
}
89+
90+
inline void assert_from_conf_throw(Confable& x, const char json_input[]) {
91+
assert_from_conf_throw(x, Conf{Json::parse(json_input)});
92+
}
93+
94+
template <typename T>
95+
inline void assert_from_conf(T& x, const Conf& input) {
96+
x.from_conf(input);
97+
}
98+
99+
template <typename T>
100+
inline void assert_from_conf(T& x, const char json_input[]) {
101+
assert_from_conf(x, Conf{Json::parse(json_input)});
102+
}
103+
104+
template <typename T>
105+
inline void assert_from_eq_to(T& x, const Json& identity) {
106+
assert_from_conf(x, Conf{identity});
107+
assert_to_json(x, identity);
108+
}
109+
110+
template <typename T>
111+
inline void assert_from_eq_to(T& x, const char json_input[]) {
112+
assert_from_eq_to(x, Json::parse(json_input));
113+
}
114+
115+
} // namespace fable
116+
117+
#endif // FABLE_UTILITY_GTEST_HPP_

fable/src/fable/schema/const_test.cpp

+16-24
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
* \see fable/schema/const.hpp
2121
*/
2222

23-
#include <iostream> // for cout, endl
24-
#include <string> // for string
25-
using namespace std; // NOLINT(build/namespaces)
23+
#include <gtest/gtest.h> // for TEST
2624

27-
#include <gtest/gtest.h>
25+
#include <fable/confable.hpp> // for Confable
26+
#include <fable/schema.hpp> // for Const
27+
#include <fable/utility/gtest.hpp> // for assert_validate
2828

29-
#include <fable/confable.hpp> // for Confable
30-
#include <fable/schema.hpp> // for Const
29+
namespace {
3130

3231
struct MyConstStruct : public fable::Confable {
3332
CONFABLE_SCHEMA(MyConstStruct) {
@@ -39,14 +38,11 @@ struct MyConstStruct : public fable::Confable {
3938
}
4039
};
4140

42-
inline void assert_json_eq(const fable::Json& j, const fable::Json& k) {
43-
ASSERT_EQ(std::string(j.dump()), std::string(k.dump()));
44-
}
41+
} // anonymous namespace
4542

4643
TEST(fable_schema_const, schema) {
4744
MyConstStruct tmp;
48-
49-
fable::Json expect = R"({
45+
fable::assert_schema_eq(tmp, R"({
5046
"type": "object",
5147
"properties": {
5248
"release": {
@@ -60,34 +56,30 @@ TEST(fable_schema_const, schema) {
6056
},
6157
"required": ["release"],
6258
"additionalProperties": false
63-
})"_json;
64-
65-
assert_json_eq(tmp.schema().json_schema(), expect);
59+
})");
6660
}
6761

6862
TEST(fable_schema_const, validate) {
6963
MyConstStruct tmp;
7064

71-
tmp.schema().validate(fable::Conf{R"({
65+
fable::assert_validate(tmp, R"({
7266
"release": "2"
73-
})"_json});
67+
})");
7468

75-
tmp.schema().validate(fable::Conf{R"({
69+
fable::assert_validate(tmp, R"({
7670
"release": "2",
7771
"major": 2
78-
})"_json});
72+
})");
7973

80-
fable::Conf wrong{R"({
74+
fable::assert_invalidate(tmp, R"({
8175
"release": "wrong"
82-
})"_json};
83-
ASSERT_ANY_THROW(tmp.schema().validate(wrong));
76+
})");
8477
}
8578

8679
TEST(fable_schema_const, to_json) {
8780
MyConstStruct tmp;
88-
fable::Json expect = R"({
81+
fable::assert_to_json(tmp, R"({
8982
"release": "2",
9083
"major": 2
91-
})"_json;
92-
assert_json_eq(tmp.schema().to_json(), expect);
84+
})");
9385
}

fable/src/fable/schema/enum_test.cpp

+17-55
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
#include <gtest/gtest.h>
2424

25-
#include <fable/confable.hpp>
26-
#include <fable/schema.hpp>
27-
using namespace fable; // NOLINT(build/namespaces)
25+
#include <fable/confable.hpp> // for Confable
26+
#include <fable/schema.hpp> // for Struct, ...
27+
#include <fable/utility/gtest.hpp> // for assert_to_json, ...
2828

2929
namespace logger {
3030
enum LogLevel {
@@ -44,84 +44,46 @@ FABLE_ENUM_SERIALIZATION(logger::LogLevel, ({
4444

4545
using LogLevel = logger::LogLevel;
4646

47-
struct LoggerStruct : public Confable {
47+
struct LoggerStruct : public fable::Confable {
4848
boost::optional<LogLevel> level;
4949

5050
CONFABLE_SCHEMA(LoggerStruct) {
51-
using namespace schema; // NOLINT(build/namespaces)
51+
using namespace fable::schema; // NOLINT(build/namespaces)
5252
return Struct{
5353
{"level", make_schema(&level, "")},
5454
};
5555
}
5656
};
5757

5858
TEST(fable_schema_enum, struct_enum) {
59-
LoggerStruct s;
60-
Json empty = R"({
61-
})"_json;
62-
Json input = R"({
63-
"level": "info"
64-
})"_json;
65-
Json filled = R"({
66-
"level": "info"
67-
})"_json;
68-
69-
ASSERT_NO_THROW({
70-
ASSERT_EQ(s.to_json().dump(2), empty.dump(2));
71-
s.from_conf(Conf{input});
72-
ASSERT_EQ(s.to_json().dump(2), filled.dump(2));
73-
});
59+
LoggerStruct l;
60+
fable::assert_to_json(l, "{}");
61+
fable::assert_from_eq_to(l, fable::Json{{"level", "info"}});
7462
}
7563

7664
TEST(fable_schema_enum, vector_enum_ok) {
77-
using namespace schema; // NOLINT(build/namespaces)
65+
using namespace fable::schema; // NOLINT(build/namespaces)
7866
std::vector<LogLevel> xs;
7967
Array<LogLevel, Enum<LogLevel>> s{&xs, ""};
8068

81-
Json empty = R"([])"_json;
82-
Json input = R"([
83-
"info"
84-
])"_json;
85-
Json filled = R"([
69+
fable::assert_to_json(s, "[]");
70+
fable::assert_from_eq_to(s, R"([
8671
"info"
87-
])"_json;
88-
89-
Json j;
90-
s.to_json(j);
91-
ASSERT_EQ(j.dump(2), empty.dump(2));
92-
93-
s.from_conf(Conf{input});
72+
])");
9473
ASSERT_EQ(xs.size(), 1);
9574
ASSERT_EQ(xs[0], LogLevel::info);
96-
ASSERT_NO_THROW({
97-
s.to_json(j);
98-
ASSERT_EQ(j.dump(2), filled.dump(2));
99-
});
10075
}
10176

10277
TEST(fable_schema_enum, vector_struct_enum) {
103-
using namespace schema; // NOLINT(build/namespaces)
78+
using namespace fable::schema; // NOLINT(build/namespaces)
10479
std::vector<LoggerStruct> xs;
10580
Array<LoggerStruct, FromConfable<LoggerStruct>> s{&xs, ""};
10681

107-
Json empty = R"([])"_json;
108-
Json input = R"([{
109-
"level": "info"
110-
}])"_json;
111-
Json filled = R"([{
112-
"level": "info"
113-
}])"_json;
114-
115-
Json j;
116-
s.to_json(j);
117-
ASSERT_EQ(j.dump(2), empty.dump(2));
82+
fable::assert_to_json(s, "[]");
83+
fable::assert_from_eq_to(s, R"([
84+
{"level": "info"}
85+
])");
11886

119-
s.from_conf(Conf{input});
12087
ASSERT_EQ(xs.size(), 1);
12188
ASSERT_EQ(*(xs[0].level), LogLevel::info);
122-
ASSERT_EQ(Json(xs).dump(2), filled.dump(2));
123-
ASSERT_NO_THROW({
124-
s.to_json(j);
125-
ASSERT_EQ(j.dump(2), filled.dump(2));
126-
});
12789
}

fable/src/fable/schema/number_test.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ using namespace std; // NOLINT(build/namespaces)
2626

2727
#include <gtest/gtest.h>
2828

29-
#include <fable/confable.hpp> // for Confable
30-
#include <fable/schema.hpp> // for Number
29+
#include <fable/confable.hpp> // for Confable
30+
#include <fable/schema.hpp> // for Number
31+
#include <fable/utility/gtest.hpp> // for assert_validate
3132

3233
struct MyNumberStruct : public fable::Confable {
3334
uint8_t number;
@@ -39,13 +40,9 @@ struct MyNumberStruct : public fable::Confable {
3940
}
4041
};
4142

42-
inline void assert_json_eq(const fable::Json& j, const fable::Json& k) {
43-
ASSERT_EQ(std::string(j.dump()), std::string(k.dump()));
44-
}
45-
4643
TEST(fable_schema_number, schema) {
4744
MyNumberStruct tmp;
48-
fable::Json expect = R"({
45+
fable::assert_schema_eq(tmp, R"({
4946
"type": "object",
5047
"properties": {
5148
"number": {
@@ -59,23 +56,23 @@ TEST(fable_schema_number, schema) {
5956
}
6057
},
6158
"additionalProperties": false
62-
})"_json;
63-
64-
assert_json_eq(tmp.schema().json_schema(), expect);
59+
})");
6560
}
6661

6762
TEST(fable_schema_number, validate) {
6863
MyNumberStruct tmp;
6964

7065
std::vector<uint8_t> ok{0, 1, 2, 3, 4, 5, 6, 7, 15};
7166
for (auto x : ok) {
72-
fable::Conf c{fable::Json{{"number", x}}};
73-
ASSERT_NO_THROW(tmp.schema().validate(c));
67+
fable::assert_validate(tmp, fable::Json{
68+
{"number", x},
69+
});
7470
};
7571

7672
std::vector<uint8_t> wrong{8, 16, 32, 255};
7773
for (auto x : wrong) {
78-
fable::Conf c{fable::Json{{"number", x}}};
79-
ASSERT_ANY_THROW(tmp.schema().validate(c));
74+
fable::assert_invalidate(tmp, fable::Json{
75+
{"number", x},
76+
});
8077
}
8178
}

0 commit comments

Comments
 (0)