From 6f6c1fb90c51107835523048dfaeaac21f913c4a Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 25 Jun 2014 19:55:20 +0400 Subject: [PATCH] Add tests --- powerline/vim.py | 4 ++-- tests/lib/config_mock.py | 15 ++++++++---- tests/test_configuration.py | 47 ++++++++++++++++++++++++++++++++++++- tests/vim.py | 7 ++++++ 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/powerline/vim.py b/powerline/vim.py index 517bae82..a26f5e40 100644 --- a/powerline/vim.py +++ b/powerline/vim.py @@ -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})\')' diff --git a/tests/lib/config_mock.py b/tests/lib/config_mock.py index 1a27d7cd..2ad78853 100644 --- a/tests/lib/config_mock.py +++ b/tests/lib/config_mock.py @@ -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 ) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index ed6996ad..d6e64f40 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -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__': diff --git a/tests/vim.py b/tests/vim.py index f447f490..ef37db71 100644 --- a/tests/vim.py +++ b/tests/vim.py @@ -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()