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_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(self, _, value): colorizer = Colorizer(color_map={ 'a': ('[', ']'), 'b': ('<', '>'), }) self.assertEqual( ColorizedObject(Mark(value, ['a', 'b']), ('[<', '>]')), colorizer.colorize(Mark(value, ['a', 'b'])), )
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_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_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_hexadecimal_int_rendering_of_marked(self, _, value): # Apparently in Python 3, %x expects a real integer. If you know how to # make it work with a Marked integer, please let me know ! if PY3: return self.assertEqual('%x' % value, '%x' % Mark(value, 'a'))
def test_csh_format_no_highlighter_no_color_support(self): colorizer = GenericColorizer(color_map={ 'bracket': ('[', ']'), }) formatter = ColorizingFormatter(fmt='%(message)s') color_stream = MagicMock() color_stream.isatty = lambda: False handler = ColorizingStreamHandler( stream=color_stream, colorizer=colorizer, ) handler.setFormatter(formatter) record = LogRecord( name='my_record', level=DEBUG, pathname='my_path', lineno=42, msg='%s + %s gives %s', args=( 4, 5, Mark(4 + 5, color_tag='bracket'), ), exc_info=None, ) self.assertEqual('4 + 5 gives 9', handler.format(record)) # Make sure that the colorizer attribute was removed after processing. self.assertFalse(hasattr(record, 'colorizer'))
def test_string_rendering_of_marked(self, _, value): # Python2 non-unicode string want to use the 'ascii' encoding for this # conversion, which cannot work. if PY2 and isinstance(value, unicode): return self.assertEqual('{0}'.format(value), '{0}'.format(Mark(value, 'a')))
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_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 test_marked_objects_can_be_nested_for(self, _, value): obj = Mark(Mark(value, 'b'), 'a') self.assertEqual(['a', 'b'], obj.color_tag) self.assertEqual(value, obj.obj) obj = Mark(Mark(value, ['b', 'c']), 'a') self.assertEqual(['a', 'b', 'c'], obj.color_tag) self.assertEqual(value, obj.obj) obj = Mark(Mark(value, 'c'), ['a', 'b']) self.assertEqual(['a', 'b', 'c'], obj.color_tag) self.assertEqual(value, obj.obj) obj = Mark(Mark(value, ['c', 'd']), ['a', 'b']) self.assertEqual(['a', 'b', 'c', 'd'], obj.color_tag) self.assertEqual(value, obj.obj)
def test_csh_format_with_context(self): colorizer = GenericColorizer(color_map={ 'bracket': ('[', ']'), 'context': ('{', '}'), }) highlighter = GenericColorizer(color_map={ 'bracket': ('<', '>'), 'context': ('(', ')'), }) formatter = ColorizingFormatter(fmt='%(levelname)s %(message)s') color_stream = MagicMock() color_stream.isatty = lambda: True handler = ColorizingStreamHandler( stream=color_stream, colorizer=colorizer, highlighter=highlighter, attributes_map={ 'message': 'context', 'levelname': 'bracket', }, ) handler.setFormatter(formatter) record = LogRecord( name='my_record', level=DEBUG, pathname='my_path', lineno=42, msg='%s + %s gives %s', args=( 4, 5, Mark(4 + 5, color_tag='bracket'), ), exc_info=None, ) self.assertEqual( '[DEBUG] {4 + 5 gives }{[9]}{}', handler.format(record), ) # Make sure that the colorizer attribute was removed after processing. self.assertFalse(hasattr(record, 'colorizer'))
def test_float_rendering_of_marked(self, _, value): self.assertEqual('%f' % value, '%f' % Mark(value, 'a'))
def test_marked_objects_dont_compare_to_their_value_as(self, _, value): self.assertNotEqual(value, Mark(value, 'a'))
def test_marked_objects_have_a_color_tag_attribute_for(self, _, value): self.assertTrue(hasattr(Mark(value, 'a'), 'color_tag')) self.assertTrue(hasattr(Mark(value, color_tag='info'), 'color_tag'), )
def test_unicode_rendering_of_marked(self, _, value): self.assertEqual(u'{0}'.format(value), u'{0}'.format(Mark(value, 'a')))
def test_explicit_unicode_in_python3(self): if PY3: self.assertEqual( u'test', Mark(u'test', 'foo').__unicode__(), )