UT stub for extra methods in BufferedFile

This commit is contained in:
carterl 2015-02-13 08:00:01 +01:00
parent 79adad5e33
commit e3ba8d9530
4 changed files with 41 additions and 18 deletions

View File

@ -105,12 +105,29 @@ int fmt::BufferedFile::fileno() const {
}
void fmt::BufferedFile::flush() {
if (!file_)
return;
// FIXME: int result = FMT_SYSTEM(fflush(file_));
int result = ::fflush(file_);
if (result != 0)
throw SystemError(errno, "cannot flush file");
if (!file_)
return;
int result = FMT_SYSTEM(fflush(file_));
if (result != 0)
throw SystemError(errno, "cannot flush file");
}
void fmt::BufferedFile::write_raw(fmt::StringRef str) {
if (!file_)
return;
std::size_t result = FMT_SYSTEM(fwrite(str.c_str(), str.size(), 1, file_));
if (result < str.size()) {
// FIXME: According to ```man fwrite```, fwrite won't set errno at all
throw SystemError(errno, "failed to write raw string, only {} character(s) written", result);
}
}
void fmt::BufferedFile::write_raw(char c) {
if (!file_)
return;
int result = FMT_SYSTEM(fputc(c, file_));
if (result == EOF)
throw SystemError(errno, "failed to write '{}'", result);
}

14
posix.h
View File

@ -197,19 +197,9 @@ public:
void flush(); // FIXME: Should be const?
// Writes raw string without formatting
std::size_t write_raw(fmt::StringRef str) {
// TODO: throw exception when failing
if (!is_open())
return 0;
return ::fwrite(str.c_str(), str.size(), 1, file_);
}
void write_raw(fmt::StringRef str);
// Writes a single charactor without formatting
void write_raw(char c) {
// TODO: throw exception when failing
if (!is_open())
return;
::fputc(c, file_);
}
void write_raw(char c);
void print(fmt::StringRef format_str, const ArgList &args) {
fmt::print(file_, format_str, args);

View File

@ -178,6 +178,18 @@ int test::fileno(FILE *stream) {
return ::FMT_POSIX(fileno(stream));
}
int test::fflush(FILE *stream) {
return ::fflush(stream);
}
int test::fwrite(const void *str, size_t size, size_t count, FILE *stream) {
return ::fwrite(str, size, count, stream);
}
int test::fputc(int ch, FILE *stream) {
return ::fputc(ch, stream);
}
#ifndef _WIN32
# define EXPECT_RETRY(statement, func, message) \
func##_count = 1; \

View File

@ -73,6 +73,10 @@ int pipe(int *pfds, unsigned psize, int textmode);
FILE *fopen(const char *filename, const char *mode);
int fclose(FILE *stream);
int fileno(FILE *stream);
int fflush(FILE *stream);
int fwrite(const void *str, size_t size, size_t count, FILE *stream);
int fputc(int ch, FILE *stream);
} // namespace test