Skip to content

Instantly share code, notes, and snippets.

@ericgj
Created September 14, 2012 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericgj/3718933 to your computer and use it in GitHub Desktop.
Save ericgj/3718933 to your computer and use it in GitHub Desktop.
rspec to minitest
# Usage
# ruby gsubs.rb './tests/**/*.rb'
ARGF.replace Dir[ARGV[0]] unless ARGV.empty?
require 'fileutils'
$GSUBS = \
[
[ /^(\s*)(.+)\.should\s+be_true/ , '\1assert \2' ],
[ /^(\s*)(.+)\.should\s+be_false/ , '\1refute \2' ],
[ /^(\s*)(.+)\.should\s*==\s*(.+)$/ , '\1assert_equal \3, \2' ],
[ /^(\s*)(.+)\.should\s*==\s*(.+)$/ , '\1assert_match(\3, \2)' ],
[ /^(\s*)(.+)\.should_not\s+be_(.+)$/ , '\1refute \2.\3?' ],
# this next one isn't quite right because it needs to wrap the next lines as a lambda
[ /\.should_receive\(([^\)]+)\)\.and_return\(([^\)]+)\)/, '.stub(\1, \2) do |s| # FIXME' ],
]
def changed_file(old_path)
File.join( File.dirname(old_path), 'changed', File.basename(old_path) )
end
def flush_to_file!(lines, fname)
FileUtils.mkdir_p File.dirname(fname)
File.open(fname, 'w') do |f|
lines.each do |str| f.puts str end
end
lines.clear
end
last_f = nil
changed = []
ARGF.each_line do |line|
unless last_f.nil? || last_f == ARGF.path
new_f = changed_file(last_f)
flush_to_file! changed, new_f
end
new_line = $GSUBS.inject('') do |str, (rx, replace)|
str = line.gsub(rx, replace)
end
changed << new_line
last_f = ARGF.path
end
unless changed.empty?
new_f = changed_file(last_f)
flush_to_file! changed, new_f
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment