2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
#ifndef OSG_UNITTESTFRAMEWORK
|
|
|
|
#define OSG_UNITTESTFRAMEWORK 1
|
|
|
|
|
|
|
|
#include <osg/Export>
|
|
|
|
#include <osg/Referenced>
|
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Timer>
|
2004-03-03 21:27:21 +08:00
|
|
|
#include <osg/Notify>
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
#include <fstream>
|
|
|
|
|
2004-11-27 00:01:04 +08:00
|
|
|
/**
|
|
|
|
|
|
|
|
\namespace osgUtx
|
|
|
|
|
|
|
|
The osgUtx is a unit test framework.
|
|
|
|
*/
|
|
|
|
|
2002-08-28 22:26:09 +08:00
|
|
|
namespace osgUtx{
|
|
|
|
|
|
|
|
class TestVisitor;
|
|
|
|
|
2004-11-27 00:01:04 +08:00
|
|
|
|
2002-08-28 22:26:09 +08:00
|
|
|
/**
|
|
|
|
Test, an abstract base class, is the Composite pattern's \em component
|
|
|
|
class for our graph of test cases, and defines the basic interface
|
|
|
|
for all Test components. It is a referent, and may be pointed
|
|
|
|
to by an osg::ref_ptr.
|
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT Test: public osg::Referenced
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
typedef TestVisitor Visitor; // Test is redundant
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
Test( const std::string& sName ) : _name( sName ) {}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
const std::string& name() const { return _name; }
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
virtual bool accept( Visitor& ) = 0;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~Test() {}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
std::string _name;
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
TestContext wraps up information which is passed to tests as they are run,
|
|
|
|
and may contain test-specific information or 'global' test objects, such
|
|
|
|
as an output stream for verbose output during the running of tests.
|
|
|
|
|
|
|
|
\todo Improve the output stream code by providing a filtering stream.
|
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT TestContext
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestContext();
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
bool shouldStop() { return false; }
|
|
|
|
bool isVerbose() { return true; }
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
enum TraceLevel{
|
|
|
|
Off, ///< All tracing turned off
|
|
|
|
Results, ///< Output results only
|
|
|
|
Full ///< Full test diagnostic output
|
|
|
|
};
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
void setTraceLevel(TraceLevel tl);
|
|
|
|
TraceLevel getTraceLevel() const;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
std::ostream& tout(TraceLevel tl=Full) const;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestContext(const TestContext&);
|
|
|
|
TestContext operator=(const TestContext&);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
|
|
|
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT TraceStream{
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
public:
|
|
|
|
TraceStream(std::ostream& o=osg::notify(osg::NOTICE), TraceLevel tl=Results);
|
|
|
|
~TraceStream();
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
void setTraceLevel(TraceLevel tl);
|
|
|
|
TraceLevel getTraceLevel() const;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
std::ostream& stream(TraceLevel tl);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
private:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TraceLevel _traceLevel;
|
|
|
|
std::ostream* _outputStreamPtr;
|
|
|
|
std::ofstream _nullStream;
|
|
|
|
};
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
mutable TraceStream _tout;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TestSuite;
|
|
|
|
class TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Visits while maintaining the current hierarchical context. Also allows
|
2004-09-13 23:14:11 +08:00
|
|
|
the traversal to be short-circuited at any point during the visitation.
|
2002-08-28 22:26:09 +08:00
|
|
|
*/
|
|
|
|
class TestVisitor
|
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
//..Should we enter this node and its children?
|
|
|
|
virtual bool visitEnter( TestSuite* ) { return true; }
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
//..Returns true to continue to next Leaf
|
|
|
|
virtual bool visit( TestCase* ) = 0;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
//..Returns true to continue to next Composite
|
|
|
|
virtual bool visitLeave( TestSuite* ) { return true; }
|
2003-01-10 17:25:42 +08:00
|
|
|
|
|
|
|
protected:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestVisitor() {}
|
|
|
|
TestVisitor( const TestVisitor& ) {}
|
|
|
|
virtual ~TestVisitor() {}
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2004-09-13 23:14:11 +08:00
|
|
|
TestCase, supplies the interface for a Composite pattern's
|
2002-08-28 22:26:09 +08:00
|
|
|
\em leaf class, though it is not a leaf in itself.
|
|
|
|
*/
|
|
|
|
class TestCase : public Test
|
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
typedef TestContext Context; // Test in TestContext? is redundant
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestCase( const std::string& sName ) : Test( sName ) {}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
virtual bool accept( Visitor& v ) { return v.visit( this ); }
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
virtual void run( const Context& ) = 0; // Subclass OSGUTX_EXPORT Responsibility
|
2003-01-10 17:25:42 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~TestCase() {}
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2004-09-13 23:14:11 +08:00
|
|
|
Base class catchable for the exceptions which may be thrown to
|
2002-08-28 22:26:09 +08:00
|
|
|
indicate problems during the run of a TestCase.
|
|
|
|
*/
|
|
|
|
class TestX
|
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestX(const std::string& s):_what(s) {}
|
|
|
|
virtual ~TestX() {}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
const std::string& what() const { return _what; }
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
private:
|
2004-03-03 21:27:21 +08:00
|
|
|
std::string _what;
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
A TestFailureX indicates a failure in the tested component.
|
|
|
|
*/
|
|
|
|
class TestFailureX: public TestX
|
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2004-03-03 21:27:21 +08:00
|
|
|
TestFailureX(const std::string& s):TestX(s) {}
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
A TestErrorX indicates an error while testing a component,
|
2004-09-13 23:14:11 +08:00
|
|
|
which prevents the test from being run. It does not indicate
|
2002-08-28 22:26:09 +08:00
|
|
|
a problem with the component, but rather a problem during the
|
|
|
|
run which prevents the component from being tested.
|
|
|
|
*/
|
|
|
|
class TestErrorX: public TestX
|
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2004-03-03 21:27:21 +08:00
|
|
|
TestErrorX(const std::string& s):TestX(s) {}
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
TestCase_ is a class template for a leaf TestCase, which allows TestFixture
|
|
|
|
classes to be easily collected into the tree of tests, and have their public
|
|
|
|
test methods called. It is worth noting that, for a given TestCase_, an
|
2004-09-13 23:14:11 +08:00
|
|
|
instance of the test fixture class will be constructed prior to the
|
2002-08-28 22:26:09 +08:00
|
|
|
test method being called, and destructed afterwards. This prevents 'leakage'
|
|
|
|
of information from one test case to the next.
|
|
|
|
*/
|
|
|
|
template< typename FixtureT >
|
|
|
|
class TestCase_ : public TestCase
|
|
|
|
{
|
2004-03-03 21:27:21 +08:00
|
|
|
typedef void (FixtureT::*TestMethodPtr)( const Context& );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// Constructor adds the TestMethod pointer
|
|
|
|
TestCase_( const std::string& sName, TestMethodPtr pTestMethod ) :
|
|
|
|
TestCase( sName ),
|
|
|
|
_pTestMethod( pTestMethod )
|
|
|
|
{
|
|
|
|
}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// Create a TestFixture instance and invoke TestMethod?
|
|
|
|
virtual void run( const Context& ctx )
|
|
|
|
{
|
|
|
|
( FixtureT().*_pTestMethod )( ctx );
|
|
|
|
}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~TestCase_() {}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestMethodPtr _pTestMethod;
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
A TestSuite is the \em composite component of the Composite pattern,
|
|
|
|
and allows aggregation of Tests into hierarchies.
|
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT TestSuite : public Test
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestSuite( const std::string& name );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
/** Adds a Test to the suite. */
|
|
|
|
void add( Test* pTest );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
/**
|
|
|
|
@returns The immediate child denoted by name, or 0 if not found.
|
|
|
|
*/
|
|
|
|
Test* findChild(const std::string& name);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
virtual bool accept( Test::Visitor& v );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~TestSuite() {}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<Test> > Tests;
|
|
|
|
Tests _tests; // Collection of Suites and/or Cases
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
TestGraph is a singleton providing central access to the tree of tests;
|
|
|
|
primarily, it provides access to the root suite.
|
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT TestGraph
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
static TestGraph& instance();
|
|
|
|
|
|
|
|
/**
|
|
|
|
@return a pointer to the root TestSuite.
|
|
|
|
*/
|
|
|
|
TestSuite* root();
|
|
|
|
|
|
|
|
/**
|
2004-09-13 23:14:11 +08:00
|
|
|
A utility function for accessing an arbitrary suite by pathname, relative to
|
2004-03-03 21:27:21 +08:00
|
|
|
the suite 'tsuite' (defaults to root if null), and with the option of creating
|
|
|
|
the \em TestSuite designated by \em path, if it does not already exist.
|
|
|
|
|
|
|
|
This method may return 0 if the suite either cannot be found (and createIfNecssary
|
|
|
|
is 0), or the first component of \em path is not the same as the name of the
|
|
|
|
TestSuite \em tsuite.
|
|
|
|
|
|
|
|
This was written to aid the auto-registration of tests at specific points in
|
|
|
|
the test tree, where the tests' AutoRegistrationAgents may be distributed across
|
|
|
|
several files, and cannot be guaranteed to run in a given order. E.g. You cannot
|
|
|
|
register a test "root.osg.MyTest" unless you know that the the suite "root.osg"
|
|
|
|
already exists.
|
|
|
|
|
|
|
|
|
|
|
|
@param path The name of the TestSuite to return.
|
|
|
|
@param tsuite The suite to 'start from'. Path is relative to this
|
|
|
|
suite (defaults to root suite).
|
|
|
|
@param createIfNecessary Optionally create the TestSuite(s) denoted by path if
|
|
|
|
they do not exist.
|
|
|
|
*/
|
|
|
|
TestSuite* suite(const std::string& path, TestSuite* tsuite = 0,bool createIfNecessary = false);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
private:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
/**
|
|
|
|
Does the same job as the version of suite listed above, but the path
|
|
|
|
is passed in as components in a list, represented by the iterator parameters.
|
|
|
|
*/
|
|
|
|
TestSuite* suite(
|
|
|
|
std::list<std::string>::iterator it,
|
|
|
|
std::list<std::string>::iterator end,
|
|
|
|
TestSuite* tsuite, bool createIfNecessary);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestGraph();
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestGraph(const TestGraph&);
|
|
|
|
TestGraph& operator=(const TestGraph&);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
osg::ref_ptr<TestSuite> root_;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Maintains a string that when accessed in the "visit" member, returns the
|
|
|
|
current qualified TestSuite path.
|
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT TestQualifier : public TestVisitor
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
2004-03-03 21:27:21 +08:00
|
|
|
enum { SEPCHAR = '.' };
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// Entering a composite: Push its name on the Path
|
|
|
|
virtual bool visitEnter( TestSuite* pSuite );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// Leaving a composite: Pop its name from the Path
|
|
|
|
virtual bool visitLeave( TestSuite* pSuite );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// Provide read-only access to the current qualifier
|
|
|
|
const std::string& currentPath() const;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
private:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
std::string _path; // Current qualifier
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
QualifiedTestPrinter prints to standard output a list of fully
|
|
|
|
qualified tests.
|
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT QualifiedTestPrinter : public TestQualifier
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
virtual bool visit( TestCase* pTest );
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
A TestRecord records the output of a given test case, i.e. its start/stop time,
|
|
|
|
its result, and a textual description of any problems.
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
\todo Consider adding accessor methods if necessary, to get the details
|
|
|
|
stored in the TestRecord.
|
2002-08-28 22:26:09 +08:00
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT TestRecord
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
2004-03-03 21:27:21 +08:00
|
|
|
public:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
void log(const TestFailureX& e);
|
|
|
|
void log(const TestErrorX& e);
|
|
|
|
void log(const std::exception& e);
|
|
|
|
void log(const std::string& s);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// Default copy construction and assignment are OK
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// FIXME: Add accessors?
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
private:
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// Onlye a TestReport can create a TestRecord
|
|
|
|
friend class TestReport;
|
|
|
|
TestRecord(const std::string& name);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
enum Result{
|
|
|
|
Success,Failure,Error
|
|
|
|
};
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
friend std::ostream& operator<<(std::ostream& o,const TestRecord& tr);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
static osg::Timer timer_; // To time tests
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
std::string name_;
|
|
|
|
osg::Timer_t start_;
|
|
|
|
osg::Timer_t stop_;
|
|
|
|
Result result_;
|
|
|
|
std::string problem_;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
A TestReport represents the complete set of results (TestRecords) for a
|
|
|
|
given test run.
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
\todo Add support for printing the test report in various formats:
|
|
|
|
e.g. text, XML, CSV
|
2002-08-28 22:26:09 +08:00
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT TestReport
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestRecord& createRecord(const std::string& s){
|
|
|
|
_records.push_back(TestRecord(s));
|
|
|
|
return _records.back();
|
|
|
|
}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
private:
|
2004-03-03 21:27:21 +08:00
|
|
|
std::list<TestRecord> _records;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
A TestRunner is a visitor which will run specified tests as it traverses the
|
|
|
|
test graph.
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
\todo Consider an accessor method to get at the TestReport if necessary.
|
2002-08-28 22:26:09 +08:00
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT TestRunner : public TestQualifier
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestRunner( TestContext& ctx );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
/**
|
|
|
|
Tests may be specified by partial names. E.g. specifiying "root"
|
|
|
|
will run all tests below root, i.e. all tests.
|
|
|
|
Specifiying "root.osg" will run all tests below \em root.osg.
|
|
|
|
Specifying "root.osg.de" will run all tests (and suites) below
|
|
|
|
\em root.osg with names beginning with the \em de.
|
|
|
|
*/
|
|
|
|
void specify( const std::string& sQualifiedName );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
bool visitEnter( TestSuite* pSuite );
|
|
|
|
bool visit( TestCase* pTest );
|
|
|
|
bool visitLeave( TestSuite* pSuite );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
void perform( TestCase* pTest );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
TestReport _db; // Results
|
|
|
|
TestContext& _ctx; // The Global Testing Context
|
|
|
|
std::vector<std::string> _tests; // Specified Tests
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Starts a TestSuite singleton function
|
|
|
|
@see OSGUTX_ADD_TESTCASE, OSGUTX_END_TESTSUITE
|
|
|
|
*/
|
|
|
|
#define OSGUTX_BEGIN_TESTSUITE( tsuite ) \
|
2004-03-03 21:27:21 +08:00
|
|
|
osgUtx::TestSuite* tsuite##_TestSuite() \
|
|
|
|
{ \
|
|
|
|
static osg::ref_ptr<osgUtx::TestSuite> s_suite = 0; \
|
|
|
|
if ( s_suite == 0 ) { \
|
|
|
|
s_suite = new osgUtx::TestSuite( #tsuite );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Adds a test case to a suite object being created in a TestSuite singleton function.
|
|
|
|
@see OSGUTX_BEGIN_TESTSUITE, OSGUTX_END_TESTSUITE
|
|
|
|
*/
|
|
|
|
#define OSGUTX_ADD_TESTCASE( tfixture, tmethod ) \
|
2004-03-03 21:27:21 +08:00
|
|
|
s_suite->add( new osgUtx::TestCase_<tfixture>( \
|
|
|
|
#tmethod, &tfixture::tmethod ) );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Ends a TestSuite singleton function
|
|
|
|
@see OSGUTX_BEGIN_TESTSUITE, OSGUTX_ADD_TESTCASE
|
|
|
|
*/
|
|
|
|
#define OSGUTX_END_TESTSUITE \
|
2004-03-03 21:27:21 +08:00
|
|
|
} \
|
|
|
|
return s_suite.get(); \
|
|
|
|
}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
/** Define a TestSuite accessor */
|
|
|
|
#define OSGUTX_TESTSUITE( tsuite ) \
|
2004-03-03 21:27:21 +08:00
|
|
|
tsuite##_TestSuite()
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Adds a suite to a suite - allows composition of test suites.
|
|
|
|
@see OSGUTX_BEGIN_TESTSUITE, OSGUTX_END_TESTSUITE
|
|
|
|
*/
|
|
|
|
#define OSGUTX_ADD_TESTSUITE( childSuite ) \
|
2004-03-03 21:27:21 +08:00
|
|
|
s_suite->add( childSuite##_TestSuite() );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
/** Autoregister a testsuite with the root suite at startup */
|
|
|
|
#define OSGUTX_AUTOREGISTER_TESTSUITE( tsuite ) \
|
2004-03-03 21:27:21 +08:00
|
|
|
static osgUtx::TestSuiteAutoRegistrationAgent tsuite##_autoRegistrationObj__( tsuite##_TestSuite() );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
/** Auto register a testsuite with at designated point in the suite graph at startup */
|
|
|
|
#define OSGUTX_AUTOREGISTER_TESTSUITE_AT( tsuite , path ) \
|
2004-03-03 21:27:21 +08:00
|
|
|
static osgUtx::TestSuiteAutoRegistrationAgent tsuite##_autoRegistrationObj__( tsuite##_TestSuite(), #path );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
namespace osgUtx{
|
|
|
|
|
|
|
|
/**
|
|
|
|
A helper struct to perform automatic registration at program startup; not for
|
|
|
|
direct use, it should be used via the following macros. (It's a secret agent :-)
|
|
|
|
|
|
|
|
@see OSGUTX_AUTOREGISTER_TESTSUITE, OSGUTX_AUTOREGISTER_TESTSUITE_AT
|
|
|
|
*/
|
|
|
|
struct TestSuiteAutoRegistrationAgent
|
|
|
|
{
|
2004-03-03 21:27:21 +08:00
|
|
|
TestSuiteAutoRegistrationAgent(TestSuite* tsuite, const char* path = 0)
|
|
|
|
{
|
|
|
|
if( ! path ) path = "root";
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
// Find the suite named in 'path', create it if necessary
|
|
|
|
TestSuite *regSuite = osgUtx::TestGraph::instance().suite( path, 0, true );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
if(!regSuite){
|
|
|
|
osg::notify(osg::WARN)<<"Warning, unable to register test suite named \""<<tsuite->name()<<"\" at "
|
|
|
|
<<path<<", falling back to root suite."<<std::endl;
|
|
|
|
regSuite = osgUtx::TestGraph::instance().root();
|
|
|
|
}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
2004-03-03 21:27:21 +08:00
|
|
|
regSuite->add(tsuite);
|
|
|
|
}
|
2002-08-28 22:26:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
OSGUTX_TEST_F is a convenience macro, analogous to assert(), which will
|
|
|
|
throw an osgUtx::TestFailureX if \em expr evaluates to false; this should be
|
|
|
|
used to test for failure in a given test, as opposed to an actual error
|
|
|
|
in the test owing to some other reason than the tested code being faulty.
|
|
|
|
|
|
|
|
The exception will indicate the file and line number of the failed expression,
|
|
|
|
along with expression itself.
|
|
|
|
*/
|
|
|
|
#define OSGUTX_TEST_F( expr ) \
|
2004-03-03 21:27:21 +08:00
|
|
|
if( !(expr) ){ \
|
|
|
|
std::stringstream ss; \
|
|
|
|
ss<< #expr <<" failure: "<<__FILE__<<", line "<<__LINE__<<std::ends; \
|
|
|
|
throw osgUtx::TestFailureX(ss.str()); \
|
|
|
|
}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
OSGUTX_TEST_E is a convenience macro, analogous to assert(), which will
|
|
|
|
throw an osgUtx::TestErrorX if \em expr evaluates to false; this should be
|
|
|
|
used to test for an error in a given test, as opposed to a failure
|
|
|
|
in the tested code.
|
|
|
|
|
|
|
|
The exception will indicate the file and line number of the failed expression,
|
|
|
|
along with expression itself.
|
|
|
|
*/
|
|
|
|
#define OSGUTX_TEST_E( expr ) \
|
2004-03-03 21:27:21 +08:00
|
|
|
if( !(expr) ){ \
|
|
|
|
std::stringstream ss; \
|
|
|
|
ss<< #expr <<" error: "<<__FILE__<<", line "<<__LINE__<<std::ends; \
|
|
|
|
throw osgUtx::TestErrorX(ss.str()); \
|
|
|
|
}
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
#endif // OSG_UNITTESTFRAMEWORK
|