rspec-kickstarter 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,32 @@
1
1
  #!/usr/bin/env ruby
2
+ #
3
+ # rspec-kickstarter [dir/*.rb]
4
+ #
2
5
 
6
+ require 'optparse'
3
7
  require 'rspec_kickstarter/generator'
4
8
 
5
- generator = RSpecKickstarter::Generator.new
9
+ force_write = false
10
+ dry_run = false
11
+ spec_dir = './spec'
6
12
 
7
- dir_or_file = ARGV.first
13
+ opt = OptionParser.new
14
+ opt.on('-f', 'Creates new specs or appends on existing specs') { |_| force_write = true }
15
+ opt.on('-n', 'Dry run') { |_| dry_run = true }
16
+ opt.on('-o VAL', 'Output directory') { |dir| spec_dir = dir }
17
+
18
+ args = opt.parse(ARGV)
19
+ dir_or_file = args.first
20
+ if dir_or_file.nil?
21
+ puts "Usage: rspec-kickstarter [dir/*.rb]"
22
+ exit 1
23
+ end
8
24
  unless dir_or_file.match(/.rb$/)
9
25
  dir_or_file = dir_or_file.gsub(/\/$/, '') + "/**/*.rb"
10
26
  end
27
+
28
+ generator = RSpecKickstarter::Generator.new(spec_dir)
11
29
  Dir.glob(dir_or_file).each do |file_path|
12
- generator.wite_spec_if_absent(file_path)
30
+ generator.write_spec(file_path, force_write, dry_run)
13
31
  end
14
32
 
@@ -37,8 +37,11 @@ module RSpecKickstarter
37
37
  end
38
38
 
39
39
  def instance_name(c)
40
- # TODO prefer snake_case
41
- c.name.downcase
40
+ c.name.gsub(/::/, '/').
41
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
42
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
43
+ tr("-", "_").
44
+ downcase
42
45
  end
43
46
 
44
47
  def to_param_names_array(params)
@@ -80,7 +83,7 @@ module RSpecKickstarter
80
83
  end
81
84
  end
82
85
 
83
- def wite_spec_if_absent(file_path)
86
+ def write_spec(file_path, force_write = false, dry_run = false)
84
87
 
85
88
  body = File.read(file_path)
86
89
  RDoc::TopLevel.reset()
@@ -99,8 +102,48 @@ module RSpecKickstarter
99
102
  puts "#{file_path} skipped (Class/Module not found)."
100
103
  else
101
104
 
102
- self_path = file_path.gsub(/^(lib\/)|(app\/)/, '').gsub(/\.rb$/, '')
103
- code = <<SPEC
105
+ spec_path = spec_dir + '/' + file_path.gsub(/^(lib\/)|(app\/)/, '').gsub(/\.rb$/, '_spec.rb')
106
+
107
+ if force_write && File.exist?(spec_path)
108
+ existing_spec = File.read(spec_path)
109
+ racking_methods = c.method_list.select { |m| m.visibility == :public }.reject { |m| existing_spec.match(m.name) }
110
+ if racking_methods.empty?
111
+ puts "#{spec_path} skipped."
112
+ else
113
+ additional_spec = <<SPEC
114
+ #{racking_methods.map { |method|
115
+ <<EACH_SPEC
116
+ # TODO auto-generated
117
+ describe '#{method.name}' do
118
+ it 'should work' do#{get_instantiation_code(c, method)}#{get_params_initialization_code(method)}
119
+ result = #{get_method_invocation_code(c, method)}
120
+ result.should_not be_nil
121
+ end
122
+ end
123
+ EACH_SPEC
124
+ }.join("\n")}
125
+ SPEC
126
+ last_end_not_found = true
127
+ code = existing_spec.split("\n").reverse.reject { |line|
128
+ if last_end_not_found
129
+ last_end_not_found = line.strip.gsub(/#.+$/, '') != "end"
130
+ true
131
+ else
132
+ false
133
+ end
134
+ }.reverse.join("\n") + "\n" + additional_spec + "\nend\n"
135
+ if dry_run
136
+ puts "----- #{spec_path} -----"
137
+ puts code
138
+ else
139
+ File.open(spec_path, 'w') { |f| f.write(code) }
140
+ end
141
+ puts "#{spec_path} modified."
142
+ end
143
+
144
+ else
145
+ self_path = file_path.gsub(/^(lib\/)|(app\/)/, '').gsub(/\.rb$/, '')
146
+ code = <<SPEC
104
147
  # -*- encoding: utf-8 -*-
105
148
  require 'spec_helper'
106
149
  require '#{self_path}'
@@ -109,10 +152,11 @@ describe #{get_complete_class_name(c)} do
109
152
 
110
153
  #{c.method_list.select { |m| m.visibility == :public }.map { |method|
111
154
  <<EACH_SPEC
155
+ # TODO auto-generated
112
156
  describe '#{method.name}' do
113
157
  it 'should work' do#{get_instantiation_code(c, method)}#{get_params_initialization_code(method)}
114
158
  result = #{get_method_invocation_code(c, method)}
115
- # result.should_not be_nil
159
+ result.should_not be_nil
116
160
  end
117
161
  end
118
162
  EACH_SPEC
@@ -120,17 +164,23 @@ EACH_SPEC
120
164
  end
121
165
  SPEC
122
166
 
123
- # TODO improve the logic
124
- spec_path = spec_dir + '/' + file_path.gsub(/^(lib\/)|(app\/)/, '').gsub(/\.rb$/, '_spec.rb')
125
- if File.exist?(spec_path)
126
- puts "#{spec_path} already exists."
127
- else
128
- FileUtils.mkdir_p(File.dirname(spec_path))
129
- File.open(spec_path, 'w') { |f| f.write(code) }
130
- puts "#{spec_path} created."
167
+ if File.exist?(spec_path)
168
+ puts "#{spec_path} already exists."
169
+ else
170
+ if dry_run
171
+ puts "----- #{spec_path} -----"
172
+ puts code
173
+ else
174
+ FileUtils.mkdir_p(File.dirname(spec_path))
175
+ File.open(spec_path, 'w') { |f| f.write(code) }
176
+ puts "#{spec_path} created."
177
+ end
178
+ end
131
179
  end
132
180
  end
181
+
133
182
  end
183
+
134
184
  end
135
185
  end
136
186
 
@@ -1,3 +1,3 @@
1
1
  module RSpecKickstarter
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.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.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -46,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  version: '0'
47
47
  requirements: []
48
48
  rubyforge_project:
49
- rubygems_version: 1.8.23
49
+ rubygems_version: 1.8.24
50
50
  signing_key:
51
51
  specification_version: 3
52
52
  summary: RSpec kickstarter generates spec files for existing code.