Mercurial > irccd
changeset 304:04a8c96aeb33
Tests: add test for server-topic, #559
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 15 Oct 2016 23:01:26 +0200 |
parents | ca5bc0624fb5 |
children | e1e596a75355 |
files | libirccd/irccd/cmd-server-topic.cpp libirccd/irccd/cmd-server-topic.hpp tests/CMakeLists.txt tests/cmd-server-topic/CMakeLists.txt tests/cmd-server-topic/main.cpp |
diffstat | 5 files changed, 114 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/libirccd/irccd/cmd-server-topic.cpp Sat Oct 15 17:56:35 2016 +0200 +++ b/libirccd/irccd/cmd-server-topic.cpp Sat Oct 15 23:01:26 2016 +0200 @@ -20,50 +20,25 @@ #include "irccd.hpp" #include "server.hpp" #include "service-server.hpp" +#include "transport.hpp" +#include "util.hpp" namespace irccd { namespace command { ServerTopicCommand::ServerTopicCommand() - : Command("server-topic", "Server", "Change a channel topic") + : Command("server-topic") { } -std::vector<Command::Arg> ServerTopicCommand::args() const -{ - return { - { "server", true }, - { "channel", true }, - { "topic", true } - }; -} - -std::vector<Command::Property> ServerTopicCommand::properties() const +void ServerTopicCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) { - return { - { "server", { nlohmann::json::value_t::string }}, - { "channel", { nlohmann::json::value_t::string }}, - { "topic", { nlohmann::json::value_t::string }} - }; -} - -nlohmann::json ServerTopicCommand::request(Irccdctl &, const CommandRequest &args) const -{ - return nlohmann::json::object({ - { "server", args.arg(0) }, - { "channel", args.arg(1) }, - { "topic", args.arg(2) } - }); -} - -nlohmann::json ServerTopicCommand::exec(Irccd &irccd, const nlohmann::json &request) const -{ - Command::exec(irccd, request); - - irccd.servers().require(request["server"])->topic(request["channel"], request["topic"]); - - return nlohmann::json::object(); + irccd.servers().require(util::json::requireIdentifier(args, "server"))->topic( + util::json::requireString(args, "channel"), + util::json::requireString(args, "topic") + ); + client.success("server-topic"); } } // !command
--- a/libirccd/irccd/cmd-server-topic.hpp Sat Oct 15 17:56:35 2016 +0200 +++ b/libirccd/irccd/cmd-server-topic.hpp Sat Oct 15 23:01:26 2016 +0200 @@ -25,6 +25,7 @@ */ #include "command.hpp" +#include "sysconfig.hpp" namespace irccd { @@ -33,32 +34,17 @@ /** * \brief Implementation of server-topic transport command. */ -class ServerTopicCommand : public Command { +class IRCCD_EXPORT ServerTopicCommand : public Command { public: /** * Constructor. */ - IRCCD_EXPORT ServerTopicCommand(); - - /** - * \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; + ServerTopicCommand(); /** * \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 Sat Oct 15 17:56:35 2016 +0200 +++ b/tests/CMakeLists.txt Sat Oct 15 23:01:26 2016 +0200 @@ -29,6 +29,7 @@ add_subdirectory(cmd-server-nick) add_subdirectory(cmd-server-notice) add_subdirectory(cmd-server-part) + add_subdirectory(cmd-server-topic) # # Misc # add_subdirectory(command)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-server-topic/CMakeLists.txt Sat Oct 15 23:01:26 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-topic + SOURCES main.cpp + LIBRARIES libirccd libirccdctl +) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-server-topic/main.cpp Sat Oct 15 23:01:26 2016 +0200 @@ -0,0 +1,76 @@ +/* + * main.cpp -- test server-topic 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-topic.hpp> +#include <command-tester.hpp> +#include <server-tester.hpp> + +using namespace irccd; +using namespace irccd::command; + +namespace { + +std::string channel; +std::string topic; + +} // !namespace + +class ServerTopicTest : public ServerTester { +public: + void topic(std::string channel, std::string topic) override + { + ::channel = channel; + ::topic = topic; + } +}; + +class ServerTopicCommandTest : public CommandTester { +public: + ServerTopicCommandTest() + : CommandTester(std::make_unique<ServerTopicCommand>(), + std::make_unique<ServerTopicTest>()) + { + m_irccdctl.client().request({ + { "command", "server-topic" }, + { "server", "test" }, + { "channel", "#staff" }, + { "topic", "new version" } + }); + } +}; + +TEST_F(ServerTopicCommandTest, basic) +{ + try { + poll([&] () { + return !channel.empty() && !topic.empty(); + }); + + ASSERT_EQ("#staff", channel); + ASSERT_EQ("new version", topic); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}