Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
petr-polezhaev opened this issue Feb 20, 2023 · 0 comments
Open

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

petr-polezhaev opened this issue Feb 20, 2023 · 0 comments

Comments

@petr-polezhaev
Copy link

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<U> 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<U&> 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 <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_range_equals.hpp>

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 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant