Skip to content

Instantly share code, notes, and snippets.

@leschenko
Created December 22, 2014 06:06
Show Gist options
  • Save leschenko/175a4584dc68543de2e7 to your computer and use it in GitHub Desktop.
Save leschenko/175a4584dc68543de2e7 to your computer and use it in GitHub Desktop.
Easy migrate to new rspec expect syntax
#!/usr/bin/env ruby
dir = ARGV[0]
regexps = [
{from: /(\s+)(.+?)\.should ==/, to: '\1expect(\2).to eq'},
{from: /(\s+)(.+?)\.should =~ \[/, to: '\1expect(\2).to match_array ['},
{from: 'be_true', to: 'be_truthy'},
{from: 'be_false', to: 'be_falsey'},
{from: /(\s+)(.+?)\.should(?:(_)?(not)?) be_(\w+)/, to: '\1expect(\2).\4\3to be_\5'},
{from: /(\s+)(.+?)\.should(?:(_)?(not)?)_(\w+)/, to: '\1expect(\2).\4\3to \5'},
{from: /(\s+)(.+?)\.should (?:have|has)\((\d+)\)[.\w]*/, to: '\1expect(\2.length).to eq \3'},
{from: /(\s+)(.+?)\.stub/, to: '\1allow(\2).to receive'}
]
Dir["#{dir}/**/*_spec.rb"].each do |path|
puts path
data = File.read(path)
regexps.each { |regexp| data.gsub!(regexp[:from], regexp[:to]) }
File.write(path, data)
end
puts 'DONE!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment