catchorg / catchorg/Catch2

Exposing `BinaryExpr` members

Open
#2,982 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

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` 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`](https://github.com/catchorg/Catch2/blob/74fcff6e5b190fb833a231b7f7c1829e3c3ac54d/src/catch2/internal/catch_decomposer.hpp#L186) these getters:
```cpp
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 const&`).

Would you be open to that addition?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.