From a00d7859f663053b81362f2e45a849c643f5c390 2011-08-21 13:41:23 From: HanzZ Date: 2011-08-21 13:41:23 Subject: [PATCH] Dummy unit test --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 75e9d506487954296fb3a236b82d449c3f15685a..ef838be38a920c069a48abc03b5131f3f8d8caac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/transport/config.h b/include/transport/config.h index 396fbbdecebb6d06302055d4d311025568cf73ba..c0ad64e576acfc4d0219a59454b855dbd838164c 100644 --- a/include/transport/config.h +++ b/include/transport/config.h @@ -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. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 45b2341811b88728926b7c1234ceb7313b90a7b5..9db9993e932ad47eb08f612d20d03f563a1aa4c3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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}) diff --git a/src/config.cpp b/src/config.cpp index 46ef14c2faabe2ede6fcc9cc0aad5d15d11284a1..aa370efbbe45a7ce061f40ccb40328bcf64781bc 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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()->default_value(""), "Transport Jabber ID") ("service.server", value()->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); diff --git a/src/tests/main.cpp b/src/tests/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd1c5f27e67e9f6ee7971b2562b9243506906712 --- /dev/null +++ b/src/tests/main.cpp @@ -0,0 +1,33 @@ +#include "main.h" +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/src/tests/main.h b/src/tests/main.h new file mode 100644 index 0000000000000000000000000000000000000000..27f1edcf84b067a54000f8835af78a4e7440804a --- /dev/null +++ b/src/tests/main.h @@ -0,0 +1,6 @@ +#ifndef TESTS_MAIN_H +#define TESTS_MAIN_H +#include +#include + +#endif diff --git a/src/tests/userregistry.cpp b/src/tests/userregistry.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c932c1bf02d18a8972a76e349731ef0fa9ae1a37 --- /dev/null +++ b/src/tests/userregistry.cpp @@ -0,0 +1,33 @@ +#include "transport/userregistry.h" +#include "transport/config.h" +#include +#include + +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);