I mostly work in source code hierarchies where for a given source file X.source the location of the file with the unit tests is tests/test_X.source and more often than not I need to do edit the unit tests after having opened the actual source file.
Being the geek that I am I *obviously* need to come up with some sort of optimisation or shortcut even if it takes 10x as long as stupidly typing in ":e tests/test_X.source" all the time
Thankfully, the solution in vim turns out to be quite straightforward. The following mapping (conveniently added to your $HOME/.vimrc file) will open the unit test file when you type %%
nnoremap %% :e =escape(expand("%:h")."/tests/test_".expand("%:t"), "")^M
Please note that the last bit ("^M") is just one character (the Enter key) that you can get by typing ^V followed by the Enter key.
I guess what I should really do is write a configurable vim plugin that opens arbitrary files/locations based on the current buffer/location. Oh well, so much to do and so little time
September 27, 2010 at 9:51 am |
I like Derek Wyatt’s FSwitch plugin, available here:
http://www.vim.org/scripts/script.php?script_id=2590
It’s very configurable. I use it for exactly the use case
you are describing.
Michael Henry
September 27, 2010 at 2:35 pm |
Use instead of a literal ^M, it’ll be easier to enter or copy and paste.
I use a plugin of my own, so it’s infinitely configurable
http://mg.pov.lt/vim/plugin/py-test-switcher.vim
http://mg.pov.lt/vim/plugin/py_test_switcher.py
September 27, 2010 at 2:36 pm |
I meant to say “Use <CR> instead of a literal ^M, it’ll be easier to enter”.
How ironic.
September 27, 2010 at 8:42 pm |
I’ve done a plugin specifically for assisting you with switching focus from source/test pair windows when doing Python unit testing in Vim. It’s over here, at Github: http://github.com/nvie/vim-pyunit
It’s pretty configurable, and if I can judge it correctly from your post, it probably suits your need without any additional configuration. The default behaviour creates tests in “tests/test_X.py”.
Let me know if you find it useful. I’ll probably write a blog post about the plugin soon, maybe also a small screen cast of the plugin in action, too, if the moon alignment is right.
Cheers,
Vincent
September 28, 2010 at 11:16 am |
Cool! Thanks for the tip.
[]‘s, HB!
September 28, 2010 at 3:57 pm |
tpope’s rake plugin does this too. :R (related file) switches between source and test.
http://github.com/tpope/vim-rake/blob/master/doc/rake.txt#L30
just fwiw.
October 6, 2010 at 12:37 pm |
Hello, thanks for all the great comments and pointers to existing plugins!