|
11 | 11 | import unittest |
12 | 12 | from test import support |
13 | 13 |
|
14 | | -from textwrap import TextWrapper, wrap, fill, dedent |
| 14 | +from textwrap import TextWrapper, wrap, fill, dedent, indent |
15 | 15 |
|
16 | 16 |
|
17 | 17 | class BaseTestCase(unittest.TestCase): |
@@ -594,11 +594,147 @@ def test_dedent_preserve_margin_tabs(self): |
594 | 594 | self.assertEqual(expect, dedent(text)) |
595 | 595 |
|
596 | 596 |
|
| 597 | +# Test textwrap.indent |
| 598 | +class IndentTestCase(unittest.TestCase): |
| 599 | + # The examples used for tests. If any of these change, the expected |
| 600 | + # results in the various test cases must also be updated. |
| 601 | + # The roundtrip cases are separate, because textwrap.dedent doesn't |
| 602 | + # handle Windows line endings |
| 603 | + ROUNDTRIP_CASES = ( |
| 604 | + # Basic test case |
| 605 | + "Hi.\nThis is a test.\nTesting.", |
| 606 | + # Include a blank line |
| 607 | + "Hi.\nThis is a test.\n\nTesting.", |
| 608 | + # Include leading and trailing blank lines |
| 609 | + "\nHi.\nThis is a test.\nTesting.\n", |
| 610 | + ) |
| 611 | + CASES = ROUNDTRIP_CASES + ( |
| 612 | + # Use Windows line endings |
| 613 | + "Hi.\r\nThis is a test.\r\nTesting.\r\n", |
| 614 | + # Pathological case |
| 615 | + "\nHi.\r\nThis is a test.\n\r\nTesting.\r\n\n", |
| 616 | + ) |
| 617 | + |
| 618 | + def test_indent_nomargin_default(self): |
| 619 | + # indent should do nothing if 'prefix' is empty. |
| 620 | + for text in self.CASES: |
| 621 | + self.assertEqual(indent(text, ''), text) |
| 622 | + |
| 623 | + def test_indent_nomargin_explicit_default(self): |
| 624 | + # The same as test_indent_nomargin, but explicitly requesting |
| 625 | + # the default behaviour by passing None as the predicate |
| 626 | + for text in self.CASES: |
| 627 | + self.assertEqual(indent(text, '', None), text) |
| 628 | + |
| 629 | + def test_indent_nomargin_all_lines(self): |
| 630 | + # The same as test_indent_nomargin, but using the optional |
| 631 | + # predicate argument |
| 632 | + predicate = lambda line: True |
| 633 | + for text in self.CASES: |
| 634 | + self.assertEqual(indent(text, '', predicate), text) |
| 635 | + |
| 636 | + def test_indent_no_lines(self): |
| 637 | + # Explicitly skip indenting any lines |
| 638 | + predicate = lambda line: False |
| 639 | + for text in self.CASES: |
| 640 | + self.assertEqual(indent(text, ' ', predicate), text) |
| 641 | + |
| 642 | + def test_roundtrip_spaces(self): |
| 643 | + # A whitespace prefix should roundtrip with dedent |
| 644 | + for text in self.ROUNDTRIP_CASES: |
| 645 | + self.assertEqual(dedent(indent(text, ' ')), text) |
| 646 | + |
| 647 | + def test_roundtrip_tabs(self): |
| 648 | + # A whitespace prefix should roundtrip with dedent |
| 649 | + for text in self.ROUNDTRIP_CASES: |
| 650 | + self.assertEqual(dedent(indent(text, '\t\t')), text) |
| 651 | + |
| 652 | + def test_roundtrip_mixed(self): |
| 653 | + # A whitespace prefix should roundtrip with dedent |
| 654 | + for text in self.ROUNDTRIP_CASES: |
| 655 | + self.assertEqual(dedent(indent(text, ' \t \t ')), text) |
| 656 | + |
| 657 | + def test_indent_default(self): |
| 658 | + # Test default indenting of lines that are not whitespace only |
| 659 | + prefix = ' ' |
| 660 | + expected = ( |
| 661 | + # Basic test case |
| 662 | + " Hi.\n This is a test.\n Testing.", |
| 663 | + # Include a blank line |
| 664 | + " Hi.\n This is a test.\n\n Testing.", |
| 665 | + # Include leading and trailing blank lines |
| 666 | + "\n Hi.\n This is a test.\n Testing.\n", |
| 667 | + # Use Windows line endings |
| 668 | + " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 669 | + # Pathological case |
| 670 | + "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n", |
| 671 | + ) |
| 672 | + for text, expect in zip(self.CASES, expected): |
| 673 | + self.assertEqual(indent(text, prefix), expect) |
| 674 | + |
| 675 | + def test_indent_explicit_default(self): |
| 676 | + # Test default indenting of lines that are not whitespace only |
| 677 | + prefix = ' ' |
| 678 | + expected = ( |
| 679 | + # Basic test case |
| 680 | + " Hi.\n This is a test.\n Testing.", |
| 681 | + # Include a blank line |
| 682 | + " Hi.\n This is a test.\n\n Testing.", |
| 683 | + # Include leading and trailing blank lines |
| 684 | + "\n Hi.\n This is a test.\n Testing.\n", |
| 685 | + # Use Windows line endings |
| 686 | + " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 687 | + # Pathological case |
| 688 | + "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n", |
| 689 | + ) |
| 690 | + for text, expect in zip(self.CASES, expected): |
| 691 | + self.assertEqual(indent(text, prefix, None), expect) |
| 692 | + |
| 693 | + def test_indent_all_lines(self): |
| 694 | + # Add 'prefix' to all lines, including whitespace-only ones. |
| 695 | + prefix = ' ' |
| 696 | + expected = ( |
| 697 | + # Basic test case |
| 698 | + " Hi.\n This is a test.\n Testing.", |
| 699 | + # Include a blank line |
| 700 | + " Hi.\n This is a test.\n \n Testing.", |
| 701 | + # Include leading and trailing blank lines |
| 702 | + " \n Hi.\n This is a test.\n Testing.\n", |
| 703 | + # Use Windows line endings |
| 704 | + " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 705 | + # Pathological case |
| 706 | + " \n Hi.\r\n This is a test.\n \r\n Testing.\r\n \n", |
| 707 | + ) |
| 708 | + predicate = lambda line: True |
| 709 | + for text, expect in zip(self.CASES, expected): |
| 710 | + self.assertEqual(indent(text, prefix, predicate), expect) |
| 711 | + |
| 712 | + def test_indent_empty_lines(self): |
| 713 | + # Add 'prefix' solely to whitespace-only lines. |
| 714 | + prefix = ' ' |
| 715 | + expected = ( |
| 716 | + # Basic test case |
| 717 | + "Hi.\nThis is a test.\nTesting.", |
| 718 | + # Include a blank line |
| 719 | + "Hi.\nThis is a test.\n \nTesting.", |
| 720 | + # Include leading and trailing blank lines |
| 721 | + " \nHi.\nThis is a test.\nTesting.\n", |
| 722 | + # Use Windows line endings |
| 723 | + "Hi.\r\nThis is a test.\r\nTesting.\r\n", |
| 724 | + # Pathological case |
| 725 | + " \nHi.\r\nThis is a test.\n \r\nTesting.\r\n \n", |
| 726 | + ) |
| 727 | + predicate = lambda line: not line.strip() |
| 728 | + for text, expect in zip(self.CASES, expected): |
| 729 | + self.assertEqual(indent(text, prefix, predicate), expect) |
| 730 | + |
| 731 | + |
597 | 732 | def test_main(): |
598 | 733 | support.run_unittest(WrapTestCase, |
599 | 734 | LongWordTestCase, |
600 | 735 | IndentTestCases, |
601 | | - DedentTestCase) |
| 736 | + DedentTestCase, |
| 737 | + IndentTestCase) |
602 | 738 |
|
603 | 739 | if __name__ == '__main__': |
604 | 740 | test_main() |
0 commit comments