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.4 2010/03/26 04:45:14 hartwork 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 #include "cpptest-source.h"
00038 
00039 namespace Test
00040 {
00041     class Output;
00042     
00052     class Suite
00053     {
00054     public: 
00055         Suite();
00056         virtual ~Suite();
00057         
00058         void add(std::auto_ptr<Suite> suite);
00059         
00060         bool run(Output& output, bool cont_after_fail = true);
00061         
00062     protected:
00065         typedef void (Suite::*Func)();
00066         
00067         bool continue_after_failure() const { return _continue; }
00068         
00069         virtual void setup()     {}
00070         virtual void tear_down() {}
00071         
00072         void register_test(Func func, const std::string& name);
00073         void assertment(Source s);
00074         
00075     private:
00076         struct DoRun;
00077         struct ExecTests;
00078         struct SubSuiteTests;
00079         struct SuiteTime;
00080         struct SubSuiteTime;
00081         
00082         friend struct DoRun;
00083         friend struct ExecTests;
00084         friend struct SubSuiteTests;
00085         friend struct SubSuiteTime;
00086         
00087         struct Data
00088         {
00089             Func            _func;
00090             std::string     _name;
00091             Time            _time;
00092         
00093             Data(Func func, const std::string& name)
00094                 : _func(func), _name(name) {}
00095         };
00096         
00097         typedef std::list<Data>     Tests;
00098         typedef std::list<Suite*>   Suites;
00099         
00100         std::string         _name;          // Suite name
00101         const std::string*  _cur_test;      // Current test func name
00102         Suites              _suites;        // External test suites
00103         Tests               _tests;         // All tests
00104         Output*             _output;        // Output handler
00105         bool                _result   : 1;  // Test result
00106         bool                _success  : 1;  // Set if no test failed
00107         bool                _continue : 1;  // Continue func after failures
00108         
00109         void do_run(Output* os, bool cont_after_fail);
00110         int total_tests() const;
00111         Time total_time(bool recursive) const;
00112         
00113         // Disable
00114         //
00115         Suite(const Suite&);
00116         Suite& operator=(const Suite&);
00117     };
00118 
00134     #define TEST_ADD(func)\
00135         register_test(static_cast<Func>(&func), #func);
00136     
00137 } // namespace Test
00138 
00139 #endif // #ifndef CPPTEST_SUITE_H
00140 

Supported by:

SourceForge Logo