diff --git a/format-test.cc b/format-test.cc index f8987d62..52a7005b 100644 --- a/format-test.cc +++ b/format-test.cc @@ -310,12 +310,13 @@ TEST(UtilTest, SystemError) { EXPECT_EQ(42, e.error_code()); } -// TODO: test WinErrorSink, FormatSystemErrorMessage, FormatWinErrorMessage +typedef void (*FormatErrorMessage)( + fmt::Writer &out, int error_code, fmt::StringRef message); -TEST(UtilTest, SystemErrorSink) { - const int TEST_ERROR = EDOM; +template +void CheckErrorSink(int error_code, FormatErrorMessage format) { fmt::SystemError error("", 0); - fmt::SystemErrorSink sink(TEST_ERROR); + Sink sink(error_code); fmt::Writer w; w << "test"; try { @@ -323,32 +324,46 @@ TEST(UtilTest, SystemErrorSink) { } catch (const fmt::SystemError &e) { error = e; } - EXPECT_EQ(str(fmt::Format("test: {}") << strerror(TEST_ERROR)), error.what()); - EXPECT_EQ(TEST_ERROR, error.error_code()); + fmt::Writer message; + format(message, error_code, "test"); + EXPECT_EQ(str(message), error.what()); + EXPECT_EQ(error_code, error.error_code()); +} + +template +void CheckThrowError(int error_code, FormatErrorMessage format, + fmt::Formatter (*throw_error)(int error_code, StringRef format)) { + fmt::SystemError error("", 0); + try { + throw_error(error_code, "test {}") << "error"; + } catch (const fmt::SystemError &e) { + error = e; + } + fmt::Writer message; + format(message, error_code, "test error"); + EXPECT_EQ(str(message), error.what()); + EXPECT_EQ(error_code, error.error_code()); +} + +TEST(UtilTest, FormatSystemErrorMessage) { + fmt::Writer message; + fmt::internal::FormatSystemErrorMessage(message, EDOM, "test"); + EXPECT_EQ(str(fmt::Format("test: {}") << strerror(EDOM)), fmt::str(message)); +} + +TEST(UtilTest, SystemErrorSink) { + CheckErrorSink( + EDOM, fmt::internal::FormatSystemErrorMessage); } TEST(UtilTest, ThrowSystemError) { - const int TEST_ERROR = EDOM; - fmt::SystemError error("", 0); - try { - fmt::ThrowSystemError(TEST_ERROR, "test {}") << "error"; - } catch (const fmt::SystemError &e) { - error = e; - } - EXPECT_EQ(str(fmt::Format("test error: {}") << strerror(TEST_ERROR)), - error.what()); - EXPECT_EQ(TEST_ERROR, error.error_code()); + CheckThrowError(EDOM, + fmt::internal::FormatSystemErrorMessage, fmt::ThrowSystemError); } #ifdef _WIN32 -TEST(UtilTest, ThrowWinError) { - const int TEST_ERROR = ERROR_FILE_EXISTS; - fmt::SystemError error("", 0); - try { - fmt::ThrowWinError(TEST_ERROR, "test {}") << "error"; - } catch (const fmt::SystemError &e) { - error = e; - } + +TEST(UtilTest, FormatWinErrorMessage) { LPWSTR message = 0; FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, @@ -356,11 +371,24 @@ TEST(UtilTest, ThrowWinError) { reinterpret_cast(&message), 0, 0); fmt::internal::UTF16ToUTF8 utf8_message(message); LocalFree(message); - EXPECT_EQ(str(fmt::Format("test error: {}") << fmt::str(utf8_message)), - error.what()); - EXPECT_EQ(TEST_ERROR, error.error_code()); + fmt::Writer actual_message; + fmt::internal::FormatWinErrorMessage( + actual_message, ERROR_FILE_EXISTS, "test"); + EXPECT_EQ(str(fmt::Format("test: {}") << fmt::str(utf8_message)), + fmt::str(actual_message)); } -#endif + +TEST(UtilTest, WinErrorSink) { + CheckErrorSink( + ERROR_FILE_EXISTS, fmt::internal::FormatWinErrorMessage); +} + +TEST(UtilTest, ThrowWinError) { + CheckThrowError(ERROR_FILE_EXISTS, + fmt::internal::FormatWinErrorMessage, fmt::ThrowWinError); +} + +#endif // _WIN32 class TestString { private: