@@ -1624,5 +1624,169 @@ def test_permute_optional_groups(self):
1624
1624
self .assertEqual (actual , expected )
1625
1625
1626
1626
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\n b" , "a\n b" ),
1633
+ ("a\n b\n " , "a\n b" ),
1634
+ ("a\n b " , "a\n b" ),
1635
+ ("\n a\n b\n \n " , "a\n b" ),
1636
+ ("\n \n a\n b\n \n " , "a\n b" ),
1637
+ ("\n \n a\n \n b\n \n " , "a\n \n b" ),
1638
+ # Note, leading whitespace is preserved:
1639
+ (" a\n b" , " a\n b" ),
1640
+ (" a\n b " , " a\n b" ),
1641
+ (" \n \n a\n b \n \n " , " a\n b" ),
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
+
1627
1791
if __name__ == "__main__" :
1628
1792
unittest .main ()
0 commit comments