transpec 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +35 -3
  4. data/README.md.erb +35 -3
  5. data/lib/transpec/cli.rb +50 -4
  6. data/lib/transpec/commit_message.rb +25 -0
  7. data/lib/transpec/git.rb +18 -0
  8. data/lib/transpec/record.rb +28 -0
  9. data/lib/transpec/report.rb +109 -0
  10. data/lib/transpec/rewriter.rb +17 -8
  11. data/lib/transpec/syntax.rb +25 -22
  12. data/lib/transpec/syntax/be_close.rb +6 -0
  13. data/lib/transpec/syntax/double.rb +5 -0
  14. data/lib/transpec/syntax/matcher.rb +17 -1
  15. data/lib/transpec/syntax/method_stub.rb +40 -8
  16. data/lib/transpec/syntax/raise_error.rb +17 -0
  17. data/lib/transpec/syntax/send_node_syntax.rb +4 -0
  18. data/lib/transpec/syntax/should.rb +23 -2
  19. data/lib/transpec/syntax/should_receive.rb +54 -2
  20. data/lib/transpec/version.rb +2 -2
  21. data/spec/.rubocop.yml +1 -1
  22. data/spec/support/shared_context.rb +10 -0
  23. data/spec/transpec/cli_spec.rb +76 -29
  24. data/spec/transpec/commit_message_spec.rb +63 -0
  25. data/spec/transpec/configuration_spec.rb +1 -1
  26. data/spec/transpec/git_spec.rb +114 -38
  27. data/spec/transpec/record_spec.rb +18 -0
  28. data/spec/transpec/report_spec.rb +89 -0
  29. data/spec/transpec/rewriter_spec.rb +5 -0
  30. data/spec/transpec/syntax/be_close_spec.rb +10 -1
  31. data/spec/transpec/syntax/double_spec.rb +10 -0
  32. data/spec/transpec/syntax/matcher_spec.rb +31 -0
  33. data/spec/transpec/syntax/method_stub_spec.rb +53 -44
  34. data/spec/transpec/syntax/raise_error_spec.rb +64 -24
  35. data/spec/transpec/syntax/should_receive_spec.rb +67 -7
  36. data/spec/transpec/syntax/should_spec.rb +26 -0
  37. data/tasks/test.rake +27 -12
  38. metadata +11 -2
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'transpec/record'
5
+
6
+ module Transpec
7
+ describe Record do
8
+ subject(:report) { Record.new(original_syntax, converted_syntax) }
9
+ let(:original_syntax) { 'obj.should' }
10
+ let(:converted_syntax) { 'expect(obj).to' }
11
+
12
+ describe '#to_s' do
13
+ it 'returns "`original syntax` -> `converted syntax`"' do
14
+ report.to_s.should == '`obj.should` -> `expect(obj).to`'
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,89 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'transpec/report'
5
+ require 'transpec/record'
6
+ require 'transpec/syntax'
7
+
8
+ module Transpec
9
+ describe Report do
10
+ subject(:report) { Report.new }
11
+
12
+ before do
13
+ report.records << Record.new('obj.stub(:message)', 'allow(obj).to receive(:message)')
14
+ report.records << Record.new('obj.should', 'expect(obj).to')
15
+ report.records << Record.new('obj.should', 'expect(obj).to')
16
+ report.invalid_context_errors <<
17
+ Syntax::InvalidContextError.new(double('range'), '#should', '#expect')
18
+ end
19
+
20
+ describe '#unique_record_counts' do
21
+ subject(:unique_record_counts) { report.unique_record_counts }
22
+
23
+ it 'returns counts for unique records' do
24
+ unique_record_counts.size.should == 2
25
+ end
26
+
27
+ it 'returns hash with record as key and count as value' do
28
+ unique_record_counts.each do |record, count|
29
+ case record
30
+ when Record.new('obj.stub(:message)', 'allow(obj).to receive(:message)')
31
+ count.should == 1
32
+ when Record.new('obj.should', 'expect(obj).to')
33
+ count.should == 2
34
+ end
35
+ end
36
+ end
37
+
38
+ it 'is sorted by count in descending order' do
39
+ unique_record_counts.values.should == [2, 1]
40
+ end
41
+ end
42
+
43
+ describe '#summary' do
44
+ it 'returns summary string' do
45
+ report.summary.should == <<-END.gsub(/^\s+\|/, '')
46
+ |2 conversions
47
+ | from: obj.should
48
+ | to: expect(obj).to
49
+ |1 conversion
50
+ | from: obj.stub(:message)
51
+ | to: allow(obj).to receive(:message)
52
+ END
53
+ end
54
+
55
+ context 'when :separate_by_blank_line option is enabled' do
56
+ it 'separates conversion entries by blank line' do
57
+ report.summary(separate_by_blank_line: true).should == <<-END.gsub(/^\s+\|/, '')
58
+ |2 conversions
59
+ | from: obj.should
60
+ | to: expect(obj).to
61
+ |
62
+ |1 conversion
63
+ | from: obj.stub(:message)
64
+ | to: allow(obj).to receive(:message)
65
+ END
66
+ end
67
+ end
68
+
69
+ context 'when :bullet option is specified' do
70
+ it 'adds the bullet for each conversion entry' do
71
+ report.summary(bullet: '-').should == <<-END.gsub(/^\s+\|/, '')
72
+ |- 2 conversions
73
+ | from: obj.should
74
+ | to: expect(obj).to
75
+ |- 1 conversion
76
+ | from: obj.stub(:message)
77
+ | to: allow(obj).to receive(:message)
78
+ END
79
+ end
80
+ end
81
+ end
82
+
83
+ describe '#stats' do
84
+ it 'returns stats string' do
85
+ report.stats.should == '3 conversions, 1 incomplete, 0 errors'
86
+ end
87
+ end
88
+ end
89
+ end
@@ -68,6 +68,11 @@ module Transpec
68
68
  it 'rewrites all targets properly' do
69
69
  should == expected_source
70
70
  end
71
+
72
+ it 'adds records for only completed conversions' do
73
+ rewriter.rewrite(source)
74
+ rewriter.report.records.count.should == 2
75
+ end
71
76
  end
72
77
 
73
78
  context 'when the source has a monkey-patched expectation outside of example group context' do
@@ -21,6 +21,10 @@ module Transpec
21
21
  end
22
22
 
23
23
  describe '#convert_to_be_within!' do
24
+ before do
25
+ be_close_object.convert_to_be_within!
26
+ end
27
+
24
28
  context 'when it is `be_close(expected, delta)` form' do
25
29
  let(:source) do
26
30
  <<-END
@@ -39,9 +43,14 @@ module Transpec
39
43
  end
40
44
 
41
45
  it 'converts into `be_within(delta).of(expected)` form' do
42
- be_close_object.convert_to_be_within!
43
46
  rewritten_source.should == expected_source
44
47
  end
48
+
49
+ it 'adds record "`be_close(expected, delta)` -> `be_within(delta).of(expected)`"' do
50
+ record = be_close_object.report.records.first
51
+ record.original_syntax.should == 'be_close(expected, delta)'
52
+ record.converted_syntax.should == 'be_within(delta).of(expected)'
53
+ end
45
54
  end
46
55
  end
47
56
  end
@@ -63,6 +63,12 @@ module Transpec
63
63
  it 'replaces with #double' do
64
64
  rewritten_source.should == expected_source
65
65
  end
66
+
67
+ it "adds record \"`#{method}('something')` -> `double('something')`\"" do
68
+ record = double_object.report.records.first
69
+ record.original_syntax.should == "#{method}('something')"
70
+ record.converted_syntax.should == "double('something')"
71
+ end
66
72
  end
67
73
  end
68
74
 
@@ -79,6 +85,10 @@ module Transpec
79
85
  it 'does nothing' do
80
86
  rewritten_source.should == source
81
87
  end
88
+
89
+ it 'reports nothing' do
90
+ double_object.report.records.should be_empty
91
+ end
82
92
  end
83
93
  end
84
94
  end
@@ -13,6 +13,8 @@ module Transpec
13
13
  Matcher.new(should_object.matcher_node, source_rewriter)
14
14
  end
15
15
 
16
+ let(:record) { matcher.report.records.first }
17
+
16
18
  describe '#method_name' do
17
19
  context 'when it is operator matcher' do
18
20
  let(:source) do
@@ -89,6 +91,11 @@ module Transpec
89
91
  rewritten_source.should == expected_source
90
92
  end
91
93
 
94
+ it 'adds record "`== expected` -> `eq(expected)`"' do
95
+ record.original_syntax.should == '== expected'
96
+ record.converted_syntax.should == 'eq(expected)'
97
+ end
98
+
92
99
  # Operator methods allow their argument to be in the next line,
93
100
  # but non-operator methods do not.
94
101
  #
@@ -189,6 +196,11 @@ module Transpec
189
196
  it 'converts into `eq(1)` form' do
190
197
  rewritten_source.should == expected_source
191
198
  end
199
+
200
+ it 'adds record "`== expected` -> `eq(expected)`"' do
201
+ record.original_syntax.should == '== expected'
202
+ record.converted_syntax.should == 'eq(expected)'
203
+ end
192
204
  end
193
205
 
194
206
  context 'when it is `be.==(1)` form' do
@@ -314,6 +326,11 @@ module Transpec
314
326
  it "converts into `be #{operator} 1` form" do
315
327
  rewritten_source.should == expected_source
316
328
  end
329
+
330
+ it "adds record \"`#{operator} expected` -> `be #{operator} expected`\"" do
331
+ record.original_syntax.should == "#{operator} expected"
332
+ record.converted_syntax.should == "be #{operator} expected"
333
+ end
317
334
  end
318
335
 
319
336
  context "when it is `be #{operator} 1` form" do
@@ -328,6 +345,10 @@ module Transpec
328
345
  it 'does nothing' do
329
346
  rewritten_source.should == source
330
347
  end
348
+
349
+ it 'reports nothing' do
350
+ matcher.report.records.should be_empty
351
+ end
331
352
  end
332
353
  end
333
354
 
@@ -351,6 +372,11 @@ module Transpec
351
372
  it 'converts into `match(/pattern/)` form' do
352
373
  rewritten_source.should == expected_source
353
374
  end
375
+
376
+ it 'adds record "`=~ /pattern/` -> `match(/pattern/)`"' do
377
+ record.original_syntax.should == '=~ /pattern/'
378
+ record.converted_syntax.should == 'match(/pattern/)'
379
+ end
354
380
  end
355
381
 
356
382
  context 'when it is `=~/pattern/` form' do
@@ -417,6 +443,11 @@ module Transpec
417
443
  it 'converts into `match_array([1, 2])` form' do
418
444
  rewritten_source.should == expected_source
419
445
  end
446
+
447
+ it 'adds record "`=~ [1, 2]` -> `match_array([1, 2])`"' do
448
+ record.original_syntax.should == '=~ [1, 2]'
449
+ record.converted_syntax.should == 'match_array([1, 2])'
450
+ end
420
451
  end
421
452
 
422
453
  context 'when it is `=~[1, 2]` form' do
@@ -20,6 +20,8 @@ module Transpec
20
20
  fail 'No method stub node is found!'
21
21
  end
22
22
 
23
+ let(:record) { method_stub_object.report.records.first }
24
+
23
25
  describe '.target_node?' do
24
26
  let(:send_node) do
25
27
  ast.each_descendent_node do |node|
@@ -91,6 +93,7 @@ module Transpec
91
93
  describe '#allowize!' do
92
94
  before do
93
95
  method_stub_object.context.stub(:in_example_group?).and_return(true)
96
+ method_stub_object.allowize!
94
97
  end
95
98
 
96
99
  [:stub, :stub!].each do |method|
@@ -112,9 +115,13 @@ module Transpec
112
115
  end
113
116
 
114
117
  it 'converts into `allow(subject).to receive(:method)` form' do
115
- method_stub_object.allowize!
116
118
  rewritten_source.should == expected_source
117
119
  end
120
+
121
+ it "adds record \"`obj.#{method}(:message)` -> `allow(obj).to receive(:message)`\"" do
122
+ record.original_syntax.should == "obj.#{method}(:message)"
123
+ record.converted_syntax.should == 'allow(obj).to receive(:message)'
124
+ end
118
125
  end
119
126
 
120
127
  context "when it is `subject.#{method}(:method).and_return(value)` form" do
@@ -135,7 +142,6 @@ module Transpec
135
142
  end
136
143
 
137
144
  it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
138
- method_stub_object.allowize!
139
145
  rewritten_source.should == expected_source
140
146
  end
141
147
  end
@@ -158,7 +164,6 @@ module Transpec
158
164
  end
159
165
 
160
166
  it 'converts into `allow(subject).to receive(:method).and_raise(RuntimeError)` form' do
161
- method_stub_object.allowize!
162
167
  rewritten_source.should == expected_source
163
168
  end
164
169
  end
@@ -191,7 +196,6 @@ module Transpec
191
196
  end
192
197
 
193
198
  it 'keeps the style as far as possible' do
194
- method_stub_object.allowize!
195
199
  rewritten_source.should == expected_source
196
200
  end
197
201
  end
@@ -214,9 +218,14 @@ module Transpec
214
218
  end
215
219
 
216
220
  it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
217
- method_stub_object.allowize!
218
221
  rewritten_source.should == expected_source
219
222
  end
223
+
224
+ it 'adds record ' +
225
+ "\"`obj.#{method}(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
226
+ record.original_syntax.should == "obj.#{method}(:message => value)"
227
+ record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
228
+ end
220
229
  end
221
230
 
222
231
  context "when it is `subject.#{method}(method: value)` form" do
@@ -237,9 +246,14 @@ module Transpec
237
246
  end
238
247
 
239
248
  it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
240
- method_stub_object.allowize!
241
249
  rewritten_source.should == expected_source
242
250
  end
251
+
252
+ it 'adds record ' +
253
+ "\"`obj.#{method}(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
254
+ record.original_syntax.should == "obj.#{method}(:message => value)"
255
+ record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
256
+ end
243
257
  end
244
258
 
245
259
  context "when it is `subject.#{method}(:a_method => a_value, b_method => b_value)` form" do
@@ -262,10 +276,15 @@ module Transpec
262
276
 
263
277
  it 'converts into `allow(subject).to receive(:a_method).and_return(a_value)` ' +
264
278
  'and `allow(subject).to receive(:b_method).and_return(b_value)` form' do
265
- method_stub_object.allowize!
266
279
  rewritten_source.should == expected_source
267
280
  end
268
281
 
282
+ it 'adds record ' +
283
+ "\"`obj.#{method}(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
284
+ record.original_syntax.should == "obj.#{method}(:message => value)"
285
+ record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
286
+ end
287
+
269
288
  context 'when the statement continues over multi lines' do
270
289
  let(:source) do
271
290
  <<-END
@@ -291,7 +310,6 @@ module Transpec
291
310
  end
292
311
 
293
312
  it 'keeps the style except around the hash' do
294
- method_stub_object.allowize!
295
313
  rewritten_source.should == expected_source
296
314
  end
297
315
  end
@@ -309,9 +327,12 @@ module Transpec
309
327
  end
310
328
 
311
329
  it 'does nothing' do
312
- method_stub_object.allowize!
313
330
  rewritten_source.should == source
314
331
  end
332
+
333
+ it 'reports nothing' do
334
+ method_stub_object.report.records.should be_empty
335
+ end
315
336
  end
316
337
  end
317
338
 
@@ -334,9 +355,14 @@ module Transpec
334
355
  end
335
356
 
336
357
  it 'converts into `allow_any_instance_of(SomeClass).to receive(:method)` form' do
337
- method_stub_object.allowize!
338
358
  rewritten_source.should == expected_source
339
359
  end
360
+
361
+ it "adds record \"`SomeClass.any_instance.#{method}(:message)` " +
362
+ '-> `allow_any_instance_of(obj).to receive(:message)`"' do
363
+ record.original_syntax.should == "SomeClass.any_instance.#{method}(:message)"
364
+ record.converted_syntax.should == 'allow_any_instance_of(SomeClass).to receive(:message)'
365
+ end
340
366
  end
341
367
  end
342
368
 
@@ -351,24 +377,12 @@ module Transpec
351
377
  end
352
378
 
353
379
  it 'does nothing' do
354
- method_stub_object.allowize!
355
380
  rewritten_source.should == source
356
381
  end
357
- end
358
- end
359
-
360
- context 'when already replaced deprecated method' do
361
- let(:source) do
362
- <<-END
363
- it 'responds to #foo' do
364
- subject.stub!(:foo)
365
- end
366
- END
367
- end
368
382
 
369
- it 'raises error' do
370
- method_stub_object.replace_deprecated_method!
371
- -> { method_stub_object.allowize! }.should raise_error
383
+ it 'reports nothing' do
384
+ method_stub_object.report.records.should be_empty
385
+ end
372
386
  end
373
387
  end
374
388
  end
@@ -376,6 +390,7 @@ module Transpec
376
390
  describe '#replace_deprecated_method!' do
377
391
  before do
378
392
  method_stub_object.context.stub(:in_example_group?).and_return(true)
393
+ method_stub_object.replace_deprecated_method!
379
394
  end
380
395
 
381
396
  [
@@ -400,9 +415,14 @@ module Transpec
400
415
  end
401
416
 
402
417
  it "replaces with ##{replacement_method}" do
403
- method_stub_object.replace_deprecated_method!
404
418
  rewritten_source.should == expected_source
405
419
  end
420
+
421
+ it 'adds record ' +
422
+ "\"`obj.#{method}(:message)` -> `obj.#{replacement_method}(:message)`\"" do
423
+ record.original_syntax.should == "obj.#{method}(:message)"
424
+ record.converted_syntax.should == "obj.#{replacement_method}(:message)"
425
+ end
406
426
  end
407
427
  end
408
428
 
@@ -420,24 +440,12 @@ module Transpec
420
440
  end
421
441
 
422
442
  it 'does nothing' do
423
- method_stub_object.replace_deprecated_method!
424
443
  rewritten_source.should == source
425
444
  end
426
- end
427
- end
428
445
 
429
- context 'when already allowized' do
430
- let(:source) do
431
- <<-END
432
- it 'responds to #foo' do
433
- subject.stub!(:foo)
434
- end
435
- END
436
- end
437
-
438
- it 'raises error' do
439
- method_stub_object.allowize!
440
- -> { method_stub_object.replace_deprecated_method! }.should raise_error
446
+ it 'reports nothing' do
447
+ method_stub_object.report.records.should be_empty
448
+ end
441
449
  end
442
450
  end
443
451
  end
@@ -495,6 +503,10 @@ module Transpec
495
503
  end
496
504
 
497
505
  describe '#remove_allowance_for_no_message!' do
506
+ before do
507
+ method_stub_object.remove_allowance_for_no_message!
508
+ end
509
+
498
510
  context 'when it is `subject.stub(:method).any_number_of_times` form' do
499
511
  let(:source) do
500
512
  <<-END
@@ -513,7 +525,6 @@ module Transpec
513
525
  end
514
526
 
515
527
  it 'removes `.any_number_of_times`' do
516
- method_stub_object.remove_allowance_for_no_message!
517
528
  rewritten_source.should == expected_source
518
529
  end
519
530
  end
@@ -536,7 +547,6 @@ module Transpec
536
547
  end
537
548
 
538
549
  it 'removes `.at_least(0)`' do
539
- method_stub_object.remove_allowance_for_no_message!
540
550
  rewritten_source.should == expected_source
541
551
  end
542
552
  end
@@ -551,7 +561,6 @@ module Transpec
551
561
  end
552
562
 
553
563
  it 'does nothing' do
554
- method_stub_object.remove_allowance_for_no_message!
555
564
  rewritten_source.should == source
556
565
  end
557
566
  end