Skip to content

Commit

Permalink
Fix testing
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueDrink9 committed Feb 7, 2021
1 parent e692273 commit e5b28e4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 40 deletions.
102 changes: 67 additions & 35 deletions tests/run_vimahk_tests.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
; Results are outputed as the current time and date in %A_ScriptDir%\testlogs

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance Force
#warn
sendlevel, 1 ; So vim commands get triggered by this script
sendlevel, 5 ; So vim commands get triggered by this script
SetTitleMatchMode 2 ; window title functions will match by containing the match text.
; Only affects sendevent, used for sending the test one key at a time.
; Gives the vim script time to interpret it, also useful to increase when
; debugging failures.
SetKeyDelay, 80
; (gives vim script time to react).
DetectHiddenWindows, on
; #Warn ; Enable warnings to assist with detecting common errors.

; Contains clipboard related functions, among others.
#include %A_ScriptDir%\utility_functions.ahk
Expand All @@ -38,25 +37,15 @@ TestsFailed := False
IfNotExist, testLogs
FileCreateDir, testLogs
LogFileName = testLogs\%A_Now%.txt ;%A_Scriptdir%\testlogs\%A_Now%.txt
IniOriginal = "%A_AppData%\AutoHotkey\vim_ahk.ini"
IniBackup = "%A_Temp%\AutoHotkey\vim_ahk_backup_%A_Now%.ini"

; Initialise the programs
SetWorkingDir %A_ScriptDir%\testLogs ; Temp vim files are put out of the way.
run, cmd.exe /r gvim -u NONE,,,VimPID
sleep, 200
WaitForWindowToActivate("ahk_class Vim") ; Wait for vim to start
WinMaximize,ahk_class Vim
SetWorkingDir %A_ScriptDir%

send :imap jj <esc>{return} ; Prepare vim
; So newlines are handled correctly between both notepad and vim
send :imap ^q`r^q`n ^q{return 2}

Run, Notepad,,,NotepadPID
sleep, 200
WaitForWindowToActivate("ahk_class Notepad") ; Wait for notepad to start
WinMaximize, ahk_class Notepad

run, %A_ScriptDir%/vim.ahk --testing,,, AHKVimPID ; Run our vim emulator script.
StartVim()
StartNotepad()
BackupIni()
; Run our vim emulator script.
run, %A_ScriptDir%/../vim.ahk --testing,,, AHKVimPID

; Set all our scripts and two testing programs to Above normal priority, for test reliability.
Process, Priority, ,A ; This script
Expand Down Expand Up @@ -94,10 +83,7 @@ ReadFileWithComments(OutputArray){
{
testString := output[1]
; escape special chars
StringReplace, testString, testString, ^, {^}, A
StringReplace, testString, testString, +, {+}, A
StringReplace, testString, testString, #, {#}, A
StringReplace, testString, testString, !, {!}, A
testString := SanitiseTextForAhk(testString)
OutputArray.push(testString)
}
}
Expand All @@ -116,13 +102,44 @@ RunTests(){
EndTesting()
}

StartVim(){
global VimPID, VimWinID
SetWorkingDir %A_ScriptDir%\testLogs ; Temp vim files are put out of the way.
; Using this sets VimPID to the cmd.exe PID, which isn't correct.
run, cmd.exe /r gvim -u NONE,,,VimPID
; Usually not found by AHK.
; run, gvim -u NONE,,,VimPID
sleep, 200
WinActivate, ahk_class Vim ahk_pid %VimPID%
WaitForWindowToActivate("ahk_class Vim")
WinMaximize,ahk_class Vim
SetWorkingDir %A_ScriptDir%
WinGet, VimWinID, ID, A

send :imap jj <esc>{return} ; Prepare vim
; So newlines are handled correctly between both notepad and vim
send :imap ^q`r^q`n ^q{return 2}
}

StartNotepad(){
global NotepadPID, NotepadWinID
Run, Notepad,,,NotepadPID
sleep, 200
WinActivate, ahk_class Notepad ahk_pid %NotepadPID%
WaitForWindowToActivate("ahk_class Notepad")
WinMaximize, ahk_class Notepad
WinGet, NotepadWinID, ID, A
}

SwitchToVim(){
WinActivate, ahk_class Vim
global VimPID, VimWinID
WinActivate, ahk_class Vim ahk_id %VimWinID%
WaitForWindowToActivate("ahk_class Vim")
}

SwitchToNotepad(){
WinActivate, ahk_class Notepad
global NotepadPID, NotepadWinID
WinActivate, ahk_class Notepad ahk_id %NotepadWinID%
WaitForWindowToActivate("ahk_class Notepad")
}

Expand Down Expand Up @@ -180,7 +197,7 @@ SendTestToVimAndReturnResult(test){
sleep, 50
SaveClipboard()
clipboard= ; Empty the clipboard for clipwait to work
send {esc}:`%d{numpadAdd} ; select all text, cut to system clipboard
send {esc}:`%d{+} ; select all text, cut to system clipboard
send {return}
ClipWait
output := Clipboard
Expand All @@ -189,12 +206,21 @@ SendTestToVimAndReturnResult(test){
}

TestAndCompareOutput(test){
global Log
NotepadOutput := SendTestToNotepadAndReturnResult(test)
VimOutput := SendTestToVimAndReturnResult(test)
CompareStrings(NotepadOutput, VimOutput, test)
}

; Replaces AHK special characters with their sanitised versions, so they can be
; sent literally.
SanitiseTextForAhk(input){
input := StrReplace(input, "^", "{^}")
input := StrReplace(input, "!", "{!}")
input := StrReplace(input, "+", "{+}")
input := StrReplace(input, "#", "{#}")
return input
}

; Use a diff, then log the result in temp files
CompareStrings(NotepadOutput, VIMOutput, CurrentTest){
Global LogFileName
Expand Down Expand Up @@ -236,7 +262,7 @@ EndTesting(){
SwitchToVim()
send :q{!}
send {return} ; Exit vim.

if (TestsFailed == True)
{
if not isQuiet() {
Expand All @@ -255,16 +281,22 @@ EndTesting(){
}
}

BackupIni(){
global IniOriginal, IniBackup
FileMove, %IniOriginal%, %IniBackup%
}


RestoreIni(){
global IniOriginal, IniBackup
FileMove, %IniBackup%, %IniOriginal%, True
}

EndScript(exitCode){
Global NotepadPID
Global AHKVimPID
Global VimPID
Global NotepadPID, AHKVimPID, VimPID
RestoreIni()
process, Close, %NotepadPID%
process, Close, %AHKVimPID%
process, Close, %VimPID%
process, Close, %AHKVimPID%
if exitCode = 1
ExitApp, 1 ; Failed exit
else
Expand All @@ -273,4 +305,4 @@ EndScript(exitCode){

EndScript(1)

+ & esc::EndScript(1) ; Abort
LShift & esc::EndScript(1) ; Abort
11 changes: 6 additions & 5 deletions tests/testcases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
; Use {esc} to send an escape key, and {return} to send a newline.
; Otherwise it is best to avoid {} characters.
; Please put a comment at the end of your test case stating what it tests.
; Do not finish a test with a capital letter, for some reason that breaks vim
;
; Sample text (reference only, see testscript for actual text):
;
Expand All @@ -20,12 +21,12 @@

iAt start of first lin.{esc}ie{esc}IWord ; i,I
ahe {esc}{esc}A Also this.; a, A, double esc
w$^iBOL; ^, no whitespace
I {esc}$^ialmostBOL; ^, whitespace
w$0iBOL; 0, no whitespace
w$^iBOLne; ^, no whitespace
I {esc}$^ialmostBOLne; ^, whitespace
w$0iBOLne; 0, no whitespace
$bbbi[3rd word from end]; b with punctuation?
GiEOF; G, go to end of file.
GiEOF{esc}ggiBOF; gg (go to beginning of file)
GiEOFle; G, go to end of file.
GiEOF{esc}ggiBOFle; gg (go to beginning of file)
ddGp; delete line, paste linewise at final line.
dw; d with single motion
d4w; d with repeat motion
Expand Down

0 comments on commit e5b28e4

Please sign in to comment.