From 8f511fc12f62fa4ef8995b67a51849550c4f127f Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 6 May 2020 17:15:46 -0700 Subject: [PATCH] Make copyfmt not throw (#1666) --- include/fmt/ostream.h | 2 +- test/ostream-test.cc | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 83c37486..6c7d69b6 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -101,8 +101,8 @@ void format_value(buffer& buf, const T& value, #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) if (loc) output.imbue(loc.get()); #endif - output.exceptions(std::ios_base::failbit | std::ios_base::badbit); output << value; + output.exceptions(std::ios_base::failbit | std::ios_base::badbit); buf.resize(buf.size()); } diff --git a/test/ostream-test.cc b/test/ostream-test.cc index d9329345..0fc5ead4 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -21,9 +21,9 @@ template <> struct formatter : formatter { }; } // namespace fmt -#include "fmt/ostream.h" - #include + +#include "fmt/ostream.h" #include "gmock.h" #include "gtest-extra.h" #include "util.h" @@ -269,7 +269,7 @@ std::ostream& operator<<(std::ostream& os, return os << "bar"; } -TEST(FormatterTest, FormatExplicitlyConvertibleToStringLike) { +TEST(OStreamTest, FormatExplicitlyConvertibleToStringLike) { EXPECT_EQ("bar", fmt::format("{}", explicitly_convertible_to_string_like())); } @@ -285,8 +285,20 @@ std::ostream& operator<<(std::ostream& os, return os << "bar"; } -TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringView) { +TEST(OStreamTest, FormatExplicitlyConvertibleToStdStringView) { EXPECT_EQ("bar", fmt::format("{}", explicitly_convertible_to_string_like())); } #endif // FMT_USE_STRING_VIEW + +struct copyfmt_test {}; + +std::ostream& operator<<(std::ostream& os, copyfmt_test) { + std::ios ios(nullptr); + ios.copyfmt(os); + return os << "foo"; +} + +TEST(OStreamTest, CopyFmt) { + EXPECT_EQ("foo", fmt::format("{}", copyfmt_test())); +}