diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-10-15 18:22:54 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-10-15 18:22:54 (GMT) |
commit | acc9f3fb1622c0685b52097b977cafada742be99 (patch) | |
tree | 6f75fc531b1731d6467637d1ffe3f71e39e09f62 /Lib/test/test_textwrap.py | |
parent | bc2bfa6b68e138986e57e2925e5a31c0f17615ab (diff) | |
download | cpython-acc9f3fb1622c0685b52097b977cafada742be99.zip cpython-acc9f3fb1622c0685b52097b977cafada742be99.tar.gz cpython-acc9f3fb1622c0685b52097b977cafada742be99.tar.bz2 |
Issue #18725: The textwrap module now supports truncating multiline text.
Diffstat (limited to 'Lib/test/test_textwrap.py')
-rw-r--r-- | Lib/test/test_textwrap.py | 105 |
1 files changed, 98 insertions, 7 deletions
diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py index 36c15cc..1bba77e 100644 --- a/Lib/test/test_textwrap.py +++ b/Lib/test/test_textwrap.py @@ -42,10 +42,6 @@ class BaseTestCase(unittest.TestCase): "\nexpected %r\n" "but got %r" % (expect, result)) - def check_shorten(self, text, width, expect, **kwargs): - result = shorten(text, width, **kwargs) - self.check(result, expect) - class WrapTestCase(BaseTestCase): @@ -433,6 +429,90 @@ What a mess! self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"]) +class MaxLinesTestCase(BaseTestCase): + text = "Hello there, how are you this fine day? I'm glad to hear it!" + + def test_simple(self): + self.check_wrap(self.text, 12, + ["Hello [...]"], + max_lines=0) + self.check_wrap(self.text, 12, + ["Hello [...]"], + max_lines=1) + self.check_wrap(self.text, 12, + ["Hello there,", + "how [...]"], + max_lines=2) + self.check_wrap(self.text, 13, + ["Hello there,", + "how are [...]"], + max_lines=2) + self.check_wrap(self.text, 80, [self.text], max_lines=1) + self.check_wrap(self.text, 12, + ["Hello there,", + "how are you", + "this fine", + "day? I'm", + "glad to hear", + "it!"], + max_lines=6) + + def test_spaces(self): + # strip spaces before placeholder + self.check_wrap(self.text, 12, + ["Hello there,", + "how are you", + "this fine", + "day? [...]"], + max_lines=4) + # placeholder at the start of line + self.check_wrap(self.text, 6, + ["Hello", + "[...]"], + max_lines=2) + # final spaces + self.check_wrap(self.text + ' ' * 10, 12, + ["Hello there,", + "how are you", + "this fine", + "day? I'm", + "glad to hear", + "it!"], + max_lines=6) + + def test_placeholder(self): + self.check_wrap(self.text, 12, + ["Hello..."], + max_lines=1, + placeholder='...') + self.check_wrap(self.text, 12, + ["Hello there,", + "how are..."], + max_lines=2, + placeholder='...') + # long placeholder and indentation + with self.assertRaises(ValueError): + wrap(self.text, 16, initial_indent=' ', + max_lines=1, placeholder=' [truncated]...') + with self.assertRaises(ValueError): + wrap(self.text, 16, subsequent_indent=' ', + max_lines=2, placeholder=' [truncated]...') + self.check_wrap(self.text, 16, + [" Hello there,", + " [truncated]..."], + max_lines=2, + initial_indent=' ', + subsequent_indent=' ', + placeholder=' [truncated]...') + self.check_wrap(self.text, 16, + [" [truncated]..."], + max_lines=1, + initial_indent=' ', + subsequent_indent=' ', + placeholder=' [truncated]...') + self.check_wrap(self.text, 80, [self.text], placeholder='.' * 1000) + + class LongWordTestCase (BaseTestCase): def setUp(self): self.wrapper = TextWrapper() @@ -493,6 +573,14 @@ How *do* you spell that odd word, anyways? result = wrap(self.text, width=30, break_long_words=0) self.check(result, expect) + def test_max_lines_long(self): + self.check_wrap(self.text, 12, + ['Did you say ', + '"supercalifr', + 'agilisticexp', + '[...]'], + max_lines=4) + class IndentTestCases(BaseTestCase): @@ -782,6 +870,10 @@ class IndentTestCase(unittest.TestCase): class ShortenTestCase(BaseTestCase): + def check_shorten(self, text, width, expect, **kwargs): + result = shorten(text, width, **kwargs) + self.check(result, expect) + 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!" @@ -825,10 +917,9 @@ class ShortenTestCase(BaseTestCase): self.check_shorten("hello world! ", 10, "[...]") def test_width_too_small_for_placeholder(self): - wrapper = TextWrapper(width=8) - wrapper.shorten("x" * 20, placeholder="(......)") + shorten("x" * 20, width=8, placeholder="(......)") with self.assertRaises(ValueError): - wrapper.shorten("x" * 20, placeholder="(.......)") + shorten("x" * 20, width=8, placeholder="(.......)") def test_first_word_too_long_but_placeholder_fits(self): self.check_shorten("Helloo", 5, "[...]") |