Using Search and Replace Regular Expressions to Convert from Test::Unit to Rspec

June 3, 2007 Chad Woolley

I was just converting some Test::Unit tests to Rspec, and these regexps were handy. In one file, they handled 51 out of 53 lines, saving my fingers a lot of work. Tests can take an infinite variety of formats, so these obviously won’t apply to everything, but they do illustrate how to use regexp substitution. This is using TextMate, your regexp implementation may vary…

from -> to
search string
replace string

def test_foo -> it "test_foo" do
def (test_[a-z_]*)
it "$1" do

assert !foo -> foo.should_not be_true
assert !(.*)$
$1.should_not be_true

assert foo -> foo.should be_true
assert (.*)$
$1.should be_true

assert_equal foo, bar -> bar.should == foo
assert_equal (.*), (.*)$
$2.should == $1

About the Author

Biography

Previous
Multi-clipboard for Mac
Multi-clipboard for Mac

IntelliJ IDEA has a great feature: if you hit control-shift-V you see a list of the ten most recent selecti...

Next
Rails Filter Parameter Logging
Rails Filter Parameter Logging

Q: How do you keep passwords from appearing in plain text in your Rails log file? A: Filter Parameter Logg...