Mercurial > irccd
changeset 300:d9f6eeacd806
Tests: add test for server-mode, #559
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 14 Oct 2016 22:01:15 +0200 |
parents | e60125973197 |
children | b7ad1b65104c |
files | libirccd/irccd/cmd-server-mode.cpp libirccd/irccd/cmd-server-mode.hpp tests/CMakeLists.txt tests/cmd-server-mode/CMakeLists.txt tests/cmd-server-mode/main.cpp |
diffstat | 5 files changed, 109 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/libirccd/irccd/cmd-server-mode.cpp Fri Oct 14 21:20:36 2016 +0200 +++ b/libirccd/irccd/cmd-server-mode.cpp Fri Oct 14 22:01:15 2016 +0200 @@ -18,49 +18,26 @@ #include "cmd-server-mode.hpp" #include "irccd.hpp" +#include "transport.hpp" #include "server.hpp" #include "service-server.hpp" +#include "util.hpp" namespace irccd { namespace command { ServerModeCommand::ServerModeCommand() - : Command("server-mode", "Server", "Change your mode") + : Command("server-mode") { } -std::vector<Command::Arg> ServerModeCommand::args() const -{ - return { - { "server", true }, - { "mode", true } - }; -} - -std::vector<Command::Property> ServerModeCommand::properties() const +void ServerModeCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) { - return { - { "server", { nlohmann::json::value_t::string }}, - { "mode", { nlohmann::json::value_t::string }} - }; -} - -nlohmann::json ServerModeCommand::request(Irccdctl &, const CommandRequest &args) const -{ - return nlohmann::json::object({ - { "server", args.arg(0) }, - { "mode", args.arg(1) } - }); -} - -nlohmann::json ServerModeCommand::exec(Irccd &irccd, const nlohmann::json &request) const -{ - Command::exec(irccd, request); - - irccd.servers().require(request["server"])->mode(request["mode"]); - - return nlohmann::json::object(); + irccd.servers().require(util::json::requireIdentifier(args, "server"))->mode( + util::json::requireString(args, "mode") + ); + client.success("server-mode"); } } // !command
--- a/libirccd/irccd/cmd-server-mode.hpp Fri Oct 14 21:20:36 2016 +0200 +++ b/libirccd/irccd/cmd-server-mode.hpp Fri Oct 14 22:01:15 2016 +0200 @@ -25,6 +25,7 @@ */ #include "command.hpp" +#include "sysconfig.hpp" namespace irccd { @@ -33,32 +34,17 @@ /** * \brief Implementation of server-mode transport command. */ -class ServerModeCommand : public Command { +class IRCCD_EXPORT ServerModeCommand : public Command { public: /** * Constructor. */ - IRCCD_EXPORT ServerModeCommand(); - - /** - * \copydoc Command::args - */ - IRCCD_EXPORT std::vector<Arg> args() const override; - - /** - * \copydoc Command::properties - */ - IRCCD_EXPORT std::vector<Property> properties() const override; - - /** - * \copydoc Command::request - */ - IRCCD_EXPORT nlohmann::json request(Irccdctl &irccdctl, const CommandRequest &args) const override; + ServerModeCommand(); /** * \copydoc Command::exec */ - IRCCD_EXPORT nlohmann::json exec(Irccd &irccd, const nlohmann::json &request) const override; + void exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) override; }; } // !command
--- a/tests/CMakeLists.txt Fri Oct 14 21:20:36 2016 +0200 +++ b/tests/CMakeLists.txt Fri Oct 14 22:01:15 2016 +0200 @@ -25,6 +25,7 @@ add_subdirectory(cmd-server-disconnect) add_subdirectory(cmd-server-me) add_subdirectory(cmd-server-message) + add_subdirectory(cmd-server-mode) # # Misc # add_subdirectory(command)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-server-mode/CMakeLists.txt Fri Oct 14 22:01:15 2016 +0200 @@ -0,0 +1,24 @@ +# +# CMakeLists.txt -- CMake build system for irccd +# +# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> +# +# 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. +# + +irccd_define_test( + NAME cmd-server-mode + SOURCES main.cpp + LIBRARIES libirccd libirccdctl +) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-server-mode/main.cpp Fri Oct 14 22:01:15 2016 +0200 @@ -0,0 +1,72 @@ +/* + * main.cpp -- test server-mode remote command + * + * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> + * + * 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 <cmd-server-mode.hpp> +#include <command-tester.hpp> +#include <server-tester.hpp> + +using namespace irccd; +using namespace irccd::command; + +namespace { + +std::string mode; + +} // !namespace + +class ServerModeTest : public ServerTester { +public: + void mode(std::string mode) override + { + ::mode = mode; + } +}; + +class ServerModeCommandTest : public CommandTester { +public: + ServerModeCommandTest() + : CommandTester(std::make_unique<ServerModeCommand>(), + std::make_unique<ServerModeTest>()) + { + m_irccdctl.client().request({ + { "command", "server-mode" }, + { "server", "test" }, + { "mode", "+t" } + }); + } +}; + +TEST_F(ServerModeCommandTest, basic) +{ + try { + poll([&] () { + return !mode.empty(); + }); + + ASSERT_EQ("+t", mode); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}