rspec_piccolo 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --color
2
- --format progress
1
+ --color
2
+ --format progress
data/README.md CHANGED
@@ -144,6 +144,326 @@ describe SomeClass do
144
144
  end
145
145
  ~~~
146
146
 
147
+ ### Case class_name, class_place, method_names with report
148
+ you can output report by -r(reportable) option.
149
+
150
+ ~~~
151
+ piccolo execute Hoge hoge/hige/hoge hoge hige -r
152
+ ~~~
153
+
154
+ product code
155
+ ~~~
156
+ # encoding: utf-8
157
+
158
+ class Hoge
159
+ def hoge
160
+ "hoge"
161
+ end
162
+ def hige
163
+ "hige"
164
+ end
165
+ end
166
+
167
+ ~~~
168
+
169
+ Result, spec/hoge/hige/hoge_spec.rb
170
+ ~~~
171
+ # encoding: utf-8
172
+ require "spec_helper"
173
+ require "hoge/hige/hoge"
174
+
175
+ describe Hoge do
176
+ REPORT = "rspec_report"
177
+ DIRS = File.path(__FILE__).gsub(/^.*\/spec\//, '').gsub(File.basename(__FILE__), '')
178
+ OUT_DIR = "./#{REPORT}/#{DIRS}"
179
+ REPORT_NAME = report_name = File.basename(__FILE__, ".rb")
180
+ REPORT_FILE = "#{OUT_DIR}#{REPORT_NAME}.tsv"
181
+
182
+ mkspec_report = Proc.new do
183
+ Dir.mkdir(REPORT) unless File.exists?(REPORT)
184
+ FileUtils.mkdir_p(OUT_DIR) unless File.exists?(OUT_DIR)
185
+ File.open(REPORT_FILE, "w") {|f|f.puts "method\tcase\ttitle\tsuccess\/failure"}
186
+ end.call
187
+
188
+ success = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\tsuccess"}}
189
+ failure = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\tfailure"}}
190
+
191
+ context :hoge do
192
+ cases = [
193
+ {
194
+ case_no: 1,
195
+ case_title: "case_title",
196
+ expected: "expected",
197
+ success_hook: success,
198
+ failure_hook: failure
199
+ },
200
+ ]
201
+
202
+ cases.each do |c|
203
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
204
+ begin
205
+ case_before c
206
+
207
+ # -- given --
208
+ hoge = Hoge.new
209
+
210
+ # -- when --
211
+ # TODO: implement execute code
212
+ # actual = hoge.hoge
213
+
214
+ # -- then --
215
+ # TODO: implement assertion code
216
+ # ret = expect(actual).to eq(c[:expected])
217
+ ensure
218
+ case_after c
219
+ sf_hook = ret ? c[:success_hook] : c[:failure_hook]
220
+ sf_hook.call(c)
221
+ end
222
+ end
223
+
224
+ def case_before(c)
225
+ # implement each case before
226
+ File.open(REPORT_FILE, "a") {|f|f.print "hoge\t#{c[:case_no]}\t#{c[:case_title]}"}
227
+ end
228
+
229
+ def case_after(c)
230
+ # implement each case after
231
+ end
232
+ end
233
+ end
234
+
235
+ context :hige do
236
+ cases = [
237
+ {
238
+ case_no: 1,
239
+ case_title: "case_title",
240
+ expected: "expected",
241
+ success_hook: success,
242
+ failure_hook: failure
243
+ },
244
+ ]
245
+
246
+ cases.each do |c|
247
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
248
+ begin
249
+ case_before c
250
+
251
+ # -- given --
252
+ hoge = Hoge.new
253
+
254
+ # -- when --
255
+ # TODO: implement execute code
256
+ # actual = hoge.hige
257
+
258
+ # -- then --
259
+ # TODO: implement assertion code
260
+ # ret = expect(actual).to eq(c[:expected])
261
+ ensure
262
+ case_after c
263
+ sf_hook = ret ? c[:success_hook] : c[:failure_hook]
264
+ sf_hook.call(c)
265
+ end
266
+ end
267
+
268
+ def case_before(c)
269
+ # implement each case before
270
+ File.open(REPORT_FILE, "a") {|f|f.print "hige\t#{c[:case_no]}\t#{c[:case_title]}"}
271
+ end
272
+
273
+ def case_after(c)
274
+ # implement each case after
275
+ end
276
+ end
277
+ end
278
+
279
+ end
280
+ ~~~
281
+
282
+ Edit manually, spec/hoge/hige/hoge_spec.rb
283
+ ~~~
284
+ # encoding: utf-8
285
+ require "spec_helper"
286
+ require "hoge/hige/hoge"
287
+
288
+ describe Hoge do
289
+ REPORT = "rspec_report"
290
+ DIRS = File.path(__FILE__).gsub(/^.*\/spec\//, '').gsub(File.basename(__FILE__), '')
291
+ OUT_DIR = "./#{REPORT}/#{DIRS}"
292
+ REPORT_NAME = report_name = File.basename(__FILE__, ".rb")
293
+ REPORT_FILE = "#{OUT_DIR}#{REPORT_NAME}.tsv"
294
+
295
+ mkspec_report = Proc.new do
296
+ Dir.mkdir(REPORT) unless File.exists?(REPORT)
297
+ FileUtils.mkdir_p(OUT_DIR) unless File.exists?(OUT_DIR)
298
+ File.open(REPORT_FILE, "w") {|f|f.puts "method\tcase\ttitle\tsuccess\/failure"}
299
+ end.call
300
+
301
+ success = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\tsuccess"}}
302
+ failure = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\tfailure"}}
303
+
304
+ context :hoge do
305
+ cases = [
306
+ {
307
+ case_no: 1,
308
+ case_title: "valid",
309
+ expected: "hoge",
310
+ success_hook: success,
311
+ failure_hook: failure
312
+ },
313
+ {
314
+ case_no: 2,
315
+ case_title: "invalid",
316
+ expected: "hige",
317
+ success_hook: success,
318
+ failure_hook: failure
319
+ },
320
+ ]
321
+
322
+ cases.each do |c|
323
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
324
+ begin
325
+ case_before c
326
+
327
+ # -- given --
328
+ hoge = Hoge.new
329
+
330
+ # -- when --
331
+ actual = hoge.hoge
332
+
333
+ # -- then --
334
+ ret = expect(actual).to eq(c[:expected])
335
+ ensure
336
+ case_after c
337
+ sf_hook = ret ? c[:success_hook] : c[:failure_hook]
338
+ sf_hook.call(c)
339
+ end
340
+ end
341
+
342
+ def case_before(c)
343
+ # implement each case before
344
+ File.open(REPORT_FILE, "a") {|f|f.print "hoge\t#{c[:case_no]}\t#{c[:case_title]}"}
345
+ end
346
+
347
+ def case_after(c)
348
+ # implement each case after
349
+ end
350
+ end
351
+ end
352
+
353
+ context :hige do
354
+ cases = [
355
+ {
356
+ case_no: 1,
357
+ case_title: "valid",
358
+ expected: "hige",
359
+ success_hook: success,
360
+ failure_hook: failure
361
+ },
362
+ {
363
+ case_no: 2,
364
+ case_title: "invalid",
365
+ expected: "hoge",
366
+ success_hook: success,
367
+ failure_hook: failure
368
+ },
369
+ ]
370
+
371
+ cases.each do |c|
372
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
373
+ begin
374
+ case_before c
375
+
376
+ # -- given --
377
+ hoge = Hoge.new
378
+
379
+ # -- when --
380
+ actual = hoge.hige
381
+
382
+ # -- then --
383
+ ret = expect(actual).to eq(c[:expected])
384
+ ensure
385
+ case_after c
386
+ sf_hook = ret ? c[:success_hook] : c[:failure_hook]
387
+ sf_hook.call(c)
388
+ end
389
+ end
390
+
391
+ def case_before(c)
392
+ # implement each case before
393
+ File.open(REPORT_FILE, "a") {|f|f.print "hige\t#{c[:case_no]}\t#{c[:case_title]}"}
394
+ end
395
+
396
+ def case_after(c)
397
+ # implement each case after
398
+ end
399
+ end
400
+ end
401
+
402
+ end
403
+ ~~~
404
+
405
+ Execute rspec command.
406
+ ~~~
407
+ $rspec
408
+ Run options: include {:focus=>true}
409
+
410
+ All examples were filtered out; ignoring {:focus=>true}
411
+ .F.F
412
+
413
+ Failures:
414
+
415
+ 1) Hoge hoge |case_no=2|case_title=invalid
416
+ Failure/Error: ret = expect(actual).to eq(c[:expected])
417
+
418
+ expected: "hige"
419
+ got: "hoge"
420
+
421
+ (compared using ==)
422
+ # ./spec/hoge/hige/hoge_spec.rb:51:in `block (4 levels) in <top (required)>'
423
+
424
+ 2) Hoge hige |case_no=2|case_title=invalid
425
+ Failure/Error: ret = expect(actual).to eq(c[:expected])
426
+
427
+ expected: "hoge"
428
+ got: "hige"
429
+
430
+ (compared using ==)
431
+ # ./spec/hoge/hige/hoge_spec.rb:100:in `block (4 levels) in <top (required)>'
432
+
433
+ Finished in 0.009 seconds
434
+ 4 examples, 2 failures
435
+
436
+ Failed examples:
437
+
438
+ rspec ./spec/hoge/hige/hoge_spec.rb:40 # Hoge hoge |case_no=2|case_title=invalid
439
+ rspec ./spec/hoge/hige/hoge_spec.rb:89 # Hoge hige |case_no=2|case_title=invalid
440
+ ~~~
441
+
442
+ you get tsv report.
443
+ ~~~
444
+ tree
445
+ ┠ lib
446
+ ┃┗ hoge
447
+ ┃ ┗ hige
448
+ ┃ ┗ hoge.rb
449
+ ┠ rspec_report
450
+ ┃┗ hoge
451
+ ┃ ┗ hige
452
+ ┃ ┗ hoge_spec.tsv
453
+ ┗ spec
454
+ ┠ hoge
455
+ ┃ ┗ hige
456
+ ┃ ┗ hoge_spec.rb
457
+ ┗ spec_helper.rb
458
+
459
+ $cat rspec_report/hoge/hige/hoge_spec.tsv
460
+ method case title success/failure
461
+ hoge 1 valid success
462
+ hoge 2 invalid failure
463
+ hige 1 valid success
464
+ hige 2 invalid failure
465
+ ~~~
466
+
147
467
  ## Pragmatic Usase
148
468
  You can edit your spec template like this sample.
149
469
 
@@ -315,6 +635,12 @@ Finished in 0.0045 seconds
315
635
  4 examples, 0 failures
316
636
  ~~~
317
637
 
638
+ ## History
639
+ * version 0.0.5 : add reportable option.
640
+ * version 0.0.4 : you can get multi level directory spec case.
641
+ * version 0.0.2 : fix bin.(use thor).
642
+ * version 0.0.1 : first release.
643
+
318
644
  ## Contributing
319
645
 
320
646
  1. Fork it
@@ -8,6 +8,7 @@ module RSpecPiccolo
8
8
  class CLI < Thor
9
9
  class_option :help, :type => :boolean, :aliases => 'h', :desc => 'help message.'
10
10
  class_option :version, :type => :boolean, :aliases => 'v', :desc => 'version'
11
+ class_option :reportable, :type => :boolean, :aliases => 'r', :desc => 'reportable'
11
12
  default_task :execute
12
13
 
13
14
  desc "execute", "generate rspec spec_file with list_cases"
@@ -16,7 +17,7 @@ module RSpecPiccolo
16
17
  class_path = args[1]
17
18
  method_names = args[2..-1] if args.size > 2
18
19
 
19
- RSpecPiccolo::Core.new.generate class_name, class_path, method_names
20
+ RSpecPiccolo::Core.new.generate class_name, class_path, method_names, options[:reportable]
20
21
  end
21
22
 
22
23
  desc "version", "version"
@@ -13,10 +13,28 @@ require "spec_helper"
13
13
  require "<%=class_path%>"
14
14
 
15
15
  describe <%=class_name%> do
16
+ <%=reportable_prepare%>
16
17
  <%=methods_template%>
17
18
  end
18
19
  EOS
19
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
+
20
38
  #== RSPec method template
21
39
  METHOD_TEMPLATE=<<-EOS
22
40
  context :<%=method_name%> do
@@ -24,7 +42,8 @@ end
24
42
  {
25
43
  case_no: 1,
26
44
  case_title: "case_title",
27
- expected: "expected"
45
+ expected: "expected",
46
+ <%=reportable_case%>
28
47
  },
29
48
  ]
30
49
 
@@ -42,14 +61,16 @@ end
42
61
 
43
62
  # -- then --
44
63
  # TODO: implement assertion code
45
- # expect(actual).to eq(c[:expected])
64
+ # ret = expect(actual).to eq(c[:expected])
46
65
  ensure
47
66
  case_after c
67
+ <%=reportable_case_after%>
48
68
  end
49
69
  end
50
70
 
51
71
  def case_before(c)
52
72
  # implement each case before
73
+ <%=reportable_case_before%>
53
74
  end
54
75
 
55
76
  def case_after(c)
@@ -58,6 +79,18 @@ end
58
79
  end
59
80
  end
60
81
  EOS
82
+
83
+ REPORTABLE_CASE =<<-EOS
84
+ success_hook: success,
85
+ failure_hook: failure
86
+ EOS
87
+
88
+ REPORTABLE_CASE_BEFORE = ' File.open(REPORT_FILE, "a") {|f|f.print "method_name\\t#{c[:case_no]}\\t#{c[:case_title]}"}'
89
+
90
+ REPORTABLE_CASE_AFTER =<<-EOS
91
+ sf_hook = ret ? c[:success_hook] : c[:failure_hook]
92
+ sf_hook.call(c)
93
+ EOS
61
94
 
62
95
  #== initialize
63
96
  def initialize
@@ -69,11 +102,11 @@ EOS
69
102
  #- class_name: spec's module+class full name
70
103
  #- class_path: spec's class_path(if you want to create spec/hoge_spec.rb, you should set 'hoge_spec.rb')
71
104
  #- method_names: target class's method list
72
- def generate(class_name, class_path, method_names)
105
+ def generate(class_name, class_path, method_names, reportable = false)
73
106
  validate_class_name class_name
74
107
  validate_class_path class_path
75
- methods_template = generate_method_template(class_name, method_names)
76
- @contents = generate_class_template(class_name, class_path, methods_template)
108
+ methods_template = generate_method_template(class_name, method_names, reportable)
109
+ @contents = generate_class_template(class_name, class_path, methods_template, reportable)
77
110
  create_spec_directory class_path
78
111
  File.open("./spec/#{class_path}_spec.rb", "w") {|f|f.puts @contents}
79
112
  end
@@ -89,11 +122,15 @@ EOS
89
122
  raise RSpecPiccoloError.new("class_path is not allowed empty value") if class_path.empty?
90
123
  end
91
124
 
92
- def generate_method_template(class_name, method_names)
125
+ def generate_method_template(class_name, method_names, reportable)
93
126
  return "" if method_names.nil?
127
+ reportable_case = reportable ? REPORTABLE_CASE.chop : ""
94
128
  instance_name = class_name.gsub('::', '_').underscore.downcase
95
129
  method_templates = []
96
130
  method_names.each do |method_name|
131
+ reportable_case_before = reportable ? REPORTABLE_CASE_BEFORE.dup : ""
132
+ reportable_case_before.gsub!("method_name", method_name)
133
+ reportable_case_after = reportable ? REPORTABLE_CASE_AFTER.dup.chop : ""
97
134
  method_templates << ERB.new(METHOD_TEMPLATE).result(binding)
98
135
  end
99
136
  method_templates.join("\n")
@@ -104,7 +141,8 @@ EOS
104
141
  FileUtils.mkdir_p("./spec/#{File.dirname(class_path)}")
105
142
  end
106
143
 
107
- def generate_class_template(class_name, class_path, methods_template)
144
+ def generate_class_template(class_name, class_path, methods_template, reportable)
145
+ reportable_prepare = reportable ? REPORTABLE_PREPARE : ""
108
146
  ERB.new(CLASS_TEMPLATE).result(binding)
109
147
  end
110
148
  end