Skip to content

Commit 648688c

Browse files
gh-106368: Harden Argument Clinic parser tests (#106384)
1 parent 2e92edb commit 648688c

File tree

1 file changed

+68
-42
lines changed

1 file changed

+68
-42
lines changed

Lib/test/test_clinic.py

+68-42
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def test_disallowed_grouping__two_top_groups_on_left(self):
643643
self.assertEqual(out, expected_msg)
644644

645645
def test_disallowed_grouping__two_top_groups_on_right(self):
646-
self.parse_function_should_fail("""
646+
out = self.parse_function_should_fail("""
647647
module foo
648648
foo.two_top_groups_on_right
649649
param: int
@@ -654,9 +654,14 @@ def test_disallowed_grouping__two_top_groups_on_right(self):
654654
group2 : int
655655
]
656656
""")
657+
msg = (
658+
"Function two_top_groups_on_right has an unsupported group "
659+
"configuration. (Unexpected state 6.b)"
660+
)
661+
self.assertIn(msg, out)
657662

658663
def test_disallowed_grouping__parameter_after_group_on_right(self):
659-
self.parse_function_should_fail("""
664+
out = self.parse_function_should_fail("""
660665
module foo
661666
foo.parameter_after_group_on_right
662667
param: int
@@ -667,9 +672,14 @@ def test_disallowed_grouping__parameter_after_group_on_right(self):
667672
group2 : int
668673
]
669674
""")
675+
msg = (
676+
"Function parameter_after_group_on_right has an unsupported group "
677+
"configuration. (Unexpected state 6.a)"
678+
)
679+
self.assertIn(msg, out)
670680

671681
def test_disallowed_grouping__group_after_parameter_on_left(self):
672-
self.parse_function_should_fail("""
682+
out = self.parse_function_should_fail("""
673683
module foo
674684
foo.group_after_parameter_on_left
675685
[
@@ -680,9 +690,14 @@ def test_disallowed_grouping__group_after_parameter_on_left(self):
680690
]
681691
param: int
682692
""")
693+
msg = (
694+
"Function group_after_parameter_on_left has an unsupported group "
695+
"configuration. (Unexpected state 2.b)"
696+
)
697+
self.assertIn(msg, out)
683698

684699
def test_disallowed_grouping__empty_group_on_left(self):
685-
self.parse_function_should_fail("""
700+
out = self.parse_function_should_fail("""
686701
module foo
687702
foo.empty_group
688703
[
@@ -692,9 +707,14 @@ def test_disallowed_grouping__empty_group_on_left(self):
692707
]
693708
param: int
694709
""")
710+
msg = (
711+
"Function empty_group has an empty group.\n"
712+
"All groups must contain at least one parameter."
713+
)
714+
self.assertIn(msg, out)
695715

696716
def test_disallowed_grouping__empty_group_on_right(self):
697-
self.parse_function_should_fail("""
717+
out = self.parse_function_should_fail("""
698718
module foo
699719
foo.empty_group
700720
param: int
@@ -704,6 +724,11 @@ def test_disallowed_grouping__empty_group_on_right(self):
704724
group2 : int
705725
]
706726
""")
727+
msg = (
728+
"Function empty_group has an empty group.\n"
729+
"All groups must contain at least one parameter."
730+
)
731+
self.assertIn(msg, out)
707732

708733
def test_no_parameters(self):
709734
function = self.parse_function("""
@@ -732,69 +757,60 @@ class foo.Bar "unused" "notneeded"
732757
self.assertEqual(1, len(function.parameters))
733758

734759
def test_illegal_module_line(self):
735-
self.parse_function_should_fail("""
760+
out = self.parse_function_should_fail("""
736761
module foo
737762
foo.bar => int
738763
/
739764
""")
765+
msg = "Illegal function name: foo.bar => int"
766+
self.assertIn(msg, out)
740767

741768
def test_illegal_c_basename(self):
742-
self.parse_function_should_fail("""
769+
out = self.parse_function_should_fail("""
743770
module foo
744771
foo.bar as 935
745772
/
746773
""")
774+
msg = "Illegal C basename: 935"
775+
self.assertIn(msg, out)
747776

748777
def test_single_star(self):
749-
self.parse_function_should_fail("""
750-
module foo
751-
foo.bar
752-
*
753-
*
754-
""")
755-
756-
def test_parameters_required_after_star_without_initial_parameters_or_docstring(self):
757-
self.parse_function_should_fail("""
758-
module foo
759-
foo.bar
760-
*
761-
""")
762-
763-
def test_parameters_required_after_star_without_initial_parameters_with_docstring(self):
764-
self.parse_function_should_fail("""
778+
out = self.parse_function_should_fail("""
765779
module foo
766780
foo.bar
767781
*
768-
Docstring here.
769-
""")
770-
771-
def test_parameters_required_after_star_with_initial_parameters_without_docstring(self):
772-
self.parse_function_should_fail("""
773-
module foo
774-
foo.bar
775-
this: int
776782
*
777783
""")
784+
self.assertIn("Function bar uses '*' more than once.", out)
778785

779-
def test_parameters_required_after_star_with_initial_parameters_and_docstring(self):
780-
self.parse_function_should_fail("""
781-
module foo
782-
foo.bar
783-
this: int
784-
*
785-
Docstring.
786-
""")
786+
def test_parameters_required_after_star(self):
787+
dataset = (
788+
"module foo\nfoo.bar\n *",
789+
"module foo\nfoo.bar\n *\nDocstring here.",
790+
"module foo\nfoo.bar\n this: int\n *",
791+
"module foo\nfoo.bar\n this: int\n *\nDocstring.",
792+
)
793+
msg = "Function bar specifies '*' without any parameters afterwards."
794+
for block in dataset:
795+
with self.subTest(block=block):
796+
out = self.parse_function_should_fail(block)
797+
self.assertIn(msg, out)
787798

788799
def test_single_slash(self):
789-
self.parse_function_should_fail("""
800+
out = self.parse_function_should_fail("""
790801
module foo
791802
foo.bar
792803
/
793804
/
794805
""")
806+
msg = (
807+
"Function bar has an unsupported group configuration. "
808+
"(Unexpected state 0.d)"
809+
)
810+
self.assertIn(msg, out)
795811

796812
def test_mix_star_and_slash(self):
797-
self.parse_function_should_fail("""
813+
out = self.parse_function_should_fail("""
798814
module foo
799815
foo.bar
800816
x: int
@@ -803,14 +819,24 @@ def test_mix_star_and_slash(self):
803819
z: int
804820
/
805821
""")
822+
msg = (
823+
"Function bar mixes keyword-only and positional-only parameters, "
824+
"which is unsupported."
825+
)
826+
self.assertIn(msg, out)
806827

807828
def test_parameters_not_permitted_after_slash_for_now(self):
808-
self.parse_function_should_fail("""
829+
out = self.parse_function_should_fail("""
809830
module foo
810831
foo.bar
811832
/
812833
x: int
813834
""")
835+
msg = (
836+
"Function bar has an unsupported group configuration. "
837+
"(Unexpected state 0.d)"
838+
)
839+
self.assertIn(msg, out)
814840

815841
def test_parameters_no_more_than_one_vararg(self):
816842
expected_msg = (

0 commit comments

Comments
 (0)