]> git.tdb.fi Git - libs/test.git/commitdiff
Better output for exception tests
authorMikko Rasa <tdb@tdb.fi>
Sat, 1 Dec 2012 12:25:07 +0000 (14:25 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 1 Dec 2012 12:25:07 +0000 (14:25 +0200)
source/exceptioncheck.h
source/test.cpp

index 82115024a87b796704c6083d527167698f7eab94..b7f9d3a6636d8c4f67320febddbb414e88350cba 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef MSP_TEST_EXCEPTIONCHECK_H_
 #define MSP_TEST_EXCEPTIONCHECK_H_
 
+#include <typeinfo>
+
 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<typename T>
@@ -19,6 +23,9 @@ class ExceptionTypeCheck: public ExceptionCheck
 public:
        virtual bool check(const std::exception &e) const
        { return dynamic_cast<const T *>(&e); }
+
+       virtual const std::type_info &get_type_info() const
+       { return typeid(T); }
 };
 
 } // namespace Test
index 0c2081bf0303c68f4306e5ccce65dfdfafa50bb2..b4693b294ea57cfaa54194ebf33e79fd0335813a 100644 (file)
@@ -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(...)
                {