|
2 | 2 | #include <msg/message.hpp>
|
3 | 3 |
|
4 | 4 | #include <catch2/catch_test_macros.hpp>
|
| 5 | +#include <catch2/matchers/catch_matchers_templated.hpp> |
5 | 6 |
|
6 | 7 | #include <algorithm>
|
7 | 8 | #include <array>
|
@@ -500,3 +501,40 @@ TEST_CASE("message equivalence (views)", "[message]") {
|
500 | 501 | auto cv2 = other.as_const_view();
|
501 | 502 | CHECK(not equivalent(cv1, cv2));
|
502 | 503 | }
|
| 504 | + |
| 505 | +namespace { |
| 506 | +template <typename View> |
| 507 | +struct MsgEquivMatcher : Catch::Matchers::MatcherGenericBase { |
| 508 | + MsgEquivMatcher(View v) : view{v} {} |
| 509 | + |
| 510 | + template <typename OtherMsg> bool match(OtherMsg const &other) const { |
| 511 | + return msg::equivalent(view, other); |
| 512 | + } |
| 513 | + |
| 514 | + std::string describe() const override { |
| 515 | + return "Equivalent: " + Catch::rangeToString(view.data()); |
| 516 | + } |
| 517 | + |
| 518 | + private: |
| 519 | + View view; |
| 520 | +}; |
| 521 | + |
| 522 | +template <typename Msg> |
| 523 | +auto MsgEquiv(Msg const &msg) -> MsgEquivMatcher<typename Msg::const_view_t> { |
| 524 | + return {msg.as_const_view()}; |
| 525 | +} |
| 526 | +} // namespace |
| 527 | + |
| 528 | +TEST_CASE("message equivalence matcher", "[message]") { |
| 529 | + owning<msg_defn> m{"f1"_field = 0xba11, "f2"_field = 0x42, |
| 530 | + "f3"_field = 0xd00d}; |
| 531 | + auto cv1 = m.as_const_view(); |
| 532 | + auto mv1 = m.as_mutable_view(); |
| 533 | + |
| 534 | + CHECK_THAT(cv1, MsgEquiv(m)); |
| 535 | + CHECK_THAT(mv1, MsgEquiv(m)); |
| 536 | + |
| 537 | + owning<msg_defn> m2{"f1"_field = 0xba12, "f2"_field = 0x42, |
| 538 | + "f3"_field = 0xd00d}; |
| 539 | + CHECK_THAT(m2, not MsgEquiv(m)); |
| 540 | +} |
0 commit comments