From 461c7fff23384187add4e5fa0eb37f5b0721098f Mon Sep 17 00:00:00 2001 From: Ionite Date: Tue, 25 Oct 2022 01:01:56 -0400 Subject: Made arg fix compatible with tests --- tests/bot/exts/info/test_information.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/bot/exts/info/test_information.py b/tests/bot/exts/info/test_information.py index 9f5143c01..267346c58 100644 --- a/tests/bot/exts/info/test_information.py +++ b/tests/bot/exts/info/test_information.py @@ -614,7 +614,7 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): str(rule_number) for rule_number in extracted_rule_numbers if rule_number < 1 or rule_number > len(self.full_rules)) - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, raw_user_input) self.assertEqual( self.ctx.send.call_args, @@ -631,7 +631,7 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): for raw_user_input, expected_matched_rule_numbers in test_cases: with self.subTest(identifier=raw_user_input): - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, raw_user_input) self.assertEqual(expected_matched_rule_numbers, final_rule_numbers) async def test_return_default_rules_when_no_input_or_no_match_are_found(self): @@ -643,7 +643,7 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): for raw_user_input, expected_matched_rule_numbers in test_cases: with self.subTest(identifier=raw_user_input): - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, raw_user_input) embed = self.ctx.send.call_args.kwargs['embed'] self.assertEqual(information.DEFAULT_RULES_DESCRIPTION, embed.description) self.assertEqual(expected_matched_rule_numbers, final_rule_numbers) -- cgit v1.2.3 From 54e3fcff5411b9026d4a31759842aae25611859e Mon Sep 17 00:00:00 2001 From: Ionite Date: Tue, 25 Oct 2022 09:47:12 -0400 Subject: Added new star expression fix --- bot/exts/info/information.py | 28 +++++++++++----------------- tests/bot/exts/info/test_information.py | 6 +++--- 2 files changed, 14 insertions(+), 20 deletions(-) (limited to 'tests') diff --git a/bot/exts/info/information.py b/bot/exts/info/information.py index 80e7a5b6c..73afa3309 100644 --- a/bot/exts/info/information.py +++ b/bot/exts/info/information.py @@ -524,22 +524,15 @@ class Information(Cog): await self.send_raw_content(ctx, message, json=True) @command(aliases=("rule",)) - async def rules(self, ctx: Context, args: Optional[str]) -> Optional[Set[int]]: + async def rules(self, ctx: Context, *, args: Optional[str]) -> Optional[Set[int]]: """ Provides a link to all rules or, if specified, displays specific rule(s). It accepts either rule numbers or particular keywords that map to a particular rule. Rule numbers and keywords can be sent in any order. """ - # Temporary fix for discord.py greedy string quote conversion bug - if not args: - args = () - elif isinstance(args, str): - msg = ctx.message.content - # Remove the command - if len(msg := msg.split()) > 1: - msg.pop(0) - args = tuple(msg) + # Note: *args cannot be used due to a discord.py bug + # causing infinite loops during greedy string parsing. rules_embed = Embed(title="Rules", color=Colour.og_blurple(), url="https://www.pythondiscord.com/pages/rules") keywords, rule_numbers = [], [] @@ -551,13 +544,14 @@ class Information(Cog): for rule_keyword in rule_keywords: keyword_to_rule_number[rule_keyword] = rule_number - for word in args: - try: - rule_numbers.append(int(word)) - except ValueError: - if (kw := word.lower()) not in keyword_to_rule_number: - break - keywords.append(kw) + if args: + for word in args.split(): + try: + rule_numbers.append(int(word)) + except ValueError: + if (kw := word.lower()) not in keyword_to_rule_number: + break + keywords.append(kw) if not rule_numbers and not keywords: # Neither rules nor keywords were submitted. Return the default description. diff --git a/tests/bot/exts/info/test_information.py b/tests/bot/exts/info/test_information.py index 267346c58..9f5143c01 100644 --- a/tests/bot/exts/info/test_information.py +++ b/tests/bot/exts/info/test_information.py @@ -614,7 +614,7 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): str(rule_number) for rule_number in extracted_rule_numbers if rule_number < 1 or rule_number > len(self.full_rules)) - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) self.assertEqual( self.ctx.send.call_args, @@ -631,7 +631,7 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): for raw_user_input, expected_matched_rule_numbers in test_cases: with self.subTest(identifier=raw_user_input): - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) self.assertEqual(expected_matched_rule_numbers, final_rule_numbers) async def test_return_default_rules_when_no_input_or_no_match_are_found(self): @@ -643,7 +643,7 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): for raw_user_input, expected_matched_rule_numbers in test_cases: with self.subTest(identifier=raw_user_input): - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) embed = self.ctx.send.call_args.kwargs['embed'] self.assertEqual(information.DEFAULT_RULES_DESCRIPTION, embed.description) self.assertEqual(expected_matched_rule_numbers, final_rule_numbers) -- cgit v1.2.3 From 71fdd6cfd4e5353be61a46a2ab629835238122fc Mon Sep 17 00:00:00 2001 From: Ionite Date: Tue, 25 Oct 2022 09:53:47 -0400 Subject: Update tests for new single string format --- tests/bot/exts/info/test_information.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/bot/exts/info/test_information.py b/tests/bot/exts/info/test_information.py index 9f5143c01..e016ae8ed 100644 --- a/tests/bot/exts/info/test_information.py +++ b/tests/bot/exts/info/test_information.py @@ -603,9 +603,9 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): async def test_return_none_if_one_rule_number_is_invalid(self): test_cases = [ - (('1', '6', '7', '8'), (6, 7, 8)), - (('10', "first"), (10, )), - (("first", 10), (10, )) + ("1 6 7 8", (6, 7, 8)), + ("10 first", (10,)), + ("first 10", (10,)) ] for raw_user_input, extracted_rule_numbers in test_cases: @@ -614,7 +614,7 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): str(rule_number) for rule_number in extracted_rule_numbers if rule_number < 1 or rule_number > len(self.full_rules)) - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, args=raw_user_input) self.assertEqual( self.ctx.send.call_args, @@ -624,26 +624,26 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): async def test_return_correct_rule_numbers(self): test_cases = [ - (("1", "2", "first"), {1, 2}), - (("1", "hello", "2", "second"), {1}), - (("second", "third", "unknown", "999"), {2, 3}) + ("1 2 first", {1, 2}), + ("1 hello 2 second", {1}), + ("second third unknown 999", {2, 3}) ] for raw_user_input, expected_matched_rule_numbers in test_cases: with self.subTest(identifier=raw_user_input): - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, args=raw_user_input) self.assertEqual(expected_matched_rule_numbers, final_rule_numbers) async def test_return_default_rules_when_no_input_or_no_match_are_found(self): test_cases = [ - ((), None), - (("hello", "2", "second"), None), - (("hello", "999"), None), + ("", None), + ("hello 2 second", None), + ("hello 999", None), ] for raw_user_input, expected_matched_rule_numbers in test_cases: with self.subTest(identifier=raw_user_input): - final_rule_numbers = await self.cog.rules(self.cog, self.ctx, *raw_user_input) + final_rule_numbers = await self.cog.rules(self.cog, self.ctx, args=raw_user_input) embed = self.ctx.send.call_args.kwargs['embed'] self.assertEqual(information.DEFAULT_RULES_DESCRIPTION, embed.description) self.assertEqual(expected_matched_rule_numbers, final_rule_numbers) -- cgit v1.2.3 From 8023741e77394877cafda526fc2cbca50aaa77db Mon Sep 17 00:00:00 2001 From: Ionite Date: Tue, 25 Oct 2022 10:56:13 -0400 Subject: Update tests to work with rules arg fix --- tests/bot/exts/info/test_information.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/bot/exts/info/test_information.py b/tests/bot/exts/info/test_information.py index e016ae8ed..0444ca465 100644 --- a/tests/bot/exts/info/test_information.py +++ b/tests/bot/exts/info/test_information.py @@ -625,8 +625,8 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): test_cases = [ ("1 2 first", {1, 2}), - ("1 hello 2 second", {1}), - ("second third unknown 999", {2, 3}) + ("1 hello 2 second", {1, 2}), + ("second third unknown 999", None) ] for raw_user_input, expected_matched_rule_numbers in test_cases: @@ -637,8 +637,7 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): async def test_return_default_rules_when_no_input_or_no_match_are_found(self): test_cases = [ ("", None), - ("hello 2 second", None), - ("hello 999", None), + ("hello abc", None), ] for raw_user_input, expected_matched_rule_numbers in test_cases: -- cgit v1.2.3 From 8f68607654e92e1ff1c026d37f339a983ebe8840 Mon Sep 17 00:00:00 2001 From: Ionite Date: Tue, 25 Oct 2022 11:19:05 -0400 Subject: Reverse changes to invalid arg break --- bot/exts/info/information.py | 6 ++++-- tests/bot/exts/info/test_information.py | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/bot/exts/info/information.py b/bot/exts/info/information.py index 44cd55362..2eb9382e3 100644 --- a/bot/exts/info/information.py +++ b/bot/exts/info/information.py @@ -546,8 +546,10 @@ class Information(Cog): try: rule_numbers.append(int(word)) except ValueError: - if (kw := word.lower()) in keyword_to_rule_number: - keywords.append(kw) + # Stop on first invalid keyword/index to allow for normal messaging after + if (kw := word.lower()) not in keyword_to_rule_number: + break + keywords.append(kw) if not rule_numbers and not keywords: # Neither rules nor keywords were submitted. Return the default description. diff --git a/tests/bot/exts/info/test_information.py b/tests/bot/exts/info/test_information.py index 0444ca465..65595e959 100644 --- a/tests/bot/exts/info/test_information.py +++ b/tests/bot/exts/info/test_information.py @@ -625,8 +625,8 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): test_cases = [ ("1 2 first", {1, 2}), - ("1 hello 2 second", {1, 2}), - ("second third unknown 999", None) + ("1 hello 2 second", {1}), + ("second third unknown 999", {2, 3}), ] for raw_user_input, expected_matched_rule_numbers in test_cases: @@ -637,7 +637,8 @@ class RuleCommandTests(unittest.IsolatedAsyncioTestCase): async def test_return_default_rules_when_no_input_or_no_match_are_found(self): test_cases = [ ("", None), - ("hello abc", None), + ("hello 2 second", None), + ("hello 999", None), ] for raw_user_input, expected_matched_rule_numbers in test_cases: -- cgit v1.2.3