mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-27 12:30:06 +00:00
Test system error reporting.
This commit is contained in:
parent
6ec1389de2
commit
5c639eb4c4
@ -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 <typename Sink>
|
||||
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 <typename Sink>
|
||||
void CheckThrowError(int error_code, FormatErrorMessage format,
|
||||
fmt::Formatter<Sink> (*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<fmt::SystemErrorSink>(
|
||||
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<LPWSTR>(&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<fmt::WinErrorSink>(
|
||||
ERROR_FILE_EXISTS, fmt::internal::FormatWinErrorMessage);
|
||||
}
|
||||
|
||||
TEST(UtilTest, ThrowWinError) {
|
||||
CheckThrowError(ERROR_FILE_EXISTS,
|
||||
fmt::internal::FormatWinErrorMessage, fmt::ThrowWinError);
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
||||
|
||||
class TestString {
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user