|
1 | 1 | """Discretely test functionality of our custom TextWrapper""" |
2 | 2 | import datetime |
3 | 3 |
|
4 | | -from tabulate import _CustomTextWrap as CTW, tabulate |
| 4 | +from tabulate import _CustomTextWrap as CTW, tabulate, _strip_ansi |
5 | 5 | from textwrap import TextWrapper as OTW |
6 | 6 |
|
7 | 7 | from common import skip, assert_equal |
@@ -157,6 +157,42 @@ def test_wrap_color_line_splillover(): |
157 | 157 | assert_equal(expected, result) |
158 | 158 |
|
159 | 159 |
|
| 160 | +def test_wrap_color_line_longword(): |
| 161 | + """TextWrapper: Wrap a line - preserve internal color tags and wrap them to |
| 162 | + other lines when required, requires adding the colors tags to other lines as appropriate |
| 163 | + and avoiding splitting escape codes.""" |
| 164 | + data = "This_is_a_\033[31mtest_string_for_testing_TextWrap\033[0m_with_colors" |
| 165 | + |
| 166 | + expected = [ |
| 167 | + "This_is_a_\033[31mte\033[0m", |
| 168 | + "\033[31mst_string_fo\033[0m", |
| 169 | + "\033[31mr_testing_Te\033[0m", |
| 170 | + "\033[31mxtWrap\033[0m_with_", |
| 171 | + "colors", |
| 172 | + ] |
| 173 | + wrapper = CTW(width=12) |
| 174 | + result = wrapper.wrap(data) |
| 175 | + assert_equal(expected, result) |
| 176 | + |
| 177 | + |
| 178 | +def test_wrap_color_line_multiple_escapes(): |
| 179 | + data = "012345(\x1b[32ma\x1b[0mbc\x1b[32mdefghij\x1b[0m)" |
| 180 | + expected = [ |
| 181 | + "012345(\x1b[32ma\x1b[0mbc\x1b[32m\x1b[0m", |
| 182 | + "\x1b[32mdefghij\x1b[0m)", |
| 183 | + ] |
| 184 | + wrapper = CTW(width=10) |
| 185 | + result = wrapper.wrap(data) |
| 186 | + assert_equal(expected, result) |
| 187 | + |
| 188 | + clean_data = _strip_ansi(data) |
| 189 | + for width in range(2, len(clean_data)): |
| 190 | + wrapper = CTW(width=width) |
| 191 | + result = wrapper.wrap(data) |
| 192 | + # Comparing after stripping ANSI should be enough to catch broken escape codes |
| 193 | + assert_equal(clean_data, _strip_ansi("".join(result))) |
| 194 | + |
| 195 | + |
160 | 196 | def test_wrap_datetime(): |
161 | 197 | """TextWrapper: Show that datetimes can be wrapped without crashing""" |
162 | 198 | data = [ |
|
0 commit comments