UltiSnips/test/vim_test_case.py

192 lines
6.6 KiB
Python
Raw Normal View History

# encoding: utf-8
# pylint: skip-file
import os
import subprocess
import tempfile
import textwrap
import time
import unittest
from test.constant import PYTHON3
from test.vim_interface import create_directory, TempFileManager
2015-01-20 15:26:03 -05:00
def plugin_cache_dir():
"""The directory that we check out our bundles to."""
2015-01-20 15:26:03 -05:00
return os.path.join(tempfile.gettempdir(), 'UltiSnips_test_vim_plugins')
class VimTestCase(unittest.TestCase, TempFileManager):
snippets = ()
files = {}
2015-01-20 15:26:03 -05:00
text_before = ' --- some text before --- \n\n'
text_after = '\n\n --- some text after --- '
expected_error = ''
wanted = ''
keys = ''
sleeptime = 0.00
2015-01-20 15:26:03 -05:00
output = ''
plugins = []
# Skip this test for the given reason or None for not skipping it.
skip_if = lambda self: None
version = None # Will be set to vim --version output
maxDiff = None # Show all diff output, always.
def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
2015-01-20 15:26:03 -05:00
TempFileManager.__init__(self, 'Case')
def runTest(self):
# Only checks the output. All work is done in setUp().
wanted = self.text_before + self.wanted + self.text_after
if self.expected_error:
self.assertRegexpMatches(self.output, self.expected_error)
return
for i in range(self.retries):
if self.output != wanted:
# Redo this, but slower
self.sleeptime += 0.02
self.tearDown()
self.setUp()
self.assertEqual(self.output, wanted)
def _extra_options_pre_init(self, vim_config):
"""Adds extra lines to the vim_config list."""
def _extra_options_post_init(self, vim_config):
"""Adds extra lines to the vim_config list."""
def _before_test(self):
2015-01-20 15:26:03 -05:00
"""Send these keys before the test runs.
Used for buffer local variables and other options.
"""
def _create_file(self, file_path, content):
"""Creates a file in the runtimepath that is created for this test.
2015-01-20 15:26:03 -05:00
Returns the absolute path to the file.
"""
return self.write_temp(file_path, textwrap.dedent(content + '\n'))
def _link_file(self, source, relative_destination):
2015-01-20 15:26:03 -05:00
"""Creates a link from 'source' to the 'relative_destination' in our
temp dir."""
absdir = self.name_temp(relative_destination)
create_directory(absdir)
os.symlink(source, os.path.join(absdir, os.path.basename(source)))
def setUp(self):
if not VimTestCase.version:
2015-01-20 15:26:03 -05:00
VimTestCase.version, _ = subprocess.Popen(['vim', '--version'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
if PYTHON3:
2015-01-20 15:26:03 -05:00
VimTestCase.version = VimTestCase.version.decode('utf-8')
if self.plugins and not self.test_plugins:
2015-01-20 15:26:03 -05:00
return self.skipTest('Not testing integration with other plugins.')
reason_for_skipping = self.skip_if()
if reason_for_skipping is not None:
return self.skipTest(reason_for_skipping)
vim_config = []
vim_config.append('set nocompatible')
vim_config.append('set runtimepath=$VIMRUNTIME,%s,%s' % (
os.path.dirname(os.path.dirname(__file__)), self._temp_dir))
if self.plugins:
2015-01-20 15:26:03 -05:00
self._link_file(
os.path.join(
plugin_cache_dir(),
'vim-pathogen',
'autoload'),
'.')
for plugin in self.plugins:
2015-01-20 15:26:03 -05:00
self._link_file(
os.path.join(
plugin_cache_dir(),
os.path.basename(plugin)),
'bundle')
vim_config.append('execute pathogen#infect()')
# Vim parameters.
vim_config.append('syntax on')
vim_config.append('filetype plugin indent on')
vim_config.append('set clipboard=""')
vim_config.append('set encoding=utf-8')
vim_config.append('set fileencoding=utf-8')
vim_config.append('set buftype=nofile')
vim_config.append('set shortmess=at')
vim_config.append('let @" = ""')
vim_config.append('let g:UltiSnipsExpandTrigger="<tab>"')
vim_config.append('let g:UltiSnipsJumpForwardTrigger="?"')
vim_config.append('let g:UltiSnipsJumpBackwardTrigger="+"')
vim_config.append('let g:UltiSnipsListSnippets="@"')
2015-01-20 15:26:03 -05:00
vim_config.append(
'let g:UltiSnipsUsePythonVersion="%i"' %
(3 if PYTHON3 else 2))
vim_config.append('let g:UltiSnipsSnippetDirectories=["us"]')
self._extra_options_pre_init(vim_config)
# Now activate UltiSnips.
vim_config.append('call UltiSnips#bootstrap#Bootstrap()')
self._extra_options_post_init(vim_config)
# Finally, add the snippets and some configuration for the test.
2015-01-20 15:26:03 -05:00
vim_config.append('%s << EOF' % ('py3' if PYTHON3 else 'py'))
2015-01-20 15:26:03 -05:00
if len(self.snippets) and not isinstance(self.snippets[0], tuple):
self.snippets = (self.snippets, )
for s in self.snippets:
sv, content = s[:2]
2015-01-20 15:26:03 -05:00
description = ''
options = ''
priority = 0
if len(s) > 2:
description = s[2]
if len(s) > 3:
options = s[3]
if len(s) > 4:
priority = s[4]
2015-01-20 15:26:03 -05:00
vim_config.append('UltiSnips_Manager.add_snippet(%r, %r, %r, %r, priority=%i)' % (
sv, content, description, options, priority))
# fill buffer with default text and place cursor in between.
prefilled_text = (self.text_before + self.text_after).splitlines()
2015-01-20 15:26:03 -05:00
vim_config.append('vim.current.buffer[:] = %r\n' % prefilled_text)
vim_config.append(
'vim.current.window.cursor = (max(len(vim.current.buffer)//2, 1), 0)')
# End of python stuff.
2015-01-20 15:26:03 -05:00
vim_config.append('EOF')
for name, content in self.files.items():
self._create_file(name, content)
self.vim.launch(vim_config)
self._before_test()
if not self.interrupt:
# Go into insert mode and type the keys but leave Vim some time to
# react.
for c in 'i' + self.keys:
self.vim.send(c)
time.sleep(self.sleeptime)
self.output = self.vim.get_buffer_data()
def tearDown(self):
if self.interrupt:
2015-01-20 15:26:03 -05:00
print('Working directory: %s' % (self._temp_dir))
return
self.vim.leave_with_wait()
self.clear_temp()
# vim:fileencoding=utf-8:foldmarker={{{#,#}}}: