mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] gh-106368: Add tests for formatting helpers in Argument Clinic (GH-106415) (#106438)
gh-106368: Add tests for formatting helpers in Argument Clinic (GH-106415)
(cherry picked from commit 2fb9480c83
)
Co-authored-by: Erlend E. Aasland <erlend@python.org>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
d5ed72b696
commit
6f684044a9
1 changed files with 164 additions and 0 deletions
|
@ -1692,5 +1692,169 @@ class PermutationTests(unittest.TestCase):
|
|||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
class FormatHelperTests(unittest.TestCase):
|
||||
|
||||
def test_strip_leading_and_trailing_blank_lines(self):
|
||||
dataset = (
|
||||
# Input lines, expected output.
|
||||
("a\nb", "a\nb"),
|
||||
("a\nb\n", "a\nb"),
|
||||
("a\nb ", "a\nb"),
|
||||
("\na\nb\n\n", "a\nb"),
|
||||
("\n\na\nb\n\n", "a\nb"),
|
||||
("\n\na\n\nb\n\n", "a\n\nb"),
|
||||
# Note, leading whitespace is preserved:
|
||||
(" a\nb", " a\nb"),
|
||||
(" a\nb ", " a\nb"),
|
||||
(" \n \n a\nb \n \n ", " a\nb"),
|
||||
)
|
||||
for lines, expected in dataset:
|
||||
with self.subTest(lines=lines, expected=expected):
|
||||
out = clinic.strip_leading_and_trailing_blank_lines(lines)
|
||||
self.assertEqual(out, expected)
|
||||
|
||||
def test_normalize_snippet(self):
|
||||
snippet = """
|
||||
one
|
||||
two
|
||||
three
|
||||
"""
|
||||
|
||||
# Expected outputs:
|
||||
zero_indent = (
|
||||
"one\n"
|
||||
"two\n"
|
||||
"three"
|
||||
)
|
||||
four_indent = (
|
||||
" one\n"
|
||||
" two\n"
|
||||
" three"
|
||||
)
|
||||
eight_indent = (
|
||||
" one\n"
|
||||
" two\n"
|
||||
" three"
|
||||
)
|
||||
expected_outputs = {0: zero_indent, 4: four_indent, 8: eight_indent}
|
||||
for indent, expected in expected_outputs.items():
|
||||
with self.subTest(indent=indent):
|
||||
actual = clinic.normalize_snippet(snippet, indent=indent)
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
def test_accumulator(self):
|
||||
acc = clinic.text_accumulator()
|
||||
self.assertEqual(acc.output(), "")
|
||||
acc.append("a")
|
||||
self.assertEqual(acc.output(), "a")
|
||||
self.assertEqual(acc.output(), "")
|
||||
acc.append("b")
|
||||
self.assertEqual(acc.output(), "b")
|
||||
self.assertEqual(acc.output(), "")
|
||||
acc.append("c")
|
||||
acc.append("d")
|
||||
self.assertEqual(acc.output(), "cd")
|
||||
self.assertEqual(acc.output(), "")
|
||||
|
||||
def test_quoted_for_c_string(self):
|
||||
dataset = (
|
||||
# input, expected
|
||||
(r"abc", r"abc"),
|
||||
(r"\abc", r"\\abc"),
|
||||
(r"\a\bc", r"\\a\\bc"),
|
||||
(r"\a\\bc", r"\\a\\\\bc"),
|
||||
(r'"abc"', r'\"abc\"'),
|
||||
(r"'a'", r"\'a\'"),
|
||||
)
|
||||
for line, expected in dataset:
|
||||
with self.subTest(line=line, expected=expected):
|
||||
out = clinic.quoted_for_c_string(line)
|
||||
self.assertEqual(out, expected)
|
||||
|
||||
def test_rstrip_lines(self):
|
||||
lines = (
|
||||
"a \n"
|
||||
"b\n"
|
||||
" c\n"
|
||||
" d \n"
|
||||
)
|
||||
expected = (
|
||||
"a\n"
|
||||
"b\n"
|
||||
" c\n"
|
||||
" d\n"
|
||||
)
|
||||
out = clinic.rstrip_lines(lines)
|
||||
self.assertEqual(out, expected)
|
||||
|
||||
def test_format_escape(self):
|
||||
line = "{}, {a}"
|
||||
expected = "{{}}, {{a}}"
|
||||
out = clinic.format_escape(line)
|
||||
self.assertEqual(out, expected)
|
||||
|
||||
def test_indent_all_lines(self):
|
||||
# Blank lines are expected to be unchanged.
|
||||
self.assertEqual(clinic.indent_all_lines("", prefix="bar"), "")
|
||||
|
||||
lines = (
|
||||
"one\n"
|
||||
"two" # The missing newline is deliberate.
|
||||
)
|
||||
expected = (
|
||||
"barone\n"
|
||||
"bartwo"
|
||||
)
|
||||
out = clinic.indent_all_lines(lines, prefix="bar")
|
||||
self.assertEqual(out, expected)
|
||||
|
||||
# If last line is empty, expect it to be unchanged.
|
||||
lines = (
|
||||
"\n"
|
||||
"one\n"
|
||||
"two\n"
|
||||
""
|
||||
)
|
||||
expected = (
|
||||
"bar\n"
|
||||
"barone\n"
|
||||
"bartwo\n"
|
||||
""
|
||||
)
|
||||
out = clinic.indent_all_lines(lines, prefix="bar")
|
||||
self.assertEqual(out, expected)
|
||||
|
||||
def test_suffix_all_lines(self):
|
||||
# Blank lines are expected to be unchanged.
|
||||
self.assertEqual(clinic.suffix_all_lines("", suffix="foo"), "")
|
||||
|
||||
lines = (
|
||||
"one\n"
|
||||
"two" # The missing newline is deliberate.
|
||||
)
|
||||
expected = (
|
||||
"onefoo\n"
|
||||
"twofoo"
|
||||
)
|
||||
out = clinic.suffix_all_lines(lines, suffix="foo")
|
||||
self.assertEqual(out, expected)
|
||||
|
||||
# If last line is empty, expect it to be unchanged.
|
||||
lines = (
|
||||
"\n"
|
||||
"one\n"
|
||||
"two\n"
|
||||
""
|
||||
)
|
||||
expected = (
|
||||
"foo\n"
|
||||
"onefoo\n"
|
||||
"twofoo\n"
|
||||
""
|
||||
)
|
||||
out = clinic.suffix_all_lines(lines, suffix="foo")
|
||||
self.assertEqual(out, expected)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue