From 31680b6bdc9a84c3ca3c1cd56b23bc05e1d89595 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 13:59:16 +0200 Subject: [PATCH 1/2] gh-106368: Add tests for formatting helpers in Argument Clinic Added new test class FormatHelperTests() --- Lib/test/test_clinic.py | 162 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 544e7323e4f606..48041c6af80a59 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -1692,5 +1692,167 @@ def test_permute_optional_groups(self): 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 + """ + template = ( + "{indent}one\n" + "{indent}two\n" + "{indent}three" + ) + for indent in 0, 4, 8: + expected = template.format(indent=" " * indent) + with self.subTest(snippet=snippet, indent=indent, expected=expected): + out = clinic.normalize_snippet(snippet, indent=indent) + self.assertEqual(out, 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_c_repr(self): + dataset = "", "a", "abc" + for line in dataset: + expected = f'"{line}"' + with self.subTest(line=line, expected=expected): + out = clinic.c_repr(line) + self.assertEqual(out, expected) + + 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): + prefix = "prefix" + # Blank lines are expected to be unchanged. + self.assertEqual(clinic.indent_all_lines("", prefix=prefix), "") + + lines = ( + "one\n" + "two" # The missing newline is deliberate. + ) + expected = ( + f"{prefix}one\n" + f"{prefix}two" + ) + out = clinic.indent_all_lines(lines, prefix=prefix) + self.assertEqual(out, expected) + + # If last line is empty, expect it to be unchanged. + lines = ( + "\n" + "one\n" + "two\n" + "" + ) + expected = ( + f"{prefix}\n" + f"{prefix}one\n" + f"{prefix}two\n" + f"" + ) + out = clinic.indent_all_lines(lines, prefix=prefix) + self.assertEqual(out, expected) + + def test_suffix_all_lines(self): + suffix = "suffix" + # Blank lines are expected to be unchanged. + self.assertEqual(clinic.suffix_all_lines("", suffix=suffix), "") + + lines = ( + "one\n" + "two" # The missing newline is deliberate. + ) + expected = ( + f"one{suffix}\n" + f"two{suffix}" + ) + out = clinic.suffix_all_lines(lines, suffix=suffix) + self.assertEqual(out, expected) + + # If last line is empty, expect it to be unchanged. + lines = ( + "\n" + "one\n" + "two\n" + "" + ) + expected = ( + f"{suffix}\n" + f"one{suffix}\n" + f"two{suffix}\n" + f"" + ) + out = clinic.suffix_all_lines(lines, suffix=suffix) + self.assertEqual(out, expected) + + if __name__ == "__main__": unittest.main() From 84a2cce05f5d21006a2ce99206b38272c3829ef8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 5 Jul 2023 00:10:49 +0200 Subject: [PATCH 2/2] Address review: - Be explicit - Ditch trivial test --- Lib/test/test_clinic.py | 76 +++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 48041c6af80a59..c095d14234375f 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -1719,16 +1719,28 @@ def test_normalize_snippet(self): two three """ - template = ( - "{indent}one\n" - "{indent}two\n" - "{indent}three" + + # Expected outputs: + zero_indent = ( + "one\n" + "two\n" + "three" ) - for indent in 0, 4, 8: - expected = template.format(indent=" " * indent) - with self.subTest(snippet=snippet, indent=indent, expected=expected): - out = clinic.normalize_snippet(snippet, indent=indent) - self.assertEqual(out, expected) + 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() @@ -1744,14 +1756,6 @@ def test_accumulator(self): self.assertEqual(acc.output(), "cd") self.assertEqual(acc.output(), "") - def test_c_repr(self): - dataset = "", "a", "abc" - for line in dataset: - expected = f'"{line}"' - with self.subTest(line=line, expected=expected): - out = clinic.c_repr(line) - self.assertEqual(out, expected) - def test_quoted_for_c_string(self): dataset = ( # input, expected @@ -1790,19 +1794,18 @@ def test_format_escape(self): self.assertEqual(out, expected) def test_indent_all_lines(self): - prefix = "prefix" # Blank lines are expected to be unchanged. - self.assertEqual(clinic.indent_all_lines("", prefix=prefix), "") + self.assertEqual(clinic.indent_all_lines("", prefix="bar"), "") lines = ( "one\n" "two" # The missing newline is deliberate. ) expected = ( - f"{prefix}one\n" - f"{prefix}two" + "barone\n" + "bartwo" ) - out = clinic.indent_all_lines(lines, prefix=prefix) + out = clinic.indent_all_lines(lines, prefix="bar") self.assertEqual(out, expected) # If last line is empty, expect it to be unchanged. @@ -1813,28 +1816,27 @@ def test_indent_all_lines(self): "" ) expected = ( - f"{prefix}\n" - f"{prefix}one\n" - f"{prefix}two\n" - f"" + "bar\n" + "barone\n" + "bartwo\n" + "" ) - out = clinic.indent_all_lines(lines, prefix=prefix) + out = clinic.indent_all_lines(lines, prefix="bar") self.assertEqual(out, expected) def test_suffix_all_lines(self): - suffix = "suffix" # Blank lines are expected to be unchanged. - self.assertEqual(clinic.suffix_all_lines("", suffix=suffix), "") + self.assertEqual(clinic.suffix_all_lines("", suffix="foo"), "") lines = ( "one\n" "two" # The missing newline is deliberate. ) expected = ( - f"one{suffix}\n" - f"two{suffix}" + "onefoo\n" + "twofoo" ) - out = clinic.suffix_all_lines(lines, suffix=suffix) + out = clinic.suffix_all_lines(lines, suffix="foo") self.assertEqual(out, expected) # If last line is empty, expect it to be unchanged. @@ -1845,12 +1847,12 @@ def test_suffix_all_lines(self): "" ) expected = ( - f"{suffix}\n" - f"one{suffix}\n" - f"two{suffix}\n" - f"" + "foo\n" + "onefoo\n" + "twofoo\n" + "" ) - out = clinic.suffix_all_lines(lines, suffix=suffix) + out = clinic.suffix_all_lines(lines, suffix="foo") self.assertEqual(out, expected) 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