mirror of
https://github.com/fmtlib/fmt.git
synced 2025-04-01 10:20:59 +00:00
Fix warnings.
This commit is contained in:
parent
83e1658109
commit
494f53421e
@ -1587,7 +1587,7 @@ TEST(FormatterTest, FormatExamples) {
|
|||||||
|
|
||||||
EXPECT_THROW({
|
EXPECT_THROW({
|
||||||
const char *filename = "nonexistent";
|
const char *filename = "nonexistent";
|
||||||
FILE *f = fopen(filename, "r");
|
FILE *f = std::fopen(filename, "r");
|
||||||
if (!f)
|
if (!f)
|
||||||
fmt::ThrowSystemError(errno, "Cannot open file '{}'") << filename;
|
fmt::ThrowSystemError(errno, "Cannot open file '{}'") << filename;
|
||||||
}, fmt::SystemError);
|
}, fmt::SystemError);
|
||||||
@ -1669,26 +1669,34 @@ TEST(FormatterTest, OutputNotWrittenOnError) {
|
|||||||
EXPECT_EQ(0, num_writes);
|
EXPECT_EQ(0, num_writes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if FMT_USE_FILE_DESCRIPTORS
|
||||||
|
|
||||||
TEST(FormatterTest, FileSink) {
|
TEST(FormatterTest, FileSink) {
|
||||||
FILE *f = std::fopen("out", "w");
|
File read_end, write_end;
|
||||||
fmt::FileSink fs(f);
|
File::pipe(read_end, write_end);
|
||||||
fs(Writer() << "test");
|
BufferedFile f = write_end.fdopen("w");
|
||||||
std::fclose(f);
|
EXPECT_WRITE(f.get(), {
|
||||||
EXPECT_EQ("test", ReadFile("out"));
|
fmt::FileSink fs(f.get());
|
||||||
|
fs(Writer() << "test");
|
||||||
|
}, "test");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, FileSinkWriteError) {
|
TEST(FormatterTest, FileSinkWriteError) {
|
||||||
FILE *f = std::fopen("out", "r");
|
File read_end, write_end;
|
||||||
fmt::FileSink fs(f);
|
File::pipe(read_end, write_end);
|
||||||
int result = std::fwrite(" ", 1, 1, f);
|
BufferedFile f = read_end.fdopen("r");
|
||||||
|
fmt::FileSink fs(f.get());
|
||||||
|
int result = std::fwrite(" ", 1, 1, f.get());
|
||||||
int error_code = errno;
|
int error_code = errno;
|
||||||
EXPECT_EQ(0, result);
|
EXPECT_EQ(0, result);
|
||||||
std::string error_message =
|
EXPECT_SYSTEM_ERROR(
|
||||||
str(Format("{}: {}") << "cannot write to file" << strerror(error_code));
|
fs(Writer() << "test"), error_code, "cannot write to file");
|
||||||
EXPECT_THROW_MSG(fs(Writer() << "test"), fmt::SystemError, error_message);
|
|
||||||
std::fclose(f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
# pragma message "warning: some tests are disabled"
|
||||||
|
#endif
|
||||||
|
|
||||||
// The test doesn't compile on older compilers which follow C++03 and
|
// The test doesn't compile on older compilers which follow C++03 and
|
||||||
// require an accessible copy constructor when binding a temporary to
|
// require an accessible copy constructor when binding a temporary to
|
||||||
// a const reference.
|
// a const reference.
|
||||||
@ -1812,7 +1820,7 @@ TEST(FormatIntTest, FormatDec) {
|
|||||||
EXPECT_EQ("42", FormatDec(42ull));
|
EXPECT_EQ("42", FormatDec(42ull));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef FMT_USE_FILE_DESCRIPTORS
|
#if FMT_USE_FILE_DESCRIPTORS
|
||||||
|
|
||||||
TEST(FormatTest, PrintColored) {
|
TEST(FormatTest, PrintColored) {
|
||||||
EXPECT_WRITE(stdout, fmt::PrintColored(fmt::RED, "Hello, {}!\n") << "world",
|
EXPECT_WRITE(stdout, fmt::PrintColored(fmt::RED, "Hello, {}!\n") << "world",
|
||||||
|
@ -39,12 +39,6 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
std::string FormatSystemErrorMessage(int error_code, fmt::StringRef message) {
|
|
||||||
fmt::Writer out;
|
|
||||||
fmt::internal::FormatSystemErrorMessage(out, error_code, message);
|
|
||||||
return str(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Suppresses Windows assertions on invalid file descriptors, making
|
// Suppresses Windows assertions on invalid file descriptors, making
|
||||||
// POSIX functions return proper error codes instead of crashing on Windows.
|
// POSIX functions return proper error codes instead of crashing on Windows.
|
||||||
class SuppressAssert {
|
class SuppressAssert {
|
||||||
@ -70,26 +64,9 @@ class SuppressAssert {
|
|||||||
|
|
||||||
#define SUPPRESS_ASSERT(statement) { SuppressAssert sa; statement; }
|
#define SUPPRESS_ASSERT(statement) { SuppressAssert sa; statement; }
|
||||||
|
|
||||||
#define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
|
|
||||||
EXPECT_THROW_MSG(statement, fmt::SystemError, \
|
|
||||||
FormatSystemErrorMessage(error_code, message))
|
|
||||||
|
|
||||||
#define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
|
#define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
|
||||||
EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
|
EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
|
||||||
|
|
||||||
// Checks if the file is open by reading one character from it.
|
|
||||||
bool IsOpen(int fd) {
|
|
||||||
char buffer;
|
|
||||||
return FMT_POSIX(read(fd, &buffer, 1)) == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsClosed(int fd) {
|
|
||||||
char buffer;
|
|
||||||
std::streamsize result = 0;
|
|
||||||
SUPPRESS_ASSERT(result = FMT_POSIX(read(fd, &buffer, 1)));
|
|
||||||
return result == -1 && errno == EBADF;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tests that assertion macros evaluate their arguments exactly once.
|
// Tests that assertion macros evaluate their arguments exactly once.
|
||||||
class SingleEvaluationTest : public ::testing::Test {
|
class SingleEvaluationTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
@ -265,6 +242,19 @@ TEST(StreamingAssertionsTest, EXPECT_WRITE) {
|
|||||||
|
|
||||||
#if FMT_USE_FILE_DESCRIPTORS
|
#if FMT_USE_FILE_DESCRIPTORS
|
||||||
|
|
||||||
|
// Checks if the file is open by reading one character from it.
|
||||||
|
bool IsOpen(int fd) {
|
||||||
|
char buffer;
|
||||||
|
return FMT_POSIX(read(fd, &buffer, 1)) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsClosed(int fd) {
|
||||||
|
char buffer;
|
||||||
|
std::streamsize result = 0;
|
||||||
|
SUPPRESS_ASSERT(result = FMT_POSIX(read(fd, &buffer, 1)));
|
||||||
|
return result == -1 && errno == EBADF;
|
||||||
|
}
|
||||||
|
|
||||||
TEST(ErrorCodeTest, Ctor) {
|
TEST(ErrorCodeTest, Ctor) {
|
||||||
EXPECT_EQ(0, ErrorCode().get());
|
EXPECT_EQ(0, ErrorCode().get());
|
||||||
EXPECT_EQ(42, ErrorCode(42).get());
|
EXPECT_EQ(42, ErrorCode(42).get());
|
||||||
@ -671,6 +661,7 @@ TEST(OutputRedirectTest, ErrorInDtor) {
|
|||||||
write_dup.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail
|
write_dup.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: test EXPECT_SYSTEM_ERROR
|
||||||
// TODO: test retry on EINTR
|
// TODO: test retry on EINTR
|
||||||
|
|
||||||
#endif // FMT_USE_FILE_DESCRIPTORS
|
#endif // FMT_USE_FILE_DESCRIPTORS
|
||||||
|
@ -78,7 +78,7 @@ File::File(const char *path, int oflag) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
File::~File() FMT_NOEXCEPT(true) {
|
File::~File() FMT_NOEXCEPT(true) {
|
||||||
// Don't need to retry close in case of EINTR.
|
// Don't retry close in case of EINTR!
|
||||||
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
|
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
|
||||||
if (fd_ != -1 && ::FMT_POSIX(close(fd_)) != 0)
|
if (fd_ != -1 && ::FMT_POSIX(close(fd_)) != 0)
|
||||||
fmt::ReportSystemError(errno, "cannot close file");
|
fmt::ReportSystemError(errno, "cannot close file");
|
||||||
@ -158,7 +158,7 @@ void File::pipe(File &read_end, File &write_end) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BufferedFile File::fdopen(const char *mode) {
|
BufferedFile File::fdopen(const char *mode) {
|
||||||
BufferedFile f(::fdopen(fd_, mode));
|
BufferedFile f(::FMT_POSIX(fdopen(fd_, mode)));
|
||||||
fd_ = -1;
|
fd_ = -1;
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@ -222,3 +222,9 @@ std::string OutputRedirect::RestoreAndRead() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif // FMT_USE_FILE_DESCRIPTORS
|
#endif // FMT_USE_FILE_DESCRIPTORS
|
||||||
|
|
||||||
|
std::string FormatSystemErrorMessage(int error_code, fmt::StringRef message) {
|
||||||
|
fmt::Writer out;
|
||||||
|
fmt::internal::FormatSystemErrorMessage(out, error_code, message);
|
||||||
|
return str(out);
|
||||||
|
}
|
||||||
|
@ -81,6 +81,12 @@
|
|||||||
FMT_TEST_THROW_(statement, expected_exception, \
|
FMT_TEST_THROW_(statement, expected_exception, \
|
||||||
expected_message, GTEST_NONFATAL_FAILURE_)
|
expected_message, GTEST_NONFATAL_FAILURE_)
|
||||||
|
|
||||||
|
std::string FormatSystemErrorMessage(int error_code, fmt::StringRef message);
|
||||||
|
|
||||||
|
#define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
|
||||||
|
EXPECT_THROW_MSG(statement, fmt::SystemError, \
|
||||||
|
FormatSystemErrorMessage(error_code, message))
|
||||||
|
|
||||||
#ifndef FMT_USE_FILE_DESCRIPTORS
|
#ifndef FMT_USE_FILE_DESCRIPTORS
|
||||||
# define FMT_USE_FILE_DESCRIPTORS 0
|
# define FMT_USE_FILE_DESCRIPTORS 0
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user