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

@ -63,6 +63,7 @@ class SnippetInstance(object):
# 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 endcol-newcol > 0:
if newcol != 0 and vim.eval("mode()") == 'i': if newcol != 0 and vim.eval("mode()") == 'i':
move_one_right = "l" move_one_right = "l"
else: else:
@ -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()

132
test.py Normal file → Executable file
View 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")
# Enter insert mode
self.type("i")
# Execute the command
self.cmd() self.cmd()
self.output = '\n'.join(vim.current.buffer[:])
finally:
vim.command(":q!")
def insert(self,string): handle, fn = tempfile.mkstemp(prefix="PySnipEmuTest",suffix=".txt")
"""A helper function to type some text""" os.close(handle)
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): self.escape()
vim.command("call PyVimSnips_ExpandSnippet()") self.type(":w! %s\n" % fn)
def tearDown(self): # Give screen a chance to send the cmd and vim to write the file
PySnipSnippets.clear_snippets() 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')