Skip to content

Commit

Permalink
fable: Extend gtest.hpp utility header
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed Mar 31, 2023
1 parent 90bbf58 commit 1a97427
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions fable/include/fable/utility/gtest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,52 @@

namespace fable {

/**
* Assert that both JSON values are identical.
*
* This function dumps both JSON with indentation so that failing
* tests can nicely format the diff between strings.
*/
inline void assert_eq(const Json& j, const Json& k) {
ASSERT_EQ(std::string(j.dump(2)), std::string(k.dump(2)));
}

/**
* Assert that both JSON values are identical.
*
* The second parameter is parsed to JSON and then dumped.
*
* This function dumps both JSON with indentation so that failing
* tests can nicely format the diff between strings.
*/
inline void assert_eq(const Json& j, const char expect[]) {
assert_eq(j, parse_json(expect));
}

/**
* Assert that both JSON values are NOT identical.
*/
inline void assert_ne(const Json& j, const Json& k) {
ASSERT_NE(std::string(j.dump(2)), std::string(k.dump(2)));
}

/**
* Assert that both JSON values are NOT identical.
*
* The second parameter is parsed to JSON and then dumped.
*/
inline void assert_ne(const Json& j, const char expect[]) {
assert_ne(j, parse_json(expect));
}

inline void assert_schema_eq(const Schema& s, const Json& expect) {
assert_eq(s.json_schema(), expect);
}

inline void assert_schema_eq(const Schema& s, const char expect[]) {
assert_eq(s.json_schema(), parse_json(expect));
}

inline void assert_schema_eq(const Confable& x, const Json& expect) {
assert_eq(x.schema().json_schema(), expect);
}
Expand All @@ -59,32 +89,56 @@ inline void assert_schema_eq(const Confable& x, const char expect[]) {
assert_eq(x.schema().json_schema(), parse_json(expect));
}

inline void assert_validate(const Confable& x, const Conf& input) {
inline void assert_validate(const Schema& s, const Conf& input) {
try {
x.schema().validate(input);
s.validate(input);
} catch (SchemaError& e) {
pretty_print(e, std::cerr);
throw;
};
}

inline void assert_validate(const Schema& s, const char json_input[]) {
assert_validate(s, Conf{parse_json(json_input)});
}

inline void assert_validate(const Confable& x, const Conf& input) {
assert_validate(x.schema(), input);
}

inline void assert_validate(const Confable& x, const char json_input[]) {
assert_validate(x, Conf{parse_json(json_input)});
assert_validate(x.schema(), Conf{parse_json(json_input)});
}

inline void assert_invalidate(const Schema& s, const Conf& input) {
ASSERT_THROW(s.validate(input), SchemaError);
}

inline void assert_invalidate(const Schema& s, const char json_input[]) {
ASSERT_THROW(s.validate(Conf{parse_json(json_input)}), SchemaError);
}

inline void assert_invalidate(const Confable& x, const Conf& input) {
ASSERT_THROW(x.schema().validate(input), SchemaError);
assert_invalidate(x.schema(), input);
}

inline void assert_invalidate(const Confable& x, const char json_input[]) {
assert_invalidate(x, Conf{parse_json(json_input)});
}

/**
* Assert that the serialization is equal to the expected JSON.
*/
template <typename T>
inline void assert_to_json(const T& x, const Json& expect) {
assert_eq(x.to_json(), expect);
}

/**
* Assert that the serialization is equal to the expected JSON string.
*
* The string is parsed to Json, so that field order is not important.
*/
template <typename T>
inline void assert_to_json(const T& x, const char json_expect[]) {
assert_to_json(x, parse_json(json_expect));
Expand All @@ -110,12 +164,24 @@ inline void assert_from_conf(T& x, const char json_input[]) {
assert_from_conf(x, Conf{parse_json(json_input)});
}

/**
* Assert that deserializing the input works and serializes to the same input.
*
* This asserts that the type supports the identity function. This does not need
* to hold for any type, but you may want it to hold for your type.
*/
template <typename T>
inline void assert_from_eq_to(T& x, const Json& identity) {
assert_from_conf(x, Conf{identity});
assert_to_json(x, identity);
}

/**
* Assert that deserializing the input works and serializes to the same input.
*
* This asserts that the type supports the identity function. This does not need
* to hold for any type, but you may want it to hold for your type.
*/
template <typename T>
inline void assert_from_eq_to(T& x, const char json_input[]) {
assert_from_eq_to(x, parse_json(json_input));
Expand Down

0 comments on commit 1a97427

Please sign in to comment.