2014-07-19 11:45:44 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
# pylint: skip-file
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
import textwrap
|
|
|
|
import time
|
|
|
|
import unittest
|
|
|
|
|
2015-03-05 05:21:09 -05:00
|
|
|
from test.constant import PYTHON3
|
|
|
|
from test.vim_interface import create_directory, TempFileManager
|
2014-07-19 11:45:44 -04:00
|
|
|
|
2015-01-20 15:26:03 -05:00
|
|
|
|
2014-07-19 11:45:44 -04: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')
|
|
|
|
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
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 = ''
|
2014-07-19 11:45:44 -04:00
|
|
|
sleeptime = 0.00
|
2015-01-20 15:26:03 -05:00
|
|
|
output = ''
|
2014-07-19 11:45:44 -04:00
|
|
|
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
|
2015-04-18 11:15:05 -04:00
|
|
|
maxDiff = None # Show all diff output, always.
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
unittest.TestCase.__init__(self, *args, **kwargs)
|
2015-01-20 15:26:03 -05:00
|
|
|
TempFileManager.__init__(self, 'Case')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
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'))
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
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."""
|
2014-07-19 11:45:44 -04:00
|
|
|
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()
|
2014-07-19 11:45:44 -04:00
|
|
|
if PYTHON3:
|
2015-01-20 15:26:03 -05:00
|
|
|
VimTestCase.version = VimTestCase.version.decode('utf-8')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
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.')
|
2014-07-19 11:45:44 -04:00
|
|
|
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')
|
2014-11-24 04:45:37 -05:00
|
|
|
vim_config.append('set runtimepath=$VIMRUNTIME,%s,%s' % (
|
|
|
|
os.path.dirname(os.path.dirname(__file__)), self._temp_dir))
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
if self.plugins:
|
2015-01-20 15:26:03 -05:00
|
|
|
self._link_file(
|
|
|
|
os.path.join(
|
|
|
|
plugin_cache_dir(),
|
|
|
|
'vim-pathogen',
|
|
|
|
'autoload'),
|
|
|
|
'.')
|
2014-07-19 11:45:44 -04:00
|
|
|
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()')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
# 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')
|
2014-11-15 04:30:52 -05:00
|
|
|
vim_config.append('let @" = ""')
|
2014-07-19 11:45:44 -04:00
|
|
|
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))
|
2014-07-19 11:45:44 -04:00
|
|
|
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'))
|
2014-07-19 11:45:44 -04:00
|
|
|
|
2015-01-20 15:26:03 -05:00
|
|
|
if len(self.snippets) and not isinstance(self.snippets[0], tuple):
|
|
|
|
self.snippets = (self.snippets, )
|
2014-07-19 11:45:44 -04:00
|
|
|
for s in self.snippets:
|
|
|
|
sv, content = s[:2]
|
2015-01-20 15:26:03 -05:00
|
|
|
description = ''
|
|
|
|
options = ''
|
2014-07-19 11:45:44 -04:00
|
|
|
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))
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
# 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)')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
# End of python stuff.
|
2015-01-20 15:26:03 -05:00
|
|
|
vim_config.append('EOF')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
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))
|
2014-07-19 11:45:44 -04:00
|
|
|
return
|
|
|
|
self.vim.leave_with_wait()
|
|
|
|
self.clear_temp()
|
|
|
|
|
|
|
|
# vim:fileencoding=utf-8:foldmarker={{{#,#}}}:
|