test_term.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import pytest
  4. from case import skip
  5. from celery.five import text_t
  6. from celery.utils import term
  7. from celery.utils.term import colored, fg
  8. @skip.if_win32()
  9. class test_colored:
  10. @pytest.fixture(autouse=True)
  11. def preserve_encoding(self, patching):
  12. patching('sys.getdefaultencoding', 'utf-8')
  13. @pytest.mark.parametrize('name,color', [
  14. ('black', term.BLACK),
  15. ('red', term.RED),
  16. ('green', term.GREEN),
  17. ('yellow', term.YELLOW),
  18. ('blue', term.BLUE),
  19. ('magenta', term.MAGENTA),
  20. ('cyan', term.CYAN),
  21. ('white', term.WHITE),
  22. ])
  23. def test_colors(self, name, color):
  24. assert fg(30 + color) in str(colored().names[name]('foo'))
  25. @pytest.mark.parametrize('name', [
  26. 'bold', 'underline', 'blink', 'reverse', 'bright',
  27. 'ired', 'igreen', 'iyellow', 'iblue', 'imagenta',
  28. 'icyan', 'iwhite', 'reset',
  29. ])
  30. def test_modifiers(self, name):
  31. assert str(getattr(colored(), name)('f'))
  32. def test_unicode(self):
  33. assert text_t(colored().green('∂bar'))
  34. assert colored().red('éefoo') + colored().green('∂bar')
  35. assert colored().red('foo').no_color() == 'foo'
  36. def test_repr(self):
  37. assert repr(colored().blue('åfoo'))
  38. assert "''" in repr(colored())
  39. def test_more_unicode(self):
  40. c = colored()
  41. s = c.red('foo', c.blue('bar'), c.green('baz'))
  42. assert s.no_color()
  43. c._fold_no_color(s, 'øfoo')
  44. c._fold_no_color('fooå', s)
  45. c = colored().red('åfoo')
  46. assert c._add(c, 'baræ') == '\x1b[1;31m\xe5foo\x1b[0mbar\xe6'
  47. c2 = colored().blue('ƒƒz')
  48. c3 = c._add(c, c2)
  49. assert c3 == '\x1b[1;31m\xe5foo\x1b[0m\x1b[1;34m\u0192\u0192z\x1b[0m'