2007-08-13 09:55:32 +02:00
|
|
|
/*
|
|
|
|
|
Suite - helper class for running collections of tests
|
|
|
|
|
|
2008-03-10 04:25:03 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2007-08-13 09:55:32 +02:00
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program 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
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
2007-08-26 19:14:39 +02:00
|
|
|
|
2007-08-13 09:55:32 +02:00
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <tr1/memory>
|
2007-08-17 00:36:07 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <sstream>
|
2007-08-23 17:52:33 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2007-08-17 00:36:07 +02:00
|
|
|
|
2008-11-26 05:19:59 +01:00
|
|
|
#include "include/nobugcfg.hpp"
|
|
|
|
|
#include "lib/cmdline.hpp"
|
2007-08-19 21:57:19 +02:00
|
|
|
#include "common/test/suite.hpp"
|
|
|
|
|
#include "common/test/run.hpp"
|
2008-11-26 05:19:59 +01:00
|
|
|
#include "include/error.hpp"
|
2007-08-17 00:36:07 +02:00
|
|
|
#include "common/util.hpp"
|
|
|
|
|
|
2007-08-13 09:55:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
|
{
|
|
|
|
|
using std::map;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using std::auto_ptr;
|
|
|
|
|
using std::tr1::shared_ptr;
|
2007-08-23 17:52:33 +02:00
|
|
|
using boost::algorithm::trim;
|
2007-08-17 00:36:07 +02:00
|
|
|
|
|
|
|
|
using util::isnil;
|
2007-08-23 17:52:33 +02:00
|
|
|
using util::contains;
|
2007-08-13 09:55:32 +02:00
|
|
|
|
|
|
|
|
typedef map<string, Launcher*> TestMap;
|
|
|
|
|
typedef shared_ptr<TestMap> PTestMap;
|
|
|
|
|
typedef map<string,PTestMap> GroupMap;
|
2007-08-17 00:36:07 +02:00
|
|
|
|
2007-08-13 09:55:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/** helper to collect and manage the test cases.
|
|
|
|
|
* Every testcase class should create a Launch instance
|
|
|
|
|
* which causes a call to Suite::enroll(), so we can add a
|
|
|
|
|
* pointer to this Launcher into a map indexed by the
|
|
|
|
|
* provided testIDs and groupIDs.
|
|
|
|
|
* This enables us to build a Suite instance for any
|
|
|
|
|
* requested group and then instantiiate and invoke
|
|
|
|
|
* individual testcases accordingly.
|
|
|
|
|
*/
|
|
|
|
|
class Registry
|
|
|
|
|
{
|
|
|
|
|
auto_ptr<GroupMap> groups;
|
|
|
|
|
public:
|
|
|
|
|
Registry() : groups(new GroupMap ) {};
|
|
|
|
|
PTestMap& getGroup (string grpID) { return (*groups)[grpID]; };
|
|
|
|
|
void add2group (Launcher* test, string testID, string groupID);
|
|
|
|
|
};
|
|
|
|
|
|
2007-08-14 08:14:21 +02:00
|
|
|
void
|
|
|
|
|
Registry::add2group (Launcher* test, string testID, string groupID)
|
2007-08-26 19:14:39 +02:00
|
|
|
{
|
|
|
|
|
REQUIRE( test );
|
|
|
|
|
REQUIRE( !isnil(testID) );
|
|
|
|
|
REQUIRE( !isnil(groupID) );
|
|
|
|
|
|
|
|
|
|
PTestMap& group = getGroup(groupID);
|
|
|
|
|
if (!group)
|
|
|
|
|
group.reset( new TestMap );
|
|
|
|
|
(*group)[testID] = test;
|
|
|
|
|
}
|
2007-08-13 09:55:32 +02:00
|
|
|
|
|
|
|
|
Registry testcases;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** register the given test-launcher, so it can be later accessed
|
|
|
|
|
* either as a member of one of the specified groups, or direcly
|
|
|
|
|
* by its testID. Any test is automatically added to the groupID
|
|
|
|
|
* #ALLGROUP
|
2007-08-24 16:41:16 +02:00
|
|
|
* @param test the Launcher object used to run this test
|
|
|
|
|
* @param testID unique ID to refere to this test (will be used as std::map key)
|
2007-08-13 09:55:32 +02:00
|
|
|
* @param groups List of group-IDs selected by whitespace
|
2007-08-24 16:41:16 +02:00
|
|
|
*
|
2007-08-13 09:55:32 +02:00
|
|
|
*/
|
2007-08-14 08:14:21 +02:00
|
|
|
void
|
|
|
|
|
Suite::enroll (Launcher* test, string testID, string groups)
|
2007-08-26 19:14:39 +02:00
|
|
|
{
|
|
|
|
|
REQUIRE( test );
|
|
|
|
|
REQUIRE( !isnil(testID) );
|
|
|
|
|
|
|
|
|
|
std::istringstream ss(groups);
|
|
|
|
|
string group;
|
|
|
|
|
while (ss >> group )
|
|
|
|
|
testcases.add2group(test, testID, group);
|
|
|
|
|
|
|
|
|
|
// Magic: allways add any testcas to groupID="ALL"
|
|
|
|
|
testcases.add2group(test,testID, ALLGROUP);
|
|
|
|
|
}
|
2007-08-13 09:55:32 +02:00
|
|
|
|
|
|
|
|
/** "magic" groupID containing all registered testcases */
|
|
|
|
|
const string Suite::ALLGROUP = "ALL";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** create a suite comprised of all the testcases
|
|
|
|
|
* previously @link #enroll() registered @endlink with this
|
|
|
|
|
* this group.
|
|
|
|
|
* @see #run() running tests in a Suite
|
|
|
|
|
*/
|
|
|
|
|
Suite::Suite(string groupID)
|
|
|
|
|
: groupID_(groupID)
|
2007-08-26 19:14:39 +02:00
|
|
|
{
|
|
|
|
|
REQUIRE( !isnil(groupID) );
|
|
|
|
|
TRACE(test, "Test-Suite( groupID=%s )\n", groupID.c_str () );
|
|
|
|
|
|
|
|
|
|
if (!testcases.getGroup(groupID))
|
2008-03-10 06:09:44 +01:00
|
|
|
throw lumiera::error::Invalid ();
|
2007-08-26 19:14:39 +02:00
|
|
|
//throw "empty testsuite"; /////////// TODO Errorhandling!
|
|
|
|
|
}
|
2007-08-13 09:55:32 +02:00
|
|
|
|
2007-08-23 17:52:33 +02:00
|
|
|
#define VALID(test,testID) \
|
|
|
|
|
ASSERT ((test), "NULL testcase laucher for test '%s' found in testsuite '%s'", groupID_.c_str(),testID.c_str());
|
|
|
|
|
|
2007-08-13 09:55:32 +02:00
|
|
|
|
|
|
|
|
/** run all testcases contained in this Suite.
|
2007-08-23 17:52:33 +02:00
|
|
|
* The first argument in the commandline, if present,
|
|
|
|
|
* will select one single testcase with a matching ID.
|
|
|
|
|
* In case of invoking a single testcase, the given cmdline
|
|
|
|
|
* will be forwarded to the testcase, after removind the
|
|
|
|
|
* testcaseID from cmdline[0]. Otherwise, every testcase
|
|
|
|
|
* in this suite is invoked with a empty cmdline vector.
|
|
|
|
|
* @param cmdline ref to the vector of commandline tokens
|
2007-08-13 09:55:32 +02:00
|
|
|
*/
|
2007-08-14 08:14:21 +02:00
|
|
|
void
|
2007-08-23 17:52:33 +02:00
|
|
|
Suite::run (Arg cmdline)
|
2007-08-26 19:14:39 +02:00
|
|
|
{
|
|
|
|
|
PTestMap tests = testcases.getGroup(groupID_);
|
|
|
|
|
if (!tests)
|
2008-03-10 06:09:44 +01:00
|
|
|
throw lumiera::error::Invalid (); ///////// TODO: pass error description
|
2007-08-26 19:14:39 +02:00
|
|
|
|
|
|
|
|
if (0 < cmdline.size())
|
|
|
|
|
{
|
|
|
|
|
string& testID (cmdline[0]);
|
|
|
|
|
trim(testID);
|
|
|
|
|
if ( contains (*tests, testID))
|
|
|
|
|
{
|
|
|
|
|
// first cmdline argument denotes a valid testcase registered in
|
|
|
|
|
// this group: invoke just this test with the remaining cmdline
|
|
|
|
|
Launcher* test = (*tests)[testID];
|
|
|
|
|
cmdline.erase (cmdline.begin());
|
|
|
|
|
VALID (test,testID);
|
|
|
|
|
(*test)()->run(cmdline);
|
|
|
|
|
return;
|
|
|
|
|
} }
|
|
|
|
|
|
|
|
|
|
// no test-ID was specified.
|
|
|
|
|
// instantiiate all tests cases and execute them.
|
|
|
|
|
for ( TestMap::iterator i=tests->begin(); i!=tests->end(); ++i )
|
|
|
|
|
{
|
|
|
|
|
std::cout << "\n ----------"<< i->first<< "----------\n";
|
|
|
|
|
Launcher* test = (i->second);
|
|
|
|
|
VALID (test, i->first);
|
|
|
|
|
(*test)()->run(cmdline); // actually no cmdline arguments
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-23 17:52:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/** print to stdout an ennumeration of all testcases in this suite,
|
|
|
|
|
* in a format suitable for use with Cehteh's ./test.sh
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
Suite::describe ()
|
2007-08-26 19:14:39 +02:00
|
|
|
{
|
|
|
|
|
util::Cmdline noCmdline("");
|
|
|
|
|
PTestMap tests = testcases.getGroup(groupID_);
|
|
|
|
|
ASSERT (tests);
|
|
|
|
|
|
|
|
|
|
std::cout << "TESTING \"Component Test Suite: " << groupID_ << "\" ./test-components\n\n";
|
2007-08-23 17:52:33 +02:00
|
|
|
|
2007-08-26 19:14:39 +02:00
|
|
|
for ( TestMap::iterator i=tests->begin(); i!=tests->end(); ++i )
|
|
|
|
|
{
|
|
|
|
|
string key (i->first);
|
|
|
|
|
std::cout << "\n\n";
|
|
|
|
|
std::cout << "TEST \""<<key<<"\" "<<key<<" <<END\n";
|
|
|
|
|
Launcher* test = (i->second);
|
|
|
|
|
VALID (test, i->first);
|
2007-09-25 23:39:46 +02:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
(*test)()->run(noCmdline); // run it to insert test generated output
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
2008-03-10 06:09:44 +01:00
|
|
|
std::cout << "PLANNED ============= " << lumiera_error() << "\n";
|
2007-09-25 23:39:46 +02:00
|
|
|
}
|
2007-08-26 19:14:39 +02:00
|
|
|
std::cout << "END\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-13 09:55:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace test
|