unittest.h

00001 /*
00002  *  Copyright (C) 2006  Helmut Grohne
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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

Generated on Sat Feb 7 01:26:50 2009 by  doxygen 1.5.1