comparison tests/plugin-hangman/main.cpp @ 155:20cd5dc0e063

Plugin hangman: add initial unit test
author David Demelier <markand@malikania.fr>
date Sun, 22 May 2016 23:19:05 +0200
parents
children 1c22fcce1662
comparison
equal deleted inserted replaced
154:f554d1314208 155:20cd5dc0e063
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 <gtest/gtest.h>
20
21 #include <irccd/irccd.hpp>
22 #include <irccd/server.hpp>
23 #include <irccd/service-plugin.hpp>
24
25 using namespace irccd;
26
27 class ServerTest : public Server {
28 private:
29 std::string m_last;
30
31 public:
32 inline ServerTest()
33 : Server("test", ServerInfo())
34 {
35 }
36
37 inline const std::string &last() const noexcept
38 {
39 return m_last;
40 }
41
42 void message(std::string target, std::string message) override
43 {
44 m_last = util::join({target, message});
45 }
46 };
47
48 class HangmanTest : public testing::Test {
49 protected:
50 Irccd m_irccd;
51 PluginService &m_ps;
52
53 std::shared_ptr<ServerTest> m_server;
54 std::shared_ptr<Plugin> m_plugin;
55
56 public:
57 HangmanTest()
58 : m_ps(m_irccd.pluginService())
59 , m_server(std::make_shared<ServerTest>())
60 {
61 m_ps.setFormats("hangman", {
62 { "asked", "asked=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{letter}" },
63 { "dead", "dead=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{word}" },
64 { "found", "found=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{word}" },
65 { "start", "start=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{word}" },
66 { "win", "win=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{word}" },
67 { "wrong-letter", "wrong-letter=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{letter}" },
68 { "wrong-player", "wrong-player=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{letter}" },
69 { "wrong-word", "wrong-word=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{word}" }
70 });
71 }
72
73 void load(PluginConfig config = PluginConfig())
74 {
75 // Add file if not there.
76 if (config.count("file") == 0)
77 config.emplace("file", SOURCEDIR "/words.conf");
78
79 m_ps.configure("hangman", config);
80 m_ps.load("hangman", PLUGINDIR "/hangman.js");
81 m_plugin = m_ps.require("hangman");
82 }
83 };
84
85 TEST_F(HangmanTest, asked)
86 {
87 load({{ "collaborative", "false" }});
88
89 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
90 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "s");
91 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "s");
92
93 ASSERT_EQ("#hangman:asked=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:s", m_server->last());
94 }
95
96 TEST_F(HangmanTest, dead)
97 {
98 load({{ "collaborative", "false" }});
99
100 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
101 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "a");
102 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "b");
103 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "c");
104 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "d");
105 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "e");
106 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "f");
107 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "g");
108 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "h");
109 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "i");
110 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "j");
111
112 ASSERT_EQ("#hangman:dead=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:sky", m_server->last());
113 }
114
115 TEST_F(HangmanTest, found)
116 {
117 load({{ "collaborative", "false" }});
118
119 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
120 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "s");
121
122 ASSERT_EQ("#hangman:found=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:s _ _", m_server->last());
123 }
124
125 TEST_F(HangmanTest, start)
126 {
127 load();
128
129 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
130
131 ASSERT_EQ("#hangman:start=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:_ _ _", m_server->last());
132 }
133
134 TEST_F(HangmanTest, win1)
135 {
136 load({{ "collaborative", "false" }});
137
138 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
139 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "s");
140 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "k");
141 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "y");
142
143 ASSERT_EQ("#hangman:win=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:sky", m_server->last());
144 }
145
146 TEST_F(HangmanTest, win2)
147 {
148 load({{ "collaborative", "false" }});
149
150 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
151 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "sky");
152
153 ASSERT_EQ("#hangman:win=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:sky", m_server->last());
154 }
155
156 TEST_F(HangmanTest, wrongLetter)
157 {
158 load();
159
160 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
161 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "x");
162
163 ASSERT_EQ("#hangman:wrong-letter=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:x", m_server->last());
164 }
165
166 TEST_F(HangmanTest, wrongWord)
167 {
168 load();
169
170 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
171 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "cheese");
172
173 ASSERT_EQ("#hangman:wrong-word=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:cheese", m_server->last());
174 }
175
176 TEST_F(HangmanTest, collaborativeDisabled)
177 {
178 // Disable collaborative mode.
179 load({{ "collaborative", "false" }});
180
181 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
182 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "s");
183 ASSERT_EQ("#hangman:found=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:s _ _", m_server->last());
184 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "k");
185 ASSERT_EQ("#hangman:found=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:s k _", m_server->last());
186 }
187
188 TEST_F(HangmanTest, collaborativeEnabled)
189 {
190 // Enable collaborative mode.
191 load({{ "collaborative", "true" }});
192
193 m_plugin->onCommand(m_irccd, m_server, "jean!jean@localhost", "#hangman", "");
194 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "s");
195 ASSERT_EQ("#hangman:found=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:s _ _", m_server->last());
196 m_plugin->onMessage(m_irccd, m_server, "jean!jean@localhost", "#hangman", "k");
197 ASSERT_EQ("#hangman:wrong-player=hangman:!hangman:test:#hangman:jean!jean@localhost:jean:k", m_server->last());
198 m_plugin->onMessage(m_irccd, m_server, "francis!francis@localhost", "#hangman", "k");
199 ASSERT_EQ("#hangman:found=hangman:!hangman:test:#hangman:francis!francis@localhost:francis:s k _", m_server->last());
200 }
201
202 int main(int argc, char **argv)
203 {
204 path::setApplicationPath(argv[0]);
205 testing::InitGoogleTest(&argc, argv);
206
207 return RUN_ALL_TESTS();
208 }