test_term.py 1.7 KB

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