test_print_tokens.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """
  2. Test `shortcuts.print_tokens`.
  3. """
  4. from __future__ import unicode_literals
  5. from prompt_toolkit.shortcuts import print_tokens
  6. from prompt_toolkit.token import Token
  7. from prompt_toolkit.styles import style_from_dict
  8. class _Capture:
  9. " Emulate an stdout object. "
  10. encoding = 'utf-8'
  11. def __init__(self):
  12. self._data = []
  13. def write(self, data):
  14. self._data.append(data)
  15. @property
  16. def data(self):
  17. return b''.join(self._data)
  18. def flush(self):
  19. pass
  20. def isatty(self):
  21. return True
  22. def test_print_tokens():
  23. f = _Capture()
  24. print_tokens([(Token, 'hello'), (Token, 'world')], file=f)
  25. assert b'hello' in f.data
  26. assert b'world' in f.data
  27. def test_with_style():
  28. f = _Capture()
  29. style = style_from_dict({
  30. Token.Hello: '#ff0066',
  31. Token.World: '#44ff44 italic',
  32. })
  33. tokens = [
  34. (Token.Hello, 'Hello '),
  35. (Token.World, 'world'),
  36. ]
  37. print_tokens(tokens, style=style, file=f)
  38. assert b'\x1b[0;38;5;197mHello' in f.data
  39. assert b'\x1b[0;38;5;83;3mworld' in f.data