예제 #1
0
    def _split(self, text):
        # Override default's '_split' method to define URLs as unbreakable,
        # and reduce URLs if needed.
        # XXX This is fragile, since it uses TextWrapper private API.

        # Keep a mapping from reduced URL to full URL
        self.urls_map = {}

        # Get the chunks
        chunks = []
        for segment in url_expr.split(text):
            starts = segment.startswith
            if starts('http://') or starts('https://') or starts('ftp://'):
                if len(segment) > 95:
                    # Reduce URL
                    url = segment
                    segment = segment[:46] + '...' + segment[-46:]
                    self.urls_map[segment] = url
                else:
                    self.urls_map[segment] = segment
                chunks.append(segment)
            else:
                chunks.extend(TextWrapper._split(self, segment))
        return chunks
예제 #2
0
    def _split(self, text):
        # Override default's '_split' method to define URLs as unbreakable,
        # and reduce URLs if needed.
        # XXX This is fragile, since it uses TextWrapper private API.

        # Keep a mapping from reduced URL to full URL
        self.urls_map = {}

        # Get the chunks
        chunks = []
        for segment in url_expr.split(text):
            starts = segment.startswith
            if starts('http://') or starts('https://') or starts('ftp://'):
                if len(segment) > 95:
                    # Reduce URL
                    url = segment
                    segment = segment[:46] + '...' + segment[-46:]
                    self.urls_map[segment] = url
                else:
                    self.urls_map[segment] = segment
                chunks.append(segment)
            else:
                chunks.extend(TextWrapper._split(self, segment))
        return chunks
예제 #3
0
class WrapTestCase(BaseTestCase):
    def setUp(self):
        self.wrapper = TextWrapper(width=45)

    def test_simple(self):
        # Simple case: just words, spaces, and a bit of punctuation

        text = "Hello there, how are you this fine day?  I'm glad to hear it!"

        self.check_wrap(text, 12, [
            "Hello there,", "how are you", "this fine", "day?  I'm",
            "glad to hear", "it!"
        ])
        self.check_wrap(text, 42, [
            "Hello there, how are you this fine day?", "I'm glad to hear it!"
        ])
        self.check_wrap(text, 80, [text])

    def test_whitespace(self):
        # Whitespace munging and end-of-sentence detection

        text = """\
This is a paragraph that already has
line breaks.  But some of its lines are much longer than the others,
so it needs to be wrapped.
Some lines are \ttabbed too.
What a mess!
"""

        expect = [
            "This is a paragraph that already has line",
            "breaks.  But some of its lines are much",
            "longer than the others, so it needs to be",
            "wrapped.  Some lines are  tabbed too.  What a", "mess!"
        ]

        wrapper = TextWrapper(45, fix_sentence_endings=True)
        result = wrapper.wrap(text)
        self.check(result, expect)

        result = wrapper.fill(text)
        self.check(result, '\n'.join(expect))

    def test_fix_sentence_endings(self):
        wrapper = TextWrapper(60, fix_sentence_endings=True)

        # SF #847346: ensure that fix_sentence_endings=True does the
        # right thing even on input short enough that it doesn't need to
        # be wrapped.
        text = "A short line. Note the single space."
        expect = ["A short line.  Note the single space."]
        self.check(wrapper.wrap(text), expect)

        # Test some of the hairy end cases that _fix_sentence_endings()
        # is supposed to handle (the easy stuff is tested in
        # test_whitespace() above).
        text = "Well, Doctor? What do you think?"
        expect = ["Well, Doctor?  What do you think?"]
        self.check(wrapper.wrap(text), expect)

        text = "Well, Doctor?\nWhat do you think?"
        self.check(wrapper.wrap(text), expect)

        text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
        expect = ['I say, chaps!  Anyone for "tennis?"  Hmmph!']
        self.check(wrapper.wrap(text), expect)

        wrapper.width = 20
        expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!']
        self.check(wrapper.wrap(text), expect)

        text = 'And she said, "Go to hell!"\nCan you believe that?'
        expect = ['And she said, "Go to', 'hell!"  Can you', 'believe that?']
        self.check(wrapper.wrap(text), expect)

        wrapper.width = 60
        expect = ['And she said, "Go to hell!"  Can you believe that?']
        self.check(wrapper.wrap(text), expect)

        text = 'File stdio.h is nice.'
        expect = ['File stdio.h is nice.']
        self.check(wrapper.wrap(text), expect)

    def test_wrap_short(self):
        # Wrapping to make short lines longer

        text = "This is a\nshort paragraph."

        self.check_wrap(text, 20, ["This is a short", "paragraph."])
        self.check_wrap(text, 40, ["This is a short paragraph."])

    def test_wrap_short_1line(self):
        # Test endcases

        text = "This is a short line."

        self.check_wrap(text, 30, ["This is a short line."])
        self.check_wrap(text,
                        30, ["(1) This is a short line."],
                        initial_indent="(1) ")

    def test_hyphenated(self):
        # Test breaking hyphenated words

        text = ("this-is-a-useful-feature-for-"
                "reformatting-posts-from-tim-peters'ly")

        self.check_wrap(text, 40, [
            "this-is-a-useful-feature-for-",
            "reformatting-posts-from-tim-peters'ly"
        ])
        self.check_wrap(text, 41, [
            "this-is-a-useful-feature-for-",
            "reformatting-posts-from-tim-peters'ly"
        ])
        self.check_wrap(text, 42, [
            "this-is-a-useful-feature-for-reformatting-",
            "posts-from-tim-peters'ly"
        ])

    def test_hyphenated_numbers(self):
        # Test that hyphenated numbers (eg. dates) are not broken like words.
        text = ("Python 1.0.0 was released on 1994-01-26.  Python 1.0.1 was\n"
                "released on 1994-02-15.")

        self.check_wrap(text, 35, [
            'Python 1.0.0 was released on', '1994-01-26.  Python 1.0.1 was',
            'released on 1994-02-15.'
        ])
        self.check_wrap(text, 40, [
            'Python 1.0.0 was released on 1994-01-26.',
            'Python 1.0.1 was released on 1994-02-15.'
        ])

        text = "I do all my shopping at 7-11."
        self.check_wrap(text, 25, ["I do all my shopping at", "7-11."])
        self.check_wrap(text, 27, ["I do all my shopping at", "7-11."])
        self.check_wrap(text, 29, ["I do all my shopping at 7-11."])

    def test_em_dash(self):
        # Test text with em-dashes
        text = "Em-dashes should be written -- thus."
        self.check_wrap(text, 25, ["Em-dashes should be", "written -- thus."])

        # Probe the boundaries of the properly written em-dash,
        # ie. " -- ".
        self.check_wrap(text, 29, ["Em-dashes should be written", "-- thus."])
        expect = ["Em-dashes should be written --", "thus."]
        self.check_wrap(text, 30, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 36, ["Em-dashes should be written -- thus."])

        # The improperly written em-dash is handled too, because
        # it's adjacent to non-whitespace on both sides.
        text = "You can also do--this or even---this."
        expect = ["You can also do", "--this or even", "---this."]
        self.check_wrap(text, 15, expect)
        self.check_wrap(text, 16, expect)
        expect = ["You can also do--", "this or even---", "this."]
        self.check_wrap(text, 17, expect)
        self.check_wrap(text, 19, expect)
        expect = ["You can also do--this or even", "---this."]
        self.check_wrap(text, 29, expect)
        self.check_wrap(text, 31, expect)
        expect = ["You can also do--this or even---", "this."]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 35, expect)

        # All of the above behaviour could be deduced by probing the
        # _split() method.
        text = "Here's an -- em-dash and--here's another---and another!"
        expect = [
            "Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", "and",
            "--", "here's", " ", "another", "---", "and", " ", "another!"
        ]
        self.check_split(text, expect)

        text = "and then--bam!--he was gone"
        expect = [
            "and", " ", "then", "--", "bam!", "--", "he", " ", "was", " ",
            "gone"
        ]
        self.check_split(text, expect)

    def test_unix_options(self):
        # Test that Unix-style command-line options are wrapped correctly.
        # Both Optik (OptionParser) and Docutils rely on this behaviour!

        text = "You should use the -n option, or --dry-run in its long form."
        self.check_wrap(text, 20, [
            "You should use the", "-n option, or --dry-", "run in its long",
            "form."
        ])
        self.check_wrap(text, 21, [
            "You should use the -n", "option, or --dry-run",
            "in its long form."
        ])
        expect = [
            "You should use the -n option, or", "--dry-run in its long form."
        ]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 34, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 38, expect)
        expect = [
            "You should use the -n option, or --dry-", "run in its long form."
        ]
        self.check_wrap(text, 39, expect)
        self.check_wrap(text, 41, expect)
        expect = [
            "You should use the -n option, or --dry-run", "in its long form."
        ]
        self.check_wrap(text, 42, expect)

        # Again, all of the above can be deduced from _split().
        text = "the -n option, or --dry-run or --dryrun"
        expect = [
            "the", " ", "-n", " ", "option,", " ", "or", " ", "--dry-", "run",
            " ", "or", " ", "--dryrun"
        ]
        self.check_split(text, expect)

    def test_funky_hyphens(self):
        # Screwy edge cases cooked up by David Goodger.  All reported
        # in SF bug #596434.
        self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
        self.check_split("what the--", ["what", " ", "the--"])
        self.check_split("what the--.", ["what", " ", "the--."])
        self.check_split("--text--.", ["--text--."])

        # When I first read bug #596434, this is what I thought David
        # was talking about.  I was wrong; these have always worked
        # fine.  The real problem is tested in test_funky_parens()
        # below...
        self.check_split("--option", ["--option"])
        self.check_split("--option-opt", ["--option-", "opt"])
        self.check_split("foo --option-opt bar",
                         ["foo", " ", "--option-", "opt", " ", "bar"])

    def test_punct_hyphens(self):
        # Oh bother, SF #965425 found another problem with hyphens --
        # hyphenated words in single quotes weren't handled correctly.
        # In fact, the bug is that *any* punctuation around a hyphenated
        # word was handled incorrectly, except for a leading "--", which
        # was special-cased for Optik and Docutils.  So test a variety
        # of styles of punctuation around a hyphenated word.
        # (Actually this is based on an Optik bug report, #813077).
        self.check_split("the 'wibble-wobble' widget",
                         ['the', ' ', "'wibble-", "wobble'", ' ', 'widget'])
        self.check_split('the "wibble-wobble" widget',
                         ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget'])
        self.check_split("the (wibble-wobble) widget",
                         ['the', ' ', "(wibble-", "wobble)", ' ', 'widget'])
        self.check_split("the ['wibble-wobble'] widget",
                         ['the', ' ', "['wibble-", "wobble']", ' ', 'widget'])

    def test_funky_parens(self):
        # Second part of SF bug #596434: long option strings inside
        # parentheses.
        self.check_split("foo (--option) bar",
                         ["foo", " ", "(--option)", " ", "bar"])

        # Related stuff -- make sure parens work in simpler contexts.
        self.check_split("foo (bar) baz", ["foo", " ", "(bar)", " ", "baz"])
        self.check_split("blah (ding dong), wubba",
                         ["blah", " ", "(ding", " ", "dong),", " ", "wubba"])

    def test_initial_whitespace(self):
        # SF bug #622849 reported inconsistent handling of leading
        # whitespace; let's test that a bit, shall we?
        text = " This is a sentence with leading whitespace."
        self.check_wrap(text, 50,
                        [" This is a sentence with leading whitespace."])
        self.check_wrap(text, 30,
                        [" This is a sentence with", "leading whitespace."])

    def test_no_drop_whitespace(self):
        # SF patch #1581073
        text = " This is a    sentence with     much whitespace."
        self.check_wrap(text,
                        10, [
                            " This is a", "    ", "sentence ", "with     ",
                            "much white", "space."
                        ],
                        drop_whitespace=False)

    if test_support.have_unicode:

        def test_unicode(self):
            # *Very* simple test of wrapping Unicode strings.  I'm sure
            # there's more to it than this, but let's at least make
            # sure textwrap doesn't crash on Unicode input!
            text = u"Hello there, how are you today?"
            self.check_wrap(text, 50, [u"Hello there, how are you today?"])
            self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
            olines = self.wrapper.wrap(text)
            self.assertIsInstance(olines, list)
            self.assertIsInstance(olines[0], unicode)
            otext = self.wrapper.fill(text)
            self.assertIsInstance(otext, unicode)

        def test_no_split_at_umlaut(self):
            text = u"Die Empf\xe4nger-Auswahl"
            self.check_wrap(text, 13, [u"Die", u"Empf\xe4nger-", u"Auswahl"])

        def test_umlaut_followed_by_dash(self):
            text = u"aa \xe4\xe4-\xe4\xe4"
            self.check_wrap(text, 7, [u"aa \xe4\xe4-", u"\xe4\xe4"])

    def test_split(self):
        # Ensure that the standard _split() method works as advertised
        # in the comments

        text = "Hello there -- you goof-ball, use the -b option!"

        result = self.wrapper._split(text)
        self.check(result, [
            "Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
            "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"
        ])

    def test_break_on_hyphens(self):
        # Ensure that the break_on_hyphens attributes work
        text = "yaba daba-doo"
        self.check_wrap(text, 10, ["yaba daba-", "doo"], break_on_hyphens=True)
        self.check_wrap(text, 10, ["yaba", "daba-doo"], break_on_hyphens=False)

    def test_bad_width(self):
        # Ensure that width <= 0 is caught.
        text = "Whatever, it doesn't matter."
        self.assertRaises(ValueError, wrap, text, 0)
        self.assertRaises(ValueError, wrap, text, -1)
예제 #4
0
class WrapTestCase(BaseTestCase):

    def setUp(self):
        self.wrapper = TextWrapper(width=45)

    def test_simple(self):
        # Simple case: just words, spaces, and a bit of punctuation

        text = "Hello there, how are you this fine day?  I'm glad to hear it!"

        self.check_wrap(text, 12,
                        ["Hello there,",
                         "how are you",
                         "this fine",
                         "day?  I'm",
                         "glad to hear",
                         "it!"])
        self.check_wrap(text, 42,
                        ["Hello there, how are you this fine day?",
                         "I'm glad to hear it!"])
        self.check_wrap(text, 80, [text])

    def test_empty_string(self):
        # Check that wrapping the empty string returns an empty list.
        self.check_wrap("", 6, [])
        self.check_wrap("", 6, [], drop_whitespace=False)

    def test_empty_string_with_initial_indent(self):
        # Check that the empty string is not indented.
        self.check_wrap("", 6, [], initial_indent="++")
        self.check_wrap("", 6, [], initial_indent="++", drop_whitespace=False)

    def test_whitespace(self):
        # Whitespace munging and end-of-sentence detection

        text = """\
This is a paragraph that already has
line breaks.  But some of its lines are much longer than the others,
so it needs to be wrapped.
Some lines are \ttabbed too.
What a mess!
"""

        expect = ["This is a paragraph that already has line",
                  "breaks.  But some of its lines are much",
                  "longer than the others, so it needs to be",
                  "wrapped.  Some lines are  tabbed too.  What a",
                  "mess!"]

        wrapper = TextWrapper(45, fix_sentence_endings=True)
        result = wrapper.wrap(text)
        self.check(result, expect)

        result = wrapper.fill(text)
        self.check(result, '\n'.join(expect))

    def test_fix_sentence_endings(self):
        wrapper = TextWrapper(60, fix_sentence_endings=True)

        # SF #847346: ensure that fix_sentence_endings=True does the
        # right thing even on input short enough that it doesn't need to
        # be wrapped.
        text = "A short line. Note the single space."
        expect = ["A short line.  Note the single space."]
        self.check(wrapper.wrap(text), expect)

        # Test some of the hairy end cases that _fix_sentence_endings()
        # is supposed to handle (the easy stuff is tested in
        # test_whitespace() above).
        text = "Well, Doctor? What do you think?"
        expect = ["Well, Doctor?  What do you think?"]
        self.check(wrapper.wrap(text), expect)

        text = "Well, Doctor?\nWhat do you think?"
        self.check(wrapper.wrap(text), expect)

        text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
        expect = ['I say, chaps!  Anyone for "tennis?"  Hmmph!']
        self.check(wrapper.wrap(text), expect)

        wrapper.width = 20
        expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!']
        self.check(wrapper.wrap(text), expect)

        text = 'And she said, "Go to hell!"\nCan you believe that?'
        expect = ['And she said, "Go to',
                  'hell!"  Can you',
                  'believe that?']
        self.check(wrapper.wrap(text), expect)

        wrapper.width = 60
        expect = ['And she said, "Go to hell!"  Can you believe that?']
        self.check(wrapper.wrap(text), expect)

        text = 'File stdio.h is nice.'
        expect = ['File stdio.h is nice.']
        self.check(wrapper.wrap(text), expect)

    def test_wrap_short(self):
        # Wrapping to make short lines longer

        text = "This is a\nshort paragraph."

        self.check_wrap(text, 20, ["This is a short",
                                   "paragraph."])
        self.check_wrap(text, 40, ["This is a short paragraph."])


    def test_wrap_short_1line(self):
        # Test endcases

        text = "This is a short line."

        self.check_wrap(text, 30, ["This is a short line."])
        self.check_wrap(text, 30, ["(1) This is a short line."],
                        initial_indent="(1) ")


    def test_hyphenated(self):
        # Test breaking hyphenated words

        text = ("this-is-a-useful-feature-for-"
                "reformatting-posts-from-tim-peters'ly")

        self.check_wrap(text, 40,
                        ["this-is-a-useful-feature-for-",
                         "reformatting-posts-from-tim-peters'ly"])
        self.check_wrap(text, 41,
                        ["this-is-a-useful-feature-for-",
                         "reformatting-posts-from-tim-peters'ly"])
        self.check_wrap(text, 42,
                        ["this-is-a-useful-feature-for-reformatting-",
                         "posts-from-tim-peters'ly"])

    def test_hyphenated_numbers(self):
        # Test that hyphenated numbers (eg. dates) are not broken like words.
        text = ("Python 1.0.0 was released on 1994-01-26.  Python 1.0.1 was\n"
                "released on 1994-02-15.")

        self.check_wrap(text, 30, ['Python 1.0.0 was released on',
                                   '1994-01-26.  Python 1.0.1 was',
                                   'released on 1994-02-15.'])
        self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.',
                                   'Python 1.0.1 was released on 1994-02-15.'])

        text = "I do all my shopping at 7-11."
        self.check_wrap(text, 25, ["I do all my shopping at",
                                   "7-11."])
        self.check_wrap(text, 27, ["I do all my shopping at",
                                   "7-11."])
        self.check_wrap(text, 29, ["I do all my shopping at 7-11."])

    def test_em_dash(self):
        # Test text with em-dashes
        text = "Em-dashes should be written -- thus."
        self.check_wrap(text, 25,
                        ["Em-dashes should be",
                         "written -- thus."])

        # Probe the boundaries of the properly written em-dash,
        # ie. " -- ".
        self.check_wrap(text, 29,
                        ["Em-dashes should be written",
                         "-- thus."])
        expect = ["Em-dashes should be written --",
                  "thus."]
        self.check_wrap(text, 30, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 36,
                        ["Em-dashes should be written -- thus."])

        # The improperly written em-dash is handled too, because
        # it's adjacent to non-whitespace on both sides.
        text = "You can also do--this or even---this."
        expect = ["You can also do",
                  "--this or even",
                  "---this."]
        self.check_wrap(text, 15, expect)
        self.check_wrap(text, 16, expect)
        expect = ["You can also do--",
                  "this or even---",
                  "this."]
        self.check_wrap(text, 17, expect)
        self.check_wrap(text, 19, expect)
        expect = ["You can also do--this or even",
                  "---this."]
        self.check_wrap(text, 29, expect)
        self.check_wrap(text, 31, expect)
        expect = ["You can also do--this or even---",
                  "this."]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 35, expect)

        # All of the above behaviour could be deduced by probing the
        # _split() method.
        text = "Here's an -- em-dash and--here's another---and another!"
        expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
                  "and", "--", "here's", " ", "another", "---",
                  "and", " ", "another!"]
        self.check_split(text, expect)

        text = "and then--bam!--he was gone"
        expect = ["and", " ", "then", "--", "bam!", "--",
                  "he", " ", "was", " ", "gone"]
        self.check_split(text, expect)


    def test_unix_options (self):
        # Test that Unix-style command-line options are wrapped correctly.
        # Both Optik (OptionParser) and Docutils rely on this behaviour!

        text = "You should use the -n option, or --dry-run in its long form."
        self.check_wrap(text, 20,
                        ["You should use the",
                         "-n option, or --dry-",
                         "run in its long",
                         "form."])
        self.check_wrap(text, 21,
                        ["You should use the -n",
                         "option, or --dry-run",
                         "in its long form."])
        expect = ["You should use the -n option, or",
                  "--dry-run in its long form."]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 34, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 38, expect)
        expect = ["You should use the -n option, or --dry-",
                  "run in its long form."]
        self.check_wrap(text, 39, expect)
        self.check_wrap(text, 41, expect)
        expect = ["You should use the -n option, or --dry-run",
                  "in its long form."]
        self.check_wrap(text, 42, expect)

        # Again, all of the above can be deduced from _split().
        text = "the -n option, or --dry-run or --dryrun"
        expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
                  "--dry-", "run", " ", "or", " ", "--dryrun"]
        self.check_split(text, expect)

    def test_funky_hyphens (self):
        # Screwy edge cases cooked up by David Goodger.  All reported
        # in SF bug #596434.
        self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
        self.check_split("what the--", ["what", " ", "the--"])
        self.check_split("what the--.", ["what", " ", "the--."])
        self.check_split("--text--.", ["--text--."])

        # When I first read bug #596434, this is what I thought David
        # was talking about.  I was wrong; these have always worked
        # fine.  The real problem is tested in test_funky_parens()
        # below...
        self.check_split("--option", ["--option"])
        self.check_split("--option-opt", ["--option-", "opt"])
        self.check_split("foo --option-opt bar",
                         ["foo", " ", "--option-", "opt", " ", "bar"])

    def test_punct_hyphens(self):
        # Oh bother, SF #965425 found another problem with hyphens --
        # hyphenated words in single quotes weren't handled correctly.
        # In fact, the bug is that *any* punctuation around a hyphenated
        # word was handled incorrectly, except for a leading "--", which
        # was special-cased for Optik and Docutils.  So test a variety
        # of styles of punctuation around a hyphenated word.
        # (Actually this is based on an Optik bug report, #813077).
        self.check_split("the 'wibble-wobble' widget",
                         ['the', ' ', "'wibble-", "wobble'", ' ', 'widget'])
        self.check_split('the "wibble-wobble" widget',
                         ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget'])
        self.check_split("the (wibble-wobble) widget",
                         ['the', ' ', "(wibble-", "wobble)", ' ', 'widget'])
        self.check_split("the ['wibble-wobble'] widget",
                         ['the', ' ', "['wibble-", "wobble']", ' ', 'widget'])

    def test_funky_parens (self):
        # Second part of SF bug #596434: long option strings inside
        # parentheses.
        self.check_split("foo (--option) bar",
                         ["foo", " ", "(--option)", " ", "bar"])

        # Related stuff -- make sure parens work in simpler contexts.
        self.check_split("foo (bar) baz",
                         ["foo", " ", "(bar)", " ", "baz"])
        self.check_split("blah (ding dong), wubba",
                         ["blah", " ", "(ding", " ", "dong),",
                          " ", "wubba"])

    def test_drop_whitespace_false(self):
        # Check that drop_whitespace=False preserves whitespace.
        # SF patch #1581073
        text = " This is a    sentence with     much whitespace."
        self.check_wrap(text, 10,
                        [" This is a", "    ", "sentence ",
                         "with     ", "much white", "space."],
                        drop_whitespace=False)

    def test_drop_whitespace_false_whitespace_only(self):
        # Check that drop_whitespace=False preserves a whitespace-only string.
        self.check_wrap("   ", 6, ["   "], drop_whitespace=False)

    def test_drop_whitespace_false_whitespace_only_with_indent(self):
        # Check that a whitespace-only string gets indented (when
        # drop_whitespace is False).
        self.check_wrap("   ", 6, ["     "], drop_whitespace=False,
                        initial_indent="  ")

    def test_drop_whitespace_whitespace_only(self):
        # Check drop_whitespace on a whitespace-only string.
        self.check_wrap("  ", 6, [])

    def test_drop_whitespace_leading_whitespace(self):
        # Check that drop_whitespace does not drop leading whitespace (if
        # followed by non-whitespace).
        # SF bug #622849 reported inconsistent handling of leading
        # whitespace; let's test that a bit, shall we?
        text = " This is a sentence with leading whitespace."
        self.check_wrap(text, 50,
                        [" This is a sentence with leading whitespace."])
        self.check_wrap(text, 30,
                        [" This is a sentence with", "leading whitespace."])

    def test_drop_whitespace_whitespace_line(self):
        # Check that drop_whitespace skips the whole line if a non-leading
        # line consists only of whitespace.
        text = "abcd    efgh"
        # Include the result for drop_whitespace=False for comparison.
        self.check_wrap(text, 6, ["abcd", "    ", "efgh"],
                        drop_whitespace=False)
        self.check_wrap(text, 6, ["abcd", "efgh"])

    def test_drop_whitespace_whitespace_only_with_indent(self):
        # Check that initial_indent is not applied to a whitespace-only
        # string.  This checks a special case of the fact that dropping
        # whitespace occurs before indenting.
        self.check_wrap("  ", 6, [], initial_indent="++")

    def test_drop_whitespace_whitespace_indent(self):
        # Check that drop_whitespace does not drop whitespace indents.
        # This checks a special case of the fact that dropping whitespace
        # occurs before indenting.
        self.check_wrap("abcd efgh", 6, ["  abcd", "  efgh"],
                        initial_indent="  ", subsequent_indent="  ")

    def test_split(self):
        # Ensure that the standard _split() method works as advertised
        # in the comments

        text = "Hello there -- you goof-ball, use the -b option!"

        result = self.wrapper._split(text)
        self.check(result,
             ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
              "ball,", " ", "use", " ", "the", " ", "-b", " ",  "option!"])

    def test_break_on_hyphens(self):
        # Ensure that the break_on_hyphens attributes work
        text = "yaba daba-doo"
        self.check_wrap(text, 10, ["yaba daba-", "doo"],
                        break_on_hyphens=True)
        self.check_wrap(text, 10, ["yaba", "daba-doo"],
                        break_on_hyphens=False)

    def test_bad_width(self):
        # Ensure that width <= 0 is caught.
        text = "Whatever, it doesn't matter."
        self.assertRaises(ValueError, wrap, text, 0)
        self.assertRaises(ValueError, wrap, text, -1)

    def test_no_split_at_umlaut(self):
        text = "Die Empf\xe4nger-Auswahl"
        self.check_wrap(text, 13, ["Die", "Empf\xe4nger-", "Auswahl"])

    def test_umlaut_followed_by_dash(self):
        text = "aa \xe4\xe4-\xe4\xe4"
        self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"])
예제 #5
0
class WrapTestCase(BaseTestCase):
    def setUp(self):
        self.wrapper = TextWrapper(width=45)

    def test_simple(self):
        # Simple case: just words, spaces, and a bit of punctuation

        text = "Hello there, how are you this fine day?  I'm glad to hear it!"

        self.check_wrap(text, 12, ["Hello there,", "how are you", "this fine", "day?  I'm", "glad to hear", "it!"])
        self.check_wrap(text, 42, ["Hello there, how are you this fine day?", "I'm glad to hear it!"])
        self.check_wrap(text, 80, [text])

    def test_whitespace(self):
        # Whitespace munging and end-of-sentence detection

        text = """\
This is a paragraph that already has
line breaks.  But some of its lines are much longer than the others,
so it needs to be wrapped.
Some lines are \ttabbed too.
What a mess!
"""

        expect = [
            "This is a paragraph that already has line",
            "breaks.  But some of its lines are much",
            "longer than the others, so it needs to be",
            "wrapped.  Some lines are  tabbed too.  What a",
            "mess!",
        ]

        wrapper = TextWrapper(45, fix_sentence_endings=True)
        result = wrapper.wrap(text)
        self.check(result, expect)

        result = wrapper.fill(text)
        self.check(result, "\n".join(expect))

    def test_fix_sentence_endings(self):
        wrapper = TextWrapper(60, fix_sentence_endings=True)

        # SF #847346: ensure that fix_sentence_endings=True does the
        # right thing even on input short enough that it doesn't need to
        # be wrapped.
        text = "A short line. Note the single space."
        expect = ["A short line.  Note the single space."]
        self.check(wrapper.wrap(text), expect)

        # Test some of the hairy end cases that _fix_sentence_endings()
        # is supposed to handle (the easy stuff is tested in
        # test_whitespace() above).
        text = "Well, Doctor? What do you think?"
        expect = ["Well, Doctor?  What do you think?"]
        self.check(wrapper.wrap(text), expect)

        text = "Well, Doctor?\nWhat do you think?"
        self.check(wrapper.wrap(text), expect)

        text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
        expect = ['I say, chaps!  Anyone for "tennis?"  Hmmph!']
        self.check(wrapper.wrap(text), expect)

        wrapper.width = 20
        expect = ["I say, chaps!", 'Anyone for "tennis?"', "Hmmph!"]
        self.check(wrapper.wrap(text), expect)

        text = 'And she said, "Go to hell!"\nCan you believe that?'
        expect = ['And she said, "Go to', 'hell!"  Can you', "believe that?"]
        self.check(wrapper.wrap(text), expect)

        wrapper.width = 60
        expect = ['And she said, "Go to hell!"  Can you believe that?']
        self.check(wrapper.wrap(text), expect)

    def test_wrap_short(self):
        # Wrapping to make short lines longer

        text = "This is a\nshort paragraph."

        self.check_wrap(text, 20, ["This is a short", "paragraph."])
        self.check_wrap(text, 40, ["This is a short paragraph."])

    def test_wrap_short_1line(self):
        # Test endcases

        text = "This is a short line."

        self.check_wrap(text, 30, ["This is a short line."])
        self.check_wrap(text, 30, ["(1) This is a short line."], initial_indent="(1) ")

    def test_hyphenated(self):
        # Test breaking hyphenated words

        text = "this-is-a-useful-feature-for-" "reformatting-posts-from-tim-peters'ly"

        self.check_wrap(text, 40, ["this-is-a-useful-feature-for-", "reformatting-posts-from-tim-peters'ly"])
        self.check_wrap(text, 41, ["this-is-a-useful-feature-for-", "reformatting-posts-from-tim-peters'ly"])
        self.check_wrap(text, 42, ["this-is-a-useful-feature-for-reformatting-", "posts-from-tim-peters'ly"])

    def test_hyphenated_numbers(self):
        # Test that hyphenated numbers (eg. dates) are not broken like words.
        text = "Python 1.0.0 was released on 1994-01-26.  Python 1.0.1 was\n" "released on 1994-02-15."

        self.check_wrap(
            text, 30, ["Python 1.0.0 was released on", "1994-01-26.  Python 1.0.1 was", "released on 1994-02-15."]
        )
        self.check_wrap(
            text, 40, ["Python 1.0.0 was released on 1994-01-26.", "Python 1.0.1 was released on 1994-02-15."]
        )

        text = "I do all my shopping at 7-11."
        self.check_wrap(text, 25, ["I do all my shopping at", "7-11."])
        self.check_wrap(text, 27, ["I do all my shopping at", "7-11."])
        self.check_wrap(text, 29, ["I do all my shopping at 7-11."])

    def test_em_dash(self):
        # Test text with em-dashes
        text = "Em-dashes should be written -- thus."
        self.check_wrap(text, 25, ["Em-dashes should be", "written -- thus."])

        # Probe the boundaries of the properly written em-dash,
        # ie. " -- ".
        self.check_wrap(text, 29, ["Em-dashes should be written", "-- thus."])
        expect = ["Em-dashes should be written --", "thus."]
        self.check_wrap(text, 30, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 36, ["Em-dashes should be written -- thus."])

        # The improperly written em-dash is handled too, because
        # it's adjacent to non-whitespace on both sides.
        text = "You can also do--this or even---this."
        expect = ["You can also do", "--this or even", "---this."]
        self.check_wrap(text, 15, expect)
        self.check_wrap(text, 16, expect)
        expect = ["You can also do--", "this or even---", "this."]
        self.check_wrap(text, 17, expect)
        self.check_wrap(text, 19, expect)
        expect = ["You can also do--this or even", "---this."]
        self.check_wrap(text, 29, expect)
        self.check_wrap(text, 31, expect)
        expect = ["You can also do--this or even---", "this."]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 35, expect)

        # All of the above behaviour could be deduced by probing the
        # _split() method.
        text = "Here's an -- em-dash and--here's another---and another!"
        expect = [
            "Here's",
            " ",
            "an",
            " ",
            "--",
            " ",
            "em-",
            "dash",
            " ",
            "and",
            "--",
            "here's",
            " ",
            "another",
            "---",
            "and",
            " ",
            "another!",
        ]
        self.check_split(text, expect)

        text = "and then--bam!--he was gone"
        expect = ["and", " ", "then", "--", "bam!", "--", "he", " ", "was", " ", "gone"]
        self.check_split(text, expect)

    def test_unix_options(self):
        # Test that Unix-style command-line options are wrapped correctly.
        # Both Optik (OptionParser) and Docutils rely on this behaviour!

        text = "You should use the -n option, or --dry-run in its long form."
        self.check_wrap(text, 20, ["You should use the", "-n option, or --dry-", "run in its long", "form."])
        self.check_wrap(text, 21, ["You should use the -n", "option, or --dry-run", "in its long form."])
        expect = ["You should use the -n option, or", "--dry-run in its long form."]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 34, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 38, expect)
        expect = ["You should use the -n option, or --dry-", "run in its long form."]
        self.check_wrap(text, 39, expect)
        self.check_wrap(text, 41, expect)
        expect = ["You should use the -n option, or --dry-run", "in its long form."]
        self.check_wrap(text, 42, expect)

        # Again, all of the above can be deduced from _split().
        text = "the -n option, or --dry-run or --dryrun"
        expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", "--dry-", "run", " ", "or", " ", "--dryrun"]
        self.check_split(text, expect)

    def test_funky_hyphens(self):
        # Screwy edge cases cooked up by David Goodger.  All reported
        # in SF bug #596434.
        self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
        self.check_split("what the--", ["what", " ", "the--"])
        self.check_split("what the--.", ["what", " ", "the--."])
        self.check_split("--text--.", ["--text--."])

        # When I first read bug #596434, this is what I thought David
        # was talking about.  I was wrong; these have always worked
        # fine.  The real problem is tested in test_funky_parens()
        # below...
        self.check_split("--option", ["--option"])
        self.check_split("--option-opt", ["--option-", "opt"])
        self.check_split("foo --option-opt bar", ["foo", " ", "--option-", "opt", " ", "bar"])

    def test_punct_hyphens(self):
        # Oh bother, SF #965425 found another problem with hyphens --
        # hyphenated words in single quotes weren't handled correctly.
        # In fact, the bug is that *any* punctuation around a hyphenated
        # word was handled incorrectly, except for a leading "--", which
        # was special-cased for Optik and Docutils.  So test a variety
        # of styles of punctuation around a hyphenated word.
        # (Actually this is based on an Optik bug report, #813077).
        self.check_split("the 'wibble-wobble' widget", ["the", " ", "'wibble-", "wobble'", " ", "widget"])
        self.check_split('the "wibble-wobble" widget', ["the", " ", '"wibble-', 'wobble"', " ", "widget"])
        self.check_split("the (wibble-wobble) widget", ["the", " ", "(wibble-", "wobble)", " ", "widget"])
        self.check_split("the ['wibble-wobble'] widget", ["the", " ", "['wibble-", "wobble']", " ", "widget"])

    def test_funky_parens(self):
        # Second part of SF bug #596434: long option strings inside
        # parentheses.
        self.check_split("foo (--option) bar", ["foo", " ", "(--option)", " ", "bar"])

        # Related stuff -- make sure parens work in simpler contexts.
        self.check_split("foo (bar) baz", ["foo", " ", "(bar)", " ", "baz"])
        self.check_split("blah (ding dong), wubba", ["blah", " ", "(ding", " ", "dong),", " ", "wubba"])

    def test_initial_whitespace(self):
        # SF bug #622849 reported inconsistent handling of leading
        # whitespace; let's test that a bit, shall we?
        text = " This is a sentence with leading whitespace."
        self.check_wrap(text, 50, [" This is a sentence with leading whitespace."])
        self.check_wrap(text, 30, [" This is a sentence with", "leading whitespace."])

    if test_support.have_unicode:

        def test_unicode(self):
            # *Very* simple test of wrapping Unicode strings.  I'm sure
            # there's more to it than this, but let's at least make
            # sure textwrap doesn't crash on Unicode input!
            text = u"Hello there, how are you today?"
            self.check_wrap(text, 50, [u"Hello there, how are you today?"])
            self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
            olines = self.wrapper.wrap(text)
            assert isinstance(olines, list) and isinstance(olines[0], unicode)
            otext = self.wrapper.fill(text)
            assert isinstance(otext, unicode)

    def test_split(self):
        # Ensure that the standard _split() method works as advertised
        # in the comments

        text = "Hello there -- you goof-ball, use the -b option!"

        result = self.wrapper._split(text)
        self.check(
            result,
            [
                "Hello",
                " ",
                "there",
                " ",
                "--",
                " ",
                "you",
                " ",
                "goof-",
                "ball,",
                " ",
                "use",
                " ",
                "the",
                " ",
                "-b",
                " ",
                "option!",
            ],
        )

    def test_bad_width(self):
        # Ensure that width <= 0 is caught.
        text = "Whatever, it doesn't matter."
        self.assertRaises(ValueError, wrap, text, 0)
        self.assertRaises(ValueError, wrap, text, -1)
class WrapTestCase(BaseTestCase):

    def setUp(self):
        self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)

    def test_simple(self):
        # Simple case: just words, spaces, and a bit of punctuation

        text = "Hello there, how are you this fine day?  I'm glad to hear it!"

        self.check_wrap(text, 12,
                        ["Hello there,",
                         "how are you",
                         "this fine",
                         "day?  I'm",
                         "glad to hear",
                         "it!"])
        self.check_wrap(text, 42,
                        ["Hello there, how are you this fine day?",
                         "I'm glad to hear it!"])
        self.check_wrap(text, 80, [text])


    def test_whitespace(self):
        # Whitespace munging and end-of-sentence detection

        text = """\
This is a paragraph that already has
line breaks.  But some of its lines are much longer than the others,
so it needs to be wrapped.
Some lines are \ttabbed too.
What a mess!
"""

        expect = ["This is a paragraph that already has line",
                  "breaks.  But some of its lines are much",
                  "longer than the others, so it needs to be",
                  "wrapped.  Some lines are  tabbed too.  What a",
                  "mess!"]

        result = self.wrapper.wrap(text)
        self.check(result, expect)

        result = self.wrapper.fill(text)
        self.check(result, '\n'.join(expect))


    def test_wrap_short(self):
        # Wrapping to make short lines longer

        text = "This is a\nshort paragraph."

        self.check_wrap(text, 20, ["This is a short",
                                   "paragraph."])
        self.check_wrap(text, 40, ["This is a short paragraph."])


    def test_wrap_short_1line(self):
        # Test endcases

        text = "This is a short line."

        self.check_wrap(text, 30, ["This is a short line."])
        self.check_wrap(text, 30, ["(1) This is a short line."],
                        initial_indent="(1) ")


    def test_hyphenated(self):
        # Test breaking hyphenated words

        text = ("this-is-a-useful-feature-for-"
                "reformatting-posts-from-tim-peters'ly")

        self.check_wrap(text, 40,
                        ["this-is-a-useful-feature-for-",
                         "reformatting-posts-from-tim-peters'ly"])
        self.check_wrap(text, 41,
                        ["this-is-a-useful-feature-for-",
                         "reformatting-posts-from-tim-peters'ly"])
        self.check_wrap(text, 42,
                        ["this-is-a-useful-feature-for-reformatting-",
                         "posts-from-tim-peters'ly"])

    def test_em_dash(self):
        # Test text with em-dashes
        text = "Em-dashes should be written -- thus."
        self.check_wrap(text, 25,
                        ["Em-dashes should be",
                         "written -- thus."])

        # Probe the boundaries of the properly written em-dash,
        # ie. " -- ".
        self.check_wrap(text, 29,
                        ["Em-dashes should be written",
                         "-- thus."])
        expect = ["Em-dashes should be written --",
                  "thus."]
        self.check_wrap(text, 30, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 36,
                        ["Em-dashes should be written -- thus."])

        # The improperly written em-dash is handled too, because
        # it's adjacent to non-whitespace on both sides.
        text = "You can also do--this or even---this."
        expect = ["You can also do",
                  "--this or even",
                  "---this."]
        self.check_wrap(text, 15, expect)
        self.check_wrap(text, 16, expect)
        expect = ["You can also do--",
                  "this or even---",
                  "this."]
        self.check_wrap(text, 17, expect)
        self.check_wrap(text, 19, expect)
        expect = ["You can also do--this or even",
                  "---this."]
        self.check_wrap(text, 29, expect)
        self.check_wrap(text, 31, expect)
        expect = ["You can also do--this or even---",
                  "this."]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 35, expect)

        # All of the above behaviour could be deduced by probing the
        # _split() method.
        text = "Here's an -- em-dash and--here's another---and another!"
        expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
                  "and", "--", "here's", " ", "another", "---",
                  "and", " ", "another!"]
        self.check_split(text, expect)

        text = "and then--bam!--he was gone"
        expect = ["and", " ", "then", "--", "bam!", "--",
                  "he", " ", "was", " ", "gone"]
        self.check_split(text, expect)


    def test_unix_options (self):
        # Test that Unix-style command-line options are wrapped correctly.
        # Both Optik (OptionParser) and Docutils rely on this behaviour!

        text = "You should use the -n option, or --dry-run in its long form."
        self.check_wrap(text, 20,
                        ["You should use the",
                         "-n option, or --dry-",
                         "run in its long",
                         "form."])
        self.check_wrap(text, 21,
                        ["You should use the -n",
                         "option, or --dry-run",
                         "in its long form."])
        expect = ["You should use the -n option, or",
                  "--dry-run in its long form."]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 34, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 38, expect)
        expect = ["You should use the -n option, or --dry-",
                  "run in its long form."]
        self.check_wrap(text, 39, expect)
        self.check_wrap(text, 41, expect)
        expect = ["You should use the -n option, or --dry-run",
                  "in its long form."]
        self.check_wrap(text, 42, expect)

        # Again, all of the above can be deduced from _split().
        text = "the -n option, or --dry-run or --dryrun"
        expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
                  "--dry-", "run", " ", "or", " ", "--dryrun"]
        self.check_split(text, expect)

    def test_funky_hyphens (self):
        # Screwy edge cases cooked up by David Goodger.  All reported
        # in SF bug #596434.
        self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
        self.check_split("what the--", ["what", " ", "the--"])
        self.check_split("what the--.", ["what", " ", "the--."])
        self.check_split("--text--.", ["--text--."])

        # When I first read bug #596434, this is what I thought David
        # was talking about.  I was wrong; these have always worked
        # fine.  The real problem is tested in test_funky_parens()
        # below...
        self.check_split("--option", ["--option"])
        self.check_split("--option-opt", ["--option-", "opt"])
        self.check_split("foo --option-opt bar",
                         ["foo", " ", "--option-", "opt", " ", "bar"])

    def test_funky_parens (self):
        # Second part of SF bug #596434: long option strings inside
        # parentheses.
        self.check_split("foo (--option) bar",
                         ["foo", " ", "(--option)", " ", "bar"])

        # Related stuff -- make sure parens work in simpler contexts.
        self.check_split("foo (bar) baz",
                         ["foo", " ", "(bar)", " ", "baz"])
        self.check_split("blah (ding dong), wubba",
                         ["blah", " ", "(ding", " ", "dong),",
                          " ", "wubba"])

    def test_initial_whitespace(self):
        # SF bug #622849 reported inconsistent handling of leading
        # whitespace; let's test that a bit, shall we?
        text = " This is a sentence with leading whitespace."
        self.check_wrap(text, 50,
                        [" This is a sentence with leading whitespace."])
        self.check_wrap(text, 30,
                        [" This is a sentence with", "leading whitespace."])

    def test_unicode(self):
        # *Very* simple test of wrapping Unicode strings.  I'm sure
        # there's more to it than this, but let's at least make
        # sure textwrap doesn't crash on Unicode input!
        text = u"Hello there, how are you today?"
        self.check_wrap(text, 50, [u"Hello there, how are you today?"])
        self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
        olines = self.wrapper.wrap(text)
        assert isinstance(olines, list) and isinstance(olines[0], unicode)
        otext = self.wrapper.fill(text)
        assert isinstance(otext, unicode)

    def test_split(self):
        # Ensure that the standard _split() method works as advertised
        # in the comments

        text = "Hello there -- you goof-ball, use the -b option!"

        result = self.wrapper._split(text)
        self.check(result,
             ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
              "ball,", " ", "use", " ", "the", " ", "-b", " ",  "option!"])

    def test_bad_width(self):
        # Ensure that width <= 0 is caught.
        text = "Whatever, it doesn't matter."
        self.assertRaises(ValueError, wrap, text, 0)
        self.assertRaises(ValueError, wrap, text, -1)
예제 #7
0
class WrapTestCase(BaseTestCase):
    def setUp(self):
        self.wrapper = TextWrapper(width=45)

    def test_simple(self):
        text = "Hello there, how are you this fine day?  I'm glad to hear it!"
        self.check_wrap(text, 12, [
            'Hello there,', 'how are you', 'this fine', "day?  I'm",
            'glad to hear', 'it!'
        ])
        self.check_wrap(text, 42, [
            'Hello there, how are you this fine day?', "I'm glad to hear it!"
        ])
        self.check_wrap(text, 80, [text])

    def test_empty_string(self):
        self.check_wrap('', 6, [])
        self.check_wrap('', 6, [], drop_whitespace=False)

    def test_empty_string_with_initial_indent(self):
        self.check_wrap('', 6, [], initial_indent='++')
        self.check_wrap('', 6, [], initial_indent='++', drop_whitespace=False)

    def test_whitespace(self):
        text = """This is a paragraph that already has
line breaks.  But some of its lines are much longer than the others,
so it needs to be wrapped.
Some lines are 	tabbed too.
What a mess!
"""
        expect = [
            'This is a paragraph that already has line',
            'breaks.  But some of its lines are much',
            'longer than the others, so it needs to be',
            'wrapped.  Some lines are  tabbed too.  What a', 'mess!'
        ]
        wrapper = TextWrapper(45, fix_sentence_endings=True)
        result = wrapper.wrap(text)
        self.check(result, expect)
        result = wrapper.fill(text)
        self.check(result, '\n'.join(expect))
        text = '\tTest\tdefault\t\ttabsize.'
        expect = ['        Test    default         tabsize.']
        self.check_wrap(text, 80, expect)
        text = '\tTest\tcustom\t\ttabsize.'
        expect = ['    Test    custom      tabsize.']
        self.check_wrap(text, 80, expect, tabsize=4)

    def test_fix_sentence_endings(self):
        wrapper = TextWrapper(60, fix_sentence_endings=True)
        text = 'A short line. Note the single space.'
        expect = ['A short line.  Note the single space.']
        self.check(wrapper.wrap(text), expect)
        text = 'Well, Doctor? What do you think?'
        expect = ['Well, Doctor?  What do you think?']
        self.check(wrapper.wrap(text), expect)
        text = 'Well, Doctor?\nWhat do you think?'
        self.check(wrapper.wrap(text), expect)
        text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
        expect = ['I say, chaps!  Anyone for "tennis?"  Hmmph!']
        self.check(wrapper.wrap(text), expect)
        wrapper.width = 20
        expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!']
        self.check(wrapper.wrap(text), expect)
        text = 'And she said, "Go to hell!"\nCan you believe that?'
        expect = ['And she said, "Go to', 'hell!"  Can you', 'believe that?']
        self.check(wrapper.wrap(text), expect)
        wrapper.width = 60
        expect = ['And she said, "Go to hell!"  Can you believe that?']
        self.check(wrapper.wrap(text), expect)
        text = 'File stdio.h is nice.'
        expect = ['File stdio.h is nice.']
        self.check(wrapper.wrap(text), expect)

    def test_wrap_short(self):
        text = 'This is a\nshort paragraph.'
        self.check_wrap(text, 20, ['This is a short', 'paragraph.'])
        self.check_wrap(text, 40, ['This is a short paragraph.'])

    def test_wrap_short_1line(self):
        text = 'This is a short line.'
        self.check_wrap(text, 30, ['This is a short line.'])
        self.check_wrap(text,
                        30, ['(1) This is a short line.'],
                        initial_indent='(1) ')

    def test_hyphenated(self):
        text = (
            "this-is-a-useful-feature-for-reformatting-posts-from-tim-peters'ly"
        )
        self.check_wrap(text, 40, [
            'this-is-a-useful-feature-for-',
            "reformatting-posts-from-tim-peters'ly"
        ])
        self.check_wrap(text, 41, [
            'this-is-a-useful-feature-for-',
            "reformatting-posts-from-tim-peters'ly"
        ])
        self.check_wrap(text, 42, [
            'this-is-a-useful-feature-for-reformatting-',
            "posts-from-tim-peters'ly"
        ])
        expect = (
            "this-|is-|a-|useful-|feature-|for-|reformatting-|posts-|from-|tim-|peters'ly"
            .split('|'))
        self.check_wrap(text, 1, expect, break_long_words=False)
        self.check_split(text, expect)
        self.check_split('e-mail', ['e-mail'])
        self.check_split('Jelly-O', ['Jelly-O'])
        self.check_split('half-a-crown', 'half-|a-|crown'.split('|'))

    def test_hyphenated_numbers(self):
        text = """Python 1.0.0 was released on 1994-01-26.  Python 1.0.1 was
released on 1994-02-15."""
        self.check_wrap(text, 30, [
            'Python 1.0.0 was released on', '1994-01-26.  Python 1.0.1 was',
            'released on 1994-02-15.'
        ])
        self.check_wrap(text, 40, [
            'Python 1.0.0 was released on 1994-01-26.',
            'Python 1.0.1 was released on 1994-02-15.'
        ])
        self.check_wrap(text, 1, text.split(), break_long_words=False)
        text = 'I do all my shopping at 7-11.'
        self.check_wrap(text, 25, ['I do all my shopping at', '7-11.'])
        self.check_wrap(text, 27, ['I do all my shopping at', '7-11.'])
        self.check_wrap(text, 29, ['I do all my shopping at 7-11.'])
        self.check_wrap(text, 1, text.split(), break_long_words=False)

    def test_em_dash(self):
        text = 'Em-dashes should be written -- thus.'
        self.check_wrap(text, 25, ['Em-dashes should be', 'written -- thus.'])
        self.check_wrap(text, 29, ['Em-dashes should be written', '-- thus.'])
        expect = ['Em-dashes should be written --', 'thus.']
        self.check_wrap(text, 30, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 36, ['Em-dashes should be written -- thus.'])
        text = 'You can also do--this or even---this.'
        expect = ['You can also do', '--this or even', '---this.']
        self.check_wrap(text, 15, expect)
        self.check_wrap(text, 16, expect)
        expect = ['You can also do--', 'this or even---', 'this.']
        self.check_wrap(text, 17, expect)
        self.check_wrap(text, 19, expect)
        expect = ['You can also do--this or even', '---this.']
        self.check_wrap(text, 29, expect)
        self.check_wrap(text, 31, expect)
        expect = ['You can also do--this or even---', 'this.']
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 35, expect)
        text = "Here's an -- em-dash and--here's another---and another!"
        expect = [
            "Here's", ' ', 'an', ' ', '--', ' ', 'em-', 'dash', ' ', 'and',
            '--', "here's", ' ', 'another', '---', 'and', ' ', 'another!'
        ]
        self.check_split(text, expect)
        text = 'and then--bam!--he was gone'
        expect = [
            'and', ' ', 'then', '--', 'bam!', '--', 'he', ' ', 'was', ' ',
            'gone'
        ]
        self.check_split(text, expect)

    def test_unix_options(self):
        text = 'You should use the -n option, or --dry-run in its long form.'
        self.check_wrap(text, 20, [
            'You should use the', '-n option, or --dry-', 'run in its long',
            'form.'
        ])
        self.check_wrap(text, 21, [
            'You should use the -n', 'option, or --dry-run',
            'in its long form.'
        ])
        expect = [
            'You should use the -n option, or', '--dry-run in its long form.'
        ]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 34, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 38, expect)
        expect = [
            'You should use the -n option, or --dry-', 'run in its long form.'
        ]
        self.check_wrap(text, 39, expect)
        self.check_wrap(text, 41, expect)
        expect = [
            'You should use the -n option, or --dry-run', 'in its long form.'
        ]
        self.check_wrap(text, 42, expect)
        text = 'the -n option, or --dry-run or --dryrun'
        expect = [
            'the', ' ', '-n', ' ', 'option,', ' ', 'or', ' ', '--dry-', 'run',
            ' ', 'or', ' ', '--dryrun'
        ]
        self.check_split(text, expect)

    def test_funky_hyphens(self):
        self.check_split('what the--hey!', ['what', ' ', 'the', '--', 'hey!'])
        self.check_split('what the--', ['what', ' ', 'the--'])
        self.check_split('what the--.', ['what', ' ', 'the--.'])
        self.check_split('--text--.', ['--text--.'])
        self.check_split('--option', ['--option'])
        self.check_split('--option-opt', ['--option-', 'opt'])
        self.check_split('foo --option-opt bar',
                         ['foo', ' ', '--option-', 'opt', ' ', 'bar'])

    def test_punct_hyphens(self):
        self.check_split("the 'wibble-wobble' widget",
                         ['the', ' ', "'wibble-", "wobble'", ' ', 'widget'])
        self.check_split('the "wibble-wobble" widget',
                         ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget'])
        self.check_split('the (wibble-wobble) widget',
                         ['the', ' ', '(wibble-', 'wobble)', ' ', 'widget'])
        self.check_split("the ['wibble-wobble'] widget",
                         ['the', ' ', "['wibble-", "wobble']", ' ', 'widget'])
        self.check_split("what-d'you-call-it.",
                         "what-d'you-|call-|it.".split('|'))

    def test_funky_parens(self):
        self.check_split('foo (--option) bar',
                         ['foo', ' ', '(--option)', ' ', 'bar'])
        self.check_split('foo (bar) baz', ['foo', ' ', '(bar)', ' ', 'baz'])
        self.check_split('blah (ding dong), wubba',
                         ['blah', ' ', '(ding', ' ', 'dong),', ' ', 'wubba'])

    def test_drop_whitespace_false(self):
        text = ' This is a    sentence with     much whitespace.'
        self.check_wrap(text,
                        10, [
                            ' This is a', '    ', 'sentence ', 'with     ',
                            'much white', 'space.'
                        ],
                        drop_whitespace=False)

    def test_drop_whitespace_false_whitespace_only(self):
        self.check_wrap('   ', 6, ['   '], drop_whitespace=False)

    def test_drop_whitespace_false_whitespace_only_with_indent(self):
        self.check_wrap('   ',
                        6, ['     '],
                        drop_whitespace=False,
                        initial_indent='  ')

    def test_drop_whitespace_whitespace_only(self):
        self.check_wrap('  ', 6, [])

    def test_drop_whitespace_leading_whitespace(self):
        text = ' This is a sentence with leading whitespace.'
        self.check_wrap(text, 50,
                        [' This is a sentence with leading whitespace.'])
        self.check_wrap(text, 30,
                        [' This is a sentence with', 'leading whitespace.'])

    def test_drop_whitespace_whitespace_line(self):
        text = 'abcd    efgh'
        self.check_wrap(text,
                        6, ['abcd', '    ', 'efgh'],
                        drop_whitespace=False)
        self.check_wrap(text, 6, ['abcd', 'efgh'])

    def test_drop_whitespace_whitespace_only_with_indent(self):
        self.check_wrap('  ', 6, [], initial_indent='++')

    def test_drop_whitespace_whitespace_indent(self):
        self.check_wrap('abcd efgh',
                        6, ['  abcd', '  efgh'],
                        initial_indent='  ',
                        subsequent_indent='  ')

    def test_split(self):
        text = 'Hello there -- you goof-ball, use the -b option!'
        result = self.wrapper._split(text)
        self.check(result, [
            'Hello', ' ', 'there', ' ', '--', ' ', 'you', ' ', 'goof-',
            'ball,', ' ', 'use', ' ', 'the', ' ', '-b', ' ', 'option!'
        ])

    def test_break_on_hyphens(self):
        text = 'yaba daba-doo'
        self.check_wrap(text, 10, ['yaba daba-', 'doo'], break_on_hyphens=True)
        self.check_wrap(text, 10, ['yaba', 'daba-doo'], break_on_hyphens=False)

    def test_bad_width(self):
        text = "Whatever, it doesn't matter."
        self.assertRaises(ValueError, wrap, text, 0)
        self.assertRaises(ValueError, wrap, text, -1)

    def test_no_split_at_umlaut(self):
        text = 'Die Empfänger-Auswahl'
        self.check_wrap(text, 13, ['Die', 'Empfänger-', 'Auswahl'])

    def test_umlaut_followed_by_dash(self):
        text = 'aa ää-ää'
        self.check_wrap(text, 7, ['aa ää-', 'ää'])

    def test_non_breaking_space(self):
        text = 'This is a sentence with non-breaking\xa0space.'
        self.check_wrap(
            text,
            20, ['This is a sentence', 'with non-', 'breaking\xa0space.'],
            break_on_hyphens=True)
        self.check_wrap(
            text,
            20, ['This is a sentence', 'with', 'non-breaking\xa0space.'],
            break_on_hyphens=False)

    def test_narrow_non_breaking_space(self):
        text = 'This is a sentence with non-breaking\u202fspace.'
        self.check_wrap(
            text,
            20, ['This is a sentence', 'with non-', 'breaking\u202fspace.'],
            break_on_hyphens=True)
        self.check_wrap(
            text,
            20, ['This is a sentence', 'with', 'non-breaking\u202fspace.'],
            break_on_hyphens=False)
예제 #8
0
class WrapTestCase(BaseTestCase):
    def setUp(self):
        self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)

    def test_simple(self):
        # Simple case: just words, spaces, and a bit of punctuation

        text = "Hello there, how are you this fine day?  I'm glad to hear it!"

        self.check_wrap(text, 12, [
            "Hello there,", "how are you", "this fine", "day?  I'm",
            "glad to hear", "it!"
        ])
        self.check_wrap(text, 42, [
            "Hello there, how are you this fine day?", "I'm glad to hear it!"
        ])
        self.check_wrap(text, 80, [text])

    def test_whitespace(self):
        # Whitespace munging and end-of-sentence detection

        text = """\
This is a paragraph that already has
line breaks.  But some of its lines are much longer than the others,
so it needs to be wrapped.
Some lines are \ttabbed too.
What a mess!
"""

        expect = [
            "This is a paragraph that already has line",
            "breaks.  But some of its lines are much",
            "longer than the others, so it needs to be",
            "wrapped.  Some lines are  tabbed too.  What a", "mess!"
        ]

        result = self.wrapper.wrap(text)
        self.check(result, expect)

        result = self.wrapper.fill(text)
        self.check(result, '\n'.join(expect))

    def test_wrap_short(self):
        # Wrapping to make short lines longer

        text = "This is a\nshort paragraph."

        self.check_wrap(text, 20, ["This is a short", "paragraph."])
        self.check_wrap(text, 40, ["This is a short paragraph."])

    def test_wrap_short_1line(self):
        # Test endcases

        text = "This is a short line."

        self.check_wrap(text, 30, ["This is a short line."])
        self.check_wrap(text,
                        30, ["(1) This is a short line."],
                        initial_indent="(1) ")

    def test_hyphenated(self):
        # Test breaking hyphenated words

        text = ("this-is-a-useful-feature-for-"
                "reformatting-posts-from-tim-peters'ly")

        self.check_wrap(text, 40, [
            "this-is-a-useful-feature-for-",
            "reformatting-posts-from-tim-peters'ly"
        ])
        self.check_wrap(text, 41, [
            "this-is-a-useful-feature-for-",
            "reformatting-posts-from-tim-peters'ly"
        ])
        self.check_wrap(text, 42, [
            "this-is-a-useful-feature-for-reformatting-",
            "posts-from-tim-peters'ly"
        ])

    def test_em_dash(self):
        # Test text with em-dashes
        text = "Em-dashes should be written -- thus."
        self.check_wrap(text, 25, ["Em-dashes should be", "written -- thus."])

        # Probe the boundaries of the properly written em-dash,
        # ie. " -- ".
        self.check_wrap(text, 29, ["Em-dashes should be written", "-- thus."])
        expect = ["Em-dashes should be written --", "thus."]
        self.check_wrap(text, 30, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 36, ["Em-dashes should be written -- thus."])

        # The improperly written em-dash is handled too, because
        # it's adjacent to non-whitespace on both sides.
        text = "You can also do--this or even---this."
        expect = ["You can also do", "--this or even", "---this."]
        self.check_wrap(text, 15, expect)
        self.check_wrap(text, 16, expect)
        expect = ["You can also do--", "this or even---", "this."]
        self.check_wrap(text, 17, expect)
        self.check_wrap(text, 19, expect)
        expect = ["You can also do--this or even", "---this."]
        self.check_wrap(text, 29, expect)
        self.check_wrap(text, 31, expect)
        expect = ["You can also do--this or even---", "this."]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 35, expect)

        # All of the above behaviour could be deduced by probing the
        # _split() method.
        text = "Here's an -- em-dash and--here's another---and another!"
        expect = [
            "Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", "and",
            "--", "here's", " ", "another", "---", "and", " ", "another!"
        ]
        self.check_split(text, expect)

        text = "and then--bam!--he was gone"
        expect = [
            "and", " ", "then", "--", "bam!", "--", "he", " ", "was", " ",
            "gone"
        ]
        self.check_split(text, expect)

    def test_unix_options(self):
        # Test that Unix-style command-line options are wrapped correctly.
        # Both Optik (OptionParser) and Docutils rely on this behaviour!

        text = "You should use the -n option, or --dry-run in its long form."
        self.check_wrap(text, 20, [
            "You should use the", "-n option, or --dry-", "run in its long",
            "form."
        ])
        self.check_wrap(text, 21, [
            "You should use the -n", "option, or --dry-run",
            "in its long form."
        ])
        expect = [
            "You should use the -n option, or", "--dry-run in its long form."
        ]
        self.check_wrap(text, 32, expect)
        self.check_wrap(text, 34, expect)
        self.check_wrap(text, 35, expect)
        self.check_wrap(text, 38, expect)
        expect = [
            "You should use the -n option, or --dry-", "run in its long form."
        ]
        self.check_wrap(text, 39, expect)
        self.check_wrap(text, 41, expect)
        expect = [
            "You should use the -n option, or --dry-run", "in its long form."
        ]
        self.check_wrap(text, 42, expect)

        # Again, all of the above can be deduced from _split().
        text = "the -n option, or --dry-run or --dryrun"
        expect = [
            "the", " ", "-n", " ", "option,", " ", "or", " ", "--dry-", "run",
            " ", "or", " ", "--dryrun"
        ]
        self.check_split(text, expect)

    def test_funky_hyphens(self):
        # Screwy edge cases cooked up by David Goodger.  All reported
        # in SF bug #596434.
        self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
        self.check_split("what the--", ["what", " ", "the--"])
        self.check_split("what the--.", ["what", " ", "the--."])
        self.check_split("--text--.", ["--text--."])

        # When I first read bug #596434, this is what I thought David
        # was talking about.  I was wrong; these have always worked
        # fine.  The real problem is tested in test_funky_parens()
        # below...
        self.check_split("--option", ["--option"])
        self.check_split("--option-opt", ["--option-", "opt"])
        self.check_split("foo --option-opt bar",
                         ["foo", " ", "--option-", "opt", " ", "bar"])

    def test_funky_parens(self):
        # Second part of SF bug #596434: long option strings inside
        # parentheses.
        self.check_split("foo (--option) bar",
                         ["foo", " ", "(--option)", " ", "bar"])

        # Related stuff -- make sure parens work in simpler contexts.
        self.check_split("foo (bar) baz", ["foo", " ", "(bar)", " ", "baz"])
        self.check_split("blah (ding dong), wubba",
                         ["blah", " ", "(ding", " ", "dong),", " ", "wubba"])

    def test_initial_whitespace(self):
        # SF bug #622849 reported inconsistent handling of leading
        # whitespace; let's test that a bit, shall we?
        text = " This is a sentence with leading whitespace."
        self.check_wrap(text, 50,
                        [" This is a sentence with leading whitespace."])
        self.check_wrap(text, 30,
                        [" This is a sentence with", "leading whitespace."])

    def test_unicode(self):
        # *Very* simple test of wrapping Unicode strings.  I'm sure
        # there's more to it than this, but let's at least make
        # sure textwrap doesn't crash on Unicode input!
        text = u"Hello there, how are you today?"
        self.check_wrap(text, 50, [u"Hello there, how are you today?"])
        self.check_wrap(text, 20, [u"Hello there, how are", "you today?"])
        olines = self.wrapper.wrap(text)
        assert isinstance(olines, list) and isinstance(olines[0], unicode)
        otext = self.wrapper.fill(text)
        assert isinstance(otext, unicode)

    def test_split(self):
        # Ensure that the standard _split() method works as advertised
        # in the comments

        text = "Hello there -- you goof-ball, use the -b option!"

        result = self.wrapper._split(text)
        self.check(result, [
            "Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
            "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"
        ])

    def test_bad_width(self):
        # Ensure that width <= 0 is caught.
        text = "Whatever, it doesn't matter."
        self.assertRaises(ValueError, wrap, text, 0)
        self.assertRaises(ValueError, wrap, text, -1)