Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 1 | #ifndef AOS_TESTING_FLATBUFFER_EQ_H_ |
| 2 | #define AOS_TESTING_FLATBUFFER_EQ_H_ |
| 3 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 4 | #include "gmock/gmock.h" |
| 5 | |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 6 | #include "aos/flatbuffer_merge.h" |
| 7 | #include "aos/flatbuffers.h" |
| 8 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 9 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 10 | namespace aos::testing { |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 11 | |
Brian Silverman | 497838d | 2021-02-17 21:39:50 -0800 | [diff] [blame] | 12 | // Use FlatbufferUnwrapped to instantiate this. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 13 | template <typename T> |
Brian Silverman | 497838d | 2021-02-17 21:39:50 -0800 | [diff] [blame] | 14 | class FlatbufferUnwrappedMatcher { |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 15 | public: |
Brian Silverman | 497838d | 2021-02-17 21:39:50 -0800 | [diff] [blame] | 16 | FlatbufferUnwrappedMatcher(::testing::Matcher<const T *> matcher) |
| 17 | : matcher_(std::move(matcher)) {} |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 18 | |
| 19 | bool MatchAndExplain(const T *t, |
| 20 | ::testing::MatchResultListener *listener) const { |
Brian Silverman | 497838d | 2021-02-17 21:39:50 -0800 | [diff] [blame] | 21 | return matcher_.MatchAndExplain(t, listener); |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | bool MatchAndExplain(const aos::Flatbuffer<T> &t, |
| 25 | ::testing::MatchResultListener *listener) const { |
| 26 | return MatchAndExplain(&t.message(), listener); |
| 27 | } |
| 28 | |
Brian Silverman | 497838d | 2021-02-17 21:39:50 -0800 | [diff] [blame] | 29 | void DescribeTo(std::ostream *os) const { matcher_.DescribeTo(os); } |
| 30 | |
| 31 | void DescribeNegationTo(std::ostream *os) const { |
| 32 | matcher_.DescribeNegationTo(os); |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | const ::testing::Matcher<const T *> matcher_; |
| 37 | }; |
| 38 | |
| 39 | // Returns a googlemock matcher which will compare a `const T *` or a `const |
| 40 | // aos::Flatbuffer<T> &` against another matcher which only handles `const T *`. |
| 41 | // This will automatically propagate the nice error messages. |
| 42 | // |
| 43 | // T must be a flatbuffer table type. |
| 44 | template <typename T> |
| 45 | inline auto FlatbufferUnwrapped(::testing::Matcher<const T *> matcher) { |
| 46 | return ::testing::MakePolymorphicMatcher( |
| 47 | FlatbufferUnwrappedMatcher(std::move(matcher))); |
| 48 | } |
| 49 | |
| 50 | // Use FlatbufferEq to instantiate this. |
| 51 | template <typename T> |
| 52 | class FlatbufferEqMatcher : public ::testing::MatcherInterface<const T *> { |
| 53 | public: |
Austin Schuh | 2bc3162 | 2021-11-19 13:34:17 -0800 | [diff] [blame] | 54 | FlatbufferEqMatcher(aos::FlatbufferVector<T> expected) |
Brian Silverman | 497838d | 2021-02-17 21:39:50 -0800 | [diff] [blame] | 55 | : expected_(std::move(expected)) {} |
| 56 | ~FlatbufferEqMatcher() override = default; |
| 57 | |
| 58 | bool MatchAndExplain( |
| 59 | const T *t, ::testing::MatchResultListener *listener) const override { |
| 60 | *listener << "is " << aos::FlatbufferToJson(t); |
| 61 | return aos::CompareFlatBuffer(t, &expected_.message()); |
| 62 | } |
| 63 | |
| 64 | void DescribeTo(std::ostream *os) const override { |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 65 | *os << "is equal to " << aos::FlatbufferToJson(&expected_.message()); |
| 66 | } |
| 67 | |
Brian Silverman | 497838d | 2021-02-17 21:39:50 -0800 | [diff] [blame] | 68 | void DescribeNegationTo(std::ostream *os) const override { |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 69 | *os << "is not equal to " << aos::FlatbufferToJson(&expected_.message()); |
| 70 | } |
| 71 | |
| 72 | private: |
Austin Schuh | 2bc3162 | 2021-11-19 13:34:17 -0800 | [diff] [blame] | 73 | const aos::FlatbufferVector<T> expected_; |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | // Returns a googlemock matcher which will compare a `const T *` or a `const |
| 77 | // aos::Flatbuffer<T> &` against expected. This will automatically give nice |
| 78 | // error messages if they don't match. |
| 79 | // |
| 80 | // T must be a flatbuffer table type. |
| 81 | template <typename T> |
Brian Silverman | 497838d | 2021-02-17 21:39:50 -0800 | [diff] [blame] | 82 | inline auto FlatbufferEq(const aos::NonSizePrefixedFlatbuffer<T> &expected) { |
| 83 | return FlatbufferUnwrapped(::testing::MakeMatcher( |
Austin Schuh | 2bc3162 | 2021-11-19 13:34:17 -0800 | [diff] [blame] | 84 | new FlatbufferEqMatcher(aos::FlatbufferVector<T>(expected)))); |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 87 | } // namespace aos::testing |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 88 | |
| 89 | #endif // AOS_TESTING_FLATBUFFER_EQ_H_ |