rspec-kickstarter 0.1.3 → 0.2.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.
- data/bin/rspec-kickstarter +23 -3
- data/lib/rspec_kickstarter/generator.rb +129 -49
- data/lib/rspec_kickstarter/version.rb +1 -1
- metadata +1 -1
data/bin/rspec-kickstarter
CHANGED
@@ -8,12 +8,22 @@ require 'rspec_kickstarter/generator'
|
|
8
8
|
|
9
9
|
force_write = false
|
10
10
|
dry_run = false
|
11
|
+
rails_mode = false
|
11
12
|
spec_dir = './spec'
|
13
|
+
delta_path = nil
|
14
|
+
full_path = nil
|
12
15
|
|
13
16
|
opt = OptionParser.new
|
14
17
|
opt.on('-f', 'Create if absent or append to the existing spec') { |_| force_write = true }
|
18
|
+
opt.on('--force', '') { |_| force_write = true }
|
15
19
|
opt.on('-n', 'Dry run mode (shows generated code to console)') { |_| dry_run = true }
|
16
|
-
opt.on('-
|
20
|
+
opt.on('--dry-run', '') { |_| dry_run = true }
|
21
|
+
opt.on('-r', 'Run in Rails mode') { |_| rails_mode = true }
|
22
|
+
opt.on('--rails', '') { |_| rails_mode = true }
|
23
|
+
opt.on('-o VAL', 'Output directory (default: ./spec)') { |dir| spec_dir = dir }
|
24
|
+
opt.on('--output-dir VAL', '') { |dir| spec_dir = dir }
|
25
|
+
opt.on('--delta-template VAL', 'Delta template filepath') { |path| delta_path = path }
|
26
|
+
opt.on('--full-template VAL', 'Full template filepath') { |path| full_path = path }
|
17
27
|
|
18
28
|
args = opt.parse(ARGV)
|
19
29
|
dir_or_file = args.first
|
@@ -25,8 +35,18 @@ unless dir_or_file.match(/.rb$/)
|
|
25
35
|
dir_or_file = dir_or_file.gsub(/\/$/, '') + "/**/*.rb"
|
26
36
|
end
|
27
37
|
|
28
|
-
|
38
|
+
delta_template = nil
|
39
|
+
if ! delta_path.nil? && File.exist?(delta_path)
|
40
|
+
delta_template = File.read(delta_path)
|
41
|
+
end
|
42
|
+
full_template = nil
|
43
|
+
if ! full_path.nil? && File.exist?(full_path)
|
44
|
+
full_template = File.read(full_path)
|
45
|
+
end
|
46
|
+
|
47
|
+
generator = RSpecKickstarter::Generator.new(spec_dir, delta_template, full_template)
|
48
|
+
|
29
49
|
Dir.glob(dir_or_file).each do |file_path|
|
30
|
-
generator.write_spec(file_path, force_write, dry_run)
|
50
|
+
generator.write_spec(file_path, force_write, dry_run, rails_mode)
|
31
51
|
end
|
32
52
|
|
@@ -1,21 +1,24 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
|
3
|
+
require 'erb'
|
3
4
|
require 'rdoc'
|
4
|
-
require 'rdoc/parser/ruby'
|
5
|
-
require 'rdoc/options'
|
6
5
|
require 'rdoc/generator'
|
6
|
+
require 'rdoc/options'
|
7
|
+
require 'rdoc/parser/ruby'
|
7
8
|
require 'rdoc/stats'
|
8
9
|
require 'rspec_kickstarter'
|
9
10
|
|
10
11
|
class RSpecKickstarter::Generator
|
11
12
|
|
12
|
-
attr_accessor :spec_dir
|
13
|
+
attr_accessor :spec_dir, :append_templtae, :full_template
|
13
14
|
|
14
|
-
def initialize(spec_dir = './spec')
|
15
|
+
def initialize(spec_dir = './spec', delta_template = nil, full_template = nil)
|
15
16
|
@spec_dir = spec_dir.gsub(/\/$/, '')
|
17
|
+
@delta_template = delta_template
|
18
|
+
@full_template = full_template
|
16
19
|
end
|
17
20
|
|
18
|
-
def write_spec(file_path, force_write = false, dry_run = false)
|
21
|
+
def write_spec(file_path, force_write = false, dry_run = false, rails_mode = false)
|
19
22
|
|
20
23
|
top_level = get_ruby_parser(file_path).scan
|
21
24
|
c = extract_target_class_or_module(top_level)
|
@@ -37,19 +40,18 @@ class RSpecKickstarter::Generator
|
|
37
40
|
if lacking_methods.empty?
|
38
41
|
puts "#{spec_path} skipped."
|
39
42
|
else
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
SPEC
|
43
|
+
methods_to_generate = lacking_methods
|
44
|
+
|
45
|
+
if ! @delta_template.nil?
|
46
|
+
additional_spec = ERB.new(@delta_template, nil, '-', '_additional_spec_code').result(binding)
|
47
|
+
elsif rails_mode && self_path.match(/controllers/)
|
48
|
+
additional_spec = ERB.new(RAILS_CONTROLLER_METHODS_PART_TEMPLATE, nil, '-', '_additional_spec_code').result(binding)
|
49
|
+
elsif rails_mode && self_path.match(/helpers/)
|
50
|
+
additional_spec = ERB.new(RAILS_HELPER_METHODS_PART_TEMPLATE, nil, '-', '_additional_spec_code').result(binding)
|
51
|
+
else
|
52
|
+
additional_spec = ERB.new(BASIC_METHODS_PART_TEMPLATE, nil, '-', '_additional_spec_code').result(binding)
|
53
|
+
end
|
54
|
+
|
53
55
|
last_end_not_found = true
|
54
56
|
code = existing_spec.split("\n").reverse.reject { |line|
|
55
57
|
if last_end_not_found
|
@@ -70,30 +72,19 @@ SPEC
|
|
70
72
|
|
71
73
|
else
|
72
74
|
# Create a new spec
|
73
|
-
|
74
75
|
self_path = to_string_value_to_require(file_path)
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
76
|
+
methods_to_generate = c.method_list.select { |m| m.visibility == :public }
|
77
|
+
|
78
|
+
if ! @full_template.nil?
|
79
|
+
code = ERB.new(@full_template, nil, '-', '_new_spec_code').result(binding)
|
80
|
+
elsif rails_mode && self_path.match(/controllers/)
|
81
|
+
code = ERB.new(RAILS_CONTROLLER_NEW_SPEC_TEMPLATE, nil, '-', '_new_spec_code').result(binding)
|
82
|
+
elsif rails_mode && self_path.match(/helpers/)
|
83
|
+
code = ERB.new(RAILS_HELPER_NEW_SPEC_TEMPLATE, nil, '-', '_new_spec_code').result(binding)
|
84
|
+
else
|
85
|
+
code = ERB.new(BASIC_NEW_SPEC_TEMPLATE, nil, '-', '_new_spec_code').result(binding)
|
86
|
+
end
|
81
87
|
|
82
|
-
#{c.method_list
|
83
|
-
.select { |m| m.visibility == :public }
|
84
|
-
.map { |method|
|
85
|
-
<<EACH_SPEC
|
86
|
-
# TODO auto-generated
|
87
|
-
describe '#{method.name}' do
|
88
|
-
it 'works' do#{get_instantiation_code(c, method)}#{get_params_initialization_code(method)}
|
89
|
-
result = #{get_method_invocation_code(c, method)}
|
90
|
-
expect(result).not_to be_nil
|
91
|
-
end
|
92
|
-
end
|
93
|
-
EACH_SPEC
|
94
|
-
}.join("\n")}
|
95
|
-
end
|
96
|
-
SPEC
|
97
88
|
if dry_run
|
98
89
|
puts "----- #{spec_path} -----"
|
99
90
|
puts code
|
@@ -222,10 +213,10 @@ SPEC
|
|
222
213
|
else
|
223
214
|
constructor = c.method_list.find { |m| m.name == 'new' }
|
224
215
|
if constructor.nil?
|
225
|
-
"
|
216
|
+
" #{instance_name(c)} = #{get_complete_class_name(c)}.new\n"
|
226
217
|
else
|
227
218
|
get_params_initialization_code(constructor) +
|
228
|
-
"
|
219
|
+
" #{instance_name(c)} = #{get_complete_class_name(c)}.new(#{to_param_names_array(constructor.params).join(', ')})\n"
|
229
220
|
end
|
230
221
|
end
|
231
222
|
end
|
@@ -236,19 +227,23 @@ SPEC
|
|
236
227
|
# b = stub('b')
|
237
228
|
#
|
238
229
|
def get_params_initialization_code(method)
|
239
|
-
code = to_param_names_array(method.params).map { |p| " #{p} = stub('#{p}')" }.join("\n")
|
240
|
-
code.empty? ? "" : "
|
230
|
+
code = to_param_names_array(method.params).map { |p| " #{p} = stub('#{p}')" }.join("\n")
|
231
|
+
code.empty? ? "" : "#{code}\n"
|
241
232
|
end
|
242
233
|
|
243
234
|
#
|
244
235
|
# e.g. BarBaz.do_something(a, b) { |c| }
|
245
236
|
#
|
246
237
|
def get_method_invocation_code(c, method)
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
238
|
+
target = method.singleton ? get_complete_class_name(c) : instance_name(c)
|
239
|
+
"#{target}.#{method.name}(#{to_param_names_array(method.params).join(', ')})#{get_block_code(method)}"
|
240
|
+
end
|
241
|
+
|
242
|
+
#
|
243
|
+
# e.g. do_something(a, b) { |c| }
|
244
|
+
#
|
245
|
+
def get_rails_helper_method_invocation_code(method)
|
246
|
+
"#{method.name}(#{to_param_names_array(method.params).join(', ')})#{get_block_code(method)}"
|
252
247
|
end
|
253
248
|
|
254
249
|
#
|
@@ -262,5 +257,90 @@ SPEC
|
|
262
257
|
end
|
263
258
|
end
|
264
259
|
|
260
|
+
def get_rails_http_method(method_name)
|
261
|
+
http_method = RAILS_RESOURCE_METHOD_AND_HTTPMETHOD[method_name]
|
262
|
+
http_method.nil? ? 'get' : http_method
|
263
|
+
end
|
264
|
+
|
265
|
+
BASIC_METHODS_PART_TEMPLATE = <<SPEC
|
266
|
+
<%- methods_to_generate.map { |method| %>
|
267
|
+
# TODO auto-generated
|
268
|
+
describe '<%= method.name %>' do
|
269
|
+
it 'works' do
|
270
|
+
<%- unless get_instantiation_code(c, method).nil? -%><%= get_instantiation_code(c, method) %><%- end -%>
|
271
|
+
<%- unless get_params_initialization_code(method).nil? -%><%= get_params_initialization_code(method) %><%- end -%>
|
272
|
+
result = <%= get_method_invocation_code(c, method) %>
|
273
|
+
expect(result).not_to be_nil
|
274
|
+
end
|
275
|
+
end
|
276
|
+
<% } %>
|
277
|
+
SPEC
|
278
|
+
|
279
|
+
BASIC_NEW_SPEC_TEMPLATE = <<SPEC
|
280
|
+
# -*- encoding: utf-8 -*-
|
281
|
+
|
282
|
+
require 'spec_helper'
|
283
|
+
<% unless rails_mode then %>require '<%= self_path %>'
|
284
|
+
<% end -%>
|
285
|
+
|
286
|
+
describe <%= get_complete_class_name(c) %> do
|
287
|
+
<%= ERB.new(BASIC_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
|
288
|
+
end
|
289
|
+
SPEC
|
290
|
+
|
291
|
+
RAILS_RESOURCE_METHOD_AND_HTTPMETHOD = {
|
292
|
+
'index' => 'get',
|
293
|
+
'new' => 'get',
|
294
|
+
'create' => 'post',
|
295
|
+
'show' => 'get',
|
296
|
+
'edit' => 'get',
|
297
|
+
'update' => 'put',
|
298
|
+
'destroy' => 'delete'
|
299
|
+
}
|
300
|
+
|
301
|
+
RAILS_CONTROLLER_METHODS_PART_TEMPLATE = <<SPEC
|
302
|
+
<%- methods_to_generate.map { |method| %>
|
303
|
+
# TODO auto-generated
|
304
|
+
describe '<%= method.name %>' do
|
305
|
+
it 'returns OK' do
|
306
|
+
<%= get_rails_http_method(method.name) %> :<%= method.name %>, {}, {}
|
307
|
+
expect(response.status).to eq(200)
|
308
|
+
end
|
309
|
+
end
|
310
|
+
<% } %>
|
311
|
+
SPEC
|
312
|
+
|
313
|
+
RAILS_CONTROLLER_NEW_SPEC_TEMPLATE = <<SPEC
|
314
|
+
# -*- encoding: utf-8 -*-
|
315
|
+
|
316
|
+
require 'spec_helper'
|
317
|
+
|
318
|
+
describe <%= get_complete_class_name(c) %> do
|
319
|
+
<%= ERB.new(RAILS_CONTROLLER_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
|
320
|
+
end
|
321
|
+
SPEC
|
322
|
+
|
323
|
+
RAILS_HELPER_METHODS_PART_TEMPLATE = <<SPEC
|
324
|
+
<%- methods_to_generate.map { |method| %>
|
325
|
+
# TODO auto-generated
|
326
|
+
describe '<%= method.name %>' do
|
327
|
+
it 'works' do
|
328
|
+
result = <%= get_rails_helper_method_invocation_code(method) %>
|
329
|
+
expect(result).not_to be_nil
|
330
|
+
end
|
331
|
+
end
|
332
|
+
<% } %>
|
333
|
+
SPEC
|
334
|
+
|
335
|
+
RAILS_HELPER_NEW_SPEC_TEMPLATE = <<SPEC
|
336
|
+
# -*- encoding: utf-8 -*-
|
337
|
+
|
338
|
+
require 'spec_helper'
|
339
|
+
|
340
|
+
describe <%= get_complete_class_name(c) %> do
|
341
|
+
<%= ERB.new(RAILS_HELPER_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
|
342
|
+
end
|
343
|
+
SPEC
|
344
|
+
|
265
345
|
end
|
266
346
|
|