1
0

formatter_test.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import logging
  4. from compose.cli import colors
  5. from compose.cli.formatter import ConsoleWarningFormatter
  6. from tests import unittest
  7. MESSAGE = 'this is the message'
  8. def make_log_record(level, message=None):
  9. return logging.LogRecord('name', level, 'pathame', 0, message or MESSAGE, (), None)
  10. class ConsoleWarningFormatterTestCase(unittest.TestCase):
  11. def setUp(self):
  12. self.formatter = ConsoleWarningFormatter()
  13. def test_format_warn(self):
  14. output = self.formatter.format(make_log_record(logging.WARN))
  15. expected = colors.yellow('WARNING') + ': '
  16. assert output == expected + MESSAGE
  17. def test_format_error(self):
  18. output = self.formatter.format(make_log_record(logging.ERROR))
  19. expected = colors.red('ERROR') + ': '
  20. assert output == expected + MESSAGE
  21. def test_format_info(self):
  22. output = self.formatter.format(make_log_record(logging.INFO))
  23. assert output == MESSAGE
  24. def test_format_unicode_info(self):
  25. message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
  26. output = self.formatter.format(make_log_record(logging.INFO, message))
  27. assert output == message.decode('utf-8')
  28. def test_format_unicode_warn(self):
  29. message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
  30. output = self.formatter.format(make_log_record(logging.WARN, message))
  31. expected = colors.yellow('WARNING') + ': '
  32. assert output == '{0}{1}'.format(expected, message.decode('utf-8'))
  33. def test_format_unicode_error(self):
  34. message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
  35. output = self.formatter.format(make_log_record(logging.ERROR, message))
  36. expected = colors.red('ERROR') + ': '
  37. assert output == '{0}{1}'.format(expected, message.decode('utf-8'))