As a Rubyist and Textmate user, you’ll probably be aware that def
will tab-expand to stub out a method definition. You might also be aware that, for the purposes of Test::Unit, deft
will tab-expand to a test method beginning def test_
, allowing you to append the name of your test.
But that’s not much more help, because if we’re naming our tests properly, they’re probably going to have very_long_names, and hitting underscore all those times is a bit of a pain. So I rectified that, with this command (and it’s a command, rather than a snippet, because of all the processing it does). Pull up the commands dialog (Command-Opt-Control-C), create a new command in the Ruby bundle, and give it the following code:
#!/usr/bin/env ruby
name = STDIN.read.strip
testname = name.gsub(" ", "_").downcase
print <<OUTPUT
def test_#{testname}
\$0
end
OUTPUT
The command’s Input should be “Selected Text” or “Line”; its Output should be “Insert as Snippet”. The scope should be set to source.ruby
. And give it whatever key definition you want; I’ve got it on ctrl-opt-shift-t.
Usage is easy. On a new line in your test file, type the name of your test in plain English with no punctuation, eg:
get to index should list all items
and then hit your shortcut. You’ll get the following out:
def test_get_to_index_should_list_all_items
end
and your cursor will be slap bang in the middle of the test, indented, ready to write. That’s what I really want from a test definition snippet – something more than deft
supplies. It’s another minute or two’s work to make it strip punctuation, so you can convert real sentences to test cases. I just decided to condition myself to save on coding on this morning’s commute.