2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgunittests.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2003-01-22 00:45:36 +08:00
|
|
|
*/
|
2007-06-12 22:20:16 +08:00
|
|
|
|
2006-12-20 01:06:59 +08:00
|
|
|
#include "UnitTestFramework.h"
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace osgUtx
|
|
|
|
{
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
TestContext::TestContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestContext::setTraceLevel(TraceLevel tl)
|
|
|
|
{
|
|
|
|
_tout.setTraceLevel(tl);
|
|
|
|
}
|
|
|
|
|
|
|
|
TestContext::TraceLevel TestContext::getTraceLevel() const
|
|
|
|
{
|
|
|
|
return _tout.getTraceLevel();
|
|
|
|
}
|
|
|
|
|
2002-08-29 00:59:22 +08:00
|
|
|
std::ostream& TestContext::tout(TraceLevel tl) const
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
return _tout.stream(tl);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
TestContext::TraceStream::TraceStream(std::ostream& o, TraceLevel tl):
|
|
|
|
_traceLevel(tl),
|
|
|
|
_outputStreamPtr(&o),
|
|
|
|
#if defined(WIN32) && !(defined(__CYGWIN__) || defined(__MINGW32__))
|
|
|
|
_nullStream("nul")
|
|
|
|
#else
|
|
|
|
_nullStream("/dev/null")
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TestContext::TraceStream::~TraceStream()
|
|
|
|
{
|
|
|
|
_nullStream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestContext::TraceStream::setTraceLevel(TraceLevel tl)
|
|
|
|
{
|
|
|
|
_traceLevel = tl;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestContext::TraceLevel TestContext::TraceStream::getTraceLevel() const
|
|
|
|
{
|
|
|
|
return _traceLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& TestContext::TraceStream::stream(TestContext::TraceLevel tl)
|
|
|
|
{
|
|
|
|
if(_traceLevel >= tl){
|
|
|
|
return *_outputStreamPtr;
|
|
|
|
}
|
|
|
|
return _nullStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
TestGraph& TestGraph::instance()
|
|
|
|
{
|
|
|
|
static TestGraph instance_;
|
|
|
|
return instance_;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestSuite* TestGraph::root()
|
|
|
|
{
|
|
|
|
return root_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
TestSuite* TestGraph::suite(const std::string& path, TestSuite* tsuite, bool createIfNecessary)
|
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
list<string> pathComponents;
|
|
|
|
|
2002-08-29 00:17:54 +08:00
|
|
|
std::string::const_iterator it1 = path.begin();
|
|
|
|
std::string::const_iterator it2 = it1;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
// Dissect the path into it's constituent components
|
|
|
|
do{
|
|
|
|
|
2007-05-29 17:57:18 +08:00
|
|
|
while( it2 != path.end() && *it2 != '.' ) ++it2;
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
// Consider a check for "" empty strings?
|
2002-08-29 00:17:54 +08:00
|
|
|
pathComponents.push_back( std::string(it1,it2) );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
if( it2 != path.end()) ++it2;
|
|
|
|
|
|
|
|
it1 = it2;
|
|
|
|
|
|
|
|
}while( it2 != path.end());
|
|
|
|
|
|
|
|
return suite(pathComponents.begin(), pathComponents.end(),
|
|
|
|
tsuite, createIfNecessary);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TestSuite* TestGraph::suite(
|
2005-11-17 21:35:53 +08:00
|
|
|
std::list<std::string>::iterator it,
|
2002-08-29 00:59:22 +08:00
|
|
|
std::list<std::string>::iterator end,
|
2002-08-28 22:26:09 +08:00
|
|
|
TestSuite* tsuite, bool createIfNecessary)
|
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
if( ! tsuite) tsuite = root();
|
|
|
|
|
|
|
|
// Make sure these tie up
|
|
|
|
if(*it != tsuite->name()) return 0;
|
|
|
|
|
|
|
|
++it;
|
|
|
|
if(it == end) return tsuite;
|
|
|
|
|
|
|
|
Test* child = tsuite->findChild(*it);
|
|
|
|
|
|
|
|
if(child){
|
|
|
|
|
|
|
|
// We've found a child with the right name. But is it a
|
|
|
|
// test suite?
|
|
|
|
|
|
|
|
if(TestSuite* childSuite = dynamic_cast<TestSuite*>(child)){
|
|
|
|
return suite(it, end, childSuite, createIfNecessary);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We could return 0 here, to indicate that someone is
|
|
|
|
// trying to add a TestSuite named 'xxx' to a suite with a
|
|
|
|
// Test already named 'xxx'. But we don't enforce uniqueness
|
|
|
|
// the other way round, so we don't do it this way round
|
|
|
|
// either. Carry on as normal, and create a TestSuite of
|
|
|
|
// the same name if createIfNecessary is true.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(createIfNecessary){
|
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
TestSuite* childSuite = new TestSuite(*it);
|
2002-08-28 22:26:09 +08:00
|
|
|
tsuite->add(childSuite);
|
|
|
|
return suite(it, end, childSuite, createIfNecessary);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
TestGraph::TestGraph(): root_(new TestSuite("root"))
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool TestQualifier::visitEnter( TestSuite* pSuite )
|
|
|
|
{
|
|
|
|
_path.append( pSuite->name() );
|
|
|
|
_path += SEPCHAR;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leaving a composite: Pop its name from the Path
|
|
|
|
bool TestQualifier::visitLeave( TestSuite* pSuite )
|
|
|
|
{
|
2002-08-29 00:59:22 +08:00
|
|
|
// assert( _path.rfind( pSuite->name() + static_cast<const char>(SEPCHAR))
|
|
|
|
// == _path.size() - pSuite->name().size() - 1);
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
_path.erase( _path.size() - pSuite->name().size() -1 );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide read-only access to the current qualifier
|
2002-08-29 00:17:54 +08:00
|
|
|
const std::string& TestQualifier::currentPath() const
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
osg::Timer TestRecord::timer_;
|
|
|
|
|
|
|
|
void TestRecord::start()
|
|
|
|
{
|
|
|
|
start_ = timer_.tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRecord::stop()
|
|
|
|
{
|
|
|
|
stop_ = timer_.tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRecord::log(const TestFailureX& e)
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
result_ = Failure;
|
|
|
|
problem_ = e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRecord::log(const TestErrorX& e)
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
result_ = Error;
|
|
|
|
problem_ = e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRecord::log(const std::exception& e)
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
result_ = Error;
|
|
|
|
problem_ = e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRecord::log(const std::string& s)
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
result_ = Error;
|
|
|
|
problem_ = s;
|
|
|
|
}
|
|
|
|
|
2002-08-29 00:17:54 +08:00
|
|
|
TestRecord::TestRecord(const std::string& name):
|
2002-08-28 22:26:09 +08:00
|
|
|
name_(name),
|
|
|
|
start_(0),
|
|
|
|
stop_(0),
|
|
|
|
result_(Success),
|
|
|
|
problem_("No problem")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& o,const TestRecord& tr)
|
|
|
|
{
|
|
|
|
if(tr.result_ == TestRecord::Success) o<<"pass";
|
|
|
|
else if(tr.result_ == TestRecord::Failure) o<<"fail";
|
|
|
|
else o<<"error";
|
|
|
|
|
|
|
|
o<<"\t"<<tr.name_;
|
|
|
|
|
|
|
|
|
|
|
|
//o<<tr.start_<<'\t'<<tr.stop_<<'\t'<<TestRecord::timer_.delta_s(tr.start_,tr.stop_);
|
|
|
|
|
|
|
|
// Just print out the duration
|
|
|
|
o<<'\t'<<TestRecord::timer_.delta_s(tr.start_,tr.stop_)<<'s';
|
|
|
|
|
|
|
|
if(tr.result_ != TestRecord::Success){
|
|
|
|
o<<'\t'<<tr.problem_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
TestRunner::TestRunner( TestContext& ctx ) : _ctx( ctx )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-08-29 00:17:54 +08:00
|
|
|
void TestRunner::specify( const std::string& sQualifiedName )
|
2002-08-28 22:26:09 +08:00
|
|
|
{
|
|
|
|
_tests.push_back( sQualifiedName );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestRunner::visitEnter( TestSuite* pSuite )
|
|
|
|
{
|
|
|
|
TestQualifier::visitEnter( pSuite );
|
|
|
|
return !_ctx.shouldStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
|
|
|
|
2002-11-12 04:18:27 +08:00
|
|
|
namespace osgUtx{
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
struct isSpecified{
|
|
|
|
|
|
|
|
const std::string& pTestName_;
|
|
|
|
|
|
|
|
isSpecified(const std::string& s): pTestName_(s) {}
|
|
|
|
|
|
|
|
bool operator()(const std::string& specifiedTest){
|
|
|
|
return pTestName_.find(specifiedTest) == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
|
|
|
|
|
|
|
bool TestRunner::visit( TestCase* pTest )
|
|
|
|
{
|
|
|
|
if ( std::find_if(_tests.begin(),_tests.end(),
|
2002-11-12 04:18:27 +08:00
|
|
|
osgUtx::isSpecified(currentPath() + pTest->name() ) ) != _tests.end()) perform( pTest );
|
2002-08-28 22:26:09 +08:00
|
|
|
|
|
|
|
return !_ctx.shouldStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestRunner::visitLeave( TestSuite* pSuite )
|
|
|
|
{
|
|
|
|
TestQualifier::visitLeave( pSuite );
|
|
|
|
return !_ctx.shouldStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::perform( TestCase* pTest )
|
|
|
|
{
|
|
|
|
TestRecord& record = _db.createRecord( currentPath() + pTest->name() );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
record.start();
|
|
|
|
pTest->run( _ctx );
|
|
|
|
record.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
catch ( const TestFailureX& e )
|
|
|
|
{
|
|
|
|
record.log( e );
|
|
|
|
}
|
|
|
|
catch ( const TestErrorX& e )
|
|
|
|
{
|
|
|
|
record.log( e );
|
|
|
|
}
|
|
|
|
catch ( const std::exception& e )
|
|
|
|
{
|
|
|
|
record.log( e );
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
2002-08-29 00:59:22 +08:00
|
|
|
record.log( std::string("Unknown") );
|
2002-08-28 22:26:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_ctx.tout(TestContext::Results) << record << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
TestSuite::TestSuite( const std::string& name ) : Test( name )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSuite::add( Test* pTest )
|
|
|
|
{
|
|
|
|
_tests.push_back( pTest );
|
|
|
|
}
|
|
|
|
|
|
|
|
Test* TestSuite::findChild(const std::string& name)
|
|
|
|
{
|
|
|
|
for(Tests::iterator it = _tests.begin();
|
|
|
|
it != _tests.end();
|
|
|
|
++it){
|
|
|
|
|
|
|
|
if ((*it)->name() == name) return (*it).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestSuite::accept( Test::Visitor& v )
|
|
|
|
{
|
|
|
|
if ( v.visitEnter( this ) )
|
|
|
|
{
|
|
|
|
Tests::iterator end = _tests.end();
|
|
|
|
for ( Tests::iterator at = _tests.begin(); at != end; ++at )
|
|
|
|
if ( !(*at)->accept( v ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.visitLeave( this ); // continue with siblings?
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool QualifiedTestPrinter::visit( TestCase* pTest )
|
|
|
|
{
|
2004-03-03 21:27:21 +08:00
|
|
|
osg::notify(osg::NOTICE) << currentPath() + pTest->name() << std::endl;
|
2002-08-28 22:26:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2007-04-06 23:36:13 +08:00
|
|
|
}
|