synvert-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ module Synvert::Core
4
+ describe Rewriter::Scope do
5
+ let(:source) {
6
+ """
7
+ describe Post do
8
+ before :each do
9
+ @user = FactoryGirl.create :user
10
+ end
11
+
12
+ it 'gets posts' do
13
+ post1 = FactoryGirl.create :post
14
+ post2 = FactoryGirl.create :post
15
+ end
16
+ end
17
+ """
18
+ }
19
+ let(:node) { Parser::CurrentRuby.parse(source) }
20
+ let(:instance) { double(:current_node => node, :current_node= => node, :current_source => source) }
21
+
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
30
+ end
31
+
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
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,153 @@
1
+ require 'spec_helper'
2
+
3
+ module Synvert::Core
4
+ describe Rewriter do
5
+ it 'parses description' do
6
+ rewriter = Rewriter.new 'name' do
7
+ description 'rewriter description'
8
+ end
9
+ rewriter.process
10
+ expect(rewriter.description).to eq 'rewriter description'
11
+ end
12
+
13
+ it 'parses if_gem' do
14
+ expect(Rewriter::GemSpec).to receive(:new).with('synvert', {gte: '1.0.0'})
15
+ rewriter = Rewriter.new 'name' do
16
+ if_gem 'synvert', {gte: '1.0.0'}
17
+ end
18
+ rewriter.process
19
+ end
20
+
21
+ describe 'parses within_file' do
22
+ it 'does nothing if if_gem not match' do
23
+ expect_any_instance_of(Rewriter::GemSpec).to receive(:match?).and_return(false)
24
+ expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
25
+ rewriter = Rewriter.new 'name' do
26
+ if_gem 'synvert', '1.0.0'
27
+ within_file 'config/routes.rb' do; end
28
+ end
29
+ rewriter.process
30
+ end
31
+
32
+ it 'delegates process to instances if if_gem not exist' do
33
+ expect_any_instance_of(Rewriter::Instance).to receive(:process)
34
+ rewriter = Rewriter.new 'name' do
35
+ within_file 'config/routes.rb' do; end
36
+ end
37
+ rewriter.process
38
+ end
39
+
40
+ it 'delegates process to instances if if_gem matches' do
41
+ expect_any_instance_of(Rewriter::GemSpec).to receive(:match?).and_return(true)
42
+ expect_any_instance_of(Rewriter::Instance).to receive(:process)
43
+ rewriter = Rewriter.new 'name' do
44
+ if_gem 'synvert', '1.0.0'
45
+ within_file 'config/routes.rb' do; end
46
+ end
47
+ rewriter.process
48
+ end
49
+
50
+ it 'does nothing in sandbox mode' do
51
+ expect_any_instance_of(Rewriter::GemSpec).not_to receive(:match?)
52
+ expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
53
+ rewriter = Rewriter.new 'name' do
54
+ if_gem 'synvert', '1.0.0'
55
+ within_file 'config/routes.rb' do; end
56
+ end
57
+ rewriter.process_with_sandbox
58
+ end
59
+ end
60
+
61
+ describe 'parses add_file' do
62
+ it 'creates a new file' do
63
+ rewriter = Rewriter.new 'rewriter2' do
64
+ add_file './foo.bar', 'FooBar'
65
+ end
66
+ rewriter.process
67
+ expect(File.read './foo.bar').to eq 'FooBar'
68
+ FileUtils.rm './foo.bar'
69
+ end
70
+
71
+ it 'does nothing in sandbox mode' do
72
+ rewriter = Rewriter.new 'rewriter2' do
73
+ add_file './foo.bar', 'FooBar'
74
+ end
75
+ rewriter.process_with_sandbox
76
+ expect(File.exist?('./foo.bar')).to be_false
77
+ end
78
+ end
79
+
80
+ describe 'parses add_snippet' do
81
+ it 'processes the rewritter' do
82
+ rewriter1 = Rewriter.new 'rewriter1'
83
+ rewriter2 = Rewriter.new 'rewriter2' do
84
+ add_snippet :rewriter1
85
+ end
86
+ expect(rewriter1).to receive(:process)
87
+ rewriter2.process
88
+ end
89
+
90
+ it 'adds sub_snippets' do
91
+ rewriter1 = Rewriter.new 'rewriter1'
92
+ rewriter2 = Rewriter.new 'rewriter2' do
93
+ add_snippet :rewriter1
94
+ end
95
+ expect(rewriter1).to receive(:process)
96
+ rewriter2.process
97
+ expect(rewriter2.sub_snippets).to eq [rewriter1]
98
+ end
99
+
100
+ it 'raises RewriterNotFound' do
101
+ rewriter = Rewriter.new 'name' do
102
+ add_snippet :not_exist
103
+ end
104
+ expect { rewriter.process }.to raise_error(RewriterNotFound)
105
+ end
106
+ end
107
+
108
+ it 'parses helper_method' do
109
+ instance = double
110
+ expect(Rewriter::Instance).to receive(:new).and_return(instance)
111
+ expect(instance).to receive(:process)
112
+ rewriter = Rewriter.new 'name' do
113
+ helper_method 'dynamic_helper' do |arg1, arg2|
114
+ 'dynamic result'
115
+ end
116
+ within_file 'spec/spec_helper.rb' do; end
117
+ end
118
+ rewriter.process
119
+ expect(instance.dynamic_helper('arg1', 'arg2')).to eq 'dynamic result'
120
+ end
121
+
122
+ it 'parses todo' do
123
+ rewriter = Rewriter.new 'name' do
124
+ todo "this rewriter doesn't do blah blah blah"
125
+ end
126
+ rewriter.process
127
+ expect(rewriter.todo).to eq "this rewriter doesn't do blah blah blah"
128
+ end
129
+
130
+ describe 'class methods' do
131
+ before :each do
132
+ Rewriter.clear
133
+ end
134
+
135
+ it 'registers and fetches' do
136
+ rewriter = Rewriter.new 'rewriter'
137
+ expect(Rewriter.fetch('rewriter')).to eq rewriter
138
+ end
139
+
140
+ it 'registers and calls rewriter' do
141
+ rewriter = Rewriter.new 'rewriter'
142
+ expect(rewriter).to receive(:process)
143
+ Rewriter.call 'rewriter'
144
+ end
145
+
146
+ it 'registers and lists all available rewriters' do
147
+ rewriter1 = Rewriter.new 'rewriter1'
148
+ rewriter2 = Rewriter.new 'rewriter2'
149
+ expect(Rewriter.availables).to eq [rewriter1, rewriter2]
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'synvert/core/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "synvert-core"
8
+ spec.version = Synvert::Core::VERSION
9
+ spec.authors = ["Richard Huang"]
10
+ spec.email = ["flyerhzm@gmail.com"]
11
+ spec.summary = %q{convert ruby code to better syntax.}
12
+ spec.description = %q{convert ruby code to better syntax automatically.}
13
+ spec.homepage = "https://github.com/xinminlabs/synvert-core"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "parser"
22
+ spec.add_runtime_dependency "activesupport"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: synvert-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Huang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: convert ruby code to better syntax automatically.
84
+ email:
85
+ - flyerhzm@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CHANGELOG.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lib/synvert/core.rb
99
+ - lib/synvert/core/cli.rb
100
+ - lib/synvert/core/configuration.rb
101
+ - lib/synvert/core/exceptions.rb
102
+ - lib/synvert/core/node_ext.rb
103
+ - lib/synvert/core/rewriter.rb
104
+ - lib/synvert/core/rewriter/action.rb
105
+ - lib/synvert/core/rewriter/condition.rb
106
+ - lib/synvert/core/rewriter/gem_spec.rb
107
+ - lib/synvert/core/rewriter/instance.rb
108
+ - lib/synvert/core/rewriter/scope.rb
109
+ - lib/synvert/core/version.rb
110
+ - spec/spec_helper.rb
111
+ - spec/support/parser_helper.rb
112
+ - spec/synvert/core/node_ext_spec.rb
113
+ - spec/synvert/core/rewriter/action_spec.rb
114
+ - spec/synvert/core/rewriter/condition_spec.rb
115
+ - spec/synvert/core/rewriter/gem_spec_spec.rb
116
+ - spec/synvert/core/rewriter/instance_spec.rb
117
+ - spec/synvert/core/rewriter/scope_spec.rb
118
+ - spec/synvert/core/rewriter_spec.rb
119
+ - synvert-core.gemspec
120
+ homepage: https://github.com/xinminlabs/synvert-core
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: convert ruby code to better syntax.
144
+ test_files:
145
+ - spec/spec_helper.rb
146
+ - spec/support/parser_helper.rb
147
+ - spec/synvert/core/node_ext_spec.rb
148
+ - spec/synvert/core/rewriter/action_spec.rb
149
+ - spec/synvert/core/rewriter/condition_spec.rb
150
+ - spec/synvert/core/rewriter/gem_spec_spec.rb
151
+ - spec/synvert/core/rewriter/instance_spec.rb
152
+ - spec/synvert/core/rewriter/scope_spec.rb
153
+ - spec/synvert/core/rewriter_spec.rb