synvert 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/synvert.rb +1 -0
- data/lib/synvert/node_ext.rb +29 -33
- data/lib/synvert/rewriter.rb +16 -8
- data/lib/synvert/rewriter/action.rb +83 -35
- data/lib/synvert/rewriter/condition.rb +16 -13
- data/lib/synvert/rewriter/instance.rb +60 -22
- data/lib/synvert/rewriter/scope.rb +24 -8
- data/lib/synvert/snippets/factory_girl/syntax_methods.rb +20 -9
- data/lib/synvert/snippets/rails/upgrade_3_2_to_4_0.rb +153 -77
- data/lib/synvert/version.rb +1 -1
- data/spec/spec_helper.rb +2 -0
- data/spec/synvert/node_ext_spec.rb +15 -44
- data/spec/synvert/rewriter/action_spec.rb +146 -46
- data/spec/synvert/rewriter/condition_spec.rb +32 -15
- data/spec/synvert/rewriter/instance_spec.rb +74 -35
- data/spec/synvert/rewriter/scope_spec.rb +16 -12
- data/spec/synvert/rewriter_spec.rb +5 -4
- data/spec/synvert/snippets/factory_girl/syntax_methods_spec.rb +154 -0
- data/spec/synvert/snippets/rails/upgrade_3_2_to_4_0_spec.rb +268 -0
- data/synvert.gemspec +2 -0
- metadata +34 -2
@@ -2,74 +2,174 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Synvert
|
4
4
|
describe Rewriter::ReplaceWithAction do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
subject {
|
6
|
+
source = "post = FactoryGirl.create_list :post, 2"
|
7
|
+
send_node = Parser::CurrentRuby.parse(source).children[1]
|
8
|
+
instance = double(:current_node => send_node)
|
9
|
+
Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}')
|
10
|
+
}
|
11
|
+
|
12
|
+
it 'gets begin_pos' do
|
13
|
+
expect(subject.begin_pos).to eq 7
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'gets end_pos' do
|
17
|
+
expect(subject.end_pos).to eq 39
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'gets rewritten_code' do
|
21
|
+
expect(subject.rewritten_code).to eq 'create_list :post, 2'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe Rewriter::AppendAction < Rewriter::Action do
|
26
|
+
describe 'class node' do
|
27
|
+
subject {
|
28
|
+
source = "class User\n has_many :posts\nend"
|
29
|
+
class_node = Parser::CurrentRuby.parse(source)
|
30
|
+
instance = double(:current_node => class_node)
|
31
|
+
Rewriter::AppendAction.new(instance, "def as_json\n super\nend")
|
32
|
+
}
|
33
|
+
|
34
|
+
it 'gets begin_pos' do
|
35
|
+
expect(subject.begin_pos).to eq 28
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'gets end_pos' do
|
39
|
+
expect(subject.end_pos).to eq 28
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'gets rewritten_code' do
|
43
|
+
expect(subject.rewritten_code).to eq "\n\n def as_json\n super\n end"
|
12
44
|
end
|
13
45
|
end
|
14
46
|
end
|
15
47
|
|
16
48
|
describe Rewriter::InsertAction do
|
17
|
-
describe '
|
18
|
-
|
19
|
-
|
20
|
-
source = "RSpec.configure do |config|\nend"
|
49
|
+
describe 'block node without args' do
|
50
|
+
subject {
|
51
|
+
source = "Synvert::Application.configure do\nend"
|
21
52
|
block_node = Parser::CurrentRuby.parse(source)
|
22
|
-
|
23
|
-
|
53
|
+
instance = double(:current_node => block_node)
|
54
|
+
Rewriter::InsertAction.new(instance, 'config.eager_load = true')
|
55
|
+
}
|
56
|
+
|
57
|
+
it 'gets begin_pos' do
|
58
|
+
expect(subject.begin_pos).to eq 33
|
24
59
|
end
|
25
60
|
|
26
|
-
it '
|
27
|
-
|
28
|
-
source = "class Test::Unit::TestCase\nend"
|
29
|
-
block_node = Parser::CurrentRuby.parse(source)
|
30
|
-
output = action.rewrite(source, block_node)
|
31
|
-
expect(output).to eq "class Test::Unit::TestCase\n include FactoryGirl::Syntax::Methods\nend"
|
61
|
+
it 'gets end_pos' do
|
62
|
+
expect(subject.end_pos).to eq 33
|
32
63
|
end
|
33
64
|
|
34
|
-
it '
|
35
|
-
|
36
|
-
source = "require 'cucumber/rails'\nActionController::Base.allow_rescue = false"
|
37
|
-
block_node = Parser::CurrentRuby.parse(source)
|
38
|
-
output = action.rewrite(source, block_node)
|
39
|
-
expect(output).to eq "require 'cucumber/rails'\nActionController::Base.allow_rescue = false\nWorld(FactoryGirl::Syntax::Methods)"
|
65
|
+
it 'gets rewritten_code' do
|
66
|
+
expect(subject.rewritten_code).to eq "\n config.eager_load = true"
|
40
67
|
end
|
68
|
+
end
|
41
69
|
|
42
|
-
|
43
|
-
|
44
|
-
source = "
|
70
|
+
describe 'block node with args' do
|
71
|
+
subject {
|
72
|
+
source = "RSpec.configure do |config|\nend"
|
45
73
|
block_node = Parser::CurrentRuby.parse(source)
|
46
|
-
|
47
|
-
|
74
|
+
instance = double(:current_node => block_node)
|
75
|
+
Rewriter::InsertAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
|
76
|
+
}
|
77
|
+
|
78
|
+
it 'gets begin_pos' do
|
79
|
+
expect(subject.begin_pos).to eq 27
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'gets end_pos' do
|
83
|
+
expect(subject.end_pos).to eq 27
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'gets rewritten_code' do
|
87
|
+
expect(subject.rewritten_code).to eq "\n config.include FactoryGirl::Syntax::Methods"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'class node without superclass' do
|
92
|
+
subject {
|
93
|
+
source = "class User\n has_many :posts\nend"
|
94
|
+
class_node = Parser::CurrentRuby.parse(source)
|
95
|
+
instance = double(:current_node => class_node)
|
96
|
+
Rewriter::InsertAction.new(instance, 'include Deletable')
|
97
|
+
}
|
98
|
+
|
99
|
+
it 'gets begin_pos' do
|
100
|
+
expect(subject.begin_pos).to eq 10
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'gets end_pos' do
|
104
|
+
expect(subject.end_pos).to eq 10
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'gets rewritten_code' do
|
108
|
+
expect(subject.rewritten_code).to eq "\n include Deletable"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe 'class node with superclass' do
|
113
|
+
subject {
|
114
|
+
source = "class User < ActiveRecord::Base\n has_many :posts\nend"
|
115
|
+
class_node = Parser::CurrentRuby.parse(source)
|
116
|
+
instance = double(:current_node => class_node)
|
117
|
+
Rewriter::InsertAction.new(instance, 'include Deletable')
|
118
|
+
}
|
119
|
+
|
120
|
+
it 'gets begin_pos' do
|
121
|
+
expect(subject.begin_pos).to eq 31
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'gets end_pos' do
|
125
|
+
expect(subject.end_pos).to eq 31
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'gets rewritten_code' do
|
129
|
+
expect(subject.rewritten_code).to eq "\n include Deletable"
|
48
130
|
end
|
49
131
|
end
|
50
132
|
end
|
51
133
|
|
52
134
|
describe Rewriter::InsertAfterAction do
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
135
|
+
subject {
|
136
|
+
source = " include Foo"
|
137
|
+
node = Parser::CurrentRuby.parse(source)
|
138
|
+
instance = double(:current_node => node)
|
139
|
+
Rewriter::InsertAfterAction.new(instance, 'include Bar')
|
140
|
+
}
|
141
|
+
|
142
|
+
it 'gets begin_pos' do
|
143
|
+
expect(subject.begin_pos).to eq 13
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'gets end_pos' do
|
147
|
+
expect(subject.end_pos).to eq 13
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'gets rewritten_code' do
|
151
|
+
expect(subject.rewritten_code).to eq "\n include Bar"
|
61
152
|
end
|
62
153
|
end
|
63
154
|
|
64
155
|
describe Rewriter::RemoveAction do
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
156
|
+
subject {
|
157
|
+
source = "user = User.new params[:user]\nuser.save\nrender\n"
|
158
|
+
send_node = Parser::CurrentRuby.parse(source).children[1]
|
159
|
+
instance = double(:current_node => send_node)
|
160
|
+
Rewriter::RemoveAction.new(instance)
|
161
|
+
}
|
162
|
+
|
163
|
+
it 'gets begin_pos' do
|
164
|
+
expect(subject.begin_pos).to eq 30
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'gets end_pos' do
|
168
|
+
expect(subject.end_pos).to eq 39
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'gets rewritten_code' do
|
172
|
+
expect(subject.rewritten_code).to eq ""
|
73
173
|
end
|
74
174
|
end
|
75
175
|
end
|
@@ -11,22 +11,31 @@ module Synvert
|
|
11
11
|
"""
|
12
12
|
}
|
13
13
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
14
|
+
let(:instance) { double(:current_node => node, :current_source => source) }
|
14
15
|
|
15
|
-
describe '#
|
16
|
-
it '
|
17
|
-
|
18
|
-
|
16
|
+
describe '#process' do
|
17
|
+
it 'call block if match anything' do
|
18
|
+
run = false
|
19
|
+
condition = Rewriter::UnlessExistCondition.new instance, type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
20
|
+
run = true
|
21
|
+
end
|
22
|
+
condition.process
|
23
|
+
expect(run).to be_true
|
19
24
|
end
|
20
25
|
|
21
|
-
it '
|
22
|
-
|
23
|
-
|
26
|
+
it 'not call block if not match anything' do
|
27
|
+
run = false
|
28
|
+
condition = Rewriter::UnlessExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
29
|
+
run = true
|
30
|
+
end
|
31
|
+
condition.process
|
32
|
+
expect(run).to be_false
|
24
33
|
end
|
25
34
|
end
|
26
35
|
end
|
27
36
|
|
28
37
|
describe Rewriter::IfOnlyExistCondition do
|
29
|
-
describe '#
|
38
|
+
describe '#process' do
|
30
39
|
it 'gets matching nodes' do
|
31
40
|
source = """
|
32
41
|
RSpec.configure do |config|
|
@@ -34,12 +43,16 @@ module Synvert
|
|
34
43
|
end
|
35
44
|
"""
|
36
45
|
node = Parser::CurrentRuby.parse(source)
|
37
|
-
|
38
|
-
|
39
|
-
|
46
|
+
instance = double(:current_node => node, :current_source => source)
|
47
|
+
run = false
|
48
|
+
condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
49
|
+
run = true
|
50
|
+
end
|
51
|
+
condition.process
|
52
|
+
expect(run).to be_true
|
40
53
|
end
|
41
54
|
|
42
|
-
it '
|
55
|
+
it 'not call block if does not match' do
|
43
56
|
source = """
|
44
57
|
RSpec.configure do |config|
|
45
58
|
config.include EmailSpec::Helpers
|
@@ -47,9 +60,13 @@ module Synvert
|
|
47
60
|
end
|
48
61
|
"""
|
49
62
|
node = Parser::CurrentRuby.parse(source)
|
50
|
-
|
51
|
-
|
52
|
-
|
63
|
+
instance = double(:current_node => node, :current_source => source)
|
64
|
+
run = false
|
65
|
+
condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
66
|
+
run = true
|
67
|
+
end
|
68
|
+
condition.process
|
69
|
+
expect(run).to be_false
|
53
70
|
end
|
54
71
|
end
|
55
72
|
end
|
@@ -2,43 +2,79 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Synvert
|
4
4
|
describe Rewriter::Instance do
|
5
|
-
let(:
|
5
|
+
let(:rewriter) { Rewriter.new 'description' do; end }
|
6
|
+
let(:instance) { Rewriter::Instance.new(rewriter, 'file pattern') }
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
it 'parses within_node' do
|
9
|
+
scope = double()
|
10
|
+
block = Proc.new {}
|
11
|
+
expect(Rewriter::Scope).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(scope)
|
12
|
+
expect(scope).to receive(:process)
|
13
|
+
instance.within_node(type: 'send', message: 'create', &block)
|
12
14
|
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
it 'parses with_node' do
|
17
|
+
scope = double()
|
18
|
+
block = Proc.new {}
|
19
|
+
expect(Rewriter::Scope).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(scope)
|
20
|
+
expect(scope).to receive(:process)
|
21
|
+
instance.with_node(type: 'send', message: 'create', &block)
|
19
22
|
end
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
it 'parses unless_exist_node' do
|
25
|
+
condition = double()
|
26
|
+
block = Proc.new {}
|
27
|
+
expect(Rewriter::UnlessExistCondition).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(condition)
|
28
|
+
expect(condition).to receive(:process)
|
29
|
+
instance.unless_exist_node(type: 'send', message: 'create', &block)
|
26
30
|
end
|
27
31
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
32
|
+
it 'parses if_only_exist_node' do
|
33
|
+
condition = double()
|
34
|
+
block = Proc.new {}
|
35
|
+
expect(Rewriter::IfOnlyExistCondition).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(condition)
|
36
|
+
expect(condition).to receive(:process)
|
37
|
+
instance.if_only_exist_node(type: 'send', message: 'create', &block)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'parses append' do
|
41
|
+
expect(Rewriter::AppendAction).to receive(:new).with(instance, 'include FactoryGirl::Syntax::Methods')
|
42
|
+
instance.append "include FactoryGirl::Syntax::Methods"
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'parses insert' do
|
46
|
+
expect(Rewriter::InsertAction).to receive(:new).with(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
|
47
|
+
instance.insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'parses insert_after' do
|
51
|
+
expect(Rewriter::InsertAfterAction).to receive(:new).with(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
|
52
|
+
instance.insert_after "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'parses replace_with' do
|
56
|
+
expect(Rewriter::ReplaceWithAction).to receive(:new).with(instance, 'create {{arguments}}')
|
57
|
+
instance.replace_with 'create {{arguments}}'
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'parses remove' do
|
61
|
+
expect(Rewriter::RemoveAction).to receive(:new).with(instance)
|
62
|
+
instance.remove
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'parses assign / fetch' do
|
66
|
+
instance.assign 'parameters', 'user', 'email'
|
67
|
+
expect(instance.fetch 'parameters', 'user').to eq 'email'
|
33
68
|
end
|
34
69
|
|
35
70
|
describe '#process' do
|
36
71
|
before { Configuration.instance.set :path, '.' }
|
37
72
|
|
38
73
|
it 'FactoryGirl uses short syntax' do
|
39
|
-
instance = Rewriter::Instance.new
|
40
|
-
|
41
|
-
|
74
|
+
instance = Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
|
75
|
+
with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
|
76
|
+
replace_with 'create {{arguments}}'
|
77
|
+
end
|
42
78
|
end
|
43
79
|
input = """
|
44
80
|
it 'uses factory_girl' do
|
@@ -61,10 +97,11 @@ end
|
|
61
97
|
end
|
62
98
|
|
63
99
|
it 'includes FactoryGirl::Syntax::Methods' do
|
64
|
-
instance = Rewriter::Instance.new
|
65
|
-
|
66
|
-
|
67
|
-
|
100
|
+
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
|
101
|
+
with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
|
102
|
+
unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
103
|
+
insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
|
104
|
+
end
|
68
105
|
end
|
69
106
|
end
|
70
107
|
input = """
|
@@ -83,10 +120,11 @@ end
|
|
83
120
|
end
|
84
121
|
|
85
122
|
it 'does not include FactoryGirl::Syntax::Methods' do
|
86
|
-
instance = Rewriter::Instance.new
|
87
|
-
|
88
|
-
|
89
|
-
|
123
|
+
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
|
124
|
+
with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
|
125
|
+
unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
126
|
+
insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
|
127
|
+
end
|
90
128
|
end
|
91
129
|
end
|
92
130
|
input = """
|
@@ -106,9 +144,10 @@ end
|
|
106
144
|
end
|
107
145
|
|
108
146
|
it 'process nested send nodes' do
|
109
|
-
instance = Rewriter::Instance.new
|
110
|
-
|
111
|
-
|
147
|
+
instance = Rewriter::Instance.new rewriter, 'config/*.rb' do
|
148
|
+
with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'active_record'}, message: 'identity_map=' do
|
149
|
+
remove
|
150
|
+
end
|
112
151
|
end
|
113
152
|
input = 'config.active_record.identity_map = true'
|
114
153
|
output = ''
|
@@ -17,21 +17,25 @@ end
|
|
17
17
|
"""
|
18
18
|
}
|
19
19
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
20
|
+
let(:instance) { double(:current_node => node, :current_node= => node, :current_source => source) }
|
20
21
|
|
21
|
-
describe '#
|
22
|
-
it '
|
23
|
-
|
24
|
-
|
22
|
+
describe '#process' do
|
23
|
+
it 'not call block if no matching node' do
|
24
|
+
run = false
|
25
|
+
scope = Rewriter::Scope.new instance, type: 'send', message: 'missing' do
|
26
|
+
run = true
|
27
|
+
end
|
28
|
+
scope.process
|
29
|
+
expect(run).to be_false
|
25
30
|
end
|
26
31
|
|
27
|
-
it '
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
expect(scope.matching_nodes([node]).size).to eq 1
|
32
|
+
it 'call block if there is matching node' do
|
33
|
+
run = false
|
34
|
+
scope = Rewriter::Scope.new instance, type: 'send', receiver: 'FactoryGirl', message: 'create', arguments: [':post'] do
|
35
|
+
run = true
|
36
|
+
end
|
37
|
+
scope.process
|
38
|
+
expect(run).to be_true
|
35
39
|
end
|
36
40
|
end
|
37
41
|
end
|