Test EXPECT_SYSTEM_ERROR.
This commit is contained in:
parent
5415b2a43b
commit
34648f4019
@ -106,6 +106,10 @@ void ThrowException() {
|
||||
throw std::runtime_error("test");
|
||||
}
|
||||
|
||||
void ThrowSystemError() {
|
||||
fmt::ThrowSystemError(EDOM, "test");
|
||||
}
|
||||
|
||||
// Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument
|
||||
// exactly once.
|
||||
TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) {
|
||||
@ -114,6 +118,14 @@ TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) {
|
||||
EXPECT_EQ(s_ + 1, p_);
|
||||
}
|
||||
|
||||
// Tests that when EXPECT_SYSTEM_ERROR fails, it evaluates its message argument
|
||||
// exactly once.
|
||||
TEST_F(SingleEvaluationTest, FailedEXPECT_SYSTEM_ERROR) {
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(ThrowSystemError(), EDOM, p_++), "01234");
|
||||
EXPECT_EQ(s_ + 1, p_);
|
||||
}
|
||||
|
||||
// Tests that when EXPECT_WRITE fails, it evaluates its message argument
|
||||
// exactly once.
|
||||
TEST_F(SingleEvaluationTest, FailedEXPECT_WRITE) {
|
||||
@ -156,6 +168,39 @@ TEST_F(SingleEvaluationTest, ExceptionTests) {
|
||||
EXPECT_EQ(4, b_);
|
||||
}
|
||||
|
||||
TEST_F(SingleEvaluationTest, SystemErrorTests) {
|
||||
// successful EXPECT_SYSTEM_ERROR
|
||||
EXPECT_SYSTEM_ERROR({ // NOLINT
|
||||
a_++;
|
||||
ThrowSystemError();
|
||||
}, EDOM, (b_++, "test"));
|
||||
EXPECT_EQ(1, a_);
|
||||
EXPECT_EQ(1, b_);
|
||||
|
||||
// failed EXPECT_SYSTEM_ERROR, throws different type
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR({ // NOLINT
|
||||
a_++;
|
||||
ThrowException();
|
||||
}, EDOM, (b_++, "test")), "throws a different type");
|
||||
EXPECT_EQ(2, a_);
|
||||
EXPECT_EQ(2, b_);
|
||||
|
||||
// failed EXPECT_SYSTEM_ERROR, throws an exception with different message
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR({ // NOLINT
|
||||
a_++;
|
||||
ThrowSystemError();
|
||||
}, EDOM, (b_++, "other")),
|
||||
"throws an exception with a different message");
|
||||
EXPECT_EQ(3, a_);
|
||||
EXPECT_EQ(3, b_);
|
||||
|
||||
// failed EXPECT_SYSTEM_ERROR, throws nothing
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(a_++, EDOM, (b_++, "test")), "throws nothing");
|
||||
EXPECT_EQ(4, a_);
|
||||
EXPECT_EQ(4, b_);
|
||||
}
|
||||
|
||||
// Tests that assertion arguments are evaluated exactly once.
|
||||
TEST_F(SingleEvaluationTest, WriteTests) {
|
||||
// successful EXPECT_WRITE
|
||||
@ -187,6 +232,18 @@ TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) {
|
||||
throw runtime_error("a"), runtime_error, "b"), "");
|
||||
}
|
||||
|
||||
// Tests that the compiler will not complain about unreachable code in the
|
||||
// EXPECT_SYSTEM_ERROR macro.
|
||||
TEST(ExpectSystemErrorTest, DoesNotGenerateUnreachableCodeWarning) {
|
||||
int n = 0;
|
||||
EXPECT_SYSTEM_ERROR(throw fmt::SystemError(
|
||||
FormatSystemErrorMessage(EDOM, "test"), EDOM), EDOM, "test");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(n++, EDOM, ""), "");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(throw 1, EDOM, ""), "");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(
|
||||
throw fmt::SystemError("aaa", EDOM), EDOM, "bbb"), "");
|
||||
}
|
||||
|
||||
TEST(AssertionSyntaxTest, ExceptionAssertionBehavesLikeSingleStatement) {
|
||||
if (::testing::internal::AlwaysFalse())
|
||||
EXPECT_THROW_MSG(DoNothing(), std::exception, "");
|
||||
@ -197,6 +254,16 @@ TEST(AssertionSyntaxTest, ExceptionAssertionBehavesLikeSingleStatement) {
|
||||
DoNothing();
|
||||
}
|
||||
|
||||
TEST(AssertionSyntaxTest, SystemErrorAssertionBehavesLikeSingleStatement) {
|
||||
if (::testing::internal::AlwaysFalse())
|
||||
EXPECT_SYSTEM_ERROR(DoNothing(), EDOM, "");
|
||||
|
||||
if (::testing::internal::AlwaysTrue())
|
||||
EXPECT_SYSTEM_ERROR(ThrowSystemError(), EDOM, "test");
|
||||
else
|
||||
DoNothing();
|
||||
}
|
||||
|
||||
TEST(AssertionSyntaxTest, WriteAssertionBehavesLikeSingleStatement) {
|
||||
if (::testing::internal::AlwaysFalse())
|
||||
EXPECT_WRITE(stdout, std::printf("x"), "x");
|
||||
@ -225,6 +292,27 @@ TEST(ExpectTest, EXPECT_THROW_MSG) {
|
||||
" Actual: test");
|
||||
}
|
||||
|
||||
// Tests EXPECT_SYSTEM_ERROR.
|
||||
TEST(ExpectTest, EXPECT_SYSTEM_ERROR) {
|
||||
EXPECT_SYSTEM_ERROR(ThrowSystemError(), EDOM, "test");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(ThrowException(), EDOM, "test"),
|
||||
"Expected: ThrowException() throws an exception of "
|
||||
"type fmt::SystemError.\n Actual: it throws a different type.");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(DoNothing(), EDOM, "test"),
|
||||
"Expected: DoNothing() throws an exception of type fmt::SystemError.\n"
|
||||
" Actual: it throws nothing.");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(ThrowSystemError(), EDOM, "other"),
|
||||
str(fmt::Format(
|
||||
"ThrowSystemError() throws an exception with a different message.\n"
|
||||
"Expected: {}\n"
|
||||
" Actual: {}")
|
||||
<< FormatSystemErrorMessage(EDOM, "other")
|
||||
<< FormatSystemErrorMessage(EDOM, "test")));
|
||||
}
|
||||
|
||||
// Tests EXPECT_WRITE.
|
||||
TEST(ExpectTest, EXPECT_WRITE) {
|
||||
EXPECT_WRITE(stdout, DoNothing(), "");
|
||||
@ -244,6 +332,14 @@ TEST(StreamingAssertionsTest, EXPECT_THROW_MSG) {
|
||||
<< "expected failure", "expected failure");
|
||||
}
|
||||
|
||||
TEST(StreamingAssertionsTest, EXPECT_SYSTEM_ERROR) {
|
||||
EXPECT_SYSTEM_ERROR(ThrowSystemError(), EDOM, "test")
|
||||
<< "unexpected failure";
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(ThrowSystemError(), EDOM, "other")
|
||||
<< "expected failure", "expected failure");
|
||||
}
|
||||
|
||||
TEST(StreamingAssertionsTest, EXPECT_WRITE) {
|
||||
EXPECT_WRITE(stdout, std::printf("test"), "test")
|
||||
<< "unexpected failure";
|
||||
@ -704,8 +800,7 @@ TEST(OutputRedirectTest, ErrorInDtor) {
|
||||
write_copy.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail
|
||||
}
|
||||
|
||||
// TODO: test EXPECT_SYSTEM_ERROR
|
||||
// TODO: test retry on EINTR
|
||||
// TODO: test retry on EINTR, test FormatSystemErrorMessage
|
||||
|
||||
#endif // FMT_USE_FILE_DESCRIPTORS
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user