From 9cb2cd7e532e8edfc27a113d5efa735bcaa85d3a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 1 Dec 2012 14:25:07 +0200 Subject: [PATCH] Better output for exception tests --- source/exceptioncheck.h | 9 ++++++++- source/test.cpp | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/source/exceptioncheck.h b/source/exceptioncheck.h index 8211502..b7f9d3a 100644 --- a/source/exceptioncheck.h +++ b/source/exceptioncheck.h @@ -1,6 +1,8 @@ #ifndef MSP_TEST_EXCEPTIONCHECK_H_ #define MSP_TEST_EXCEPTIONCHECK_H_ +#include + namespace Msp { namespace Test { @@ -8,9 +10,11 @@ class ExceptionCheck { protected: ExceptionCheck() { } - public: + virtual ~ExceptionCheck() { } + virtual bool check(const std::exception &) const = 0; + virtual const std::type_info &get_type_info() const = 0; }; template @@ -19,6 +23,9 @@ class ExceptionTypeCheck: public ExceptionCheck public: virtual bool check(const std::exception &e) const { return dynamic_cast(&e); } + + virtual const std::type_info &get_type_info() const + { return typeid(T); } }; } // namespace Test diff --git a/source/test.cpp b/source/test.cpp index 0c2081b..b4693b2 100644 --- a/source/test.cpp +++ b/source/test.cpp @@ -88,14 +88,26 @@ void Test::run(bool verbose) } catch(const exception &e) { - if(exc_check && exc_check->check(e)) + if(exc_check) { - pass_test(); - debug(Debug::demangle(typeid(e).name())); - debug(e.what()); + if(exc_check->check(e)) + { + pass_test(); + debug(Debug::demangle(typeid(e).name())); + debug(e.what()); + } + else + { + fail_test("Exception of incorrect type thrown"); + debug(format("%s (expected %s)", Debug::demangle(typeid(e).name()), Debug::demangle(exc_check->get_type_info().name()))); + debug(e.what()); + } } else + { + debug(Debug::demangle(typeid(e).name())); fail_test(e.what()); + } } catch(...) { -- 2.43.0