From bc3a4418885f1711c2533ce61408cf97fc2f8d9f Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sun, 18 Sep 2022 19:25:48 +0200 Subject: [PATCH] Poll: Make answers case-insensitive --- plugins/Poll/plugin.py | 6 ++++-- plugins/Poll/test.py | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/plugins/Poll/plugin.py b/plugins/Poll/plugin.py index 680a884d2..c21be4359 100644 --- a/plugins/Poll/plugin.py +++ b/plugins/Poll/plugin.py @@ -131,7 +131,7 @@ class Poll_(callbacks.Plugin): 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( id_ for (id_, _) in answers @@ -194,6 +194,8 @@ class Poll_(callbacks.Plugin): if msg.nick in poll.votes: irc.error(_("You already voted on this poll."), Raise=True) + answer_id = answer_id.casefold() + if answer_id not in poll.answers: irc.error( format( @@ -221,7 +223,7 @@ class Poll_(callbacks.Plugin): counts.update({answer_id: 0 for answer_id in poll.answers}) 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() ] diff --git a/plugins/Poll/test.py b/plugins/Poll/test.py index b891fd862..988157d70 100644 --- a/plugins/Poll/test.py +++ b/plugins/Poll/test.py @@ -131,12 +131,27 @@ class PollTestCase(ChannelPluginTestCase): def testDuplicateId(self): self.assertResponse( 'poll add "Is this a test?" "Yes" "Yes" "Maybe"', - "Error: Duplicate answer identifier(s): Yes", + "Error: Duplicate answer identifier(s): yes", ) self.assertResponse( '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):