synvert-core 0.16.0 → 0.20.0

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -1
  3. data/CHANGELOG.md +16 -0
  4. data/lib/synvert/core.rb +1 -4
  5. data/lib/synvert/core/configuration.rb +9 -17
  6. data/lib/synvert/core/engine/erb.rb +29 -22
  7. data/lib/synvert/core/exceptions.rb +3 -3
  8. data/lib/synvert/core/node_ext.rb +102 -101
  9. data/lib/synvert/core/rewriter.rb +33 -19
  10. data/lib/synvert/core/rewriter/action.rb +3 -5
  11. data/lib/synvert/core/rewriter/action/append_action.rb +1 -1
  12. data/lib/synvert/core/rewriter/action/insert_action.rb +7 -3
  13. data/lib/synvert/core/rewriter/action/insert_after_action.rb +1 -1
  14. data/lib/synvert/core/rewriter/action/remove_action.rb +1 -1
  15. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +4 -3
  16. data/lib/synvert/core/rewriter/action/replace_with_action.rb +6 -4
  17. data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
  18. data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -2
  19. data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
  20. data/lib/synvert/core/rewriter/gem_spec.rb +10 -10
  21. data/lib/synvert/core/rewriter/helper.rb +3 -5
  22. data/lib/synvert/core/rewriter/instance.rb +22 -19
  23. data/lib/synvert/core/rewriter/scope/within_scope.rb +18 -11
  24. data/lib/synvert/core/version.rb +1 -1
  25. data/spec/spec_helper.rb +1 -6
  26. data/spec/synvert/core/engine/erb_spec.rb +30 -30
  27. data/spec/synvert/core/node_ext_spec.rb +53 -52
  28. data/spec/synvert/core/rewriter/action/insert_action_spec.rb +8 -8
  29. data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +3 -3
  30. data/spec/synvert/core/rewriter/action/remove_action_spec.rb +1 -1
  31. data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +15 -11
  32. data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +17 -9
  33. data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +21 -12
  34. data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +17 -9
  35. data/spec/synvert/core/rewriter/gem_spec_spec.rb +11 -10
  36. data/spec/synvert/core/rewriter/helper_spec.rb +34 -31
  37. data/spec/synvert/core/rewriter/instance_spec.rb +104 -70
  38. data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +8 -6
  39. data/spec/synvert/core/rewriter/scope/within_scope.rb +16 -9
  40. data/spec/synvert/core/rewriter_spec.rb +105 -68
  41. metadata +2 -4
  42. data/spec/synvert/core/configuration_spec.rb +0 -12
@@ -20,7 +20,7 @@ module Synvert::Core
20
20
  end
21
21
 
22
22
  it 'gets rewritten_code' do
23
- expect(subject.rewritten_code).to eq ""
23
+ expect(subject.rewritten_code).to eq ''
24
24
  end
25
25
  end
26
26
  end
@@ -4,20 +4,20 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter::ReplaceWithAction do
7
- context "replace with single line" do
7
+ context 'replace with single line' do
8
8
  subject {
9
- source = "post = FactoryGirl.create_list :post, 2"
9
+ source = 'post = FactoryGirl.create_list :post, 2'
10
10
  send_node = Parser::CurrentRuby.parse(source).children[1]
11
11
  instance = double(current_node: send_node)
12
12
  Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}')
13
13
  }
14
14
 
15
15
  it 'gets begin_pos' do
16
- expect(subject.begin_pos).to eq "post = ".length
16
+ expect(subject.begin_pos).to eq 'post = '.length
17
17
  end
18
18
 
19
19
  it 'gets end_pos' do
20
- expect(subject.end_pos).to eq "post = FactoryGirl.create_list :post, 2".length
20
+ expect(subject.end_pos).to eq 'post = FactoryGirl.create_list :post, 2'.length
21
21
  end
22
22
 
23
23
  it 'gets rewritten_code' do
@@ -25,15 +25,19 @@ module Synvert::Core
25
25
  end
26
26
  end
27
27
 
28
- context "#replace with multiple line" do
28
+ context '#replace with multiple line' do
29
29
  subject {
30
- source = " its(:size) { should == 1 }"
30
+ source = ' its(:size) { should == 1 }'
31
31
  send_node = Parser::CurrentRuby.parse(source)
32
32
  instance = double(current_node: send_node)
33
- Rewriter::ReplaceWithAction.new(instance, """describe '#size' do
33
+ Rewriter::ReplaceWithAction.new(
34
+ instance,
35
+ "describe '#size' do
34
36
  subject { super().size }
35
37
  it { {{body}} }
36
- end""", autoindent: false)
38
+ end",
39
+ autoindent: false
40
+ )
37
41
  }
38
42
 
39
43
  it 'gets begin_pos' do
@@ -41,14 +45,14 @@ end""", autoindent: false)
41
45
  end
42
46
 
43
47
  it 'gets end_pos' do
44
- expect(subject.end_pos).to eq " its(:size) { should == 1 }".length
48
+ expect(subject.end_pos).to eq ' its(:size) { should == 1 }'.length
45
49
  end
46
50
 
47
51
  it 'gets rewritten_code' do
48
- expect(subject.rewritten_code).to eq """describe '#size' do
52
+ expect(subject.rewritten_code).to eq "describe '#size' do
49
53
  subject { super().size }
50
54
  it { should == 1 }
51
- end"""
55
+ end"
52
56
  end
53
57
  end
54
58
  end
@@ -5,31 +5,39 @@ require 'spec_helper'
5
5
  module Synvert::Core
6
6
  describe Rewriter::IfExistCondition do
7
7
  let(:source) {
8
- """
8
+ '
9
9
  RSpec.configure do |config|
10
10
  config.include EmailSpec::Helpers
11
11
  config.include EmailSpec::Methods
12
12
  end
13
- """
13
+ '
14
14
  }
15
15
  let(:node) { Parser::CurrentRuby.parse(source) }
16
- let(:instance) { double(:current_node => node) }
16
+ let(:instance) { double(current_node: node) }
17
17
 
18
18
  describe '#process' do
19
19
  it 'call block if match anything' do
20
20
  run = false
21
- condition = Rewriter::IfExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
22
- run = true
23
- end
21
+ condition =
22
+ Rewriter::IfExistCondition.new instance,
23
+ type: 'send',
24
+ message: 'include',
25
+ arguments: ['EmailSpec::Helpers'] do
26
+ run = true
27
+ end
24
28
  condition.process
25
29
  expect(run).to be_truthy
26
30
  end
27
31
 
28
32
  it 'not call block if not match anything' do
29
33
  run = false
30
- condition = Rewriter::IfExistCondition.new instance, type: 'send', message: 'include', arguments: ['FactoryGirl::SyntaxMethods'] do
31
- run = true
32
- end
34
+ condition =
35
+ Rewriter::IfExistCondition.new instance,
36
+ type: 'send',
37
+ message: 'include',
38
+ arguments: ['FactoryGirl::SyntaxMethods'] do
39
+ run = true
40
+ end
33
41
  condition.process
34
42
  expect(run).to be_falsey
35
43
  end
@@ -5,38 +5,47 @@ require 'spec_helper'
5
5
  module Synvert::Core
6
6
  describe Rewriter::IfOnlyExistCondition do
7
7
  let(:source) {
8
- """
8
+ '
9
9
  RSpec.configure do |config|
10
10
  config.include EmailSpec::Helpers
11
11
  config.include EmailSpec::Methods
12
12
  end
13
- """
13
+ '
14
14
  }
15
15
  let(:node) { Parser::CurrentRuby.parse(source) }
16
- let(:instance) { double(:current_node => node) }
16
+ let(:instance) { double(current_node: node) }
17
17
 
18
18
  describe '#process' do
19
19
  it 'gets matching nodes' do
20
- source = """
20
+ source =
21
+ '
21
22
  RSpec.configure do |config|
22
23
  config.include EmailSpec::Helpers
23
24
  end
24
- """
25
+ '
25
26
  node = Parser::CurrentRuby.parse(source)
26
- instance = double(:current_node => node)
27
+ instance = double(current_node: node)
27
28
  run = false
28
- condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
29
- run = true
30
- end
29
+ condition =
30
+ Rewriter::IfOnlyExistCondition.new instance,
31
+ type: 'send',
32
+ message: 'include',
33
+ arguments: ['EmailSpec::Helpers'] do
34
+ run = true
35
+ end
31
36
  condition.process
32
37
  expect(run).to be_truthy
33
38
  end
34
39
 
35
40
  it 'not call block if does not match' do
36
41
  run = false
37
- condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
38
- run = true
39
- end
42
+ condition =
43
+ Rewriter::IfOnlyExistCondition.new instance,
44
+ type: 'send',
45
+ message: 'include',
46
+ arguments: ['EmailSpec::Helpers'] do
47
+ run = true
48
+ end
40
49
  condition.process
41
50
  expect(run).to be_falsey
42
51
  end
@@ -5,31 +5,39 @@ require 'spec_helper'
5
5
  module Synvert::Core
6
6
  describe Rewriter::UnlessExistCondition do
7
7
  let(:source) {
8
- """
8
+ '
9
9
  RSpec.configure do |config|
10
10
  config.include EmailSpec::Helpers
11
11
  config.include EmailSpec::Methods
12
12
  end
13
- """
13
+ '
14
14
  }
15
15
  let(:node) { Parser::CurrentRuby.parse(source) }
16
- let(:instance) { double(:current_node => node) }
16
+ let(:instance) { double(current_node: node) }
17
17
 
18
18
  describe '#process' do
19
19
  it 'call block if match anything' do
20
20
  run = false
21
- condition = Rewriter::UnlessExistCondition.new instance, type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
22
- run = true
23
- end
21
+ condition =
22
+ Rewriter::UnlessExistCondition.new instance,
23
+ type: 'send',
24
+ message: 'include',
25
+ arguments: ['FactoryGirl::Syntax::Methods'] do
26
+ run = true
27
+ end
24
28
  condition.process
25
29
  expect(run).to be_truthy
26
30
  end
27
31
 
28
32
  it 'not call block if not match anything' do
29
33
  run = false
30
- condition = Rewriter::UnlessExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
31
- run = true
32
- end
34
+ condition =
35
+ Rewriter::UnlessExistCondition.new instance,
36
+ type: 'send',
37
+ message: 'include',
38
+ arguments: ['EmailSpec::Helpers'] do
39
+ run = true
40
+ end
33
41
  condition.process
34
42
  expect(run).to be_falsey
35
43
  end
@@ -4,7 +4,8 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter::GemSpec do
7
- let(:gemfile_lock_content) { """
7
+ let(:gemfile_lock_content) {
8
+ '
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
@@ -14,41 +15,41 @@ GEM
14
15
  slop (~> 3.4, >= 3.4.5)
15
16
  rake (10.1.1)
16
17
  slop (3.4.7)
17
- """
18
+ '
18
19
  }
19
20
 
20
21
  it 'returns true if version in Gemfile.lock is greater than definition' do
21
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
22
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
22
23
  expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
23
- gem_spec = Rewriter::GemSpec.new('ast', {gte: '1.0.0'})
24
+ gem_spec = Rewriter::GemSpec.new('ast', { gte: '1.0.0' })
24
25
  expect(gem_spec).to be_match
25
26
  end
26
27
 
27
28
  it 'returns true if version in Gemfile.lock is equal to definition' do
28
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
29
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
29
30
  expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
30
31
  gem_spec = Rewriter::GemSpec.new('ast', '1.1.0')
31
32
  expect(gem_spec).to be_match
32
33
  end
33
34
 
34
35
  it 'returns false if version in Gemfile.lock is less than definition' do
35
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
36
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
36
37
  expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
37
- gem_spec = Rewriter::GemSpec.new('ast', {gt: '1.2.0'})
38
+ gem_spec = Rewriter::GemSpec.new('ast', { gt: '1.2.0' })
38
39
  expect(gem_spec).not_to be_match
39
40
  end
40
41
 
41
42
  it 'returns false if gem does not exist in Gemfile.lock' do
42
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(true)
43
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
43
44
  expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
44
45
  gem_spec = Rewriter::GemSpec.new('synvert', '1.0.0')
45
46
  expect(gem_spec).not_to be_match
46
47
  end
47
48
 
48
49
  it 'raise Synvert::Core::GemfileLockNotFound if Gemfile.lock does not exist' do
49
- expect(File).to receive(:exists?).with('./Gemfile.lock').and_return(false)
50
+ expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(false)
50
51
  gem_spec = Rewriter::GemSpec.new('ast', '1.1.0')
51
- expect { gem_spec.match? }.to raise_error(Synvert::Core::GemfileLockNotFound)
52
+ expect(gem_spec).to be_match
52
53
  end
53
54
  end
54
55
  end
@@ -7,74 +7,77 @@ module Synvert::Core
7
7
  let(:dummy_instance) { Class.new { include Rewriter::Helper }.new }
8
8
  let(:instance) do
9
9
  rewriter = Rewriter.new('foo', 'bar')
10
- Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do; end
10
+ Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
11
+ end
11
12
  end
12
13
 
13
- describe "add_receiver_if_necessary" do
14
- context "with receiver" do
15
- let(:node) { parse("User.save(false)") }
14
+ describe 'add_receiver_if_necessary' do
15
+ context 'with receiver' do
16
+ let(:node) { parse('User.save(false)') }
16
17
 
17
- it "adds reciever" do
18
+ it 'adds reciever' do
18
19
  allow(dummy_instance).to receive(:node).and_return(node)
19
- expect(dummy_instance.add_receiver_if_necessary("save(validate: false)")).to eq "{{receiver}}.save(validate: false)"
20
+ expect(
21
+ dummy_instance.add_receiver_if_necessary('save(validate: false)')
22
+ ).to eq '{{receiver}}.save(validate: false)'
20
23
  end
21
24
  end
22
25
 
23
- context "without receiver" do
24
- let(:node) { parse("save(false)") }
26
+ context 'without receiver' do
27
+ let(:node) { parse('save(false)') }
25
28
 
26
29
  it "doesn't add reciever" do
27
30
  allow(dummy_instance).to receive(:node).and_return(node)
28
- expect(dummy_instance.add_receiver_if_necessary("save(validate: false)")).to eq "save(validate: false)"
31
+ expect(dummy_instance.add_receiver_if_necessary('save(validate: false)')).to eq 'save(validate: false)'
29
32
  end
30
33
  end
31
34
  end
32
35
 
33
- describe "add_arguments_with_parenthesis_if_necessary" do
34
- context "with arguments" do
35
- let(:node) { parse("user.save(false)") }
36
+ describe 'add_arguments_with_parenthesis_if_necessary' do
37
+ context 'with arguments' do
38
+ let(:node) { parse('user.save(false)') }
36
39
 
37
- it "gets arguments with parenthesis" do
40
+ it 'gets arguments with parenthesis' do
38
41
  allow(dummy_instance).to receive(:node).and_return(node)
39
- expect(dummy_instance.add_arguments_with_parenthesis_if_necessary).to eq "({{arguments}})"
42
+ expect(dummy_instance.add_arguments_with_parenthesis_if_necessary).to eq '({{arguments}})'
40
43
  end
41
44
  end
42
45
 
43
- context "without argument" do
44
- let(:node) { parse("user.save") }
46
+ context 'without argument' do
47
+ let(:node) { parse('user.save') }
45
48
 
46
- it "gets nothing" do
49
+ it 'gets nothing' do
47
50
  allow(dummy_instance).to receive(:node).and_return(node)
48
- expect(dummy_instance.add_arguments_with_parenthesis_if_necessary).to eq ""
51
+ expect(dummy_instance.add_arguments_with_parenthesis_if_necessary).to eq ''
49
52
  end
50
53
  end
51
54
  end
52
55
 
53
- describe "add_curly_brackets_if_necessary" do
54
- it "add {} if code does not have" do
55
- expect(dummy_instance.add_curly_brackets_if_necessary("foo: bar")).to eq "{ foo: bar }"
56
+ describe 'add_curly_brackets_if_necessary' do
57
+ it 'add {} if code does not have' do
58
+ expect(dummy_instance.add_curly_brackets_if_necessary('foo: bar')).to eq '{ foo: bar }'
56
59
  end
57
60
 
58
61
  it "doesn't add {} if code already has" do
59
- expect(dummy_instance.add_curly_brackets_if_necessary("{foo: bar}")).to eq "{foo: bar}"
62
+ expect(dummy_instance.add_curly_brackets_if_necessary('{foo: bar}')).to eq '{foo: bar}'
60
63
  end
61
64
  end
62
65
 
63
- describe "strip_brackets" do
64
- it "strip ()" do
65
- expect(dummy_instance.strip_brackets("(123)")).to eq "123"
66
+ describe 'strip_brackets' do
67
+ it 'strip ()' do
68
+ expect(dummy_instance.strip_brackets('(123)')).to eq '123'
66
69
  end
67
70
 
68
- it "strip {}" do
69
- expect(dummy_instance.strip_brackets("{123}")).to eq "123"
71
+ it 'strip {}' do
72
+ expect(dummy_instance.strip_brackets('{123}')).to eq '123'
70
73
  end
71
74
 
72
- it "strip []" do
73
- expect(dummy_instance.strip_brackets("[123]")).to eq "123"
75
+ it 'strip []' do
76
+ expect(dummy_instance.strip_brackets('[123]')).to eq '123'
74
77
  end
75
78
 
76
- it "not strip unmatched (]" do
77
- expect(dummy_instance.strip_brackets("(123]")).to eq "(123]"
79
+ it 'not strip unmatched (]' do
80
+ expect(dummy_instance.strip_brackets('(123]')).to eq '(123]'
78
81
  end
79
82
  end
80
83
  end
@@ -12,82 +12,104 @@ module Synvert::Core
12
12
  }
13
13
 
14
14
  it 'parses within_node' do
15
- scope = double()
16
- block = Proc.new {}
17
- expect(Rewriter::WithinScope).to receive(:new).with(instance, { type: 'send', message: 'create' }, { recursive: true }, &block).and_return(scope)
15
+ scope = double
16
+ block = proc {}
17
+ expect(Rewriter::WithinScope).to receive(:new)
18
+ .with(instance, { type: 'send', message: 'create' }, { recursive: true }, &block)
19
+ .and_return(scope)
18
20
  expect(scope).to receive(:process)
19
21
  instance.within_node(type: 'send', message: 'create', &block)
20
22
  end
21
23
 
22
24
  it 'parses with_node' do
23
- scope = double()
24
- block = Proc.new {}
25
- expect(Rewriter::WithinScope).to receive(:new).with(instance, { type: 'send', message: 'create' }, { recursive: true }, &block).and_return(scope)
25
+ scope = double
26
+ block = proc {}
27
+ expect(Rewriter::WithinScope).to receive(:new)
28
+ .with(instance, { type: 'send', message: 'create' }, { recursive: true }, &block)
29
+ .and_return(scope)
26
30
  expect(scope).to receive(:process)
27
31
  instance.with_node(type: 'send', message: 'create', &block)
28
32
  end
29
33
 
30
34
  it 'parses within_direct_node' do
31
- scope = double()
32
- block = Proc.new {}
33
- expect(Rewriter::WithinScope).to receive(:new).with(instance, { type: 'send', message: 'create' }, { recursive: false }, &block).and_return(scope)
35
+ scope = double
36
+ block = proc {}
37
+ expect(Rewriter::WithinScope).to receive(:new)
38
+ .with(instance, { type: 'send', message: 'create' }, { recursive: false }, &block)
39
+ .and_return(scope)
34
40
  expect(scope).to receive(:process)
35
41
  instance.within_direct_node(type: 'send', message: 'create', &block)
36
42
  end
37
43
 
38
44
  it 'parses with_direct_node' do
39
- scope = double()
40
- block = Proc.new {}
41
- expect(Rewriter::WithinScope).to receive(:new).with(instance, { type: 'send', message: 'create' }, { recursive: false }, &block).and_return(scope)
45
+ scope = double
46
+ block = proc {}
47
+ expect(Rewriter::WithinScope).to receive(:new)
48
+ .with(instance, { type: 'send', message: 'create' }, { recursive: false }, &block)
49
+ .and_return(scope)
42
50
  expect(scope).to receive(:process)
43
51
  instance.with_direct_node(type: 'send', message: 'create', &block)
44
52
  end
45
53
 
46
54
  it 'parses goto_node' do
47
- scope = double()
48
- block = Proc.new {}
55
+ scope = double
56
+ block = proc {}
49
57
  expect(Rewriter::GotoScope).to receive(:new).with(instance, :caller, &block).and_return(scope)
50
58
  expect(scope).to receive(:process)
51
59
  instance.goto_node(:caller, &block)
52
60
  end
53
61
 
54
62
  it 'parses if_exist_node' do
55
- condition = double()
56
- block = Proc.new {}
57
- expect(Rewriter::IfExistCondition).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(condition)
63
+ condition = double
64
+ block = proc {}
65
+ expect(Rewriter::IfExistCondition).to receive(:new)
66
+ .with(instance, type: 'send', message: 'create', &block)
67
+ .and_return(condition)
58
68
  expect(condition).to receive(:process)
59
69
  instance.if_exist_node(type: 'send', message: 'create', &block)
60
70
  end
61
71
 
62
72
  it 'parses unless_exist_node' do
63
- condition = double()
64
- block = Proc.new {}
65
- expect(Rewriter::UnlessExistCondition).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(condition)
73
+ condition = double
74
+ block = proc {}
75
+ expect(Rewriter::UnlessExistCondition).to receive(:new)
76
+ .with(instance, type: 'send', message: 'create', &block)
77
+ .and_return(condition)
66
78
  expect(condition).to receive(:process)
67
79
  instance.unless_exist_node(type: 'send', message: 'create', &block)
68
80
  end
69
81
 
70
82
  it 'parses if_only_exist_node' do
71
- condition = double()
72
- block = Proc.new {}
73
- expect(Rewriter::IfOnlyExistCondition).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(condition)
83
+ condition = double
84
+ block = proc {}
85
+ expect(Rewriter::IfOnlyExistCondition).to receive(:new)
86
+ .with(instance, type: 'send', message: 'create', &block)
87
+ .and_return(condition)
74
88
  expect(condition).to receive(:process)
75
89
  instance.if_only_exist_node(type: 'send', message: 'create', &block)
76
90
  end
77
91
 
78
92
  it 'parses append' do
79
93
  expect(Rewriter::AppendAction).to receive(:new).with(instance, 'include FactoryGirl::Syntax::Methods', {})
80
- instance.append "include FactoryGirl::Syntax::Methods"
94
+ instance.append 'include FactoryGirl::Syntax::Methods'
81
95
  end
82
96
 
83
97
  it 'parses insert' do
84
- expect(Rewriter::InsertAction).to receive(:new).with(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods', {})
85
- instance.insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
98
+ expect(Rewriter::InsertAction).to receive(:new).with(
99
+ instance,
100
+ '{{arguments.first}}.include FactoryGirl::Syntax::Methods',
101
+ {}
102
+ )
103
+ instance.insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
86
104
  end
87
105
 
88
106
  it 'parses insert_after' do
89
- expect(Rewriter::InsertAfterAction).to receive(:new).with(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods', {})
90
- instance.insert_after "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
107
+ expect(Rewriter::InsertAfterAction).to receive(:new).with(
108
+ instance,
109
+ '{{arguments.first}}.include FactoryGirl::Syntax::Methods',
110
+ {}
111
+ )
112
+ instance.insert_after '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
91
113
  end
92
114
 
93
115
  it 'parses replace_with' do
@@ -109,25 +131,28 @@ module Synvert::Core
109
131
  let(:rewriter) { Rewriter.new('foo', 'bar') }
110
132
 
111
133
  it 'writes new code to file' do
112
- instance = Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
113
- with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
114
- replace_with 'create {{arguments}}'
134
+ instance =
135
+ Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
136
+ with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
137
+ replace_with 'create {{arguments}}'
138
+ end
115
139
  end
116
- end
117
- input = """
140
+ input =
141
+ "
118
142
  it 'uses factory_girl' do
119
143
  user = FactoryGirl.create :user
120
144
  post = FactoryGirl.create :post, user: user
121
145
  assert post.valid?
122
146
  end
123
- """
124
- output = """
147
+ "
148
+ output =
149
+ "
125
150
  it 'uses factory_girl' do
126
151
  user = create :user
127
152
  post = create :post, user: user
128
153
  assert post.valid?
129
154
  end
130
- """
155
+ "
131
156
  expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
132
157
  expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
133
158
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
@@ -135,23 +160,26 @@ end
135
160
  end
136
161
 
137
162
  it 'does not write if file content is not changed' do
138
- instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
139
- with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
140
- unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
141
- insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
163
+ instance =
164
+ Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
165
+ with_node type: 'block', caller: { receiver: 'RSpec', message: 'configure' } do
166
+ unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
167
+ insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
168
+ end
142
169
  end
143
170
  end
144
- end
145
- input = """
171
+ input =
172
+ '
146
173
  RSpec.configure do |config|
147
174
  config.include FactoryGirl::Syntax::Methods
148
175
  end
149
- """
150
- output = """
176
+ '
177
+ output =
178
+ '
151
179
  RSpec.configure do |config|
152
180
  config.include FactoryGirl::Syntax::Methods
153
181
  end
154
- """
182
+ '
155
183
  expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
156
184
  expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input)
157
185
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
@@ -159,23 +187,26 @@ end
159
187
  end
160
188
 
161
189
  it 'does not read file if already read' do
162
- instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
163
- with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
164
- unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
165
- insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
190
+ instance =
191
+ Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
192
+ with_node type: 'block', caller: { receiver: 'RSpec', message: 'configure' } do
193
+ unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
194
+ insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
195
+ end
166
196
  end
167
197
  end
168
- end
169
- input = """
198
+ input =
199
+ '
170
200
  RSpec.configure do |config|
171
201
  config.include FactoryGirl::Syntax::Methods
172
202
  end
173
- """
174
- output = """
203
+ '
204
+ output =
205
+ '
175
206
  RSpec.configure do |config|
176
207
  config.include FactoryGirl::Syntax::Methods
177
208
  end
178
- """
209
+ '
179
210
  expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb']).twice
180
211
  expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input).once
181
212
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
@@ -184,25 +215,28 @@ end
184
215
  end
185
216
 
186
217
  it 'updates file_source and file_ast when writing a file' do
187
- instance = Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
188
- with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
189
- replace_with 'create {{arguments}}'
218
+ instance =
219
+ Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
220
+ with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
221
+ replace_with 'create {{arguments}}'
222
+ end
190
223
  end
191
- end
192
- input = """
224
+ input =
225
+ "
193
226
  it 'uses factory_girl' do
194
227
  user = FactoryGirl.create :user
195
228
  post = FactoryGirl.create :post, user: user
196
229
  assert post.valid?
197
230
  end
198
- """
199
- output = """
231
+ "
232
+ output =
233
+ "
200
234
  it 'uses factory_girl' do
201
235
  user = create :user
202
236
  post = create :post, user: user
203
237
  assert post.valid?
204
238
  end
205
- """
239
+ "
206
240
  expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
207
241
  expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
208
242
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
@@ -212,10 +246,10 @@ end
212
246
  end
213
247
  end
214
248
 
215
- describe "#get_conflict_actions" do
249
+ describe '#get_conflict_actions' do
216
250
  let(:rewriter) { Rewriter.new('foo', 'bar') }
217
251
 
218
- it "has no conflict" do
252
+ it 'has no conflict' do
219
253
  action1 = double(begin_pos: 10, end_pos: 20)
220
254
  action2 = double(begin_pos: 30, end_pos: 40)
221
255
  action3 = double(begin_pos: 50, end_pos: 60)
@@ -226,7 +260,7 @@ end
226
260
  expect(instance.instance_variable_get(:@actions)).to eq [action1, action2, action3]
227
261
  end
228
262
 
229
- it "has no conflict" do
263
+ it 'has no conflict' do
230
264
  action1 = double(begin_pos: 30, end_pos: 40)
231
265
  action2 = double(begin_pos: 50, end_pos: 60)
232
266
  action3 = double(begin_pos: 10, end_pos: 20)
@@ -240,8 +274,8 @@ end
240
274
 
241
275
  describe '#process_with_node' do
242
276
  it 'resets current_node' do
243
- node1 = double()
244
- node2 = double()
277
+ node1 = double
278
+ node2 = double
245
279
  instance.process_with_node(node1) do
246
280
  instance.current_node = node2
247
281
  expect(instance.current_node).to eq node2
@@ -252,9 +286,9 @@ end
252
286
 
253
287
  describe '#process_with_other_node' do
254
288
  it 'resets current_node' do
255
- node1 = double()
256
- node2 = double()
257
- node3 = double()
289
+ node1 = double
290
+ node2 = double
291
+ node3 = double
258
292
  instance.current_node = node1
259
293
  instance.process_with_other_node(node2) do
260
294
  instance.current_node = node3