Tests now work through simulating input via screen. Good solution

This commit is contained in:
Holger Rapp 2009-07-01 10:39:46 +02:00
parent 1926c49f96
commit 1e3dd7b4be
2 changed files with 88 additions and 88 deletions

View File

@ -33,7 +33,7 @@ class SnippetInstance(object):
self._start = start
self._end = end
self._ts = ts
self._cts = 1
def select_next_tab(self):
@ -42,7 +42,7 @@ class SnippetInstance(object):
self._cts = 0
else:
self._cts = 1
ts = self._ts[self._cts]
lineno, col = self._start
@ -53,23 +53,24 @@ class SnippetInstance(object):
else:
newcol = ts.span[0]
endcol = ts.span[1]
self._cts += 1
vim.current.window.cursor = newline, newcol
# Select the word
# Depending on the current mode and position, we
# might need to move escape out of the mode and this
# will move our cursor one left
if newcol != 0 and vim.eval("mode()") == 'i':
move_one_right = "l"
else:
move_one_right = ""
if endcol-newcol > 0:
if newcol != 0 and vim.eval("mode()") == 'i':
move_one_right = "l"
else:
move_one_right = ""
vim.command(r'call feedkeys("\<Esc>%sv%il\<c-g>")'
% (move_one_right, endcol-newcol-1))
vim.command(r'call feedkeys("\<Esc>%sv%il\<c-g>")'
% (move_one_right, endcol-newcol-1))
class Snippet(object):
_TB_EXPR = re.compile(r'\$(?:(?:{(\d+):(.*?)})|(\d+))')
@ -133,19 +134,19 @@ class Snippet(object):
newcol = col + len(lines[-1])
else:
newcol = len(lines[-1])
lines[0] = before + lines[0]
lines[-1] += after
vim.current.buffer[lineno-1:lineno-1+len(lines)] = lines
vim.current.window.cursor = newline, newcol
if len(ts):
s = SnippetInstance( (lineno,col), (newline,newcol), ts)
s.select_next_tab()
return s
@ -154,15 +155,12 @@ class SnippetManager(object):
self.reset()
def reset(self):
self.clear_snippets()
self._snippets = {}
self._current_snippets = []
def add_snippet(self,trigger,value):
self._snippets[trigger] = Snippet(trigger,value)
def clear_snippets(self):
self._snippets = {}
def try_expand(self):
if len(self._current_snippets):
self._current_snippets[-1].select_next_tab()
@ -183,7 +181,7 @@ class SnippetManager(object):
s = self._snippets[word].launch(before.rstrip()[:-len(word)], after)
if s is not None:
self._current_snippets.append(s)
def cursor_moved(self):
pass

138
test.py Normal file → Executable file
View File

@ -2,38 +2,60 @@
# encoding: utf-8
#
import vim
import os
import tempfile
import unittest
from PySnipEmu import PySnipSnippets
import time
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):
PySnipSnippets.reset()
self.escape()
self.type(":py PySnipSnippets.reset()\n")
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")
try:
self.cmd()
self.output = '\n'.join(vim.current.buffer[:])
finally:
vim.command(":q!")
# Clear the buffer
self.type("bggVGd")
def insert(self,string):
"""A helper function to type some text"""
vim.command('normal i%s' % string)
def change(self,string):
"""A helper function to type some text"""
vim.command('normal c%s' % string)
# Enter insert mode
self.type("i")
def expand(self):
vim.command("call PyVimSnips_ExpandSnippet()")
# Execute the command
self.cmd()
def tearDown(self):
PySnipSnippets.clear_snippets()
handle, fn = tempfile.mkstemp(prefix="PySnipEmuTest",suffix=".txt")
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):
"""Overwrite these in the children"""
@ -50,43 +72,37 @@ class _SimpleExpands(_VimTest):
class SimpleExpand_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("hallo ")
self.expand()
self.type("hallo\t")
def runTest(self):
self.assertEqual(self.output,"Hallo Welt! ")
self.assertEqual(self.output,"Hallo Welt!")
class SimpleExpandTypeAfterExpand_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("hallo ")
self.expand()
self.insert("and again")
self.type("hallo\tand again")
def runTest(self):
self.assertEqual(self.output,"Hallo Welt!and again ")
self.assertEqual(self.output,"Hallo Welt!and again")
class SimpleExpandTypeAfterExpand1_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("na du hallo ")
self.expand()
self.insert("and again")
self.type("na du hallo\tand again")
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):
def cmd(self):
self.insert("hallo ")
self.expand()
self.type("hallo \t")
def runTest(self):
self.assertEqual(self.output,"hallo ")
self.assertEqual(self.output,"hallo ")
class ExpandInTheMiddleOfLine_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("Wie hallo gehts?")
vim.command("normal 02f ")
self.expand()
self.type("Wie hallo gehts?")
self.escape()
self.type("bhi\t")
def runTest(self):
self.assertEqual(self.output,"Wie Hallo Welt! gehts?")
@ -97,22 +113,22 @@ class MultilineExpand_ExceptCorrectResult(_VimTest):
)
def cmd(self):
self.insert("Wie hallo gehts?")
vim.command("normal 02f ")
self.expand()
self.type("Wie hallo gehts?")
self.escape()
self.type("bhi\t")
def runTest(self):
self.assertEqual(self.output, "Wie Hallo Welt!\nUnd Wie gehts? gehts?")
class MultilineExpandTestTyping_ExceptCorrectResult(_VimTest):
snippets = (
("hallo", "Hallo Welt!\nUnd Wie gehts?"),
)
def cmd(self):
self.insert("Wie hallo gehts?")
vim.command("normal 02f ")
self.expand()
self.insert("Huiui!")
self.type("Wie hallo gehts?")
self.escape()
self.type("bhi\tHuiui!")
def runTest(self):
self.assertEqual(self.output,
@ -127,12 +143,10 @@ class ExitTabStop_ExceptCorrectResult(_VimTest):
)
def cmd(self):
self.insert("echo ")
self.expand()
self.insert("test")
self.type("echo\ttest")
def runTest(self):
self.assertEqual(self.output,"test run ")
self.assertEqual(self.output,"test run")
class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
snippets = (
@ -140,11 +154,10 @@ class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
)
def cmd(self):
self.insert("echo ")
self.expand()
self.type("echo\t")
def runTest(self):
self.assertEqual(self.output,"echo Hallo ")
self.assertEqual(self.output,"echo Hallo")
class TextTabStopSimpleReplace_ExceptCorrectResult(_VimTest):
snippets = (
@ -152,18 +165,13 @@ class TextTabStopSimpleReplace_ExceptCorrectResult(_VimTest):
)
def cmd(self):
self.insert("hallo ")
self.expand()
vim.command(r'call feedkeys("na")')
self.type("hallo\tna\tDu Nase")
def runTest(self):
self.assertEqual(self.output,"hallo End na ")
self.assertEqual(self.output,"hallo Du Nase na")
if __name__ == '__main__':
import sys
from cStringIO import StringIO
s = StringIO()
tests = [
SimpleExpand_ExceptCorrectResult(),
@ -180,10 +188,4 @@ if __name__ == '__main__':
# suite = unittest.TestLoader(.loadTestsFromModule(__import__("test"))
suite = unittest.TestSuite()
suite.addTests(tests)
res = unittest.TextTestRunner(stream=s).run(suite)
# if res.wasSuccessful():
# vim.command("qa!")
vim.current.buffer[:] = s.getvalue().split('\n')
res = unittest.TextTestRunner().run(suite)