rspec_piccolo 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,7 +24,7 @@ You have to execute these commands in your project root directory.
24
24
  piccolo execute SomeClass some_class_place
25
25
  ~~~
26
26
 
27
- Result, spec/some_class_place
27
+ Result, spec/some_class_place_spec.rb
28
28
  ~~~ruby
29
29
  # encoding: utf-8
30
30
  require "spec_helper"
@@ -40,7 +40,7 @@ end
40
40
  piccolo execute SomeModule::SomeClass some_directory/some_class_place
41
41
  ~~~
42
42
 
43
- Result, spec/some_directory/some_class_place
43
+ Result, spec/some_directory/some_class_place_spec.rb
44
44
  ~~~ruby
45
45
  # encoding: utf-8
46
46
  require "spec_helper"
@@ -56,7 +56,7 @@ end
56
56
  piccolo execute SomeClass some_class_place method1 method2
57
57
  ~~~
58
58
 
59
- Result, spec/some_class_place
59
+ Result, spec/some_class_place_spec.rb
60
60
  ~~~ruby
61
61
  # encoding: utf-8
62
62
  require "spec_helper"
@@ -140,7 +140,31 @@ describe SomeClass do
140
140
  end
141
141
  end
142
142
  end
143
+ end
144
+ ~~~
145
+
146
+ ### Case class_name, class_place, method_names with product code
147
+ ~~~bash
148
+ piccolo execute SomeClass some_class_place method1 method2 -p
149
+ ~~~
150
+
151
+ Result, spec/some_class_place_spec.rb
152
+ ~~~ruby
153
+ # omission
154
+ ~~~
155
+
156
+ Result, lib/some_class_place.rb
157
+ ~~~ruby
158
+ # encoding: utf-8
159
+
160
+ class SomeClass
161
+ def method1
162
+ # TOOD: implement your code
163
+ end
143
164
 
165
+ def method2
166
+ # TOOD: implement your code
167
+ end
144
168
  end
145
169
  ~~~
146
170
 
@@ -149,7 +173,7 @@ end
149
173
  piccolo execute SomeClass some_class_place instance_method class_method@c
150
174
  ~~~
151
175
 
152
- Result, spec/some_class_place
176
+ Result, spec/some_class_place_spec.rb
153
177
  ~~~ruby
154
178
  $ cat spec/some_class_place_spec.rb
155
179
  # encoding: utf-8
@@ -164,7 +188,6 @@ describe SomeClass do
164
188
  case_no: 1,
165
189
  case_title: "case_title",
166
190
  expected: "expected",
167
-
168
191
  },
169
192
  ]
170
193
 
@@ -185,13 +208,11 @@ describe SomeClass do
185
208
  # ret = expect(actual).to eq(c[:expected])
186
209
  ensure
187
210
  case_after c
188
-
189
211
  end
190
212
  end
191
213
 
192
214
  def case_before(c)
193
215
  # implement each case before
194
-
195
216
  end
196
217
 
197
218
  def case_after(c)
@@ -206,7 +227,6 @@ describe SomeClass do
206
227
  case_no: 1,
207
228
  case_title: "case_title",
208
229
  expected: "expected",
209
-
210
230
  },
211
231
  ]
212
232
 
@@ -224,16 +244,14 @@ describe SomeClass do
224
244
 
225
245
  # -- then --
226
246
  # TODO: implement assertion code
227
- # ret = expect(actual).to eq(c[:expected])
247
+ # expect(actual).to eq(c[:expected])
228
248
  ensure
229
249
  case_after c
230
-
231
250
  end
232
251
  end
233
252
 
234
253
  def case_before(c)
235
254
  # implement each case before
236
-
237
255
  end
238
256
 
239
257
  def case_after(c)
@@ -241,7 +259,6 @@ describe SomeClass do
241
259
  end
242
260
  end
243
261
  end
244
-
245
262
  end
246
263
  ~~~
247
264
 
@@ -376,7 +393,6 @@ describe Hoge do
376
393
  end
377
394
  end
378
395
  end
379
-
380
396
  end
381
397
  ~~~
382
398
 
@@ -499,7 +515,6 @@ describe Hoge do
499
515
  end
500
516
  end
501
517
  end
502
-
503
518
  end
504
519
  ~~~
505
520
 
@@ -720,7 +735,6 @@ describe FizzBuzz do
720
735
  end
721
736
  end
722
737
  end
723
-
724
738
  end
725
739
  ~~~
726
740
 
@@ -737,6 +751,7 @@ Finished in 0.0045 seconds
737
751
  ~~~
738
752
 
739
753
  ## History
754
+ * version 0.0.7 : add product code generation option .delete unuse empty-line, unuse ret variable.
740
755
  * version 0.0.6 : add class method generation.
741
756
  * version 0.0.5 : add reportable option.
742
757
  * version 0.0.4 : you can get multi level directory spec case.
data/bin/piccolo CHANGED
@@ -1,30 +1,31 @@
1
- #!/usr/bin/env ruby
2
- require 'rspec_piccolo/version'
3
- require 'rspec_piccolo'
4
- require "thor"
5
-
6
- module RSpecPiccolo
7
- #= RSpecPiccolo CLI
8
- class CLI < Thor
9
- class_option :help, :type => :boolean, :aliases => 'h', :desc => 'help message.'
10
- class_option :version, :type => :boolean, :aliases => 'v', :desc => 'version'
11
- class_option :reportable, :type => :boolean, :aliases => 'r', :desc => 'reportable'
12
- default_task :execute
13
-
14
- desc "execute", "generate rspec spec_file with list_cases"
15
- def execute(*args)
16
- class_name = args[0]
17
- class_path = args[1]
18
- method_names = args[2..-1] if args.size > 2
19
-
20
- RSpecPiccolo::Core.new.generate class_name, class_path, method_names, options[:reportable]
21
- end
22
-
23
- desc "version", "version"
24
- def version
25
- p RSpecPiccolo::VERSION
26
- end
27
- end
28
- end
29
-
30
- RSpecPiccolo::CLI.start(ARGV)
1
+ #!/usr/bin/env ruby
2
+ require 'rspec_piccolo/version'
3
+ require 'rspec_piccolo'
4
+ require 'thor'
5
+
6
+ module RSpecPiccolo
7
+ # = RSpecPiccolo CLI
8
+ class CLI < Thor
9
+ class_option :help, type: :boolean, aliases: 'h', desc: 'help message.'
10
+ class_option :version, type: :boolean, aliases: 'v', desc: 'version'
11
+ class_option :reportable, type: :boolean, aliases: 'r', desc: 'reportable'
12
+ class_option :productcode, type: :boolean, aliases: 'p', desc: 'output with productcode'
13
+ default_task :execute
14
+
15
+ desc 'execute', 'generate rspec spec_file with list_cases'
16
+ def execute(*args)
17
+ class_name = args[0]
18
+ class_path = args[1]
19
+ method_names = args[2..-1] if args.size > 2
20
+
21
+ RSpecPiccolo::Core.new.generate class_name, class_path, method_names, options
22
+ end
23
+
24
+ desc 'version', 'version'
25
+ def version
26
+ p RSpecPiccolo::VERSION
27
+ end
28
+ end
29
+ end
30
+
31
+ RSpecPiccolo::CLI.start(ARGV)
data/lib/rspec_piccolo.rb CHANGED
@@ -1,158 +1,227 @@
1
- require "rspec_piccolo/version"
2
- require "erb"
3
- require 'active_support/inflector'
4
- require 'fileutils'
5
-
6
- module RSpecPiccolo
7
- #= RSpecPiccolo Core
8
- class Core
9
- #== RSPec class template
10
- CLASS_TEMPLATE=<<-EOS
11
- # encoding: utf-8
12
- require "spec_helper"
13
- require "<%=class_path%>"
14
-
15
- describe <%=class_name%> do
16
- <%=reportable_prepare%>
17
- <%=methods_template%>
18
- end
19
- EOS
20
-
21
- REPORTABLE_PREPARE =<<-EOS
22
- REPORT = "rspec_report"
23
- DIRS = File.path(__FILE__).gsub(/^.*\\/spec\\//, '').gsub(File.basename(__FILE__), '')
24
- OUT_DIR = "./#\{REPORT}/#\{DIRS}"
25
- REPORT_NAME = report_name = File.basename(__FILE__, ".rb")
26
- REPORT_FILE = "#\{OUT_DIR}#\{REPORT_NAME}.tsv"
27
-
28
- mkspec_report = Proc.new do
29
- Dir.mkdir(REPORT) unless File.exists?(REPORT)
30
- FileUtils.mkdir_p(OUT_DIR) unless File.exists?(OUT_DIR)
31
- File.open(REPORT_FILE, "w") {|f|f.puts "method\\tcase\\ttitle\\tsuccess\\/failure"}
32
- end.call
33
-
34
- success = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\\tsuccess"}}
35
- failure = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\\tfailure"}}
36
- EOS
37
-
38
- #== RSPec method template
39
- METHOD_TEMPLATE=<<-EOS
40
- context :<%=method_name%> do
41
- cases = [
42
- {
43
- case_no: 1,
44
- case_title: "case_title",
45
- expected: "expected",
46
- <%=reportable_case%>
47
- },
48
- ]
49
-
50
- cases.each do |c|
51
- it "|case_no=\#{c[:case_no]}|case_title=\#{c[:case_title]}" do
52
- begin
53
- case_before c
54
-
55
- # -- given --
56
- <%=given_src%>
57
-
58
- # -- when --
59
- # TODO: implement execute code
60
- <%=when_src%>
61
-
62
- # -- then --
63
- # TODO: implement assertion code
64
- # ret = expect(actual).to eq(c[:expected])
65
- ensure
66
- case_after c
67
- <%=reportable_case_after%>
68
- end
69
- end
70
-
71
- def case_before(c)
72
- # implement each case before
73
- <%=reportable_case_before%>
74
- end
75
-
76
- def case_after(c)
77
- # implement each case after
78
- end
79
- end
80
- end
81
- EOS
82
- # <%=given_src%>
83
- # <%=when_src%>
84
-
85
- REPORTABLE_CASE =<<-EOS
86
- success_hook: success,
87
- failure_hook: failure
88
- EOS
89
-
90
- REPORTABLE_CASE_BEFORE = ' File.open(REPORT_FILE, "a") {|f|f.print "method_name\\t#{c[:case_no]}\\t#{c[:case_title]}"}'
91
-
92
- REPORTABLE_CASE_AFTER =<<-EOS
93
- sf_hook = ret ? c[:success_hook] : c[:failure_hook]
94
- sf_hook.call(c)
95
- EOS
96
-
97
- #== initialize
98
- def initialize
99
- @contents = ""
100
- end
101
-
102
- #== generate rspec test case
103
- #=== params
104
- #- class_name: spec's module+class full name
105
- #- class_path: spec's class_path(if you want to create spec/hoge_spec.rb, you should set 'hoge_spec.rb')
106
- #- method_names: target class's method list
107
- def generate(class_name, class_path, method_names, reportable = false)
108
- validate_class_name class_name
109
- validate_class_path class_path
110
- methods_template = generate_method_template(class_name, method_names, reportable)
111
- @contents = generate_class_template(class_name, class_path, methods_template, reportable)
112
- create_spec_directory class_path
113
- File.open("./spec/#{class_path}_spec.rb", "w") {|f|f.puts @contents}
114
- end
115
-
116
- private
117
- def validate_class_name(class_name)
118
- raise RSpecPiccoloError.new("class_name is not allowed nil value") if class_name.nil?
119
- raise RSpecPiccoloError.new("class_name is not allowed empty value") if class_name.empty?
120
- end
121
-
122
- def validate_class_path(class_path)
123
- raise RSpecPiccoloError.new("class_path is not allowed nil value") if class_path.nil?
124
- raise RSpecPiccoloError.new("class_path is not allowed empty value") if class_path.empty?
125
- end
126
-
127
- def generate_method_template(class_name, method_names, reportable)
128
- return "" if method_names.nil?
129
-
130
- reportable_case = reportable ? REPORTABLE_CASE.chop : ""
131
- instance_name = class_name.gsub('::', '_').underscore.downcase
132
- method_templates = []
133
- method_names.each do |method_name|
134
- is_class_method = (method_name.match(/@c$/) ? true : false)
135
- method_name = method_name.gsub("@c", "") if is_class_method
136
- given_src = is_class_method ? "# nothing" : "#{instance_name} = #{class_name}.new"
137
- when_src = is_class_method ? "# actual = #{class_name}.#{method_name}" : "# actual = #{instance_name}.#{method_name}"
138
- reportable_case_before = reportable ? REPORTABLE_CASE_BEFORE.dup : ""
139
- reportable_case_before.gsub!("method_name", method_name)
140
- reportable_case_after = reportable ? REPORTABLE_CASE_AFTER.dup.chop : ""
141
- method_templates << ERB.new(METHOD_TEMPLATE).result(binding)
142
- end
143
- method_templates.join("\n")
144
- end
145
-
146
- def create_spec_directory(class_path)
147
- return if Dir.exists? "./spec/#{File.dirname(class_path)}"
148
- FileUtils.mkdir_p("./spec/#{File.dirname(class_path)}")
149
- end
150
-
151
- def generate_class_template(class_name, class_path, methods_template, reportable)
152
- reportable_prepare = reportable ? REPORTABLE_PREPARE : ""
153
- ERB.new(CLASS_TEMPLATE).result(binding)
154
- end
155
- end
156
-
157
- class RSpecPiccoloError < StandardError;end
158
- end
1
+ require 'rspec_piccolo/version'
2
+ require 'erb'
3
+ require 'active_support/inflector'
4
+ require 'fileutils'
5
+
6
+ module RSpecPiccolo
7
+ # RSpecPiccolo Core
8
+ class Core
9
+ # RSPec class template
10
+ CLASS_TEMPLATE = <<-EOS
11
+ # encoding: utf-8
12
+ require "spec_helper"
13
+ require "<%=class_path%>"
14
+
15
+ describe <%=class_name%> do
16
+ <%=reportable_prepare%>
17
+ <%=methods_template%>
18
+ end
19
+ EOS
20
+
21
+ REPORTABLE_PREPARE = <<-EOS
22
+ REPORT = "rspec_report"
23
+ DIRS = File.path(__FILE__).gsub(/^.*\\/spec\\//, '').gsub(File.basename(__FILE__), '')
24
+ OUT_DIR = "./#\{REPORT}/#\{DIRS}"
25
+ REPORT_NAME = report_name = File.basename(__FILE__, ".rb")
26
+ REPORT_FILE = "#\{OUT_DIR}#\{REPORT_NAME}.tsv"
27
+
28
+ mkspec_report = Proc.new do
29
+ Dir.mkdir(REPORT) unless File.exists?(REPORT)
30
+ FileUtils.mkdir_p(OUT_DIR) unless File.exists?(OUT_DIR)
31
+ File.open(REPORT_FILE, "w") {|f|f.puts "method\\tcase\\ttitle\\tsuccess\\/failure"}
32
+ end.call
33
+
34
+ success = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\\tsuccess"}}
35
+ failure = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\\tfailure"}}
36
+ EOS
37
+
38
+ # RSPec method template
39
+ METHOD_TEMPLATE = <<-EOS
40
+ context :<%=method_name%> do
41
+ cases = [
42
+ {
43
+ case_no: 1,
44
+ case_title: "case_title",
45
+ expected: "expected",<%=reportable_case%>
46
+ },
47
+ ]
48
+
49
+ cases.each do |c|
50
+ it "|case_no=\#{c[:case_no]}|case_title=\#{c[:case_title]}" do
51
+ begin
52
+ case_before c
53
+
54
+ # -- given --
55
+ <%=given_src%>
56
+
57
+ # -- when --
58
+ # TODO: implement execute code
59
+ <%=when_src%>
60
+
61
+ # -- then --
62
+ # TODO: implement assertion code
63
+ # <%=reportable_case_ret%>expect(actual).to eq(c[:expected])
64
+ ensure
65
+ case_after c<%=reportable_case_after%>
66
+ end
67
+ end
68
+
69
+ def case_before(c)
70
+ # implement each case before<%=reportable_case_before%>
71
+ end
72
+
73
+ def case_after(c)
74
+ # implement each case after
75
+ end
76
+ end
77
+ end
78
+ EOS
79
+
80
+ REPORTABLE_CASE = <<-EOS
81
+
82
+ success_hook: success,
83
+ failure_hook: failure
84
+ EOS
85
+
86
+ REPORTABLE_CASE_BEFORE = <<-EOS
87
+
88
+ File.open(REPORT_FILE, "a") {|f|f.print "method_name\\t\#{c[:case_no]}\\t\#{c[:case_title]}"}
89
+ EOS
90
+
91
+ REPORTABLE_CASE_AFTER = <<-EOS
92
+
93
+ sf_hook = ret ? c[:success_hook] : c[:failure_hook]
94
+ sf_hook.call(c)
95
+ EOS
96
+
97
+ REPORTABLE_CASE_RET = "ret = "
98
+
99
+ PRODUCT_CLASS_TEMPLATE = <<-EOS
100
+ # encoding: utf-8
101
+
102
+ <%=module_start%>
103
+ <%=module_indent%>class <%=class_name%>
104
+ <%=methods_template%>
105
+ <%=module_indent%>end
106
+ <%=module_end%>
107
+ EOS
108
+
109
+ # initialize
110
+ def initialize
111
+ @contents = ''
112
+ end
113
+
114
+ #== generate rspec test case
115
+ #=== params
116
+ #- class_name: spec's module+class full name
117
+ #- class_path: spec's class_path(if you want to create spec/hoge_spec.rb, you should set 'hoge_spec.rb')
118
+ #- method_names: target class's method list
119
+ #- options: options
120
+ def generate(class_name, class_path, method_names, options)
121
+ validate_class_name class_name
122
+ validate_class_path class_path
123
+ methods_template = generate_method_template(class_name, method_names, options)
124
+ @contents = generate_class_template(class_name, class_path, methods_template.chop, options)
125
+ create_spec_directory class_path
126
+ File.open("./spec/#{class_path}_spec.rb", 'w:UTF-8') { |f|f.puts @contents }
127
+ output_product_code(class_name, class_path, method_names) if output_product? options
128
+ end
129
+
130
+ private
131
+ def validate_class_name(class_name)
132
+ fail RSpecPiccoloError.new('class_name is not allowed nil value') if class_name.nil?
133
+ fail RSpecPiccoloError.new('class_name is not allowed empty value') if class_name.empty?
134
+ end
135
+
136
+ def validate_class_path(class_path)
137
+ fail RSpecPiccoloError.new('class_path is not allowed nil value') if class_path.nil?
138
+ fail RSpecPiccoloError.new('class_path is not allowed empty value') if class_path.empty?
139
+ end
140
+
141
+ def generate_method_template(class_name, method_names, options)
142
+ return '' if method_names.nil?
143
+ reportable_case = options[:reportable] ? REPORTABLE_CASE.chop : ''
144
+ instance_name = class_name.gsub('::', '_').underscore.downcase
145
+ method_templates = []
146
+ method_names.each do |method_name|
147
+ is_class_method = is_class_method?(method_name)
148
+ method_name = method_name.gsub('@c', '') if is_class_method
149
+ given_src = is_class_method ? '# nothing' : "#{instance_name} = #{class_name}.new"
150
+ when_src = is_class_method ? "# actual = #{class_name}.#{method_name}" : "# actual = #{instance_name}.#{method_name}"
151
+ reportable_case_before = options[:reportable] ? REPORTABLE_CASE_BEFORE.dup.chop : ''
152
+ reportable_case_before.gsub!('method_name', method_name)
153
+ reportable_case_after = options[:reportable] ? REPORTABLE_CASE_AFTER.dup.chop : ''
154
+ reportable_case_ret = options[:reportable] ? REPORTABLE_CASE_RET.dup : ''
155
+ method_templates << ERB.new(METHOD_TEMPLATE).result(binding)
156
+ end
157
+ method_templates.join("\n")
158
+ end
159
+
160
+ def create_spec_directory(class_path)
161
+ return if Dir.exists? "./spec/#{File.dirname(class_path)}"
162
+ FileUtils.mkdir_p("./spec/#{File.dirname(class_path)}")
163
+ end
164
+
165
+ def generate_class_template(class_name, class_path, methods_template, options)
166
+ reportable_prepare = options[:reportable] ? REPORTABLE_PREPARE : ''
167
+ ERB.new(CLASS_TEMPLATE).result(binding)
168
+ end
169
+
170
+ def output_product?(options)
171
+ options[:productcode] ? true : false
172
+ end
173
+
174
+ def output_product_code(class_name, class_path, method_names)
175
+ has_module = has_module? class_name
176
+ module_indent = has_module ? ' ' : ''
177
+ module_name, class_name = get_module_class_names class_name, has_module
178
+ methods_template = generate_product_method_template(method_names, module_indent)
179
+ contents = generate_product_class_template(module_name, class_name, class_path, methods_template.chop, module_indent, has_module)
180
+ create_lib_directory(class_path)
181
+ File.open("./lib/#{class_path}.rb", 'w:UTF-8') { |f|f.puts contents }
182
+ end
183
+
184
+ def create_lib_directory(class_path)
185
+ return if Dir.exists? "./lib/#{File.dirname(class_path)}"
186
+ FileUtils.mkdir_p("./lib/#{File.dirname(class_path)}")
187
+ end
188
+
189
+ def has_module?(class_name)
190
+ class_name.include?('::') ? true : false
191
+ end
192
+
193
+ def get_module_class_names(class_name, has_module)
194
+ return '', class_name unless has_module
195
+ ret = class_name.match /(.*)::(.*)/
196
+ return ret[1], ret[2]
197
+ end
198
+
199
+ def generate_product_method_template(method_names, module_indent)
200
+ method_code = []
201
+ method_names.each do |method_name|
202
+ method_name = "self.#{method_name.gsub('@c', '')}" if is_class_method?(method_name)
203
+ method_code << "#{module_indent} def #{method_name}"
204
+ method_code << "#{module_indent} # TOOD: implement your code"
205
+ method_code << "#{module_indent} end"
206
+ method_code << ""
207
+ end
208
+ method_code.join("\n")
209
+ end
210
+
211
+ def is_class_method?(method_name)
212
+ method_name.match(/@c$/) ? true : false
213
+ end
214
+
215
+ def generate_product_class_template(module_name, class_name, class_path, methods_template, module_indent, has_module)
216
+ module_start = ""
217
+ module_end = ""
218
+ if has_module
219
+ module_start = "module #{module_name}"
220
+ module_end = "end"
221
+ end
222
+ ERB.new(PRODUCT_CLASS_TEMPLATE).result(binding)
223
+ end
224
+ end
225
+
226
+ class RSpecPiccoloError < StandardError; end
227
+ end