synvert 0.0.17 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/CHANGELOG.md +4 -0
- data/README.md +7 -26
- data/bin/synvert +0 -1
- data/lib/synvert/cli.rb +26 -14
- data/lib/synvert/snippet.rb +17 -0
- data/lib/synvert/version.rb +1 -1
- data/lib/synvert.rb +3 -11
- data/synvert.gemspec +2 -3
- metadata +6 -85
- data/lib/synvert/configuration.rb +0 -25
- data/lib/synvert/exceptions.rb +0 -13
- data/lib/synvert/node_ext.rb +0 -319
- data/lib/synvert/rewriter/action.rb +0 -224
- data/lib/synvert/rewriter/condition.rb +0 -56
- data/lib/synvert/rewriter/gem_spec.rb +0 -42
- data/lib/synvert/rewriter/instance.rb +0 -185
- data/lib/synvert/rewriter/scope.rb +0 -46
- data/lib/synvert/rewriter.rb +0 -200
- data/lib/synvert/snippets/check_syntax.rb +0 -5
- data/lib/synvert/snippets/factory_girl/syntax_methods.rb +0 -98
- data/lib/synvert/snippets/rails/convert_dynamic_finders.rb +0 -93
- data/lib/synvert/snippets/rails/strong_parameters.rb +0 -93
- data/lib/synvert/snippets/rails/upgrade_3_0_to_3_1.rb +0 -135
- data/lib/synvert/snippets/rails/upgrade_3_1_to_3_2.rb +0 -42
- data/lib/synvert/snippets/rails/upgrade_3_2_to_4_0.rb +0 -230
- data/lib/synvert/snippets/rspec/be_close_to_be_within.rb +0 -18
- data/lib/synvert/snippets/rspec/block_to_expect.rb +0 -22
- data/lib/synvert/snippets/rspec/boolean_matcher.rb +0 -20
- data/lib/synvert/snippets/rspec/collection_matcher.rb +0 -34
- data/lib/synvert/snippets/rspec/its_to_it.rb +0 -89
- data/lib/synvert/snippets/rspec/message_expectation.rb +0 -41
- data/lib/synvert/snippets/rspec/method_stub.rb +0 -84
- data/lib/synvert/snippets/rspec/negative_error_expectation.rb +0 -21
- data/lib/synvert/snippets/rspec/new_syntax.rb +0 -18
- data/lib/synvert/snippets/rspec/one_liner_expectation.rb +0 -71
- data/lib/synvert/snippets/rspec/should_to_expect.rb +0 -50
- data/lib/synvert/snippets/rspec/stub_and_mock_to_double.rb +0 -22
- data/lib/synvert/snippets/ruby/new_hash_syntax.rb +0 -21
- data/lib/synvert/snippets/ruby/new_lambda_syntax.rb +0 -20
- data/spec/spec_helper.rb +0 -26
- data/spec/support/parser_helper.rb +0 -5
- data/spec/synvert/node_ext_spec.rb +0 -201
- data/spec/synvert/rewriter/action_spec.rb +0 -225
- data/spec/synvert/rewriter/condition_spec.rb +0 -106
- data/spec/synvert/rewriter/gem_spec_spec.rb +0 -52
- data/spec/synvert/rewriter/instance_spec.rb +0 -163
- data/spec/synvert/rewriter/scope_spec.rb +0 -42
- data/spec/synvert/rewriter_spec.rb +0 -153
- data/spec/synvert/snippets/factory_girl/syntax_methods_spec.rb +0 -154
- data/spec/synvert/snippets/rails/convert_dynamic_finders_spec.rb +0 -83
- data/spec/synvert/snippets/rails/strong_parameters_spec.rb +0 -132
- data/spec/synvert/snippets/rails/upgrade_3_0_to_3_1_spec.rb +0 -88
- data/spec/synvert/snippets/rails/upgrade_3_1_to_3_2_spec.rb +0 -41
- data/spec/synvert/snippets/rails/upgrade_3_2_to_4_0_spec.rb +0 -299
- data/spec/synvert/snippets/rspec/new_syntax_spec.rb +0 -183
- data/spec/synvert/snippets/ruby/new_hash_syntax_spec.rb +0 -27
- data/spec/synvert/snippets/ruby/new_lambda_syntax_spec.rb +0 -27
@@ -1,89 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "convert_rspec_its_to_it" do
|
2
|
-
description <<-EOF
|
3
|
-
It converts rspec its to it.
|
4
|
-
|
5
|
-
describe 'example' do
|
6
|
-
subject { { foo: 1, bar: 2 } }
|
7
|
-
its(:size) { should == 2 }
|
8
|
-
its([:foo]) { should == 1 }
|
9
|
-
its('keys.first') { should == :foo }
|
10
|
-
end
|
11
|
-
=>
|
12
|
-
describe 'example' do
|
13
|
-
subject { { foo: 1, bar: 2 } }
|
14
|
-
|
15
|
-
describe '#size' do
|
16
|
-
subject { super().size }
|
17
|
-
it { should == 2 }
|
18
|
-
end
|
19
|
-
|
20
|
-
describe '[:foo]' do
|
21
|
-
subject { super()[:foo] }
|
22
|
-
it { should == 1 }
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '#keys' do
|
26
|
-
subject { super().keys }
|
27
|
-
describe '#first' do
|
28
|
-
subject { super().first }
|
29
|
-
it { should == :foo }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
EOF
|
34
|
-
|
35
|
-
if_gem 'rspec', {gte: '2.99.0'}
|
36
|
-
|
37
|
-
within_files 'spec/**/*.rb' do
|
38
|
-
# describe 'example' do
|
39
|
-
# subject { { foo: 1, bar: 2 } }
|
40
|
-
# its(:size) { should == 2 }
|
41
|
-
# its([:foo]) { should == 1 }
|
42
|
-
# its('keys.first') { should == :foo }
|
43
|
-
# end
|
44
|
-
# =>
|
45
|
-
# describe 'example' do
|
46
|
-
# subject { { foo: 1, bar: 2 } }
|
47
|
-
#
|
48
|
-
# describe '#size' do
|
49
|
-
# subject { super().size }
|
50
|
-
# it { should == 2 }
|
51
|
-
# end
|
52
|
-
#
|
53
|
-
# describe '[:foo]' do
|
54
|
-
# subject { super()[:foo] }
|
55
|
-
# it { should == 1 }
|
56
|
-
# end
|
57
|
-
#
|
58
|
-
# describe '#keys' do
|
59
|
-
# subject { super().keys }
|
60
|
-
# describe '#first' do
|
61
|
-
# subject { super().first }
|
62
|
-
# it { should == :foo }
|
63
|
-
# end
|
64
|
-
# end
|
65
|
-
# end
|
66
|
-
[:should, :should_not].each do |message|
|
67
|
-
with_node type: 'block', caller: {message: 'its'} do
|
68
|
-
if node.body.length == 1
|
69
|
-
its_arg = node.caller.arguments.first.source(self)
|
70
|
-
its_arg = its_arg[1...-1] if its_arg =~ /^['"].*['"]$/
|
71
|
-
its_arg = its_arg[1..-1] if its_arg[0] == ':'
|
72
|
-
rewritten_code = ""
|
73
|
-
args = its_arg.split(".")
|
74
|
-
args.each_with_index do |arg, index|
|
75
|
-
describe_name = arg[0] =~ /^[a-z]/ ? '#' + arg : arg
|
76
|
-
message_name = arg[0] =~ /^[a-z]/ ? '.' + arg : arg
|
77
|
-
rewritten_code << "#{' ' * index}describe '#{describe_name}' do\n"
|
78
|
-
rewritten_code << "#{' ' * (index + 1)}subject { super()#{message_name} }\n"
|
79
|
-
rewritten_code << "#{' ' * (index + 1)}it { {{body}} }\n" if index + 1 == args.length
|
80
|
-
end
|
81
|
-
args.length.times do |i|
|
82
|
-
rewritten_code << "#{' ' * (args.length - 1 - i)}end\n"
|
83
|
-
end
|
84
|
-
replace_with rewritten_code
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "convert_rspec_message_expectation" do
|
2
|
-
description <<-EOF
|
3
|
-
It convert rspec message expectation.
|
4
|
-
|
5
|
-
obj.should_receive(:message) => expect(obj).to receive(:message)
|
6
|
-
Klass.any_instance.should_receive(:message) => expect_any_instance_of(Klass).to receive(:message)
|
7
|
-
|
8
|
-
expect(obj).to receive(:message).and_return { 1 } => expect(obj).to receive(:message) { 1 }
|
9
|
-
|
10
|
-
expect(obj).to receive(:message).and_return { 1 } => expect(obj).to receive(:message) { 1 }
|
11
|
-
|
12
|
-
expect(obj).to receive(:message).and_return => expect(obj).to receive(:message)
|
13
|
-
EOF
|
14
|
-
|
15
|
-
if_gem 'rspec', {gte: '2.14.0'}
|
16
|
-
|
17
|
-
within_files 'spec/**/*.rb' do
|
18
|
-
# obj.should_receive(:message) => expect(obj).to receive(:message)
|
19
|
-
# Klass.any_instance.should_receive(:message) => expect_any_instance_of(Klass).to receive(:message)
|
20
|
-
with_node type: 'send', message: 'should_receive' do
|
21
|
-
if_exist_node type: 'send', message: 'any_instance' do
|
22
|
-
replace_with "expect_any_instance_of({{receiver.receiver}}).to receive({{arguments}})"
|
23
|
-
end
|
24
|
-
unless_exist_node type: 'send', message: 'any_instance' do
|
25
|
-
replace_with "expect({{receiver}}).to receive({{arguments}})"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
within_files 'spec/**/*.rb' do
|
31
|
-
# expect(obj).to receive(:message).and_return { 1 } => expect(obj).to receive(:message) { 1 }
|
32
|
-
with_node type: 'send', receiver: {type: 'send', message: 'expect'}, arguments: {first: {type: 'block', caller: {type: 'send', message: 'and_return', arguments: []}}} do
|
33
|
-
replace_with "{{receiver}}.to {{arguments.first.caller.receiver}} { {{arguments.first.body}} }"
|
34
|
-
end
|
35
|
-
|
36
|
-
# expect(obj).to receive(:message).and_return => expect(obj).to receive(:message)
|
37
|
-
with_node type: 'send', receiver: {type: 'send', message: 'expect'}, arguments: {first: {type: 'send', message: 'and_return', arguments: []}} do
|
38
|
-
replace_with "{{receiver}}.to {{arguments.first.receiver}}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,84 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "convert_rspec_method_stub" do
|
2
|
-
description <<-EOF
|
3
|
-
It converts rspec method stub.
|
4
|
-
|
5
|
-
obj.stub!(:message) => obj.stub(:message)
|
6
|
-
obj.unstub!(:message) => obj.unstub(:message)
|
7
|
-
|
8
|
-
obj.stub(:message).any_number_of_times => allow(obj).to receive(:message)
|
9
|
-
obj.stub(:message).at_least(0) => allow(obj).to receive(:message)
|
10
|
-
|
11
|
-
obj.stub(:message) => allow(obj).to receive(:message)
|
12
|
-
Klass.any_instance.stub(:message) => allow_any_instance_of(Klass).to receive(:message)
|
13
|
-
|
14
|
-
obj.stub_chain(:foo, :bar, :baz) => allow(obj).to receive_message_chain(:foo, :bar, :baz)
|
15
|
-
|
16
|
-
obj.stub(:foo => 1, :bar => 2) => allow(obj).to receive_messages(:foo => 1, :bar => 2)
|
17
|
-
|
18
|
-
allow(obj).to receive(:message).and_return { 1 } => allow(obj).to receive(:message) { 1 }
|
19
|
-
|
20
|
-
allow(obj).to receive(:message).and_return => allow(obj).to receive(:message)
|
21
|
-
EOF
|
22
|
-
|
23
|
-
if_gem 'rspec', {gte: '2.14.0'}
|
24
|
-
|
25
|
-
within_files 'spec/**/*.rb' do
|
26
|
-
# obj.stub!(:message) => obj.stub(:message)
|
27
|
-
# obj.unstub!(:message) => obj.unstub(:message)
|
28
|
-
{stub!: 'stub', unstub!: 'unstub'}.each do |old_message, new_message|
|
29
|
-
with_node type: 'send', message: old_message do
|
30
|
-
replace_with "{{receiver}}.#{new_message}({{arguments}})"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# obj.stub(:message).any_number_of_times => allow(obj).to receive(:message)
|
35
|
-
# obj.stub(:message).at_least(0) => allow(obj).to receive(:message)
|
36
|
-
with_node type: 'send', message: 'any_number_of_times' do
|
37
|
-
replace_with "{{receiver}}"
|
38
|
-
end
|
39
|
-
|
40
|
-
with_node type: 'send', message: 'at_least', arguments: [0] do
|
41
|
-
replace_with "{{receiver}}"
|
42
|
-
end
|
43
|
-
|
44
|
-
# obj.stub(:message) => allow(obj).to receive(:message)
|
45
|
-
# Klass.any_instance.stub(:message) => allow_any_instance_of(Klass).to receive(:message)
|
46
|
-
with_node type: 'send', message: 'stub', arguments: {first: {type: {not: 'hash'}}} do
|
47
|
-
if_exist_node type: 'send', message: 'any_instance' do
|
48
|
-
replace_with "allow_any_instance_of({{receiver.receiver}}).to receive({{arguments}})"
|
49
|
-
end
|
50
|
-
unless_exist_node type: 'send', message: 'any_instance' do
|
51
|
-
replace_with "allow({{receiver}}).to receive({{arguments}})"
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
if_gem 'rspec', {gte: '3.0.0'}
|
57
|
-
|
58
|
-
within_files 'spec/**/*.rb' do
|
59
|
-
# obj.stub_chain(:foo, :bar, :baz) => allow(obj).to receive_message_chain(:foo, :bar, :baz)
|
60
|
-
with_node type: 'send', message: 'stub_chain' do
|
61
|
-
if_exist_node type: 'send', message: 'any_instance' do
|
62
|
-
replace_with "allow_any_instance_of({{receiver.receiver}}).to receive_message_chain({{arguments}})"
|
63
|
-
end
|
64
|
-
unless_exist_node type: 'send', message: 'any_instance' do
|
65
|
-
replace_with "allow({{receiver}}).to receive_message_chain({{arguments}})"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
# obj.stub(:foo => 1, :bar => 2) => allow(obj).to receive_messages(:foo => 1, :bar => 2)
|
70
|
-
with_node type: 'send', message: 'stub', arguments: {first: {type: 'hash'}} do
|
71
|
-
replace_with "allow({{receiver}}).to receive_messages({{arguments}})"
|
72
|
-
end
|
73
|
-
|
74
|
-
# allow(obj).to receive(:message).and_return { 1 } => allow(obj).to receive(:message) { 1 }
|
75
|
-
with_node type: 'send', receiver: {type: 'send', message: 'allow'}, arguments: {first: {type: 'block', caller: {type: 'send', message: 'and_return', arguments: []}}} do
|
76
|
-
replace_with "{{receiver}}.to {{arguments.first.caller.receiver}} { {{arguments.first.body}} }"
|
77
|
-
end
|
78
|
-
|
79
|
-
# allow(obj).to receive(:message).and_return => allow(obj).to receive(:message)
|
80
|
-
with_node type: 'send', receiver: {type: 'send', message: 'allow'}, arguments: {first: {type: 'send', message: 'and_return', arguments: []}} do
|
81
|
-
replace_with "{{receiver}}.to {{arguments.first.receiver}}"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "convert_rspec_negative_error_expectation" do
|
2
|
-
description <<-EOF
|
3
|
-
It converts rspec negative error expectation.
|
4
|
-
|
5
|
-
expect { do_something }.not_to raise_error(SomeErrorClass) => expect { do_something }.not_to raise_error
|
6
|
-
expect { do_something }.not_to raise_error('message') => expect { do_something }.not_to raise_error
|
7
|
-
expect { do_something }.not_to raise_error(SomeErrorClass, 'message') => expect { do_something }.not_to raise_error
|
8
|
-
EOF
|
9
|
-
if_gem 'rspec', {gte: '2.14.0'}
|
10
|
-
|
11
|
-
within_files 'spec/**/*.rb' do
|
12
|
-
# expect { do_something }.not_to raise_error(SomeErrorClass) => expect { do_something }.not_to raise_error
|
13
|
-
# expect { do_something }.not_to raise_error('message') => expect { do_something }.not_to raise_error
|
14
|
-
# expect { do_something }.not_to raise_error(SomeErrorClass, 'message') => expect { do_something }.not_to raise_error
|
15
|
-
within_node type: 'send', receiver: {type: 'block'}, message: 'not_to' do
|
16
|
-
with_node type: 'send', message: 'raise_error' do
|
17
|
-
replace_with "raise_error"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "rspec_new_syntax" do
|
2
|
-
description <<-EOF
|
3
|
-
It converts rspec code to new syntax, it calls all convert_rspec_* snippets.
|
4
|
-
EOF
|
5
|
-
|
6
|
-
add_snippet "convert_rspec_should_to_expect"
|
7
|
-
add_snippet "convert_rspec_block_to_expect"
|
8
|
-
add_snippet "convert_rspec_one_liner_expectation"
|
9
|
-
add_snippet "convert_rspec_boolean_matcher"
|
10
|
-
add_snippet "convert_rspec_be_close_to_be_within"
|
11
|
-
add_snippet "convert_rspec_collection_matcher"
|
12
|
-
add_snippet "convert_rspec_negative_error_expectation"
|
13
|
-
add_snippet "convert_rspec_its_to_it"
|
14
|
-
|
15
|
-
add_snippet "convert_rspec_stub_and_mock_to_double"
|
16
|
-
add_snippet "convert_rspec_message_expectation"
|
17
|
-
add_snippet "convert_rspec_method_stub"
|
18
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "convert_rspec_one_liner_expectation" do
|
2
|
-
description <<-EOF
|
3
|
-
It convers rspec one liner expectation.
|
4
|
-
|
5
|
-
it { should matcher } => it { is_expected.to matcher }
|
6
|
-
it { should_not matcher } => it { is_expected.not_to matcher }
|
7
|
-
|
8
|
-
it { should have(3).items }
|
9
|
-
=>
|
10
|
-
it 'has 3 items' do
|
11
|
-
expect(subject.size).to eq(3)
|
12
|
-
end
|
13
|
-
|
14
|
-
it { should have_at_least(3).players }
|
15
|
-
=>
|
16
|
-
it 'has at least 3 players' do
|
17
|
-
expect(subject.players.size).to be >= 3
|
18
|
-
end
|
19
|
-
EOF
|
20
|
-
|
21
|
-
if_gem 'rspec', {gte: '2.99.0'}
|
22
|
-
|
23
|
-
matcher_converters = {have: 'eq', have_exactly: 'eq', have_at_least: 'be >=', have_at_most: 'be <='}
|
24
|
-
within_files 'spec/**/*.rb' do
|
25
|
-
{should: 'to', should_not: 'not_to'}.each do |old_message, new_message|
|
26
|
-
# it { should matcher } => it { is_expected.to matcher }
|
27
|
-
# it { should_not matcher } => it { is_expected.not_to matcher }
|
28
|
-
with_node type: 'block', caller: {message: 'it'} do
|
29
|
-
if_only_exist_node type: 'send', receiver: nil, message: old_message do
|
30
|
-
receiver = node.body.first.arguments.first.receiver
|
31
|
-
unless receiver && matcher_converters.include?(receiver.message)
|
32
|
-
matcher = node.body.first.arguments.first.source(self)
|
33
|
-
replace_with "it { is_expected.#{new_message} #{matcher} }"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# it { should have(3).items }
|
39
|
-
# =>
|
40
|
-
# it 'has 3 items' do
|
41
|
-
# expect(subject.size).to eq(3)
|
42
|
-
# end
|
43
|
-
#
|
44
|
-
# it { should have_at_least(3).players }
|
45
|
-
# =>
|
46
|
-
# it 'has at least 3 players' do
|
47
|
-
# expect(subject.players.size).to be >= 3
|
48
|
-
# end
|
49
|
-
matcher_converters.each do |old_matcher, new_matcher|
|
50
|
-
with_node type: 'block', caller: {message: 'it'} do
|
51
|
-
if_only_exist_node type: 'send', receiver: nil, message: old_message, arguments: {first: {type: 'send', receiver: {type: 'send', message: old_matcher}}} do
|
52
|
-
times = node.body.first.arguments.first.receiver.arguments.first.source(self)
|
53
|
-
items_name = node.body.first.arguments.first.message
|
54
|
-
new_code = ""
|
55
|
-
if :items == items_name
|
56
|
-
new_code << "it 'has #{times} items' do\n"
|
57
|
-
new_code << " expect(subject.size).#{new_message} #{new_matcher}(#{times})\n"
|
58
|
-
new_code << "end"
|
59
|
-
else
|
60
|
-
it_message = "#{old_matcher.to_s.sub('have', 'has').gsub('_', ' ')} #{times} #{items_name}"
|
61
|
-
new_code << "it '#{it_message}' do\n"
|
62
|
-
new_code << " expect(subject.#{items_name}.size).#{new_message} #{new_matcher} #{times}\n"
|
63
|
-
new_code << "end"
|
64
|
-
end
|
65
|
-
replace_with new_code
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "convert_rspec_should_to_expect" do
|
2
|
-
description <<-EOF
|
3
|
-
It converts rspec should to expect.
|
4
|
-
|
5
|
-
obj.should matcher => expect(obj).to matcher
|
6
|
-
obj.should_not matcher => expect(obj).not_to matcher
|
7
|
-
|
8
|
-
1.should == 1 => expect(1).to eq 1
|
9
|
-
1.should < 1 => expect(1).to be < 2
|
10
|
-
Integer.should === 1 => expect(Integer).to be === 1
|
11
|
-
|
12
|
-
'string'.should =~ /^str/ => expect('string').to match /^str/
|
13
|
-
[1, 2, 3].should =~ [2, 1, 3] => expect([1, 2, 3]).to match_array [2, 1, 3]
|
14
|
-
EOF
|
15
|
-
|
16
|
-
if_gem 'rspec', {gte: '2.11.0'}
|
17
|
-
|
18
|
-
within_files 'spec/**/*.rb' do
|
19
|
-
# obj.should matcher => expect(obj).to matcher
|
20
|
-
# obj.should_not matcher => expect(obj).not_to matcher
|
21
|
-
{should: 'to', should_not: 'not_to'}.each do |old_message, new_message|
|
22
|
-
with_node type: 'send', receiver: {type: {not: 'block'}}, message: old_message do
|
23
|
-
if node.receiver && node.arguments.size > 0
|
24
|
-
replace_with "expect({{receiver}}).#{new_message} {{arguments}}"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# 1.should == 1 => expect(1).to eq 1
|
29
|
-
# 1.should < 1 => expect(1).to be < 2
|
30
|
-
# Integer.should === 1 => expect(Integer).to be === 1
|
31
|
-
{'==' => 'eq', '<' => 'be <', '>' => 'be >', '<=' => 'be <=', '>=' => 'be >=', '===' => 'be ==='}.each do |old_matcher, new_matcher|
|
32
|
-
with_node type: 'send', receiver: {type: 'send', message: old_message}, message: old_matcher do
|
33
|
-
if node.receiver.receiver
|
34
|
-
replace_with "expect({{receiver.receiver}}).#{new_message} #{new_matcher} {{arguments}}"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# 'string'.should =~ /^str/ => expect('string').to match /^str/
|
40
|
-
# [1, 2, 3].should =~ [2, 1, 3] => expect([1, 2, 3]).to match_array [2, 1, 3]
|
41
|
-
with_node type: 'send', receiver: {type: 'send', message: old_message}, message: '=~' do
|
42
|
-
if :regexp == node.arguments.first.type
|
43
|
-
replace_with "expect({{receiver.receiver}}).#{new_message} match {{arguments}}"
|
44
|
-
else
|
45
|
-
replace_with "expect({{receiver.receiver}}).#{new_message} match_array {{arguments}}"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "convert_rspec_stub_and_mock_to_double" do
|
2
|
-
description <<-EOF
|
3
|
-
It converts stub and mock to double.
|
4
|
-
|
5
|
-
stub('something') => double('something')
|
6
|
-
mock('something') => double('something')
|
7
|
-
EOF
|
8
|
-
|
9
|
-
if_gem 'rspec', {gte: '2.14.0'}
|
10
|
-
|
11
|
-
within_files 'spec/**/*.rb' do
|
12
|
-
# stub('something') => double('something')
|
13
|
-
# mock('something') => double('something')
|
14
|
-
with_node type: 'send', receiver: nil, message: 'stub' do
|
15
|
-
replace_with "double({{arguments}})"
|
16
|
-
end
|
17
|
-
|
18
|
-
with_node type: 'send', receiver: nil, message: 'mock' do
|
19
|
-
replace_with "double({{arguments}})"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "ruby_new_hash_syntax" do
|
2
|
-
description <<-EOF
|
3
|
-
Use ruby new hash syntax.
|
4
|
-
|
5
|
-
{:foo => 'bar'} => {foo: 'bar'}
|
6
|
-
EOF
|
7
|
-
|
8
|
-
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9.0")
|
9
|
-
within_files '**/*.rb' do
|
10
|
-
# {:foo => 'bar'} => {foo: 'bar'}
|
11
|
-
within_node type: 'hash' do
|
12
|
-
with_node type: 'pair' do
|
13
|
-
if :sym == node.key.type
|
14
|
-
new_key = node.key.source(self)[1..-1]
|
15
|
-
replace_with "#{new_key}: {{value}}"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
Synvert::Rewriter.new "ruby_new_lambda_syntax" do
|
2
|
-
description <<-EOF
|
3
|
-
Use ruby new lambda syntax
|
4
|
-
|
5
|
-
lambda { # do some thing } => -> { # do some thing }
|
6
|
-
EOF
|
7
|
-
|
8
|
-
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9.0")
|
9
|
-
within_files '**/*.rb' do
|
10
|
-
# lambda { |a, b, c| a + b + c } => ->(a, b, c) { a + b + c }
|
11
|
-
within_node type: 'block', caller: {type: 'send', message: 'lambda'} do
|
12
|
-
if node.arguments.empty?
|
13
|
-
replace_with "-> { {{body}} }"
|
14
|
-
else
|
15
|
-
replace_with "->({{arguments}}) { {{body}} }"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
2
|
-
|
3
|
-
require 'synvert'
|
4
|
-
require 'fakefs/spec_helpers'
|
5
|
-
|
6
|
-
require 'coveralls'
|
7
|
-
Coveralls.wear!
|
8
|
-
|
9
|
-
Dir[File.join(File.dirname(__FILE__), 'support', '*')].each do |path|
|
10
|
-
require path
|
11
|
-
end
|
12
|
-
|
13
|
-
RSpec.configure do |config|
|
14
|
-
config.include ParserHelper
|
15
|
-
config.include FakeFS::SpecHelpers, fakefs: true
|
16
|
-
|
17
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
18
|
-
config.run_all_when_everything_filtered = true
|
19
|
-
config.filter_run :focus
|
20
|
-
|
21
|
-
config.order = 'random'
|
22
|
-
|
23
|
-
config.before do
|
24
|
-
Synvert::Configuration.instance.set :skip_files, []
|
25
|
-
end
|
26
|
-
end
|
@@ -1,201 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Parser::AST::Node do
|
4
|
-
describe '#name' do
|
5
|
-
it 'gets for class node' do
|
6
|
-
node = parse('class Synvert; end')
|
7
|
-
expect(node.name).to eq parse('Synvert')
|
8
|
-
|
9
|
-
node = parse('class Synvert::Rewriter::Instance; end')
|
10
|
-
expect(node.name).to eq parse('Synvert::Rewriter::Instance')
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'gets for module node' do
|
14
|
-
node = parse('module Synvert; end')
|
15
|
-
expect(node.name).to eq parse('Synvert')
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'gets for def node' do
|
19
|
-
node = parse('def current_node; end')
|
20
|
-
expect(node.name).to eq :current_node
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'gets for defs node' do
|
24
|
-
node = parse('def self.current_node; end')
|
25
|
-
expect(node.name).to eq :current_node
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
describe '#receiver' do
|
30
|
-
it 'gets for send node' do
|
31
|
-
node = parse('FactoryGirl.create :post')
|
32
|
-
expect(node.receiver).to eq parse('FactoryGirl')
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
describe '#message' do
|
37
|
-
it 'gets for send node' do
|
38
|
-
node = parse('FactoryGirl.create :post')
|
39
|
-
expect(node.message).to eq :create
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe '#arguments' do
|
44
|
-
it 'gets for send node' do
|
45
|
-
node = parse("FactoryGirl.create :post, title: 'post'")
|
46
|
-
expect(node.arguments).to eq parse("[:post, title: 'post']").children
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'gets for block node' do
|
50
|
-
source = 'RSpec.configure do |config|; end'
|
51
|
-
node = parse(source)
|
52
|
-
instance = double(current_source: source)
|
53
|
-
expect(node.arguments.map { |argument| argument.source(instance) }).to eq ['config']
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'gets for defined? node' do
|
57
|
-
node = parse('defined?(Bundler)')
|
58
|
-
expect(node.arguments).to eq [parse('Bundler')]
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
describe '#caller' do
|
63
|
-
it 'gets for block node' do
|
64
|
-
node = parse('RSpec.configure do |config|; end')
|
65
|
-
expect(node.caller).to eq parse('RSpec.configure')
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe '#body' do
|
70
|
-
it 'gets one line for block node' do
|
71
|
-
node = parse('RSpec.configure do |config|; include EmailSpec::Helpers; end')
|
72
|
-
expect(node.body).to eq [parse('include EmailSpec::Helpers')]
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'gets multiple lines for block node' do
|
76
|
-
node = parse('RSpec.configure do |config|; include EmailSpec::Helpers; include EmailSpec::Matchers; end')
|
77
|
-
expect(node.body).to eq [parse('include EmailSpec::Helpers'), parse('include EmailSpec::Matchers')]
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'gets for begin node' do
|
81
|
-
node = parse('foo; bar')
|
82
|
-
expect(node.body).to eq [parse('foo'), parse('bar')]
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
describe "#keys" do
|
87
|
-
it 'gets for hash node' do
|
88
|
-
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
89
|
-
expect(node.keys).to eq [Parser::CurrentRuby.parse(':foo'), Parser::CurrentRuby.parse("'foo'")]
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
describe "#values" do
|
94
|
-
it 'gets for hash node' do
|
95
|
-
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
96
|
-
expect(node.values).to eq [Parser::CurrentRuby.parse(':bar'), Parser::CurrentRuby.parse("'bar'")]
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
describe "#key" do
|
101
|
-
it 'gets for pair node' do
|
102
|
-
node = parse("{:foo => 'bar'}").children[0]
|
103
|
-
expect(node.key).to eq Parser::CurrentRuby.parse(':foo')
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
describe "#value" do
|
108
|
-
it 'gets for hash node' do
|
109
|
-
node = parse("{:foo => 'bar'}").children[0]
|
110
|
-
expect(node.value).to eq Parser::CurrentRuby.parse("'bar'")
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
describe "#condition" do
|
115
|
-
it 'gets for if node' do
|
116
|
-
node = parse('if defined?(Bundler); end')
|
117
|
-
expect(node.condition).to eq parse('defined?(Bundler)')
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
describe '#source' do
|
122
|
-
it 'gets for node' do
|
123
|
-
source = 'params[:user][:email]'
|
124
|
-
instance = double(current_source: source)
|
125
|
-
node = parse(source)
|
126
|
-
expect(node.source(instance)).to eq 'params[:user][:email]'
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
describe '#indent' do
|
131
|
-
it 'gets column number' do
|
132
|
-
node = parse(' FactoryGirl.create :post')
|
133
|
-
expect(node.indent).to eq 2
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
describe '#recursive_children' do
|
138
|
-
it 'iterates all children recursively' do
|
139
|
-
node = parse('class Synvert; def current_node; @node; end; end')
|
140
|
-
children = []
|
141
|
-
node.recursive_children { |child| children << child.type }
|
142
|
-
expect(children).to be_include :const
|
143
|
-
expect(children).to be_include :def
|
144
|
-
expect(children).to be_include :args
|
145
|
-
expect(children).to be_include :ivar
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
describe '#match?' do
|
150
|
-
let(:instance) { Synvert::Rewriter::Instance.new('file pattern') }
|
151
|
-
|
152
|
-
it 'matches class name' do
|
153
|
-
source = 'class Synvert; end'
|
154
|
-
instance.current_source = source
|
155
|
-
node = parse(source)
|
156
|
-
expect(node).to be_match(instance, type: 'class', name: 'Synvert')
|
157
|
-
end
|
158
|
-
|
159
|
-
it 'matches message with regexp' do
|
160
|
-
source = 'User.find_by_login(login)'
|
161
|
-
instance.current_source = source
|
162
|
-
node = parse(source)
|
163
|
-
expect(node).to be_match(instance, type: 'send', message: /^find_by_/)
|
164
|
-
end
|
165
|
-
|
166
|
-
it 'matches arguments with symbol' do
|
167
|
-
source = 'params[:user]'
|
168
|
-
instance.current_source = source
|
169
|
-
node = parse(source)
|
170
|
-
expect(node).to be_match(instance, type: 'send', receiver: 'params', message: '[]', arguments: [:user])
|
171
|
-
end
|
172
|
-
|
173
|
-
it 'matches assign number' do
|
174
|
-
source = 'at_least(0)'
|
175
|
-
instance.current_source = source
|
176
|
-
node = parse(source)
|
177
|
-
expect(node).to be_match(instance, type: 'send', arguments: [0])
|
178
|
-
end
|
179
|
-
|
180
|
-
it 'matches arguments with string' do
|
181
|
-
source = 'params["user"]'
|
182
|
-
instance.current_source = source
|
183
|
-
node = parse(source)
|
184
|
-
expect(node).to be_match(instance, type: 'send', receiver: 'params', message: '[]', arguments: ['user'])
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'matches arguments any' do
|
188
|
-
source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
|
189
|
-
instance.current_source = source
|
190
|
-
node = parse(source)
|
191
|
-
expect(node).to be_match(instance, type: 'send', arguments: {any: 'Lifo::Cache'})
|
192
|
-
end
|
193
|
-
|
194
|
-
it 'matches not' do
|
195
|
-
source = 'class Synvert; end'
|
196
|
-
instance.current_source = source
|
197
|
-
node = parse(source)
|
198
|
-
expect(node).not_to be_match(instance, type: 'class', name: {not: 'Synvert'})
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|