catchorg / catchorg/Catch2

CHECK_THAT fails to stringify a range with std::views::filter

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

Description

`std::ranges::filter_view` has only non-const `begin`/`end` as it writes the filtered value into the view object (per standard requirements), yet the `Catch::stringify` and `Catch::StringMaker...::convert` both accept const-references only and `IsStreamInsertable` tests for `std::declval` only (rvalue) with `const T&` as argument

To fix this I had to:
1. Change `stringify` to universal reference and use forward
2. Move `IsStreamInsertable` constness to the template argument (specified by callers)
3. Use `std::declval` in `IsStreamInsertable` is `is_range_impl`
4. Add non-const overloads to `StringMaker` (normal and range specialization)
5. Add a non-const overload to `ReusableStringStream::operator<<`
6. Remove const from `rangeToString`

Test case:
```
#include
#include

TEST_CASE("std::views::filter")
{
int const ints[] = {1, 2, 3};
auto range = ints | std::views::filter([](int const x) { return x % 2 == 0; });
int const expected[] = {3}; // forcing the error output
CHECK_THAT(range, Catch::Matchers::RangeEquals(expected));
}
```
https://gcc.godbolt.org/z/4EexKf1d5

Expected:

```
CHECK_THAT( range, Catch::Matchers::RangeEquals(expected) )
with expansion:
{ 2 } elements are { 3 }
```

Actually get:
```
CHECK_THAT( range, Catch::Matchers::RangeEquals(expected) )
with expansion:
{ ? } elements are { 3 }
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the std::views::filter case from the issue and inspect Catch::stringify, IsStreamInsertable, StringMaker, ReusableStringStream::operator<<, and rangeToString. Trace how the range is formatted and add a regression test for the reported CHECK_THAT output. Done means the filtered range reports { 2 } rather than { ? }.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.