UltiSnips/test.py

190 lines
4.8 KiB
Python
Raw Normal View History

2009-06-23 08:45:04 -04:00
#!/usr/bin/env python
# encoding: utf-8
#
import vim
import unittest
from PySnipEmu import PySnipSnippets
class _VimTest(unittest.TestCase):
def setUp(self):
PySnipSnippets.reset()
for sv,content in self.snippets:
PySnipSnippets.add_snippet(sv,content)
2009-06-23 08:45:04 -04:00
vim.command(":new")
try:
self.cmd()
self.output = '\n'.join(vim.current.buffer[:])
finally:
vim.command(":q!")
2009-06-23 08:45:04 -04:00
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)
2009-06-23 08:45:04 -04:00
def expand(self):
vim.command("call PyVimSnips_ExpandSnippet()")
def tearDown(self):
PySnipSnippets.clear_snippets()
def cmd(self):
"""Overwrite these in the children"""
pass
##################
# Simple Expands #
##################
class _SimpleExpands(_VimTest):
snippets = (
("hallo", "Hallo Welt!"),
)
2009-06-23 08:45:04 -04:00
class SimpleExpand_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("hallo ")
self.expand()
def runTest(self):
self.assertEqual(self.output,"Hallo Welt! ")
class SimpleExpandTypeAfterExpand_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("hallo ")
self.expand()
self.insert("and again")
def runTest(self):
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")
def runTest(self):
self.assertEqual(self.output,"na du Hallo Welt!and again ")
class DoNotExpandAfterSpace_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("hallo ")
self.expand()
def runTest(self):
self.assertEqual(self.output,"hallo ")
class ExpandInTheMiddleOfLine_ExceptCorrectResult(_SimpleExpands):
def cmd(self):
self.insert("Wie hallo gehts?")
vim.command("normal 02f ")
self.expand()
def runTest(self):
self.assertEqual(self.output,"Wie Hallo Welt! gehts?")
class MultilineExpand_ExceptCorrectResult(_VimTest):
snippets = (
("hallo", "Hallo Welt!\nUnd Wie gehts?"),
)
2009-06-23 08:45:04 -04:00
def cmd(self):
self.insert("Wie hallo gehts?")
vim.command("normal 02f ")
self.expand()
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!")
def runTest(self):
self.assertEqual(self.output,
"Wie Hallo Welt!\nUnd Wie gehts?Huiui! gehts?")
2009-06-23 08:45:04 -04:00
############
# TabStops #
############
class ExitTabStop_ExceptCorrectResult(_VimTest):
snippets = (
("echo", "$0 run"),
)
2009-06-23 08:45:04 -04:00
def cmd(self):
self.insert("echo ")
self.expand()
self.insert("test")
def runTest(self):
self.assertEqual(self.output,"test run ")
class TextTabStopNoReplace_ExceptCorrectResult(_VimTest):
snippets = (
("echo", "echo ${1:Hallo}"),
)
def cmd(self):
self.insert("echo ")
self.expand()
def runTest(self):
self.assertEqual(self.output,"echo Hallo ")
class TextTabStopSimpleReplace_ExceptCorrectResult(_VimTest):
snippets = (
("hallo", "hallo ${0:End} ${1:Beginning}"),
)
def cmd(self):
self.insert("hallo ")
self.expand()
vim.command(r'call feedkeys("na")')
2009-06-23 08:45:04 -04:00
def runTest(self):
self.assertEqual(self.output,"hallo End na ")
2009-06-23 08:45:04 -04:00
if __name__ == '__main__':
import sys
from cStringIO import StringIO
s = StringIO()
tests = [
SimpleExpand_ExceptCorrectResult(),
SimpleExpandTypeAfterExpand_ExceptCorrectResult(),
SimpleExpandTypeAfterExpand1_ExceptCorrectResult(),
DoNotExpandAfterSpace_ExceptCorrectResult(),
ExpandInTheMiddleOfLine_ExceptCorrectResult(),
MultilineExpand_ExceptCorrectResult(),
MultilineExpandTestTyping_ExceptCorrectResult(),
ExitTabStop_ExceptCorrectResult(),
TextTabStopNoReplace_ExceptCorrectResult(),
TextTabStopSimpleReplace_ExceptCorrectResult(),
]
# suite = unittest.TestLoader(.loadTestsFromModule(__import__("test"))
suite = unittest.TestSuite()
suite.addTests(tests)
2009-06-23 08:45:04 -04:00
res = unittest.TextTestRunner(stream=s).run(suite)
# if res.wasSuccessful():
# vim.command("qa!")
2009-06-23 08:45:04 -04:00
vim.current.buffer[:] = s.getvalue().split('\n')