00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 #ifndef LIBMUTH_UNITTEST_H
00020 #define LIBMUTH_UNITTEST_H
00021 
00022 #include "compiler.h"
00023 #include "unittest_decls.h"
00024 
00025 inline ForwardGatherer::ForwardGatherer(BaseGatherer &g, const std::string &p)
00026                 : gatherer(g.forwardto()), prefix(g.prefixed() + p) {
00027 }
00028 
00029 inline Gatherer::Gatherer() : succeeded(0), failed(0) {
00030 }
00031 
00032 inline BaseTest::BaseTest(const std::string &n) : name(n) {
00033 }
00034 
00035 template<class F> inline TestCase<F>::TestCase(const std::string &n,
00036                 void (F::*func)()) : BaseTest(n), function(func) {
00037 }
00038 
00039 template<class F> inline TestCase<F>::TestCase(const TestCase<F> &o)
00040                 : BaseTest(o), function(o.function) {
00041 }
00042 
00043 template<class F> inline TestCase<F> *TestCase<F>::clone() const {
00044         return new TestCase<F>(*this);
00045 }
00046 
00047 template<class F> void TestCase<F>::run(BaseGatherer &g) {
00048         F fixture;
00049         bool succeeded(false);
00050         try {
00051                 (fixture.*function)();
00052                 succeeded = true;
00053         } catch(Failure &f) {
00054                 g.fail(this->name, f.what());
00055         } catch(...) {
00056                 g.fail(this->name, std::string("unknown exception caught"));
00057         } if(likely(succeeded))
00058                 g.succeed(this->name);
00059 }
00060 
00061 inline TestSuite::TestSuite(const std::string &n) : BaseTest(n) {
00062 }
00063 
00064 inline TestSuite::TestSuite(const TestSuite &o) : BaseTest(o) {
00065         for(std::list<BaseTest*>::const_iterator i(o.tests.begin());
00066                         i!=o.tests.end();
00067                         ++i)
00068                 this->tests.push_back((*i)->clone());
00069 }
00070 
00071 inline Failure::Failure(const std::string &r) : std::runtime_error(r) {
00072 }
00073 
00074 inline void unittest_assert(bool test, const std::string &r) {
00075         if(!test)
00076                 throw Failure(r);
00077 }
00078 
00079 #endif