Add tests

This commit is contained in:
ZyX 2014-06-25 19:55:20 +04:00
parent 46d58d39fa
commit 6f6c1fb90c
4 changed files with 66 additions and 7 deletions

View File

@ -23,8 +23,8 @@ def _override_from(config, override_varname):
class VimPowerline(Powerline):
def __init__(self, pyeval='PowerlinePyeval'):
super(VimPowerline, self).__init__('vim')
def __init__(self, pyeval='PowerlinePyeval', **kwargs):
super(VimPowerline, self).__init__('vim', **kwargs)
self.last_window_id = 1
self.window_statusline = '%!' + pyeval + '(\'powerline.statusline({0})\')'

View File

@ -121,16 +121,23 @@ renderer = SimpleRenderer
def get_powerline(**kwargs):
return get_powerline_raw(
TestPowerline,
ext='test',
renderer_module='tests.lib.config_mock',
logger=Logger(),
**kwargs
)
def get_powerline_raw(PowerlineClass, **kwargs):
global renderer
watcher = Watcher()
if kwargs.pop('simpler_renderer', False):
renderer = EvenSimplerRenderer
else:
renderer = SimpleRenderer
pl = TestPowerline(
ext='test',
renderer_module='tests.lib.config_mock',
logger=Logger(),
pl = PowerlineClass(
config_loader=ConfigLoader(load=load_json_config, watcher=watcher, run_once=kwargs.get('run_once')),
**kwargs
)

View File

@ -1,11 +1,15 @@
# vim:fileencoding=utf-8:noet
from __future__ import unicode_literals, absolute_import, division
import tests.vim as vim_module
import powerline as powerline_module
from tests import TestCase
from tests.lib import replace_item
from tests.lib.config_mock import swap_attributes, get_powerline, pop_events
from tests.lib.config_mock import get_powerline_raw
from functools import wraps
from copy import deepcopy
import sys
import os
config = {
@ -29,6 +33,10 @@ config = {
'theme': 'default',
'colorscheme': 'default',
},
'vim': {
'theme': 'default',
'colorscheme': 'default',
},
},
},
'colors': {
@ -47,6 +55,11 @@ config = {
'str2': {'fg': 'col3', 'bg': 'col4', 'attr': ['underline']},
},
},
'colorschemes/vim/default': {
'groups': {
'environment': {'fg': 'col3', 'bg': 'col4', 'attr': ['underline']},
},
},
'themes/test/default': {
'segments': {
'left': [
@ -73,6 +86,19 @@ config = {
],
},
},
'themes/vim/default': {
'default_module': 'powerline.segments.common',
'segments': {
'left': [
{
'name': 'environment',
'args': {
'variable': 'TEST',
},
},
],
},
},
}
@ -122,15 +148,34 @@ class TestSingleLine(TestCase):
], width=10)
class TestVim(TestCase):
def test_environ_update(self):
# Regression test: test that segment obtains environment from vim, not
# from os.environ.
from powerline.vim import VimPowerline
with vim_module._with('environ', TEST='abc'):
with get_powerline_raw(VimPowerline) as powerline:
window = vim_module.current.window
window_id = 1
winnr = window.number
self.assertEqual(powerline.render(window, window_id, winnr), '%#Pl_3_8404992_4_192_underline#\xa0abc%#Pl_4_192_NONE_None_NONE#>>')
vim_module._environ['TEST'] = 'def'
self.assertEqual(powerline.render(window, window_id, winnr), '%#Pl_3_8404992_4_192_underline#\xa0def%#Pl_4_192_NONE_None_NONE#>>')
replaces = {}
def setUpModule():
global replaces
replaces = swap_attributes(globals(), powerline_module, replaces)
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'path')))
tearDownModule = setUpModule
def tearDownModule():
global replaces
replaces = swap_attributes(globals(), powerline_module, replaces)
sys.path.pop(0)
if __name__ == '__main__':

View File

@ -11,6 +11,9 @@ options = {
}
_last_bufnr = 0
_highlights = {}
from collections import defaultdict as _defaultdict
_environ = _defaultdict(lambda: '')
del _defaultdict
_thread_id = None
@ -169,6 +172,8 @@ def eval(expr):
return vars[expr[2:]]
elif expr.startswith('&'):
return options[expr[1:]]
elif expr.startswith('$'):
return _environ[expr[1:]]
elif expr.startswith('PowerlineRegisterCachePurgerEvent'):
_buf_purge_events.add(expr[expr.find('"') + 1:expr.rfind('"') - 1])
return '0'
@ -708,6 +713,8 @@ def _with(key, *args, **kwargs):
return _WithDict(options, **kwargs)
elif key == 'globals':
return _WithDict(vars, **kwargs)
elif key == 'environ':
return _WithDict(_environ, **kwargs)
elif key == 'split':
return _WithSplit()