Commit ee413849 authored by Greg Ward's avatar Greg Ward

Ditch the whole loop-over-subcases way of working. Add check_wrap() to

base class (WrapperTestCase) instead, and call it repeatedly in the
methods that used to have a loop-over-subcases.  Much simpler.

Rename perennial temp variable 't' to 'text'.
parent 9ebba9ac
...@@ -33,6 +33,10 @@ class WrapperTestCase(unittest.TestCase): ...@@ -33,6 +33,10 @@ class WrapperTestCase(unittest.TestCase):
'Expected:\n%s\nbut got:\n%s' % ( 'Expected:\n%s\nbut got:\n%s' % (
self.show(result), self.show(expect))) self.show(result), self.show(expect)))
def check_wrap (self, text, width, expect):
result = wrap(text, width)
self.check(result, expect)
class WrapTestCase(WrapperTestCase): class WrapTestCase(WrapperTestCase):
...@@ -42,29 +46,25 @@ class WrapTestCase(WrapperTestCase): ...@@ -42,29 +46,25 @@ class WrapTestCase(WrapperTestCase):
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.'''
t = "Hello there, how are you this fine day? I'm glad to hear it!" text = "Hello there, how are you this fine day? I'm glad to hear it!"
subcases = [ self.check_wrap(text, 12,
(12, ["Hello there,", ["Hello there,",
"how are you", "how are you",
"this fine", "this fine",
"day? I'm", "day? I'm",
"glad to hear", "glad to hear",
"it!"]), "it!"])
(42, ["Hello there, how are you this fine day?", self.check_wrap(text, 42,
"I'm glad to hear it!"]), ["Hello there, how are you this fine day?",
(80, [t]), "I'm glad to hear it!"])
] self.check_wrap(text, 80, [text])
for width, expect in subcases:
result = wrap(t, width)
self.check(result, expect)
def test_whitespace(self): def test_whitespace(self):
'''Whitespace munging and end-of-sentence detection.''' '''Whitespace munging and end-of-sentence detection.'''
t = """\ text = """\
This is a paragraph that already has This is a paragraph that already has
line breaks. But some of its lines are much longer than the others, line breaks. But some of its lines are much longer than the others,
so it needs to be wrapped. so it needs to be wrapped.
...@@ -72,63 +72,53 @@ Some lines are \ttabbed too. ...@@ -72,63 +72,53 @@ Some lines are \ttabbed too.
What a mess! What a mess!
""" """
expect = [ expect = ["This is a paragraph that already has line",
"This is a paragraph that already has line",
"breaks. But some of its lines are much", "breaks. But some of its lines are much",
"longer than the others, so it needs to be", "longer than the others, so it needs to be",
"wrapped. Some lines are tabbed too. What a", "wrapped. Some lines are tabbed too. What a",
"mess!" "mess!"]
]
result = self.wrapper.wrap(t) result = self.wrapper.wrap(text)
self.check(result, expect) self.check(result, expect)
result = self.wrapper.fill(t) result = self.wrapper.fill(text)
self.check(result, '\n'.join(expect)) self.check(result, '\n'.join(expect))
def test_wrap_short(self): def test_wrap_short(self):
'''Wrapping to make short lines longer.''' '''Wrapping to make short lines longer.'''
t = "This is a\nshort paragraph." text = "This is a\nshort paragraph."
subcases = [ self.check_wrap(text, 20, ["This is a short",
(20, ["This is a short", "paragraph."])
"paragraph."]), self.check_wrap(text, 40, ["This is a short paragraph."])
(40, ["This is a short paragraph."]),
]
for width, expect in subcases:
result = wrap(t, width)
self.check(result, expect)
def test_hyphenated(self): def test_hyphenated(self):
'''Test breaking hyphenated words.''' '''Test breaking hyphenated words.'''
t = "this-is-a-useful-feature-for-reformatting-posts-from-tim-peters'ly" text = ("this-is-a-useful-feature-for-"
"reformatting-posts-from-tim-peters'ly")
subcases = [
(40, ["this-is-a-useful-feature-for-",
"reformatting-posts-from-tim-peters'ly"]),
(41, ["this-is-a-useful-feature-for-",
"reformatting-posts-from-tim-peters'ly"]),
(42, ["this-is-a-useful-feature-for-reformatting-",
"posts-from-tim-peters'ly"]),
]
for width, expect in subcases: self.check_wrap(text, 40,
result = wrap(t, width) ["this-is-a-useful-feature-for-",
self.check(result, expect) "reformatting-posts-from-tim-peters'ly"])
self.check_wrap(text, 41,
["this-is-a-useful-feature-for-",
"reformatting-posts-from-tim-peters'ly"])
self.check_wrap(text, 42,
["this-is-a-useful-feature-for-reformatting-",
"posts-from-tim-peters'ly"])
def test_split(self): def test_split(self):
'''Ensure that the standard _split() method works as advertised '''Ensure that the standard _split() method works as advertised
in the comments.''' in the comments.'''
t = "Hello there -- you goof-ball, use the -b option!" text = "Hello there -- you goof-ball, use the -b option!"
result = self.wrapper._split(t) result = self.wrapper._split(text)
self.check(result, self.check(result,
["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
"ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
...@@ -137,43 +127,38 @@ What a mess! ...@@ -137,43 +127,38 @@ What a mess!
def test_funky_punc(self): def test_funky_punc(self):
'''Wrap text with long words and lots of punctuation.''' '''Wrap text with long words and lots of punctuation.'''
t = ''' text = '''
Did you say "supercalifragilisticexpialidocious?" Did you say "supercalifragilisticexpialidocious?"
How *do* you spell that odd word, anyways? How *do* you spell that odd word, anyways?
''' '''
subcases = [ self.check_wrap(text, 30,
(30, ['Did you say "supercalifragilis', ['Did you say "supercalifragilis',
'ticexpialidocious?" How *do*', 'ticexpialidocious?" How *do*',
'you spell that odd word,', 'you spell that odd word,',
'anyways?']), 'anyways?'])
(50, ['Did you say "supercalifragilisticexpialidocious?"', self.check_wrap(text, 50,
'How *do* you spell that odd word, anyways?']), ['Did you say "supercalifragilisticexpialidocious?"',
] 'How *do* you spell that odd word, anyways?'])
for width, expect in subcases:
result = wrap(t, width)
self.check(result, expect)
def test_long_words(self): def test_long_words(self):
'''Test with break_long_words disabled.''' '''Test with break_long_words disabled.'''
t = ''' text = '''
Did you say "supercalifragilisticexpialidocious?" Did you say "supercalifragilisticexpialidocious?"
How *do* you spell that odd word, anyways? How *do* you spell that odd word, anyways?
''' '''
self.wrapper.break_long_words = 0 self.wrapper.break_long_words = 0
self.wrapper.width = 30 self.wrapper.width = 30
result = self.wrapper.wrap(t) expect = ['Did you say',
expect = [
'Did you say',
'"supercalifragilisticexpialidocious?"', '"supercalifragilisticexpialidocious?"',
'How *do* you spell that odd', 'How *do* you spell that odd',
'word, anyways?' 'word, anyways?'
] ]
result = self.wrapper.wrap(text)
self.check(result, expect) self.check(result, expect)
# Same thing with kwargs passed to standalone wrap() function. # Same thing with kwargs passed to standalone wrap() function.
result = wrap(t, width=30, break_long_words=0) result = wrap(text, width=30, break_long_words=0)
self.check(result, expect) self.check(result, expect)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment