rspec-kickstarter 0.2.6 → 0.2.7

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.
@@ -3,6 +3,9 @@
3
3
  require "rspec_kickstarter/generator"
4
4
  require "rspec_kickstarter/version"
5
5
 
6
+ #
7
+ # RSpecKickstarter Facade
8
+ #
6
9
  module RSpecKickstarter
7
10
 
8
11
  def self.write_spec(file_path, spec_dir = './spec', force_write = false, dry_run = false)
@@ -0,0 +1,53 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'erb'
4
+ require 'rspec_kickstarter'
5
+ require 'rspec_kickstarter/erb_templates'
6
+
7
+ #
8
+ # ERB instance provider
9
+ #
10
+ class RSpecKickstarter::ERBFactory
11
+
12
+ def initialize(custom_template)
13
+ @custom_template = custom_template
14
+ end
15
+
16
+ #
17
+ # Returns ERB instance for creating new spec
18
+ #
19
+ def get_instance_for_new_spec(rails_mode, target_path)
20
+ ERB.new(get_erb_template(@custom_template, true, rails_mode, target_path), nil, '-', '_new_spec_code')
21
+ end
22
+
23
+ #
24
+ # Returns ERB instance for appeding lacking tests
25
+ #
26
+ def get_instance_for_appending(rails_mode, target_path)
27
+ ERB.new(get_erb_template(@custom_template, false, rails_mode, target_path), nil, '-', '_additional_spec_code')
28
+ end
29
+
30
+ private
31
+
32
+ #
33
+ # Returns ERB template
34
+ #
35
+ def get_erb_template(custom_template, is_full, rails_mode, target_path)
36
+ if custom_template
37
+ custom_template
38
+ elsif rails_mode && target_path.match(/controllers/)
39
+ if is_full then RSpecKickstarter::ERBTemplates::RAILS_CONTROLLER_NEW_SPEC_TEMPLATE
40
+ else RSpecKickstarter::ERBTemplates::RAILS_CONTROLLER_METHODS_PART_TEMPLATE
41
+ end
42
+ elsif rails_mode && target_path.match(/helpers/)
43
+ if is_full then RSpecKickstarter::ERBTemplates::RAILS_HELPER_NEW_SPEC_TEMPLATE
44
+ else RSpecKickstarter::ERBTemplates::RAILS_HELPER_METHODS_PART_TEMPLATE
45
+ end
46
+ else
47
+ if is_full then RSpecKickstarter::ERBTemplates::BASIC_NEW_SPEC_TEMPLATE
48
+ else RSpecKickstarter::ERBTemplates::BASIC_METHODS_PART_TEMPLATE
49
+ end
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,81 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'erb'
4
+ require 'rspec_kickstarter'
5
+
6
+ #
7
+ # ERB templates
8
+ #
9
+ module RSpecKickstarter::ERBTemplates
10
+
11
+ BASIC_METHODS_PART_TEMPLATE = <<SPEC
12
+ <%- methods_to_generate.map { |method| %>
13
+ # TODO auto-generated
14
+ describe '#<%= method.name %>' do
15
+ it 'works' do
16
+ <%- unless get_instantiation_code(c, method).nil? -%><%= get_instantiation_code(c, method) %><%- end -%>
17
+ <%- unless get_params_initialization_code(method).nil? -%><%= get_params_initialization_code(method) %><%- end -%>
18
+ result = <%= get_method_invocation_code(c, method) %>
19
+ expect(result).not_to be_nil
20
+ end
21
+ end
22
+ <% } %>
23
+ SPEC
24
+
25
+ BASIC_NEW_SPEC_TEMPLATE = <<SPEC
26
+ # -*- encoding: utf-8 -*-
27
+
28
+ require 'spec_helper'
29
+ <% unless rails_mode then %>require '<%= self_path %>'
30
+ <% end -%>
31
+
32
+ describe <%= get_complete_class_name(c) %> do
33
+ <%= ERB.new(BASIC_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
34
+ end
35
+ SPEC
36
+
37
+ RAILS_CONTROLLER_METHODS_PART_TEMPLATE = <<SPEC
38
+ <%- methods_to_generate.map { |method| %>
39
+ # TODO auto-generated
40
+ describe '<%= get_rails_http_method(method.name).upcase %> <%= method.name %>' do
41
+ it 'works' do
42
+ <%= get_rails_http_method(method.name) %> :<%= method.name %>, {}, {}
43
+ expect(response.status).to eq(200)
44
+ end
45
+ end
46
+ <% } %>
47
+ SPEC
48
+
49
+ RAILS_CONTROLLER_NEW_SPEC_TEMPLATE = <<SPEC
50
+ # -*- encoding: utf-8 -*-
51
+
52
+ require 'spec_helper'
53
+
54
+ describe <%= get_complete_class_name(c) %> do
55
+ <%= ERB.new(RAILS_CONTROLLER_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
56
+ end
57
+ SPEC
58
+
59
+ RAILS_HELPER_METHODS_PART_TEMPLATE = <<SPEC
60
+ <%- methods_to_generate.map { |method| %>
61
+ # TODO auto-generated
62
+ describe '#<%= method.name %>' do
63
+ it 'works' do
64
+ result = <%= get_rails_helper_method_invocation_code(method) %>
65
+ expect(result).not_to be_nil
66
+ end
67
+ end
68
+ <% } %>
69
+ SPEC
70
+
71
+ RAILS_HELPER_NEW_SPEC_TEMPLATE = <<SPEC
72
+ # -*- encoding: utf-8 -*-
73
+
74
+ require 'spec_helper'
75
+
76
+ describe <%= get_complete_class_name(c) %> do
77
+ <%= ERB.new(RAILS_HELPER_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
78
+ end
79
+ SPEC
80
+
81
+ end
@@ -1,14 +1,16 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- require 'erb'
4
3
  require 'rdoc'
5
- require 'rdoc/generator'
6
- require 'rdoc/options'
7
- require 'rdoc/parser/ruby'
8
- require 'rdoc/stats'
9
4
  require 'rspec_kickstarter'
5
+ require 'rspec_kickstarter/erb_factory'
6
+ require 'rspec_kickstarter/erb_templates'
7
+ require 'rspec_kickstarter/rdoc_factory'
10
8
 
9
+ #
10
+ # RSpec Code Generator
11
+ #
11
12
  class RSpecKickstarter::Generator
13
+ include RSpecKickstarter::ERBTemplates
12
14
 
13
15
  attr_accessor :spec_dir, :delta_template, :full_template
14
16
 
@@ -18,126 +20,29 @@ class RSpecKickstarter::Generator
18
20
  @full_template = full_template
19
21
  end
20
22
 
23
+ #
24
+ # Writes new spec or appends to the existing spec.
25
+ #
21
26
  def write_spec(file_path, force_write = false, dry_run = false, rails_mode = false)
22
-
23
- top_level = get_ruby_parser(file_path).scan
24
- c = extract_target_class_or_module(top_level)
25
-
26
- if c.nil?
27
- puts "#{file_path} skipped (Class/Module not found)."
28
- else
29
-
27
+ class_or_module = RSpecKickstarter::RDocFactory::get_rdoc_class_or_module(file_path)
28
+ if class_or_module
30
29
  spec_path = get_spec_path(file_path)
31
-
32
30
  if force_write && File.exist?(spec_path)
33
- # Append to the existing spec or skip
34
-
35
- existing_spec = File.read(spec_path)
36
- lacking_methods = c.method_list
37
- .select { |m| m.visibility == :public }
38
- .reject { |m| existing_spec.match(m.name) }
39
-
40
- if lacking_methods.empty?
41
- puts "#{spec_path} skipped."
42
- else
43
- # Since 'methods_to_generate' is used in ERB template, don't delete.
44
- methods_to_generate = lacking_methods
45
- additional_spec = create_erb_instance_for_appending(rails_mode, spec_path).result(binding)
46
- last_end_not_found = true
47
- code = existing_spec.split("\n").reverse.reject { |line|
48
- if last_end_not_found
49
- last_end_not_found = line.gsub(/#.+$/, '').strip != "end"
50
- true
51
- else
52
- false
53
- end
54
- }.reverse.join("\n") + "\n" + additional_spec + "\nend\n"
55
- if dry_run
56
- puts "----- #{spec_path} -----"
57
- puts code
58
- else
59
- File.open(spec_path, 'w') { |f| f.write(code) }
60
- end
61
- puts "#{spec_path} modified."
62
- end
63
-
64
- else
65
- # Create a new spec
66
-
67
- # Since 'methods_to_generate' is used in ERB template, don't delete.
68
- methods_to_generate = c.method_list.select { |m| m.visibility == :public }
69
- self_path = to_string_value_to_require(file_path)
70
- code = create_erb_instance_for_new_spec(rails_mode, self_path).result(binding)
71
-
72
- if dry_run
73
- puts "----- #{spec_path} -----"
74
- puts code
75
- else
76
- if File.exist?(spec_path)
77
- puts "#{spec_path} already exists."
78
- else
79
- FileUtils.mkdir_p(File.dirname(spec_path))
80
- File.open(spec_path, 'w') { |f| f.write(code) }
81
- puts "#{spec_path} created."
82
- end
83
- end
84
- end
85
- end
86
-
87
- end
88
-
89
- #
90
- # Creates new RDoc::Parser::Ruby instance.
91
- #
92
- def get_ruby_parser(file_path)
93
- top_level = RDoc::TopLevel.new(file_path)
94
- if RUBY_VERSION.to_f < 2.0
95
- # reset is removed since 2.0
96
- RDoc::TopLevel.reset()
97
- end
98
-
99
- # RDoc::Stats initialization
100
- if defined?(RDoc::Store)
101
- # RDoc 4.0.0 requires RDoc::Store internally.
102
- store = RDoc::Store.new
103
- top_level.store = store
104
- stats = RDoc::Stats.new(store, 1)
105
- else
106
- stats = RDoc::Stats.new(1)
107
- end
108
-
109
- RDoc::Parser::Ruby.new(
110
- top_level,
111
- file_path,
112
- File.read(file_path),
113
- RDoc::Options.new,
114
- stats
115
- )
116
- end
117
-
118
- #
119
- # Extracts RDoc::NormalClass/RDoc::NormalModule from RDoc::TopLevel.
120
- #
121
- def extract_target_class_or_module(top_level)
122
- c = top_level.classes.first
123
- if c.nil?
124
- m = top_level.modules.first
125
- if m.nil?
126
- top_level.is_a?(RDoc::NormalModule) ? top_level : nil
31
+ append_to_existing_spec(class_or_module, dry_run, rails_mode, spec_path)
127
32
  else
128
- extract_target_class_or_module(m)
33
+ create_new_spec(class_or_module, dry_run, rails_mode, file_path, spec_path)
129
34
  end
130
35
  else
131
- c
36
+ puts "#{file_path} skipped (Class/Module not found)."
132
37
  end
133
38
  end
134
39
 
135
40
  #
136
41
  # Gets the complete class name from RDoc::NormalClass/RDoc::NormalModule instance.
137
42
  #
138
- def get_complete_class_name(c, name = c.name)
139
- if !c.parent.name.nil? && c.parent.is_a?(RDoc::NormalModule)
140
- get_complete_class_name(c.parent, "#{c.parent.name}::#{name}")
43
+ def get_complete_class_name(class_or_module, name = class_or_module.name)
44
+ if !class_or_module.parent.name.nil? && class_or_module.parent.is_a?(RDoc::NormalModule)
45
+ get_complete_class_name(class_or_module.parent, "#{class_or_module.parent.name}::#{name}")
141
46
  else
142
47
  name
143
48
  end
@@ -192,38 +97,72 @@ class RSpecKickstarter::Generator
192
97
  end
193
98
 
194
99
  #
195
- # Returns ERB instance for creating new spec
100
+ # Creates new spec.
196
101
  #
197
- def create_erb_instance_for_new_spec(rails_mode, target_path)
198
- if ! @full_template.nil?
199
- ERB.new(@full_template, nil, '-', '_new_spec_code')
200
- elsif rails_mode && target_path.match(/controllers/)
201
- ERB.new(RAILS_CONTROLLER_NEW_SPEC_TEMPLATE, nil, '-', '_new_spec_code')
202
- elsif rails_mode && target_path.match(/helpers/)
203
- ERB.new(RAILS_HELPER_NEW_SPEC_TEMPLATE, nil, '-', '_new_spec_code')
102
+ def create_new_spec(class_or_module, dry_run, rails_mode, file_path, spec_path)
103
+
104
+ # These names are used in ERB template, don't delete.
105
+ methods_to_generate = class_or_module.method_list.select { |m| m.visibility == :public }
106
+ c = class_or_module
107
+ self_path = to_string_value_to_require(file_path)
108
+
109
+ erb = RSpecKickstarter::ERBFactory.new(@full_template).get_instance_for_new_spec(rails_mode, file_path)
110
+ code = erb.result(binding)
111
+
112
+ if dry_run
113
+ puts "----- #{spec_path} -----"
114
+ puts code
204
115
  else
205
- ERB.new(BASIC_NEW_SPEC_TEMPLATE, nil, '-', '_new_spec_code')
116
+ if File.exist?(spec_path)
117
+ puts "#{spec_path} already exists."
118
+ else
119
+ FileUtils.mkdir_p(File.dirname(spec_path))
120
+ File.open(spec_path, 'w') { |f| f.write(code) }
121
+ puts "#{spec_path} created."
122
+ end
206
123
  end
207
124
  end
208
125
 
209
126
  #
210
- # Returns ERB instance for appeding lacking tests
127
+ # Appends new tests to the existing spec.
211
128
  #
212
- def create_erb_instance_for_appending(rails_mode, target_path)
213
- if ! @delta_template.nil?
214
- ERB.new(@delta_template, nil, '-', '_additional_spec_code')
215
- elsif rails_mode && target_path.match(/controllers/)
216
- ERB.new(RAILS_CONTROLLER_METHODS_PART_TEMPLATE, nil, '-', '_additional_spec_code')
217
- elsif rails_mode && target_path.match(/helpers/)
218
- ERB.new(RAILS_HELPER_METHODS_PART_TEMPLATE, nil, '-', '_additional_spec_code')
129
+ def append_to_existing_spec(class_or_module, dry_run, rails_mode, spec_path)
130
+ existing_spec = File.read(spec_path)
131
+ lacking_methods = class_or_module.method_list
132
+ .select { |m| m.visibility == :public }
133
+ .reject { |m| existing_spec.match(m.name) }
134
+
135
+ if lacking_methods.empty?
136
+ puts "#{spec_path} skipped."
219
137
  else
220
- ERB.new(BASIC_METHODS_PART_TEMPLATE, nil, '-', '_additional_spec_code')
138
+ # These names are used in ERB template, don't delete.
139
+ methods_to_generate = lacking_methods
140
+ c = class_or_module
141
+
142
+ erb = RSpecKickstarter::ERBFactory.new(@delta_template).get_instance_for_appending(rails_mode, spec_path)
143
+ additional_spec = erb.result(binding)
144
+ last_end_not_found = true
145
+ code = existing_spec.split("\n").reverse.reject { |line|
146
+ if last_end_not_found
147
+ last_end_not_found = line.gsub(/#.+$/, '').strip != "end"
148
+ true
149
+ else
150
+ false
151
+ end
152
+ }.reverse.join("\n") + "\n" + additional_spec + "\nend\n"
153
+ if dry_run
154
+ puts "----- #{spec_path} -----"
155
+ puts code
156
+ else
157
+ File.open(spec_path, 'w') { |f| f.write(code) }
158
+ end
159
+ puts "#{spec_path} modified."
221
160
  end
222
161
  end
223
162
 
224
- #
163
+ # -----
225
164
  # Code generation
226
- #
165
+ # -----
227
166
 
228
167
  #
229
168
  # e.g.
@@ -286,85 +225,15 @@ class RSpecKickstarter::Generator
286
225
  http_method.nil? ? 'get' : http_method
287
226
  end
288
227
 
289
- BASIC_METHODS_PART_TEMPLATE = <<SPEC
290
- <%- methods_to_generate.map { |method| %>
291
- # TODO auto-generated
292
- describe '#<%= method.name %>' do
293
- it 'works' do
294
- <%- unless get_instantiation_code(c, method).nil? -%><%= get_instantiation_code(c, method) %><%- end -%>
295
- <%- unless get_params_initialization_code(method).nil? -%><%= get_params_initialization_code(method) %><%- end -%>
296
- result = <%= get_method_invocation_code(c, method) %>
297
- expect(result).not_to be_nil
298
- end
299
- end
300
- <% } %>
301
- SPEC
302
-
303
- BASIC_NEW_SPEC_TEMPLATE = <<SPEC
304
- # -*- encoding: utf-8 -*-
305
-
306
- require 'spec_helper'
307
- <% unless rails_mode then %>require '<%= self_path %>'
308
- <% end -%>
309
-
310
- describe <%= get_complete_class_name(c) %> do
311
- <%= ERB.new(BASIC_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
312
- end
313
- SPEC
314
-
315
228
  RAILS_RESOURCE_METHOD_AND_HTTPMETHOD = {
316
229
  'index' => 'get',
317
230
  'new' => 'get',
318
- 'create' => 'post',
231
+ 'create' => 'post',
319
232
  'show' => 'get',
320
233
  'edit' => 'get',
321
234
  'update' => 'put',
322
- 'destroy' => 'delete'
235
+ 'destroy' => 'delete'
323
236
  }
324
237
 
325
- RAILS_CONTROLLER_METHODS_PART_TEMPLATE = <<SPEC
326
- <%- methods_to_generate.map { |method| %>
327
- # TODO auto-generated
328
- describe '<%= get_rails_http_method(method.name).upcase %> <%= method.name %>' do
329
- it 'works' do
330
- <%= get_rails_http_method(method.name) %> :<%= method.name %>, {}, {}
331
- expect(response.status).to eq(200)
332
- end
333
- end
334
- <% } %>
335
- SPEC
336
-
337
- RAILS_CONTROLLER_NEW_SPEC_TEMPLATE = <<SPEC
338
- # -*- encoding: utf-8 -*-
339
-
340
- require 'spec_helper'
341
-
342
- describe <%= get_complete_class_name(c) %> do
343
- <%= ERB.new(RAILS_CONTROLLER_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
344
- end
345
- SPEC
346
-
347
- RAILS_HELPER_METHODS_PART_TEMPLATE = <<SPEC
348
- <%- methods_to_generate.map { |method| %>
349
- # TODO auto-generated
350
- describe '#<%= method.name %>' do
351
- it 'works' do
352
- result = <%= get_rails_helper_method_invocation_code(method) %>
353
- expect(result).not_to be_nil
354
- end
355
- end
356
- <% } %>
357
- SPEC
358
-
359
- RAILS_HELPER_NEW_SPEC_TEMPLATE = <<SPEC
360
- # -*- encoding: utf-8 -*-
361
-
362
- require 'spec_helper'
363
-
364
- describe <%= get_complete_class_name(c) %> do
365
- <%= ERB.new(RAILS_HELPER_METHODS_PART_TEMPLATE, nil, '-').result(binding) -%>
366
- end
367
- SPEC
368
-
369
238
  end
370
239
 
@@ -0,0 +1,72 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'rdoc'
4
+ require 'rdoc/generator'
5
+ require 'rdoc/options'
6
+ require 'rdoc/parser/ruby'
7
+ require 'rdoc/stats'
8
+
9
+ require 'rspec_kickstarter'
10
+
11
+ #
12
+ # RDoc instance factory
13
+ #
14
+ class RSpecKickstarter::RDocFactory
15
+
16
+ #
17
+ # Returns RDoc::NormalClass/RDoc::NormalModule instance.
18
+ #
19
+ def self.get_rdoc_class_or_module(file_path)
20
+ top_level = self.get_ruby_parser(file_path).scan
21
+ self.extract_target_class_or_module(top_level)
22
+ end
23
+
24
+ private
25
+
26
+ #
27
+ # Creates new RDoc::Parser::Ruby instance.
28
+ #
29
+ def self.get_ruby_parser(file_path)
30
+ top_level = RDoc::TopLevel.new(file_path)
31
+ if RUBY_VERSION.to_f < 2.0
32
+ # reset is removed since 2.0
33
+ RDoc::TopLevel.reset()
34
+ end
35
+
36
+ # RDoc::Stats initialization
37
+ if defined?(RDoc::Store)
38
+ # RDoc 4.0.0 requires RDoc::Store internally.
39
+ store = RDoc::Store.new
40
+ top_level.store = store
41
+ stats = RDoc::Stats.new(store, 1)
42
+ else
43
+ stats = RDoc::Stats.new(1)
44
+ end
45
+
46
+ RDoc::Parser::Ruby.new(
47
+ top_level,
48
+ file_path,
49
+ File.read(file_path),
50
+ RDoc::Options.new,
51
+ stats
52
+ )
53
+ end
54
+
55
+ #
56
+ # Extracts RDoc::NormalClass/RDoc::NormalModule from RDoc::TopLevel.
57
+ #
58
+ def self.extract_target_class_or_module(top_level)
59
+ c = top_level.classes.first
60
+ if c.nil?
61
+ m = top_level.modules.first
62
+ if m.nil?
63
+ top_level.is_a?(RDoc::NormalModule) ? top_level : nil
64
+ else
65
+ extract_target_class_or_module(m)
66
+ end
67
+ else
68
+ c
69
+ end
70
+ end
71
+
72
+ end
@@ -1,3 +1,3 @@
1
1
  module RSpecKickstarter
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-kickstarter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-12 00:00:00.000000000 Z
12
+ date: 2013-07-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: rspec-kickstarter supports you writing tests for existing code.
15
15
  email:
@@ -20,7 +20,10 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - bin/rspec-kickstarter
23
+ - lib/rspec_kickstarter/erb_factory.rb
24
+ - lib/rspec_kickstarter/erb_templates.rb
23
25
  - lib/rspec_kickstarter/generator.rb
26
+ - lib/rspec_kickstarter/rdoc_factory.rb
24
27
  - lib/rspec_kickstarter/version.rb
25
28
  - lib/rspec_kickstarter.rb
26
29
  homepage: https://github.com/seratch/rspec-kickstarter