From 7fc184eba45c01c371bb6f871fba1ff685157bfd Mon Sep 17 00:00:00 2001 From: Alex Cunningham Date: Fri, 16 Jul 2010 19:53:53 +0000 Subject: [PATCH] Added the ability to check if an operation throws the correct exception --- CppUnitLite/Test.h | 6 +++++- CppUnitLite/TestRegistry.cpp | 3 ++- wrap/testWrap.cpp | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CppUnitLite/Test.h b/CppUnitLite/Test.h index 6fe5a7e38..8a240046b 100644 --- a/CppUnitLite/Test.h +++ b/CppUnitLite/Test.h @@ -60,7 +60,11 @@ protected: { if (!(condition)) \ { result_.addFailure (Failure (name_, __FILE__,__LINE__, #condition)); return; } } - +#define THROWS_EXCEPTION(condition)\ +{ try { condition; \ + result_.addFailure (Failure (name_, __FILE__,__LINE__, SimpleString("Didn't throw: ") + StringFrom(#condition))); \ + return; } \ + catch (...) {} } #define CHECK_EQUAL(expected,actual)\ { if ((expected) == (actual)) return; result_.addFailure(Failure(name_, __FILE__, __LINE__, StringFrom(expected), StringFrom(actual))); } diff --git a/CppUnitLite/TestRegistry.cpp b/CppUnitLite/TestRegistry.cpp index 25db78239..42cacfe66 100644 --- a/CppUnitLite/TestRegistry.cpp +++ b/CppUnitLite/TestRegistry.cpp @@ -45,14 +45,15 @@ int TestRegistry::run (TestResult& result) result.testsStarted (); for (Test *test = tests; test != 0; test = test->getNext ()) { - // TODO: add a try/catch wrapper here try { test->run (result); } catch (std::exception& e) { + // catch standard exceptions and derivatives result.addFailure( Failure(test->getName(), test->getFilename(), test->getLineNumber(), SimpleString("Exception: ") + SimpleString(e.what()))); } catch (...) { + // catch all other exceptions result.addFailure( Failure(test->getName(), test->getFilename(), test->getLineNumber(), SimpleString("ExceptionThrown!"))); diff --git a/wrap/testWrap.cpp b/wrap/testWrap.cpp index 6c1878c87..32ae72d13 100644 --- a/wrap/testWrap.cpp +++ b/wrap/testWrap.cpp @@ -26,6 +26,11 @@ TEST( wrap, ArgumentList ) { CHECK(args.names()=="x,x,x"); } +/* ************************************************************************* */ +TEST( wrap, check_exception ) { + THROWS_EXCEPTION(Module(".", "geometry",verbose)); +} + /* ************************************************************************* */ TEST( wrap, parse ) { Module module(".", "geometry",verbose);