Created
September 19, 2012 19:02
-
-
Save jch/3751529 to your computer and use it in GitHub Desktop.
Turn shoulda formatted tests into traditional test-unit format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
banner = <<-USAGE | |
Turn shoulda formatted tests into traditional test-unit format | |
Edit in place: | |
find test -name '*_test.rb' | xargs ruby -p -i.bak test-unitfy.rb | |
Preview: | |
ruby -p test-unitfy.rb test/unit/foo_test.rb | |
USAGE | |
unless $_ | |
puts banner | |
exit(1) | |
end | |
require 'active_support/inflector' | |
TEST_REGEX ||= /(\s*)test "(.*?)" do/ | |
DESC_REGEX ||= /^context "(.+?)" do/ | |
REQ_STR ||= "require File.expand_path('../../../test_helper', __FILE__)" | |
def methodify(s) | |
s.gsub(/\s/, '_').downcase | |
end | |
case $_ | |
when TEST_REGEX # no captures unless this is done | |
$_.gsub!(TEST_REGEX, " def test_#{methodify($2)}") | |
when DESC_REGEX | |
replacement = $1.classify + "Test" | |
$_.gsub!(DESC_REGEX, "class #{replacement} < Test::Unit::TestCase") | |
else | |
$_.gsub!(REQ_STR, 'require "test_helper"') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment