Commit 34648f40 authored by Victor Zverovich's avatar Victor Zverovich

Test EXPECT_SYSTEM_ERROR.

parent 5415b2a4
...@@ -106,6 +106,10 @@ void ThrowException() { ...@@ -106,6 +106,10 @@ void ThrowException() {
throw std::runtime_error("test"); throw std::runtime_error("test");
} }
void ThrowSystemError() {
fmt::ThrowSystemError(EDOM, "test");
}
// Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument // Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument
// exactly once. // exactly once.
TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) { TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) {
...@@ -114,6 +118,14 @@ TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) { ...@@ -114,6 +118,14 @@ TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) {
EXPECT_EQ(s_ + 1, p_); 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 // Tests that when EXPECT_WRITE fails, it evaluates its message argument
// exactly once. // exactly once.
TEST_F(SingleEvaluationTest, FailedEXPECT_WRITE) { TEST_F(SingleEvaluationTest, FailedEXPECT_WRITE) {
...@@ -156,6 +168,39 @@ TEST_F(SingleEvaluationTest, ExceptionTests) { ...@@ -156,6 +168,39 @@ TEST_F(SingleEvaluationTest, ExceptionTests) {
EXPECT_EQ(4, b_); 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. // Tests that assertion arguments are evaluated exactly once.
TEST_F(SingleEvaluationTest, WriteTests) { TEST_F(SingleEvaluationTest, WriteTests) {
// successful EXPECT_WRITE // successful EXPECT_WRITE
...@@ -187,6 +232,18 @@ TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) { ...@@ -187,6 +232,18 @@ TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) {
throw runtime_error("a"), runtime_error, "b"), ""); 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) { TEST(AssertionSyntaxTest, ExceptionAssertionBehavesLikeSingleStatement) {
if (::testing::internal::AlwaysFalse()) if (::testing::internal::AlwaysFalse())
EXPECT_THROW_MSG(DoNothing(), std::exception, ""); EXPECT_THROW_MSG(DoNothing(), std::exception, "");
...@@ -197,6 +254,16 @@ TEST(AssertionSyntaxTest, ExceptionAssertionBehavesLikeSingleStatement) { ...@@ -197,6 +254,16 @@ TEST(AssertionSyntaxTest, ExceptionAssertionBehavesLikeSingleStatement) {
DoNothing(); 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) { TEST(AssertionSyntaxTest, WriteAssertionBehavesLikeSingleStatement) {
if (::testing::internal::AlwaysFalse()) if (::testing::internal::AlwaysFalse())
EXPECT_WRITE(stdout, std::printf("x"), "x"); EXPECT_WRITE(stdout, std::printf("x"), "x");
...@@ -225,6 +292,27 @@ TEST(ExpectTest, EXPECT_THROW_MSG) { ...@@ -225,6 +292,27 @@ TEST(ExpectTest, EXPECT_THROW_MSG) {
" Actual: test"); " 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. // Tests EXPECT_WRITE.
TEST(ExpectTest, EXPECT_WRITE) { TEST(ExpectTest, EXPECT_WRITE) {
EXPECT_WRITE(stdout, DoNothing(), ""); EXPECT_WRITE(stdout, DoNothing(), "");
...@@ -244,6 +332,14 @@ TEST(StreamingAssertionsTest, EXPECT_THROW_MSG) { ...@@ -244,6 +332,14 @@ TEST(StreamingAssertionsTest, EXPECT_THROW_MSG) {
<< "expected failure", "expected failure"); << "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) { TEST(StreamingAssertionsTest, EXPECT_WRITE) {
EXPECT_WRITE(stdout, std::printf("test"), "test") EXPECT_WRITE(stdout, std::printf("test"), "test")
<< "unexpected failure"; << "unexpected failure";
...@@ -704,8 +800,7 @@ TEST(OutputRedirectTest, ErrorInDtor) { ...@@ -704,8 +800,7 @@ TEST(OutputRedirectTest, ErrorInDtor) {
write_copy.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail write_copy.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail
} }
// TODO: test EXPECT_SYSTEM_ERROR // TODO: test retry on EINTR, test FormatSystemErrorMessage
// TODO: test retry on EINTR
#endif // FMT_USE_FILE_DESCRIPTORS #endif // FMT_USE_FILE_DESCRIPTORS
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment