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