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 | |
| 4 | #include "aos/flatbuffer_merge.h" |
| 5 | #include "aos/flatbuffers.h" |
| 6 | #include "aos/json_to_flatbuffer.h" |
| 7 | #include "gmock/gmock.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
| 12 | // Use FlatbufferEq to instantiate this. |
| 13 | template <typename T> |
| 14 | class FlatbufferEqMatcher { |
| 15 | public: |
| 16 | FlatbufferEqMatcher(aos::FlatbufferString<T> expected) |
| 17 | : expected_(std::move(expected)) {} |
| 18 | |
| 19 | bool MatchAndExplain(const T *t, |
| 20 | ::testing::MatchResultListener *listener) const { |
| 21 | *listener << "is " << aos::FlatbufferToJson(t); |
| 22 | return aos::CompareFlatBuffer(t, &expected_.message()); |
| 23 | } |
| 24 | |
| 25 | bool MatchAndExplain(const aos::Flatbuffer<T> &t, |
| 26 | ::testing::MatchResultListener *listener) const { |
| 27 | return MatchAndExplain(&t.message(), listener); |
| 28 | } |
| 29 | |
| 30 | void DescribeTo(std::ostream *os) const { |
| 31 | *os << "is equal to " << aos::FlatbufferToJson(&expected_.message()); |
| 32 | } |
| 33 | |
| 34 | void DescribeNegationTo(std::ostream *os) const { |
| 35 | *os << "is not equal to " << aos::FlatbufferToJson(&expected_.message()); |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | const aos::FlatbufferString<T> expected_; |
| 40 | }; |
| 41 | |
| 42 | // Returns a googlemock matcher which will compare a `const T *` or a `const |
| 43 | // aos::Flatbuffer<T> &` against expected. This will automatically give nice |
| 44 | // error messages if they don't match. |
| 45 | // |
| 46 | // T must be a flatbuffer table type. |
| 47 | template <typename T> |
| 48 | ::testing::PolymorphicMatcher<FlatbufferEqMatcher<T>> FlatbufferEq( |
| 49 | const aos::NonSizePrefixedFlatbuffer<T> &expected) { |
| 50 | return ::testing::MakePolymorphicMatcher( |
| 51 | FlatbufferEqMatcher(aos::FlatbufferString<T>(expected))); |
| 52 | } |
| 53 | |
| 54 | } // namespace testing |
| 55 | } // namespace aos |
| 56 | |
| 57 | #endif // AOS_TESTING_FLATBUFFER_EQ_H_ |