Skip to content

Commit

Permalink
fix: printing of temporaries
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Oct 3, 2024
1 parent 76dde7f commit 65e56ab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/mimic++/Printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,20 @@ namespace mimicpp::detail
template <print_iterator OutIter, format::detail::formattable<CharT> T>
OutIter print(
OutIter out,
T& value,
T&& value,
[[maybe_unused]] const priority_tag<1>
)
{
return format::format_to(
std::move(out),
"{}",
value);
std::forward<T>(value));
}

template <print_iterator OutIter>
OutIter print(
OutIter out,
auto&,
auto&&,
[[maybe_unused]] const priority_tag<0>
)
{
Expand Down
38 changes: 38 additions & 0 deletions test/unit-tests/Printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

#include <ranges>
#include <sstream>

using namespace mimicpp;
Expand Down Expand Up @@ -384,6 +385,19 @@ TEST_CASE(
std::move(stream).str(),
Catch::Matchers::Equals("{ {?}, {?}, {?} }"));
}

SECTION("Views are supported.")
{
const std::vector vec{42, 1337};

print(
std::ostreambuf_iterator{stream},
vec
| std::views::transform([](const auto v) { return 2 * v; }));
REQUIRE_THAT(
std::move(stream).str(),
Catch::Matchers::Equals("{ 84, 2674 }"));
}
}

SECTION("std::source_location has specialized printer.")
Expand All @@ -405,6 +419,30 @@ TEST_CASE(
}
}

TEST_CASE(
"print supports printing of temporaries.",
"[print]"
)
{
SECTION("Something printable.")
{
StringStreamT stream{};
print(std::ostreambuf_iterator{stream}, 42);
REQUIRE_THAT(
std::move(stream).str(),
Catch::Matchers::Equals("42"));
}

SECTION("Something non-printable.")
{
StringStreamT stream{};
print(std::ostreambuf_iterator{stream}, NonPrintable{});
REQUIRE_THAT(
std::move(stream).str(),
Catch::Matchers::Equals("{?}"));
}
}

TEST_CASE(
"ValueCategory is formattable.",
"[print]"
Expand Down

0 comments on commit 65e56ab

Please sign in to comment.