10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Test suite for the textwrap module. 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Original tests written by Greg Ward <gward@python.net>. 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Converted to PyUnit by Peter Hansen <peter@engcorp.com>. 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Currently maintained by Greg Ward. 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# $Id$ 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom textwrap import TextWrapper, wrap, fill, dedent 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BaseTestCase(unittest.TestCase): 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '''Parent class with utility methods for textwrap tests.''' 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def show(self, textin): 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(textin, list): 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = [] 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in range(len(textin)): 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.append(" %d: %r" % (i, textin[i])) 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = '\n'.join(result) 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif isinstance(textin, basestring): 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = " %s\n" % repr(textin) 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return result 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def check(self, result, expect): 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, expect, 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'expected:\n%s\nbut got:\n%s' % ( 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.show(expect), self.show(result))) 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def check_wrap(self, text, width, expect, **kwargs): 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = wrap(text, width, **kwargs) 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, expect) 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def check_split(self, text, expect): 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.wrapper._split(text) 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, expect, 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "\nexpected %r\n" 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "but got %r" % (expect, result)) 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass WrapTestCase(BaseTestCase): 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.wrapper = TextWrapper(width=45) 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_simple(self): 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Simple case: just words, spaces, and a bit of punctuation 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Hello there, how are you this fine day? I'm glad to hear it!" 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 12, 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["Hello there,", 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "how are you", 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "this fine", 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "day? I'm", 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "glad to hear", 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "it!"]) 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 42, 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["Hello there, how are you this fine day?", 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "I'm glad to hear it!"]) 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 80, [text]) 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_empty_string(self): 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that wrapping the empty string returns an empty list. 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap("", 6, []) 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap("", 6, [], drop_whitespace=False) 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_empty_string_with_initial_indent(self): 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that the empty string is not indented. 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap("", 6, [], initial_indent="++") 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap("", 6, [], initial_indent="++", drop_whitespace=False) 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_whitespace(self): 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Whitespace munging and end-of-sentence detection 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = """\ 830a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThis is a paragraph that already has 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoline breaks. But some of its lines are much longer than the others, 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoso it needs to be wrapped. 860a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSome lines are \ttabbed too. 870a8c90248264a8b26970b4473770bcc3df8515fJosh GaoWhat a mess! 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["This is a paragraph that already has line", 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "breaks. But some of its lines are much", 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "longer than the others, so it needs to be", 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "wrapped. Some lines are tabbed too. What a", 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "mess!"] 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao wrapper = TextWrapper(45, fix_sentence_endings=True) 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = wrapper.wrap(text) 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, expect) 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = wrapper.fill(text) 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, '\n'.join(expect)) 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_fix_sentence_endings(self): 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao wrapper = TextWrapper(60, fix_sentence_endings=True) 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # SF #847346: ensure that fix_sentence_endings=True does the 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # right thing even on input short enough that it doesn't need to 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # be wrapped. 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "A short line. Note the single space." 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["A short line. Note the single space."] 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(wrapper.wrap(text), expect) 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test some of the hairy end cases that _fix_sentence_endings() 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # is supposed to handle (the easy stuff is tested in 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # test_whitespace() above). 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Well, Doctor? What do you think?" 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["Well, Doctor? What do you think?"] 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(wrapper.wrap(text), expect) 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Well, Doctor?\nWhat do you think?" 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(wrapper.wrap(text), expect) 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = 'I say, chaps! Anyone for "tennis?"\nHmmph!' 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ['I say, chaps! Anyone for "tennis?" Hmmph!'] 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(wrapper.wrap(text), expect) 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao wrapper.width = 20 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!'] 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(wrapper.wrap(text), expect) 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = 'And she said, "Go to hell!"\nCan you believe that?' 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ['And she said, "Go to', 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'hell!" Can you', 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'believe that?'] 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(wrapper.wrap(text), expect) 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao wrapper.width = 60 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ['And she said, "Go to hell!" Can you believe that?'] 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(wrapper.wrap(text), expect) 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = 'File stdio.h is nice.' 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ['File stdio.h is nice.'] 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(wrapper.wrap(text), expect) 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_wrap_short(self): 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Wrapping to make short lines longer 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "This is a\nshort paragraph." 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 20, ["This is a short", 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "paragraph."]) 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 40, ["This is a short paragraph."]) 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_wrap_short_1line(self): 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test endcases 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "This is a short line." 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 30, ["This is a short line."]) 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 30, ["(1) This is a short line."], 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao initial_indent="(1) ") 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_hyphenated(self): 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test breaking hyphenated words 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = ("this-is-a-useful-feature-for-" 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "reformatting-posts-from-tim-peters'ly") 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 40, 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["this-is-a-useful-feature-for-", 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "reformatting-posts-from-tim-peters'ly"]) 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 41, 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["this-is-a-useful-feature-for-", 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "reformatting-posts-from-tim-peters'ly"]) 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 42, 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["this-is-a-useful-feature-for-reformatting-", 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "posts-from-tim-peters'ly"]) 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_hyphenated_numbers(self): 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test that hyphenated numbers (eg. dates) are not broken like words. 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n" 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "released on 1994-02-15.") 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 35, ['Python 1.0.0 was released on', 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '1994-01-26. Python 1.0.1 was', 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'released on 1994-02-15.']) 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.', 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Python 1.0.1 was released on 1994-02-15.']) 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "I do all my shopping at 7-11." 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 25, ["I do all my shopping at", 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "7-11."]) 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 27, ["I do all my shopping at", 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "7-11."]) 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 29, ["I do all my shopping at 7-11."]) 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_em_dash(self): 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test text with em-dashes 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Em-dashes should be written -- thus." 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 25, 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["Em-dashes should be", 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "written -- thus."]) 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Probe the boundaries of the properly written em-dash, 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ie. " -- ". 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 29, 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["Em-dashes should be written", 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "-- thus."]) 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["Em-dashes should be written --", 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "thus."] 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 30, expect) 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 35, expect) 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 36, 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["Em-dashes should be written -- thus."]) 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The improperly written em-dash is handled too, because 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # it's adjacent to non-whitespace on both sides. 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "You can also do--this or even---this." 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["You can also do", 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "--this or even", 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "---this."] 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 15, expect) 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 16, expect) 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["You can also do--", 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "this or even---", 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "this."] 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 17, expect) 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 19, expect) 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["You can also do--this or even", 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "---this."] 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 29, expect) 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 31, expect) 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["You can also do--this or even---", 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "this."] 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 32, expect) 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 35, expect) 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # All of the above behaviour could be deduced by probing the 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # _split() method. 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Here's an -- em-dash and--here's another---and another!" 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "and", "--", "here's", " ", "another", "---", 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "and", " ", "another!"] 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split(text, expect) 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "and then--bam!--he was gone" 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["and", " ", "then", "--", "bam!", "--", 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "he", " ", "was", " ", "gone"] 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split(text, expect) 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_unix_options (self): 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test that Unix-style command-line options are wrapped correctly. 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Both Optik (OptionParser) and Docutils rely on this behaviour! 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "You should use the -n option, or --dry-run in its long form." 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 20, 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["You should use the", 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "-n option, or --dry-", 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "run in its long", 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "form."]) 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 21, 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["You should use the -n", 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "option, or --dry-run", 2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "in its long form."]) 2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["You should use the -n option, or", 2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "--dry-run in its long form."] 2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 32, expect) 2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 34, expect) 2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 35, expect) 2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 38, expect) 2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["You should use the -n option, or --dry-", 2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "run in its long form."] 2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 39, expect) 2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 41, expect) 2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["You should use the -n option, or --dry-run", 2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "in its long form."] 2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 42, expect) 2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Again, all of the above can be deduced from _split(). 2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "the -n option, or --dry-run or --dryrun" 2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", 2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "--dry-", "run", " ", "or", " ", "--dryrun"] 2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split(text, expect) 2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_funky_hyphens (self): 2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Screwy edge cases cooked up by David Goodger. All reported 2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # in SF bug #596434. 2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"]) 2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("what the--", ["what", " ", "the--"]) 2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("what the--.", ["what", " ", "the--."]) 2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("--text--.", ["--text--."]) 2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # When I first read bug #596434, this is what I thought David 2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # was talking about. I was wrong; these have always worked 2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # fine. The real problem is tested in test_funky_parens() 2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # below... 3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("--option", ["--option"]) 3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("--option-opt", ["--option-", "opt"]) 3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("foo --option-opt bar", 3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["foo", " ", "--option-", "opt", " ", "bar"]) 3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_punct_hyphens(self): 3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Oh bother, SF #965425 found another problem with hyphens -- 3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # hyphenated words in single quotes weren't handled correctly. 3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # In fact, the bug is that *any* punctuation around a hyphenated 3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # word was handled incorrectly, except for a leading "--", which 3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # was special-cased for Optik and Docutils. So test a variety 3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # of styles of punctuation around a hyphenated word. 3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (Actually this is based on an Optik bug report, #813077). 3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("the 'wibble-wobble' widget", 3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ['the', ' ', "'wibble-", "wobble'", ' ', 'widget']) 3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split('the "wibble-wobble" widget', 3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget']) 3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("the (wibble-wobble) widget", 3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ['the', ' ', "(wibble-", "wobble)", ' ', 'widget']) 3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("the ['wibble-wobble'] widget", 3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ['the', ' ', "['wibble-", "wobble']", ' ', 'widget']) 3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_funky_parens (self): 3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Second part of SF bug #596434: long option strings inside 3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # parentheses. 3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("foo (--option) bar", 3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["foo", " ", "(--option)", " ", "bar"]) 3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Related stuff -- make sure parens work in simpler contexts. 3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("foo (bar) baz", 3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["foo", " ", "(bar)", " ", "baz"]) 3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_split("blah (ding dong), wubba", 3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["blah", " ", "(ding", " ", "dong),", 3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao " ", "wubba"]) 3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_drop_whitespace_false(self): 3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that drop_whitespace=False preserves whitespace. 3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # SF patch #1581073 3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " This is a sentence with much whitespace." 3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 10, 3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [" This is a", " ", "sentence ", 3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "with ", "much white", "space."], 3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao drop_whitespace=False) 3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_drop_whitespace_false_whitespace_only(self): 3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that drop_whitespace=False preserves a whitespace-only string. 3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(" ", 6, [" "], drop_whitespace=False) 3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_drop_whitespace_false_whitespace_only_with_indent(self): 3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that a whitespace-only string gets indented (when 3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # drop_whitespace is False). 3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(" ", 6, [" "], drop_whitespace=False, 3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao initial_indent=" ") 3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_drop_whitespace_whitespace_only(self): 3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check drop_whitespace on a whitespace-only string. 3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(" ", 6, []) 3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_drop_whitespace_leading_whitespace(self): 3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that drop_whitespace does not drop leading whitespace (if 3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # followed by non-whitespace). 3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # SF bug #622849 reported inconsistent handling of leading 3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # whitespace; let's test that a bit, shall we? 3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " This is a sentence with leading whitespace." 3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 50, 3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [" This is a sentence with leading whitespace."]) 3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 30, 3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [" This is a sentence with", "leading whitespace."]) 3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_drop_whitespace_whitespace_line(self): 3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that drop_whitespace skips the whole line if a non-leading 3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # line consists only of whitespace. 3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "abcd efgh" 3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Include the result for drop_whitespace=False for comparison. 3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 6, ["abcd", " ", "efgh"], 3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao drop_whitespace=False) 3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 6, ["abcd", "efgh"]) 3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_drop_whitespace_whitespace_only_with_indent(self): 3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that initial_indent is not applied to a whitespace-only 3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # string. This checks a special case of the fact that dropping 3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # whitespace occurs before indenting. 3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(" ", 6, [], initial_indent="++") 3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_drop_whitespace_whitespace_indent(self): 3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check that drop_whitespace does not drop whitespace indents. 3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # This checks a special case of the fact that dropping whitespace 3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # occurs before indenting. 3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap("abcd efgh", 6, [" abcd", " efgh"], 3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao initial_indent=" ", subsequent_indent=" ") 3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if test_support.have_unicode: 3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_unicode(self): 3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # *Very* simple test of wrapping Unicode strings. I'm sure 3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # there's more to it than this, but let's at least make 3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # sure textwrap doesn't crash on Unicode input! 3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = u"Hello there, how are you today?" 3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 50, [u"Hello there, how are you today?"]) 3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 20, [u"Hello there, how are", "you today?"]) 3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao olines = self.wrapper.wrap(text) 4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsInstance(olines, list) 4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsInstance(olines[0], unicode) 4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao otext = self.wrapper.fill(text) 4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsInstance(otext, unicode) 4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_no_split_at_umlaut(self): 4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = u"Die Empf\xe4nger-Auswahl" 4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 13, [u"Die", u"Empf\xe4nger-", u"Auswahl"]) 4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_umlaut_followed_by_dash(self): 4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = u"aa \xe4\xe4-\xe4\xe4" 4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 7, [u"aa \xe4\xe4-", u"\xe4\xe4"]) 4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_split(self): 4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Ensure that the standard _split() method works as advertised 4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # in the comments 4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Hello there -- you goof-ball, use the -b option!" 4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.wrapper._split(text) 4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, 4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", 4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) 4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_break_on_hyphens(self): 4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Ensure that the break_on_hyphens attributes work 4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "yaba daba-doo" 4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 10, ["yaba daba-", "doo"], 4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break_on_hyphens=True) 4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(text, 10, ["yaba", "daba-doo"], 4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break_on_hyphens=False) 4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_bad_width(self): 4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Ensure that width <= 0 is caught. 4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Whatever, it doesn't matter." 4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ValueError, wrap, text, 0) 4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ValueError, wrap, text, -1) 4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass LongWordTestCase (BaseTestCase): 4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.wrapper = TextWrapper() 4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.text = '''\ 4430a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDid you say "supercalifragilisticexpialidocious?" 4440a8c90248264a8b26970b4473770bcc3df8515fJosh GaoHow *do* you spell that odd word, anyways? 4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao''' 4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_break_long(self): 4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Wrap text with long words and lots of punctuation 4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(self.text, 30, 4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ['Did you say "supercalifragilis', 4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'ticexpialidocious?" How *do*', 4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'you spell that odd word,', 4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'anyways?']) 4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(self.text, 50, 4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ['Did you say "supercalifragilisticexpialidocious?"', 4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'How *do* you spell that odd word, anyways?']) 4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # SF bug 797650. Prevent an infinite loop by making sure that at 4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # least one character gets split off on every pass. 4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap('-'*10+'hello', 10, 4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ['----------', 4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ' h', 4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ' e', 4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ' l', 4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ' l', 4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ' o'], 4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao subsequent_indent = ' '*15) 4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # bug 1146. Prevent a long word to be wrongly wrapped when the 4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # preceding word is exactly one character shorter than the width 4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_wrap(self.text, 12, 4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ['Did you say ', 4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '"supercalifr', 4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'agilisticexp', 4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'ialidocious?', 4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '" How *do*', 4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'you spell', 4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'that odd', 4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'word,', 4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'anyways?']) 4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_nobreak_long(self): 4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test with break_long_words disabled 4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.wrapper.break_long_words = 0 4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.wrapper.width = 30 4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = ['Did you say', 4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '"supercalifragilisticexpialidocious?"', 4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'How *do* you spell that odd', 4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'word, anyways?' 4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ] 4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.wrapper.wrap(self.text) 4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, expect) 4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Same thing with kwargs passed to standalone wrap() function. 4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = wrap(self.text, width=30, break_long_words=0) 4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, expect) 4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass IndentTestCases(BaseTestCase): 5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # called before each test method 5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.text = '''\ 5050a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThis paragraph will be filled, first without any indentation, 5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoand then with some (including a hanging indent).''' 5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_fill(self): 5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test the fill() method 5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = '''\ 5130a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThis paragraph will be filled, first 5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowithout any indentation, and then with 5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaosome (including a hanging indent).''' 5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = fill(self.text, 40) 5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, expect) 5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_initial_indent(self): 5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test initial_indent parameter 5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = [" This paragraph will be filled,", 5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "first without any indentation, and then", 5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "with some (including a hanging indent)."] 5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = wrap(self.text, 40, initial_indent=" ") 5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, expect) 5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "\n".join(expect) 5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = fill(self.text, 40, initial_indent=" ") 5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, expect) 5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_subsequent_indent(self): 5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test subsequent_indent parameter 5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = '''\ 5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao * This paragraph will be filled, first 5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao without any indentation, and then 5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with some (including a hanging 5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao indent).''' 5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = fill(self.text, 40, 5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao initial_indent=" * ", subsequent_indent=" ") 5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check(result, expect) 5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Despite the similar names, DedentTestCase is *not* the inverse 5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# of IndentTestCase! 5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass DedentTestCase(unittest.TestCase): 5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def assertUnchanged(self, text): 5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """assert that dedent() has no effect on 'text'""" 5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(text, dedent(text)) 5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_dedent_nomargin(self): 5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # No lines indented. 5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Hello there.\nHow are you?\nOh good, I'm glad." 5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertUnchanged(text) 5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Similar, with a blank line. 5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Hello there.\n\nBoo!" 5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertUnchanged(text) 5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Some lines indented, but overall margin is still zero. 5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Hello there.\n This is indented." 5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertUnchanged(text) 5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Again, add a blank line. 5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "Hello there.\n\n Boo!\n" 5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertUnchanged(text) 5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_dedent_even(self): 5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # All lines indented by two spaces. 5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " Hello there.\n How are ya?\n Oh good." 5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "Hello there.\nHow are ya?\nOh good." 5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Same, with blank lines. 5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " Hello there.\n\n How are ya?\n Oh good.\n" 5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "Hello there.\n\nHow are ya?\nOh good.\n" 5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Now indent one of the blank lines. 5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " Hello there.\n \n How are ya?\n Oh good.\n" 5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "Hello there.\n\nHow are ya?\nOh good.\n" 5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_dedent_uneven(self): 5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Lines indented unevenly. 5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = '''\ 5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def foo(): 5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while 1: 5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return foo 5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = '''\ 5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef foo(): 5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while 1: 6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return foo 6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao''' 6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Uneven indentation with a blank line. 6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " Foo\n Bar\n\n Baz\n" 6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "Foo\n Bar\n\n Baz\n" 6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Uneven indentation with a whitespace-only line. 6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " Foo\n Bar\n \n Baz\n" 6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "Foo\n Bar\n\n Baz\n" 6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # dedent() should not mangle internal tabs 6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_dedent_preserve_internal_tabs(self): 6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " hello\tthere\n how are\tyou?" 6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "hello\tthere\nhow are\tyou?" 6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make sure that it preserves tabs when it's not making any 6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # changes at all 6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(expect)) 6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # dedent() should not mangle tabs in the margin (i.e. 6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # tabs and spaces both count as margin, but are *not* 6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # considered equivalent) 6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_dedent_preserve_margin_tabs(self): 6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " hello there\n\thow are you?" 6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertUnchanged(text) 6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # same effect even if we have 8 spaces 6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " hello there\n\thow are you?" 6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertUnchanged(text) 6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # dedent() only removes whitespace that can be uniformly removed! 6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = "\thello there\n\thow are you?" 6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "hello there\nhow are you?" 6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " \thello there\n \thow are you?" 6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " \t hello there\n \t how are you?" 6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = " \thello there\n \t how are you?" 6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expect = "hello there\n how are you?" 6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(expect, dedent(text)) 6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main(): 6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_support.run_unittest(WrapTestCase, 6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao LongWordTestCase, 6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao IndentTestCases, 6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao DedentTestCase) 6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__': 6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_main() 659