Mercurial > irccd
comparison tests/src/plugins/joke/main.cpp @ 618:5afc0b3a9ad8
Plugin joke: brand new plugin, closes #609 @2h
The new joke plugin offers a convenient registry of jokes that are displayed in
a random and unique order.
It keeps track of displayed jokes per channel/server pairs.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 19 Dec 2017 22:02:12 +0100 |
parents | |
children | 27587ff92a64 |
comparison
equal
deleted
inserted
replaced
617:241583937af0 | 618:5afc0b3a9ad8 |
---|---|
1 /* | |
2 * main.cpp -- test joke plugin | |
3 * | |
4 * Copyright (c) 2013-2017 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 #define BOOST_TEST_MODULE "Joke plugin" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <irccd/test/plugin_test.hpp> | |
23 | |
24 namespace irccd { | |
25 | |
26 class joke_test : public plugin_test { | |
27 public: | |
28 joke_test() | |
29 : plugin_test(PLUGIN_NAME, PLUGIN_PATH) | |
30 { | |
31 plugin_->set_formats({ | |
32 { "error", "error=#{server}:#{channel}:#{origin}:#{nickname}" } | |
33 }); | |
34 } | |
35 | |
36 void load(plugin_config config = {}) | |
37 { | |
38 // Add file if not there. | |
39 if (config.count("file") == 0) | |
40 config.emplace("file", CMAKE_CURRENT_SOURCE_DIR "/jokes.json"); | |
41 | |
42 plugin_->set_config(config); | |
43 plugin_->on_load(irccd_); | |
44 } | |
45 }; | |
46 | |
47 BOOST_FIXTURE_TEST_SUITE(joke_test_suite, joke_test) | |
48 | |
49 BOOST_AUTO_TEST_CASE(simple) | |
50 { | |
51 /* | |
52 * Jokes.json have two jokes. | |
53 * | |
54 * aaa | |
55 * | |
56 * And | |
57 * | |
58 * bbbb | |
59 * bbbb | |
60 */ | |
61 std::unordered_map<std::string, int> said{ | |
62 { "aaa", 0 }, | |
63 { "bbbb", 0 } | |
64 }; | |
65 | |
66 load(); | |
67 | |
68 auto call = [&] () { | |
69 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#joke", ""}); | |
70 | |
71 auto cmd = server_->cqueue().back(); | |
72 | |
73 // "bbbb" is two lines. | |
74 if (cmd["message"] == "bbbb") { | |
75 auto first = server_->cqueue().front(); | |
76 | |
77 BOOST_TEST(first["command"].template get<std::string>() == "message"); | |
78 BOOST_TEST(first["target"].template get<std::string>() == "#joke"); | |
79 BOOST_TEST(first["message"].template get<std::string>() == "bbbb"); | |
80 } else | |
81 BOOST_TEST(cmd["message"].template get<std::string>() == "aaa"); | |
82 | |
83 said[cmd["message"].template get<std::string>()] += 1; | |
84 server_->cqueue().clear(); | |
85 }; | |
86 | |
87 call(); | |
88 call(); | |
89 | |
90 BOOST_TEST(said.size() == 2U); | |
91 BOOST_TEST(said["aaa"] == 1U); | |
92 BOOST_TEST(said["bbbb"] == 1U); | |
93 } | |
94 | |
95 BOOST_AUTO_TEST_CASE(toobig) | |
96 { | |
97 // xxx and yyy are both 3-lines which we disallow. only a must be said. | |
98 load({ | |
99 { "file", CMAKE_CURRENT_SOURCE_DIR "/jokes-toobig.json" }, | |
100 { "max-list-lines", "2" } | |
101 }); | |
102 | |
103 std::unordered_map<std::string, int> said{ | |
104 { "a", 0 } | |
105 }; | |
106 | |
107 auto call = [&] () { | |
108 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#joke", ""}); | |
109 | |
110 auto cmd = server_->cqueue().back(); | |
111 | |
112 BOOST_TEST(cmd["command"].template get<std::string>() == "message"); | |
113 BOOST_TEST(cmd["target"].template get<std::string>() == "#joke"); | |
114 BOOST_TEST(cmd["message"].template get<std::string>() == "a"); | |
115 | |
116 said[cmd["message"].template get<std::string>()] += 1; | |
117 server_->cqueue().clear(); | |
118 }; | |
119 | |
120 call(); | |
121 call(); | |
122 call(); | |
123 | |
124 BOOST_TEST(said.size() == 1U); | |
125 BOOST_TEST(said["a"] == 3U); | |
126 } | |
127 | |
128 BOOST_AUTO_TEST_CASE(invalid) | |
129 { | |
130 // Only a is the valid joke in this file. | |
131 load({ | |
132 { "file", CMAKE_CURRENT_SOURCE_DIR "/jokes-invalid.json" }, | |
133 }); | |
134 | |
135 std::unordered_map<std::string, int> said{ | |
136 { "a", 0 } | |
137 }; | |
138 | |
139 auto call = [&] () { | |
140 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#joke", ""}); | |
141 | |
142 auto cmd = server_->cqueue().back(); | |
143 | |
144 BOOST_TEST(cmd["command"].template get<std::string>() == "message"); | |
145 BOOST_TEST(cmd["target"].template get<std::string>() == "#joke"); | |
146 BOOST_TEST(cmd["message"].template get<std::string>() == "a"); | |
147 | |
148 server_->cqueue().clear(); | |
149 said[cmd["message"].template get<std::string>()] += 1; | |
150 }; | |
151 | |
152 call(); | |
153 call(); | |
154 call(); | |
155 | |
156 BOOST_TEST(said.size() == 1U); | |
157 BOOST_TEST(said["a"] == 3U); | |
158 } | |
159 | |
160 BOOST_AUTO_TEST_SUITE(errors) | |
161 | |
162 BOOST_AUTO_TEST_CASE(not_found) | |
163 { | |
164 load({{"file", "doesnotexist.json"}}); | |
165 | |
166 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#joke", ""}); | |
167 | |
168 auto cmd = server_->cqueue().back(); | |
169 | |
170 BOOST_TEST(cmd["command"].get<std::string>() == "message"); | |
171 BOOST_TEST(cmd["target"].get<std::string>() == "#joke"); | |
172 BOOST_TEST(cmd["message"].get<std::string>() == "error=test:#joke:jean!jean@localhost:jean"); | |
173 } | |
174 | |
175 BOOST_AUTO_TEST_CASE(not_array) | |
176 { | |
177 load({{"file", CMAKE_CURRENT_SOURCE_DIR "/jokes-not-array.json"}}); | |
178 | |
179 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#joke", ""}); | |
180 | |
181 auto cmd = server_->cqueue().back(); | |
182 | |
183 BOOST_TEST(cmd["command"].get<std::string>() == "message"); | |
184 BOOST_TEST(cmd["target"].get<std::string>() == "#joke"); | |
185 BOOST_TEST(cmd["message"].get<std::string>() == "error=test:#joke:jean!jean@localhost:jean"); | |
186 } | |
187 | |
188 BOOST_AUTO_TEST_CASE(empty) | |
189 { | |
190 load({{"file", CMAKE_CURRENT_SOURCE_DIR "/jokes-empty.json"}}); | |
191 | |
192 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#joke", ""}); | |
193 | |
194 auto cmd = server_->cqueue().back(); | |
195 | |
196 BOOST_TEST(cmd["command"].get<std::string>() == "message"); | |
197 BOOST_TEST(cmd["target"].get<std::string>() == "#joke"); | |
198 BOOST_TEST(cmd["message"].get<std::string>() == "error=test:#joke:jean!jean@localhost:jean"); | |
199 } | |
200 | |
201 BOOST_AUTO_TEST_SUITE_END() | |
202 | |
203 BOOST_AUTO_TEST_SUITE_END() | |
204 | |
205 } // !irccd |