Dummy unit test

This commit is contained in:
HanzZ 2011-08-21 13:41:23 +02:00
parent 60219dd55c
commit a00d7859f6
7 changed files with 105 additions and 3 deletions

View file

@ -137,6 +137,13 @@ ADD_SUBDIRECTORY(backends)
#ADD_SUBDIRECTORY(tests)
ADD_SUBDIRECTORY(spectrum_manager)
if (CPPUNIT_FOUND)
message("tests : yes")
include_directories(${CPPUNIT_INCLUDE_DIR})
else()
message("tests : no (install CPPUnit)")
endif()
if(DOXYGEN_FOUND)
message("Docs : yes")
ADD_SUBDIRECTORY(docs)

View file

@ -62,6 +62,10 @@ class Config {
/// \param opts extra options which will be recognized by a parser
bool load(const std::string &configfile, boost::program_options::options_description &opts);
bool load(std::istream &ifs, boost::program_options::options_description &opts);
bool load(std::istream &ifs);
/// Loads data from config file.
/// This function loads only config variables needed by libtransport.

View file

@ -7,6 +7,14 @@ if (PROTOBUF_FOUND)
PROTOBUF_GENERATE_CPP(PROTOBUF_SRC PROTOBUF_HDRS "pbnetwork.proto")
endif()
if (CPPUNIT_FOUND)
FILE(GLOB SRC_TEST tests/*.cpp)
ADD_EXECUTABLE(libtransport_test ${SRC_TEST})
target_link_libraries(libtransport_test transport ${CPPUNIT_LIBRARIES} ${Boost_LIBRARIES})
endif()
# SOURCE_GROUP(headers FILES ${HEADERS})
ADD_LIBRARY(transport SHARED ${HEADERS} ${SRC} ${SWIFTEN_SRC} ${PROTOBUF_SRC} ${PROTOBUF_HDRS})

View file

@ -26,11 +26,19 @@ using namespace boost::program_options;
namespace Transport {
bool Config::load(const std::string &configfile, boost::program_options::options_description &opts) {
m_unregistered.clear();
std::ifstream ifs(configfile.c_str());
if (!ifs.is_open())
return false;
m_file = configfile;
bool ret = load(ifs, opts);
ifs.close();
return ret;
}
bool Config::load(std::istream &ifs, boost::program_options::options_description &opts) {
m_unregistered.clear();
opts.add_options()
("service.jid", value<std::string>()->default_value(""), "Transport Jabber ID")
("service.server", value<std::string>()->default_value(""), "Server to connect to")
@ -84,13 +92,16 @@ bool Config::load(const std::string &configfile, boost::program_options::options
store(parsed, m_variables);
notify(m_variables);
m_file = configfile;
onConfigReloaded();
return true;
}
bool Config::load(std::istream &ifs) {
options_description opts("Transport options");
return load(ifs, opts);
}
bool Config::load(const std::string &configfile) {
options_description opts("Transport options");
return load(configfile, opts);

33
src/tests/main.cpp Normal file
View file

@ -0,0 +1,33 @@
#include "main.h"
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>
int main (int argc, char* argv[])
{
// informs test-listener about testresults
CPPUNIT_NS :: TestResult testresult;
// register listener for collecting the test-results
CPPUNIT_NS :: TestResultCollector collectedresults;
testresult.addListener (&collectedresults);
// register listener for per-test progress output
CPPUNIT_NS :: BriefTestProgressListener progress;
testresult.addListener (&progress);
// insert test-suite at test-runner by registry
CPPUNIT_NS :: TestRunner testrunner;
testrunner.addTest (CPPUNIT_NS :: TestFactoryRegistry :: getRegistry ().makeTest ());
testrunner.run (testresult);
// output results in compiler-format
CPPUNIT_NS :: CompilerOutputter compileroutputter (&collectedresults, std::cerr);
compileroutputter.write ();
// return 0 if tests were successful
return collectedresults.wasSuccessful () ? 0 : 1;
}

6
src/tests/main.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef TESTS_MAIN_H
#define TESTS_MAIN_H
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#endif

View file

@ -0,0 +1,33 @@
#include "transport/userregistry.h"
#include "transport/config.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
using namespace Transport;
class UserRegistryTest : public CPPUNIT_NS :: TestFixture {
CPPUNIT_TEST_SUITE (UserRegistryTest);
// CPPUNIT_TEST (storeBuddies);
// CPPUNIT_TEST (storeBuddiesRemove);
CPPUNIT_TEST_SUITE_END ();
public:
void setUp (void) {
std::istringstream ifs;
cfg = new Config();
cfg->load(ifs);
userRegistry = new UserRegistry(cfg);
}
void tearDown (void) {
delete userRegistry;
delete cfg;
}
private:
UserRegistry *userRegistry;
Config *cfg;
};
CPPUNIT_TEST_SUITE_REGISTRATION (UserRegistryTest);