synvert 0.0.3 → 0.0.4

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.
@@ -2,74 +2,174 @@ require 'spec_helper'
2
2
 
3
3
  module Synvert
4
4
  describe Rewriter::ReplaceWithAction do
5
- describe '#rewrite' do
6
- it 'replaces code' do
7
- action = Rewriter::ReplaceWithAction.new('create_list {{self.arguments}}')
8
- source = "post = FactoryGirl.create_list :post, 2"
9
- send_node = Parser::CurrentRuby.parse(source).children[1]
10
- output = action.rewrite(source, send_node)
11
- expect(output).to eq "post = create_list :post, 2"
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 '#rewrite' do
18
- it 'insert code to block node' do
19
- action = Rewriter::InsertAction.new('{{self.arguments.first}}.include FactoryGirl::Syntax::Methods')
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
- output = action.rewrite(source, block_node)
23
- expect(output).to eq "RSpec.configure do |config|\n config.include FactoryGirl::Syntax::Methods\nend"
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 'insert code to class node' do
27
- action = Rewriter::InsertAction.new('include FactoryGirl::Syntax::Methods')
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 'insert code to begin node' do
35
- action = Rewriter::InsertAction.new('World(FactoryGirl::Syntax::Methods)')
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
- it 'insert code to other node' do
43
- action = Rewriter::InsertAction.new('World(FactoryGirl::Syntax::Methods)')
44
- source = "require 'cucumber/rails'"
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
- output = action.rewrite(source, block_node)
47
- expect(output).to eq "require 'cucumber/rails'\nWorld(FactoryGirl::Syntax::Methods)"
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
- describe '#rewrite' do
54
- it 'insert_after code' do
55
- action = Rewriter::InsertAfterAction.new('include Bar')
56
- source = " include Foo"
57
- node = Parser::CurrentRuby.parse(source)
58
- output = action.rewrite(source, node)
59
- expect(output).to eq " include Foo\n include Bar"
60
- end
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
- describe '#rewrite' do
66
- it 'remove code' do
67
- action = Rewriter::RemoveAction.new
68
- source = "user = User.new params[:user]\nuser.save\nrender\n"
69
- send_node = Parser::CurrentRuby.parse(source).children[1]
70
- output = action.rewrite(source, send_node)
71
- expect(output).to eq "user = User.new params[:user]\nrender\n"
72
- end
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 '#matching_nodes' do
16
- it 'gets empty array if does not matchi anything' do
17
- condition = Rewriter::UnlessExistCondition.new type: 'send', message: 'include', arguments: {first: {to_s: 'FactoryGirl::Syntax::Methods'} }
18
- expect(condition.matching_nodes([node]).size).to eq 1
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 'gets matching nodes' do
22
- condition = Rewriter::UnlessExistCondition.new type: 'send', message: 'include', arguments: {first: {to_s: 'EmailSpec::Helpers'} }
23
- expect(condition.matching_nodes([node])).to eq []
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 '#matching_nodes' do
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
- condition = Rewriter::IfOnlyExistCondition.new type: 'send', message: 'include', arguments: {first: 'EmailSpec::Helpers'}
39
- expect(condition.matching_nodes([node])).to eq [node]
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 'gets empty array if does not match' do
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
- condition = Rewriter::IfOnlyExistCondition.new type: 'send', message: 'include', arguments: {first: {to_s: 'EmailSpec::Helpers'} }
52
- expect(condition.matching_nodes([node])).to eq []
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(:instance) { Rewriter::Instance.new('file pattern') }
5
+ let(:rewriter) { Rewriter.new 'description' do; end }
6
+ let(:instance) { Rewriter::Instance.new(rewriter, 'file pattern') }
6
7
 
7
- describe '#insert' do
8
- it 'sets an action' do
9
- expect(Rewriter::InsertAction).to receive(:new).with('{{self.arguments.first}}.include FactoryGirl::Syntax::Methods')
10
- instance.insert "{{self.arguments.first}}.include FactoryGirl::Syntax::Methods"
11
- end
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
- describe '#insert_after' do
15
- it 'sets an action' do
16
- expect(Rewriter::InsertAfterAction).to receive(:new).with('{{self.arguments.first}}.include FactoryGirl::Syntax::Methods')
17
- instance.insert_after "{{self.arguments.first}}.include FactoryGirl::Syntax::Methods"
18
- end
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
- describe '#replace_with' do
22
- it 'sets an action' do
23
- expect(Rewriter::ReplaceWithAction).to receive(:new).with('create {{self.arguments}}')
24
- instance.replace_with 'create {{self.arguments}}'
25
- end
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
- describe '#remove' do
29
- it 'sets an action' do
30
- expect(Rewriter::RemoveAction).to receive(:new)
31
- instance.remove
32
- end
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('spec/**/*_spec.rb')
40
- instance.with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
41
- replace_with 'create {{self.arguments}}'
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('spec/spec_helper.rb')
65
- instance.with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
66
- unless_exist_node type: 'send', message: 'include', arguments: {first: {to_s: 'FactoryGirl::Syntax::Methods'}} do
67
- insert "{{self.arguments.first}}.include FactoryGirl::Syntax::Methods"
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('spec/spec_helper.rb')
87
- instance.with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
88
- unless_exist_node type: 'send', message: 'include', arguments: {first: {to_s: 'FactoryGirl::Syntax::Methods'}} do
89
- insert "{{self.arguments.first}}.include FactoryGirl::Syntax::Methods"
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('config/*.rb')
110
- instance.with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'active_record'}, message: 'identity_map=' do
111
- remove
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 '#matching_nodes' do
22
- it 'gets empty array if does not match anything' do
23
- scope = Rewriter::Scope.new type: 'send', message: 'missing'
24
- expect(scope.matching_nodes([node])).to eq []
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 'gets matching nodes' do
28
- scope = Rewriter::Scope.new type: 'send', receiver: 'FactoryGirl', message: 'create', arguments: [':post']
29
- expect(scope.matching_nodes([node]).size).to eq 2
30
- end
31
-
32
- it 'gets matching nodes witch block caller' do
33
- scope = Rewriter::Scope.new type: 'block', caller: {message: 'it', arguments: ['gets posts']}
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