CppTest home page CppTest project page

cpptest-suite.h

Go to the documentation of this file.
00001 // ---
00002 //
00003 // $Id: cpptest-suite.h,v 1.3 2005/06/08 08:08:06 nilu Exp $
00004 //
00005 // CppTest - A C++ Unit Testing Framework
00006 // Copyright (c) 2003 Niklas Lundell
00007 //
00008 // ---
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the
00022 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023 // Boston, MA 02111-1307, USA.
00024 //
00025 // ---
00026 
00029 #ifndef CPPTEST_SUITE_H
00030 #define CPPTEST_SUITE_H
00031 
00032 #include <list>
00033 #include <memory>
00034 #include <string>
00035 
00036 #include "cpptest-time.h"
00037 
00038 namespace Test
00039 {
00040     class Output;
00041     
00051     class Suite
00052     {
00053     public: 
00054         Suite();
00055         virtual ~Suite();
00056         
00057         void add(std::auto_ptr<Suite> suite);
00058         
00059         bool run(Output& output, bool cont_after_fail = true);
00060         
00061     protected:
00064         typedef void (Suite::*Func)();
00065         
00066         bool continue_after_failure() const { return _continue; }
00067         
00068         virtual void setup()     {}
00069         virtual void tear_down() {}
00070         
00071         void register_test(Func func, const std::string& name);
00072         void assertment(Source s);
00073         
00074     private:
00075         struct DoRun;
00076         struct ExecTests;
00077         struct SubSuiteTests;
00078         struct SuiteTime;
00079         struct SubSuiteTime;
00080         
00081         friend struct DoRun;
00082         friend struct ExecTests;
00083         friend struct SubSuiteTests;
00084         friend struct SubSuiteTime;
00085         
00086         struct Data
00087         {
00088             Func            _func;
00089             std::string     _name;
00090             Time            _time;
00091         
00092             Data(Func func, const std::string& name)
00093                 : _func(func), _name(name) {}
00094         };
00095         
00096         typedef std::list<Data>     Tests;
00097         typedef std::list<Suite*>   Suites;
00098         
00099         std::string         _name;          // Suite name
00100         const std::string*  _cur_test;      // Current test func name
00101         Suites              _suites;        // External test suites
00102         Tests               _tests;         // All tests
00103         Output*             _output;        // Output handler
00104         bool                _result   : 1;  // Test result
00105         bool                _success  : 1;  // Set if no test failed
00106         bool                _continue : 1;  // Continue func after failures
00107         
00108         void do_run(Output* os, bool cont_after_fail);
00109         int total_tests() const;
00110         Time total_time(bool recursive) const;
00111         
00112         // Disable
00113         //
00114         Suite(const Suite&);
00115         Suite& operator=(const Suite&);
00116     };
00117 
00133     #define TEST_ADD(func)\
00134         register_test(static_cast<Func>(&func), #func);
00135     
00136 } // namespace Test
00137 
00138 #endif // #ifndef CPPTEST_SUITE_H
00139 

Supported by:

SourceForge Logo