mirror of
https://github.com/fmtlib/fmt.git
synced 2025-08-05 13:36:32 -04:00
Use actual example code and move safe_fopen to os-test
This commit is contained in:
parent
2fa3e1a1bb
commit
6a3b40524c
@ -1809,19 +1809,15 @@ TEST(format_test, format_examples) {
|
|||||||
fmt::format_to(std::back_inserter(out), "The answer is {}.", 42);
|
fmt::format_to(std::back_inserter(out), "The answer is {}.", 42);
|
||||||
EXPECT_EQ("The answer is 42.", to_string(out));
|
EXPECT_EQ("The answer is 42.", to_string(out));
|
||||||
|
|
||||||
const char* filename = "nonexistent";
|
EXPECT_THROW(
|
||||||
FILE* ftest = safe_fopen(filename, "r");
|
|
||||||
if (ftest) fclose(ftest);
|
|
||||||
int error_code = errno;
|
|
||||||
EXPECT_TRUE(ftest == nullptr);
|
|
||||||
EXPECT_SYSTEM_ERROR(
|
|
||||||
{
|
{
|
||||||
FILE* f = safe_fopen(filename, "r");
|
const char* filename = "madeup";
|
||||||
if (!f)
|
FILE* file = fopen(filename, "r");
|
||||||
throw fmt::system_error(errno, "Cannot open file '{}'", filename);
|
if (!file)
|
||||||
fclose(f);
|
throw fmt::system_error(errno, "cannot open file '{}'", filename);
|
||||||
|
fclose(file);
|
||||||
},
|
},
|
||||||
error_code, "Cannot open file 'nonexistent'");
|
std::system_error);
|
||||||
|
|
||||||
EXPECT_EQ("First, thou shalt count to three",
|
EXPECT_EQ("First, thou shalt count to three",
|
||||||
fmt::format("First, thou shalt count to {0}", "three"));
|
fmt::format("First, thou shalt count to {0}", "three"));
|
||||||
|
@ -19,10 +19,21 @@ using fmt::buffered_file;
|
|||||||
using testing::HasSubstr;
|
using testing::HasSubstr;
|
||||||
using wstring_view = fmt::basic_string_view<wchar_t>;
|
using wstring_view = fmt::basic_string_view<wchar_t>;
|
||||||
|
|
||||||
static std::string uniq_file_name(unsigned line_number) {
|
static auto uniq_file_name(unsigned line_number) -> std::string {
|
||||||
return "test-file" + std::to_string(line_number);
|
return "test-file" + std::to_string(line_number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto safe_fopen(const char* filename, const char* mode) -> FILE* {
|
||||||
|
#if defined(_WIN32) && !defined(__MINGW32__)
|
||||||
|
// Fix MSVC warning about "unsafe" fopen.
|
||||||
|
FILE* f = nullptr;
|
||||||
|
errno = fopen_s(&f, filename, mode);
|
||||||
|
return f;
|
||||||
|
#else
|
||||||
|
return std::fopen(filename, mode);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
|
11
test/util.h
11
test/util.h
@ -31,17 +31,6 @@ extern const char* const file_content;
|
|||||||
// Opens a buffered file for reading.
|
// Opens a buffered file for reading.
|
||||||
auto open_buffered_file(FILE** fp = nullptr) -> fmt::buffered_file;
|
auto open_buffered_file(FILE** fp = nullptr) -> fmt::buffered_file;
|
||||||
|
|
||||||
inline auto safe_fopen(const char* filename, const char* mode) -> FILE* {
|
|
||||||
#if defined(_WIN32) && !defined(__MINGW32__)
|
|
||||||
// Fix MSVC warning about "unsafe" fopen.
|
|
||||||
FILE* f = nullptr;
|
|
||||||
errno = fopen_s(&f, filename, mode);
|
|
||||||
return f;
|
|
||||||
#else
|
|
||||||
return std::fopen(filename, mode);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Char> class basic_test_string {
|
template <typename Char> class basic_test_string {
|
||||||
private:
|
private:
|
||||||
std::basic_string<Char> value_;
|
std::basic_string<Char> value_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user