aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorshiqian <shiqian@861a406c-534a-0410-8894-cb66d6ee9925>2008-09-12 20:57:22 +0000
committershiqian <shiqian@861a406c-534a-0410-8894-cb66d6ee9925>2008-09-12 20:57:22 +0000
commit36865d8d354465e3461eedf2949a4b7799985d5d (patch)
tree3f8afc3eceff8a13df8eecfcfb1fb1bfab102f2c /include
parent0bbdb14f74f70d0e1b50f5510a61d98d8e82bd2e (diff)
downloadgoogletest-36865d8d354465e3461eedf2949a4b7799985d5d.tar.gz
googletest-36865d8d354465e3461eedf2949a4b7799985d5d.tar.bz2
googletest-36865d8d354465e3461eedf2949a4b7799985d5d.zip
Adds exception assertions. By balaz.dan@gmail.com.
Diffstat (limited to 'include')
-rw-r--r--include/gtest/gtest.h22
-rw-r--r--include/gtest/internal/gtest-internal.h61
2 files changed, 83 insertions, 0 deletions
diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h
index 057efa26..67cf4ac0 100644
--- a/include/gtest/gtest.h
+++ b/include/gtest/gtest.h
@@ -928,6 +928,28 @@ class AssertHelper {
// Generates a success with a generic message.
#define SUCCEED() GTEST_SUCCESS("Succeeded")
+// Macros for testing exceptions.
+//
+// * {ASSERT|EXPECT}_THROW(statement, expected_exception):
+// Tests that the statement throws the expected exception.
+// * {ASSERT|EXPECT}_NO_THROW(statement):
+// Tests that the statement doesn't throw any exception.
+// * {ASSERT|EXPECT}_ANY_THROW(statement):
+// Tests that the statement throws an exception.
+
+#define EXPECT_THROW(statement, expected_exception) \
+ GTEST_TEST_THROW(statement, expected_exception, GTEST_NONFATAL_FAILURE)
+#define EXPECT_NO_THROW(statement) \
+ GTEST_TEST_NO_THROW(statement, GTEST_NONFATAL_FAILURE)
+#define EXPECT_ANY_THROW(statement) \
+ GTEST_TEST_ANY_THROW(statement, GTEST_NONFATAL_FAILURE)
+#define ASSERT_THROW(statement, expected_exception) \
+ GTEST_TEST_THROW(statement, expected_exception, GTEST_FATAL_FAILURE)
+#define ASSERT_NO_THROW(statement) \
+ GTEST_TEST_NO_THROW(statement, GTEST_FATAL_FAILURE)
+#define ASSERT_ANY_THROW(statement) \
+ GTEST_TEST_ANY_THROW(statement, GTEST_FATAL_FAILURE)
+
// Boolean assertions.
#define EXPECT_TRUE(condition) \
GTEST_TEST_BOOLEAN(condition, #condition, false, true, \
diff --git a/include/gtest/internal/gtest-internal.h b/include/gtest/internal/gtest-internal.h
index 8adf13e4..898047e8 100644
--- a/include/gtest/internal/gtest-internal.h
+++ b/include/gtest/internal/gtest-internal.h
@@ -717,6 +717,67 @@ class TypeParameterizedTestCase<Fixture, Templates0, Types> {
#define GTEST_SUCCESS(message) \
GTEST_MESSAGE(message, ::testing::TPRT_SUCCESS)
+
+#define GTEST_TEST_THROW(statement, expected_exception, fail) \
+ GTEST_AMBIGUOUS_ELSE_BLOCKER \
+ if (const char* gtest_msg = "") { \
+ bool gtest_caught_expected = false; \
+ try { \
+ statement; \
+ } \
+ catch (expected_exception const&) { \
+ gtest_caught_expected = true; \
+ } \
+ catch (...) { \
+ gtest_msg = "Expected: " #statement " throws an exception of type " \
+ #expected_exception ".\n Actual: it throws a different " \
+ "type."; \
+ goto GTEST_CONCAT_TOKEN(gtest_label_testthrow_, __LINE__); \
+ } \
+ if (!gtest_caught_expected) { \
+ gtest_msg = "Expected: " #statement " throws an exception of type " \
+ #expected_exception ".\n Actual: it throws nothing."; \
+ goto GTEST_CONCAT_TOKEN(gtest_label_testthrow_, __LINE__); \
+ } \
+ } else \
+ GTEST_CONCAT_TOKEN(gtest_label_testthrow_, __LINE__): \
+ fail(gtest_msg)
+
+#define GTEST_TEST_NO_THROW(statement, fail) \
+ GTEST_AMBIGUOUS_ELSE_BLOCKER \
+ if (const char* gtest_msg = "") { \
+ try { \
+ statement; \
+ } \
+ catch (...) { \
+ gtest_msg = "Expected: " #statement " doesn't throw an exception.\n" \
+ " Actual: it throws."; \
+ goto GTEST_CONCAT_TOKEN(gtest_label_testnothrow_, __LINE__); \
+ } \
+ } else \
+ GTEST_CONCAT_TOKEN(gtest_label_testnothrow_, __LINE__): \
+ fail(gtest_msg)
+
+#define GTEST_TEST_ANY_THROW(statement, fail) \
+ GTEST_AMBIGUOUS_ELSE_BLOCKER \
+ if (const char* gtest_msg = "") { \
+ bool gtest_caught_any = false; \
+ try { \
+ statement; \
+ } \
+ catch (...) { \
+ gtest_caught_any = true; \
+ } \
+ if (!gtest_caught_any) { \
+ gtest_msg = "Expected: " #statement " throws an exception.\n" \
+ " Actual: it doesn't."; \
+ goto GTEST_CONCAT_TOKEN(gtest_label_testanythrow_, __LINE__); \
+ } \
+ } else \
+ GTEST_CONCAT_TOKEN(gtest_label_testanythrow_, __LINE__): \
+ fail(gtest_msg)
+
+
#define GTEST_TEST_BOOLEAN(boolexpr, booltext, actual, expected, fail) \
GTEST_AMBIGUOUS_ELSE_BLOCKER \
if (boolexpr) \