Throw SystemError if fwrite fails.

This commit is contained in:
Victor Zverovich
2014-04-30 06:55:21 -07:00
parent 4e33311597
commit 859a4975f6
3 changed files with 182 additions and 65 deletions
+13 -1
View File
@@ -100,7 +100,7 @@ using fmt::pad;
} \
catch (expected_exception const& e) { \
gtest_caught_expected = true; \
if (std::strcmp(message, e.what()) != 0) \
if (std::string(message) != e.what()) \
throw; \
} \
catch (...) { \
@@ -1561,6 +1561,18 @@ TEST(FormatterTest, FileSink) {
EXPECT_EQ("test", ReadFile("out"));
}
TEST(FormatterTest, FileSinkWriteError) {
FILE *f = std::fopen("out", "r");
fmt::FileSink fs(f);
int result = std::fwrite(" ", 1, 1, f);
int error_code = errno;
EXPECT_EQ(0, result);
std::string error_message =
str(Format("{}: {}") << "cannot write to file" << strerror(error_code));
EXPECT_THROW_MSG(fs(Writer() << "test"), fmt::SystemError, error_message);
std::fclose(f);
}
// The test doesn't compile on older compilers which follow C++03 and
// require an accessible copy constructor when binding a temporary to
// a const reference.