changeset 164:3b38931801ff

Plugin logger: add initial unit test
author David Demelier <markand@malikania.fr>
date Wed, 25 May 2016 21:27:16 +0200
parents 557b0e318d20
children 6e27e3cf98fb
files plugins/logger/logger.js tests/CMakeLists.txt tests/plugin-logger/CMakeLists.txt tests/plugin-logger/main.cpp
diffstat 4 files changed, 227 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/logger/logger.js	Wed May 25 20:56:48 2016 +0200
+++ b/plugins/logger/logger.js	Wed May 25 21:27:16 2016 +0200
@@ -16,13 +16,6 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-// Modules.
-var Directory	= Irccd.Directory;
-var File	= Irccd.File;
-var Logger	= Irccd.Logger;
-var Plugin	= Irccd.Plugin;
-var Util	= Irccd.Util;
-
 // Plugin information.
 info = {
 	author: "David Demelier <markand@malikania.fr>",
@@ -31,6 +24,13 @@
 	version: "@IRCCD_VERSION@"
 };
 
+// Modules.
+var Directory	= Irccd.Directory;
+var File	= Irccd.File;
+var Logger	= Irccd.Logger;
+var Plugin	= Irccd.Plugin;
+var Util	= Irccd.Util;
+
 /**
  * All available formats.
  */
@@ -53,7 +53,7 @@
  */
 function loadFormats()
 {
-	// --- DEPRECATED ------------------------------------------
+	// --- DEPRECATED -------------------------------------------
 	//
 	// This code will be removed.
 	//
@@ -95,7 +95,7 @@
 	var path = Util.format(Plugin.config["path"], args);
 	var directory = File.dirname(path);
 
-	/* Try to create the directory */
+	// Try to create the directory.
 	if (!File.exists(directory)) {
 		Logger.debug("creating directory: " + directory);
 		Directory.mkdir(directory);
--- a/tests/CMakeLists.txt	Wed May 25 20:56:48 2016 +0200
+++ b/tests/CMakeLists.txt	Wed May 25 21:27:16 2016 +0200
@@ -42,5 +42,6 @@
 		add_subdirectory(plugin-auth)
 		add_subdirectory(plugin-hangman)
 		add_subdirectory(plugin-history)
+		add_subdirectory(plugin-logger)
 	endif ()
 endif ()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/plugin-logger/CMakeLists.txt	Wed May 25 21:27:16 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 plugin-logger
+	SOURCES main.cpp
+	LIBRARIES libirccd
+	FLAGS PLUGINDIR=\"${IRCCD_FAKEROOTDIR}/${WITH_PLUGINDIR}\"
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/plugin-logger/main.cpp	Wed May 25 21:27:16 2016 +0200
@@ -0,0 +1,193 @@
+/*
+ * main.cpp -- test hangman plugin
+ *
+ * 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 <fstream>
+#include <iterator>
+
+#include <gtest/gtest.h>
+
+#include <irccd/irccd.hpp>
+#include <irccd/logger.hpp>
+#include <irccd/server.hpp>
+#include <irccd/service-plugin.hpp>
+
+using namespace irccd;
+
+class ServerTest : public Server {
+public:
+	inline ServerTest()
+		: Server("test", ServerInfo())
+	{
+	}
+};
+
+class LoggerTest : public testing::Test {
+protected:
+	Irccd m_irccd;
+	PluginService &m_ps;
+
+	std::shared_ptr<ServerTest> m_server;
+	std::shared_ptr<Plugin> m_plugin;
+
+	std::string last() const
+	{
+		std::ifstream file(BINARYDIR "/log.txt");
+
+		return std::string(std::istreambuf_iterator<char>(file.rdbuf()), {});
+	}
+
+public:
+	LoggerTest()
+		: m_ps(m_irccd.pluginService())
+		, m_server(std::make_shared<ServerTest>())
+	{
+		remove(BINARYDIR "/log.txt");
+
+		m_ps.setFormats("logger", {
+			{ "cmode", "cmode=#{server}:#{channel}:#{origin}:#{nickname}:#{mode}:#{arg}" },
+			{ "cnotice", "cnotice=#{server}:#{channel}:#{origin}:#{nickname}:#{message}" },
+			{ "join", "join=#{server}:#{channel}:#{origin}:#{nickname}" },
+			{ "kick", "kick=#{server}:#{channel}:#{origin}:#{nickname}:#{target}:#{reason}" },
+			{ "me", "me=#{server}:#{channel}:#{origin}:#{nickname}:#{message}" },
+			{ "message", "message=#{server}:#{channel}:#{origin}:#{nickname}:#{message}" },
+			{ "mode", "mode=#{server}:#{origin}:#{nickname}:#{mode}:#{arg}" },
+			{ "notice", "notice=#{server}:#{origin}:#{nickname}:#{message}" },
+			{ "part", "part=#{server}:#{channel}:#{origin}:#{nickname}:#{reason}" },
+			{ "query", "query=#{server}:#{origin}:#{nickname}:#{message}" },
+			{ "topic", "topic=#{server}:#{channel}:#{origin}:#{nickname}:#{topic}" },
+		});
+	}
+
+	void load(PluginConfig config = PluginConfig())
+	{
+		if (config.count("path") == 0)
+			config.emplace("path", BINARYDIR "/log.txt");
+
+		m_ps.configure("logger", config);
+		m_ps.load("logger", PLUGINDIR "/logger.js");
+		m_plugin = m_ps.require("logger");
+	}
+};
+
+TEST_F(LoggerTest, formatChannelMode)
+{
+	load();
+
+	m_plugin->onChannelMode(m_irccd, m_server, "jean!jean@localhost", "#staff", "+o", "jean");
+
+	ASSERT_EQ("cmode=test:#staff:jean!jean@localhost:jean:+o:jean\n", last());
+}
+
+TEST_F(LoggerTest, formatChannelNotice)
+{
+	load();
+
+	m_plugin->onChannelNotice(m_irccd, m_server, "jean!jean@localhost", "#staff", "bonjour!");
+
+	ASSERT_EQ("cnotice=test:#staff:jean!jean@localhost:jean:bonjour!\n", last());
+}
+
+TEST_F(LoggerTest, formatJoin)
+{
+	load();
+
+	m_plugin->onJoin(m_irccd, m_server, "jean!jean@localhost", "#staff");
+
+	ASSERT_EQ("join=test:#staff:jean!jean@localhost:jean\n", last());
+}
+
+TEST_F(LoggerTest, formatKick)
+{
+	load();
+
+	m_plugin->onKick(m_irccd, m_server, "jean!jean@localhost", "#staff", "badboy", "please do not flood");
+
+	ASSERT_EQ("kick=test:#staff:jean!jean@localhost:jean:badboy:please do not flood\n", last());
+}
+
+TEST_F(LoggerTest, formatMe)
+{
+	load();
+
+	m_plugin->onMe(m_irccd, m_server, "jean!jean@localhost", "#staff", "is drinking water");
+
+	ASSERT_EQ("me=test:#staff:jean!jean@localhost:jean:is drinking water\n", last());
+}
+
+TEST_F(LoggerTest, formatMessage)
+{
+	load();
+
+	m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#staff", "hello guys");
+
+	ASSERT_EQ("message=test:#staff:jean!jean@localhost:jean:hello guys\n", last());
+}
+
+TEST_F(LoggerTest, formatMode)
+{
+	load();
+
+	m_plugin->onMode(m_irccd, m_server, "jean!jean@localhost", "+i");
+
+	ASSERT_EQ("mode=test:jean!jean@localhost:jean:+i:\n", last());
+}
+
+TEST_F(LoggerTest, formatNotice)
+{
+	load();
+
+	m_plugin->onNotice(m_irccd, m_server, "jean!jean@localhost", "tu veux voir mon chat ?");
+
+	ASSERT_EQ("notice=test:jean!jean@localhost:jean:tu veux voir mon chat ?\n", last());
+}
+
+TEST_F(LoggerTest, formatPart)
+{
+	load();
+
+	m_plugin->onPart(m_irccd, m_server, "jean!jean@localhost", "#staff", "too noisy here");
+
+	ASSERT_EQ("part=test:#staff:jean!jean@localhost:jean:too noisy here\n", last());
+}
+
+TEST_F(LoggerTest, formatQuery)
+{
+	load();
+
+	m_plugin->onQuery(m_irccd, m_server, "jean!jean@localhost", "much irccd, wow");
+
+	ASSERT_EQ("query=test:jean!jean@localhost:jean:much irccd, wow\n", last());
+}
+
+TEST_F(LoggerTest, formatTopic)
+{
+	load();
+
+	m_plugin->onTopic(m_irccd, m_server, "jean!jean@localhost", "#staff", "oh yeah yeaaaaaaaah");
+
+	ASSERT_EQ("topic=test:#staff:jean!jean@localhost:jean:oh yeah yeaaaaaaaah\n", last());
+}
+
+int main(int argc, char **argv)
+{
+	path::setApplicationPath(argv[0]);
+	testing::InitGoogleTest(&argc, argv);
+	log::setInterface(std::make_unique<log::Silent>());
+
+	return RUN_ALL_TESTS();
+}