Mercurial > irccd
comparison tests/test-plugin-tictactoe.c @ 1011:a35537c50f09
irccd: hide conn implementation
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 17 Feb 2021 20:05:00 +0100 |
parents | 3ea3361f0fc7 |
children | 8f8ce47aba8a |
comparison
equal
deleted
inserted
replaced
1010:d1ec9b99b580 | 1011:a35537c50f09 |
---|---|
20 | 20 |
21 #define GREATEST_USE_ABBREVS 0 | 21 #define GREATEST_USE_ABBREVS 0 |
22 #include <greatest.h> | 22 #include <greatest.h> |
23 | 23 |
24 #include <irccd/compat.h> | 24 #include <irccd/compat.h> |
25 #include <irccd/conn.h> | |
25 #include <irccd/irccd.h> | 26 #include <irccd/irccd.h> |
26 #include <irccd/js-plugin.h> | 27 #include <irccd/js-plugin.h> |
27 #include <irccd/log.h> | 28 #include <irccd/log.h> |
28 #include <irccd/plugin.h> | 29 #include <irccd/plugin.h> |
29 #include <irccd/server.h> | 30 #include <irccd/server.h> |
30 #include <irccd/util.h> | 31 #include <irccd/util.h> |
31 | 32 |
32 #define CALL(t, m) do { \ | 33 #define CALL(t, m) do { \ |
33 memset(server->conn.out, 0, sizeof (server->conn.out)); \ | 34 memset(server->conn->out, 0, sizeof (server->conn->out)); \ |
34 irc_plugin_handle(plugin, &(const struct irc_event) { \ | 35 irc_plugin_handle(plugin, &(const struct irc_event) { \ |
35 .type = t, \ | 36 .type = t, \ |
36 .server = server, \ | 37 .server = server, \ |
37 .message = { \ | 38 .message = { \ |
38 .origin = "jean!jean@localhost", \ | 39 .origin = "jean!jean@localhost", \ |
39 .channel = "#hangman", \ | 40 .channel = "#hangman", \ |
40 .message = m \ | 41 .message = m \ |
41 } \ | 42 } \ |
42 }); \ | 43 }); \ |
43 } while (0) | 44 } while (0) |
44 | 45 |
45 #define CALL_EX(t, o, c, m) do { \ | 46 #define CALL_EX(t, o, c, m) do { \ |
46 memset(server->conn.out, 0, sizeof (server->conn.out)); \ | 47 memset(server->conn->out, 0, sizeof (server->conn->out)); \ |
47 irc_plugin_handle(plugin, &(const struct irc_event) { \ | 48 irc_plugin_handle(plugin, &(const struct irc_event) { \ |
48 .type = t, \ | 49 .type = t, \ |
49 .server = server, \ | 50 .server = server, \ |
50 .message = { \ | 51 .message = { \ |
51 .origin = o, \ | 52 .origin = o, \ |
52 .channel = c, \ | 53 .channel = c, \ |
53 .message = m \ | 54 .message = m \ |
54 } \ | 55 } \ |
55 }); \ | 56 }); \ |
56 } while (0) | 57 } while (0) |
57 | 58 |
58 static struct irc_server *server; | 59 static struct irc_server *server; |
59 static struct irc_plugin *plugin; | 60 static struct irc_plugin *plugin; |
60 | 61 |
102 { | 103 { |
103 const char *lines[5] = {0}; | 104 const char *lines[5] = {0}; |
104 char player = 0, *buf; | 105 char player = 0, *buf; |
105 | 106 |
106 /* We need to skip 4 lines.*/ | 107 /* We need to skip 4 lines.*/ |
107 buf = irc_util_strdup(server->conn.out); | 108 buf = irc_util_strdup(server->conn->out); |
108 irc_util_split(buf, lines, 5, '\n'); | 109 irc_util_split(buf, lines, 5, '\n'); |
109 | 110 |
110 if (!lines[4] || sscanf(lines[4], "PRIVMSG #tictactoe :turn=#tictactoe:!tictactoe:%c:tictactoe:test\r\n", &player) != 1) | 111 if (!lines[4] || sscanf(lines[4], "PRIVMSG #tictactoe :turn=#tictactoe:!tictactoe:%c:tictactoe:test\r\n", &player) != 1) |
111 errx(1, "could not determine player"); | 112 errx(1, "could not determine player"); |
112 | 113 |
135 play("b1"); | 136 play("b1"); |
136 play("a 2"); | 137 play("a 2"); |
137 play("b2"); | 138 play("b2"); |
138 play("a3"); | 139 play("a3"); |
139 | 140 |
140 GREATEST_ASSERT_EQ(5U, irc_util_split(server->conn.out, lines, 5, '\n')); | 141 GREATEST_ASSERT_EQ(5U, irc_util_split(server->conn->out, lines, 5, '\n')); |
141 GREATEST_ASSERT_EQ(0, sscanf(lines[0], "PRIVMSG #tictactoe : a b c\r")); | 142 GREATEST_ASSERT_EQ(0, sscanf(lines[0], "PRIVMSG #tictactoe : a b c\r")); |
142 GREATEST_ASSERT_EQ(2, sscanf(lines[1], "PRIVMSG #tictactoe :1 %c %c .\r", &k1, &k2)); | 143 GREATEST_ASSERT_EQ(2, sscanf(lines[1], "PRIVMSG #tictactoe :1 %c %c .\r", &k1, &k2)); |
143 GREATEST_ASSERT_EQ(2, sscanf(lines[2], "PRIVMSG #tictactoe :2 %c %c .\r", &k1, &k2)); | 144 GREATEST_ASSERT_EQ(2, sscanf(lines[2], "PRIVMSG #tictactoe :2 %c %c .\r", &k1, &k2)); |
144 GREATEST_ASSERT_EQ(1, sscanf(lines[3], "PRIVMSG #tictactoe :3 %c . .\r", &k1)); | 145 GREATEST_ASSERT_EQ(1, sscanf(lines[3], "PRIVMSG #tictactoe :3 %c . .\r", &k1)); |
145 GREATEST_ASSERT_EQ(1, sscanf(lines[4], "PRIVMSG #tictactoe :win=#tictactoe:!tictactoe:%c:tictactoe:test\r\n", &k1)); | 146 GREATEST_ASSERT_EQ(1, sscanf(lines[4], "PRIVMSG #tictactoe :win=#tictactoe:!tictactoe:%c:tictactoe:test\r\n", &k1)); |
169 play("a 2"); | 170 play("a 2"); |
170 play("a 3"); | 171 play("a 3"); |
171 play("a 1"); | 172 play("a 1"); |
172 play("b 1"); | 173 play("b 1"); |
173 | 174 |
174 GREATEST_ASSERT_EQ(5U, irc_util_split(server->conn.out, lines, 5, '\n')); | 175 GREATEST_ASSERT_EQ(5U, irc_util_split(server->conn->out, lines, 5, '\n')); |
175 GREATEST_ASSERT_EQ(0, sscanf(lines[0], "PRIVMSG #tictactoe : a b c\r")); | 176 GREATEST_ASSERT_EQ(0, sscanf(lines[0], "PRIVMSG #tictactoe : a b c\r")); |
176 GREATEST_ASSERT_EQ(3, sscanf(lines[1], "PRIVMSG #tictactoe :1 %c %c %c\r", &k1, &k2, &k3)); | 177 GREATEST_ASSERT_EQ(3, sscanf(lines[1], "PRIVMSG #tictactoe :1 %c %c %c\r", &k1, &k2, &k3)); |
177 GREATEST_ASSERT_EQ(3, sscanf(lines[2], "PRIVMSG #tictactoe :2 %c %c %c\r", &k1, &k2, &k3)); | 178 GREATEST_ASSERT_EQ(3, sscanf(lines[2], "PRIVMSG #tictactoe :2 %c %c %c\r", &k1, &k2, &k3)); |
178 GREATEST_ASSERT_EQ(3, sscanf(lines[3], "PRIVMSG #tictactoe :3 %c %c %c\r", &k1, &k2, &k3)); | 179 GREATEST_ASSERT_EQ(3, sscanf(lines[3], "PRIVMSG #tictactoe :3 %c %c %c\r", &k1, &k2, &k3)); |
179 GREATEST_ASSERT_EQ(1, sscanf(lines[4], "PRIVMSG #tictactoe :draw=#tictactoe:!tictactoe:%c:tictactoe:test\r\n", &k1)); | 180 GREATEST_ASSERT_EQ(1, sscanf(lines[4], "PRIVMSG #tictactoe :draw=#tictactoe:!tictactoe:%c:tictactoe:test\r\n", &k1)); |
189 CALL_EX(IRC_EVENT_COMMAND, "a", "#tictactoe", "b"); | 190 CALL_EX(IRC_EVENT_COMMAND, "a", "#tictactoe", "b"); |
190 | 191 |
191 play("a 1"); | 192 play("a 1"); |
192 play("a 1"); | 193 play("a 1"); |
193 | 194 |
194 GREATEST_ASSERT_EQ(2, sscanf(server->conn.out, "PRIVMSG #tictactoe :used=#tictactoe:!tictactoe:%c:%c:tictactoe:test\r\n", &k1, &k2)); | 195 GREATEST_ASSERT_EQ(2, sscanf(server->conn->out, "PRIVMSG #tictactoe :used=#tictactoe:!tictactoe:%c:%c:tictactoe:test\r\n", &k1, &k2)); |
195 | 196 |
196 GREATEST_PASS(); | 197 GREATEST_PASS(); |
197 } | 198 } |
198 | 199 |
199 GREATEST_TEST | 200 GREATEST_TEST |
201 { | 202 { |
202 char k1, k2; | 203 char k1, k2; |
203 | 204 |
204 /* Player select itself. */ | 205 /* Player select itself. */ |
205 CALL_EX(IRC_EVENT_COMMAND, "a", "#tictactoe", "a"); | 206 CALL_EX(IRC_EVENT_COMMAND, "a", "#tictactoe", "a"); |
206 GREATEST_ASSERT_EQ(2, sscanf(server->conn.out, "PRIVMSG #tictactoe :invalid=#tictactoe:!tictactoe:%c:%c:tictactoe:test\r\n", &k1, &k2)); | 207 GREATEST_ASSERT_EQ(2, sscanf(server->conn->out, "PRIVMSG #tictactoe :invalid=#tictactoe:!tictactoe:%c:%c:tictactoe:test\r\n", &k1, &k2)); |
207 | 208 |
208 /* Player select the bot. */ | 209 /* Player select the bot. */ |
209 CALL_EX(IRC_EVENT_COMMAND, "a", "#tictactoe", "t"); | 210 CALL_EX(IRC_EVENT_COMMAND, "a", "#tictactoe", "t"); |
210 GREATEST_ASSERT_EQ(2, sscanf(server->conn.out, "PRIVMSG #tictactoe :invalid=#tictactoe:!tictactoe:%c:%c:tictactoe:test\r\n", &k1, &k2)); | 211 GREATEST_ASSERT_EQ(2, sscanf(server->conn->out, "PRIVMSG #tictactoe :invalid=#tictactoe:!tictactoe:%c:%c:tictactoe:test\r\n", &k1, &k2)); |
211 | 212 |
212 /* Someone not on the channel. */ | 213 /* Someone not on the channel. */ |
213 CALL_EX(IRC_EVENT_COMMAND, "a", "#tictactoe", "jean"); | 214 CALL_EX(IRC_EVENT_COMMAND, "a", "#tictactoe", "jean"); |
214 GREATEST_ASSERT_EQ(2, sscanf(server->conn.out, "PRIVMSG #tictactoe :invalid=#tictactoe:!tictactoe:%c:%c:tictactoe:test\r\n", &k1, &k2)); | 215 GREATEST_ASSERT_EQ(2, sscanf(server->conn->out, "PRIVMSG #tictactoe :invalid=#tictactoe:!tictactoe:%c:%c:tictactoe:test\r\n", &k1, &k2)); |
215 | 216 |
216 GREATEST_PASS(); | 217 GREATEST_PASS(); |
217 } | 218 } |
218 | 219 |
219 GREATEST_TEST | 220 GREATEST_TEST |
258 .type = IRC_EVENT_DISCONNECT, | 259 .type = IRC_EVENT_DISCONNECT, |
259 .server = server | 260 .server = server |
260 }); | 261 }); |
261 | 262 |
262 play("a 1"); | 263 play("a 1"); |
263 GREATEST_ASSERT_STR_EQ("", server->conn.out); | 264 GREATEST_ASSERT_STR_EQ("", server->conn->out); |
264 | 265 |
265 GREATEST_PASS(); | 266 GREATEST_PASS(); |
266 } | 267 } |
267 | 268 |
268 GREATEST_TEST | 269 GREATEST_TEST |
280 .reason = "No reason, I do what I want." | 281 .reason = "No reason, I do what I want." |
281 } | 282 } |
282 }); | 283 }); |
283 | 284 |
284 play("a 1"); | 285 play("a 1"); |
285 GREATEST_ASSERT_STR_EQ("", server->conn.out); | 286 GREATEST_ASSERT_STR_EQ("", server->conn->out); |
286 | 287 |
287 GREATEST_PASS(); | 288 GREATEST_PASS(); |
288 } | 289 } |
289 | 290 |
290 GREATEST_TEST | 291 GREATEST_TEST |
301 .reason = "I'm too bad at this game." | 302 .reason = "I'm too bad at this game." |
302 } | 303 } |
303 }); | 304 }); |
304 | 305 |
305 play("a 1"); | 306 play("a 1"); |
306 GREATEST_ASSERT_STR_EQ("", server->conn.out); | 307 GREATEST_ASSERT_STR_EQ("", server->conn->out); |
307 | 308 |
308 GREATEST_PASS(); | 309 GREATEST_PASS(); |
309 } | 310 } |
310 | 311 |
311 GREATEST_SUITE(suite_basics) | 312 GREATEST_SUITE(suite_basics) |