import unittest import textwrap import pytest # local import from main.py instead of original modules from main import Terminal # Assuming Terminal is the class that includes the wrap method from .conftest import TEST_QUICK from .accessories import as_subprocess # Assuming as_subprocess is still required 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: # test only one feature: everything on 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() # Updated to use the Terminal class from main.py 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 # assigned but never used 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() # Updated to use the Terminal class from main.py attributes = ('bright_red', 'on_bright_blue', 'underline', 'reverse', 'red_reverse', 'red_on_white', 'on_bright_white') for attr in attributes: getattr(term, attr)('x') # Dynamic calling of terminal attributes 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) # ensure we textwrap ascii the same as python self.assertEqual(internal_wrapped, my_wrapped) # ensure content matches for each line, when the sequences are # stripped back off of each line for left, right in zip(internal_wrapped, my_wrapped_colored): self.assertEqual(left, term.strip_seqs(right)) # ensure our colored textwrap is the same paragraph length 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() # Updated to use the Terminal class from main.py 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()