changeset 297:a76ccf092570

Tests: add test for server-cmode, #559
author David Demelier <markand@malikania.fr>
date Wed, 19 Oct 2016 13:43:12 +0200
parents c970b293d8cf
children a67bc47e6d02
files libirccd/irccd/cmd-server-cmode.cpp libirccd/irccd/cmd-server-cmode.hpp tests/CMakeLists.txt tests/cmd-server-cmode/CMakeLists.txt tests/cmd-server-cmode/main.cpp
diffstat 5 files changed, 110 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd/irccd/cmd-server-cmode.cpp	Sun Oct 09 12:09:17 2016 +0200
+++ b/libirccd/irccd/cmd-server-cmode.cpp	Wed Oct 19 13:43:12 2016 +0200
@@ -20,44 +20,25 @@
 #include "irccd.hpp"
 #include "server.hpp"
 #include "service-server.hpp"
+#include "transport.hpp"
+#include "util.hpp"
 
 namespace irccd {
 
 namespace command {
 
 ServerChannelModeCommand::ServerChannelModeCommand()
-    : Command("server-cmode", "Server", "Change a channel mode")
+    : Command("server-cmode")
 {
 }
 
-std::vector<Command::Arg> ServerChannelModeCommand::args() const
-{
-    return {
-        { "server",     true },
-        { "channel",    true },
-        { "mode",       true }
-    };
-}
-
-std::vector<Command::Property> ServerChannelModeCommand::properties() const
+void ServerChannelModeCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args)
 {
-    return {
-        { "server",     { nlohmann::json::value_t::string }},
-        { "channel",    { nlohmann::json::value_t::string }},
-        { "mode",       { nlohmann::json::value_t::string }}
-    };
-}
-
-nlohmann::json ServerChannelModeCommand::exec(Irccd &irccd, const nlohmann::json &request) const
-{
-    Command::exec(irccd, request);
-
-    irccd.servers().require(request["server"].get<std::string>())->cmode(
-        request["channel"].get<std::string>(),
-        request["mode"].get<std::string>()
+    irccd.servers().require(util::json::requireIdentifier(args, "server"))->cmode(
+        util::json::requireString(args, "channel"),
+        util::json::requireString(args, "mode")
     );
-
-    return nlohmann::json::object();
+    client.success("server-cmode");
 }
 
 } // !command
--- a/libirccd/irccd/cmd-server-cmode.hpp	Sun Oct 09 12:09:17 2016 +0200
+++ b/libirccd/irccd/cmd-server-cmode.hpp	Wed Oct 19 13:43:12 2016 +0200
@@ -41,19 +41,9 @@
     IRCCD_EXPORT ServerChannelModeCommand();
 
     /**
-     * \copydoc Command::args
-     */
-    IRCCD_EXPORT std::vector<Arg> args() const override;
-
-    /**
-     * \copydoc Command::properties
-     */
-    IRCCD_EXPORT std::vector<Property> properties() 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 09 12:09:17 2016 +0200
+++ b/tests/CMakeLists.txt	Wed Oct 19 13:43:12 2016 +0200
@@ -20,6 +20,7 @@
 project(tests)
 
 if (WITH_TESTS)
+    add_subdirectory(cmd-server-cmode)
     add_subdirectory(cmd-server-message)
 
 #    # Misc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-cmode/CMakeLists.txt	Wed Oct 19 13:43:12 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-cmode
+    SOURCES main.cpp
+    LIBRARIES libirccd libirccdctl
+)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-cmode/main.cpp	Wed Oct 19 13:43:12 2016 +0200
@@ -0,0 +1,76 @@
+/*
+ * main.cpp -- test server-cmode 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-cmode.hpp>
+#include <command-tester.hpp>
+#include <server-tester.hpp>
+
+using namespace irccd;
+using namespace irccd::command;
+
+namespace {
+
+std::string channel;
+std::string mode;
+
+} // !namespace
+
+class ServerChannelModeTest : public ServerTester {
+public:
+    void cmode(std::string channel, std::string mode)
+    {
+        ::channel = channel;
+        ::mode = mode;
+    }
+};
+
+class ServerChannelModeCommandTest : public CommandTester {
+public:
+    ServerChannelModeCommandTest()
+        : CommandTester(std::make_unique<ServerChannelModeCommand>(),
+                        std::make_unique<ServerChannelModeTest>())
+    {
+        m_irccdctl.client().request({
+            { "command",    "server-cmode"      },
+            { "server",     "test"              },
+            { "channel",    "#staff"            },
+            { "mode",       "+c"                }
+        });
+    }
+};
+
+TEST_F(ServerChannelModeCommandTest, basic)
+{
+    try {
+        poll([&] () {
+            return !channel.empty() && !mode.empty();
+        });
+
+        ASSERT_EQ("#staff", channel);
+        ASSERT_EQ("+c", mode);
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+int main(int argc, char **argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+
+    return RUN_ALL_TESTS();
+}