comparison tests/src/plugins/tictactoe/main.cpp @ 773:8c44bbcbbab9

Misc: style, cleanup and update
author David Demelier <markand@malikania.fr>
date Fri, 26 Oct 2018 13:01:00 +0200
parents 791d7591eca7
children f362994133ca
comparison
equal deleted inserted replaced
772:f5ccf65ae929 773:8c44bbcbbab9
31 31
32 namespace { 32 namespace {
33 33
34 class test_fixture : public js_plugin_fixture { 34 class test_fixture : public js_plugin_fixture {
35 public: 35 public:
36 test_fixture() 36 test_fixture()
37 : js_plugin_fixture(PLUGIN_PATH) 37 : js_plugin_fixture(PLUGIN_PATH)
38 { 38 {
39 plugin_->set_formats({ 39 plugin_->set_formats({
40 { "draw", "draw=#{channel}:#{command}:#{nickname}:#{plugin}:#{server}" }, 40 { "draw", "draw=#{channel}:#{command}:#{nickname}:#{plugin}:#{server}" },
41 { "invalid", "invalid=#{channel}:#{command}:#{nickname}:#{origin}:#{plugin}:#{server}" }, 41 { "invalid", "invalid=#{channel}:#{command}:#{nickname}:#{origin}:#{plugin}:#{server}" },
42 { "running", "running=#{channel}:#{command}:#{nickname}:#{origin}:#{plugin}:#{server}" }, 42 { "running", "running=#{channel}:#{command}:#{nickname}:#{origin}:#{plugin}:#{server}" },
43 { "turn", "turn=#{channel}:#{command}:#{nickname}:#{plugin}:#{server}" }, 43 { "turn", "turn=#{channel}:#{command}:#{nickname}:#{plugin}:#{server}" },
44 { "used", "used=#{channel}:#{command}:#{nickname}:#{origin}:#{plugin}:#{server}" }, 44 { "used", "used=#{channel}:#{command}:#{nickname}:#{origin}:#{plugin}:#{server}" },
45 { "win", "win=#{channel}:#{command}:#{nickname}:#{plugin}:#{server}" } 45 { "win", "win=#{channel}:#{command}:#{nickname}:#{plugin}:#{server}" }
46 }); 46 });
47 } 47 }
48 48
49 auto next_players() const -> std::pair<std::string, std::string> 49 auto next_players() const -> std::pair<std::string, std::string>
50 { 50 {
51 const auto functions = server_->find("message"); 51 const auto functions = server_->find("message");
52 52
53 if (functions.size() == 0U) 53 if (functions.size() == 0U)
54 throw std::runtime_error("no message"); 54 throw std::runtime_error("no message");
55 55
56 const auto cmd = functions.back(); 56 const auto cmd = functions.back();
57 const auto list = string_util::split(std::any_cast<std::string>(cmd[1]), ":"); 57 const auto list = string_util::split(std::any_cast<std::string>(cmd[1]), ":");
58 58
59 BOOST_TEST(list.size() == 5U); 59 BOOST_TEST(list.size() == 5U);
60 BOOST_TEST(list[0] == "turn=#tictactoe"); 60 BOOST_TEST(list[0] == "turn=#tictactoe");
61 BOOST_TEST(list[1] == "!tictactoe"); 61 BOOST_TEST(list[1] == "!tictactoe");
62 BOOST_TEST(list[3] == "tictactoe"); 62 BOOST_TEST(list[3] == "tictactoe");
63 BOOST_TEST(list[4] == "test"); 63 BOOST_TEST(list[4] == "test");
64 64
65 return list[2] == "a" ? std::make_pair("a", "b") : std::make_pair("b", "a"); 65 return list[2] == "a" ? std::make_pair("a", "b") : std::make_pair("b", "a");
66 } 66 }
67 67
68 auto start() 68 auto start()
69 { 69 {
70 plugin_->handle_command(irccd_, { server_, "a!a@localhost", "#tictactoe", "b" }); 70 plugin_->handle_command(irccd_, { server_, "a!a@localhost", "#tictactoe", "b" });
71 plugin_->handle_names(irccd_, { server_, "#tictactoe", { "a", "b" }}); 71 plugin_->handle_names(irccd_, { server_, "#tictactoe", { "a", "b" }});
72 72
73 return next_players(); 73 return next_players();
74 } 74 }
75 75
76 /** 76 /**
77 * Helper to place several tokens on the board and automatically toggling 77 * Helper to place several tokens on the board and automatically toggling
78 * players. 78 * players.
79 * 79 *
80 * This will start the game from "a" with target opponent "b". 80 * This will start the game from "a" with target opponent "b".
81 * 81 *
82 */ 82 */
83 void run(const std::initializer_list<std::string>& points) 83 void run(const std::initializer_list<std::string>& points)
84 { 84 {
85 auto players = start(); 85 auto players = start();
86 86
87 for (const auto& p : points) { 87 for (const auto& p : points) {
88 server_->clear(); 88 server_->clear();
89 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", p }); 89 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", p });
90 players = next_players(); 90 players = next_players();
91 } 91 }
92 } 92 }
93 }; 93 };
94 94
95 BOOST_FIXTURE_TEST_SUITE(test_fixture_suite, test_fixture) 95 BOOST_FIXTURE_TEST_SUITE(test_fixture_suite, test_fixture)
96 96
97 BOOST_AUTO_TEST_CASE(win) 97 BOOST_AUTO_TEST_CASE(win)
98 { 98 {
99 run({ "a 1", "b1", "a 2", "b2" }); 99 run({ "a 1", "b1", "a 2", "b2" });
100 100
101 const auto players = next_players(); 101 const auto players = next_players();
102 102
103 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 3" }); 103 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 3" });
104 104
105 const auto cmd = server_->find("message").back(); 105 const auto cmd = server_->find("message").back();
106 106
107 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe"); 107 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe");
108 108
109 const auto parts = string_util::split(std::any_cast<std::string>(cmd[1]), ":"); 109 const auto parts = string_util::split(std::any_cast<std::string>(cmd[1]), ":");
110 110
111 BOOST_TEST(parts.size() == 5U); 111 BOOST_TEST(parts.size() == 5U);
112 BOOST_TEST(parts[0] == "win=#tictactoe"); 112 BOOST_TEST(parts[0] == "win=#tictactoe");
113 BOOST_TEST(parts[1] == "!tictactoe"); 113 BOOST_TEST(parts[1] == "!tictactoe");
114 BOOST_TEST(parts[2] == players.first); 114 BOOST_TEST(parts[2] == players.first);
115 BOOST_TEST(parts[3] == "tictactoe"); 115 BOOST_TEST(parts[3] == "tictactoe");
116 BOOST_TEST(parts[4] == "test"); 116 BOOST_TEST(parts[4] == "test");
117 } 117 }
118 118
119 BOOST_AUTO_TEST_CASE(draw) 119 BOOST_AUTO_TEST_CASE(draw)
120 { 120 {
121 /* 121 /*
122 * a b c 122 * a b c
123 * 1 o x o 123 * 1 o x o
124 * 2 o x x 124 * 2 o x x
125 * 3 x o x 125 * 3 x o x
126 */ 126 */
127 run({ "b 2", "c 1", "c 3", "b 3", "c 2", "a 2", "a 3", "a 1" }); 127 run({ "b 2", "c 1", "c 3", "b 3", "c 2", "a 2", "a 3", "a 1" });
128 128
129 const auto players = next_players(); 129 const auto players = next_players();
130 130
131 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "b 1" }); 131 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "b 1" });
132 132
133 const auto cmd = server_->find("message").back(); 133 const auto cmd = server_->find("message").back();
134 134
135 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe"); 135 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe");
136 136
137 const auto parts = string_util::split(std::any_cast<std::string>(cmd[1]), ":"); 137 const auto parts = string_util::split(std::any_cast<std::string>(cmd[1]), ":");
138 138
139 BOOST_TEST(parts.size() == 5U); 139 BOOST_TEST(parts.size() == 5U);
140 BOOST_TEST(parts[0] == "draw=#tictactoe"); 140 BOOST_TEST(parts[0] == "draw=#tictactoe");
141 BOOST_TEST(parts[1] == "!tictactoe"); 141 BOOST_TEST(parts[1] == "!tictactoe");
142 BOOST_TEST(parts[2] == players.first); 142 BOOST_TEST(parts[2] == players.first);
143 BOOST_TEST(parts[3] == "tictactoe"); 143 BOOST_TEST(parts[3] == "tictactoe");
144 BOOST_TEST(parts[4] == "test"); 144 BOOST_TEST(parts[4] == "test");
145 } 145 }
146 146
147 BOOST_AUTO_TEST_CASE(used) 147 BOOST_AUTO_TEST_CASE(used)
148 { 148 {
149 auto players = start(); 149 auto players = start();
150 150
151 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 1" }); 151 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 1" });
152 plugin_->handle_message(irccd_, { server_, players.second, "#tictactoe", "a 1" }); 152 plugin_->handle_message(irccd_, { server_, players.second, "#tictactoe", "a 1" });
153 153
154 const auto cmd = server_->find("message").back(); 154 const auto cmd = server_->find("message").back();
155 155
156 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe"); 156 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe");
157 157
158 const auto parts = string_util::split(std::any_cast<std::string>(cmd[1]), ":"); 158 const auto parts = string_util::split(std::any_cast<std::string>(cmd[1]), ":");
159 159
160 BOOST_TEST(parts[0] == "used=#tictactoe"); 160 BOOST_TEST(parts[0] == "used=#tictactoe");
161 BOOST_TEST(parts[1] == "!tictactoe"); 161 BOOST_TEST(parts[1] == "!tictactoe");
162 BOOST_TEST(parts[2] == players.second); 162 BOOST_TEST(parts[2] == players.second);
163 BOOST_TEST(parts[3] == players.second); 163 BOOST_TEST(parts[3] == players.second);
164 BOOST_TEST(parts[4] == "tictactoe"); 164 BOOST_TEST(parts[4] == "tictactoe");
165 BOOST_TEST(parts[5] == "test"); 165 BOOST_TEST(parts[5] == "test");
166 } 166 }
167 167
168 BOOST_AUTO_TEST_CASE(invalid) 168 BOOST_AUTO_TEST_CASE(invalid)
169 { 169 {
170 // empty name (no names) 170 // empty name (no names)
171 plugin_->handle_command(irccd_, { server_, "jean", "#tictactoe", "" }); 171 plugin_->handle_command(irccd_, { server_, "jean", "#tictactoe", "" });
172 172
173 auto cmd = server_->find("message").back(); 173 auto cmd = server_->find("message").back();
174 174
175 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe"); 175 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe");
176 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "invalid=#tictactoe:!tictactoe:jean:jean:tictactoe:test"); 176 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "invalid=#tictactoe:!tictactoe:jean:jean:tictactoe:test");
177 177
178 // bot name (no names) 178 // bot name (no names)
179 plugin_->handle_command(irccd_, { server_, "jean", "#tictactoe", "irccd" }); 179 plugin_->handle_command(irccd_, { server_, "jean", "#tictactoe", "irccd" });
180 cmd = server_->find("message").back(); 180 cmd = server_->find("message").back();
181 181
182 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe"); 182 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe");
183 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "invalid=#tictactoe:!tictactoe:jean:jean:tictactoe:test"); 183 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "invalid=#tictactoe:!tictactoe:jean:jean:tictactoe:test");
184 184
185 // target is origin (no names) 185 // target is origin (no names)
186 plugin_->handle_command(irccd_, { server_, server_->get_nickname(), "#tictactoe", server_->get_nickname() }); 186 plugin_->handle_command(irccd_, { server_, server_->get_nickname(), "#tictactoe", server_->get_nickname() });
187 cmd = server_->find("message").back(); 187 cmd = server_->find("message").back();
188 188
189 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe"); 189 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe");
190 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "invalid=#tictactoe:!tictactoe:irccd:irccd:tictactoe:test"); 190 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "invalid=#tictactoe:!tictactoe:irccd:irccd:tictactoe:test");
191 191
192 // not existing (names) 192 // not existing (names)
193 plugin_->handle_command(irccd_, { server_, server_->get_nickname(), "#tictactoe", server_->get_nickname() }); 193 plugin_->handle_command(irccd_, { server_, server_->get_nickname(), "#tictactoe", server_->get_nickname() });
194 plugin_->handle_names(irccd_, { server_, "#tictactoe", { "a", "b", "c" }}); 194 plugin_->handle_names(irccd_, { server_, "#tictactoe", { "a", "b", "c" }});
195 cmd = server_->find("message").back(); 195 cmd = server_->find("message").back();
196 196
197 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe"); 197 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#tictactoe");
198 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "invalid=#tictactoe:!tictactoe:irccd:irccd:tictactoe:test"); 198 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "invalid=#tictactoe:!tictactoe:irccd:irccd:tictactoe:test");
199 } 199 }
200 200
201 BOOST_AUTO_TEST_CASE(random) 201 BOOST_AUTO_TEST_CASE(random)
202 { 202 {
203 /* 203 /*
204 * Ensure that the first player is not always the originator, start the game 204 * Ensure that the first player is not always the originator, start the game
205 * for at most 1'000'000 times to avoid forever loop. 205 * for at most 1'000'000 times to avoid forever loop.
206 */ 206 */
207 unsigned count = 0; 207 unsigned count = 0;
208 bool a = false; 208 bool a = false;
209 bool b = false; 209 bool b = false;
210 210
211 // Last player turn is the winner. 211 // Last player turn is the winner.
212 while (!a && !b && count++ < 1000000U) { 212 while (!a && !b && count++ < 1000000U) {
213 run({ "a 1", "b 1", "a 2", "b 2" }); 213 run({ "a 1", "b 1", "a 2", "b 2" });
214 214
215 const auto players = next_players(); 215 const auto players = next_players();
216 216
217 if (players.first == "a") 217 if (players.first == "a")
218 a = true; 218 a = true;
219 else 219 else
220 b = true; 220 b = true;
221 221
222 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 3" }); 222 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 3" });
223 } 223 }
224 } 224 }
225 225
226 BOOST_AUTO_TEST_CASE(disconnect) 226 BOOST_AUTO_TEST_CASE(disconnect)
227 { 227 {
228 const auto players = start(); 228 const auto players = start();
229 229
230 plugin_->handle_disconnect(irccd_, { server_ }); 230 plugin_->handle_disconnect(irccd_, { server_ });
231 server_->clear(); 231 server_->clear();
232 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 1" }); 232 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 1" });
233 233
234 BOOST_TEST(server_->empty()); 234 BOOST_TEST(server_->empty());
235 } 235 }
236 236
237 BOOST_AUTO_TEST_CASE(kick) 237 BOOST_AUTO_TEST_CASE(kick)
238 { 238 {
239 const auto players = start(); 239 const auto players = start();
240 240
241 server_->clear(); 241 server_->clear();
242 plugin_->handle_kick(irccd_, { server_, "kefka", "#tictactoe", players.first, "" }); 242 plugin_->handle_kick(irccd_, { server_, "kefka", "#tictactoe", players.first, "" });
243 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 1" }); 243 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 1" });
244 244
245 BOOST_TEST(server_->empty()); 245 BOOST_TEST(server_->empty());
246 } 246 }
247 247
248 BOOST_AUTO_TEST_CASE(part) 248 BOOST_AUTO_TEST_CASE(part)
249 { 249 {
250 const auto players = start(); 250 const auto players = start();
251 251
252 server_->clear(); 252 server_->clear();
253 plugin_->handle_part(irccd_, { server_, players.first, "#tictactoe", "" }); 253 plugin_->handle_part(irccd_, { server_, players.first, "#tictactoe", "" });
254 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 1" }); 254 plugin_->handle_message(irccd_, { server_, players.first, "#tictactoe", "a 1" });
255 255
256 BOOST_TEST(server_->empty()); 256 BOOST_TEST(server_->empty());
257 } 257 }
258 258
259 BOOST_AUTO_TEST_SUITE_END() 259 BOOST_AUTO_TEST_SUITE_END()
260 260
261 } // !namespace 261 } // !namespace