'test_environ/lexer'

This commit is contained in:
laerus 2016-07-01 12:42:07 +03:00
parent 6476c8491a
commit 320bb3b4da
3 changed files with 59 additions and 61 deletions

View file

@ -2,11 +2,10 @@ tools.py
--------
- maybe make stub Execer a fixture
for (text_execer, test_ast, test_context)
- decouple check_exec, check_eval, check_parse from xonsh_env
- decouple check_exec, check_eval, check_parse from xonsh_builtins
test_tools.py
-------------
- EnvPath bug
- refactor test_partial_string
- refactor test_executable
- maybe split test_commands_cache_lazy
@ -32,14 +31,10 @@ test_history.py
test_contexts.py
test_environ.py
test_execer.py
test_foreign_shells.py
test_imphooks.py
test_lazyjson.py
test_lexer.py
test_main.py
test_parser.py
test_ptk_multiline.py
test_tools.py
test_wizard.py

View file

@ -18,56 +18,56 @@ def test_env_normal():
env = Env(VAR='wakka')
assert 'wakka' == env['VAR']
def test_env_path_list():
env = Env(MYPATH=['/home/wakka'])
assert ['/home/wakka'] == env['MYPATH'].paths
env = Env(MYPATH=['wakka'])
assert ['wakka'] == env['MYPATH'].paths
@pytest.mark.parametrize('path', [['/home/wakka'], ['wakka']])
def test_env_path_list(path):
env = Env(MYPATH=path)
assert path == env['MYPATH'].paths
def test_env_path_str():
env = Env(MYPATH='/home/wakka' + os.pathsep + '/home/jawaka')
assert ['/home/wakka', '/home/jawaka'] == env['MYPATH'].paths
env = Env(MYPATH='wakka' + os.pathsep + 'jawaka')
assert ['wakka', 'jawaka'] == env['MYPATH'].paths
@pytest.mark.parametrize('path', [
['/home/wakka' + os.pathsep + '/home/jawaka'],
['wakka' + os.pathsep + 'jawaka']
])
def test_env_path_str(path):
env = Env(MYPATH=path)
assert path == env['MYPATH'].paths
def test_env_detype():
env = Env(MYPATH=['wakka', 'jawaka'])
assert 'wakka' + os.pathsep + 'jawaka' == env.detype()['MYPATH']
def test_env_detype_mutable_access_clear():
env = Env(MYPATH=['/home/wakka', '/home/jawaka'])
assert '/home/wakka' + os.pathsep + '/home/jawaka' == env.detype()['MYPATH']
env['MYPATH'][0] = '/home/woah'
@pytest.mark.parametrize('path1, path2',[
(['/home/wakka', '/home/jawaka'], '/home/woah'),
(['wakka', 'jawaka'], 'woah')
])
def test_env_detype_mutable_access_clear(path1, path2):
env = Env(MYPATH=path1)
assert path1[0] + os.pathsep + path1[1] == env.detype()['MYPATH']
env['MYPATH'][0] = path2
assert env._detyped is None
assert '/home/woah' + os.pathsep + '/home/jawaka' == env.detype()['MYPATH']
env = Env(MYPATH=['wakka', 'jawaka'])
assert 'wakka' + os.pathsep + 'jawaka' == env.detype()['MYPATH']
env['MYPATH'][0] = 'woah'
assert env._detyped is None
assert 'woah' + os.pathsep + 'jawaka' == env.detype()['MYPATH']
assert path2 + os.pathsep + path1[1] == env.detype()['MYPATH']
def test_env_detype_no_dict():
env = Env(YO={'hey': 42})
det = env.detype()
assert 'YO' not in det
def test_format_prompt():
formatter_dict = {
'a_string': 'cat',
'none': (lambda: None),
'f': (lambda: 'wakka'),
}
cases = {
'my {a_string}': 'my cat',
'my {none}{a_string}': 'my cat',
'{f} jawaka': 'wakka jawaka',
}
for p, exp in cases.items():
obs = format_prompt(template=p, formatter_dict=formatter_dict)
assert exp == obs
for p, exp in cases.items():
obs = partial_format_prompt(template=p, formatter_dict=formatter_dict)
assert exp == obs
#helper
formatter_dict = {
'a_string': 'cat',
'none': (lambda: None),
'f': (lambda: 'wakka'),
}
@pytest.mark.parametrize('inp, exp', [
('my {a_string}', 'my cat'),
('my {none}{a_string}', 'my cat'),
('{f} jawaka', 'wakka jawaka'),
])
def test_format_prompt(inp, exp):
obs = format_prompt(template=inp, formatter_dict=formatter_dict)
assert exp == obs
obs = partial_format_prompt(template=inp, formatter_dict=formatter_dict)
assert exp == obs
def test_format_prompt_with_broken_template():
for p in ('{user', '{user}{hostname'):
@ -97,21 +97,24 @@ def test_format_prompt_with_invalid_func():
assert isinstance(partial_format_prompt(p), str)
assert isinstance(format_prompt(p), str)
def test_HISTCONTROL():
def test_HISTCONTROL_none():
env = Env(HISTCONTROL=None)
assert isinstance(env['HISTCONTROL'], set)
assert len(env['HISTCONTROL']) == 0
def test_HISTCONTROL_empty():
env['HISTCONTROL'] = ''
assert isinstance(env['HISTCONTROL'], set)
assert len(env['HISTCONTROL']) == 0
def test_HISTCONTROL_ignoredups():
env['HISTCONTROL'] = 'ignoredups'
assert isinstance(env['HISTCONTROL'], set)
assert len(env['HISTCONTROL']) == 1
assert ('ignoredups' in env['HISTCONTROL'])
assert ('ignoreerr' not in env['HISTCONTROL'])
def test_HISTCONTROL_ignoreerr_ignoredups():
env['HISTCONTROL'] = 'ignoreerr,ignoredups,ignoreerr'
assert len(env['HISTCONTROL']) == 2
assert ('ignoreerr' in env['HISTCONTROL'])
@ -162,7 +165,7 @@ def test_load_static_config(s, exp, loaded, tmpdir, xonsh_builtins):
@skip_if_on_unix
def test_locate_binary_on_windows():
def test_locate_binary_on_windows(xonsh_builtins):
files = ('file1.exe', 'FILE2.BAT', 'file3.txt')
with TemporaryDirectory() as tmpdir:
for fname in files:
@ -170,9 +173,9 @@ def test_locate_binary_on_windows():
with open(fpath, 'w') as f:
f.write(fpath)
env = Env({'PATH': [tmpdir], 'PATHEXT': ['.COM', '.EXE', '.BAT']})
with mock_xonsh_env(env):
assert ( locate_binary('file1') == os.path.join(tmpdir,'file1.exe'))
assert ( locate_binary('file1.exe') == os.path.join(tmpdir,'file1.exe'))
assert ( locate_binary('file2') == os.path.join(tmpdir,'FILE2.BAT'))
assert ( locate_binary('file2.bat') == os.path.join(tmpdir,'FILE2.BAT'))
assert ( locate_binary('file3') is None)
xonsh_builtins.__xonsh_env__ = env
assert ( locate_binary('file1') == os.path.join(tmpdir,'file1.exe'))
assert ( locate_binary('file1.exe') == os.path.join(tmpdir,'file1.exe'))
assert ( locate_binary('file2') == os.path.join(tmpdir,'FILE2.BAT'))
assert ( locate_binary('file2.bat') == os.path.join(tmpdir,'FILE2.BAT'))
assert ( locate_binary('file3') is None)

View file

@ -7,6 +7,8 @@ from collections import Sequence
sys.path.insert(0, os.path.abspath('..')) # FIXME
from pprint import pformat
import pytest
try:
from ply.lex import LexToken
except ImportError:
@ -40,7 +42,7 @@ def assert_token_equal(x, y):
"""Asserts that two tokens are equal."""
if not tokens_equal(x, y):
msg = 'The tokens differ: {0!r} != {1!r}'.format(x, y)
raise AssertionError(msg)
pytest.fail(msg)
return True
def assert_tokens_equal(x, y):
@ -48,15 +50,14 @@ def assert_tokens_equal(x, y):
if len(x) != len(y):
msg = 'The tokens sequences have different lengths: {0!r} != {1!r}\n'
msg += '# x\n{2}\n\n# y\n{3}'
raise AssertionError(msg.format(len(x), len(y), pformat(x), pformat(y)))
diffs = []
pytest.fail(msg.format(len(x), len(y), pformat(x), pformat(y)))
diffs = [(a, b) for a, b in zip(x, y) if not tokens_equal(a, b)]
if len(diffs) > 0:
msg = ['The token sequences differ: ']
for a, b in diffs:
msg += ['', '- ' + repr(a), '+ ' + repr(b)]
msg = '\n'.join(msg)
raise AssertionError(msg)
pytest.fail(msg)
return True
def check_token(inp, exp):
@ -66,7 +67,7 @@ def check_token(inp, exp):
if len(obs) != 1:
msg = 'The observed sequence does not have length-1: {0!r} != 1\n'
msg += '# obs\n{1}'
raise AssertionError(msg.format(len(obs), pformat(obs)))
pytest.fail(msg.format(len(obs), pformat(obs)))
return assert_token_equal(exp, obs[0])
def check_tokens(inp, exp):
@ -194,11 +195,10 @@ def test_regex_globs():
c = '{}`{}`'.format(p,i)
assert check_token(c, ['SEARCHPATH', c, 0])
def test_float_literals():
cases = ['0.0', '.0', '0.', '1e10', '1.e42', '0.1e42', '0.5e-42',
'5E10', '5e+42']
for s in cases:
assert check_token(s, ['NUMBER', s, 0])
@pytest.mark.parametrize('case', [
'0.0', '.0', '0.', '1e10', '1.e42', '0.1e42', '0.5e-42', '5E10', '5e+42'])
def test_float_literals(case):
assert check_token(case, ['NUMBER', case, 0])
def test_ioredir():
cases = ['2>1', 'err>out', 'o>', 'all>', 'e>o', 'e>', 'out>', '2>&1']