mirror of https://github.com/python/cpython
SF #847346: merge from release23-maint branch: remove misguided
optimization for short input; beef up tests for fix_sentence_endings feature.
This commit is contained in:
parent
10c660673e
commit
f0ba764dbb
|
@ -47,7 +47,7 @@ class BaseTestCase(unittest.TestCase):
|
||||||
class WrapTestCase(BaseTestCase):
|
class WrapTestCase(BaseTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
|
self.wrapper = TextWrapper(width=45)
|
||||||
|
|
||||||
def test_simple(self):
|
def test_simple(self):
|
||||||
# Simple case: just words, spaces, and a bit of punctuation
|
# Simple case: just words, spaces, and a bit of punctuation
|
||||||
|
@ -84,12 +84,50 @@ What a mess!
|
||||||
"wrapped. Some lines are tabbed too. What a",
|
"wrapped. Some lines are tabbed too. What a",
|
||||||
"mess!"]
|
"mess!"]
|
||||||
|
|
||||||
result = self.wrapper.wrap(text)
|
wrapper = TextWrapper(45, fix_sentence_endings=True)
|
||||||
|
result = wrapper.wrap(text)
|
||||||
self.check(result, expect)
|
self.check(result, expect)
|
||||||
|
|
||||||
result = self.wrapper.fill(text)
|
result = wrapper.fill(text)
|
||||||
self.check(result, '\n'.join(expect))
|
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):
|
def test_wrap_short(self):
|
||||||
# Wrapping to make short lines longer
|
# Wrapping to make short lines longer
|
||||||
|
|
|
@ -90,14 +90,14 @@ class TextWrapper:
|
||||||
% string.lowercase)
|
% string.lowercase)
|
||||||
|
|
||||||
|
|
||||||
def __init__ (self,
|
def __init__(self,
|
||||||
width=70,
|
width=70,
|
||||||
initial_indent="",
|
initial_indent="",
|
||||||
subsequent_indent="",
|
subsequent_indent="",
|
||||||
expand_tabs=True,
|
expand_tabs=True,
|
||||||
replace_whitespace=True,
|
replace_whitespace=True,
|
||||||
fix_sentence_endings=False,
|
fix_sentence_endings=False,
|
||||||
break_long_words=True):
|
break_long_words=True):
|
||||||
self.width = width
|
self.width = width
|
||||||
self.initial_indent = initial_indent
|
self.initial_indent = initial_indent
|
||||||
self.subsequent_indent = subsequent_indent
|
self.subsequent_indent = subsequent_indent
|
||||||
|
@ -268,8 +268,6 @@ class TextWrapper:
|
||||||
"""
|
"""
|
||||||
text = self._munge_whitespace(text)
|
text = self._munge_whitespace(text)
|
||||||
indent = self.initial_indent
|
indent = self.initial_indent
|
||||||
if len(text) + len(indent) <= self.width:
|
|
||||||
return [indent + text]
|
|
||||||
chunks = self._split(text)
|
chunks = self._split(text)
|
||||||
if self.fix_sentence_endings:
|
if self.fix_sentence_endings:
|
||||||
self._fix_sentence_endings(chunks)
|
self._fix_sentence_endings(chunks)
|
||||||
|
|
Loading…
Reference in New Issue