Poll: Make answers case-insensitive

This commit is contained in:
Valentin Lorentz 2022-09-18 19:25:48 +02:00
parent 200acdfa93
commit bc3a441888
2 changed files with 21 additions and 4 deletions

View File

@ -131,7 +131,7 @@ class Poll_(callbacks.Plugin):
poll_id = max(self._polls[(irc.network, channel)], default=0) + 1 poll_id = max(self._polls[(irc.network, channel)], default=0) + 1
answers = [(answer.split()[0], answer) for answer in answers] answers = [(answer.split()[0].casefold(), answer) for answer in answers]
answer_id_counts = collections.Counter( answer_id_counts = collections.Counter(
id_ for (id_, _) in answers id_ for (id_, _) in answers
@ -194,6 +194,8 @@ class Poll_(callbacks.Plugin):
if msg.nick in poll.votes: if msg.nick in poll.votes:
irc.error(_("You already voted on this poll."), Raise=True) irc.error(_("You already voted on this poll."), Raise=True)
answer_id = answer_id.casefold()
if answer_id not in poll.answers: if answer_id not in poll.answers:
irc.error( irc.error(
format( format(
@ -221,7 +223,7 @@ class Poll_(callbacks.Plugin):
counts.update({answer_id: 0 for answer_id in poll.answers}) counts.update({answer_id: 0 for answer_id in poll.answers})
results = [ results = [
format(_("%n for %s"), (v, _("vote")), k) format(_("%n for %s"), (v, _("vote")), poll.answers[k].split()[0])
for (k, v) in counts.most_common() for (k, v) in counts.most_common()
] ]

View File

@ -131,12 +131,27 @@ class PollTestCase(ChannelPluginTestCase):
def testDuplicateId(self): def testDuplicateId(self):
self.assertResponse( self.assertResponse(
'poll add "Is this a test?" "Yes" "Yes" "Maybe"', 'poll add "Is this a test?" "Yes" "Yes" "Maybe"',
"Error: Duplicate answer identifier(s): Yes", "Error: Duplicate answer identifier(s): yes",
) )
self.assertResponse( self.assertResponse(
'poll add "Is this a test?" "Yes totally" "Yes and no" "Maybe"', 'poll add "Is this a test?" "Yes totally" "Yes and no" "Maybe"',
"Error: Duplicate answer identifier(s): Yes", "Error: Duplicate answer identifier(s): yes",
)
def testCaseInsensitive(self):
self.assertResponse(
'poll add "Is this a test?" "Yeß" "No" "Maybe"',
"The operation succeeded. Poll # 1 created.",
)
self.assertNotError("vote 1 Yeß", frm="voter1!foo@bar")
self.assertNotError("vote 1 yESS", frm="voter2!foo@bar")
self.assertNotError("vote 1 no", frm="voter3!foo@bar")
self.assertResponse(
"results 1",
"2 votes for Yeß, 1 vote for No, and 0 votes for Maybe",
) )
def testList(self): def testList(self):