blob: 2f7b3453b7b361a1bc7c4849aea3285413fe18f2 [file] [log] [blame]
Austin Schuh1ef01ef2021-02-07 20:40:36 -08001#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
9namespace aos {
10namespace testing {
11
Brian Silverman497838d2021-02-17 21:39:50 -080012// Use FlatbufferUnwrapped to instantiate this.
Austin Schuh1ef01ef2021-02-07 20:40:36 -080013template <typename T>
Brian Silverman497838d2021-02-17 21:39:50 -080014class FlatbufferUnwrappedMatcher {
Austin Schuh1ef01ef2021-02-07 20:40:36 -080015 public:
Brian Silverman497838d2021-02-17 21:39:50 -080016 FlatbufferUnwrappedMatcher(::testing::Matcher<const T *> matcher)
17 : matcher_(std::move(matcher)) {}
Austin Schuh1ef01ef2021-02-07 20:40:36 -080018
19 bool MatchAndExplain(const T *t,
20 ::testing::MatchResultListener *listener) const {
Brian Silverman497838d2021-02-17 21:39:50 -080021 return matcher_.MatchAndExplain(t, listener);
Austin Schuh1ef01ef2021-02-07 20:40:36 -080022 }
23
24 bool MatchAndExplain(const aos::Flatbuffer<T> &t,
25 ::testing::MatchResultListener *listener) const {
26 return MatchAndExplain(&t.message(), listener);
27 }
28
Brian Silverman497838d2021-02-17 21:39:50 -080029 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.
44template <typename T>
45inline auto FlatbufferUnwrapped(::testing::Matcher<const T *> matcher) {
46 return ::testing::MakePolymorphicMatcher(
47 FlatbufferUnwrappedMatcher(std::move(matcher)));
48}
49
50// Use FlatbufferEq to instantiate this.
51template <typename T>
52class FlatbufferEqMatcher : public ::testing::MatcherInterface<const T *> {
53 public:
Austin Schuh2bc31622021-11-19 13:34:17 -080054 FlatbufferEqMatcher(aos::FlatbufferVector<T> expected)
Brian Silverman497838d2021-02-17 21:39:50 -080055 : 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 Schuh1ef01ef2021-02-07 20:40:36 -080065 *os << "is equal to " << aos::FlatbufferToJson(&expected_.message());
66 }
67
Brian Silverman497838d2021-02-17 21:39:50 -080068 void DescribeNegationTo(std::ostream *os) const override {
Austin Schuh1ef01ef2021-02-07 20:40:36 -080069 *os << "is not equal to " << aos::FlatbufferToJson(&expected_.message());
70 }
71
72 private:
Austin Schuh2bc31622021-11-19 13:34:17 -080073 const aos::FlatbufferVector<T> expected_;
Austin Schuh1ef01ef2021-02-07 20:40:36 -080074};
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.
81template <typename T>
Brian Silverman497838d2021-02-17 21:39:50 -080082inline auto FlatbufferEq(const aos::NonSizePrefixedFlatbuffer<T> &expected) {
83 return FlatbufferUnwrapped(::testing::MakeMatcher(
Austin Schuh2bc31622021-11-19 13:34:17 -080084 new FlatbufferEqMatcher(aos::FlatbufferVector<T>(expected))));
Austin Schuh1ef01ef2021-02-07 20:40:36 -080085}
86
87} // namespace testing
88} // namespace aos
89
90#endif // AOS_TESTING_FLATBUFFER_EQ_H_