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());
|
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) {
|
template <typename Sink>
|
||||||
const int TEST_ERROR = EDOM;
|
void CheckErrorSink(int error_code, FormatErrorMessage format) {
|
||||||
fmt::SystemError error("", 0);
|
fmt::SystemError error("", 0);
|
||||||
fmt::SystemErrorSink sink(TEST_ERROR);
|
Sink sink(error_code);
|
||||||
fmt::Writer w;
|
fmt::Writer w;
|
||||||
w << "test";
|
w << "test";
|
||||||
try {
|
try {
|
||||||
@ -323,32 +324,46 @@ TEST(UtilTest, SystemErrorSink) {
|
|||||||
} catch (const fmt::SystemError &e) {
|
} catch (const fmt::SystemError &e) {
|
||||||
error = e;
|
error = e;
|
||||||
}
|
}
|
||||||
EXPECT_EQ(str(fmt::Format("test: {}") << strerror(TEST_ERROR)), error.what());
|
fmt::Writer message;
|
||||||
EXPECT_EQ(TEST_ERROR, error.error_code());
|
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) {
|
TEST(UtilTest, ThrowSystemError) {
|
||||||
const int TEST_ERROR = EDOM;
|
CheckThrowError(EDOM,
|
||||||
fmt::SystemError error("", 0);
|
fmt::internal::FormatSystemErrorMessage, fmt::ThrowSystemError);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
TEST(UtilTest, ThrowWinError) {
|
|
||||||
const int TEST_ERROR = ERROR_FILE_EXISTS;
|
TEST(UtilTest, FormatWinErrorMessage) {
|
||||||
fmt::SystemError error("", 0);
|
|
||||||
try {
|
|
||||||
fmt::ThrowWinError(TEST_ERROR, "test {}") << "error";
|
|
||||||
} catch (const fmt::SystemError &e) {
|
|
||||||
error = e;
|
|
||||||
}
|
|
||||||
LPWSTR message = 0;
|
LPWSTR message = 0;
|
||||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
||||||
@ -356,11 +371,24 @@ TEST(UtilTest, ThrowWinError) {
|
|||||||
reinterpret_cast<LPWSTR>(&message), 0, 0);
|
reinterpret_cast<LPWSTR>(&message), 0, 0);
|
||||||
fmt::internal::UTF16ToUTF8 utf8_message(message);
|
fmt::internal::UTF16ToUTF8 utf8_message(message);
|
||||||
LocalFree(message);
|
LocalFree(message);
|
||||||
EXPECT_EQ(str(fmt::Format("test error: {}") << fmt::str(utf8_message)),
|
fmt::Writer actual_message;
|
||||||
error.what());
|
fmt::internal::FormatWinErrorMessage(
|
||||||
EXPECT_EQ(TEST_ERROR, error.error_code());
|
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 {
|
class TestString {
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user