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_DECLS_H 00020 #define LIBMUTH_UNITTEST_DECLS_H 00021 00022 #include <list> 00023 #include <memory> 00024 #include <stdexcept> 00025 #include <string> 00026 00031 class Fixture { 00032 public: 00033 virtual ~Fixture(); 00034 }; 00035 00039 class BaseGatherer { 00040 public: 00046 virtual std::auto_ptr<BaseGatherer> clone()=0; 00051 virtual void succeed(const std::string &n)=0; 00057 virtual void fail(const std::string &n, const std::string &r)=0; 00063 virtual void pending(int n=1); 00070 virtual BaseGatherer &forwardto(); 00077 virtual const std::string &prefixed() const; 00078 virtual ~BaseGatherer(); 00079 }; 00080 00085 class ForwardGatherer : public BaseGatherer { 00086 private: 00087 BaseGatherer &gatherer; 00088 std::string prefix; 00089 public: 00090 std::auto_ptr<BaseGatherer> clone(); 00096 ForwardGatherer(BaseGatherer &g, const std::string &p); 00097 void succeed(const std::string &n); 00098 void fail(const std::string &n, const std::string &r); 00099 void pending(int n); 00100 BaseGatherer &forwardto(); 00101 const std::string &prefixed() const; 00102 }; 00103 00107 class Gatherer : public BaseGatherer { 00108 private: 00109 int succeeded; 00110 int failed; 00111 public: 00112 Gatherer(); 00113 std::auto_ptr<BaseGatherer> clone(); 00114 void succeed(const std::string &n); 00115 void fail(const std::string &n, const std::string &r); 00119 void stats(); 00124 int failed_tests(); 00125 }; 00126 00131 class BaseTest { 00132 protected: 00136 std::string name; 00137 public: 00142 BaseTest(const std::string &n); 00148 virtual BaseTest *clone() const=0; 00153 virtual void run(BaseGatherer &g)=0; 00154 virtual ~BaseTest(); 00155 }; 00156 00161 template<class F> class TestCase : public BaseTest { 00162 private: 00163 void (F::*function)(); 00164 public: 00170 TestCase(const std::string &n, void (F::*func)()); 00175 TestCase(const TestCase &other); 00176 TestCase<F> *clone() const; 00177 void run(BaseGatherer &g); 00178 }; 00179 00183 class TestSuite : public BaseTest { 00184 private: 00185 std::list<BaseTest*> tests; 00186 public: 00191 TestSuite(const std::string &n); 00196 TestSuite(const TestSuite &o); 00197 TestSuite *clone() const; 00202 void addtest(BaseTest *test); 00203 void run(BaseGatherer &g); 00204 ~TestSuite(); 00205 }; 00206 00210 class Failure : public std::runtime_error { 00211 public: 00216 Failure(const std::string &r); 00217 }; 00218 00224 void unittest_assert(bool test, const std::string &r); 00225 00233 void unittest_run(BaseTest *t, BaseGatherer &g); 00234 00242 void unittest_main(std::auto_ptr<BaseTest> t); 00243 00244 #endif