Skip to content

Commit 9f8aa7a

Browse files
[3.11] gh-106368: Add tests for permutation helpers in Argument Clinic (GH-106407) (#106410)
Added new test class PermutationTests() (cherry picked from commit 8f6df5e) Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 3cc5c4a commit 9f8aa7a

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

Lib/test/test_clinic.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,5 +1518,111 @@ def test_gh_99240_double_free(self):
15181518
ac_tester.gh_99240_double_free('a', '\0b')
15191519

15201520

1521+
class PermutationTests(unittest.TestCase):
1522+
"""Test permutation support functions."""
1523+
1524+
def test_permute_left_option_groups(self):
1525+
expected = (
1526+
(),
1527+
(3,),
1528+
(2, 3),
1529+
(1, 2, 3),
1530+
)
1531+
data = list(zip([1, 2, 3])) # Generate a list of 1-tuples.
1532+
actual = tuple(clinic.permute_left_option_groups(data))
1533+
self.assertEqual(actual, expected)
1534+
1535+
def test_permute_right_option_groups(self):
1536+
expected = (
1537+
(),
1538+
(1,),
1539+
(1, 2),
1540+
(1, 2, 3),
1541+
)
1542+
data = list(zip([1, 2, 3])) # Generate a list of 1-tuples.
1543+
actual = tuple(clinic.permute_right_option_groups(data))
1544+
self.assertEqual(actual, expected)
1545+
1546+
def test_permute_optional_groups(self):
1547+
empty = {
1548+
"left": (), "required": (), "right": (),
1549+
"expected": ((),),
1550+
}
1551+
noleft1 = {
1552+
"left": (), "required": ("b",), "right": ("c",),
1553+
"expected": (
1554+
("b",),
1555+
("b", "c"),
1556+
),
1557+
}
1558+
noleft2 = {
1559+
"left": (), "required": ("b", "c",), "right": ("d",),
1560+
"expected": (
1561+
("b", "c"),
1562+
("b", "c", "d"),
1563+
),
1564+
}
1565+
noleft3 = {
1566+
"left": (), "required": ("b", "c",), "right": ("d", "e"),
1567+
"expected": (
1568+
("b", "c"),
1569+
("b", "c", "d"),
1570+
("b", "c", "d", "e"),
1571+
),
1572+
}
1573+
noright1 = {
1574+
"left": ("a",), "required": ("b",), "right": (),
1575+
"expected": (
1576+
("b",),
1577+
("a", "b"),
1578+
),
1579+
}
1580+
noright2 = {
1581+
"left": ("a",), "required": ("b", "c"), "right": (),
1582+
"expected": (
1583+
("b", "c"),
1584+
("a", "b", "c"),
1585+
),
1586+
}
1587+
noright3 = {
1588+
"left": ("a", "b"), "required": ("c",), "right": (),
1589+
"expected": (
1590+
("c",),
1591+
("b", "c"),
1592+
("a", "b", "c"),
1593+
),
1594+
}
1595+
leftandright1 = {
1596+
"left": ("a",), "required": ("b",), "right": ("c",),
1597+
"expected": (
1598+
("b",),
1599+
("a", "b"), # Prefer left.
1600+
("a", "b", "c"),
1601+
),
1602+
}
1603+
leftandright2 = {
1604+
"left": ("a", "b"), "required": ("c", "d"), "right": ("e", "f"),
1605+
"expected": (
1606+
("c", "d"),
1607+
("b", "c", "d"), # Prefer left.
1608+
("a", "b", "c", "d"), # Prefer left.
1609+
("a", "b", "c", "d", "e"),
1610+
("a", "b", "c", "d", "e", "f"),
1611+
),
1612+
}
1613+
dataset = (
1614+
empty,
1615+
noleft1, noleft2, noleft3,
1616+
noright1, noright2, noright3,
1617+
leftandright1, leftandright2,
1618+
)
1619+
for params in dataset:
1620+
with self.subTest(**params):
1621+
left, required, right, expected = params.values()
1622+
permutations = clinic.permute_optional_groups(left, required, right)
1623+
actual = tuple(permutations)
1624+
self.assertEqual(actual, expected)
1625+
1626+
15211627
if __name__ == "__main__":
15221628
unittest.main()

Tools/clinic/clinic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ class PythonLanguage(Language):
460460

461461
def permute_left_option_groups(l):
462462
"""
463-
Given [1, 2, 3], should yield:
463+
Given [(1,), (2,), (3,)], should yield:
464464
()
465465
(3,)
466466
(2, 3)
@@ -475,7 +475,7 @@ def permute_left_option_groups(l):
475475

476476
def permute_right_option_groups(l):
477477
"""
478-
Given [1, 2, 3], should yield:
478+
Given [(1,), (2,), (3,)], should yield:
479479
()
480480
(1,)
481481
(1, 2)

0 commit comments

Comments
 (0)