changeset 490:711575f6f686

Tests: add journal_server, closes #679
author David Demelier <markand@malikania.fr>
date Fri, 18 Aug 2017 10:39:40 +0200
parents 349fe29d86d5
children 7f2ebbb7a45d
files libirccd-test/CMakeLists.txt libirccd-test/irccd/journal_server.cpp libirccd-test/irccd/journal_server.hpp
diffstat 3 files changed, 308 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd-test/CMakeLists.txt	Sun Aug 20 08:16:39 2017 +0200
+++ b/libirccd-test/CMakeLists.txt	Fri Aug 18 10:39:40 2017 +0200
@@ -23,6 +23,8 @@
     SOURCES
         ${libirccd-test_SOURCE_DIR}/irccd/command-tester.cpp
         ${libirccd-test_SOURCE_DIR}/irccd/command-tester.hpp
+        ${libirccd-test_SOURCE_DIR}/irccd/journal_server.cpp
+        ${libirccd-test_SOURCE_DIR}/irccd/journal_server.hpp
         $<$<BOOL:${WITH_JS}>:${libirccd-test_SOURCE_DIR}/irccd/plugin-tester.cpp>
         $<$<BOOL:${WITH_JS}>:${libirccd-test_SOURCE_DIR}/irccd/plugin-tester.hpp>
         $<$<BOOL:${WITH_JS}>:${libirccd-test_SOURCE_DIR}/irccd/plugin_test.cpp>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libirccd-test/irccd/journal_server.cpp	Fri Aug 18 10:39:40 2017 +0200
@@ -0,0 +1,146 @@
+/*
+ * journal_server.cpp -- journaled server that logs every command
+ *
+ * Copyright (c) 2013-2017 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 "journal_server.hpp"
+
+namespace irccd {
+
+void journal_server::cmode(std::string channel, std::string mode)
+{
+    cqueue_.push_back({
+        { "command",    "cmode"     },
+        { "channel",    channel     },
+        { "mode",       mode        }
+    });
+}
+
+void journal_server::cnotice(std::string channel, std::string message)
+{
+    cqueue_.push_back({
+        { "command",    "cnotice"   },
+        { "channel",    channel     },
+        { "message",    message     }
+    });
+}
+
+void journal_server::invite(std::string target, std::string channel)
+{
+    cqueue_.push_back({
+        { "command",    "invite"    },
+        { "target",     target      },
+        { "channel",    channel     }
+    });
+}
+
+void journal_server::join(std::string channel, std::string password)
+{
+    cqueue_.push_back({
+        { "command",    "join"      },
+        { "channel",    channel     },
+        { "password",   password    }
+    });
+}
+
+void journal_server::kick(std::string target, std::string channel, std::string reason)
+{
+    cqueue_.push_back({
+        { "command",    "kick"      },
+        { "target",     target      },
+        { "channel",    channel     },
+        { "reason",     reason      }
+    });
+}
+
+void journal_server::me(std::string target, std::string message)
+{
+    cqueue_.push_back({
+        { "command",    "me"        },
+        { "target",     target      },
+        { "message",    message     }
+    });
+}
+
+void journal_server::message(std::string target, std::string message)
+{
+    cqueue_.push_back({
+        { "command",    "message"   },
+        { "target",     target      },
+        { "message",    message     }
+    });
+}
+
+void journal_server::mode(std::string mode)
+{
+    cqueue_.push_back({
+        { "command",    "mode"      },
+        { "mode",       mode        }
+    });
+}
+
+void journal_server::names(std::string channel)
+{
+    cqueue_.push_back({
+        { "command",    "names"     },
+        { "channel",    channel     }
+    });
+}
+
+void journal_server::notice(std::string target, std::string message)
+{
+    cqueue_.push_back({
+        { "command",    "notice"    },
+        { "target",     target      },
+        { "message",    message     }
+    });
+}
+
+void journal_server::part(std::string channel, std::string reason)
+{
+    cqueue_.push_back({
+        { "command",    "part"      },
+        { "channel",    channel     },
+        { "reason",     reason      }
+    });
+}
+
+void journal_server::send(std::string raw)
+{
+    cqueue_.push_back({
+        { "command",    "send"      },
+        { "raw",        raw         }
+    });
+}
+
+void journal_server::topic(std::string channel, std::string topic)
+{
+    cqueue_.push_back({
+        { "command",    "topic"     },
+        { "channel",    channel     },
+        { "topic",      topic       }
+    });
+}
+
+void journal_server::whois(std::string target)
+{
+    cqueue_.push_back({
+        { "command",    "whois"     },
+        { "target",     target      }
+    });
+}
+
+} // !irccd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libirccd-test/irccd/journal_server.hpp	Fri Aug 18 10:39:40 2017 +0200
@@ -0,0 +1,160 @@
+/*
+ * journal_server.hpp -- journaled server that logs every command
+ *
+ * Copyright (c) 2013-2017 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.
+ */
+
+#ifndef IRCCD_JOURNAL_SERVER_HPP
+#define IRCCD_JOURNAL_SERVER_HPP
+
+#include <deque>
+
+#include <json.hpp>
+
+#include <server.hpp>
+
+/**
+ * \file journal_server.hpp
+ * \brief Journaled server that logs every command.
+ */
+
+namespace irccd {
+
+/**
+ * \brief Journaled server that logs every command.
+ *
+ * This class is used for unit testing, it logs every user command such as
+ * message, invite.
+ *
+ * Each command store exactly the function name, parameter names in a json
+ * object in a FIFO queue, don't forget to clear that queue when you don't need
+ * it anymore.
+ *
+ * Example with message:
+ *
+ * ````json
+ * {
+ *     "command": "message",
+ *     "target": "<argument value>",
+ *     "message": "<argument value>"
+ * }
+ * ````
+ *
+ * \see cqueue()
+ */
+class journal_server : public server {
+private:
+    std::deque<nlohmann::json> cqueue_;
+
+public:
+    /**
+     * Inherited constructors.
+     */
+    using server::server;
+
+    /**
+     * Access the command queue.
+     *
+     * \return the queue
+     */
+    inline const std::deque<nlohmann::json>& cqueue() const noexcept
+    {
+        return cqueue_;
+    }
+
+    /**
+     * Overloaded function.
+     *
+     * \return the queue
+     */
+    inline std::deque<nlohmann::json>& cqueue() noexcept
+    {
+        return cqueue_;
+    }
+
+    /**
+     * \copydoc server::cmode
+     */
+    void cmode(std::string channel, std::string mode) override;
+
+    /**
+     * \copydoc server::cnotice
+     */
+    void cnotice(std::string channel, std::string message) override;
+
+    /**
+     * \copydoc server::invite
+     */
+    void invite(std::string target, std::string channel) override;
+
+    /**
+     * \copydoc server::join
+     */
+    void join(std::string channel, std::string password = "") override;
+
+    /**
+     * \copydoc server::kick
+     */
+    void kick(std::string target, std::string channel, std::string reason = "") override;
+
+    /**
+     * \copydoc server::me
+     */
+    void me(std::string target, std::string message) override;
+
+    /**
+     * \copydoc server::message
+     */
+    void message(std::string target, std::string message) override;
+
+    /**
+     * \copydoc server::mode
+     */
+    void mode(std::string mode) override;
+
+    /**
+     * \copydoc server::names
+     */
+    void names(std::string channel) override;
+
+    /**
+     * \copydoc server::notice
+     */
+    void notice(std::string target, std::string message) override;
+
+    /**
+     * \copydoc server::part
+     */
+    void part(std::string channel, std::string reason = "") override;
+
+    /**
+     * \copydoc server::send
+     */
+    void send(std::string raw) override;
+
+    /**
+     * \copydoc server::topic
+     */
+    void topic(std::string channel, std::string topic) override;
+
+    /**
+     * \copydoc server::whois
+     */
+    void whois(std::string target) override;
+};
+
+} // !irccd
+
+#endif // !IRCCD_JOURNAL_SERVER_HPP