xonsh/tests/test_ptk_highlight.py

117 lines
4.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""Test XonshLexer for pygments"""
import os
import builtins
2016-09-11 16:17:56 +08:00
import pytest
from pygments.token import (Keyword, Name, String, Error, Number,
Operator, Punctuation, Text)
from tools import skip_if_on_windows
from xonsh.built_ins import load_builtins, unload_builtins
from xonsh.pyghooks import XonshLexer
@pytest.yield_fixture(autouse=True)
def load_command_cache():
load_builtins()
if 'cd' not in builtins.aliases:
builtins.aliases['cd'] = lambda *args, **kwargs: None
yield
unload_builtins()
def check_token(code, tokens):
"""Make sure that all tokens appears in code in order"""
lx = XonshLexer()
tks = list(lx.get_tokens(code))
for tk in tokens:
while tks:
if tk == tks[0]:
break
tks = tks[1:]
else:
msg = "Token {!r} missing: {!r}".format(tk,
list(lx.get_tokens(code)))
pytest.fail(msg)
break
@skip_if_on_windows
def test_ls():
2016-09-11 20:17:49 +08:00
check_token('ls -al', [(Name.Builtin, 'ls')])
@skip_if_on_windows
def test_bin_ls():
2016-09-11 20:17:49 +08:00
check_token('/bin/ls -al', [(Name.Builtin, '/bin/ls')])
def test_py_print():
check_token('print("hello")', [(Keyword, 'print'),
(String.Double, 'hello')])
def test_invalid_cmd():
2016-09-11 16:09:10 +08:00
check_token('non-existance-cmd -al', [(Name, 'non')]) # parse as python
check_token('![non-existance-cmd -al]',
[(Error, 'non-existance-cmd')]) # parse as error
check_token('for i in range(10):', [(Keyword, 'for')]) # as py keyword
2016-09-11 16:17:56 +08:00
check_token('(1, )', [(Punctuation, '('),
(Number.Integer, '1')])
def test_multi_cmd():
check_token('cd && cd', [(Name.Builtin, 'cd'),
(Operator, '&&'),
(Name.Builtin, 'cd')])
check_token('cd || non-existance-cmd', [(Name.Builtin, 'cd'),
(Operator, '||'),
(Error, 'non-existance-cmd')
])
def test_nested():
2016-09-11 20:17:49 +08:00
check_token('echo @("hello")', [(Name.Builtin, 'echo'),
(Keyword, '@'),
(Punctuation, '('),
(String.Double, 'hello'),
(Punctuation, ')')])
check_token('print($(cd))', [(Keyword, 'print'),
(Punctuation, '('),
(Keyword, '$'),
(Punctuation, '('),
(Name.Builtin, 'cd'),
(Punctuation, ')'),
(Punctuation, ')')])
check_token('print(![echo "])"])', [(Keyword, 'print'),
(Keyword, '!'),
(Punctuation, '['),
2016-09-11 20:17:49 +08:00
(Name.Builtin, 'echo'),
(String.Double, '"])"'),
(Punctuation, ']')])
def test_path():
HERE = os.path.abspath(os.path.dirname(__file__))
test_dir = os.path.join(HERE, 'xonsh-test-highlight-path')
if not os.path.exists(test_dir):
os.mkdir(test_dir)
check_token('cd {}'.format(test_dir), [(Name.Builtin, 'cd'),
(Name.Constant, test_dir)])
check_token('cd {}-xxx'.format(test_dir), [(Name.Builtin, 'cd'),
(Text,
'{}-xxx'.format(test_dir))
])
2016-09-11 16:09:10 +08:00
def test_backtick():
check_token(r'echo g`.*\w+`', [(String.Affix, 'g'),
(String.Backtick, '`'),
(String.Regex, '.'),
(String.Regex, '*'),
(String.Escape, r'\w'),
])