| | import unittest |
| | import textwrap |
| | import pytest |
| |
|
| | |
| | from main import Terminal |
| | from .conftest import TEST_QUICK |
| | from .accessories import as_subprocess |
| |
|
| | TEXTWRAP_KEYWORD_COMBINATIONS = [ |
| | {'break_long_words': False, 'drop_whitespace': False, 'subsequent_indent': ''}, |
| | {'break_long_words': False, 'drop_whitespace': True, 'subsequent_indent': ''}, |
| | {'break_long_words': False, 'drop_whitespace': False, 'subsequent_indent': ' '}, |
| | {'break_long_words': False, 'drop_whitespace': True, 'subsequent_indent': ' '}, |
| | {'break_long_words': True, 'drop_whitespace': False, 'subsequent_indent': ''}, |
| | {'break_long_words': True, 'drop_whitespace': True, 'subsequent_indent': ''}, |
| | {'break_long_words': True, 'drop_whitespace': False, 'subsequent_indent': ' '}, |
| | {'break_long_words': True, 'drop_whitespace': True, 'subsequent_indent': ' '}, |
| | ] |
| |
|
| | if TEST_QUICK: |
| | |
| | TEXTWRAP_KEYWORD_COMBINATIONS = [ |
| | {'break_long_words': True, 'drop_whitespace': True, 'subsequent_indent': ' '} |
| | ] |
| |
|
| | class TestTerminal(unittest.TestCase): |
| |
|
| | @pytest.mark.parametrize("kwargs", TEXTWRAP_KEYWORD_COMBINATIONS) |
| | def test_sequence_wrapper_invalid_width(self): |
| | """Test exception thrown from invalid width.""" |
| | WIDTH = -3 |
| |
|
| | @as_subprocess |
| | def child(): |
| | term = Terminal() |
| | try: |
| | my_wrapped = term.wrap(u'------- -------------', WIDTH) |
| | except ValueError as err: |
| | self.assertEqual(err.args[0], |
| | "invalid width %r(%s) (must be integer > 0)" % (WIDTH, type(WIDTH))) |
| | else: |
| | self.fail('Previous stmt should have raised exception.') |
| | del my_wrapped |
| |
|
| | child() |
| |
|
| | @pytest.mark.parametrize("kwargs", TEXTWRAP_KEYWORD_COMBINATIONS) |
| | def test_sequence_wrapper(self, many_columns, kwargs): |
| | """Test that text wrapping matches internal extra options.""" |
| | @as_subprocess |
| | def child(width, pgraph, kwargs): |
| | term = Terminal() |
| | attributes = ('bright_red', 'on_bright_blue', 'underline', |
| | 'reverse', 'red_reverse', 'red_on_white', 'on_bright_white') |
| |
|
| | for attr in attributes: |
| | getattr(term, attr)('x') |
| |
|
| | pgraph_colored = u''.join( |
| | getattr(term, (attributes[idx % len(attributes)]))(char) |
| | if char != u' ' else u' ' |
| | for idx, char in enumerate(pgraph) |
| | ) |
| |
|
| | internal_wrapped = textwrap.wrap(pgraph, width=width, **kwargs) |
| | my_wrapped = term.wrap(pgraph, width=width, **kwargs) |
| | my_wrapped_colored = term.wrap(pgraph_colored, width=width, **kwargs) |
| |
|
| | |
| | self.assertEqual(internal_wrapped, my_wrapped) |
| |
|
| | |
| | |
| | for left, right in zip(internal_wrapped, my_wrapped_colored): |
| | self.assertEqual(left, term.strip_seqs(right)) |
| |
|
| | |
| | self.assertEqual(len(internal_wrapped), len(my_wrapped_colored)) |
| |
|
| | child(width=many_columns, kwargs=kwargs, |
| | pgraph=u' Z! a bc defghij klmnopqrstuvw<<>>xyz012345678900 ' * 2) |
| | child(width=many_columns, kwargs=kwargs, pgraph=u'a bb ccc') |
| |
|
| | def test_multiline(self): |
| | """Test that text wrapping matches internal extra options.""" |
| |
|
| | @as_subprocess |
| | def child(): |
| | term = Terminal() |
| | given_string = ('\n' + (32 * 'A') + '\n' + |
| | (32 * 'B') + '\n' + |
| | (32 * 'C') + '\n\n') |
| | expected = [ |
| | '', |
| | 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', |
| | 'AA', |
| | 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB', |
| | 'BB', |
| | 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC', |
| | 'CC', |
| | '', |
| | ] |
| | result = term.wrap(given_string, width=30) |
| | self.assertEqual(expected, result) |
| |
|
| | child() |
| |
|
| | if __name__ == "__main__": |
| | unittest.main() |