# HG changeset patch # User David Demelier # Date 1476007511 -7200 # Node ID 6bf457b71e0c740f1b247f2601b144698cece711 # Parent 55662f35a16bb28bc0edeffda436aad4d2ce1c91 Irccd: add new libirccd-test for unit tests diff -r 55662f35a16b -r 6bf457b71e0c CMakeLists.txt --- a/CMakeLists.txt Thu Oct 06 12:36:41 2016 +0200 +++ b/CMakeLists.txt Sun Oct 09 12:05:11 2016 +0200 @@ -100,8 +100,11 @@ endif () # Tests. -include(CTest) -# add_subdirectory(tests) +if (WITH_TESTS) + include(CTest) + add_subdirectory(libirccd-test) + add_subdirectory(tests) +endif () message("Compiling with the following flags:") message(" General flags: ${CMAKE_CXX_FLAGS}") diff -r 55662f35a16b -r 6bf457b71e0c cmake/function/IrccdDefineTest.cmake --- a/cmake/function/IrccdDefineTest.cmake Thu Oct 06 12:36:41 2016 +0200 +++ b/cmake/function/IrccdDefineTest.cmake Sun Oct 09 12:05:11 2016 +0200 @@ -48,7 +48,7 @@ foreach (r ${TEST_RESOURCES}) file(RELATIVE_PATH output ${CMAKE_CURRENT_SOURCE_DIR} ${r}) - + add_custom_command( OUTPUT ${CMAKE_BINARY_DIR}/tests/${output} COMMAND ${CMAKE_COMMAND} -E copy ${r} ${CMAKE_BINARY_DIR}/tests/${output} @@ -59,7 +59,7 @@ endforeach () # Always link to googletest - list(APPEND TEST_LIBRARIES extern-gtest) + list(APPEND TEST_LIBRARIES libirccd-test) # Executable add_executable(test-${TEST_NAME} ${TEST_SOURCES} ${TEST_RESOURCES} ${RESOURCES}) diff -r 55662f35a16b -r 6bf457b71e0c libirccd-test/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libirccd-test/CMakeLists.txt Sun Oct 09 12:05:11 2016 +0200 @@ -0,0 +1,13 @@ +project(libirccd-test) + +irccd_define_library( + TARGET libirccd-test + SOURCES + ${libirccd-test_SOURCE_DIR}/irccd/command-tester.cpp + ${libirccd-test_SOURCE_DIR}/irccd/command-tester.hpp + ${libirccd-test_SOURCE_DIR}/irccd/server-tester.cpp + ${libirccd-test_SOURCE_DIR}/irccd/server-tester.hpp + LIBRARIES libirccd libirccdctl extern-gtest + PUBLIC_INCLUDES + $ +) diff -r 55662f35a16b -r 6bf457b71e0c libirccd-test/irccd/command-tester.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libirccd-test/irccd/command-tester.cpp Sun Oct 09 12:05:11 2016 +0200 @@ -0,0 +1,46 @@ +/* + * command-tester.cpp -- test fixture helper for remote commands + * + * Copyright (c) 2013-2016 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "command-tester.hpp" +#include "client.hpp" +#include "logger.hpp" +#include "service-command.hpp" +#include "service-server.hpp" +#include "service-transport.hpp" +#include "transport.hpp" + +namespace irccd { + +CommandTester::CommandTester(std::unique_ptr cmd, + std::unique_ptr server) + : m_irccdctl(std::make_unique()) +{ + // Be silent. + log::setLogger(std::make_unique()); + log::setVerbose(false); + + m_irccd.transports().add(std::make_unique("*", 5000)); + m_irccdctl.client().connect(net::ipv4::pton("127.0.0.1", 5000)); + + if (cmd) + m_irccd.commands().add(std::move(cmd)); + if (server) + m_irccd.servers().add(std::move(server)); +} + +} // !irccd diff -r 55662f35a16b -r 6bf457b71e0c libirccd-test/irccd/command-tester.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libirccd-test/irccd/command-tester.hpp Sun Oct 09 12:05:11 2016 +0200 @@ -0,0 +1,57 @@ +/* + * command-tester.hpp -- test fixture helper for remote commands + * + * Copyright (c) 2013-2016 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef IRCCD_COMMAND_TESTER_HPP +#define IRCCD_COMMAND_TESTER_HPP + +#include + +#include "elapsed-timer.hpp" +#include "irccd.hpp" +#include "irccdctl.hpp" +#include "util.hpp" + +namespace irccd { + +class Command; +class Server; + +class CommandTester : public testing::Test { +protected: + Irccd m_irccd; + Irccdctl m_irccdctl; + + +public: + CommandTester(std::unique_ptr cmd = nullptr, + std::unique_ptr server = nullptr); + + + template + void poll(Predicate &&predicate) + { + ElapsedTimer timer; + + while (!predicate() && timer.elapsed() < 30000) + util::poller::poll(250, m_irccd, m_irccdctl); + } +}; + +} // !irccd + +#endif // !IRCCD_COMMAND_TESTER_HPP diff -r 55662f35a16b -r 6bf457b71e0c libirccd-test/irccd/server-tester.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libirccd-test/irccd/server-tester.cpp Sun Oct 09 12:05:11 2016 +0200 @@ -0,0 +1,36 @@ +/* + * server-tester.cpp -- server that does nothing + * + * Copyright (c) 2013-2016 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "server-tester.hpp" + +namespace irccd { + +ServerTester::ServerTester() + : Server("test") +{ +} + +void ServerTester::prepare(fd_set &, fd_set &, net::Handle &) noexcept +{ +} + +void ServerTester::sync(fd_set &, fd_set &) +{ +} + +} // !irccd diff -r 55662f35a16b -r 6bf457b71e0c libirccd-test/irccd/server-tester.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libirccd-test/irccd/server-tester.hpp Sun Oct 09 12:05:11 2016 +0200 @@ -0,0 +1,55 @@ +/* + * server-tester.hpp -- server that does nothing + * + * Copyright (c) 2013-2016 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef IRCCD_SERVER_TESTER_HPP +#define IRCCD_SERVER_TESTER_HPP + +/** + * \file server-tester.hpp + * \brief Server that does nothing. + */ + +#include "server.hpp" +#include "sysconfig.hpp" + +namespace irccd { + +/** + * \brief Useless server for testing purpose. + */ +class IRCCD_EXPORT ServerTester : public Server { +public: + /** + * Create a server with named 'test' + */ + ServerTester(); + + /** + * Overload that is a no-op. + */ + void prepare(fd_set &, fd_set &, net::Handle &) noexcept override; + + /** + * Overload that is a no-op. + */ + void sync(fd_set &, fd_set &) override; +}; + +} // !irccd + +#endif // !IRCCD_SERVER_TESTER_HPP