]> granicus.if.org Git - python/commitdiff
Fix an endcase bug: initial_indent was ignored when the text was short
authorGuido van Rossum <guido@python.org>
Wed, 2 Oct 2002 15:47:32 +0000 (15:47 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 2 Oct 2002 15:47:32 +0000 (15:47 +0000)
enough to fit in one line.

Lib/test/test_textwrap.py
Lib/textwrap.py

index fc5d0cc20244736f574d47ae9d5fe5baa1d07bd8..2a6d67a50d1a908686ba9ba0a293666a5c90b43e 100644 (file)
@@ -33,11 +33,11 @@ class BaseTestCase(unittest.TestCase):
             'expected:\n%s\nbut got:\n%s' % (
                 self.show(expect), self.show(result)))
 
-    def check_wrap (self, text, width, expect):
-        result = wrap(text, width)
+    def check_wrap(self, text, width, expect, **kwargs):
+        result = wrap(text, width, **kwargs)
         self.check(result, expect)
 
-    def check_split (self, wrapper, text, expect):
+    def check_split(self, wrapper, text, expect):
         result = wrapper._split(text)
         self.assertEquals(result, expect,
                           "\nexpected %r\n"
@@ -101,6 +101,16 @@ What a mess!
         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
 
index f5d1915711033707e2d48692259f5613b2dd1ec6..ff5f7efaa9b95a8b5b5f0d3d061a55acff917c7c 100644 (file)
@@ -237,8 +237,9 @@ class TextWrapper:
         converted to space.
         """
         text = self._munge_whitespace(text)
-        if len(text) <= self.width:
-            return [text]
+        indent = self.initial_indent
+        if len(text) + len(indent) <= self.width:
+            return [indent + text]
         chunks = self._split(text)
         if self.fix_sentence_endings:
             self._fix_sentence_endings(chunks)