Mercurial > irccd
changeset 313:d2b02e31478d
Tests: add test for plugin-info, #559
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 23 Oct 2016 10:38:42 +0200 |
parents | a0180b5a150c |
children | 427bbbcb50d1 |
files | libirccd/irccd/cmd-plugin-info.cpp libirccd/irccd/cmd-plugin-info.hpp tests/CMakeLists.txt tests/cmd-plugin-info/CMakeLists.txt tests/cmd-plugin-info/main.cpp |
diffstat | 5 files changed, 134 insertions(+), 67 deletions(-) [+] |
line wrap: on
line diff
--- a/libirccd/irccd/cmd-plugin-info.cpp Sat Oct 22 15:56:01 2016 +0200 +++ b/libirccd/irccd/cmd-plugin-info.cpp Sun Oct 23 10:38:42 2016 +0200 @@ -22,6 +22,7 @@ #include "irccd.hpp" #include "plugin.hpp" #include "service-plugin.hpp" +#include "transport.hpp" #include "util.hpp" namespace irccd { @@ -29,32 +30,15 @@ namespace command { PluginInfoCommand::PluginInfoCommand() - : Command("plugin-info", "Plugins", "Get plugin information") + : Command("plugin-info") { } -std::vector<Command::Arg> PluginInfoCommand::args() const +void PluginInfoCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) { - return {{ "plugin", true }}; -} - -std::vector<Command::Property> PluginInfoCommand::properties() const -{ - return {{ "plugin", { nlohmann::json::value_t::string }}}; -} + auto plugin = irccd.plugins().require(util::json::requireIdentifier(args, "plugin")); -nlohmann::json PluginInfoCommand::request(Irccdctl &, const CommandRequest &args) const -{ - return nlohmann::json::object({{ "plugin", args.arg(0) }}); -} - -nlohmann::json PluginInfoCommand::exec(Irccd &irccd, const nlohmann::json &request) const -{ - Command::exec(irccd, request); - - auto plugin = irccd.plugins().require(request.at("plugin").get<std::string>()); - - return nlohmann::json::object({ + client.success("plugin-info", { { "author", plugin->author() }, { "license", plugin->license() }, { "summary", plugin->summary() }, @@ -62,31 +46,6 @@ }); } -void PluginInfoCommand::result(Irccdctl &irccdctl, const nlohmann::json &result) const -{ - Command::result(irccdctl, result); - - auto it = result.find("status"); - - if (!it->is_boolean() || !*it) - return; - - auto get = [&] (auto key) -> std::string { - auto v = result.find(key); - - if (v == result.end() || !v->is_primitive()) - return ""; - - return v->dump(); - }; - - std::cout << std::boolalpha; - std::cout << "Author : " << get("author") << std::endl; - std::cout << "License : " << get("license") << std::endl; - std::cout << "Summary : " << get("summary") << std::endl; - std::cout << "Version : " << get("version") << std::endl; -} - } // !command } // !irccd
--- a/libirccd/irccd/cmd-plugin-info.hpp Sat Oct 22 15:56:01 2016 +0200 +++ b/libirccd/irccd/cmd-plugin-info.hpp Sun Oct 23 10:38:42 2016 +0200 @@ -41,29 +41,9 @@ IRCCD_EXPORT PluginInfoCommand(); /** - * \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; - - /** - * \copydoc Command::result - */ - IRCCD_EXPORT void result(Irccdctl &irccdctl, const nlohmann::json &response) const override; + IRCCD_EXPORT void exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) override; }; } // !command
--- a/tests/CMakeLists.txt Sat Oct 22 15:56:01 2016 +0200 +++ b/tests/CMakeLists.txt Sun Oct 23 10:38:42 2016 +0200 @@ -21,6 +21,7 @@ if (WITH_TESTS) add_subdirectory(cmd-plugin-config) + add_subdirectory(cmd-plugin-info) 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-info/CMakeLists.txt Sun Oct 23 10:38:42 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-info + SOURCES main.cpp + LIBRARIES libirccd libirccdctl +)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-plugin-info/main.cpp Sun Oct 23 10:38:42 2016 +0200 @@ -0,0 +1,104 @@ +/* + * main.cpp -- test plugin-info 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-info.hpp> +#include <command-tester.hpp> +#include <server-tester.hpp> +#include <service-plugin.hpp> +#include <plugin.hpp> + +using namespace irccd; +using namespace irccd::command; + +namespace { + +class PluginInfoCommandTest : public CommandTester { +public: + PluginInfoCommandTest() + : CommandTester(std::make_unique<PluginInfoCommand>()) + { + } +}; + +TEST_F(PluginInfoCommandTest, basic) +{ + try { + auto plugin = std::make_unique<Plugin>("test", ""); + auto response = nlohmann::json(); + + plugin->setAuthor("Francis Beaugrand"); + plugin->setLicense("GPL"); + plugin->setSummary("Completely useless plugin"); + plugin->setVersion("0.0.0.0.0.0.0.0.1-beta5"); + + m_irccd.plugins().add(std::move(plugin)); + m_irccdctl.client().onMessage.connect([&] (auto msg) { + response = std::move(msg); + }); + m_irccdctl.client().request({ + { "command", "plugin-info" }, + { "plugin", "test" }, + }); + + poll([&] () { + return response.is_object(); + }); + + ASSERT_TRUE(response.is_object()); + ASSERT_EQ("Francis Beaugrand", response["author"]); + ASSERT_EQ("GPL", response["license"]); + ASSERT_EQ("Completely useless plugin", response["summary"]); + ASSERT_EQ("0.0.0.0.0.0.0.0.1-beta5", response["version"]); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(PluginInfoCommandTest, notfound) +{ + try { + auto response = nlohmann::json(); + + m_irccdctl.client().onMessage.connect([&] (auto msg) { + response = std::move(msg); + }); + m_irccdctl.client().request({ + { "command", "plugin-info" }, + { "plugin", "test" }, + }); + + poll([&] () { + return response.is_object(); + }); + + ASSERT_TRUE(response.is_object()); + ASSERT_FALSE(response["status"]); + ASSERT_EQ("plugin test 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(); +}