Idea: some kind of automation to test vim / emacs

From razwiki
Jump to navigation Jump to search

Would be nice to be able to say

"opening vim, pressing v$y should copy the first line onto the clipboard." Then I can make changes and still see that it works...

One way to do this is to use tmux. But let's start by just modeling this in python.

def test_editor(executable: emacs | vim, keystrokes=""): ...


 def test_vimrc(keystrokes, input_text, expected_text, config):
     vim_session = open_vim(config)

     vim_buffer = prepare_buffer(vim_session, input_text)

     assembled_vim_command = "i" + input_text + "<esc>" + keystrokes

     result = run_keystrokes(vim_buffer, assembled_vim_command)

     assert result == expected_text

 def test_default_vimrc(keystrokes, input_text, expected_text):
     test_vimrc(keystrokes, input_text, expected_text, open("~/.vimrc").read())

 test_vimrc(keystrokes="-", input_text="1\n2", output_text="2\n1", config="nnoremap - ddp")

A more involved example:

def test_trim_whitespace_on_save():
    config = """
    augroup trimwhitespace
      autocmd BufWritePre * call TrimWhitespace()
    augroup END
    test_my_vimrc(keystrokes="o<cr>", input_text="just a line", output_text="just a line", config=config)

It would be useful to have some canned source files:

sample_python = """
def main():
    print("hello world")

if __name__ == "__main__":

Maybe they could even have some syntactic issue that a vim editing session would fix.

But more realistically, I'd want to even assert that the view is the same. Fortunately, commandline editors produce VT100 escape sequences which can be spooled then played to display a buffer.

Ideally, even different editors would produce the same final escape sequence paint result; this would require totally removing any modeline etc. Or at least making the modeline completely editor-independent.

A workflow to make a test could be thus: start recording. Open editor, do keystrokes, stop recording. Save the inputs and final rendering of the editor to a file. Then whenever anything changes, playing the same inputs would produce a different rendering. This allows making tests easily, since you don't have to write code for all the interactions, and test end-to-end. Cool!