Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_textwrap.WrapTestCase Class Reference
Inheritance diagram for test.test_textwrap.WrapTestCase:
Inheritance graph
[legend]
Collaboration diagram for test.test_textwrap.WrapTestCase:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def setUp
def test_simple
def test_whitespace
def test_fix_sentence_endings
def test_wrap_short
def test_wrap_short_1line
def test_hyphenated
def test_hyphenated_numbers
def test_em_dash
def test_unix_options
def test_funky_hyphens
def test_punct_hyphens
def test_funky_parens
def test_initial_whitespace
def test_no_drop_whitespace
def test_split
def test_break_on_hyphens
def test_bad_width
def test_no_split_at_umlaut
def test_umlaut_followed_by_dash
def show
def check
def check_wrap
def check_split

Public Attributes

 wrapper

Detailed Description

Definition at line 47 of file test_textwrap.py.


Member Function Documentation

def test.test_textwrap.BaseTestCase.check (   self,
  result,
  expect 
) [inherited]

Definition at line 31 of file test_textwrap.py.

00031 
00032     def check(self, result, expect):
00033         self.assertEqual(result, expect,
00034             'expected:\n%s\nbut got:\n%s' % (
00035                 self.show(expect), self.show(result)))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_textwrap.BaseTestCase.check_split (   self,
  text,
  expect 
) [inherited]

Definition at line 40 of file test_textwrap.py.

00040 
00041     def check_split(self, text, expect):
00042         result = self.wrapper._split(text)
00043         self.assertEqual(result, expect,
00044                          "\nexpected %r\n"
00045                          "but got  %r" % (expect, result))
00046 

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_textwrap.BaseTestCase.check_wrap (   self,
  text,
  width,
  expect,
  kwargs 
) [inherited]

Definition at line 36 of file test_textwrap.py.

00036 
00037     def check_wrap(self, text, width, expect, **kwargs):
00038         result = wrap(text, width, **kwargs)
00039         self.check(result, expect)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 49 of file test_textwrap.py.

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

Here is the caller graph for this function:

def test.test_textwrap.BaseTestCase.show (   self,
  textin 
) [inherited]

Definition at line 20 of file test_textwrap.py.

00020 
00021     def show(self, textin):
00022         if isinstance(textin, list):
00023             result = []
00024             for i in range(len(textin)):
00025                 result.append("  %d: %r" % (i, textin[i]))
00026             result = '\n'.join(result)
00027         elif isinstance(textin, str):
00028             result = "  %s\n" % repr(textin)
00029         return result
00030 

Here is the caller graph for this function:

Definition at line 362 of file test_textwrap.py.

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

Here is the call graph for this function:

Definition at line 354 of file test_textwrap.py.

00354 
00355     def test_break_on_hyphens(self):
00356         # Ensure that the break_on_hyphens attributes work
00357         text = "yaba daba-doo"
00358         self.check_wrap(text, 10, ["yaba daba-", "doo"],
00359                         break_on_hyphens=True)
00360         self.check_wrap(text, 10, ["yaba", "daba-doo"],
00361                         break_on_hyphens=False)

Here is the call graph for this function:

Definition at line 190 of file test_textwrap.py.

00190 
00191     def test_em_dash(self):
00192         # Test text with em-dashes
00193         text = "Em-dashes should be written -- thus."
00194         self.check_wrap(text, 25,
00195                         ["Em-dashes should be",
00196                          "written -- thus."])
00197 
00198         # Probe the boundaries of the properly written em-dash,
00199         # ie. " -- ".
00200         self.check_wrap(text, 29,
00201                         ["Em-dashes should be written",
00202                          "-- thus."])
00203         expect = ["Em-dashes should be written --",
00204                   "thus."]
00205         self.check_wrap(text, 30, expect)
00206         self.check_wrap(text, 35, expect)
00207         self.check_wrap(text, 36,
00208                         ["Em-dashes should be written -- thus."])
00209 
00210         # The improperly written em-dash is handled too, because
00211         # it's adjacent to non-whitespace on both sides.
00212         text = "You can also do--this or even---this."
00213         expect = ["You can also do",
00214                   "--this or even",
00215                   "---this."]
00216         self.check_wrap(text, 15, expect)
00217         self.check_wrap(text, 16, expect)
00218         expect = ["You can also do--",
00219                   "this or even---",
00220                   "this."]
00221         self.check_wrap(text, 17, expect)
00222         self.check_wrap(text, 19, expect)
00223         expect = ["You can also do--this or even",
00224                   "---this."]
00225         self.check_wrap(text, 29, expect)
00226         self.check_wrap(text, 31, expect)
00227         expect = ["You can also do--this or even---",
00228                   "this."]
00229         self.check_wrap(text, 32, expect)
00230         self.check_wrap(text, 35, expect)
00231 
00232         # All of the above behaviour could be deduced by probing the
00233         # _split() method.
00234         text = "Here's an -- em-dash and--here's another---and another!"
00235         expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ",
00236                   "and", "--", "here's", " ", "another", "---",
00237                   "and", " ", "another!"]
00238         self.check_split(text, expect)
00239 
00240         text = "and then--bam!--he was gone"
00241         expect = ["and", " ", "then", "--", "bam!", "--",
00242                   "he", " ", "was", " ", "gone"]
00243         self.check_split(text, expect)
00244 

Here is the call graph for this function:

Definition at line 94 of file test_textwrap.py.

00094 
00095     def test_fix_sentence_endings(self):
00096         wrapper = TextWrapper(60, fix_sentence_endings=True)
00097 
00098         # SF #847346: ensure that fix_sentence_endings=True does the
00099         # right thing even on input short enough that it doesn't need to
00100         # be wrapped.
00101         text = "A short line. Note the single space."
00102         expect = ["A short line.  Note the single space."]
00103         self.check(wrapper.wrap(text), expect)
00104 
00105         # Test some of the hairy end cases that _fix_sentence_endings()
00106         # is supposed to handle (the easy stuff is tested in
00107         # test_whitespace() above).
00108         text = "Well, Doctor? What do you think?"
00109         expect = ["Well, Doctor?  What do you think?"]
00110         self.check(wrapper.wrap(text), expect)
00111 
00112         text = "Well, Doctor?\nWhat do you think?"
00113         self.check(wrapper.wrap(text), expect)
00114 
00115         text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
00116         expect = ['I say, chaps!  Anyone for "tennis?"  Hmmph!']
00117         self.check(wrapper.wrap(text), expect)
00118 
00119         wrapper.width = 20
00120         expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!']
00121         self.check(wrapper.wrap(text), expect)
00122 
00123         text = 'And she said, "Go to hell!"\nCan you believe that?'
00124         expect = ['And she said, "Go to',
00125                   'hell!"  Can you',
00126                   'believe that?']
00127         self.check(wrapper.wrap(text), expect)
00128 
00129         wrapper.width = 60
00130         expect = ['And she said, "Go to hell!"  Can you believe that?']
00131         self.check(wrapper.wrap(text), expect)
00132 
00133         text = 'File stdio.h is nice.'
00134         expect = ['File stdio.h is nice.']
00135         self.check(wrapper.wrap(text), expect)

Here is the call graph for this function:

Definition at line 279 of file test_textwrap.py.

00279 
00280     def test_funky_hyphens (self):
00281         # Screwy edge cases cooked up by David Goodger.  All reported
00282         # in SF bug #596434.
00283         self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"])
00284         self.check_split("what the--", ["what", " ", "the--"])
00285         self.check_split("what the--.", ["what", " ", "the--."])
00286         self.check_split("--text--.", ["--text--."])
00287 
00288         # When I first read bug #596434, this is what I thought David
00289         # was talking about.  I was wrong; these have always worked
00290         # fine.  The real problem is tested in test_funky_parens()
00291         # below...
00292         self.check_split("--option", ["--option"])
00293         self.check_split("--option-opt", ["--option-", "opt"])
00294         self.check_split("foo --option-opt bar",
00295                          ["foo", " ", "--option-", "opt", " ", "bar"])

Here is the call graph for this function:

Definition at line 313 of file test_textwrap.py.

00313 
00314     def test_funky_parens (self):
00315         # Second part of SF bug #596434: long option strings inside
00316         # parentheses.
00317         self.check_split("foo (--option) bar",
00318                          ["foo", " ", "(--option)", " ", "bar"])
00319 
00320         # Related stuff -- make sure parens work in simpler contexts.
00321         self.check_split("foo (bar) baz",
00322                          ["foo", " ", "(bar)", " ", "baz"])
00323         self.check_split("blah (ding dong), wubba",
00324                          ["blah", " ", "(ding", " ", "dong),",
00325                           " ", "wubba"])

Here is the call graph for this function:

Definition at line 156 of file test_textwrap.py.

00156 
00157     def test_hyphenated(self):
00158         # Test breaking hyphenated words
00159 
00160         text = ("this-is-a-useful-feature-for-"
00161                 "reformatting-posts-from-tim-peters'ly")
00162 
00163         self.check_wrap(text, 40,
00164                         ["this-is-a-useful-feature-for-",
00165                          "reformatting-posts-from-tim-peters'ly"])
00166         self.check_wrap(text, 41,
00167                         ["this-is-a-useful-feature-for-",
00168                          "reformatting-posts-from-tim-peters'ly"])
00169         self.check_wrap(text, 42,
00170                         ["this-is-a-useful-feature-for-reformatting-",
00171                          "posts-from-tim-peters'ly"])

Here is the call graph for this function:

Definition at line 172 of file test_textwrap.py.

00172 
00173     def test_hyphenated_numbers(self):
00174         # Test that hyphenated numbers (eg. dates) are not broken like words.
00175         text = ("Python 1.0.0 was released on 1994-01-26.  Python 1.0.1 was\n"
00176                 "released on 1994-02-15.")
00177 
00178         self.check_wrap(text, 30, ['Python 1.0.0 was released on',
00179                                    '1994-01-26.  Python 1.0.1 was',
00180                                    'released on 1994-02-15.'])
00181         self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.',
00182                                    'Python 1.0.1 was released on 1994-02-15.'])
00183 
00184         text = "I do all my shopping at 7-11."
00185         self.check_wrap(text, 25, ["I do all my shopping at",
00186                                    "7-11."])
00187         self.check_wrap(text, 27, ["I do all my shopping at",
00188                                    "7-11."])
00189         self.check_wrap(text, 29, ["I do all my shopping at 7-11."])

Here is the call graph for this function:

Definition at line 326 of file test_textwrap.py.

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

Here is the call graph for this function:

Definition at line 335 of file test_textwrap.py.

00335 
00336     def test_no_drop_whitespace(self):
00337         # SF patch #1581073
00338         text = " This is a    sentence with     much whitespace."
00339         self.check_wrap(text, 10,
00340                         [" This is a", "    ", "sentence ",
00341                          "with     ", "much white", "space."],
00342                         drop_whitespace=False)

Here is the call graph for this function:

Definition at line 368 of file test_textwrap.py.

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

Here is the call graph for this function:

Definition at line 296 of file test_textwrap.py.

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

Here is the call graph for this function:

Definition at line 52 of file test_textwrap.py.

00052 
00053     def test_simple(self):
00054         # Simple case: just words, spaces, and a bit of punctuation
00055 
00056         text = "Hello there, how are you this fine day?  I'm glad to hear it!"
00057 
00058         self.check_wrap(text, 12,
00059                         ["Hello there,",
00060                          "how are you",
00061                          "this fine",
00062                          "day?  I'm",
00063                          "glad to hear",
00064                          "it!"])
00065         self.check_wrap(text, 42,
00066                         ["Hello there, how are you this fine day?",
00067                          "I'm glad to hear it!"])
00068         self.check_wrap(text, 80, [text])
00069 

Here is the call graph for this function:

Definition at line 343 of file test_textwrap.py.

00343 
00344     def test_split(self):
00345         # Ensure that the standard _split() method works as advertised
00346         # in the comments
00347 
00348         text = "Hello there -- you goof-ball, use the -b option!"
00349 
00350         result = self.wrapper._split(text)
00351         self.check(result,
00352              ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
00353               "ball,", " ", "use", " ", "the", " ", "-b", " ",  "option!"])

Here is the call graph for this function:

Definition at line 372 of file test_textwrap.py.

00372 
00373     def test_umlaut_followed_by_dash(self):
00374         text = "aa \xe4\xe4-\xe4\xe4"
00375         self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"])
00376 

Here is the call graph for this function:

Definition at line 245 of file test_textwrap.py.

00245 
00246     def test_unix_options (self):
00247         # Test that Unix-style command-line options are wrapped correctly.
00248         # Both Optik (OptionParser) and Docutils rely on this behaviour!
00249 
00250         text = "You should use the -n option, or --dry-run in its long form."
00251         self.check_wrap(text, 20,
00252                         ["You should use the",
00253                          "-n option, or --dry-",
00254                          "run in its long",
00255                          "form."])
00256         self.check_wrap(text, 21,
00257                         ["You should use the -n",
00258                          "option, or --dry-run",
00259                          "in its long form."])
00260         expect = ["You should use the -n option, or",
00261                   "--dry-run in its long form."]
00262         self.check_wrap(text, 32, expect)
00263         self.check_wrap(text, 34, expect)
00264         self.check_wrap(text, 35, expect)
00265         self.check_wrap(text, 38, expect)
00266         expect = ["You should use the -n option, or --dry-",
00267                   "run in its long form."]
00268         self.check_wrap(text, 39, expect)
00269         self.check_wrap(text, 41, expect)
00270         expect = ["You should use the -n option, or --dry-run",
00271                   "in its long form."]
00272         self.check_wrap(text, 42, expect)
00273 
00274         # Again, all of the above can be deduced from _split().
00275         text = "the -n option, or --dry-run or --dryrun"
00276         expect = ["the", " ", "-n", " ", "option,", " ", "or", " ",
00277                   "--dry-", "run", " ", "or", " ", "--dryrun"]
00278         self.check_split(text, expect)

Here is the call graph for this function:

Definition at line 70 of file test_textwrap.py.

00070 
00071     def test_whitespace(self):
00072         # Whitespace munging and end-of-sentence detection
00073 
00074         text = """\
00075 This is a paragraph that already has
00076 line breaks.  But some of its lines are much longer than the others,
00077 so it needs to be wrapped.
00078 Some lines are \ttabbed too.
00079 What a mess!
00080 """
00081 
00082         expect = ["This is a paragraph that already has line",
00083                   "breaks.  But some of its lines are much",
00084                   "longer than the others, so it needs to be",
00085                   "wrapped.  Some lines are  tabbed too.  What a",
00086                   "mess!"]
00087 
00088         wrapper = TextWrapper(45, fix_sentence_endings=True)
00089         result = wrapper.wrap(text)
00090         self.check(result, expect)
00091 
00092         result = wrapper.fill(text)
00093         self.check(result, '\n'.join(expect))

Here is the call graph for this function:

Definition at line 136 of file test_textwrap.py.

00136 
00137     def test_wrap_short(self):
00138         # Wrapping to make short lines longer
00139 
00140         text = "This is a\nshort paragraph."
00141 
00142         self.check_wrap(text, 20, ["This is a short",
00143                                    "paragraph."])
00144         self.check_wrap(text, 40, ["This is a short paragraph."])
00145 

Here is the call graph for this function:

Definition at line 146 of file test_textwrap.py.

00146 
00147     def test_wrap_short_1line(self):
00148         # Test endcases
00149 
00150         text = "This is a short line."
00151 
00152         self.check_wrap(text, 30, ["This is a short line."])
00153         self.check_wrap(text, 30, ["(1) This is a short line."],
00154                         initial_indent="(1) ")
00155 

Here is the call graph for this function:


Member Data Documentation

Definition at line 50 of file test_textwrap.py.


The documentation for this class was generated from the following file: