Skip to content

Commit fd1ec9a

Browse files
erlend-aaslandAlexWaygood
authored andcommitted
pythongh-106368: Add tests for formatting helpers in Argument Clinic (pythonGH-106415)
(cherry picked from commit 2fb9480) Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent 9f8aa7a commit fd1ec9a

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

Diff for: Lib/test/test_clinic.py

+164
Original file line numberDiff line numberDiff line change
@@ -1624,5 +1624,169 @@ def test_permute_optional_groups(self):
16241624
self.assertEqual(actual, expected)
16251625

16261626

1627+
class FormatHelperTests(unittest.TestCase):
1628+
1629+
def test_strip_leading_and_trailing_blank_lines(self):
1630+
dataset = (
1631+
# Input lines, expected output.
1632+
("a\nb", "a\nb"),
1633+
("a\nb\n", "a\nb"),
1634+
("a\nb ", "a\nb"),
1635+
("\na\nb\n\n", "a\nb"),
1636+
("\n\na\nb\n\n", "a\nb"),
1637+
("\n\na\n\nb\n\n", "a\n\nb"),
1638+
# Note, leading whitespace is preserved:
1639+
(" a\nb", " a\nb"),
1640+
(" a\nb ", " a\nb"),
1641+
(" \n \n a\nb \n \n ", " a\nb"),
1642+
)
1643+
for lines, expected in dataset:
1644+
with self.subTest(lines=lines, expected=expected):
1645+
out = clinic.strip_leading_and_trailing_blank_lines(lines)
1646+
self.assertEqual(out, expected)
1647+
1648+
def test_normalize_snippet(self):
1649+
snippet = """
1650+
one
1651+
two
1652+
three
1653+
"""
1654+
1655+
# Expected outputs:
1656+
zero_indent = (
1657+
"one\n"
1658+
"two\n"
1659+
"three"
1660+
)
1661+
four_indent = (
1662+
" one\n"
1663+
" two\n"
1664+
" three"
1665+
)
1666+
eight_indent = (
1667+
" one\n"
1668+
" two\n"
1669+
" three"
1670+
)
1671+
expected_outputs = {0: zero_indent, 4: four_indent, 8: eight_indent}
1672+
for indent, expected in expected_outputs.items():
1673+
with self.subTest(indent=indent):
1674+
actual = clinic.normalize_snippet(snippet, indent=indent)
1675+
self.assertEqual(actual, expected)
1676+
1677+
def test_accumulator(self):
1678+
acc = clinic.text_accumulator()
1679+
self.assertEqual(acc.output(), "")
1680+
acc.append("a")
1681+
self.assertEqual(acc.output(), "a")
1682+
self.assertEqual(acc.output(), "")
1683+
acc.append("b")
1684+
self.assertEqual(acc.output(), "b")
1685+
self.assertEqual(acc.output(), "")
1686+
acc.append("c")
1687+
acc.append("d")
1688+
self.assertEqual(acc.output(), "cd")
1689+
self.assertEqual(acc.output(), "")
1690+
1691+
def test_quoted_for_c_string(self):
1692+
dataset = (
1693+
# input, expected
1694+
(r"abc", r"abc"),
1695+
(r"\abc", r"\\abc"),
1696+
(r"\a\bc", r"\\a\\bc"),
1697+
(r"\a\\bc", r"\\a\\\\bc"),
1698+
(r'"abc"', r'\"abc\"'),
1699+
(r"'a'", r"\'a\'"),
1700+
)
1701+
for line, expected in dataset:
1702+
with self.subTest(line=line, expected=expected):
1703+
out = clinic.quoted_for_c_string(line)
1704+
self.assertEqual(out, expected)
1705+
1706+
def test_rstrip_lines(self):
1707+
lines = (
1708+
"a \n"
1709+
"b\n"
1710+
" c\n"
1711+
" d \n"
1712+
)
1713+
expected = (
1714+
"a\n"
1715+
"b\n"
1716+
" c\n"
1717+
" d\n"
1718+
)
1719+
out = clinic.rstrip_lines(lines)
1720+
self.assertEqual(out, expected)
1721+
1722+
def test_format_escape(self):
1723+
line = "{}, {a}"
1724+
expected = "{{}}, {{a}}"
1725+
out = clinic.format_escape(line)
1726+
self.assertEqual(out, expected)
1727+
1728+
def test_indent_all_lines(self):
1729+
# Blank lines are expected to be unchanged.
1730+
self.assertEqual(clinic.indent_all_lines("", prefix="bar"), "")
1731+
1732+
lines = (
1733+
"one\n"
1734+
"two" # The missing newline is deliberate.
1735+
)
1736+
expected = (
1737+
"barone\n"
1738+
"bartwo"
1739+
)
1740+
out = clinic.indent_all_lines(lines, prefix="bar")
1741+
self.assertEqual(out, expected)
1742+
1743+
# If last line is empty, expect it to be unchanged.
1744+
lines = (
1745+
"\n"
1746+
"one\n"
1747+
"two\n"
1748+
""
1749+
)
1750+
expected = (
1751+
"bar\n"
1752+
"barone\n"
1753+
"bartwo\n"
1754+
""
1755+
)
1756+
out = clinic.indent_all_lines(lines, prefix="bar")
1757+
self.assertEqual(out, expected)
1758+
1759+
def test_suffix_all_lines(self):
1760+
# Blank lines are expected to be unchanged.
1761+
self.assertEqual(clinic.suffix_all_lines("", suffix="foo"), "")
1762+
1763+
lines = (
1764+
"one\n"
1765+
"two" # The missing newline is deliberate.
1766+
)
1767+
expected = (
1768+
"onefoo\n"
1769+
"twofoo"
1770+
)
1771+
out = clinic.suffix_all_lines(lines, suffix="foo")
1772+
self.assertEqual(out, expected)
1773+
1774+
# If last line is empty, expect it to be unchanged.
1775+
lines = (
1776+
"\n"
1777+
"one\n"
1778+
"two\n"
1779+
""
1780+
)
1781+
expected = (
1782+
"foo\n"
1783+
"onefoo\n"
1784+
"twofoo\n"
1785+
""
1786+
)
1787+
out = clinic.suffix_all_lines(lines, suffix="foo")
1788+
self.assertEqual(out, expected)
1789+
1790+
16271791
if __name__ == "__main__":
16281792
unittest.main()

0 commit comments

Comments
 (0)