Skip to content

Exposing BinaryExpr members #2982

Open
Open
@brevzin

Description

@brevzin

Currently, Catch only provides string versions of the left- and right-hand side when tests fail. For instance, something like:

  CHECK( v1 == v2 )
with expansion:
  { 1, 2, 3 } == { 1, 3, 4 }

This is a good start, but it isn't enough when comparing more complicated structures, where you want some more information. pytest, for instance, will print a full diff. So you'll get something more like:

  CHECK( v1 == v2 )
with expansion:
  { 1, 2, 3 } == { 1, 3, 4 }
  
  At index 1 diff: 2 != 3
  
  Full diff:
    [
        1,
  -     2,
        3,
  +     4,
    ]

Which is a lot more useful. There doesn't seem to be any way to provide more specific messages for errors. The Matcher API has no relevant hook for this.

We looked into trying to do this on our own — BinaryExpr<T, U> has the T and the U, but they are both private members with no accessors. So in order to attempt to implement something like the above, we would have to either (a) reimplement/copy the Decomposer logic, which is involved or (b) do shenanigans relying on just knowing where in BinaryExpr the T and the U are and just yolo-reinterpret-casting at specific memory locations. Neither of which I actually want to do.

It would be easier to simply add to BinaryExpr these getters:

constexpr auto lhs() const -> auto const& { return m_lhs; }
constexpr auto rhs() const -> auto const& { return m_rhs; }
constexpr auto op() const -> StringRef { return m_op; }

(Returning auto const& instead of LhsT const& since LhsT could be some T& and I'd want to unconditionally return T const& there, and auto const& is just an easier way to achieve that than something like std::remove_reference_t<LhsT> const&).

Would you be open to that addition?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions