Mercurial > irccd
changeset 315:8cf706edd262
Tests: add test for plugin-reload, #559
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 23 Oct 2016 11:06:57 +0200 |
parents | 427bbbcb50d1 |
children | ee2593e7e670 |
files | libirccd/irccd/cmd-plugin-reload.cpp libirccd/irccd/cmd-plugin-reload.hpp tests/CMakeLists.txt tests/cmd-plugin-reload/CMakeLists.txt tests/cmd-plugin-reload/main.cpp |
diffstat | 5 files changed, 135 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- a/libirccd/irccd/cmd-plugin-reload.cpp Sun Oct 23 10:57:06 2016 +0200 +++ b/libirccd/irccd/cmd-plugin-reload.cpp Sun Oct 23 11:06:57 2016 +0200 @@ -20,6 +20,7 @@ #include "irccd.hpp" #include "plugin.hpp" #include "service-plugin.hpp" +#include "transport.hpp" #include "util.hpp" namespace irccd { @@ -27,32 +28,14 @@ namespace command { PluginReloadCommand::PluginReloadCommand() - : Command("plugin-reload", "Plugins", "Reload a plugin") + : Command("plugin-reload") { } -std::vector<Command::Arg> PluginReloadCommand::args() const -{ - return {{ "plugin", true }}; -} - -std::vector<Command::Property> PluginReloadCommand::properties() const +void PluginReloadCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) { - return {{ "plugin", { nlohmann::json::value_t::string }}}; -} - -nlohmann::json PluginReloadCommand::request(Irccdctl &, const CommandRequest &args) const -{ - return nlohmann::json::object({{ "plugin", args.arg(0) }}); -} - -nlohmann::json PluginReloadCommand::exec(Irccd &irccd, const nlohmann::json &request) const -{ - Command::exec(irccd, request); - - irccd.plugins().require(request["plugin"])->onReload(irccd); - - return nlohmann::json::object(); + irccd.plugins().require(util::json::requireIdentifier(args, "plugin"))->onReload(irccd); + client.success("plugin-reload"); } } // !command
--- a/libirccd/irccd/cmd-plugin-reload.hpp Sun Oct 23 10:57:06 2016 +0200 +++ b/libirccd/irccd/cmd-plugin-reload.hpp Sun Oct 23 11:06:57 2016 +0200 @@ -41,24 +41,9 @@ IRCCD_EXPORT PluginReloadCommand(); /** - * \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; - - /** * \copydoc Command::exec */ - IRCCD_EXPORT nlohmann::json exec(Irccd &irccd, const nlohmann::json &request) const override; + IRCCD_EXPORT void exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) override; }; } // !command
--- a/tests/CMakeLists.txt Sun Oct 23 10:57:06 2016 +0200 +++ b/tests/CMakeLists.txt Sun Oct 23 11:06:57 2016 +0200 @@ -23,6 +23,7 @@ add_subdirectory(cmd-plugin-config) add_subdirectory(cmd-plugin-info) add_subdirectory(cmd-plugin-load) + add_subdirectory(cmd-plugin-reload) add_subdirectory(cmd-server-cmode) add_subdirectory(cmd-server-cnotice) add_subdirectory(cmd-server-connect)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-plugin-reload/CMakeLists.txt Sun Oct 23 11:06:57 2016 +0200 @@ -0,0 +1,23 @@ +# +# 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-plugin-reload + SOURCES main.cpp + LIBRARIES libirccd libirccdctl +)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-plugin-reload/main.cpp Sun Oct 23 11:06:57 2016 +0200 @@ -0,0 +1,105 @@ +/* + * main.cpp -- test plugin-reload 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-plugin-reload.hpp> +#include <command-tester.hpp> +#include <server-tester.hpp> +#include <service-plugin.hpp> +#include <plugin.hpp> + +using namespace irccd; +using namespace irccd::command; + +namespace { + +bool called = false; + +class CustomPlugin : public Plugin { +public: + CustomPlugin() + : Plugin("test", "") + { + } + + void onReload(Irccd &) override + { + called = true; + } +}; + +class PluginReloadCommandTest : public CommandTester { +public: + PluginReloadCommandTest() + : CommandTester(std::make_unique<PluginReloadCommand>()) + { + called = false; + } +}; + +TEST_F(PluginReloadCommandTest, basic) +{ + try { + m_irccd.plugins().add(std::make_unique<CustomPlugin>()); + m_irccdctl.client().request({ + { "command", "plugin-reload" }, + { "plugin", "test" } + }); + + poll([&] () { + return called; + }); + + ASSERT_TRUE(called); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(PluginReloadCommandTest, notfound) +{ + try { + auto response = nlohmann::json(); + + m_irccdctl.client().onMessage.connect([&] (auto msg) { + response = msg; + }); + m_irccdctl.client().request({ + { "command", "plugin-reload" }, + { "plugin", "no" } + }); + + poll([&] () { + return response.is_object(); + }); + + ASSERT_TRUE(response.is_object()); + ASSERT_FALSE(response["status"]); + ASSERT_EQ("plugin no not found", response["error"]); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +} // !namespace + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}