1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 42;
use Mock::CommandRegistry;
use Plugin::Vote;
my %ctx_a = (
irc => undef,
logger => undef,
who => 'user!who@example.com',
where => "#channel_a",
ided => undef,
rest => undef,
no_reenter => undef,
);
my %ctx_b = %ctx_a;
$ctx_b{where} = "#channel_b";
my $cr = new Mock::CommandRegistry;
my $register = sub { $cr->register(@_); };
Plugin::Vote->configure($register, undef, undef, undef);
# Test all expected commands registered
{
my @expected_commands = ("vote on", "vote yes", "vote no", "vote end");
for my $cmd (@expected_commands) {
my $is_registered = $cr->is_registered_to_owner("Plugin::Vote", $cmd);
ok($is_registered, "registered command $cmd");
}
}
# Test ending a vote when none is in progress produces error message
{
my %ctx = %ctx_a;
my $r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
ok($r =~ m/no vote.*progress/i, "no vote error message");
}
sub ok_vote_match {
my ($result, $expect_ayes, $expect_noes) = @_;
if ($result =~ m/The votes are in.*Ayes: (\d+).*Noes: (\d+)/) {
is($1, $expect_ayes, "ayes match");
is($2, $expect_noes, "ayes match");
} else {
die("'$result' can't be taken apart");
}
}
sub vote_started {
my ($result) = @_;
$result =~ m/^Call to vote.*/;
}
# - Start a vote, end a vote, 0,0
{
my %ctx = %ctx_a;
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "vote started");
$ctx{rest} = "";
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 0, 0);
}
# - Start a vote, start another, error message, end a vote, 0,0
{
my %ctx = %ctx_a;
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "first vote started");
$ctx{rest} = "topic2";
$r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(!vote_started($r), "second vote not started");
$ctx{rest} = "";
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 0, 0);
}
# - Start a vote, vote yes, end a vote, 1,0
{
my %ctx = %ctx_a;
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "vote started");
$ctx{rest} = "";
$r = $cr->run_owned("Plugin::Vote", "vote yes", %ctx);
note($r);
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 1, 0);
}
# - Start a vote, vote no, end a vote, 0,1
{
my %ctx = %ctx_a;
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "vote started");
$ctx{rest} = "";
$r = $cr->run_owned("Plugin::Vote", "vote no", %ctx);
note($r);
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 0, 1);
}
# - Start a vote, vote yes twice, error message, end the vote, 1,0
{
my %ctx = %ctx_a;
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "vote started");
$ctx{rest} = "";
$r = $cr->run_owned("Plugin::Vote", "vote yes", %ctx);
note($r);
$r = $cr->run_owned("Plugin::Vote", "vote yes", %ctx);
note($r);
ok($r =~ m/already voted/, "user told about double vote");
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 1, 0);
}
# - Start a vote, vote no twice, error message, end the vote, 0,1
{
my %ctx = %ctx_a;
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "vote started");
$ctx{rest} = "";
$r = $cr->run_owned("Plugin::Vote", "vote no", %ctx);
note($r);
$r = $cr->run_owned("Plugin::Vote", "vote no", %ctx);
note($r);
ok($r =~ m/already voted/, "user told about double vote");
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 0, 1);
}
# - Start a vote, vote no then yes, error message, end the vote, 0,1
{
my %ctx = %ctx_a;
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "vote started");
$ctx{rest} = "";
$r = $cr->run_owned("Plugin::Vote", "vote no", %ctx);
note($r);
$r = $cr->run_owned("Plugin::Vote", "vote yes", %ctx);
note($r);
ok($r =~ m/already voted/, "user told about double vote");
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 0, 1);
}
# - Start a vote, two people vote yes, one no, end the vote, 2,1
{
my %ctx = %ctx_a;
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "vote started");
$ctx{rest} = "";
$r = $cr->run_owned("Plugin::Vote", "vote yes", %ctx);
note($r);
my $orig_who = $ctx{who};
$ctx{who} = "a$orig_who";
$r = $cr->run_owned("Plugin::Vote", "vote yes", %ctx);
note($r);
$ctx{who} = "b$orig_who";
$r = $cr->run_owned("Plugin::Vote", "vote no", %ctx);
note($r);
$ctx{who} = $orig_who;
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 2, 1);
}
# Test cases cross-chan
# - Start a vote #a, vote on #b, error message, end a vote, 0,0
{
my %ctx = %ctx_a;
# Start a vote on channel A
$ctx{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx);
note($r);
ok(vote_started($r), "vote started");
# Vote yes on channel B - should fail since votes are channel-bound
$r = $cr->run_owned("Plugin::Vote", "vote yes", %ctx_b);
note($r);
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx);
note($r);
ok_vote_match($r, 0, 0);
}
# - Start a vote #a, start identical vote #b, vote #a yes, vote #b no, end #a vote, 1,0, end #b vote, 0,1
{
my %ctx_m_a = %ctx_a;
my %ctx_m_b = %ctx_b;
# Start a vote on channel A
$ctx_m_a{rest} = "topic1";
my $r = $cr->run_owned("Plugin::Vote", "vote on", %ctx_m_a);
note($r);
ok(vote_started($r), "vote A started");
# Start a vote on channel B
$ctx_m_b{rest} = "topic1";
$r = $cr->run_owned("Plugin::Vote", "vote on", %ctx_m_b);
note($r);
ok(vote_started($r), "vote B started");
# Vote yes on channel A
$r = $cr->run_owned("Plugin::Vote", "vote yes", %ctx_m_a);
note($r);
# Vote no on channel B
$r = $cr->run_owned("Plugin::Vote", "vote no", %ctx_m_b);
note($r);
# Gather votes from A, expect 1 for, 0 against
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx_m_a);
note($r);
ok_vote_match($r, 1, 0);
# Gather votes from B, expect 0 for, 1 against
$r = $cr->run_owned("Plugin::Vote", "vote end", %ctx_m_b);
note($r);
ok_vote_match($r, 0, 1);
}
|