Greg Ward | 3dc94e1 | 2002-08-22 18:37:50 +0000 | [diff] [blame] | 1 | # |
Greg Ward | 6186410 | 2004-06-03 01:59:41 +0000 | [diff] [blame] | 2 | # Test suite for the textwrap module. |
Greg Ward | 3dc94e1 | 2002-08-22 18:37:50 +0000 | [diff] [blame] | 3 | # |
| 4 | # Original tests written by Greg Ward <gward@python.net>. |
| 5 | # Converted to PyUnit by Peter Hansen <peter@engcorp.com>. |
| 6 | # Currently maintained by Greg Ward. |
| 7 | # |
| 8 | # $Id$ |
| 9 | # |
| 10 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 11 | import unittest |
| 12 | |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 13 | from textwrap import TextWrapper, wrap, fill, dedent, indent, shorten |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 14 | |
| 15 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 16 | class BaseTestCase(unittest.TestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 17 | '''Parent class with utility methods for textwrap tests.''' |
| 18 | |
| 19 | def show(self, textin): |
| 20 | if isinstance(textin, list): |
| 21 | result = [] |
| 22 | for i in range(len(textin)): |
| 23 | result.append(" %d: %r" % (i, textin[i])) |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 24 | result = "\n".join(result) if result else " no lines" |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 25 | elif isinstance(textin, str): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 26 | result = " %s\n" % repr(textin) |
| 27 | return result |
| 28 | |
| 29 | |
| 30 | def check(self, result, expect): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 31 | self.assertEqual(result, expect, |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 32 | 'expected:\n%s\nbut got:\n%s' % ( |
| 33 | self.show(expect), self.show(result))) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 35 | def check_wrap(self, text, width, expect, **kwargs): |
| 36 | result = wrap(text, width, **kwargs) |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 37 | self.check(result, expect) |
| 38 | |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 39 | def check_split(self, text, expect): |
| 40 | result = self.wrapper._split(text) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 41 | self.assertEqual(result, expect, |
| 42 | "\nexpected %r\n" |
| 43 | "but got %r" % (expect, result)) |
Greg Ward | 715debd | 2002-08-22 21:16:25 +0000 | [diff] [blame] | 44 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 45 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 46 | class WrapTestCase(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 47 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 48 | def setUp(self): |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 49 | self.wrapper = TextWrapper(width=45) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 50 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 51 | def test_simple(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 52 | # Simple case: just words, spaces, and a bit of punctuation |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 53 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 54 | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 55 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 56 | self.check_wrap(text, 12, |
| 57 | ["Hello there,", |
| 58 | "how are you", |
| 59 | "this fine", |
| 60 | "day? I'm", |
| 61 | "glad to hear", |
| 62 | "it!"]) |
| 63 | self.check_wrap(text, 42, |
| 64 | ["Hello there, how are you this fine day?", |
| 65 | "I'm glad to hear it!"]) |
| 66 | self.check_wrap(text, 80, [text]) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 67 | |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 68 | def test_empty_string(self): |
| 69 | # Check that wrapping the empty string returns an empty list. |
| 70 | self.check_wrap("", 6, []) |
| 71 | self.check_wrap("", 6, [], drop_whitespace=False) |
| 72 | |
| 73 | def test_empty_string_with_initial_indent(self): |
| 74 | # Check that the empty string is not indented. |
| 75 | self.check_wrap("", 6, [], initial_indent="++") |
| 76 | self.check_wrap("", 6, [], initial_indent="++", drop_whitespace=False) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 77 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 78 | def test_whitespace(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 79 | # Whitespace munging and end-of-sentence detection |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 80 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 81 | text = """\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 82 | This is a paragraph that already has |
| 83 | line breaks. But some of its lines are much longer than the others, |
| 84 | so it needs to be wrapped. |
| 85 | Some lines are \ttabbed too. |
| 86 | What a mess! |
| 87 | """ |
| 88 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 89 | expect = ["This is a paragraph that already has line", |
| 90 | "breaks. But some of its lines are much", |
| 91 | "longer than the others, so it needs to be", |
| 92 | "wrapped. Some lines are tabbed too. What a", |
| 93 | "mess!"] |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 94 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 95 | wrapper = TextWrapper(45, fix_sentence_endings=True) |
| 96 | result = wrapper.wrap(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 97 | self.check(result, expect) |
| 98 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 99 | result = wrapper.fill(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 100 | self.check(result, '\n'.join(expect)) |
| 101 | |
Hynek Schlawack | d527259 | 2012-05-19 13:33:11 +0200 | [diff] [blame] | 102 | text = "\tTest\tdefault\t\ttabsize." |
| 103 | expect = [" Test default tabsize."] |
| 104 | self.check_wrap(text, 80, expect) |
| 105 | |
| 106 | text = "\tTest\tcustom\t\ttabsize." |
| 107 | expect = [" Test custom tabsize."] |
| 108 | self.check_wrap(text, 80, expect, tabsize=4) |
| 109 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 110 | def test_fix_sentence_endings(self): |
| 111 | wrapper = TextWrapper(60, fix_sentence_endings=True) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 112 | |
Greg Ward | f0ba764 | 2004-05-13 01:53:10 +0000 | [diff] [blame] | 113 | # SF #847346: ensure that fix_sentence_endings=True does the |
| 114 | # right thing even on input short enough that it doesn't need to |
| 115 | # be wrapped. |
| 116 | text = "A short line. Note the single space." |
| 117 | expect = ["A short line. Note the single space."] |
| 118 | self.check(wrapper.wrap(text), expect) |
| 119 | |
| 120 | # Test some of the hairy end cases that _fix_sentence_endings() |
| 121 | # is supposed to handle (the easy stuff is tested in |
| 122 | # test_whitespace() above). |
| 123 | text = "Well, Doctor? What do you think?" |
| 124 | expect = ["Well, Doctor? What do you think?"] |
| 125 | self.check(wrapper.wrap(text), expect) |
| 126 | |
| 127 | text = "Well, Doctor?\nWhat do you think?" |
| 128 | self.check(wrapper.wrap(text), expect) |
| 129 | |
| 130 | text = 'I say, chaps! Anyone for "tennis?"\nHmmph!' |
| 131 | expect = ['I say, chaps! Anyone for "tennis?" Hmmph!'] |
| 132 | self.check(wrapper.wrap(text), expect) |
| 133 | |
| 134 | wrapper.width = 20 |
| 135 | expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!'] |
| 136 | self.check(wrapper.wrap(text), expect) |
| 137 | |
| 138 | text = 'And she said, "Go to hell!"\nCan you believe that?' |
| 139 | expect = ['And she said, "Go to', |
| 140 | 'hell!" Can you', |
| 141 | 'believe that?'] |
| 142 | self.check(wrapper.wrap(text), expect) |
| 143 | |
| 144 | wrapper.width = 60 |
| 145 | expect = ['And she said, "Go to hell!" Can you believe that?'] |
| 146 | self.check(wrapper.wrap(text), expect) |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 147 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 148 | text = 'File stdio.h is nice.' |
| 149 | expect = ['File stdio.h is nice.'] |
| 150 | self.check(wrapper.wrap(text), expect) |
| 151 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 152 | def test_wrap_short(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 153 | # Wrapping to make short lines longer |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 154 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 155 | text = "This is a\nshort paragraph." |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 156 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 157 | self.check_wrap(text, 20, ["This is a short", |
| 158 | "paragraph."]) |
| 159 | self.check_wrap(text, 40, ["This is a short paragraph."]) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 160 | |
| 161 | |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 162 | def test_wrap_short_1line(self): |
| 163 | # Test endcases |
| 164 | |
| 165 | text = "This is a short line." |
| 166 | |
| 167 | self.check_wrap(text, 30, ["This is a short line."]) |
| 168 | self.check_wrap(text, 30, ["(1) This is a short line."], |
| 169 | initial_indent="(1) ") |
| 170 | |
| 171 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 172 | def test_hyphenated(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 173 | # Test breaking hyphenated words |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 174 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 175 | text = ("this-is-a-useful-feature-for-" |
| 176 | "reformatting-posts-from-tim-peters'ly") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 177 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 178 | self.check_wrap(text, 40, |
| 179 | ["this-is-a-useful-feature-for-", |
| 180 | "reformatting-posts-from-tim-peters'ly"]) |
| 181 | self.check_wrap(text, 41, |
| 182 | ["this-is-a-useful-feature-for-", |
| 183 | "reformatting-posts-from-tim-peters'ly"]) |
| 184 | self.check_wrap(text, 42, |
| 185 | ["this-is-a-useful-feature-for-reformatting-", |
| 186 | "posts-from-tim-peters'ly"]) |
Serhiy Storchaka | 72bd327 | 2015-03-24 18:32:27 +0200 | [diff] [blame] | 187 | # The test tests current behavior but is not testing parts of the API. |
| 188 | expect = ("this-|is-|a-|useful-|feature-|for-|" |
| 189 | "reformatting-|posts-|from-|tim-|peters'ly").split('|') |
| 190 | self.check_wrap(text, 1, expect, break_long_words=False) |
| 191 | self.check_split(text, expect) |
| 192 | |
| 193 | self.check_split('e-mail', ['e-mail']) |
| 194 | self.check_split('Jelly-O', ['Jelly-O']) |
| 195 | # The test tests current behavior but is not testing parts of the API. |
| 196 | self.check_split('half-a-crown', 'half-|a-|crown'.split('|')) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 197 | |
Greg Ward | 4040794 | 2005-03-05 02:53:17 +0000 | [diff] [blame] | 198 | def test_hyphenated_numbers(self): |
| 199 | # Test that hyphenated numbers (eg. dates) are not broken like words. |
| 200 | text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n" |
| 201 | "released on 1994-02-15.") |
| 202 | |
| 203 | self.check_wrap(text, 30, ['Python 1.0.0 was released on', |
| 204 | '1994-01-26. Python 1.0.1 was', |
| 205 | 'released on 1994-02-15.']) |
| 206 | self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.', |
| 207 | 'Python 1.0.1 was released on 1994-02-15.']) |
Serhiy Storchaka | 72bd327 | 2015-03-24 18:32:27 +0200 | [diff] [blame] | 208 | self.check_wrap(text, 1, text.split(), break_long_words=False) |
Greg Ward | 4040794 | 2005-03-05 02:53:17 +0000 | [diff] [blame] | 209 | |
| 210 | text = "I do all my shopping at 7-11." |
| 211 | self.check_wrap(text, 25, ["I do all my shopping at", |
| 212 | "7-11."]) |
| 213 | self.check_wrap(text, 27, ["I do all my shopping at", |
| 214 | "7-11."]) |
| 215 | self.check_wrap(text, 29, ["I do all my shopping at 7-11."]) |
Serhiy Storchaka | 72bd327 | 2015-03-24 18:32:27 +0200 | [diff] [blame] | 216 | self.check_wrap(text, 1, text.split(), break_long_words=False) |
Greg Ward | 4040794 | 2005-03-05 02:53:17 +0000 | [diff] [blame] | 217 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 218 | def test_em_dash(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 219 | # Test text with em-dashes |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 220 | text = "Em-dashes should be written -- thus." |
| 221 | self.check_wrap(text, 25, |
| 222 | ["Em-dashes should be", |
| 223 | "written -- thus."]) |
| 224 | |
| 225 | # Probe the boundaries of the properly written em-dash, |
| 226 | # ie. " -- ". |
| 227 | self.check_wrap(text, 29, |
| 228 | ["Em-dashes should be written", |
| 229 | "-- thus."]) |
| 230 | expect = ["Em-dashes should be written --", |
| 231 | "thus."] |
| 232 | self.check_wrap(text, 30, expect) |
| 233 | self.check_wrap(text, 35, expect) |
| 234 | self.check_wrap(text, 36, |
| 235 | ["Em-dashes should be written -- thus."]) |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 236 | |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 237 | # The improperly written em-dash is handled too, because |
| 238 | # it's adjacent to non-whitespace on both sides. |
| 239 | text = "You can also do--this or even---this." |
| 240 | expect = ["You can also do", |
| 241 | "--this or even", |
| 242 | "---this."] |
| 243 | self.check_wrap(text, 15, expect) |
| 244 | self.check_wrap(text, 16, expect) |
| 245 | expect = ["You can also do--", |
| 246 | "this or even---", |
| 247 | "this."] |
| 248 | self.check_wrap(text, 17, expect) |
| 249 | self.check_wrap(text, 19, expect) |
| 250 | expect = ["You can also do--this or even", |
| 251 | "---this."] |
| 252 | self.check_wrap(text, 29, expect) |
| 253 | self.check_wrap(text, 31, expect) |
| 254 | expect = ["You can also do--this or even---", |
| 255 | "this."] |
| 256 | self.check_wrap(text, 32, expect) |
| 257 | self.check_wrap(text, 35, expect) |
| 258 | |
| 259 | # All of the above behaviour could be deduced by probing the |
| 260 | # _split() method. |
| 261 | text = "Here's an -- em-dash and--here's another---and another!" |
Greg Ward | 9ad15a3 | 2002-08-22 19:47:27 +0000 | [diff] [blame] | 262 | expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", |
| 263 | "and", "--", "here's", " ", "another", "---", |
| 264 | "and", " ", "another!"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 265 | self.check_split(text, expect) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 266 | |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 267 | text = "and then--bam!--he was gone" |
| 268 | expect = ["and", " ", "then", "--", "bam!", "--", |
| 269 | "he", " ", "was", " ", "gone"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 270 | self.check_split(text, expect) |
Greg Ward | c6edb37 | 2002-08-22 21:27:05 +0000 | [diff] [blame] | 271 | |
| 272 | |
Greg Ward | 34f995b | 2002-08-22 21:10:07 +0000 | [diff] [blame] | 273 | def test_unix_options (self): |
| 274 | # Test that Unix-style command-line options are wrapped correctly. |
| 275 | # Both Optik (OptionParser) and Docutils rely on this behaviour! |
| 276 | |
| 277 | text = "You should use the -n option, or --dry-run in its long form." |
| 278 | self.check_wrap(text, 20, |
| 279 | ["You should use the", |
| 280 | "-n option, or --dry-", |
| 281 | "run in its long", |
| 282 | "form."]) |
| 283 | self.check_wrap(text, 21, |
| 284 | ["You should use the -n", |
| 285 | "option, or --dry-run", |
| 286 | "in its long form."]) |
| 287 | expect = ["You should use the -n option, or", |
| 288 | "--dry-run in its long form."] |
| 289 | self.check_wrap(text, 32, expect) |
| 290 | self.check_wrap(text, 34, expect) |
| 291 | self.check_wrap(text, 35, expect) |
| 292 | self.check_wrap(text, 38, expect) |
| 293 | expect = ["You should use the -n option, or --dry-", |
| 294 | "run in its long form."] |
| 295 | self.check_wrap(text, 39, expect) |
| 296 | self.check_wrap(text, 41, expect) |
| 297 | expect = ["You should use the -n option, or --dry-run", |
| 298 | "in its long form."] |
| 299 | self.check_wrap(text, 42, expect) |
| 300 | |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 301 | # Again, all of the above can be deduced from _split(). |
| 302 | text = "the -n option, or --dry-run or --dryrun" |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 303 | expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", |
| 304 | "--dry-", "run", " ", "or", " ", "--dryrun"] |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 305 | self.check_split(text, expect) |
| 306 | |
| 307 | def test_funky_hyphens (self): |
| 308 | # Screwy edge cases cooked up by David Goodger. All reported |
| 309 | # in SF bug #596434. |
| 310 | self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"]) |
| 311 | self.check_split("what the--", ["what", " ", "the--"]) |
| 312 | self.check_split("what the--.", ["what", " ", "the--."]) |
| 313 | self.check_split("--text--.", ["--text--."]) |
| 314 | |
Greg Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 315 | # When I first read bug #596434, this is what I thought David |
| 316 | # was talking about. I was wrong; these have always worked |
| 317 | # fine. The real problem is tested in test_funky_parens() |
| 318 | # below... |
Greg Ward | d1a72a0 | 2002-10-31 16:11:18 +0000 | [diff] [blame] | 319 | self.check_split("--option", ["--option"]) |
| 320 | self.check_split("--option-opt", ["--option-", "opt"]) |
Greg Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 321 | self.check_split("foo --option-opt bar", |
| 322 | ["foo", " ", "--option-", "opt", " ", "bar"]) |
| 323 | |
Greg Ward | 6186410 | 2004-06-03 01:59:41 +0000 | [diff] [blame] | 324 | def test_punct_hyphens(self): |
| 325 | # Oh bother, SF #965425 found another problem with hyphens -- |
| 326 | # hyphenated words in single quotes weren't handled correctly. |
| 327 | # In fact, the bug is that *any* punctuation around a hyphenated |
| 328 | # word was handled incorrectly, except for a leading "--", which |
| 329 | # was special-cased for Optik and Docutils. So test a variety |
| 330 | # of styles of punctuation around a hyphenated word. |
| 331 | # (Actually this is based on an Optik bug report, #813077). |
| 332 | self.check_split("the 'wibble-wobble' widget", |
| 333 | ['the', ' ', "'wibble-", "wobble'", ' ', 'widget']) |
| 334 | self.check_split('the "wibble-wobble" widget', |
| 335 | ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget']) |
| 336 | self.check_split("the (wibble-wobble) widget", |
| 337 | ['the', ' ', "(wibble-", "wobble)", ' ', 'widget']) |
| 338 | self.check_split("the ['wibble-wobble'] widget", |
| 339 | ['the', ' ', "['wibble-", "wobble']", ' ', 'widget']) |
| 340 | |
Serhiy Storchaka | 72bd327 | 2015-03-24 18:32:27 +0200 | [diff] [blame] | 341 | # The test tests current behavior but is not testing parts of the API. |
| 342 | self.check_split("what-d'you-call-it.", |
| 343 | "what-d'you-|call-|it.".split('|')) |
| 344 | |
Greg Ward | cc55cb9 | 2003-05-07 01:19:22 +0000 | [diff] [blame] | 345 | def test_funky_parens (self): |
| 346 | # Second part of SF bug #596434: long option strings inside |
| 347 | # parentheses. |
| 348 | self.check_split("foo (--option) bar", |
| 349 | ["foo", " ", "(--option)", " ", "bar"]) |
| 350 | |
| 351 | # Related stuff -- make sure parens work in simpler contexts. |
| 352 | self.check_split("foo (bar) baz", |
| 353 | ["foo", " ", "(bar)", " ", "baz"]) |
| 354 | self.check_split("blah (ding dong), wubba", |
| 355 | ["blah", " ", "(ding", " ", "dong),", |
| 356 | " ", "wubba"]) |
Greg Ward | 24a1c9c | 2002-08-22 21:12:54 +0000 | [diff] [blame] | 357 | |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 358 | def test_drop_whitespace_false(self): |
| 359 | # Check that drop_whitespace=False preserves whitespace. |
| 360 | # SF patch #1581073 |
| 361 | text = " This is a sentence with much whitespace." |
| 362 | self.check_wrap(text, 10, |
| 363 | [" This is a", " ", "sentence ", |
| 364 | "with ", "much white", "space."], |
| 365 | drop_whitespace=False) |
| 366 | |
| 367 | def test_drop_whitespace_false_whitespace_only(self): |
| 368 | # Check that drop_whitespace=False preserves a whitespace-only string. |
| 369 | self.check_wrap(" ", 6, [" "], drop_whitespace=False) |
| 370 | |
| 371 | def test_drop_whitespace_false_whitespace_only_with_indent(self): |
| 372 | # Check that a whitespace-only string gets indented (when |
| 373 | # drop_whitespace is False). |
| 374 | self.check_wrap(" ", 6, [" "], drop_whitespace=False, |
| 375 | initial_indent=" ") |
| 376 | |
| 377 | def test_drop_whitespace_whitespace_only(self): |
| 378 | # Check drop_whitespace on a whitespace-only string. |
| 379 | self.check_wrap(" ", 6, []) |
| 380 | |
| 381 | def test_drop_whitespace_leading_whitespace(self): |
| 382 | # Check that drop_whitespace does not drop leading whitespace (if |
| 383 | # followed by non-whitespace). |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 384 | # SF bug #622849 reported inconsistent handling of leading |
| 385 | # whitespace; let's test that a bit, shall we? |
| 386 | text = " This is a sentence with leading whitespace." |
| 387 | self.check_wrap(text, 50, |
| 388 | [" This is a sentence with leading whitespace."]) |
| 389 | self.check_wrap(text, 30, |
| 390 | [" This is a sentence with", "leading whitespace."]) |
| 391 | |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 392 | def test_drop_whitespace_whitespace_line(self): |
| 393 | # Check that drop_whitespace skips the whole line if a non-leading |
| 394 | # line consists only of whitespace. |
| 395 | text = "abcd efgh" |
| 396 | # Include the result for drop_whitespace=False for comparison. |
| 397 | self.check_wrap(text, 6, ["abcd", " ", "efgh"], |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 398 | drop_whitespace=False) |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 399 | self.check_wrap(text, 6, ["abcd", "efgh"]) |
| 400 | |
| 401 | def test_drop_whitespace_whitespace_only_with_indent(self): |
| 402 | # Check that initial_indent is not applied to a whitespace-only |
| 403 | # string. This checks a special case of the fact that dropping |
| 404 | # whitespace occurs before indenting. |
| 405 | self.check_wrap(" ", 6, [], initial_indent="++") |
| 406 | |
| 407 | def test_drop_whitespace_whitespace_indent(self): |
| 408 | # Check that drop_whitespace does not drop whitespace indents. |
| 409 | # This checks a special case of the fact that dropping whitespace |
| 410 | # occurs before indenting. |
| 411 | self.check_wrap("abcd efgh", 6, [" abcd", " efgh"], |
| 412 | initial_indent=" ", subsequent_indent=" ") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 413 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 414 | def test_split(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 415 | # Ensure that the standard _split() method works as advertised |
| 416 | # in the comments |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 417 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 418 | text = "Hello there -- you goof-ball, use the -b option!" |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 419 | |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 420 | result = self.wrapper._split(text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 421 | self.check(result, |
| 422 | ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", |
| 423 | "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) |
| 424 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 425 | def test_break_on_hyphens(self): |
| 426 | # Ensure that the break_on_hyphens attributes work |
| 427 | text = "yaba daba-doo" |
| 428 | self.check_wrap(text, 10, ["yaba daba-", "doo"], |
| 429 | break_on_hyphens=True) |
| 430 | self.check_wrap(text, 10, ["yaba", "daba-doo"], |
| 431 | break_on_hyphens=False) |
| 432 | |
Greg Ward | 4912857 | 2003-05-07 00:54:42 +0000 | [diff] [blame] | 433 | def test_bad_width(self): |
| 434 | # Ensure that width <= 0 is caught. |
| 435 | text = "Whatever, it doesn't matter." |
| 436 | self.assertRaises(ValueError, wrap, text, 0) |
| 437 | self.assertRaises(ValueError, wrap, text, -1) |
| 438 | |
Antoine Pitrou | 7c59bc6 | 2008-12-13 23:20:54 +0000 | [diff] [blame] | 439 | def test_no_split_at_umlaut(self): |
| 440 | text = "Die Empf\xe4nger-Auswahl" |
| 441 | self.check_wrap(text, 13, ["Die", "Empf\xe4nger-", "Auswahl"]) |
| 442 | |
| 443 | def test_umlaut_followed_by_dash(self): |
| 444 | text = "aa \xe4\xe4-\xe4\xe4" |
| 445 | self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"]) |
| 446 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 447 | |
Serhiy Storchaka | acc9f3f | 2013-10-15 21:22:54 +0300 | [diff] [blame] | 448 | class MaxLinesTestCase(BaseTestCase): |
| 449 | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
| 450 | |
| 451 | def test_simple(self): |
| 452 | self.check_wrap(self.text, 12, |
| 453 | ["Hello [...]"], |
| 454 | max_lines=0) |
| 455 | self.check_wrap(self.text, 12, |
| 456 | ["Hello [...]"], |
| 457 | max_lines=1) |
| 458 | self.check_wrap(self.text, 12, |
| 459 | ["Hello there,", |
| 460 | "how [...]"], |
| 461 | max_lines=2) |
| 462 | self.check_wrap(self.text, 13, |
| 463 | ["Hello there,", |
| 464 | "how are [...]"], |
| 465 | max_lines=2) |
| 466 | self.check_wrap(self.text, 80, [self.text], max_lines=1) |
| 467 | self.check_wrap(self.text, 12, |
| 468 | ["Hello there,", |
| 469 | "how are you", |
| 470 | "this fine", |
| 471 | "day? I'm", |
| 472 | "glad to hear", |
| 473 | "it!"], |
| 474 | max_lines=6) |
| 475 | |
| 476 | def test_spaces(self): |
| 477 | # strip spaces before placeholder |
| 478 | self.check_wrap(self.text, 12, |
| 479 | ["Hello there,", |
| 480 | "how are you", |
| 481 | "this fine", |
| 482 | "day? [...]"], |
| 483 | max_lines=4) |
| 484 | # placeholder at the start of line |
| 485 | self.check_wrap(self.text, 6, |
| 486 | ["Hello", |
| 487 | "[...]"], |
| 488 | max_lines=2) |
| 489 | # final spaces |
| 490 | self.check_wrap(self.text + ' ' * 10, 12, |
| 491 | ["Hello there,", |
| 492 | "how are you", |
| 493 | "this fine", |
| 494 | "day? I'm", |
| 495 | "glad to hear", |
| 496 | "it!"], |
| 497 | max_lines=6) |
| 498 | |
| 499 | def test_placeholder(self): |
| 500 | self.check_wrap(self.text, 12, |
| 501 | ["Hello..."], |
| 502 | max_lines=1, |
| 503 | placeholder='...') |
| 504 | self.check_wrap(self.text, 12, |
| 505 | ["Hello there,", |
| 506 | "how are..."], |
| 507 | max_lines=2, |
| 508 | placeholder='...') |
| 509 | # long placeholder and indentation |
| 510 | with self.assertRaises(ValueError): |
| 511 | wrap(self.text, 16, initial_indent=' ', |
| 512 | max_lines=1, placeholder=' [truncated]...') |
| 513 | with self.assertRaises(ValueError): |
| 514 | wrap(self.text, 16, subsequent_indent=' ', |
| 515 | max_lines=2, placeholder=' [truncated]...') |
| 516 | self.check_wrap(self.text, 16, |
| 517 | [" Hello there,", |
| 518 | " [truncated]..."], |
| 519 | max_lines=2, |
| 520 | initial_indent=' ', |
| 521 | subsequent_indent=' ', |
| 522 | placeholder=' [truncated]...') |
| 523 | self.check_wrap(self.text, 16, |
| 524 | [" [truncated]..."], |
| 525 | max_lines=1, |
| 526 | initial_indent=' ', |
| 527 | subsequent_indent=' ', |
| 528 | placeholder=' [truncated]...') |
| 529 | self.check_wrap(self.text, 80, [self.text], placeholder='.' * 1000) |
| 530 | |
| 531 | |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 532 | class LongWordTestCase (BaseTestCase): |
| 533 | def setUp(self): |
| 534 | self.wrapper = TextWrapper() |
Greg Ward | 24cbbcb | 2002-12-09 16:27:15 +0000 | [diff] [blame] | 535 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 536 | Did you say "supercalifragilisticexpialidocious?" |
| 537 | How *do* you spell that odd word, anyways? |
| 538 | ''' |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 539 | |
| 540 | def test_break_long(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 541 | # Wrap text with long words and lots of punctuation |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 542 | |
| 543 | self.check_wrap(self.text, 30, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 544 | ['Did you say "supercalifragilis', |
| 545 | 'ticexpialidocious?" How *do*', |
| 546 | 'you spell that odd word,', |
| 547 | 'anyways?']) |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 548 | self.check_wrap(self.text, 50, |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 549 | ['Did you say "supercalifragilisticexpialidocious?"', |
| 550 | 'How *do* you spell that odd word, anyways?']) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 551 | |
Raymond Hettinger | c11dbcd | 2003-08-30 14:43:55 +0000 | [diff] [blame] | 552 | # SF bug 797650. Prevent an infinite loop by making sure that at |
| 553 | # least one character gets split off on every pass. |
| 554 | self.check_wrap('-'*10+'hello', 10, |
| 555 | ['----------', |
| 556 | ' h', |
| 557 | ' e', |
| 558 | ' l', |
| 559 | ' l', |
| 560 | ' o'], |
| 561 | subsequent_indent = ' '*15) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 562 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 563 | # bug 1146. Prevent a long word to be wrongly wrapped when the |
| 564 | # preceding word is exactly one character shorter than the width |
| 565 | self.check_wrap(self.text, 12, |
| 566 | ['Did you say ', |
| 567 | '"supercalifr', |
| 568 | 'agilisticexp', |
| 569 | 'ialidocious?', |
| 570 | '" How *do*', |
| 571 | 'you spell', |
| 572 | 'that odd', |
| 573 | 'word,', |
| 574 | 'anyways?']) |
| 575 | |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 576 | def test_nobreak_long(self): |
| 577 | # Test with break_long_words disabled |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 578 | self.wrapper.break_long_words = 0 |
| 579 | self.wrapper.width = 30 |
Greg Ward | ee41384 | 2002-08-22 18:55:38 +0000 | [diff] [blame] | 580 | expect = ['Did you say', |
| 581 | '"supercalifragilisticexpialidocious?"', |
| 582 | 'How *do* you spell that odd', |
| 583 | 'word, anyways?' |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 584 | ] |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 585 | result = self.wrapper.wrap(self.text) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 586 | self.check(result, expect) |
| 587 | |
| 588 | # Same thing with kwargs passed to standalone wrap() function. |
Greg Ward | fd030e4 | 2002-08-22 19:02:37 +0000 | [diff] [blame] | 589 | result = wrap(self.text, width=30, break_long_words=0) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 590 | self.check(result, expect) |
| 591 | |
Serhiy Storchaka | acc9f3f | 2013-10-15 21:22:54 +0300 | [diff] [blame] | 592 | def test_max_lines_long(self): |
| 593 | self.check_wrap(self.text, 12, |
| 594 | ['Did you say ', |
| 595 | '"supercalifr', |
| 596 | 'agilisticexp', |
| 597 | '[...]'], |
| 598 | max_lines=4) |
| 599 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 600 | |
Greg Ward | 13c53c6 | 2002-08-22 18:57:26 +0000 | [diff] [blame] | 601 | class IndentTestCases(BaseTestCase): |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 602 | |
| 603 | # called before each test method |
| 604 | def setUp(self): |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 605 | self.text = '''\ |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 606 | This paragraph will be filled, first without any indentation, |
| 607 | and then with some (including a hanging indent).''' |
| 608 | |
| 609 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 610 | def test_fill(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 611 | # Test the fill() method |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 612 | |
| 613 | expect = '''\ |
| 614 | This paragraph will be filled, first |
| 615 | without any indentation, and then with |
| 616 | some (including a hanging indent).''' |
| 617 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 618 | result = fill(self.text, 40) |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 619 | self.check(result, expect) |
| 620 | |
| 621 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 622 | def test_initial_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 623 | # Test initial_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 624 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 625 | expect = [" This paragraph will be filled,", |
| 626 | "first without any indentation, and then", |
| 627 | "with some (including a hanging indent)."] |
| 628 | result = wrap(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 629 | self.check(result, expect) |
| 630 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 631 | expect = "\n".join(expect) |
| 632 | result = fill(self.text, 40, initial_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 633 | self.check(result, expect) |
| 634 | |
| 635 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 636 | def test_subsequent_indent(self): |
Guido van Rossum | 327af77 | 2002-08-22 20:13:47 +0000 | [diff] [blame] | 637 | # Test subsequent_indent parameter |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 638 | |
| 639 | expect = '''\ |
| 640 | * This paragraph will be filled, first |
| 641 | without any indentation, and then |
| 642 | with some (including a hanging |
| 643 | indent).''' |
| 644 | |
Greg Ward | f69d3c9 | 2002-08-22 19:06:45 +0000 | [diff] [blame] | 645 | result = fill(self.text, 40, |
| 646 | initial_indent=" * ", subsequent_indent=" ") |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 647 | self.check(result, expect) |
| 648 | |
| 649 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 650 | # Despite the similar names, DedentTestCase is *not* the inverse |
| 651 | # of IndentTestCase! |
| 652 | class DedentTestCase(unittest.TestCase): |
| 653 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 654 | def assertUnchanged(self, text): |
| 655 | """assert that dedent() has no effect on 'text'""" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 656 | self.assertEqual(text, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 657 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 658 | def test_dedent_nomargin(self): |
| 659 | # No lines indented. |
| 660 | text = "Hello there.\nHow are you?\nOh good, I'm glad." |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 661 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 662 | |
| 663 | # Similar, with a blank line. |
| 664 | text = "Hello there.\n\nBoo!" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 665 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 666 | |
| 667 | # Some lines indented, but overall margin is still zero. |
| 668 | text = "Hello there.\n This is indented." |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 669 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 670 | |
| 671 | # Again, add a blank line. |
| 672 | text = "Hello there.\n\n Boo!\n" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 673 | self.assertUnchanged(text) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 674 | |
| 675 | def test_dedent_even(self): |
| 676 | # All lines indented by two spaces. |
| 677 | text = " Hello there.\n How are ya?\n Oh good." |
| 678 | expect = "Hello there.\nHow are ya?\nOh good." |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 679 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 680 | |
| 681 | # Same, with blank lines. |
| 682 | text = " Hello there.\n\n How are ya?\n Oh good.\n" |
| 683 | expect = "Hello there.\n\nHow are ya?\nOh good.\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 684 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 685 | |
| 686 | # Now indent one of the blank lines. |
| 687 | text = " Hello there.\n \n How are ya?\n Oh good.\n" |
| 688 | expect = "Hello there.\n\nHow are ya?\nOh good.\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 689 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 690 | |
| 691 | def test_dedent_uneven(self): |
| 692 | # Lines indented unevenly. |
| 693 | text = '''\ |
| 694 | def foo(): |
| 695 | while 1: |
| 696 | return foo |
| 697 | ''' |
| 698 | expect = '''\ |
| 699 | def foo(): |
| 700 | while 1: |
| 701 | return foo |
| 702 | ''' |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 703 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 704 | |
| 705 | # Uneven indentation with a blank line. |
| 706 | text = " Foo\n Bar\n\n Baz\n" |
| 707 | expect = "Foo\n Bar\n\n Baz\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 708 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 709 | |
| 710 | # Uneven indentation with a whitespace-only line. |
| 711 | text = " Foo\n Bar\n \n Baz\n" |
| 712 | expect = "Foo\n Bar\n\n Baz\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 713 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 714 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 715 | # dedent() should not mangle internal tabs |
| 716 | def test_dedent_preserve_internal_tabs(self): |
| 717 | text = " hello\tthere\n how are\tyou?" |
| 718 | expect = "hello\tthere\nhow are\tyou?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 719 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 720 | |
| 721 | # make sure that it preserves tabs when it's not making any |
| 722 | # changes at all |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 723 | self.assertEqual(expect, dedent(expect)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 724 | |
| 725 | # dedent() should not mangle tabs in the margin (i.e. |
| 726 | # tabs and spaces both count as margin, but are *not* |
| 727 | # considered equivalent) |
| 728 | def test_dedent_preserve_margin_tabs(self): |
| 729 | text = " hello there\n\thow are you?" |
| 730 | self.assertUnchanged(text) |
| 731 | |
| 732 | # same effect even if we have 8 spaces |
| 733 | text = " hello there\n\thow are you?" |
| 734 | self.assertUnchanged(text) |
| 735 | |
| 736 | # dedent() only removes whitespace that can be uniformly removed! |
| 737 | text = "\thello there\n\thow are you?" |
| 738 | expect = "hello there\nhow are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 739 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 740 | |
| 741 | text = " \thello there\n \thow are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 742 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 743 | |
| 744 | text = " \t hello there\n \t how are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 745 | self.assertEqual(expect, dedent(text)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 746 | |
| 747 | text = " \thello there\n \t how are you?" |
| 748 | expect = "hello there\n how are you?" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 749 | self.assertEqual(expect, dedent(text)) |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 750 | |
Serhiy Storchaka | ea4cb63 | 2015-10-28 21:39:36 +0200 | [diff] [blame] | 751 | # test margin is smaller than smallest indent |
| 752 | text = " \thello there\n \thow are you?\n \tI'm fine, thanks" |
| 753 | expect = " \thello there\n \thow are you?\n\tI'm fine, thanks" |
| 754 | self.assertEqual(expect, dedent(text)) |
| 755 | |
Greg Ward | 9e082f4 | 2003-05-08 01:58:26 +0000 | [diff] [blame] | 756 | |
Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 757 | # Test textwrap.indent |
| 758 | class IndentTestCase(unittest.TestCase): |
| 759 | # The examples used for tests. If any of these change, the expected |
| 760 | # results in the various test cases must also be updated. |
| 761 | # The roundtrip cases are separate, because textwrap.dedent doesn't |
| 762 | # handle Windows line endings |
| 763 | ROUNDTRIP_CASES = ( |
| 764 | # Basic test case |
| 765 | "Hi.\nThis is a test.\nTesting.", |
| 766 | # Include a blank line |
| 767 | "Hi.\nThis is a test.\n\nTesting.", |
| 768 | # Include leading and trailing blank lines |
| 769 | "\nHi.\nThis is a test.\nTesting.\n", |
| 770 | ) |
| 771 | CASES = ROUNDTRIP_CASES + ( |
| 772 | # Use Windows line endings |
| 773 | "Hi.\r\nThis is a test.\r\nTesting.\r\n", |
| 774 | # Pathological case |
| 775 | "\nHi.\r\nThis is a test.\n\r\nTesting.\r\n\n", |
| 776 | ) |
| 777 | |
| 778 | def test_indent_nomargin_default(self): |
| 779 | # indent should do nothing if 'prefix' is empty. |
| 780 | for text in self.CASES: |
| 781 | self.assertEqual(indent(text, ''), text) |
| 782 | |
| 783 | def test_indent_nomargin_explicit_default(self): |
| 784 | # The same as test_indent_nomargin, but explicitly requesting |
| 785 | # the default behaviour by passing None as the predicate |
| 786 | for text in self.CASES: |
| 787 | self.assertEqual(indent(text, '', None), text) |
| 788 | |
| 789 | def test_indent_nomargin_all_lines(self): |
| 790 | # The same as test_indent_nomargin, but using the optional |
| 791 | # predicate argument |
| 792 | predicate = lambda line: True |
| 793 | for text in self.CASES: |
| 794 | self.assertEqual(indent(text, '', predicate), text) |
| 795 | |
| 796 | def test_indent_no_lines(self): |
| 797 | # Explicitly skip indenting any lines |
| 798 | predicate = lambda line: False |
| 799 | for text in self.CASES: |
| 800 | self.assertEqual(indent(text, ' ', predicate), text) |
| 801 | |
| 802 | def test_roundtrip_spaces(self): |
| 803 | # A whitespace prefix should roundtrip with dedent |
| 804 | for text in self.ROUNDTRIP_CASES: |
| 805 | self.assertEqual(dedent(indent(text, ' ')), text) |
| 806 | |
| 807 | def test_roundtrip_tabs(self): |
| 808 | # A whitespace prefix should roundtrip with dedent |
| 809 | for text in self.ROUNDTRIP_CASES: |
| 810 | self.assertEqual(dedent(indent(text, '\t\t')), text) |
| 811 | |
| 812 | def test_roundtrip_mixed(self): |
| 813 | # A whitespace prefix should roundtrip with dedent |
| 814 | for text in self.ROUNDTRIP_CASES: |
| 815 | self.assertEqual(dedent(indent(text, ' \t \t ')), text) |
| 816 | |
| 817 | def test_indent_default(self): |
| 818 | # Test default indenting of lines that are not whitespace only |
| 819 | prefix = ' ' |
| 820 | expected = ( |
| 821 | # Basic test case |
| 822 | " Hi.\n This is a test.\n Testing.", |
| 823 | # Include a blank line |
| 824 | " Hi.\n This is a test.\n\n Testing.", |
| 825 | # Include leading and trailing blank lines |
| 826 | "\n Hi.\n This is a test.\n Testing.\n", |
| 827 | # Use Windows line endings |
| 828 | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 829 | # Pathological case |
| 830 | "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n", |
| 831 | ) |
| 832 | for text, expect in zip(self.CASES, expected): |
| 833 | self.assertEqual(indent(text, prefix), expect) |
| 834 | |
| 835 | def test_indent_explicit_default(self): |
| 836 | # Test default indenting of lines that are not whitespace only |
| 837 | prefix = ' ' |
| 838 | expected = ( |
| 839 | # Basic test case |
| 840 | " Hi.\n This is a test.\n Testing.", |
| 841 | # Include a blank line |
| 842 | " Hi.\n This is a test.\n\n Testing.", |
| 843 | # Include leading and trailing blank lines |
| 844 | "\n Hi.\n This is a test.\n Testing.\n", |
| 845 | # Use Windows line endings |
| 846 | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 847 | # Pathological case |
| 848 | "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n", |
| 849 | ) |
| 850 | for text, expect in zip(self.CASES, expected): |
| 851 | self.assertEqual(indent(text, prefix, None), expect) |
| 852 | |
| 853 | def test_indent_all_lines(self): |
| 854 | # Add 'prefix' to all lines, including whitespace-only ones. |
| 855 | prefix = ' ' |
| 856 | expected = ( |
| 857 | # Basic test case |
| 858 | " Hi.\n This is a test.\n Testing.", |
| 859 | # Include a blank line |
| 860 | " Hi.\n This is a test.\n \n Testing.", |
| 861 | # Include leading and trailing blank lines |
| 862 | " \n Hi.\n This is a test.\n Testing.\n", |
| 863 | # Use Windows line endings |
| 864 | " Hi.\r\n This is a test.\r\n Testing.\r\n", |
| 865 | # Pathological case |
| 866 | " \n Hi.\r\n This is a test.\n \r\n Testing.\r\n \n", |
| 867 | ) |
| 868 | predicate = lambda line: True |
| 869 | for text, expect in zip(self.CASES, expected): |
| 870 | self.assertEqual(indent(text, prefix, predicate), expect) |
| 871 | |
| 872 | def test_indent_empty_lines(self): |
| 873 | # Add 'prefix' solely to whitespace-only lines. |
| 874 | prefix = ' ' |
| 875 | expected = ( |
| 876 | # Basic test case |
| 877 | "Hi.\nThis is a test.\nTesting.", |
| 878 | # Include a blank line |
| 879 | "Hi.\nThis is a test.\n \nTesting.", |
| 880 | # Include leading and trailing blank lines |
| 881 | " \nHi.\nThis is a test.\nTesting.\n", |
| 882 | # Use Windows line endings |
| 883 | "Hi.\r\nThis is a test.\r\nTesting.\r\n", |
| 884 | # Pathological case |
| 885 | " \nHi.\r\nThis is a test.\n \r\nTesting.\r\n \n", |
| 886 | ) |
| 887 | predicate = lambda line: not line.strip() |
| 888 | for text, expect in zip(self.CASES, expected): |
| 889 | self.assertEqual(indent(text, prefix, predicate), expect) |
| 890 | |
| 891 | |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 892 | class ShortenTestCase(BaseTestCase): |
| 893 | |
Serhiy Storchaka | acc9f3f | 2013-10-15 21:22:54 +0300 | [diff] [blame] | 894 | def check_shorten(self, text, width, expect, **kwargs): |
| 895 | result = shorten(text, width, **kwargs) |
| 896 | self.check(result, expect) |
| 897 | |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 898 | def test_simple(self): |
| 899 | # Simple case: just words, spaces, and a bit of punctuation |
| 900 | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
| 901 | |
Antoine Pitrou | c593056 | 2013-08-16 22:31:12 +0200 | [diff] [blame] | 902 | self.check_shorten(text, 18, "Hello there, [...]") |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 903 | self.check_shorten(text, len(text), text) |
| 904 | self.check_shorten(text, len(text) - 1, |
| 905 | "Hello there, how are you this fine day? " |
Antoine Pitrou | c593056 | 2013-08-16 22:31:12 +0200 | [diff] [blame] | 906 | "I'm glad to [...]") |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 907 | |
| 908 | def test_placeholder(self): |
| 909 | text = "Hello there, how are you this fine day? I'm glad to hear it!" |
| 910 | |
| 911 | self.check_shorten(text, 17, "Hello there,$$", placeholder='$$') |
| 912 | self.check_shorten(text, 18, "Hello there, how$$", placeholder='$$') |
| 913 | self.check_shorten(text, 18, "Hello there, $$", placeholder=' $$') |
| 914 | self.check_shorten(text, len(text), text, placeholder='$$') |
| 915 | self.check_shorten(text, len(text) - 1, |
| 916 | "Hello there, how are you this fine day? " |
| 917 | "I'm glad to hear$$", placeholder='$$') |
| 918 | |
| 919 | def test_empty_string(self): |
| 920 | self.check_shorten("", 6, "") |
| 921 | |
| 922 | def test_whitespace(self): |
| 923 | # Whitespace collapsing |
| 924 | text = """ |
| 925 | This is a paragraph that already has |
| 926 | line breaks and \t tabs too.""" |
| 927 | self.check_shorten(text, 62, |
| 928 | "This is a paragraph that already has line " |
| 929 | "breaks and tabs too.") |
| 930 | self.check_shorten(text, 61, |
| 931 | "This is a paragraph that already has line " |
Antoine Pitrou | c593056 | 2013-08-16 22:31:12 +0200 | [diff] [blame] | 932 | "breaks and [...]") |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 933 | |
| 934 | self.check_shorten("hello world! ", 12, "hello world!") |
Antoine Pitrou | c593056 | 2013-08-16 22:31:12 +0200 | [diff] [blame] | 935 | self.check_shorten("hello world! ", 11, "hello [...]") |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 936 | # The leading space is trimmed from the placeholder |
| 937 | # (it would be ugly otherwise). |
Antoine Pitrou | c593056 | 2013-08-16 22:31:12 +0200 | [diff] [blame] | 938 | self.check_shorten("hello world! ", 10, "[...]") |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 939 | |
| 940 | def test_width_too_small_for_placeholder(self): |
Serhiy Storchaka | acc9f3f | 2013-10-15 21:22:54 +0300 | [diff] [blame] | 941 | shorten("x" * 20, width=8, placeholder="(......)") |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 942 | with self.assertRaises(ValueError): |
Serhiy Storchaka | acc9f3f | 2013-10-15 21:22:54 +0300 | [diff] [blame] | 943 | shorten("x" * 20, width=8, placeholder="(.......)") |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 944 | |
| 945 | def test_first_word_too_long_but_placeholder_fits(self): |
Antoine Pitrou | c593056 | 2013-08-16 22:31:12 +0200 | [diff] [blame] | 946 | self.check_shorten("Helloo", 5, "[...]") |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 947 | |
Greg Ward | f676578 | 2002-08-22 18:35:49 +0000 | [diff] [blame] | 948 | |
Greg Ward | 90c0b07 | 2002-08-22 18:11:10 +0000 | [diff] [blame] | 949 | if __name__ == '__main__': |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 950 | unittest.main() |