Add support for with: statement to tests.vim
Also made everything use new vim_module._with where possible
This commit is contained in:
parent
c429d9eeb2
commit
0a05b2961a
|
@ -8,8 +8,7 @@ import tests.vim as vim_module
|
|||
import sys
|
||||
import os
|
||||
import json
|
||||
from .lib import Args, urllib_read
|
||||
import tests.lib as lib
|
||||
from .lib import Args, urllib_read, replace_module_attr
|
||||
|
||||
|
||||
VBLOCK = chr(ord('V') - 0x40)
|
||||
|
@ -20,9 +19,7 @@ class TestConfig(TestCase):
|
|||
def test_vim(self):
|
||||
from powerline.vim import VimPowerline
|
||||
cfg_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'powerline', 'config_files')
|
||||
vim_module._g['powerline_config_path'] = cfg_path
|
||||
buffers = ((lambda: vim_module._set_bufoption('buftype', 'help'), lambda: vim_module._set_bufoption('buftype', '')),
|
||||
(lambda: vim_module._edit('[Command Line]'), lambda: vim_module._bw()))
|
||||
buffers = ((('bufoptions',), {'buftype': 'help'}), (('buffer', '[Command Line]'), {}))
|
||||
with open(os.path.join(cfg_path, 'config.json'), 'r') as f:
|
||||
self.assertEqual(len(buffers), len(json.load(f)['ext']['vim']['local_themes']))
|
||||
outputs = {}
|
||||
|
@ -36,38 +33,30 @@ class TestConfig(TestCase):
|
|||
self.fail('Duplicate in set #{0} for mode {1!r} (previously defined in set #{2} for mode {3!r})'.format(i, mode, *outputs[out]))
|
||||
outputs[out] = (i, mode)
|
||||
|
||||
try:
|
||||
exclude = set(('no', 'v', 'V', VBLOCK, 's', 'S', SBLOCK, 'R', 'Rv', 'c', 'cv', 'ce', 'r', 'rm', 'r?', '!'))
|
||||
try:
|
||||
for mode in ['n', 'nc', 'no', 'v', 'V', VBLOCK, 's', 'S', SBLOCK, 'i', 'R', 'Rv', 'c', 'cv', 'ce', 'r', 'rm', 'r?', '!']:
|
||||
if mode != 'nc':
|
||||
vim_module._start_mode(mode)
|
||||
check_output(1, 0)
|
||||
for setup, teardown in buffers:
|
||||
i += 1
|
||||
if mode in exclude:
|
||||
continue
|
||||
setup()
|
||||
try:
|
||||
check_output(1, 0)
|
||||
finally:
|
||||
teardown()
|
||||
finally:
|
||||
vim_module._start_mode('n')
|
||||
finally:
|
||||
vim_module._g.pop('powerline_config_path')
|
||||
with vim_module._with('buffer', 'foo.txt'):
|
||||
with vim_module._with('globals', powerline_config_path=cfg_path):
|
||||
exclude = set(('no', 'v', 'V', VBLOCK, 's', 'S', SBLOCK, 'R', 'Rv', 'c', 'cv', 'ce', 'r', 'rm', 'r?', '!'))
|
||||
try:
|
||||
for mode in ['n', 'nc', 'no', 'v', 'V', VBLOCK, 's', 'S', SBLOCK, 'i', 'R', 'Rv', 'c', 'cv', 'ce', 'r', 'rm', 'r?', '!']:
|
||||
if mode != 'nc':
|
||||
vim_module._start_mode(mode)
|
||||
check_output(1, 0)
|
||||
for args, kwargs in buffers:
|
||||
i += 1
|
||||
if mode in exclude:
|
||||
continue
|
||||
with vim_module._with(*args, **kwargs):
|
||||
check_output(1, 0)
|
||||
finally:
|
||||
vim_module._start_mode('n')
|
||||
|
||||
def test_tmux(self):
|
||||
import powerline.segments.common
|
||||
from powerline.segments import common
|
||||
from imp import reload
|
||||
reload(powerline.segments.common)
|
||||
old_urllib_read = powerline.segments.common.urllib_read
|
||||
powerline.segments.common.urllib_read = urllib_read
|
||||
reload(common)
|
||||
from powerline.shell import ShellPowerline
|
||||
try:
|
||||
with replace_module_attr(common, 'urllib_read', urllib_read):
|
||||
ShellPowerline(Args(ext=['tmux'])).renderer.render()
|
||||
finally:
|
||||
powerline.segments.common.urllib_read = old_urllib_read
|
||||
|
||||
|
||||
old_cwd = None
|
||||
|
|
|
@ -172,16 +172,11 @@ class TestVim(TestCase):
|
|||
self.assertEqual(vim.mode(segment_info=segment_info), 'NORMAL')
|
||||
self.assertEqual(vim.mode(segment_info=segment_info, override={'i': 'INS'}), 'NORMAL')
|
||||
self.assertEqual(vim.mode(segment_info=segment_info, override={'n': 'NORM'}), 'NORM')
|
||||
try:
|
||||
vim_module._start_mode('i')
|
||||
segment_info = vim_module._get_segment_info()
|
||||
with vim_module._with('mode', 'i') as segment_info:
|
||||
self.assertEqual(vim.mode(segment_info=segment_info), 'INSERT')
|
||||
vim_module._start_mode(chr(ord('V') - 0x40))
|
||||
segment_info = vim_module._get_segment_info()
|
||||
with vim_module._with('mode', chr(ord('V') - 0x40)) as segment_info:
|
||||
self.assertEqual(vim.mode(segment_info=segment_info), 'V·BLCK')
|
||||
self.assertEqual(vim.mode(segment_info=segment_info, override={'^V': 'VBLK'}), 'VBLK')
|
||||
finally:
|
||||
vim_module._start_mode('n')
|
||||
|
||||
def test_modified_indicator(self):
|
||||
segment_info = vim_module._get_segment_info()
|
||||
|
@ -191,40 +186,30 @@ class TestVim(TestCase):
|
|||
self.assertEqual(vim.modified_indicator(segment_info=segment_info), '+')
|
||||
self.assertEqual(vim.modified_indicator(segment_info=segment_info, text='-'), '-')
|
||||
finally:
|
||||
vim_module._undo()
|
||||
vim_module._bw(segment_info['bufnr'])
|
||||
|
||||
def test_paste_indicator(self):
|
||||
segment_info = vim_module._get_segment_info()
|
||||
self.assertEqual(vim.paste_indicator(segment_info=segment_info), None)
|
||||
vim_module._options['paste'] = 1
|
||||
try:
|
||||
with vim_module._with('options', paste=1):
|
||||
self.assertEqual(vim.paste_indicator(segment_info=segment_info), 'PASTE')
|
||||
self.assertEqual(vim.paste_indicator(segment_info=segment_info, text='P'), 'P')
|
||||
finally:
|
||||
vim_module._options['paste'] = 0
|
||||
|
||||
def test_readonly_indicator(self):
|
||||
segment_info = vim_module._get_segment_info()
|
||||
self.assertEqual(vim.readonly_indicator(segment_info=segment_info), None)
|
||||
vim_module._buf_options[vim_module._buffer()]['readonly'] = 1
|
||||
try:
|
||||
with vim_module._with('bufoptions', readonly=1):
|
||||
self.assertEqual(vim.readonly_indicator(segment_info=segment_info), '')
|
||||
self.assertEqual(vim.readonly_indicator(segment_info=segment_info, text='L'), 'L')
|
||||
finally:
|
||||
vim_module._buf_options[vim_module._buffer()]['readonly'] = 0
|
||||
|
||||
def test_file_directory(self):
|
||||
segment_info = vim_module._get_segment_info()
|
||||
self.assertEqual(vim.file_directory(segment_info=segment_info), None)
|
||||
with replace_env('HOME', '/home/foo'):
|
||||
vim_module._edit('/tmp/abc')
|
||||
segment_info = vim_module._get_segment_info()
|
||||
try:
|
||||
with vim_module._with('buffer', '/tmp/abc') as segment_info:
|
||||
self.assertEqual(vim.file_directory(segment_info=segment_info), '/tmp/')
|
||||
os.environ['HOME'] = '/tmp'
|
||||
self.assertEqual(vim.file_directory(segment_info=segment_info), '~/')
|
||||
finally:
|
||||
vim_module._bw(segment_info['bufnr'])
|
||||
|
||||
def test_file_name(self):
|
||||
segment_info = vim_module._get_segment_info()
|
||||
|
@ -233,28 +218,16 @@ class TestVim(TestCase):
|
|||
[{'contents': '[No file]', 'highlight_group': ['file_name_no_file', 'file_name']}])
|
||||
self.assertEqual(vim.file_name(segment_info=segment_info, display_no_file=True, no_file_text='X'),
|
||||
[{'contents': 'X', 'highlight_group': ['file_name_no_file', 'file_name']}])
|
||||
vim_module._edit('/tmp/abc')
|
||||
segment_info = vim_module._get_segment_info()
|
||||
try:
|
||||
with vim_module._with('buffer', '/tmp/abc') as segment_info:
|
||||
self.assertEqual(vim.file_name(segment_info=segment_info), 'abc')
|
||||
finally:
|
||||
vim_module._bw(segment_info['bufnr'])
|
||||
vim_module._edit('/tmp/’’')
|
||||
segment_info = vim_module._get_segment_info()
|
||||
try:
|
||||
with vim_module._with('buffer', '/tmp/’’') as segment_info:
|
||||
self.assertEqual(vim.file_name(segment_info=segment_info), '’’')
|
||||
finally:
|
||||
vim_module._bw(segment_info['bufnr'])
|
||||
|
||||
def test_file_size(self):
|
||||
segment_info = vim_module._get_segment_info()
|
||||
self.assertEqual(vim.file_size(segment_info=segment_info), None)
|
||||
vim_module._edit(os.path.join(os.path.dirname(__file__), 'empty'))
|
||||
segment_info = vim_module._get_segment_info()
|
||||
try:
|
||||
with vim_module._with('buffer', os.path.join(os.path.dirname(__file__), 'empty')) as segment_info:
|
||||
self.assertEqual(vim.file_size(segment_info=segment_info), '0 B')
|
||||
finally:
|
||||
vim_module._bw(segment_info['bufnr'])
|
||||
|
||||
def test_file_opts(self):
|
||||
segment_info = vim_module._get_segment_info()
|
||||
|
@ -263,12 +236,9 @@ class TestVim(TestCase):
|
|||
self.assertEqual(vim.file_encoding(segment_info=segment_info),
|
||||
[{'divider_highlight_group': 'background:divider', 'contents': 'utf-8'}])
|
||||
self.assertEqual(vim.file_type(segment_info=segment_info), None)
|
||||
vim_module._set_filetype('python')
|
||||
try:
|
||||
with vim_module._with('bufoptions', filetype='python'):
|
||||
self.assertEqual(vim.file_type(segment_info=segment_info),
|
||||
[{'divider_highlight_group': 'background:divider', 'contents': 'python'}])
|
||||
finally:
|
||||
vim_module._set_filetype('')
|
||||
|
||||
def test_line_percent(self):
|
||||
segment_info = vim_module._get_segment_info()
|
||||
|
|
88
tests/vim.py
88
tests/vim.py
|
@ -346,11 +346,6 @@ def _b(bufnr):
|
|||
windows[_window - 1].buffer = buffers[bufnr]
|
||||
|
||||
|
||||
def _set_filetype(ft):
|
||||
_buf_options[_buffer()]['filetype'] = ft
|
||||
_launch_event('FileType')
|
||||
|
||||
|
||||
def _set_cursor(line, col):
|
||||
windows[_window - 1].cursor = (line, col)
|
||||
if _mode == 'n':
|
||||
|
@ -365,3 +360,86 @@ def _get_buffer():
|
|||
|
||||
def _set_bufoption(option, value, bufnr=None):
|
||||
_buf_options[bufnr or _buffer()][option] = value
|
||||
if option == 'filetype':
|
||||
_launch_event('FileType')
|
||||
|
||||
|
||||
class _WithNewBuffer(object):
|
||||
def __init__(self, func, *args, **kwargs):
|
||||
self.call = lambda: func(*args, **kwargs)
|
||||
|
||||
def __enter__(self):
|
||||
self.call()
|
||||
self.bufnr = _buffer()
|
||||
return _get_segment_info()
|
||||
|
||||
def __exit__(self, *args):
|
||||
_bw(self.bufnr)
|
||||
|
||||
|
||||
def _set_dict(d, new, setfunc=None):
|
||||
if not setfunc:
|
||||
def setfunc(k, v):
|
||||
d[k] = v
|
||||
|
||||
old = {}
|
||||
na = []
|
||||
for k, v in new.items():
|
||||
try:
|
||||
old[k] = d[k]
|
||||
except KeyError:
|
||||
na.append(k)
|
||||
setfunc(k, v)
|
||||
return old, na
|
||||
|
||||
|
||||
class _WithBufOption(object):
|
||||
def __init__(self, **new):
|
||||
self.new = new
|
||||
|
||||
def __enter__(self):
|
||||
self.bufnr = _buffer()
|
||||
self.old = _set_dict(_buf_options[self.bufnr], self.new, _set_bufoption)[0]
|
||||
|
||||
def __exit__(self, *args):
|
||||
_buf_options[self.bufnr].update(self.old)
|
||||
|
||||
|
||||
class _WithMode(object):
|
||||
def __init__(self, new):
|
||||
self.new = new
|
||||
|
||||
def __enter__(self):
|
||||
self.old = _mode
|
||||
_start_mode(self.new)
|
||||
return _get_segment_info()
|
||||
|
||||
def __exit__(self, *args):
|
||||
_start_mode(self.old)
|
||||
|
||||
|
||||
class _WithDict(object):
|
||||
def __init__(self, d, **new):
|
||||
self.new = new
|
||||
self.d = d
|
||||
|
||||
def __enter__(self):
|
||||
self.old, self.na = _set_dict(self.d, self.new)
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.d.update(self.old)
|
||||
for k in self.na:
|
||||
self.d.pop(k)
|
||||
|
||||
|
||||
def _with(key, *args, **kwargs):
|
||||
if key == 'buffer':
|
||||
return _WithNewBuffer(_edit, *args, **kwargs)
|
||||
elif key == 'mode':
|
||||
return _WithMode(*args, **kwargs)
|
||||
elif key == 'bufoptions':
|
||||
return _WithBufOption(**kwargs)
|
||||
elif key == 'options':
|
||||
return _WithDict(_options, **kwargs)
|
||||
elif key == 'globals':
|
||||
return _WithDict(_g, **kwargs)
|
||||
|
|
Loading…
Reference in New Issue