Skip to content

Instantly share code, notes, and snippets.

@zaccari
Created September 25, 2015 08:52
Show Gist options
  • Save zaccari/aeab1d310cfd82fe3957 to your computer and use it in GitHub Desktop.
Save zaccari/aeab1d310cfd82fe3957 to your computer and use it in GitHub Desktop.
Syntax converter for RSpec 2 to 3
#!/usr/bin/env bash
#
# Scans the given path(s) and converts shoulda syntax to expectations and
# upgrades rspec 3 syntaxes.
#
# Examples
#
# $ rspec2to3 spec/lib/**/*_spec.rb
files="${@}"
echo "Scanning $files"
### stub
# Replace object.stub(...) with allow(object).to receive(...)
perl -pi -w -e "s/(\S.*)\.stub\(/allow\(\1\)\.to receive\(/g;" $files
### allow any_instance
# allow(DeviceConfigurationPolicy.any_instance).to
# allow\((.*)\.any_instance\)\.to
# allow_any_instance_of\(\1\)\.to
# Replace allow(class.any_instance).to
perl -pi -w -e "s/allow\((.*)\.any_instance\)\.to/allow_any_instance_of\(\1\)\.to/g;" $files
### expect any_instance
# expect(SmxCellularImporter.any_instance).to
# expect\((.*)\.any_instance\)\.to
# expect_any_instance_of\(\1\)\.to
perl -pi -w -e "s/expect\((.*)\.any_instance\)\.to/expect_any_instance_of\(\1\)\.to/g;" $files
### stub_chain
# Time.stub_chain(:now, :year).and_return(stubbed_year)
# allow(object).to receive_message_chain
# (\S.*)\.stub_chain\(
# allow(\1).to receive_message_chain\(
perl -pi -w -e "s/(\S.*)\.stub_chain\(/allow(\1).to receive_message_chain\(/g;" $files
### should
# helper.timestamp_in_users_timezone(nil, nil).should be_nil
# (\S.*)\.should\s
# expect(\1).to
perl -pi -w -e "s/(\S.*)\.should\s/expect(\1).to /g;" $files
### FIX SHOULD
# expect(it { current_path).to match "/devices/#{@device.mac}" }
# it { current_path.should match
# expect\(it\s*{.*\)\.to match
# it { \1\.should match
perl -pi -w -e "s/expect\(it {(.*)\)\.to match/it {\1\.should match/g;" $files
### should_not
# (\S.*)\.should_not\s
# expect(\1).to_not
perl -pi -w -e "s/(\S.*)\.should_not\s/expect(\1).to_not /g;" $files
### FIX have_css
perl -pi -w -e "s/expect\((it|skip) {(.*)\)\.to have_(css|link)/\1 {\2\.should have_\3/g;" $files
perl -pi -w -e "s/expect\((it|skip) {(.*)\)\.to_not have_(css|link)/\1 {\2\.should_not have_\3/g;" $files
perl -pi -w -e "s/expect\((it|skip) {(.*)\)\.not_to have_(css|link)/\1 {\2\.should_not have_\3/g;" $files
### should_receive
# subject.should_receive(:tmp_path).at_least(:once).and_return(tmp_path)
# expect(subject).to receive(
# (\S.*)\.should_receive\(
# expect\(\1\)\.to receive\(
perl -pi -w -e "s/(\S.*)\.should_receive\(/expect\(\1\)\.to receive\(/g;" $files
### ensure_length_of
# ensure_length_of(
# validate_length_of(
perl -pi -w -e "s/ensure_length_of/validate_length_of/g;" $files
### ensure_length_of
# ensure_length_of(
# validate_length_of(
perl -pi -w -e "s/ensure_inclusion_of/validate_inclusion_of/g;" $files
### .to ==
# expect(Device.organization_change_enabled?).to == false
# expect(Device.organization_change_enabled?).to be false
# \)\.to == false
# \)\.to be false
perl -pi -w -e "s/\)\.to == false/\)\.to be false/g;" $files
# expect(Device.organization_change_enabled?).to == true
# expect(Device.organization_change_enabled?).to be true
perl -pi -w -e "s/\)\.to == true/\)\.to be true/g;" $files
# nil
perl -pi -w -e "s/\)\.to == nil/\)\.to be nil/g;" $files
# ).to == {}
# .to eq({})
perl -pi -w -e "s/\)\.to == {}/\)\.to eq\({}\)/g;" $files
# ).to ==
# expect(no_events_profile.subscribed_event_settings).to == {}
perl -pi -w -e "s/\)\.to ==/\)\.to eq/g;" $files
# ).to_not ==
perl -pi -w -e "s/\)\.to_not ==/\)\.to_not eq/g;" $files
# ).not_to ==
perl -pi -w -e "s/\)\.not_to ==/\)\.not_to eq/g;" $files
# be_nil
perl -pi -w -e "s/be_nil/be nil/g;" $files
# be_true
perl -pi -w -e "s/be_true/be true/g;" $files
# be_false
perl -pi -w -e "s/be_false/be false/g;" $files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment