Added initial support for testing on windows.

Focuses a GVim window, which is assumed to be in the ultisnips directory,
then starts sending key presses to it. Please don't change the active
window while it's running!
This commit is contained in:
rygwdn@gmail.com 2011-02-20 13:11:51 -04:00
parent b371f0c8c4
commit e66d4adfda

80
test.py
View File

@ -28,8 +28,15 @@ import os
import tempfile
import unittest
import time
import re
import platform
WIN = platform.system() == "Windows"
from textwrap import dedent
# Some constants for better reading
BS = '\x7f'
ESC = '\x1b'
@ -38,6 +45,9 @@ ARR_R = '\x1bOC'
ARR_U = '\x1bOA'
ARR_D = '\x1bOB'
# multi-key sequences generating a single key press
SEQUENCES = [ARR_L, ARR_R, ARR_U, ARR_D]
# Defined Constants
JF = "?" # Jump forwards
JB = "+" # Jump backwards
@ -49,10 +59,75 @@ EA = "#" # Expand anonymous
COMPL_KW = chr(24)+chr(14)
COMPL_ACCEPT = chr(25)
def send(s,session):
################ windows ################
if WIN:
# import windows specific modules
import win32api, win32com, win32con, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
def focus_win(title=None):
if not shell.AppActivate(title or "- GVIM"):
raise Exception("Failed to switch to GVim window")
time.sleep(1)
BRACES = re.compile("([}{])")
WIN_ESCAPES = ["+", "^", "%", "~", "[", "]", "<", ">", "(", ")"]
WIN_REPLACES = [
(BS, "{BS}"),
(ARR_L, "{LEFT}"),
(ARR_R, "{RIGHT}"),
(ARR_U, "{UP}"),
(ARR_D, "{DOWN}"),
("\t", "{TAB}"),
("\n", "~"),
(ESC, "{ESC}"),
]
def convert_keys(keys):
keys = BRACES.sub(r"{\1}", keys)
for k in WIN_ESCAPES:
keys = keys.replace(k, "{%s}" % k)
for f, r in WIN_REPLACES:
keys = keys.replace(f, r)
return keys
SEQ_BUF = []
def send_win(keys, session):
global SEQ_BUF
SEQ_BUF.append(keys)
seq = "".join(SEQ_BUF)
for f in SEQUENCES:
if f.startswith(seq) and f != seq:
return
SEQ_BUF = []
seq_o = seq
seq = convert_keys(seq)
print "keys: '%s' -> '%s'" % (seq_o, seq.replace("~", "\n"))
shell.SendKeys(seq)
################ end windows ################
def send_screen(s,session):
s = s.replace("'", r"'\''")
os.system("screen -x %s -X stuff '%s'" % (session, s))
def send(s, session):
if WIN:
send_win(s, session)
else:
send_screen(s, session)
def focus(title=None):
if WIN:
focus_win(title=title)
def type(str, session, sleeptime):
"""
Send the keystrokes to vim via screen. Pause after each char, so
@ -102,6 +177,7 @@ class _VimTest(unittest.TestCase):
pass
def setUp(self):
#focus()
self.send(ESC)
self.send(":py UltiSnips_Manager.reset(test_error=True)\n")
@ -2241,6 +2317,8 @@ if __name__ == '__main__':
test_loader = unittest.TestLoader()
all_test_suites = test_loader.loadTestsFromModule(__import__("test"))
focus()
# Ensure we are not running in VI-compatible mode.
send(""":set nocompatible\n""", options.session)