comparison tests/plugin-logger/main.cpp @ 164:3b38931801ff

Plugin logger: add initial unit test
author David Demelier <markand@malikania.fr>
date Wed, 25 May 2016 21:27:16 +0200
parents
children 6e27e3cf98fb
comparison
equal deleted inserted replaced
163:557b0e318d20 164:3b38931801ff
1 /*
2 * main.cpp -- test hangman plugin
3 *
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <fstream>
20 #include <iterator>
21
22 #include <gtest/gtest.h>
23
24 #include <irccd/irccd.hpp>
25 #include <irccd/logger.hpp>
26 #include <irccd/server.hpp>
27 #include <irccd/service-plugin.hpp>
28
29 using namespace irccd;
30
31 class ServerTest : public Server {
32 public:
33 inline ServerTest()
34 : Server("test", ServerInfo())
35 {
36 }
37 };
38
39 class LoggerTest : public testing::Test {
40 protected:
41 Irccd m_irccd;
42 PluginService &m_ps;
43
44 std::shared_ptr<ServerTest> m_server;
45 std::shared_ptr<Plugin> m_plugin;
46
47 std::string last() const
48 {
49 std::ifstream file(BINARYDIR "/log.txt");
50
51 return std::string(std::istreambuf_iterator<char>(file.rdbuf()), {});
52 }
53
54 public:
55 LoggerTest()
56 : m_ps(m_irccd.pluginService())
57 , m_server(std::make_shared<ServerTest>())
58 {
59 remove(BINARYDIR "/log.txt");
60
61 m_ps.setFormats("logger", {
62 { "cmode", "cmode=#{server}:#{channel}:#{origin}:#{nickname}:#{mode}:#{arg}" },
63 { "cnotice", "cnotice=#{server}:#{channel}:#{origin}:#{nickname}:#{message}" },
64 { "join", "join=#{server}:#{channel}:#{origin}:#{nickname}" },
65 { "kick", "kick=#{server}:#{channel}:#{origin}:#{nickname}:#{target}:#{reason}" },
66 { "me", "me=#{server}:#{channel}:#{origin}:#{nickname}:#{message}" },
67 { "message", "message=#{server}:#{channel}:#{origin}:#{nickname}:#{message}" },
68 { "mode", "mode=#{server}:#{origin}:#{nickname}:#{mode}:#{arg}" },
69 { "notice", "notice=#{server}:#{origin}:#{nickname}:#{message}" },
70 { "part", "part=#{server}:#{channel}:#{origin}:#{nickname}:#{reason}" },
71 { "query", "query=#{server}:#{origin}:#{nickname}:#{message}" },
72 { "topic", "topic=#{server}:#{channel}:#{origin}:#{nickname}:#{topic}" },
73 });
74 }
75
76 void load(PluginConfig config = PluginConfig())
77 {
78 if (config.count("path") == 0)
79 config.emplace("path", BINARYDIR "/log.txt");
80
81 m_ps.configure("logger", config);
82 m_ps.load("logger", PLUGINDIR "/logger.js");
83 m_plugin = m_ps.require("logger");
84 }
85 };
86
87 TEST_F(LoggerTest, formatChannelMode)
88 {
89 load();
90
91 m_plugin->onChannelMode(m_irccd, m_server, "jean!jean@localhost", "#staff", "+o", "jean");
92
93 ASSERT_EQ("cmode=test:#staff:jean!jean@localhost:jean:+o:jean\n", last());
94 }
95
96 TEST_F(LoggerTest, formatChannelNotice)
97 {
98 load();
99
100 m_plugin->onChannelNotice(m_irccd, m_server, "jean!jean@localhost", "#staff", "bonjour!");
101
102 ASSERT_EQ("cnotice=test:#staff:jean!jean@localhost:jean:bonjour!\n", last());
103 }
104
105 TEST_F(LoggerTest, formatJoin)
106 {
107 load();
108
109 m_plugin->onJoin(m_irccd, m_server, "jean!jean@localhost", "#staff");
110
111 ASSERT_EQ("join=test:#staff:jean!jean@localhost:jean\n", last());
112 }
113
114 TEST_F(LoggerTest, formatKick)
115 {
116 load();
117
118 m_plugin->onKick(m_irccd, m_server, "jean!jean@localhost", "#staff", "badboy", "please do not flood");
119
120 ASSERT_EQ("kick=test:#staff:jean!jean@localhost:jean:badboy:please do not flood\n", last());
121 }
122
123 TEST_F(LoggerTest, formatMe)
124 {
125 load();
126
127 m_plugin->onMe(m_irccd, m_server, "jean!jean@localhost", "#staff", "is drinking water");
128
129 ASSERT_EQ("me=test:#staff:jean!jean@localhost:jean:is drinking water\n", last());
130 }
131
132 TEST_F(LoggerTest, formatMessage)
133 {
134 load();
135
136 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#staff", "hello guys");
137
138 ASSERT_EQ("message=test:#staff:jean!jean@localhost:jean:hello guys\n", last());
139 }
140
141 TEST_F(LoggerTest, formatMode)
142 {
143 load();
144
145 m_plugin->onMode(m_irccd, m_server, "jean!jean@localhost", "+i");
146
147 ASSERT_EQ("mode=test:jean!jean@localhost:jean:+i:\n", last());
148 }
149
150 TEST_F(LoggerTest, formatNotice)
151 {
152 load();
153
154 m_plugin->onNotice(m_irccd, m_server, "jean!jean@localhost", "tu veux voir mon chat ?");
155
156 ASSERT_EQ("notice=test:jean!jean@localhost:jean:tu veux voir mon chat ?\n", last());
157 }
158
159 TEST_F(LoggerTest, formatPart)
160 {
161 load();
162
163 m_plugin->onPart(m_irccd, m_server, "jean!jean@localhost", "#staff", "too noisy here");
164
165 ASSERT_EQ("part=test:#staff:jean!jean@localhost:jean:too noisy here\n", last());
166 }
167
168 TEST_F(LoggerTest, formatQuery)
169 {
170 load();
171
172 m_plugin->onQuery(m_irccd, m_server, "jean!jean@localhost", "much irccd, wow");
173
174 ASSERT_EQ("query=test:jean!jean@localhost:jean:much irccd, wow\n", last());
175 }
176
177 TEST_F(LoggerTest, formatTopic)
178 {
179 load();
180
181 m_plugin->onTopic(m_irccd, m_server, "jean!jean@localhost", "#staff", "oh yeah yeaaaaaaaah");
182
183 ASSERT_EQ("topic=test:#staff:jean!jean@localhost:jean:oh yeah yeaaaaaaaah\n", last());
184 }
185
186 int main(int argc, char **argv)
187 {
188 path::setApplicationPath(argv[0]);
189 testing::InitGoogleTest(&argc, argv);
190 log::setInterface(std::make_unique<log::Silent>());
191
192 return RUN_ALL_TESTS();
193 }