synvert 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,10 +7,11 @@ module Synvert
7
7
  expect(rewriter.description).to eq 'this is description'
8
8
  end
9
9
 
10
- it 'parses gem_spec' do
11
- expect(Rewriter::GemSpec).to receive(:new).with('synvert', '1.0.0')
12
- Rewriter.new 'description' do
13
- gem_spec 'synvert', '1.0.0'
10
+ describe 'get / set' do
11
+ it 'set and get parameters' do
12
+ rewriter = Rewriter.new 'this is description' do; end
13
+ rewriter.set 'parameter', 'post', ':title, :description'
14
+ expect(rewriter.get 'parameter', 'post').to eq ':title, :description'
14
15
  end
15
16
  end
16
17
 
@@ -0,0 +1,154 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'FactoryGirl uses short synax' do
4
+ before do
5
+ Synvert::Configuration.instance.set :path, '.'
6
+ allow_any_instance_of(Synvert::Rewriter::GemSpec).to receive(:match?).and_return(true)
7
+ rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/factory_girl/syntax_methods.rb')
8
+ @rewriter = eval(File.read(rewriter_path))
9
+ end
10
+
11
+ describe 'rspec', fakefs: true do
12
+ let(:spec_helper_content) {'''
13
+ RSpec.configure do |config|
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ end
17
+ '''}
18
+ let(:spec_helper_rewritten_content) {'''
19
+ RSpec.configure do |config|
20
+ config.include FactoryGirl::Syntax::Methods
21
+ config.treat_symbols_as_metadata_keys_with_true_values = true
22
+ config.run_all_when_everything_filtered = true
23
+ end
24
+ '''}
25
+ let(:post_spec_content) {'''
26
+ describe Post do
27
+ it "tests post" do
28
+ post1 = FactoryGirl.create(:post)
29
+ post2 = FactoryGirl.build(:post)
30
+ post_attributes = FactoryGirl.attributes_for(:post)
31
+ post3 = FactoryGirl.build_stubbed(:post)
32
+ posts1 = FactoryGirl.create_list(:post, 2)
33
+ posts2 = FactoryGirl.build_list(:post, 2)
34
+ posts3 = FactoryGirl.create_pair(:post)
35
+ posts4 = FactoryGirl.build_pair(:post)
36
+ end
37
+ end
38
+ '''}
39
+ let(:post_spec_rewritten_content) {'''
40
+ describe Post do
41
+ it "tests post" do
42
+ post1 = create(:post)
43
+ post2 = build(:post)
44
+ post_attributes = attributes_for(:post)
45
+ post3 = build_stubbed(:post)
46
+ posts1 = create_list(:post, 2)
47
+ posts2 = build_list(:post, 2)
48
+ posts3 = create_pair(:post)
49
+ posts4 = build_pair(:post)
50
+ end
51
+ end
52
+ '''}
53
+
54
+ it 'process' do
55
+ FileUtils.mkdir 'spec'
56
+ FileUtils.mkdir 'spec/models'
57
+ File.write 'spec/spec_helper.rb', spec_helper_content
58
+ File.write 'spec/models/post_spec.rb', post_spec_content
59
+ @rewriter.process
60
+ expect(File.read 'spec/spec_helper.rb').to eq spec_helper_rewritten_content
61
+ expect(File.read 'spec/models/post_spec.rb').to eq post_spec_rewritten_content
62
+ end
63
+ end
64
+
65
+ describe 'test/unit', fakefs: true do
66
+ let(:test_helper_content) {'''
67
+ class ActiveSupport::TestCase
68
+ end
69
+ '''}
70
+ let(:test_helper_rewritten_content) {'''
71
+ class ActiveSupport::TestCase
72
+ include FactoryGirl::Syntax::Methods
73
+ end
74
+ '''}
75
+ let(:post_test_content) {'''
76
+ test "post" do
77
+ post1 = FactoryGirl.create(:post)
78
+ post2 = FactoryGirl.build(:post)
79
+ post_attributes = FactoryGirl.attributes_for(:post)
80
+ post3 = FactoryGirl.build_stubbed(:post)
81
+ posts1 = FactoryGirl.create_list(:post, 2)
82
+ posts2 = FactoryGirl.build_list(:post, 2)
83
+ posts3 = FactoryGirl.create_pair(:post)
84
+ posts4 = FactoryGirl.build_pair(:post)
85
+ end
86
+ '''}
87
+ let(:post_test_rewritten_content) {'''
88
+ test "post" do
89
+ post1 = create(:post)
90
+ post2 = build(:post)
91
+ post_attributes = attributes_for(:post)
92
+ post3 = build_stubbed(:post)
93
+ posts1 = create_list(:post, 2)
94
+ posts2 = build_list(:post, 2)
95
+ posts3 = create_pair(:post)
96
+ posts4 = build_pair(:post)
97
+ end
98
+ '''}
99
+
100
+ it 'process' do
101
+ FileUtils.mkdir 'test'
102
+ FileUtils.mkdir 'test/unit'
103
+ File.write 'test/test_helper.rb', test_helper_content
104
+ File.write 'test/unit/post_test.rb', post_test_content
105
+ @rewriter.process
106
+ expect(File.read 'test/test_helper.rb').to eq test_helper_rewritten_content
107
+ expect(File.read 'test/unit/post_test.rb').to eq post_test_rewritten_content
108
+ end
109
+ end
110
+
111
+ describe 'cucumber', fakefs: true do
112
+ let(:env_content) {'''
113
+ require "cucumber/rails"
114
+ '''}
115
+ let(:env_rewritten_content) {'''
116
+ require "cucumber/rails"
117
+ World(FactoryGirl::Syntax::Methods)
118
+ '''}
119
+ let(:post_steps_content) {'''
120
+ test "post" do
121
+ post1 = FactoryGirl.create(:post)
122
+ post2 = FactoryGirl.build(:post)
123
+ post_attributes = FactoryGirl.attributes_for(:post)
124
+ post3 = FactoryGirl.build_stubbed(:post)
125
+ posts1 = FactoryGirl.create_list(:post, 2)
126
+ posts2 = FactoryGirl.build_list(:post, 2)
127
+ posts3 = FactoryGirl.create_pair(:post)
128
+ posts4 = FactoryGirl.build_pair(:post)
129
+ end
130
+ '''}
131
+ let(:post_steps_rewritten_content) {'''
132
+ test "post" do
133
+ post1 = create(:post)
134
+ post2 = build(:post)
135
+ post_attributes = attributes_for(:post)
136
+ post3 = build_stubbed(:post)
137
+ posts1 = create_list(:post, 2)
138
+ posts2 = build_list(:post, 2)
139
+ posts3 = create_pair(:post)
140
+ posts4 = build_pair(:post)
141
+ end
142
+ '''}
143
+
144
+ it 'process' do
145
+ FileUtils.mkdir_p 'features/support'
146
+ FileUtils.mkdir_p 'features/step_definitions'
147
+ File.write 'features/support/env.rb', env_content
148
+ File.write 'features/step_definitions/post_steps.rb', post_steps_content
149
+ @rewriter.process
150
+ expect(File.read 'features/support/env.rb').to eq env_rewritten_content
151
+ expect(File.read 'features/step_definitions/post_steps.rb').to eq post_steps_rewritten_content
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,268 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Upgrade rails from 3.2 to 4.0' do
4
+ before do
5
+ Synvert::Configuration.instance.set :path, '.'
6
+ rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/rails/upgrade_3_2_to_4_0.rb')
7
+ @rewriter = eval(File.read(rewriter_path))
8
+ allow_any_instance_of(Synvert::Rewriter::GemSpec).to receive(:match?).and_return(true)
9
+ expect(SecureRandom).to receive(:hex).with(64).and_return("bf4f3f46924ecd9adcb6515681c78144545bba454420973a274d7021ff946b8ef043a95ca1a15a9d1b75f9fbdf85d1a3afaf22f4e3c2f3f78e24a0a188b581df")
10
+ end
11
+
12
+ describe 'with fakefs', fakefs: true do
13
+ let(:application_content) {'''
14
+ if defined?(Bundler)
15
+ Bundler.require(*Rails.groups(:assets => %w(development test)))
16
+ end
17
+ module Synvert
18
+ class Application < Rails::Application
19
+ config.active_record.whitelist_attributes = true
20
+ config.middleware.insert_before(Rack::Lock, ActionDispatch::BestStandardsSupport)
21
+ end
22
+ end
23
+ '''}
24
+ let(:application_rewritten_content) {'''
25
+ Bundler.require(:default, Rails.env)
26
+ module Synvert
27
+ class Application < Rails::Application
28
+ end
29
+ end
30
+ '''}
31
+ let(:production_content) {'''
32
+ Synvert::Application.configure do
33
+ config.cache_classes = true
34
+ config.active_record.identity_map = true
35
+ config.action_dispatch.best_standards_support = :builtin
36
+
37
+ ActionController::Base.page_cache_extension = "html"
38
+ end
39
+ '''}
40
+ let(:production_rewritten_content) {'''
41
+ Synvert::Application.configure do
42
+ config.eager_load = true
43
+ config.cache_classes = true
44
+
45
+ ActionController::Base.default_static_extension = "html"
46
+ end
47
+ '''}
48
+ let(:development_content) {'''
49
+ Synvert::Application.configure do
50
+ config.cache_classes = false
51
+ end
52
+ '''}
53
+ let(:development_rewritten_content) {'''
54
+ Synvert::Application.configure do
55
+ config.eager_load = false
56
+ config.cache_classes = false
57
+ end
58
+ '''}
59
+ let(:test_content) {'''
60
+ Synvert::Application.configure do
61
+ config.cache_classes = false
62
+ end
63
+ '''}
64
+ let(:test_rewritten_content) {'''
65
+ Synvert::Application.configure do
66
+ config.eager_load = false
67
+ config.cache_classes = false
68
+ end
69
+ '''}
70
+ let(:wrap_parameters_content) {'''
71
+ ActiveSupport.on_load(:action_controller) do
72
+ wrap_parameters format: [:json]
73
+ end
74
+ ActiveSupport.on_load(:active_record) do
75
+ self.include_root_in_json = false
76
+ end
77
+ '''}
78
+ let(:wrap_parameters_rewritten_content) {'''
79
+ ActiveSupport.on_load(:action_controller) do
80
+ wrap_parameters format: [:json]
81
+ end
82
+ '''}
83
+ let(:secret_token_content) {'''
84
+ Synvert::Application.config.secret_token = "0447aa931d42918bfb934750bb78257088fb671186b5d1b6f9fddf126fc8a14d34f1d045cefab3900751c3da121a8dd929aec9bafe975f1cabb48232b4002e4e"
85
+ '''}
86
+ let(:secret_token_rewritten_content) {'''
87
+ Synvert::Application.config.secret_token = "0447aa931d42918bfb934750bb78257088fb671186b5d1b6f9fddf126fc8a14d34f1d045cefab3900751c3da121a8dd929aec9bafe975f1cabb48232b4002e4e"
88
+ Synvert::Application.config.secret_key_base = "bf4f3f46924ecd9adcb6515681c78144545bba454420973a274d7021ff946b8ef043a95ca1a15a9d1b75f9fbdf85d1a3afaf22f4e3c2f3f78e24a0a188b581df"
89
+ '''}
90
+ let(:routes_content) {"""
91
+ Synvert::Application.routes.draw do
92
+ get Rack::Utils.escape('こんにちは'), controller: 'welcome', action: 'index'
93
+ match '/' => 'root#index'
94
+ match 'new', to: 'episodes#new'
95
+ end
96
+ """}
97
+ let(:routes_rewritten_content) {"""
98
+ Synvert::Application.routes.draw do
99
+ get 'こんにちは', controller: 'welcome', action: 'index'
100
+ get '/' => 'root#index'
101
+ get 'new', to: 'episodes#new'
102
+ end
103
+ """}
104
+ let(:post_model_content) {'''
105
+ class Post < ActiveRecord::Base
106
+ scope :active, where(active: true)
107
+
108
+ attr_accessible :title, :description
109
+
110
+ def serialized_attrs
111
+ self.serialized_attributes
112
+ end
113
+
114
+ def active_users_by_email(email)
115
+ User.find_all_by_email_and_active(email, true)
116
+ end
117
+
118
+ def first_active_user_by_email(email)
119
+ User.find_by_email_and_active(email, true)
120
+ end
121
+
122
+ def last_active_user_by_email(email)
123
+ User.find_last_by_email_and_active(email, true)
124
+ end
125
+
126
+ def scoped_active_user_by_email(email)
127
+ User.scoped_by_email_and_active(email, true)
128
+ end
129
+ end
130
+ '''}
131
+ let(:post_model_rewritten_content) {'''
132
+ class Post < ActiveRecord::Base
133
+ scope :active, -> { where(active: true) }
134
+
135
+ def serialized_attrs
136
+ self.class.serialized_attributes
137
+ end
138
+
139
+ def active_users_by_email(email)
140
+ User.where(email: email, active: true)
141
+ end
142
+
143
+ def first_active_user_by_email(email)
144
+ User.where(email: email, active: true).first
145
+ end
146
+
147
+ def last_active_user_by_email(email)
148
+ User.where(email: email, active: true).last
149
+ end
150
+
151
+ def scoped_active_user_by_email(email)
152
+ User.where(email: email, active: true)
153
+ end
154
+ end
155
+ '''}
156
+ let(:users_controller_content) {'''
157
+ class UsersController < ApplicationController
158
+ def new
159
+ @user = User.find_or_initialize_by_login_and_email(params[:user][:login], params[:user][:email])
160
+ end
161
+
162
+ def create
163
+ @user = User.find_or_create_by_login_and_email(params[:user][:login], params[:user][:email])
164
+ end
165
+ end
166
+ '''}
167
+ let(:users_controller_rewritten_content) {'''
168
+ class UsersController < ApplicationController
169
+ def new
170
+ @user = User.find_or_initialize_by(login: params[:user][:login], email: params[:user][:email])
171
+ end
172
+
173
+ def create
174
+ @user = User.find_or_create_by(login: params[:user][:login], email: params[:user][:email])
175
+ end
176
+ end
177
+ '''}
178
+ let(:posts_controller_content) {'''
179
+ class PostsController < ApplicationController
180
+ def update
181
+ @post = Post.find(params[:id])
182
+ if @post.update_attributes params[:post]
183
+ redirect_to post_path(@post)
184
+ else
185
+ render :action => :edit
186
+ end
187
+ end
188
+ end
189
+ '''}
190
+ let(:posts_controller_rewritten_content) {'''
191
+ class PostsController < ApplicationController
192
+ def update
193
+ @post = Post.find(params[:id])
194
+ if @post.update_attributes post_params
195
+ redirect_to post_path(@post)
196
+ else
197
+ render :action => :edit
198
+ end
199
+ end
200
+
201
+ def post_params
202
+ params.require(:post).permit(:title, :description)
203
+ end
204
+ end
205
+ '''}
206
+ let(:post_test_content) {'''
207
+ require "test_helper"
208
+
209
+ class PostTest < ActiveRecord::TestCase
210
+ end
211
+ '''}
212
+ let(:post_test_rewritten_content) {'''
213
+ require "test_helper"
214
+
215
+ class PostTest < ActiveSupport::TestCase
216
+ end
217
+ '''}
218
+ let(:test_helper_content) {'''
219
+ class ActiveSupport::TestCase
220
+ def constants
221
+ [ActionController::Integration, ActionController::IntegrationTest, ActionController::PerformanceTest, ActionController::AbstractRequest,
222
+ ActionController::Request, ActionController::AbstractResponse, ActionController::Response, ActionController::Routing]
223
+ end
224
+ end
225
+ '''}
226
+ let(:test_helper_rewritten_content) {'''
227
+ class ActiveSupport::TestCase
228
+ def constants
229
+ [ActionDispatch::Integration, ActionDispatch::IntegrationTest, ActionDispatch::PerformanceTest, ActionDispatch::Request,
230
+ ActionDispatch::Request, ActionDispatch::Response, ActionDispatch::Response, ActionDispatch::Routing]
231
+ end
232
+ end
233
+ '''}
234
+
235
+ it 'process' do
236
+ FileUtils.mkdir_p 'config/environments'
237
+ FileUtils.mkdir_p 'config/initializers'
238
+ FileUtils.mkdir_p 'app/models'
239
+ FileUtils.mkdir_p 'app/controllers'
240
+ FileUtils.mkdir_p 'test/unit'
241
+ File.write 'config/application.rb', application_content
242
+ File.write 'config/environments/production.rb', production_content
243
+ File.write 'config/environments/development.rb', development_content
244
+ File.write 'config/environments/test.rb', test_content
245
+ File.write 'config/initializers/wrap_parameters.rb', wrap_parameters_content
246
+ File.write 'config/initializers/secret_token.rb', secret_token_content
247
+ File.write 'config/routes.rb', routes_content
248
+ File.write 'app/models/post.rb', post_model_content
249
+ File.write 'app/controllers/users_controller.rb', users_controller_content
250
+ File.write 'app/controllers/posts_controller.rb', posts_controller_content
251
+ File.write 'test/unit/post_test.rb', post_test_content
252
+ File.write 'test/test_helper.rb', test_helper_content
253
+ @rewriter.process
254
+ expect(File.read 'config/application.rb').to eq application_rewritten_content
255
+ expect(File.read 'config/environments/production.rb').to eq production_rewritten_content
256
+ expect(File.read 'config/environments/development.rb').to eq development_rewritten_content
257
+ expect(File.read 'config/environments/test.rb').to eq test_rewritten_content
258
+ expect(File.read 'config/initializers/wrap_parameters.rb').to eq wrap_parameters_rewritten_content
259
+ expect(File.read 'config/initializers/secret_token.rb').to eq secret_token_rewritten_content
260
+ expect(File.read 'config/routes.rb').to eq routes_rewritten_content
261
+ expect(File.read 'app/models/post.rb').to eq post_model_rewritten_content
262
+ expect(File.read 'app/controllers/users_controller.rb').to eq users_controller_rewritten_content
263
+ expect(File.read 'app/controllers/posts_controller.rb').to eq posts_controller_rewritten_content
264
+ expect(File.read 'test/unit/post_test.rb').to eq post_test_rewritten_content
265
+ expect(File.read 'test/test_helper.rb').to eq test_helper_rewritten_content
266
+ end
267
+ end
268
+ end
data/synvert.gemspec CHANGED
@@ -19,8 +19,10 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_runtime_dependency "parser"
22
+ spec.add_runtime_dependency "activesupport"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.3"
24
25
  spec.add_development_dependency "rake"
25
26
  spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "fakefs"
26
28
  end