aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-08-09 22:22:41 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-08-09 22:22:41 +0000
commit19c30061351a6e2fbc07197a23483a873fceac2b (patch)
tree31587928ef86f1be620198b26761549d55fb709b
parent93a380c1dd687e5131e1594476a166e26ae3cc5b (diff)
downloadpaludis-19c30061351a6e2fbc07197a23483a873fceac2b.tar.gz
paludis-19c30061351a6e2fbc07197a23483a873fceac2b.tar.xz
Improve handling of unexpected exceptions in test cases
-rw-r--r--test/test_framework.cc5
-rw-r--r--test/test_framework.hh75
2 files changed, 67 insertions, 13 deletions
diff --git a/test/test_framework.cc b/test/test_framework.cc
index af8b608..b08e42c 100644
--- a/test/test_framework.cc
+++ b/test/test_framework.cc
@@ -75,6 +75,11 @@ test::set_exception_to_debug_string(std::string (*f) (const std::exception &))
DebugStringHolder::get_instance()->set(f);
}
+std::string (* test::get_exception_to_debug_string()) (const std::exception &)
+{
+ return DebugStringHolder::get_instance()->get();
+}
+
namespace paludis
{
template<>
diff --git a/test/test_framework.hh b/test/test_framework.hh
index 3f459c1..2d730c5 100644
--- a/test/test_framework.hh
+++ b/test/test_framework.hh
@@ -187,6 +187,11 @@ namespace test
* Change the function used to get a string description of an exception.
*/
void set_exception_to_debug_string(std::string (*) (const std::exception &));
+
+ /**
+ * Fetch the function used to get a string description of an exception.
+ */
+ std::string (* get_exception_to_debug_string()) (const std::exception &);
}
/**
@@ -194,9 +199,20 @@ namespace test
*/
#define TEST_CHECK_EQUAL(a, b) \
do { \
- check(__PRETTY_FUNCTION__, __FILE__, __LINE__, a == b, \
- "Expected '" #a "' to equal '" + paludis::stringify(b) + \
- "' but got '" + paludis::stringify(a) + "'"); \
+ try { \
+ check(__PRETTY_FUNCTION__, __FILE__, __LINE__, a == b, \
+ "Expected '" #a "' to equal '" + paludis::stringify(b) + \
+ "' but got '" + paludis::stringify(a) + "'"); \
+ } catch (const TestFailedException &) { \
+ throw; \
+ } catch (const std::exception & e) { \
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__, \
+ "Test threw unexpected exception " + test::get_exception_to_debug_string()(e) + \
+ " inside a TEST_CHECK_EQUAL block"); \
+ } catch (...) { \
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__, \
+ "Test threw unexpected unknown exception inside a TEST_CHECK_EQUAL block"); \
+ } \
} while (false)
/**
@@ -204,9 +220,20 @@ namespace test
*/
#define TEST_CHECK_STRINGIFY_EQUAL(a, b) \
do { \
- check(__PRETTY_FUNCTION__, __FILE__, __LINE__, paludis::stringify(a) == paludis::stringify(b), \
- "Expected '" #a "' to equal '" + paludis::stringify(b) + \
- "' but got '" + paludis::stringify(a) + "'"); \
+ try { \
+ check(__PRETTY_FUNCTION__, __FILE__, __LINE__, paludis::stringify(a) == paludis::stringify(b), \
+ "Expected '" #a "' to equal '" + paludis::stringify(b) + \
+ "' but got '" + paludis::stringify(a) + "'"); \
+ } catch (const TestFailedException &) { \
+ throw; \
+ } catch (const std::exception & e) { \
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__, \
+ "Test threw unexpected exception " + test::get_exception_to_debug_string()(e) + \
+ " inside a TEST_CHECK_STRINGIFY_EQUAL block"); \
+ } catch (...) { \
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__, \
+ "Test threw unexpected unknown exception inside a TEST_CHECK_STRINGIFY_EQUAL block"); \
+ } \
} while (false)
/**
@@ -214,8 +241,19 @@ namespace test
*/
#define TEST_CHECK(a) \
do { \
- check(__PRETTY_FUNCTION__, __FILE__, __LINE__, a, \
- "Check '" #a "' failed"); \
+ try { \
+ check(__PRETTY_FUNCTION__, __FILE__, __LINE__, a, \
+ "Check '" #a "' failed"); \
+ } catch (const TestFailedException &) { \
+ throw; \
+ } catch (const std::exception & e) { \
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__, \
+ "Test threw unexpected exception " + test::get_exception_to_debug_string()(e) + \
+ " inside a TEST_CHECK block"); \
+ } catch (...) { \
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__, \
+ "Test threw unexpected unknown exception inside a TEST_CHECK block"); \
+ } \
} while (false)
/**
@@ -224,11 +262,22 @@ namespace test
#define TEST_CHECK_THROWS(a, b) \
do { \
try { \
- a; \
- check(__PRETTY_FUNCTION__, __FILE__, __LINE__, false, \
- "Expected exception of type '" #b "' not thrown"); \
- } catch (b &) { \
- TEST_CHECK(true); \
+ try { \
+ a; \
+ check(__PRETTY_FUNCTION__, __FILE__, __LINE__, false, \
+ "Expected exception of type '" #b "' not thrown"); \
+ } catch (b &) { \
+ TEST_CHECK(true); \
+ } \
+ } catch (const TestFailedException &) { \
+ throw; \
+ } catch (const std::exception & e) { \
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__, \
+ "Test threw unexpected exception " + test::get_exception_to_debug_string()(e) + \
+ " inside a TEST_CHECK_THROWS block"); \
+ } catch (...) { \
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__, \
+ "Test threw unexpected unknown exception inside a TEST_CHECK_THROWS block"); \
} \
} while (false)