Skip to content

Commit 38c5029

Browse files
committed
add tests for untested branches of _CustomTextWrap._wrap_chunks
1 parent 443874b commit 38c5029

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

test/test_textwrapper.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,41 @@ def test_wrap_wide_char_no_column_overflow():
346346
)
347347

348348

349+
def test_wrap_max_lines_placeholder_on_current_line():
350+
"""TextWrapper: max_lines truncation appends placeholder to current line after
351+
popping trailing words that don't leave room for it (while-loop A.inner branch)"""
352+
# Line 2 has "four five six" but "six" gets popped, then "five [...]" fits in 15
353+
wrapper = CTW(width=15, max_lines=2)
354+
result = wrapper.wrap("one two three four five six seven eight")
355+
assert_equal(["one two three", "four five [...]"], result)
356+
357+
358+
def test_wrap_max_lines_placeholder_appended_to_previous_line():
359+
"""TextWrapper: max_lines truncation appends placeholder to the previous line
360+
when the current line is entirely too long (while-loop else, B.1.a branch)"""
361+
# "toolong" alone overflows with placeholder, but prev line "ab" has room for " [...]"
362+
wrapper = CTW(width=8, max_lines=2)
363+
result = wrapper.wrap("ab toolong extra")
364+
assert_equal(["ab [...]"], result)
365+
366+
367+
def test_wrap_max_lines_placeholder_alone_no_previous_lines():
368+
"""TextWrapper: max_lines=1 with an unbreakable word emits placeholder alone
369+
when there are no previous lines (while-loop else, B.2 branch)"""
370+
wrapper = CTW(width=5, max_lines=1, break_long_words=False)
371+
result = wrapper.wrap("toolong extra")
372+
assert_equal(["[...]"], result)
373+
374+
375+
def test_wrap_max_lines_placeholder_alone_previous_line_too_full():
376+
"""TextWrapper: max_lines truncation emits placeholder as a new line when
377+
the previous line has no room for it either (while-loop else, B.1.b branch)"""
378+
# prev line "hello"(5) + " [...]"(6) = 11 > width(5), so placeholder becomes its own line
379+
wrapper = CTW(width=5, max_lines=2, break_long_words=False)
380+
result = wrapper.wrap("hello toolong extra")
381+
assert_equal(["hello", "[...]"], result)
382+
383+
349384
def test_wrap_wide_char_narrower_than_char_width():
350385
"""TextWrapper: column width smaller than a single wide char must not hang (issue #399).
351386

0 commit comments

Comments
 (0)