def test_colorizer_changes_colorizable_types(self, _, value): colorizer = Colorizer(color_map={ 'a': ('[', ']'), }) self.assertEqual( ColorizedObject(Mark(value, 'a'), ('[', ']')), colorizer.colorize(Mark(value, 'a')), )
def test_colorizer_changes_colorizable_types_with_tags(self, _, value): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('<', '>'), }) self.assertEqual( ColorizedObject(Mark(value, ['a', 'b']), ('[<', '>]')), colorizer.colorize(Mark(value, ['a', 'b'])), )
def test_colorizer_get_color_pair_not_found_with_default(self): colorizer = Colorizer( { 'a': ('[', ']'), 'b': ('<', '>'), }, default_color_tag='b', ) self.assertEqual(('<', '>'), colorizer.get_color_pair(color_tag=['c']))
def test_colorizer_get_color_pair_found_with_context(self): colorizer = Colorizer({ 'a': ('[', ']'), 'b': ('<', '>'), }, ) self.assertEqual( ('><[', ']><'), colorizer.get_color_pair(color_tag=['a'], context_color_tag='b'), )
def test_colorizer_get_color_pair_found_double(self): colorizer = Colorizer({ 'a': ('[', ']'), 'b': ('<', '>'), }) self.assertEqual( ('[<', '>]'), colorizer.get_color_pair(color_tag=['a', 'b']), )
def test_colorizer_doesnt_colorize_with_unknown_color_tag(self): colorizer = Colorizer(color_map={ 'my_tag': ('START_MARK', 'STOP_MARK'), }, ) result = colorizer.colorize(Mark('hello', color_tag='my_unknown_tag')) self.assertEqual( ColorizedObject(Mark('hello', 'my_unknown_tag'), ('', '')), result, )
def test_colorizer_changes_colorizable_types_with_context(self, _, value): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('<', '>'), }) self.assertEqual( ColorizedObject(Mark(value, 'a'), ('><[', ']><')), colorizer.colorize(Mark(value, 'a'), context_color_tag='b'), )
def test_colorizer_get_color_pair_found_with_context(self): colorizer = Colorizer( { 'a': ('[', ']'), 'b': ('<', '>'), }, ) self.assertEqual( ('><[', ']><'), colorizer.get_color_pair(color_tag=['a'], context_color_tag='b'), )
def test_colorizer_doesnt_colorize_with_unknown_color_tag(self): colorizer = Colorizer( color_map={ 'my_tag': ('START_MARK', 'STOP_MARK'), }, ) result = colorizer.colorize(Mark('hello', color_tag='my_unknown_tag')) self.assertEqual( ColorizedObject(Mark('hello', 'my_unknown_tag'), ('', '')), result, )
def test_colorizer_changes_colorizable_types_with_tags_and_context( self, _, value, ): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('(', ')'), 'c': ('<', '>'), }) self.assertEqual( ColorizedObject(Mark(value, ['a', 'b']), ('><[(', ')]><')), colorizer.colorize(Mark(value, ['a', 'b']), context_color_tag='c'), )
def test_colorizer_get_color_pair_found_with_list_context(self): colorizer = Colorizer( { 'a': ('[', ']'), 'b': ('<', '>'), 'c': ('(', ')'), }, ) self.assertEqual( (')><([', '])><('), colorizer.get_color_pair( color_tag=['a'], context_color_tag=['b', 'c'], ), )
def test_colorize_message(self): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('(', ')'), }) message = '{}-{}_{a}~{b}' args = [42, Mark(42, ['a', 'b'])] kwargs = { 'a': 0, 'b': Mark(0, ['b', 'a']), } self.assertEqual( '42-[(42)]_0~([0])', colorizer.colorize_message(message, *args, **kwargs), )
def test_colorize_message(self): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('(', ')'), }) message = '{0}-{1}_{a}~{b}' args = [42, Mark(42, ['a', 'b'])] kwargs = { 'a': 0, 'b': Mark(0, ['b', 'a']), } self.assertEqual( '42-[(42)]_0~([0])', colorizer.colorize_message(message, *args, **kwargs), )
def test_colorize_message_with_context(self): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('(', ')'), 'c': ('<', '>'), }) message = Mark('{0}-{1}_{a}~{b}', 'c') args = [42, Mark(42, ['a', 'b'])] kwargs = { 'a': 0, 'b': Mark(0, ['b', 'a']), } self.assertEqual( '<42-><[(42)]><_0~><([0])><>', colorizer.colorize_message(message, *args, **kwargs), )
def test_colorize_message_with_context(self): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('(', ')'), 'c': ('<', '>'), }) message = Mark('{}-{}_{a}~{b}', 'c') args = [42, Mark(42, ['a', 'b'])] kwargs = { 'a': 0, 'b': Mark(0, ['b', 'a']), } self.assertEqual( '<42-><[(42)]><_0~><([0])><>', colorizer.colorize_message(message, *args, **kwargs), )
def test_colorizer_colorizes_with_unknown_color_tag_and_default(self): colorizer = Colorizer( color_map={ 'my_tag': ('START_MARK', 'STOP_MARK'), 'default': ('START_DEFAULT_MARK', 'STOP_DEFAULT_MARK') }, default_color_tag='default', ) result = colorizer.colorize(Mark('hello', color_tag='my_unknown_tag')) self.assertEqual( ColorizedObject( Mark( 'hello', 'my_unknown_tag', ), ( 'START_DEFAULT_MARK', 'STOP_DEFAULT_MARK', ), ), result, )
def __init__(self, stream, colorizer=Colorizer()): """ Initialize the :class:`StreamDisplay`. :param stream: The stream to be attached too. """ super(StreamDisplay, self).__init__() self.colorizer = colorizer self.output_map = {} if stream_has_color_support(stream): self.stream = AnsiToWin32(stream).stream else: self.stream = stream # Python 3 differentiates binary streams. if hasattr(stream, 'buffer'): self.binary_stream = stream.buffer else: self.binary_stream = stream
def test_colorizer_get_color_pair_not_found(self): colorizer = Colorizer({}) self.assertEqual(('', ''), colorizer.get_color_pair(color_tag=['a']))
def test_colorizer_converts_unknown_types(self, _, value): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('<', '>'), }) self.assertEqual(ColorizedObject(value), colorizer.colorize(value))
def test_colorizer_get_color_pair_found(self): colorizer = Colorizer({ 'a': ('[', ']'), }) self.assertEqual(('[', ']'), colorizer.get_color_pair(color_tag=['a']))