transpec 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -157,12 +157,16 @@ module Transpec
157
157
  @report.records << record_class.new(self, negative_form_of_to)
158
158
  end
159
159
 
160
- class ExpectRecord < Record
160
+ class ExpectBaseRecord < Record
161
161
  def initialize(should_receive, negative_form_of_to)
162
162
  @should_receive = should_receive
163
163
  @negative_form_of_to = negative_form_of_to
164
164
  end
165
165
 
166
+ def syntax_name
167
+ fail NotImplementedError
168
+ end
169
+
166
170
  def original_syntax
167
171
  syntax = if @should_receive.any_instance?
168
172
  'Klass.any_instance.'
@@ -174,42 +178,32 @@ module Transpec
174
178
 
175
179
  def converted_syntax
176
180
  syntax = if @should_receive.any_instance?
177
- 'expect_any_instance_of(Klass).'
181
+ "#{syntax_name}_any_instance_of(Klass)."
178
182
  else
179
- 'expect(obj).'
183
+ "#{syntax_name}(obj)."
180
184
  end
181
185
  syntax << (@should_receive.positive? ? 'to' : @negative_form_of_to)
182
186
  syntax << ' receive(:message)'
183
187
  end
184
188
  end
185
189
 
186
- class AllowRecord < Record
187
- def initialize(should_receive, negative_form_of_to)
188
- @should_receive = should_receive
189
- @negative_form_of_to = negative_form_of_to
190
+ class ExpectRecord < ExpectBaseRecord
191
+ def syntax_name
192
+ 'expect'
193
+ end
194
+ end
195
+
196
+ class AllowRecord < ExpectBaseRecord
197
+ def syntax_name
198
+ 'allow'
190
199
  end
191
200
 
192
201
  def original_syntax
193
- syntax = if @should_receive.any_instance?
194
- 'Klass.any_instance.'
195
- else
196
- 'obj.'
197
- end
198
- syntax << "#{@should_receive.method_name}(:message)"
202
+ syntax = super
199
203
  syntax << '.any_number_of_times' if @should_receive.any_number_of_times?
200
204
  syntax << '.at_least(0)' if @should_receive.at_least_zero?
201
205
  syntax
202
206
  end
203
-
204
- def converted_syntax
205
- syntax = if @should_receive.any_instance?
206
- 'allow_any_instance_of(Klass).'
207
- else
208
- 'allow(obj).'
209
- end
210
- syntax << (@should_receive.positive? ? 'to' : @negative_form_of_to)
211
- syntax << ' receive(:message)'
212
- end
213
207
  end
214
208
 
215
209
  class StubRecord < Record
@@ -228,20 +222,6 @@ module Transpec
228
222
  'obj.stub(:message)'
229
223
  end
230
224
  end
231
-
232
- class AnyInstanceBlockRecord < Record
233
- def initialize(should_receive, *)
234
- @should_receive = should_receive
235
- end
236
-
237
- def original_syntax
238
- "Klass.any_instance.#{@should_receive.method_name}(:message) { |arg| }"
239
- end
240
-
241
- def converted_syntax
242
- "Klass.any_instance.#{@should_receive.method_name}(:message) { |instance, arg| }"
243
- end
244
- end
245
225
  end
246
226
  end
247
227
  end
data/lib/transpec/util.rb CHANGED
@@ -60,6 +60,12 @@ module Transpec
60
60
  source[0] == '(' && source[-1] == ')'
61
61
  end
62
62
 
63
+ def first_block_arg_name(block_node)
64
+ args_node = block_node.children[1]
65
+ first_arg_node = args_node.children.first
66
+ first_arg_node.children.first
67
+ end
68
+
63
69
  def block_node_taken_by_method(node)
64
70
  parent_node = node.parent_node
65
71
  return nil unless parent_node
@@ -4,7 +4,7 @@ module Transpec
4
4
  # http://semver.org/
5
5
  module Version
6
6
  MAJOR = 1
7
- MINOR = 7
7
+ MINOR = 8
8
8
  PATCH = 0
9
9
 
10
10
  def self.to_s
@@ -18,6 +18,7 @@ module Transpec
18
18
  [:convert_deprecated_method?, true],
19
19
  [:parenthesize_matcher_arg?, true],
20
20
  [:add_receiver_arg_to_any_instance_implementation_block?, true],
21
+ [:convert_stub_with_hash_to_stub_and_return?, false],
21
22
  [:forced?, false],
22
23
  [:skip_dynamic_analysis?, false],
23
24
  [:negative_form_of_to, 'not_to'],
@@ -78,6 +78,10 @@ module Transpec
78
78
  END
79
79
  end
80
80
 
81
+ before do
82
+ configuration.convert_stub_with_hash_to_stub_and_return = true
83
+ end
84
+
81
85
  it 'converts all targets properly' do
82
86
  should == expected_source
83
87
  end
@@ -626,13 +630,59 @@ module Transpec
626
630
  context 'and Configuration#convert_deprecated_method? is true' do
627
631
  before { configuration.convert_deprecated_method = true }
628
632
 
629
- include_examples 'invokes MethodStub#allowize!'
630
- include_examples 'does not invoke MethodStub#convert_deprecated_method!'
631
- include_examples 'invokes MethodStub#remove_allowance_for_no_message!'
633
+ context 'and MethodStub#hash_arg? is false' do
634
+ before { method_stub_object.stub(:hash_arg?).and_return(false) }
635
+ include_examples 'invokes MethodStub#allowize!'
636
+ include_examples 'does not invoke MethodStub#convert_deprecated_method!'
637
+ include_examples 'invokes MethodStub#remove_allowance_for_no_message!'
638
+ end
639
+
640
+ context 'and MethodStub#hash_arg? is true' do
641
+ before { method_stub_object.stub(:hash_arg?).and_return(true) }
642
+
643
+ context 'and Configuration#convert_stub_with_hash_to_stub_and_return? is true' do
644
+ before { configuration.convert_stub_with_hash_to_stub_and_return = true }
645
+
646
+ context 'and RSpecVersion#receive_messages_available? is true' do
647
+ before { rspec_version.stub(:receive_messages_available?).and_return(true) }
648
+ include_examples 'invokes MethodStub#allowize!'
649
+ include_examples 'does not invoke MethodStub#convert_deprecated_method!'
650
+ include_examples 'invokes MethodStub#remove_allowance_for_no_message!'
651
+ end
652
+
653
+ context 'and RSpecVersion#receive_messages_available? is false' do
654
+ before { rspec_version.stub(:receive_messages_available?).and_return(false) }
655
+ include_examples 'invokes MethodStub#allowize!'
656
+ include_examples 'does not invoke MethodStub#convert_deprecated_method!'
657
+ include_examples 'invokes MethodStub#remove_allowance_for_no_message!'
658
+ end
659
+ end
660
+
661
+ context 'and Configuration#convert_stub_with_hash_to_stub_and_return? is false' do
662
+ before { configuration.convert_stub_with_hash_to_stub_and_return = false }
663
+
664
+ context 'and RSpecVersion#receive_messages_available? is true' do
665
+ before { rspec_version.stub(:receive_messages_available?).and_return(true) }
666
+ include_examples 'invokes MethodStub#allowize!'
667
+ include_examples 'does not invoke MethodStub#convert_deprecated_method!'
668
+ include_examples 'invokes MethodStub#remove_allowance_for_no_message!'
669
+ end
670
+
671
+ context 'and RSpecVersion#receive_messages_available? is false' do
672
+ before { rspec_version.stub(:receive_messages_available?).and_return(false) }
673
+ include_examples 'does not invoke MethodStub#allowize!'
674
+ include_examples 'invokes MethodStub#convert_deprecated_method!'
675
+ include_examples 'invokes MethodStub#remove_allowance_for_no_message!'
676
+ end
677
+ end
678
+ end
632
679
  end
633
680
 
634
681
  context 'and Configuration#convert_deprecated_method? is false' do
635
- before { configuration.convert_deprecated_method = false }
682
+ before do
683
+ configuration.convert_deprecated_method = false
684
+ method_stub_object.stub(:hash_arg?).and_return(false)
685
+ end
636
686
 
637
687
  include_examples 'invokes MethodStub#allowize!'
638
688
  include_examples 'does not invoke MethodStub#convert_deprecated_method!'
@@ -896,7 +946,13 @@ module Transpec
896
946
  end
897
947
 
898
948
  describe '#process_rspec_configure' do
899
- let(:rspec_configure) { double('rspec_configure').as_null_object }
949
+ let(:rspec_configure) do
950
+ double(
951
+ 'rspec_configure',
952
+ expectations: double('expectations').as_null_object,
953
+ mocks: double('mocks').as_null_object
954
+ ).as_null_object
955
+ end
900
956
 
901
957
  context 'when #need_to_modify_expectation_syntax_configuration? returns true' do
902
958
  before do
@@ -155,6 +155,15 @@ module Transpec
155
155
  end
156
156
  end
157
157
 
158
+ describe '-t/--convert-stub-with-hash option' do
159
+ let(:args) { ['--convert-stub-with-hash'] }
160
+
161
+ it 'sets Configuration#convert_stub_with_hash_to_stub_and_return? true' do
162
+ parser.parse(args)
163
+ configuration.convert_stub_with_hash_to_stub_and_return?.should be_true
164
+ end
165
+ end
166
+
158
167
  describe '-p/--no-parentheses-matcher-arg option' do
159
168
  let(:args) { ['--no-parentheses-matcher-arg'] }
160
169
 
@@ -943,7 +943,7 @@ module Transpec
943
943
  rewritten_source.should == expected_source
944
944
  end
945
945
 
946
- it 'adds record ' +
946
+ it 'adds record ' \
947
947
  '`expect(obj).to have(n).errors_on(...)` -> `expect(obj.errors_on(...).size).to eq(n)`' do
948
948
  have_object.convert_to_standard_expectation!
949
949
  record.original_syntax.should == 'expect(obj).to have(n).errors_on(...)'
@@ -993,7 +993,7 @@ module Transpec
993
993
  rewritten_source.should == expected_source
994
994
  end
995
995
 
996
- it 'adds record ' +
996
+ it 'adds record ' \
997
997
  '`expect(obj).to have(n).errors_on(...)` -> `expect(obj.send(:errors_on, ...).size).to eq(n)`' do
998
998
  have_object.convert_to_standard_expectation!
999
999
  record.original_syntax.should == 'expect(obj).to have(n).errors_on(...)'
@@ -1040,7 +1040,7 @@ module Transpec
1040
1040
  rewritten_source.should == expected_source
1041
1041
  end
1042
1042
 
1043
- it 'adds record ' +
1043
+ it 'adds record ' \
1044
1044
  '`expect(obj).to have(n).errors_on(...)` -> `expect(obj.errors_on(...).size).to eq(n)`' do
1045
1045
  have_object.convert_to_standard_expectation!
1046
1046
  record.original_syntax.should == 'expect(obj).to have(n).errors_on(...)'
@@ -151,6 +151,38 @@ module Transpec
151
151
  end
152
152
  end
153
153
 
154
+ describe '#hash_arg?' do
155
+ subject { method_stub_object.hash_arg? }
156
+
157
+ context 'when it is `subject.stub(:method => value)` form' do
158
+ let(:source) do
159
+ <<-END
160
+ describe 'example' do
161
+ it 'responds to #foo' do
162
+ subject.stub(:foo => value)
163
+ end
164
+ end
165
+ END
166
+ end
167
+
168
+ it { should be_true }
169
+ end
170
+
171
+ context 'when it is `subject.stub(:method)` form' do
172
+ let(:source) do
173
+ <<-END
174
+ describe 'example' do
175
+ it 'responds to #foo' do
176
+ subject.stub(:foo)
177
+ end
178
+ end
179
+ END
180
+ end
181
+
182
+ it { should be_false }
183
+ end
184
+ end
185
+
154
186
  describe '#allowize!' do
155
187
  before do
156
188
  method_stub_object.allowize!(rspec_version) unless example.metadata[:no_before_allowize!]
@@ -409,7 +441,7 @@ module Transpec
409
441
  rewritten_source.should == expected_source
410
442
  end
411
443
 
412
- it 'adds record ' +
444
+ it 'adds record ' \
413
445
  '`obj.stub(:message => value)` -> `allow(obj).to receive_messages(:message => value)`' do
414
446
  record.original_syntax.should == 'obj.stub(:message => value)'
415
447
  record.converted_syntax.should == 'allow(obj).to receive_messages(:message => value)'
@@ -441,7 +473,7 @@ module Transpec
441
473
  rewritten_source.should == expected_source
442
474
  end
443
475
 
444
- it 'adds record ' +
476
+ it 'adds record ' \
445
477
  '`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`' do
446
478
  record.original_syntax.should == 'obj.stub(:message => value)'
447
479
  record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
@@ -474,7 +506,7 @@ module Transpec
474
506
  rewritten_source.should == expected_source
475
507
  end
476
508
 
477
- it 'adds record ' +
509
+ it 'adds record ' \
478
510
  '`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`' do
479
511
  record.original_syntax.should == 'obj.stub(:message => value)'
480
512
  record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
@@ -503,12 +535,12 @@ module Transpec
503
535
  END
504
536
  end
505
537
 
506
- it 'converts into `allow(subject).to receive(:a_method).and_return(a_value)` ' +
538
+ it 'converts into `allow(subject).to receive(:a_method).and_return(a_value)` ' \
507
539
  'and `allow(subject).to receive(:b_method).and_return(b_value)` form' do
508
540
  rewritten_source.should == expected_source
509
541
  end
510
542
 
511
- it 'adds record ' +
543
+ it 'adds record ' \
512
544
  '`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`' do
513
545
  record.original_syntax.should == 'obj.stub(:message => value)'
514
546
  record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
@@ -685,7 +717,7 @@ module Transpec
685
717
  rewritten_source.should == expected_source
686
718
  end
687
719
 
688
- it 'adds record `Klass.any_instance.stub(:message)` ' +
720
+ it 'adds record `Klass.any_instance.stub(:message)` ' \
689
721
  '-> `allow_any_instance_of(obj).to receive(:message)`' do
690
722
  record.original_syntax.should == 'Klass.any_instance.stub(:message)'
691
723
  record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
@@ -756,14 +788,14 @@ module Transpec
756
788
  rewritten_source.should == expected_source
757
789
  end
758
790
 
759
- it 'adds record `Klass.any_instance.stub(:message)` ' +
791
+ it 'adds record `Klass.any_instance.stub(:message)` ' \
760
792
  '-> `allow_any_instance_of(obj).to receive(:message)`' do
761
793
  record.original_syntax.should == 'Klass.any_instance.stub(:message)'
762
794
  record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
763
795
  end
764
796
  end
765
797
 
766
- context 'when it is `variable.any_instance.stub(:method)` form ' +
798
+ context 'when it is `variable.any_instance.stub(:method)` form ' \
767
799
  'and the variable is an AnyInstance::Recorder' do
768
800
  context 'with runtime information' do
769
801
  include_context 'dynamic analysis objects'
@@ -794,7 +826,7 @@ module Transpec
794
826
  rewritten_source.should == expected_source
795
827
  end
796
828
 
797
- it 'adds record `Klass.any_instance.stub(:message)` ' +
829
+ it 'adds record `Klass.any_instance.stub(:message)` ' \
798
830
  '-> `allow_any_instance_of(obj).to receive(:message)`' do
799
831
  record.original_syntax.should == 'Klass.any_instance.stub(:message)'
800
832
  record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
@@ -1059,7 +1091,7 @@ module Transpec
1059
1091
  rewritten_source.should == expected_source
1060
1092
  end
1061
1093
 
1062
- it 'adds record `Klass.any_instance.stub(:message) { |arg| }` ' +
1094
+ it 'adds record `Klass.any_instance.stub(:message) { |arg| }` ' \
1063
1095
  '-> `Klass.any_instance.stub(:message) { |instance, arg| }`' do
1064
1096
  record.original_syntax.should == 'Klass.any_instance.stub(:message) { |arg| }'
1065
1097
  record.converted_syntax.should == 'Klass.any_instance.stub(:message) { |instance, arg| }'
@@ -281,7 +281,7 @@ module Transpec
281
281
  rewritten_source.should == expected_source
282
282
  end
283
283
 
284
- it 'adds record ' +
284
+ it 'adds record ' \
285
285
  '`it { should have(n).items }` -> `it \'has n items\' do subject.size.should == n end`' do
286
286
  record.original_syntax.should == 'it { should have(n).items }'
287
287
  record.converted_syntax.should == "it 'has n items' do subject.size.should == n end"
@@ -311,7 +311,7 @@ module Transpec
311
311
  rewritten_source.should == expected_source
312
312
  end
313
313
 
314
- it 'adds record `it { should_not have(n).items }`' +
314
+ it 'adds record `it { should_not have(n).items }`' \
315
315
  ' -> `it \'does not have n items\' do subject.size.should_not == n end`' do
316
316
  record.original_syntax.should == 'it { should_not have(n).items }'
317
317
  record.converted_syntax.should == "it 'does not have n items' do subject.size.should_not == n end"
@@ -341,7 +341,7 @@ module Transpec
341
341
  rewritten_source.should == expected_source
342
342
  end
343
343
 
344
- it 'adds record ' +
344
+ it 'adds record ' \
345
345
  '`it { should have(n).items }` -> `it \'has n items\' do subject.size.should == n end`' do
346
346
  record.original_syntax.should == 'it { should have(n).items }'
347
347
  record.converted_syntax.should == "it 'has n items' do subject.size.should == n end"
@@ -371,7 +371,7 @@ module Transpec
371
371
  rewritten_source.should == expected_source
372
372
  end
373
373
 
374
- it 'adds record ' +
374
+ it 'adds record ' \
375
375
  '`it { should have(n).items }` -> `it \'has n items\' do subject.size.should == n end`' do
376
376
  record.original_syntax.should == 'it { should have(n).items }'
377
377
  record.converted_syntax.should == "it 'has n items' do subject.size.should == n end"
@@ -401,7 +401,7 @@ module Transpec
401
401
  rewritten_source.should == expected_source
402
402
  end
403
403
 
404
- it 'adds record ' +
404
+ it 'adds record ' \
405
405
  '`it { should have(n).items }` -> `it \'has n items\' do subject.size.should == n end`' do
406
406
  record.original_syntax.should == 'it { should have(n).items }'
407
407
  record.converted_syntax.should == "it 'has n items' do subject.size.should == n end"
@@ -431,7 +431,7 @@ module Transpec
431
431
  rewritten_source.should == expected_source
432
432
  end
433
433
 
434
- it 'adds record `it { should_not have(n).items }`' +
434
+ it 'adds record `it { should_not have(n).items }`' \
435
435
  ' -> `it \'does not have n items\' do subject.size.should_not == n end`' do
436
436
  record.original_syntax.should == 'it { should_not have(n).items }'
437
437
  record.converted_syntax.should == "it 'does not have n items' do subject.size.should_not == n end"
@@ -463,7 +463,7 @@ module Transpec
463
463
  rewritten_source.should == expected_source
464
464
  end
465
465
 
466
- it 'adds record ' +
466
+ it 'adds record ' \
467
467
  '`it { should have(n).items }` -> `it \'has n items\' do subject.size.should == n end`' do
468
468
  record.original_syntax.should == 'it { should have(n).items }'
469
469
  record.converted_syntax.should == "it 'has n items' do subject.size.should == n end"
@@ -495,7 +495,7 @@ module Transpec
495
495
  rewritten_source.should == expected_source
496
496
  end
497
497
 
498
- it 'adds record ' +
498
+ it 'adds record ' \
499
499
  '`it \'...\' { should have(n).items }` -> `it \'...\' do subject.size.should == n end`' do
500
500
  record.original_syntax.should == "it '...' do should have(n).items end"
501
501
  record.converted_syntax.should == "it '...' do subject.size.should == n end"
@@ -525,7 +525,7 @@ module Transpec
525
525
  rewritten_source.should == expected_source
526
526
  end
527
527
 
528
- it 'adds record `it { should have_at_least(n).items }` ' +
528
+ it 'adds record `it { should have_at_least(n).items }` ' \
529
529
  '-> `it \'has at least n items\' do subject.size.should >= n end`' do
530
530
  record.original_syntax.should == 'it { should have_at_least(n).items }'
531
531
  record.converted_syntax.should == "it 'has at least n items' do subject.size.should >= n end"
@@ -571,7 +571,7 @@ module Transpec
571
571
  rewritten_source.should == expected_source
572
572
  end
573
573
 
574
- it 'adds record `it { should have(n).words }` ' +
574
+ it 'adds record `it { should have(n).words }` ' \
575
575
  '-> `it \'has n words\' do subject.words.size.should == n end`' do
576
576
  record.original_syntax.should == 'it { should have(n).words }'
577
577
  record.converted_syntax.should == "it 'has n words' do subject.words.size.should == n end"
@@ -611,7 +611,7 @@ module Transpec
611
611
  rewritten_source.should == expected_source
612
612
  end
613
613
 
614
- it 'adds record ' +
614
+ it 'adds record ' \
615
615
  '`it { should have(n).items }` -> `it \'has n items\' do expect(subject.size).to eq(n) end`' do
616
616
  record.original_syntax.should == 'it { should have(n).items }'
617
617
  record.converted_syntax.should == "it 'has n items' do expect(subject.size).to eq(n) end"
@@ -641,7 +641,7 @@ module Transpec
641
641
  rewritten_source.should == expected_source
642
642
  end
643
643
 
644
- it 'adds record `it { should_not have(n).items }`' +
644
+ it 'adds record `it { should_not have(n).items }`' \
645
645
  ' -> `it \'does not have n items\' do expect(subject.size).not_to eq(n) end`' do
646
646
  record.original_syntax.should == 'it { should_not have(n).items }'
647
647
  record.converted_syntax.should == "it 'does not have n items' do expect(subject.size).not_to eq(n) end"