@@ -1692,5 +1692,169 @@ def test_permute_optional_groups(self):
1692
1692
self .assertEqual (actual , expected )
1693
1693
1694
1694
1695
+ class FormatHelperTests (unittest .TestCase ):
1696
+
1697
+ def test_strip_leading_and_trailing_blank_lines (self ):
1698
+ dataset = (
1699
+ # Input lines, expected output.
1700
+ ("a\n b" , "a\n b" ),
1701
+ ("a\n b\n " , "a\n b" ),
1702
+ ("a\n b " , "a\n b" ),
1703
+ ("\n a\n b\n \n " , "a\n b" ),
1704
+ ("\n \n a\n b\n \n " , "a\n b" ),
1705
+ ("\n \n a\n \n b\n \n " , "a\n \n b" ),
1706
+ # Note, leading whitespace is preserved:
1707
+ (" a\n b" , " a\n b" ),
1708
+ (" a\n b " , " a\n b" ),
1709
+ (" \n \n a\n b \n \n " , " a\n b" ),
1710
+ )
1711
+ for lines , expected in dataset :
1712
+ with self .subTest (lines = lines , expected = expected ):
1713
+ out = clinic .strip_leading_and_trailing_blank_lines (lines )
1714
+ self .assertEqual (out , expected )
1715
+
1716
+ def test_normalize_snippet (self ):
1717
+ snippet = """
1718
+ one
1719
+ two
1720
+ three
1721
+ """
1722
+
1723
+ # Expected outputs:
1724
+ zero_indent = (
1725
+ "one\n "
1726
+ "two\n "
1727
+ "three"
1728
+ )
1729
+ four_indent = (
1730
+ " one\n "
1731
+ " two\n "
1732
+ " three"
1733
+ )
1734
+ eight_indent = (
1735
+ " one\n "
1736
+ " two\n "
1737
+ " three"
1738
+ )
1739
+ expected_outputs = {0 : zero_indent , 4 : four_indent , 8 : eight_indent }
1740
+ for indent , expected in expected_outputs .items ():
1741
+ with self .subTest (indent = indent ):
1742
+ actual = clinic .normalize_snippet (snippet , indent = indent )
1743
+ self .assertEqual (actual , expected )
1744
+
1745
+ def test_accumulator (self ):
1746
+ acc = clinic .text_accumulator ()
1747
+ self .assertEqual (acc .output (), "" )
1748
+ acc .append ("a" )
1749
+ self .assertEqual (acc .output (), "a" )
1750
+ self .assertEqual (acc .output (), "" )
1751
+ acc .append ("b" )
1752
+ self .assertEqual (acc .output (), "b" )
1753
+ self .assertEqual (acc .output (), "" )
1754
+ acc .append ("c" )
1755
+ acc .append ("d" )
1756
+ self .assertEqual (acc .output (), "cd" )
1757
+ self .assertEqual (acc .output (), "" )
1758
+
1759
+ def test_quoted_for_c_string (self ):
1760
+ dataset = (
1761
+ # input, expected
1762
+ (r"abc" , r"abc" ),
1763
+ (r"\abc" , r"\\abc" ),
1764
+ (r"\a\bc" , r"\\a\\bc" ),
1765
+ (r"\a\\bc" , r"\\a\\\\bc" ),
1766
+ (r'"abc"' , r'\"abc\"' ),
1767
+ (r"'a'" , r"\'a\'" ),
1768
+ )
1769
+ for line , expected in dataset :
1770
+ with self .subTest (line = line , expected = expected ):
1771
+ out = clinic .quoted_for_c_string (line )
1772
+ self .assertEqual (out , expected )
1773
+
1774
+ def test_rstrip_lines (self ):
1775
+ lines = (
1776
+ "a \n "
1777
+ "b\n "
1778
+ " c\n "
1779
+ " d \n "
1780
+ )
1781
+ expected = (
1782
+ "a\n "
1783
+ "b\n "
1784
+ " c\n "
1785
+ " d\n "
1786
+ )
1787
+ out = clinic .rstrip_lines (lines )
1788
+ self .assertEqual (out , expected )
1789
+
1790
+ def test_format_escape (self ):
1791
+ line = "{}, {a}"
1792
+ expected = "{{}}, {{a}}"
1793
+ out = clinic .format_escape (line )
1794
+ self .assertEqual (out , expected )
1795
+
1796
+ def test_indent_all_lines (self ):
1797
+ # Blank lines are expected to be unchanged.
1798
+ self .assertEqual (clinic .indent_all_lines ("" , prefix = "bar" ), "" )
1799
+
1800
+ lines = (
1801
+ "one\n "
1802
+ "two" # The missing newline is deliberate.
1803
+ )
1804
+ expected = (
1805
+ "barone\n "
1806
+ "bartwo"
1807
+ )
1808
+ out = clinic .indent_all_lines (lines , prefix = "bar" )
1809
+ self .assertEqual (out , expected )
1810
+
1811
+ # If last line is empty, expect it to be unchanged.
1812
+ lines = (
1813
+ "\n "
1814
+ "one\n "
1815
+ "two\n "
1816
+ ""
1817
+ )
1818
+ expected = (
1819
+ "bar\n "
1820
+ "barone\n "
1821
+ "bartwo\n "
1822
+ ""
1823
+ )
1824
+ out = clinic .indent_all_lines (lines , prefix = "bar" )
1825
+ self .assertEqual (out , expected )
1826
+
1827
+ def test_suffix_all_lines (self ):
1828
+ # Blank lines are expected to be unchanged.
1829
+ self .assertEqual (clinic .suffix_all_lines ("" , suffix = "foo" ), "" )
1830
+
1831
+ lines = (
1832
+ "one\n "
1833
+ "two" # The missing newline is deliberate.
1834
+ )
1835
+ expected = (
1836
+ "onefoo\n "
1837
+ "twofoo"
1838
+ )
1839
+ out = clinic .suffix_all_lines (lines , suffix = "foo" )
1840
+ self .assertEqual (out , expected )
1841
+
1842
+ # If last line is empty, expect it to be unchanged.
1843
+ lines = (
1844
+ "\n "
1845
+ "one\n "
1846
+ "two\n "
1847
+ ""
1848
+ )
1849
+ expected = (
1850
+ "foo\n "
1851
+ "onefoo\n "
1852
+ "twofoo\n "
1853
+ ""
1854
+ )
1855
+ out = clinic .suffix_all_lines (lines , suffix = "foo" )
1856
+ self .assertEqual (out , expected )
1857
+
1858
+
1695
1859
if __name__ == "__main__" :
1696
1860
unittest .main ()
0 commit comments