Skip to content

Commit fda2970

Browse files
[3.12] gh-106368: Add tests for permutation helpers in Argument Clinic (GH-106407) (#106409)
Added new test class PermutationTests() (cherry picked from commit 8f6df5e) Co-authored-by: Erlend E. Aasland <erlend@python.org>
1 parent b84365f commit fda2970

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
@@ -1586,5 +1586,111 @@ def test_cloned_func_with_converter_exception_message(self):
15861586
self.assertEqual(func(), name)
15871587

15881588

1589+
class PermutationTests(unittest.TestCase):
1590+
"""Test permutation support functions."""
1591+
1592+
def test_permute_left_option_groups(self):
1593+
expected = (
1594+
(),
1595+
(3,),
1596+
(2, 3),
1597+
(1, 2, 3),
1598+
)
1599+
data = list(zip([1, 2, 3])) # Generate a list of 1-tuples.
1600+
actual = tuple(clinic.permute_left_option_groups(data))
1601+
self.assertEqual(actual, expected)
1602+
1603+
def test_permute_right_option_groups(self):
1604+
expected = (
1605+
(),
1606+
(1,),
1607+
(1, 2),
1608+
(1, 2, 3),
1609+
)
1610+
data = list(zip([1, 2, 3])) # Generate a list of 1-tuples.
1611+
actual = tuple(clinic.permute_right_option_groups(data))
1612+
self.assertEqual(actual, expected)
1613+
1614+
def test_permute_optional_groups(self):
1615+
empty = {
1616+
"left": (), "required": (), "right": (),
1617+
"expected": ((),),
1618+
}
1619+
noleft1 = {
1620+
"left": (), "required": ("b",), "right": ("c",),
1621+
"expected": (
1622+
("b",),
1623+
("b", "c"),
1624+
),
1625+
}
1626+
noleft2 = {
1627+
"left": (), "required": ("b", "c",), "right": ("d",),
1628+
"expected": (
1629+
("b", "c"),
1630+
("b", "c", "d"),
1631+
),
1632+
}
1633+
noleft3 = {
1634+
"left": (), "required": ("b", "c",), "right": ("d", "e"),
1635+
"expected": (
1636+
("b", "c"),
1637+
("b", "c", "d"),
1638+
("b", "c", "d", "e"),
1639+
),
1640+
}
1641+
noright1 = {
1642+
"left": ("a",), "required": ("b",), "right": (),
1643+
"expected": (
1644+
("b",),
1645+
("a", "b"),
1646+
),
1647+
}
1648+
noright2 = {
1649+
"left": ("a",), "required": ("b", "c"), "right": (),
1650+
"expected": (
1651+
("b", "c"),
1652+
("a", "b", "c"),
1653+
),
1654+
}
1655+
noright3 = {
1656+
"left": ("a", "b"), "required": ("c",), "right": (),
1657+
"expected": (
1658+
("c",),
1659+
("b", "c"),
1660+
("a", "b", "c"),
1661+
),
1662+
}
1663+
leftandright1 = {
1664+
"left": ("a",), "required": ("b",), "right": ("c",),
1665+
"expected": (
1666+
("b",),
1667+
("a", "b"), # Prefer left.
1668+
("a", "b", "c"),
1669+
),
1670+
}
1671+
leftandright2 = {
1672+
"left": ("a", "b"), "required": ("c", "d"), "right": ("e", "f"),
1673+
"expected": (
1674+
("c", "d"),
1675+
("b", "c", "d"), # Prefer left.
1676+
("a", "b", "c", "d"), # Prefer left.
1677+
("a", "b", "c", "d", "e"),
1678+
("a", "b", "c", "d", "e", "f"),
1679+
),
1680+
}
1681+
dataset = (
1682+
empty,
1683+
noleft1, noleft2, noleft3,
1684+
noright1, noright2, noright3,
1685+
leftandright1, leftandright2,
1686+
)
1687+
for params in dataset:
1688+
with self.subTest(**params):
1689+
left, required, right, expected = params.values()
1690+
permutations = clinic.permute_optional_groups(left, required, right)
1691+
actual = tuple(permutations)
1692+
self.assertEqual(actual, expected)
1693+
1694+
15891695
if __name__ == "__main__":
15901696
unittest.main()

Tools/clinic/clinic.py

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

510510
def permute_left_option_groups(l):
511511
"""
512-
Given [1, 2, 3], should yield:
512+
Given [(1,), (2,), (3,)], should yield:
513513
()
514514
(3,)
515515
(2, 3)
@@ -524,7 +524,7 @@ def permute_left_option_groups(l):
524524

525525
def permute_right_option_groups(l):
526526
"""
527-
Given [1, 2, 3], should yield:
527+
Given [(1,), (2,), (3,)], should yield:
528528
()
529529
(1,)
530530
(1, 2)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy