From 28c52c2818148568c833fd51c2f85d6c747f63b5 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 30 Jul 2022 21:25:47 +0200 Subject: [PATCH] Poll: Add @poll list command --- plugins/Poll/plugin.py | 23 +++++++++++++++++++++- plugins/Poll/test.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/plugins/Poll/plugin.py b/plugins/Poll/plugin.py index f038af757..e1b83747f 100644 --- a/plugins/Poll/plugin.py +++ b/plugins/Poll/plugin.py @@ -218,11 +218,32 @@ 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")), k) for (k, v) in counts.most_common() ] irc.replies(results) + @wrap(["channel"]) + def list(self, irc, msg, args, channel): + """[] + + Lists open polls in the .""" + results = [ + format( + _("%i: %s (%n)"), + poll_id, + poll.question, + (len(poll.votes), _("vote")), + ) + for (poll_id, poll) in self._polls[(irc.network, channel)].items() + if poll.open + ] + + if results: + irc.replies(results) + else: + irc.reply(_("There are no open polls.")) + Class = Poll_ diff --git a/plugins/Poll/test.py b/plugins/Poll/test.py index a5dd12ad5..73c05f3fa 100644 --- a/plugins/Poll/test.py +++ b/plugins/Poll/test.py @@ -49,6 +49,17 @@ class PollTestCase(ChannelPluginTestCase): "2 votes for No, 1 vote for Yes, and 0 votes for Maybe", ) + def testNoResults(self): + self.assertResponse( + 'poll add "Is this a test?" "Yes" "No" "Maybe"', + "The operation succeeded. Poll # 1 created.", + ) + + self.assertResponse( + "results 1", + "0 votes for Yes, 0 votes for No, and 0 votes for Maybe", + ) + def testDoubleVoting(self): self.assertResponse( 'poll add "Is this a test?" "Yes" "No" "Maybe"', @@ -122,3 +133,36 @@ class PollTestCase(ChannelPluginTestCase): 'poll add "Is this a test?" "Yes totally" "Yes and no" "Maybe"', "Error: Duplicate answer identifier(s): Yes", ) + + def testList(self): + self.assertResponse("poll list", "There are no open polls.") + + self.assertResponse( + 'poll add "Is this a test?" "Yes" "No" "Maybe"', + "The operation succeeded. Poll # 1 created.", + ) + self.assertResponse("poll list", "1: Is this a test? (0 votes)") + + self.assertNotError("vote 1 Yes", frm="voter1!foo@bar") + self.assertResponse("poll list", "1: Is this a test? (1 vote)") + + self.assertNotError("vote 1 No", frm="voter2!foo@bar") + self.assertResponse("poll list", "1: Is this a test? (2 votes)") + + self.assertResponse( + 'poll add "Is this another test?" "Yes" "No" "Maybe"', + "The operation succeeded. Poll # 2 created.", + ) + self.assertResponse( + "poll list", + "1: Is this a test? (2 votes) and 2: Is this another test? (0 votes)", + ) + + self.assertNotError("poll close 1") + self.assertResponse( + "poll list", + "2: Is this another test? (0 votes)", + ) + + self.assertNotError("poll close 2") + self.assertResponse("poll list", "There are no open polls.")