Tests now work through simulating input via screen. Good solution
This commit is contained in:
parent
1926c49f96
commit
1e3dd7b4be
38
PySnipEmu.py
38
PySnipEmu.py
@ -33,7 +33,7 @@ class SnippetInstance(object):
|
|||||||
self._start = start
|
self._start = start
|
||||||
self._end = end
|
self._end = end
|
||||||
self._ts = ts
|
self._ts = ts
|
||||||
|
|
||||||
self._cts = 1
|
self._cts = 1
|
||||||
|
|
||||||
def select_next_tab(self):
|
def select_next_tab(self):
|
||||||
@ -42,7 +42,7 @@ class SnippetInstance(object):
|
|||||||
self._cts = 0
|
self._cts = 0
|
||||||
else:
|
else:
|
||||||
self._cts = 1
|
self._cts = 1
|
||||||
|
|
||||||
ts = self._ts[self._cts]
|
ts = self._ts[self._cts]
|
||||||
lineno, col = self._start
|
lineno, col = self._start
|
||||||
|
|
||||||
@ -53,23 +53,24 @@ class SnippetInstance(object):
|
|||||||
else:
|
else:
|
||||||
newcol = ts.span[0]
|
newcol = ts.span[0]
|
||||||
endcol = ts.span[1]
|
endcol = ts.span[1]
|
||||||
|
|
||||||
self._cts += 1
|
self._cts += 1
|
||||||
|
|
||||||
vim.current.window.cursor = newline, newcol
|
vim.current.window.cursor = newline, newcol
|
||||||
|
|
||||||
# Select the word
|
# Select the word
|
||||||
|
|
||||||
# Depending on the current mode and position, we
|
# Depending on the current mode and position, we
|
||||||
# might need to move escape out of the mode and this
|
# might need to move escape out of the mode and this
|
||||||
# will move our cursor one left
|
# will move our cursor one left
|
||||||
if newcol != 0 and vim.eval("mode()") == 'i':
|
if endcol-newcol > 0:
|
||||||
move_one_right = "l"
|
if newcol != 0 and vim.eval("mode()") == 'i':
|
||||||
else:
|
move_one_right = "l"
|
||||||
move_one_right = ""
|
else:
|
||||||
|
move_one_right = ""
|
||||||
|
|
||||||
vim.command(r'call feedkeys("\<Esc>%sv%il\<c-g>")'
|
vim.command(r'call feedkeys("\<Esc>%sv%il\<c-g>")'
|
||||||
% (move_one_right, endcol-newcol-1))
|
% (move_one_right, endcol-newcol-1))
|
||||||
|
|
||||||
class Snippet(object):
|
class Snippet(object):
|
||||||
_TB_EXPR = re.compile(r'\$(?:(?:{(\d+):(.*?)})|(\d+))')
|
_TB_EXPR = re.compile(r'\$(?:(?:{(\d+):(.*?)})|(\d+))')
|
||||||
@ -133,19 +134,19 @@ class Snippet(object):
|
|||||||
newcol = col + len(lines[-1])
|
newcol = col + len(lines[-1])
|
||||||
else:
|
else:
|
||||||
newcol = len(lines[-1])
|
newcol = len(lines[-1])
|
||||||
|
|
||||||
lines[0] = before + lines[0]
|
lines[0] = before + lines[0]
|
||||||
lines[-1] += after
|
lines[-1] += after
|
||||||
|
|
||||||
vim.current.buffer[lineno-1:lineno-1+len(lines)] = lines
|
vim.current.buffer[lineno-1:lineno-1+len(lines)] = lines
|
||||||
|
|
||||||
vim.current.window.cursor = newline, newcol
|
vim.current.window.cursor = newline, newcol
|
||||||
|
|
||||||
if len(ts):
|
if len(ts):
|
||||||
s = SnippetInstance( (lineno,col), (newline,newcol), ts)
|
s = SnippetInstance( (lineno,col), (newline,newcol), ts)
|
||||||
|
|
||||||
s.select_next_tab()
|
s.select_next_tab()
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
@ -154,15 +155,12 @@ class SnippetManager(object):
|
|||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self.clear_snippets()
|
self._snippets = {}
|
||||||
self._current_snippets = []
|
self._current_snippets = []
|
||||||
|
|
||||||
def add_snippet(self,trigger,value):
|
def add_snippet(self,trigger,value):
|
||||||
self._snippets[trigger] = Snippet(trigger,value)
|
self._snippets[trigger] = Snippet(trigger,value)
|
||||||
|
|
||||||
def clear_snippets(self):
|
|
||||||
self._snippets = {}
|
|
||||||
|
|
||||||
def try_expand(self):
|
def try_expand(self):
|
||||||
if len(self._current_snippets):
|
if len(self._current_snippets):
|
||||||
self._current_snippets[-1].select_next_tab()
|
self._current_snippets[-1].select_next_tab()
|
||||||
@ -183,7 +181,7 @@ class SnippetManager(object):
|
|||||||
s = self._snippets[word].launch(before.rstrip()[:-len(word)], after)
|
s = self._snippets[word].launch(before.rstrip()[:-len(word)], after)
|
||||||
if s is not None:
|
if s is not None:
|
||||||
self._current_snippets.append(s)
|
self._current_snippets.append(s)
|
||||||
|
|
||||||
def cursor_moved(self):
|
def cursor_moved(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
138
test.py
Normal file → Executable file
138
test.py
Normal file → Executable file
@ -2,38 +2,60 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
#
|
#
|
||||||
|
|
||||||
import vim
|
import os
|
||||||
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
import time
|
||||||
from PySnipEmu import PySnipSnippets
|
|
||||||
|
|
||||||
|
|
||||||
class _VimTest(unittest.TestCase):
|
class _VimTest(unittest.TestCase):
|
||||||
|
def type(self, str):
|
||||||
|
"""
|
||||||
|
Send the keystrokes to vim via screen. Pause after each tab, so
|
||||||
|
expansion can take place
|
||||||
|
"""
|
||||||
|
def _send(s):
|
||||||
|
os.system("screen -X stuff '%s'" % s)
|
||||||
|
|
||||||
|
splits = str.split('\t')
|
||||||
|
for w in splits[:-1]:
|
||||||
|
_send(w + '\t')
|
||||||
|
_send(splits[-1])
|
||||||
|
|
||||||
|
|
||||||
|
def escape(self):
|
||||||
|
self.type("\x1b")
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
PySnipSnippets.reset()
|
self.escape()
|
||||||
|
|
||||||
|
self.type(":py PySnipSnippets.reset()\n")
|
||||||
|
|
||||||
for sv,content in self.snippets:
|
for sv,content in self.snippets:
|
||||||
PySnipSnippets.add_snippet(sv,content)
|
self.type(''':py << EOF
|
||||||
|
PySnipSnippets.add_snippet("%s","""%s""")
|
||||||
|
EOF
|
||||||
|
''' % (sv,content))
|
||||||
|
|
||||||
vim.command(":new")
|
# Clear the buffer
|
||||||
try:
|
self.type("bggVGd")
|
||||||
self.cmd()
|
|
||||||
self.output = '\n'.join(vim.current.buffer[:])
|
|
||||||
finally:
|
|
||||||
vim.command(":q!")
|
|
||||||
|
|
||||||
def insert(self,string):
|
# Enter insert mode
|
||||||
"""A helper function to type some text"""
|
self.type("i")
|
||||||
vim.command('normal i%s' % string)
|
|
||||||
def change(self,string):
|
|
||||||
"""A helper function to type some text"""
|
|
||||||
vim.command('normal c%s' % string)
|
|
||||||
|
|
||||||
def expand(self):
|
# Execute the command
|
||||||
vim.command("call PyVimSnips_ExpandSnippet()")
|
self.cmd()
|
||||||
|
|
||||||
def tearDown(self):
|
handle, fn = tempfile.mkstemp(prefix="PySnipEmuTest",suffix=".txt")
|
||||||
PySnipSnippets.clear_snippets()
|
os.close(handle)
|
||||||
|
|
||||||
|
self.escape()
|
||||||
|
self.type(":w! %s\n" % fn)
|
||||||
|
|
||||||
|
# Give screen a chance to send the cmd and vim to write the file
|
||||||
|
time.sleep(.01)
|
||||||
|
|
||||||
|
# Read the output, chop the trailing newline
|
||||||
|
self.output = open(fn,"r").read()[:-1]
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
"""Overwrite these in the children"""
|
"""Overwrite these in the children"""
|
||||||
@ -50,43 +72,37 @@ class _SimpleExpands(_VimTest):
|
|||||||
|
|
||||||
class SimpleExpand_ExceptCorrectResult(_SimpleExpands):
|
class SimpleExpand_ExceptCorrectResult(_SimpleExpands):
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("hallo ")
|
self.type("hallo\t")
|
||||||
self.expand()
|
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"Hallo Welt! ")
|
self.assertEqual(self.output,"Hallo Welt!")
|
||||||
|
|
||||||
class SimpleExpandTypeAfterExpand_ExceptCorrectResult(_SimpleExpands):
|
class SimpleExpandTypeAfterExpand_ExceptCorrectResult(_SimpleExpands):
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("hallo ")
|
self.type("hallo\tand again")
|
||||||
self.expand()
|
|
||||||
self.insert("and again")
|
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"Hallo Welt!and again ")
|
self.assertEqual(self.output,"Hallo Welt!and again")
|
||||||
|
|
||||||
class SimpleExpandTypeAfterExpand1_ExceptCorrectResult(_SimpleExpands):
|
class SimpleExpandTypeAfterExpand1_ExceptCorrectResult(_SimpleExpands):
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("na du hallo ")
|
self.type("na du hallo\tand again")
|
||||||
self.expand()
|
|
||||||
self.insert("and again")
|
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"na du Hallo Welt!and again ")
|
self.assertEqual(self.output,"na du Hallo Welt!and again")
|
||||||
|
|
||||||
class DoNotExpandAfterSpace_ExceptCorrectResult(_SimpleExpands):
|
class DoNotExpandAfterSpace_ExceptCorrectResult(_SimpleExpands):
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("hallo ")
|
self.type("hallo \t")
|
||||||
self.expand()
|
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"hallo ")
|
self.assertEqual(self.output,"hallo ")
|
||||||
|
|
||||||
class ExpandInTheMiddleOfLine_ExceptCorrectResult(_SimpleExpands):
|
class ExpandInTheMiddleOfLine_ExceptCorrectResult(_SimpleExpands):
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("Wie hallo gehts?")
|
self.type("Wie hallo gehts?")
|
||||||
vim.command("normal 02f ")
|
self.escape()
|
||||||
self.expand()
|
self.type("bhi\t")
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"Wie Hallo Welt! gehts?")
|
self.assertEqual(self.output,"Wie Hallo Welt! gehts?")
|
||||||
@ -97,22 +113,22 @@ class MultilineExpand_ExceptCorrectResult(_VimTest):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("Wie hallo gehts?")
|
self.type("Wie hallo gehts?")
|
||||||
vim.command("normal 02f ")
|
self.escape()
|
||||||
self.expand()
|
self.type("bhi\t")
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output, "Wie Hallo Welt!\nUnd Wie gehts? gehts?")
|
self.assertEqual(self.output, "Wie Hallo Welt!\nUnd Wie gehts? gehts?")
|
||||||
|
|
||||||
class MultilineExpandTestTyping_ExceptCorrectResult(_VimTest):
|
class MultilineExpandTestTyping_ExceptCorrectResult(_VimTest):
|
||||||
snippets = (
|
snippets = (
|
||||||
("hallo", "Hallo Welt!\nUnd Wie gehts?"),
|
("hallo", "Hallo Welt!\nUnd Wie gehts?"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("Wie hallo gehts?")
|
self.type("Wie hallo gehts?")
|
||||||
vim.command("normal 02f ")
|
self.escape()
|
||||||
self.expand()
|
self.type("bhi\tHuiui!")
|
||||||
self.insert("Huiui!")
|
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,
|
self.assertEqual(self.output,
|
||||||
@ -127,12 +143,10 @@ class ExitTabStop_ExceptCorrectResult(_VimTest):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("echo ")
|
self.type("echo\ttest")
|
||||||
self.expand()
|
|
||||||
self.insert("test")
|
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"test run ")
|
self.assertEqual(self.output,"test run")
|
||||||
|
|
||||||
class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
|
class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
|
||||||
snippets = (
|
snippets = (
|
||||||
@ -140,11 +154,10 @@ class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("echo ")
|
self.type("echo\t")
|
||||||
self.expand()
|
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"echo Hallo ")
|
self.assertEqual(self.output,"echo Hallo")
|
||||||
|
|
||||||
class TextTabStopSimpleReplace_ExceptCorrectResult(_VimTest):
|
class TextTabStopSimpleReplace_ExceptCorrectResult(_VimTest):
|
||||||
snippets = (
|
snippets = (
|
||||||
@ -152,18 +165,13 @@ class TextTabStopSimpleReplace_ExceptCorrectResult(_VimTest):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def cmd(self):
|
def cmd(self):
|
||||||
self.insert("hallo ")
|
self.type("hallo\tna\tDu Nase")
|
||||||
self.expand()
|
|
||||||
vim.command(r'call feedkeys("na")')
|
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(self.output,"hallo End na ")
|
self.assertEqual(self.output,"hallo Du Nase na")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
from cStringIO import StringIO
|
|
||||||
|
|
||||||
s = StringIO()
|
|
||||||
|
|
||||||
tests = [
|
tests = [
|
||||||
SimpleExpand_ExceptCorrectResult(),
|
SimpleExpand_ExceptCorrectResult(),
|
||||||
@ -180,10 +188,4 @@ if __name__ == '__main__':
|
|||||||
# suite = unittest.TestLoader(.loadTestsFromModule(__import__("test"))
|
# suite = unittest.TestLoader(.loadTestsFromModule(__import__("test"))
|
||||||
suite = unittest.TestSuite()
|
suite = unittest.TestSuite()
|
||||||
suite.addTests(tests)
|
suite.addTests(tests)
|
||||||
res = unittest.TextTestRunner(stream=s).run(suite)
|
res = unittest.TextTestRunner().run(suite)
|
||||||
|
|
||||||
# if res.wasSuccessful():
|
|
||||||
# vim.command("qa!")
|
|
||||||
|
|
||||||
vim.current.buffer[:] = s.getvalue().split('\n')
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user