changeset 303:ca5bc0624fb5

Tests: add test for server-part, #559
author David Demelier <markand@malikania.fr>
date Sat, 15 Oct 2016 17:56:35 +0200
parents 47d40e343ffe
children 04a8c96aeb33
files libirccd/irccd/cmd-server-part.cpp libirccd/irccd/cmd-server-part.hpp tests/CMakeLists.txt tests/cmd-server-part/CMakeLists.txt tests/cmd-server-part/main.cpp
diffstat 5 files changed, 136 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd/irccd/cmd-server-part.cpp	Sat Oct 15 00:07:47 2016 +0200
+++ b/libirccd/irccd/cmd-server-part.cpp	Sat Oct 15 17:56:35 2016 +0200
@@ -20,56 +20,25 @@
 #include "irccd.hpp"
 #include "server.hpp"
 #include "service-server.hpp"
+#include "transport.hpp"
+#include "util.hpp"
 
 namespace irccd {
 
 namespace command {
 
 ServerPartCommand::ServerPartCommand()
-    : Command("server-part", "Server", "Leave a channel")
+    : Command("server-part")
 {
 }
 
-std::vector<Command::Arg> ServerPartCommand::args() const
-{
-    return {
-        { "server",     true    },
-        { "channel",    true    },
-        { "reason",     false    }
-    };
-}
-
-std::vector<Command::Property> ServerPartCommand::properties() const
-{
-    return {
-        { "server",     { nlohmann::json::value_t::string }},
-        { "channel",    { nlohmann::json::value_t::string }}
-    };
-}
-
-nlohmann::json ServerPartCommand::request(Irccdctl &, const CommandRequest &args) const
+void ServerPartCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args)
 {
-    auto req = nlohmann::json::object({
-        { "server",     args.arg(0) },
-        { "channel",    args.arg(1) }
-    });
-
-    if (args.length() == 3)
-        req.push_back({"reason", args.arg(2)});
-
-    return req;
-}
-
-nlohmann::json ServerPartCommand::exec(Irccd &irccd, const nlohmann::json &request) const
-{
-    Command::exec(irccd, request);
-
-    irccd.servers().require(request["server"])->part(
-        request["channel"],
-        request.count("reason") > 0 ? request["reason"] : ""
+    irccd.servers().require(util::json::requireIdentifier(args, "server"))->part(
+        util::json::requireString(args, "channel"),
+        util::json::getString(args, "reason")
     );
-
-    return nlohmann::json::object();
+    client.success("server-part");
 }
 
 } // !command
--- a/libirccd/irccd/cmd-server-part.hpp	Sat Oct 15 00:07:47 2016 +0200
+++ b/libirccd/irccd/cmd-server-part.hpp	Sat Oct 15 17:56:35 2016 +0200
@@ -25,6 +25,7 @@
  */
 
 #include "command.hpp"
+#include "sysconfig.hpp"
 
 namespace irccd {
 
@@ -34,32 +35,17 @@
  * \class ServerPart
  * \brief Implementation of server-part transport command.
  */
-class ServerPartCommand : public Command {
+class IRCCD_EXPORT ServerPartCommand : public Command {
 public:
     /**
      * Constructor.
      */
-    IRCCD_EXPORT ServerPartCommand();
-
-    /**
-     * \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;
+    ServerPartCommand();
 
     /**
      * \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 00:07:47 2016 +0200
+++ b/tests/CMakeLists.txt	Sat Oct 15 17:56:35 2016 +0200
@@ -28,6 +28,7 @@
     add_subdirectory(cmd-server-mode)
     add_subdirectory(cmd-server-nick)
     add_subdirectory(cmd-server-notice)
+    add_subdirectory(cmd-server-part)
 
 #    # Misc
 #    add_subdirectory(command)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-part/CMakeLists.txt	Sat Oct 15 17:56:35 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-part
+    SOURCES main.cpp
+    LIBRARIES libirccd libirccdctl
+)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-part/main.cpp	Sat Oct 15 17:56:35 2016 +0200
@@ -0,0 +1,99 @@
+/*
+ * main.cpp -- test server-part 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-part.hpp>
+#include <command-tester.hpp>
+#include <server-tester.hpp>
+
+using namespace irccd;
+using namespace irccd::command;
+
+namespace {
+
+std::string channel;
+std::string reason;
+
+} // !namespace
+
+class ServerPartTest : public ServerTester {
+public:
+    void part(std::string channel, std::string reason) override
+    {
+        ::channel = channel;
+        ::reason = reason;
+    }
+};
+
+class ServerPartCommandTest : public CommandTester {
+public:
+    ServerPartCommandTest()
+        : CommandTester(std::make_unique<ServerPartCommand>(),
+                        std::make_unique<ServerPartTest>())
+    {
+        channel.clear();
+        reason.clear();
+    }
+};
+
+TEST_F(ServerPartCommandTest, basic)
+{
+    try {
+        m_irccdctl.client().request({
+            { "command",    "server-part"   },
+            { "server",     "test"          },
+            { "channel",    "#staff"        },
+            { "reason",     "too noisy"     }
+        });
+
+        poll([&] () {
+            return !channel.empty();
+        });
+
+        ASSERT_EQ("#staff", channel);
+        ASSERT_EQ("too noisy", reason);
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+TEST_F(ServerPartCommandTest, noreason)
+{
+    try {
+        m_irccdctl.client().request({
+            { "command",    "server-part"   },
+            { "server",     "test"          },
+            { "channel",    "#staff"        }
+        });
+
+        poll([&] () {
+            return !channel.empty();
+        });
+
+        ASSERT_EQ("#staff", channel);
+        ASSERT_TRUE(reason.empty());
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+int main(int argc, char **argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+
+    return RUN_ALL_TESTS();
+}