From e66d4adfda25ef96f160ab05ed046a7b15ea53bf Mon Sep 17 00:00:00 2001 From: "rygwdn@gmail.com" <> Date: Sun, 20 Feb 2011 13:11:51 -0400 Subject: [PATCH 1/3] 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! --- test.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/test.py b/test.py index a650c4a..c0766ea 100755 --- a/test.py +++ b/test.py @@ -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) From 45bad8667cb674fee1a43f8fc558c5943d735ed2 Mon Sep 17 00:00:00 2001 From: "rygwdn@gmail.com" <> Date: Mon, 28 Feb 2011 22:00:47 -0400 Subject: [PATCH 2/3] try to ensure that the GVIM window is open before sending keys to it --- test.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test.py b/test.py index 9512f96..948eac2 100755 --- a/test.py +++ b/test.py @@ -65,7 +65,7 @@ COMPL_ACCEPT = chr(25) if WIN: # import windows specific modules - import win32api, win32com, win32con, win32com.client + import win32com.client, win32gui shell = win32com.client.Dispatch("WScript.Shell") def focus_win(title=None): @@ -73,6 +73,12 @@ def focus_win(title=None): raise Exception("Failed to switch to GVim window") time.sleep(1) +def is_focused(title=None): + cur_title = win32gui.GetWindowText(win32gui.GetForegroundWindow()) + if (title or "- GVIM") in cur_title: + return True + return False + BRACES = re.compile("([}{])") WIN_ESCAPES = ["+", "^", "%", "~", "[", "]", "<", ">", "(", ")"] WIN_REPLACES = [ @@ -106,7 +112,14 @@ def send_win(keys, session): seq_o = seq seq = convert_keys(seq) - print "keys: '%s' -> '%s'" % (seq_o, seq.replace("~", "\n")) + + if not is_focused(): + time.sleep(2) + focus() + if not is_focused(): + # This is the only way I can find to stop test execution + raise KeyboardInterrupt("Failed to focus GVIM") + shell.SendKeys(seq) ################ end windows ################ From 53f3df59db3d9f450e2095882d9468efdb437ce7 Mon Sep 17 00:00:00 2001 From: "rygwdn@gmail.com" <> Date: Mon, 28 Feb 2011 22:41:04 -0400 Subject: [PATCH 3/3] added ability to skip tests depending on platform --- test.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test.py b/test.py index 948eac2..adf9243 100755 --- a/test.py +++ b/test.py @@ -162,6 +162,10 @@ class _VimTest(unittest.TestCase): sleeptime = 0.00 output = None + skip_on_windows = False + skip_on_linux = False + skip_on_mac = False + def send(self,s): send(s, self.session) @@ -191,8 +195,19 @@ class _VimTest(unittest.TestCase): def _options_off(self): pass + def _skip(self, reason): + if hasattr(self, "skipTest"): + self.skipTest(reason) + def setUp(self): - #focus() + system = platform.system() + if self.skip_on_windows and system == "Windows": + return self._skip("Running on windows") + if self.skip_on_linux and system == "Linux": + return self._skip("Running on Linux") + if self.skip_on_mac and system == "Darwin": + return self._skip("Running on Darwin/Mac") + self.send(ESC) self.send(":py UltiSnips_Manager.reset(test_error=True)\n") @@ -564,32 +579,39 @@ class TabStop_Multiline_DelFirstOverwriteSecond_Overwrite(_VimTest): # ShellCode Interpolation # ########################### class TabStop_Shell_SimpleExample(_VimTest): + skip_on_windows = True snippets = ("test", "hi `echo hallo` you!") keys = "test" + EX + "and more" wanted = "hi hallo you!and more" class TabStop_Shell_TextInNextLine(_VimTest): + skip_on_windows = True snippets = ("test", "hi `echo hallo`\nWeiter") keys = "test" + EX + "and more" wanted = "hi hallo\nWeiterand more" class TabStop_Shell_InDefValue_Leave(_VimTest): + skip_on_windows = True sleeptime = 0.09 # Do this very slowly snippets = ("test", "Hallo ${1:now `echo fromecho`} end") keys = "test" + EX + JF + "and more" wanted = "Hallo now fromecho endand more" class TabStop_Shell_InDefValue_Overwrite(_VimTest): + skip_on_windows = True snippets = ("test", "Hallo ${1:now `echo fromecho`} end") keys = "test" + EX + "overwrite" + JF + "and more" wanted = "Hallo overwrite endand more" class TabStop_Shell_TestEscapedChars_Overwrite(_VimTest): + skip_on_windows = True snippets = ("test", r"""`echo \`echo "\\$hi"\``""") keys = "test" + EX wanted = "$hi" class TabStop_Shell_TestEscapedCharsAndShellVars_Overwrite(_VimTest): + skip_on_windows = True snippets = ("test", r"""`hi="blah"; echo \`echo "$hi"\``""") keys = "test" + EX wanted = "blah" class TabStop_Shell_ShebangPython(_VimTest): + skip_on_windows = True sleeptime = 0.09 # Do this very slowly snippets = ("test", """Hallo ${1:now `#!/usr/bin/env python print "Hallo Welt"