Reputation: 15967
I've written a simple script that works but I'm looking for a better implementation.
Here is my ideal scenario. I'm sitting in a test file that contains many tests like this:
def test_cleans_number
number = PhoneNumber.new("(123) 456-7890").number
assert_equal "1234567890", number
end
def test_cleans_a_different_number
number = PhoneNumber.new("(987) 654-3210").number
assert_equal "9876543210", number
end
def test_cleans_number_with_dots
number = PhoneNumber.new("456.123.7890").number
assert_equal "4561237890", number
end
I've written this vim script which allows me to put my cursor on the definition line of the test and it will run that test and that test only:
function! RunNearestTest()
let line = getline('.')
let test_name = substitute(line, 'def ', "", "g")
exec ":!ruby " . @% . " -n " . test_name
endfunction
" run test runner
map <leader>T :call RunNearestTest()<cr>
If it's not obvious from the script, I need the test name in order to run that test specifically. In the first example the test name is test_cleans_number
.
As I said, this works fine but what I'd like to do is be on any line in the test and it would know to jump to the previous def
, get the test_name
and then run the execution. I thought searchpair()
would be my goto here but alas I can't find enough documentation on it.
I'm happy to provide any files or extras you might need here, thank you!
Upvotes: 0
Views: 102
Reputation: 196789
The search()
function returns the number of the matching line:
function! RunNearestTest()
let line_text = getline(search('def ', 'bcW'))
let test_name = substitute(line_text, 'def ', "", "g")
exec ":!ruby " . @% . " -n " . test_name
endfunction
See :help search()
.
Upvotes: 3