synvert 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/lib/synvert/rewriter/instance.rb +36 -20
- data/lib/synvert/snippets/rails/convert_dynamic_finders.rb +7 -13
- data/lib/synvert/snippets/rails/strong_parameters.rb +4 -5
- data/lib/synvert/snippets/rails/upgrade_3_0_to_3_1.rb +9 -24
- data/lib/synvert/snippets/rails/upgrade_3_2_to_4_0.rb +1 -1
- data/lib/synvert/snippets/rspec/block_to_expect.rb +5 -5
- data/lib/synvert/snippets/rspec/boolean_matcher.rb +4 -4
- data/lib/synvert/snippets/rspec/collection_matcher.rb +8 -8
- data/lib/synvert/snippets/rspec/its_to_it.rb +30 -30
- data/lib/synvert/snippets/rspec/method_stub.rb +0 -8
- data/lib/synvert/snippets/rspec/one_liner_expectation.rb +35 -34
- data/lib/synvert/snippets/rspec/should_to_expect.rb +8 -12
- data/lib/synvert/version.rb +1 -1
- data/spec/synvert/rewriter/gem_spec_spec.rb +1 -1
- data/spec/synvert/snippets/rails/convert_dynamic_finders_spec.rb +24 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 904cb80d27d9364b9521a6002db88319440d6cd0
|
4
|
+
data.tar.gz: b065e2a1aa186550d5051a1805aed0b0b36770bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8470c708ef1bd0d97a360c3cfa7c49576311ae8a0f7197b3da69fe49a742c54d3b49e3b79784a9543c4cf5b7f2d649339bc24d885fb7f50f1ced7bdab628112d
|
7
|
+
data.tar.gz: 30f9727f898c103b693d46754f6e8c9317d35a98e3fed440ab7e5a8f4077b21fee2313b5670a999e9c7152333398f7e1b34e37009cdacd2a38a58a57dff1ff94
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -43,7 +43,7 @@ $ synvert --run-snippets factory_girl_short_syntax,upgrade_rails_3_2_to_4_0 ~/Si
|
|
43
43
|
name | description
|
44
44
|
--- | ---
|
45
45
|
factory_girl_short_syntax | FactoryGirl uses short syntax
|
46
|
-
|
46
|
+
convert_rails_dynamic_finders | Convert rails dynamic finders
|
47
47
|
strong_parameters | Use strong_parameters syntax
|
48
48
|
upgrade_rails_3_0_to_3_1 | Upgrade rails from 3.0 to 3.1
|
49
49
|
upgrade_rails_3_1_to_3_2 | Upgrade rails from 3.1 to 3.2
|
@@ -14,26 +14,30 @@ module Synvert
|
|
14
14
|
parser = Parser::CurrentRuby.new
|
15
15
|
file_pattern = File.join(Configuration.instance.get(:path), @file_pattern)
|
16
16
|
Dir.glob(file_pattern).each do |file_path|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
17
|
+
begin
|
18
|
+
source = File.read(file_path)
|
19
|
+
buffer = Parser::Source::Buffer.new file_path
|
20
|
+
buffer.source = source
|
21
|
+
|
22
|
+
parser.reset
|
23
|
+
ast = parser.parse buffer
|
24
|
+
|
25
|
+
@current_file = file_path
|
26
|
+
@current_source = source
|
27
|
+
@current_node = ast
|
28
|
+
instance_eval &@block
|
29
|
+
@current_node = ast
|
30
|
+
|
31
|
+
@actions.sort!
|
32
|
+
check_conflict_actions
|
33
|
+
@actions.reverse.each do |action|
|
34
|
+
source[action.begin_pos...action.end_pos] = action.rewritten_code
|
35
|
+
source = remove_code_or_whole_line(source, action.line)
|
36
|
+
end
|
37
|
+
@actions = []
|
38
|
+
|
39
|
+
File.write file_path, source
|
40
|
+
end while !@conflict_actions.empty?
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
@@ -81,6 +85,18 @@ module Synvert
|
|
81
85
|
|
82
86
|
private
|
83
87
|
|
88
|
+
def check_conflict_actions
|
89
|
+
i = @actions.length - 1
|
90
|
+
@conflict_actions = []
|
91
|
+
while i > 0
|
92
|
+
if @actions[i].begin_pos <= @actions[i - 1].end_pos
|
93
|
+
@conflict_actions << @actions.delete_at(i)
|
94
|
+
end
|
95
|
+
i -= 1
|
96
|
+
end
|
97
|
+
@conflict_actions
|
98
|
+
end
|
99
|
+
|
84
100
|
def remove_code_or_whole_line(source, line)
|
85
101
|
newline_at_end_of_line = source[-1] == "\n"
|
86
102
|
source_arr = source.split("\n")
|
@@ -1,4 +1,4 @@
|
|
1
|
-
Synvert::Rewriter.new "
|
1
|
+
Synvert::Rewriter.new "convert_rails_dynamic_finders", "Convert rails dynamic finders" do
|
2
2
|
helper_method 'dynamic_finder_to_hash' do |prefix|
|
3
3
|
fields = node.message.to_s[prefix.length..-1].split("_and_")
|
4
4
|
fields.length.times.map { |i|
|
@@ -12,41 +12,35 @@ Synvert::Rewriter.new "convert_dynamic_finders", "Convert dynamic finders" do
|
|
12
12
|
hash_params = dynamic_finder_to_hash("find_all_by_")
|
13
13
|
replace_with "{{receiver}}.where(#{hash_params})"
|
14
14
|
end
|
15
|
-
end
|
16
15
|
|
17
|
-
within_files '**/*.rb' do
|
18
16
|
# find_by_... => where(...).first
|
19
17
|
with_node type: 'send', message: /find_by_/ do
|
20
|
-
|
21
|
-
|
18
|
+
if :find_by_id == node.message
|
19
|
+
replace_with "{{receiver}}.find({{arguments}})"
|
20
|
+
elsif :find_by_sql != node.message
|
21
|
+
hash_params = dynamic_finder_to_hash("find_by_")
|
22
|
+
replace_with "{{receiver}}.where(#{hash_params}).first"
|
23
|
+
end
|
22
24
|
end
|
23
|
-
end
|
24
25
|
|
25
|
-
within_files '**/*.rb' do
|
26
26
|
# find_last_by_... => where(...).last
|
27
27
|
with_node type: 'send', message: /find_last_by_/ do
|
28
28
|
hash_params = dynamic_finder_to_hash("find_last_by_")
|
29
29
|
replace_with "{{receiver}}.where(#{hash_params}).last"
|
30
30
|
end
|
31
|
-
end
|
32
31
|
|
33
|
-
within_files '**/*.rb' do
|
34
32
|
# scoped_by_... => where(...)
|
35
33
|
with_node type: 'send', message: /scoped_by_/ do
|
36
34
|
hash_params = dynamic_finder_to_hash("scoped_by_")
|
37
35
|
replace_with "{{receiver}}.where(#{hash_params})"
|
38
36
|
end
|
39
|
-
end
|
40
37
|
|
41
|
-
within_files '**/*.rb' do
|
42
38
|
# find_or_initialize_by_... => find_or_initialize_by(...)
|
43
39
|
with_node type: 'send', message: /find_or_initialize_by_/ do
|
44
40
|
hash_params = dynamic_finder_to_hash("find_or_initialize_by_")
|
45
41
|
replace_with "{{receiver}}.find_or_initialize_by(#{hash_params})"
|
46
42
|
end
|
47
|
-
end
|
48
43
|
|
49
|
-
within_files '**/*.rb' do
|
50
44
|
# find_or_create_by_... => find_or_create_by(...)
|
51
45
|
with_node type: 'send', message: /find_or_create_by_/ do
|
52
46
|
hash_params = dynamic_finder_to_hash("find_or_create_by_")
|
@@ -4,9 +4,7 @@ Synvert::Rewriter.new "strong_parameters", "Use strong_parameters syntax" do
|
|
4
4
|
with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'active_record'}, message: 'whitelist_attributes=' do
|
5
5
|
remove
|
6
6
|
end
|
7
|
-
end
|
8
7
|
|
9
|
-
within_files 'config/**/*.rb' do
|
10
8
|
# remove config.active_record.mass_assignment_sanitizer = ...
|
11
9
|
with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'active_record'}, message: 'mass_assignment_sanitizer=' do
|
12
10
|
remove
|
@@ -32,9 +30,10 @@ Synvert::Rewriter.new "strong_parameters", "Use strong_parameters syntax" do
|
|
32
30
|
if parameters[object_name]
|
33
31
|
# append def xxx_params; ...; end
|
34
32
|
unless_exist_node type: 'def', name: "#{object_name}_params" do
|
35
|
-
|
36
|
-
params.require(:#{object_name}).permit(#{parameters[object_name]})
|
37
|
-
end"
|
33
|
+
new_code = "def #{object_name}_params\n"
|
34
|
+
new_code << " params.require(:#{object_name}).permit(#{parameters[object_name]})\n"
|
35
|
+
new_code << "end"
|
36
|
+
append new_code
|
38
37
|
end
|
39
38
|
|
40
39
|
# params[:xxx] => xxx_params
|
@@ -6,16 +6,12 @@ Synvert::Rewriter.new 'upgrade_rails_3_0_to_3_1', 'Upgrade rails from 3.0 to 3.1
|
|
6
6
|
unless_exist_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'assets'}, message: 'version=' do
|
7
7
|
insert "config.assets.version = '1.0'"
|
8
8
|
end
|
9
|
-
end
|
10
9
|
|
11
|
-
within_file 'config/application.rb' do
|
12
10
|
# insert config.assets.enabled = true
|
13
11
|
unless_exist_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'assets'}, message: 'enabled=' do
|
14
12
|
insert 'config.assets.enabled = true'
|
15
13
|
end
|
16
|
-
end
|
17
14
|
|
18
|
-
within_file 'config/application.rb' do
|
19
15
|
# config.assets.prefix = '/assets' => config.assets.prefix = '/asset-files'
|
20
16
|
with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'assets'}, message: 'prefix=', arguments: ['/assets'] do
|
21
17
|
replace_with "config.assets.prefix = '/asset-files'"
|
@@ -27,16 +23,12 @@ Synvert::Rewriter.new 'upgrade_rails_3_0_to_3_1', 'Upgrade rails from 3.0 to 3.1
|
|
27
23
|
with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'action_view'}, message: 'debug_rjs=' do
|
28
24
|
remove
|
29
25
|
end
|
30
|
-
end
|
31
26
|
|
32
|
-
within_file 'config/environments/development.rb' do
|
33
27
|
# insert config.assets.debug = true
|
34
28
|
unless_exist_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'assets'}, message: 'debug=' do
|
35
29
|
insert "config.assets.debug = true"
|
36
30
|
end
|
37
|
-
end
|
38
31
|
|
39
|
-
within_file 'config/environments/development.rb' do
|
40
32
|
# insert config.assets.compress = false
|
41
33
|
unless_exist_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'assets'}, message: 'compress=' do
|
42
34
|
insert "config.assets.compress = false"
|
@@ -48,16 +40,12 @@ Synvert::Rewriter.new 'upgrade_rails_3_0_to_3_1', 'Upgrade rails from 3.0 to 3.1
|
|
48
40
|
unless_exist_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'assets'}, message: 'digest=' do
|
49
41
|
insert "config.assets.digest = true"
|
50
42
|
end
|
51
|
-
end
|
52
43
|
|
53
|
-
within_file 'config/environments/production.rb' do
|
54
44
|
# insert config.assets.compile = false
|
55
45
|
unless_exist_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'assets'}, message: 'compile=' do
|
56
46
|
insert "config.assets.compile = false"
|
57
47
|
end
|
58
|
-
end
|
59
48
|
|
60
|
-
within_file 'config/environments/production.rb' do
|
61
49
|
# insert config.assets.compress = true
|
62
50
|
unless_exist_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'assets'}, message: 'compress=' do
|
63
51
|
insert "config.assets.compress = true"
|
@@ -66,27 +54,24 @@ Synvert::Rewriter.new 'upgrade_rails_3_0_to_3_1', 'Upgrade rails from 3.0 to 3.1
|
|
66
54
|
|
67
55
|
within_file 'config/environments/test.rb' do
|
68
56
|
# insert config.static_cache_control = "public, max-age=3600"
|
69
|
-
unless_exist_node type: 'send', receiver: {type: 'send', message: 'config'}, message: '
|
57
|
+
unless_exist_node type: 'send', receiver: {type: 'send', message: 'config'}, message: 'static_cache_control=' do
|
70
58
|
insert 'config.static_cache_control = "public, max-age=3600"'
|
71
59
|
end
|
72
|
-
end
|
73
60
|
|
74
|
-
within_file 'config/environments/test.rb' do
|
75
61
|
# insert config.serve_static_assets = true
|
76
62
|
unless_exist_node type: 'send', receiver: {type: 'send', message: 'config'}, message: 'serve_static_assets=' do
|
77
63
|
insert "config.serve_static_assets = true"
|
78
64
|
end
|
79
65
|
end
|
80
66
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
""".strip
|
67
|
+
new_code = "ActiveSupport.on_load(:action_controller) do\n"
|
68
|
+
new_code << " wrap_parameters format: [:json]\n"
|
69
|
+
new_code << "end\n"
|
70
|
+
new_code << "\n"
|
71
|
+
new_code << "ActiveSupport.on_load(:active_record) do\n"
|
72
|
+
new_code << " self.include_root_in_json = false\n"
|
73
|
+
new_code << "end"
|
74
|
+
add_file 'config/initializers/wrap_parameters.rb', new_code
|
90
75
|
|
91
76
|
within_file 'config/initializers/session_store.rb' do
|
92
77
|
with_node type: 'send', receiver: {type: 'send', message: 'config'}, message: 'session_store', arguments: {first: :cookie_store} do
|
@@ -1,11 +1,11 @@
|
|
1
1
|
Synvert::Rewriter.new "convert_rspec_block_to_expect", "RSpec converts block to expect" do
|
2
2
|
if_gem 'rspec', {gte: '2.11.0'}
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
within_files 'spec/**/*.rb' do
|
5
|
+
# lambda { do_something }.should raise_error => expect { do_something }.to raise_error
|
6
|
+
# proc { do_something }.should raise_error => expect { do_something }.to raise_error
|
7
|
+
# -> { do_something }.should raise_error => expect { do_something }.to raise_error
|
8
|
+
{should: 'to', should_not: 'not_to'}.each do |old_message, new_message|
|
9
9
|
with_node type: 'send', receiver: {type: 'block'}, message: old_message do
|
10
10
|
replace_with "expect { {{receiver.body}} }.#{new_message} {{arguments}}"
|
11
11
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
Synvert::Rewriter.new "convert_rspec_boolean_matcher", "RSpec converts boolean matcher" do
|
2
2
|
if_gem 'rspec', {gte: '2.99.0'}
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
within_files 'spec/**/*_spec.rb' do
|
5
|
+
# be_true => be_truthy
|
6
|
+
# be_false => be_falsey
|
7
|
+
{be_true: 'be_truthy', be_false: 'be_falsey'}.each do |old_matcher, new_matcher|
|
8
8
|
with_node type: 'send', receiver: nil, message: old_matcher do
|
9
9
|
replace_with new_matcher
|
10
10
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
Synvert::Rewriter.new "convert_rspec_collection_matcher", "RSpec converts collection matcher" do
|
2
2
|
if_gem 'rspec', {gte: '2.11.0'}
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
within_files 'spec/**/*_spec.rb' do
|
5
|
+
# expect(collection).to have(3).items => expect(collection.size).to eq(3)
|
6
|
+
# expect(collection).to have_exactly(3).items => expect(collection.size).to eq(3)
|
7
|
+
# expect(collection).to have_at_least(3).items => expect(collection.size).to be >= 3
|
8
|
+
# expect(collection).to have_at_most(3).items => expect(collection.size).to be <= 3
|
9
|
+
#
|
10
|
+
# expect(team).to have(3).players => expect(team.players.size).to eq 3
|
11
|
+
{have: 'eq', have_exactly: 'eq', have_at_least: 'be >=', have_at_most: 'be <='}.each do |old_matcher, new_matcher|
|
12
12
|
with_node type: 'send', message: 'to', arguments: {first: {type: 'send', receiver: {type: 'send', message: old_matcher}}} do
|
13
13
|
times = node.arguments.first.receiver.arguments.first.source(self)
|
14
14
|
items_name = node.arguments.first.message
|
@@ -1,36 +1,36 @@
|
|
1
1
|
Synvert::Rewriter.new "convert_rspec_its_to_it", "RSpec converts its to it" do
|
2
2
|
if_gem 'rspec', {gte: '2.99.0'}
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
4
|
+
within_files 'spec/**/*.rb' do
|
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
|
+
[:should, :should_not].each do |message|
|
34
34
|
with_node type: 'block', caller: {message: 'its'} do
|
35
35
|
if node.body.length == 1
|
36
36
|
its_arg = node.caller.arguments.first.source(self)
|
@@ -9,9 +9,7 @@ Synvert::Rewriter.new "convert_rspec_method_stub", "RSpec converts method stub"
|
|
9
9
|
replace_with "{{receiver}}.#{new_message}({{arguments}})"
|
10
10
|
end
|
11
11
|
end
|
12
|
-
end
|
13
12
|
|
14
|
-
within_files 'spec/**/*.rb' do
|
15
13
|
# obj.stub(:message).any_number_of_times => allow(obj).to receive(:message)
|
16
14
|
# obj.stub(:message).at_least(0) => allow(obj).to receive(:message)
|
17
15
|
with_node type: 'send', message: 'any_number_of_times' do
|
@@ -21,9 +19,7 @@ Synvert::Rewriter.new "convert_rspec_method_stub", "RSpec converts method stub"
|
|
21
19
|
with_node type: 'send', message: 'at_least', arguments: [0] do
|
22
20
|
replace_with "{{receiver}}"
|
23
21
|
end
|
24
|
-
end
|
25
22
|
|
26
|
-
within_files 'spec/**/*.rb' do
|
27
23
|
# obj.stub(:message) => allow(obj).to receive(:message)
|
28
24
|
# Klass.any_instance.stub(:message) => allow_any_instance_of(Klass).to receive(:message)
|
29
25
|
with_node type: 'send', message: 'stub', arguments: {first: {type: {not: 'hash'}}} do
|
@@ -48,16 +44,12 @@ Synvert::Rewriter.new "convert_rspec_method_stub", "RSpec converts method stub"
|
|
48
44
|
replace_with "allow({{receiver}}).to receive_message_chain({{arguments}})"
|
49
45
|
end
|
50
46
|
end
|
51
|
-
end
|
52
47
|
|
53
|
-
within_files 'spec/**/*.rb' do
|
54
48
|
# obj.stub(:foo => 1, :bar => 2) => allow(obj).to receive_messages(:foo => 1, :bar => 2)
|
55
49
|
with_node type: 'send', message: 'stub', arguments: {first: {type: 'hash'}} do
|
56
50
|
replace_with "allow({{receiver}}).to receive_messages({{arguments}})"
|
57
51
|
end
|
58
|
-
end
|
59
52
|
|
60
|
-
within_files 'spec/**/*.rb' do
|
61
53
|
# allow(obj).to receive(:message).and_return { 1 } => allow(obj).to receive(:message) { 1 }
|
62
54
|
with_node type: 'send', receiver: {type: 'send', message: 'allow'}, arguments: {first: {type: 'block', caller: {type: 'send', message: 'and_return', arguments: []}}} do
|
63
55
|
replace_with "{{receiver}}.to {{arguments.first.caller.receiver}} { {{arguments.first.body}} }"
|
@@ -1,51 +1,52 @@
|
|
1
1
|
Synvert::Rewriter.new "convert_rspec_one_liner_expectation", "RSpec converts one liner expectation" do
|
2
2
|
if_gem 'rspec', {gte: '2.99.0'}
|
3
3
|
|
4
|
-
{
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
4
|
+
matcher_converters = {have: 'eq', have_exactly: 'eq', have_at_least: 'be >=', have_at_most: 'be <='}
|
5
|
+
within_files 'spec/**/*.rb' do
|
6
|
+
{should: 'to', should_not: 'not_to'}.each do |old_message, new_message|
|
7
|
+
# it { should matcher } => it { is_expected.to matcher }
|
8
|
+
# it { should_not matcher } => it { is_expected.not_to matcher }
|
9
|
+
with_node type: 'block', caller: {message: 'it'} do
|
10
|
+
if_only_exist_node type: 'send', receiver: nil, message: old_message do
|
11
|
+
receiver = node.body.first.arguments.first.receiver
|
12
|
+
unless receiver && matcher_converters.include?(receiver.message)
|
13
|
+
matcher = node.body.first.arguments.first.source(self)
|
14
|
+
replace_with "it { is_expected.#{new_message} #{matcher} }"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# it { should have(3).items }
|
20
|
+
# =>
|
21
|
+
# it 'has 3 items' do
|
22
|
+
# expect(subject.size).to eq(3)
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# it { should have_at_least(3).players }
|
26
|
+
# =>
|
27
|
+
# it 'has at least 3 players' do
|
28
|
+
# expect(subject.players.size).to be >= 3
|
29
|
+
# end
|
30
|
+
matcher_converters.each do |old_matcher, new_matcher|
|
19
31
|
with_node type: 'block', caller: {message: 'it'} do
|
20
32
|
if_only_exist_node type: 'send', receiver: nil, message: old_message, arguments: {first: {type: 'send', receiver: {type: 'send', message: old_matcher}}} do
|
21
33
|
times = node.body.first.arguments.first.receiver.arguments.first.source(self)
|
22
34
|
items_name = node.body.first.arguments.first.message
|
35
|
+
new_code = ""
|
23
36
|
if :items == items_name
|
24
|
-
|
25
|
-
expect(subject.size).#{new_message} #{new_matcher}(#{times})
|
26
|
-
end"
|
37
|
+
new_code << "it 'has #{times} items' do\n"
|
38
|
+
new_code << " expect(subject.size).#{new_message} #{new_matcher}(#{times})\n"
|
39
|
+
new_code << "end"
|
27
40
|
else
|
28
41
|
it_message = "#{old_matcher.to_s.sub('have', 'has').gsub('_', ' ')} #{times} #{items_name}"
|
29
|
-
|
30
|
-
expect(subject.#{items_name}.size).#{new_message} #{new_matcher} #{times}
|
31
|
-
end"
|
42
|
+
new_code << "it '#{it_message}' do\n"
|
43
|
+
new_code << " expect(subject.#{items_name}.size).#{new_message} #{new_matcher} #{times}\n"
|
44
|
+
new_code << "end"
|
32
45
|
end
|
46
|
+
replace_with new_code
|
33
47
|
end
|
34
48
|
end
|
35
49
|
end
|
36
50
|
end
|
37
51
|
end
|
38
|
-
|
39
|
-
{should: 'to', should_not: 'not_to'}.each do |old_message, new_message|
|
40
|
-
within_files 'spec/**/*.rb' do
|
41
|
-
# it { should matcher } => it { is_expected.to matcher }
|
42
|
-
# it { should_not matcher } => it { is_expected.not_to matcher }
|
43
|
-
with_node type: 'block', caller: {message: 'it'} do
|
44
|
-
if_only_exist_node type: 'send', receiver: nil, message: old_message do
|
45
|
-
matcher = node.body.first.arguments.first.source(self)
|
46
|
-
replace_with "it { is_expected.#{new_message} #{matcher} }"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
52
|
end
|
@@ -1,31 +1,27 @@
|
|
1
1
|
Synvert::Rewriter.new "convert_rspec_should_to_expect", "RSpec converts should to expect" do
|
2
2
|
if_gem 'rspec', {gte: '2.11.0'}
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
within_files 'spec/**/*.rb' do
|
5
|
+
# obj.should matcher => expect(obj).to matcher
|
6
|
+
# obj.should_not matcher => expect(obj).not_to matcher
|
7
|
+
{should: 'to', should_not: 'not_to'}.each do |old_message, new_message|
|
8
8
|
with_node type: 'send', receiver: {type: {not: 'block'}}, message: old_message do
|
9
9
|
if node.receiver && node.arguments.size > 0
|
10
10
|
replace_with "expect({{receiver}}).#{new_message} {{arguments}}"
|
11
11
|
end
|
12
12
|
end
|
13
|
-
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
# Integer.should === 1 => expect(Integer).to be === 1
|
14
|
+
# 1.should == 1 => expect(1).to eq 1
|
15
|
+
# 1.should < 1 => expect(1).to be < 2
|
16
|
+
# Integer.should === 1 => expect(Integer).to be === 1
|
17
|
+
{'==' => 'eq', '<' => 'be <', '>' => 'be >', '<=' => 'be <=', '>=' => 'be >=', '===' => 'be ==='}.each do |old_matcher, new_matcher|
|
20
18
|
with_node type: 'send', receiver: {type: 'send', message: old_message}, message: old_matcher do
|
21
19
|
if node.receiver.receiver
|
22
20
|
replace_with "expect({{receiver.receiver}}).#{new_message} #{new_matcher} {{arguments}}"
|
23
21
|
end
|
24
22
|
end
|
25
23
|
end
|
26
|
-
end
|
27
24
|
|
28
|
-
within_files 'spec/**/*.rb' do
|
29
25
|
# 'string'.should =~ /^str/ => expect('string').to match /^str/
|
30
26
|
# [1, 2, 3].should =~ [2, 1, 3] => expect([1, 2, 3]).to match_array [2, 1, 3]
|
31
27
|
with_node type: 'send', receiver: {type: 'send', message: old_message}, message: '=~' do
|
data/lib/synvert/version.rb
CHANGED
@@ -32,7 +32,7 @@ GEM
|
|
32
32
|
it 'returns false if version in Gemfile.lock is less than definition' do
|
33
33
|
expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
|
34
34
|
expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
|
35
|
-
gem_spec = Rewriter::GemSpec.new('ast', {
|
35
|
+
gem_spec = Rewriter::GemSpec.new('ast', {gt: '1.2.0'})
|
36
36
|
expect(gem_spec).not_to be_match
|
37
37
|
end
|
38
38
|
|
@@ -26,6 +26,18 @@ class Post < ActiveRecord::Base
|
|
26
26
|
def scoped_active_user_by_email(email)
|
27
27
|
User.scoped_by_email_and_active(email, true)
|
28
28
|
end
|
29
|
+
|
30
|
+
def active_users_by_sql(email)
|
31
|
+
User.find_by_sql(["select * from users where email = ?", email])
|
32
|
+
end
|
33
|
+
|
34
|
+
def active_user_by_id(id)
|
35
|
+
User.find_by_id(id)
|
36
|
+
end
|
37
|
+
|
38
|
+
def active_user_by_account_email(account_email)
|
39
|
+
User.find_by_account_id(Account.find_by_email(account_email).id)
|
40
|
+
end
|
29
41
|
end
|
30
42
|
'''}
|
31
43
|
let(:post_model_rewritten_content) {'''
|
@@ -45,6 +57,18 @@ class Post < ActiveRecord::Base
|
|
45
57
|
def scoped_active_user_by_email(email)
|
46
58
|
User.where(email: email, active: true)
|
47
59
|
end
|
60
|
+
|
61
|
+
def active_users_by_sql(email)
|
62
|
+
User.find_by_sql(["select * from users where email = ?", email])
|
63
|
+
end
|
64
|
+
|
65
|
+
def active_user_by_id(id)
|
66
|
+
User.find(id)
|
67
|
+
end
|
68
|
+
|
69
|
+
def active_user_by_account_email(account_email)
|
70
|
+
User.where(account_id: Account.where(email: account_email).first.id).first
|
71
|
+
end
|
48
72
|
end
|
49
73
|
'''}
|
50
74
|
let(:users_controller_content) {'''
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: synvert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|