Anonymous avatar Anonymous committed 0593158

SF #847346: add test_fix_sentence_endings() to ensure that sentence
endings get fixed even if the input is too short to wrap. Also throw
in a bunch of other tests for this rather tricky and under-tested
feature.

In WrapTestCase.setUp(), don't bother setting fix_sentence_endings=True
for self.wrapper -- just use a separate wrapper in the two tests that
care about it, namely test_whitespace() and test_fix_sentence_endings().

Comments (0)

Files changed (1)

Lib/test/test_textwrap.py

 class WrapTestCase(BaseTestCase):
 
     def setUp(self):
-        self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
+        self.wrapper = TextWrapper(width=45)
 
     def test_simple(self):
         # Simple case: just words, spaces, and a bit of punctuation
                   "wrapped.  Some lines are  tabbed too.  What a",
                   "mess!"]
 
-        result = self.wrapper.wrap(text)
+        wrapper = TextWrapper(45, fix_sentence_endings=True)
+        result = wrapper.wrap(text)
         self.check(result, expect)
 
-        result = self.wrapper.fill(text)
+        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
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.