transpec 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/.travis.yml +1 -3
  4. data/CHANGELOG.md +9 -0
  5. data/README.md +141 -24
  6. data/README.md.erb +136 -24
  7. data/lib/transpec/ast/node.rb +7 -3
  8. data/lib/transpec/cli.rb +1 -1
  9. data/lib/transpec/configuration.rb +1 -0
  10. data/lib/transpec/converter.rb +31 -2
  11. data/lib/transpec/dynamic_analyzer.rb +1 -1
  12. data/lib/transpec/option_parser.rb +12 -9
  13. data/lib/transpec/project.rb +23 -12
  14. data/lib/transpec/rspec_version.rb +18 -4
  15. data/lib/transpec/static_context_inspector.rb +0 -15
  16. data/lib/transpec/syntax/example.rb +83 -0
  17. data/lib/transpec/syntax/expect.rb +5 -0
  18. data/lib/transpec/syntax/have.rb +111 -54
  19. data/lib/transpec/syntax/method_stub.rb +58 -37
  20. data/lib/transpec/syntax/mixin/allow_no_message.rb +2 -0
  21. data/lib/transpec/syntax/mixin/any_instance.rb +2 -0
  22. data/lib/transpec/syntax/mixin/should_base.rb +39 -0
  23. data/lib/transpec/syntax/oneliner_should.rb +218 -0
  24. data/lib/transpec/syntax/operator_matcher.rb +1 -0
  25. data/lib/transpec/syntax/should.rb +3 -30
  26. data/lib/transpec/util.rb +54 -0
  27. data/lib/transpec/version.rb +2 -2
  28. data/spec/support/shared_context.rb +21 -29
  29. data/spec/transpec/ast/node_spec.rb +1 -1
  30. data/spec/transpec/commit_message_spec.rb +29 -23
  31. data/spec/transpec/configuration_spec.rb +1 -0
  32. data/spec/transpec/converter_spec.rb +208 -5
  33. data/spec/transpec/dynamic_analyzer_spec.rb +2 -2
  34. data/spec/transpec/option_parser_spec.rb +1 -0
  35. data/spec/transpec/project_spec.rb +10 -0
  36. data/spec/transpec/rspec_version_spec.rb +52 -28
  37. data/spec/transpec/static_context_inspector_spec.rb +2 -2
  38. data/spec/transpec/syntax/be_boolean_spec.rb +6 -13
  39. data/spec/transpec/syntax/be_close_spec.rb +2 -9
  40. data/spec/transpec/syntax/double_spec.rb +2 -9
  41. data/spec/transpec/syntax/example_spec.rb +249 -0
  42. data/spec/transpec/syntax/expect_spec.rb +1 -1
  43. data/spec/transpec/syntax/have_spec.rb +127 -22
  44. data/spec/transpec/syntax/its_spec.rb +9 -18
  45. data/spec/transpec/syntax/method_stub_spec.rb +193 -158
  46. data/spec/transpec/syntax/oneliner_should_spec.rb +653 -0
  47. data/spec/transpec/syntax/operator_matcher_spec.rb +7 -8
  48. data/spec/transpec/syntax/raise_error_spec.rb +6 -13
  49. data/spec/transpec/syntax/rspec_configure_spec.rb +1 -8
  50. data/spec/transpec/syntax/should_receive_spec.rb +19 -28
  51. data/spec/transpec/syntax/should_spec.rb +18 -16
  52. data/spec/transpec/util_spec.rb +30 -0
  53. data/transpec.gemspec +8 -7
  54. metadata +49 -28
@@ -7,7 +7,7 @@ module Transpec
7
7
  class Syntax
8
8
  describe Expect do
9
9
  include_context 'parsed objects'
10
- include_context 'expect object'
10
+ include_context 'syntax object', Expect, :expect_object
11
11
 
12
12
  describe '#subject_node' do
13
13
  let(:source) do
@@ -2,14 +2,19 @@
2
2
 
3
3
  require 'spec_helper'
4
4
  require 'transpec/syntax/have'
5
+ require 'transpec/syntax/should'
6
+ require 'transpec/syntax/expect'
7
+ require 'transpec/syntax/oneliner_should'
8
+ require 'ast'
5
9
 
6
10
  module Transpec
7
11
  class Syntax
8
12
  describe Have do
9
13
  include ::AST::Sexp
10
14
  include_context 'parsed objects'
11
- include_context 'should object'
12
- include_context 'expect object'
15
+ include_context 'syntax object', Should, :should_object
16
+ include_context 'syntax object', Expect, :expect_object
17
+ include_context 'syntax object', OnelinerShould, :oneliner_should_object
13
18
 
14
19
  describe '#have_node' do
15
20
  let(:source) do
@@ -150,7 +155,7 @@ module Transpec
150
155
  rewritten_source.should == expected_source
151
156
  end
152
157
 
153
- it 'adds record "`collection.should have(n).items` -> `collection.size.should == n`"' do
158
+ it 'adds record `collection.should have(n).items` -> `collection.size.should == n`' do
154
159
  have_object.convert_to_standard_expectation!
155
160
  record.original_syntax.should == 'collection.should have(n).items'
156
161
  record.converted_syntax.should == 'collection.size.should == n'
@@ -178,7 +183,7 @@ module Transpec
178
183
  rewritten_source.should == expected_source
179
184
  end
180
185
 
181
- it 'adds record "`collection.should have(n).items` -> `expect(collection.size).to eq(n)`"' do
186
+ it 'adds record `collection.should have(n).items` -> `expect(collection.size).to eq(n)`' do
182
187
  record.original_syntax.should == 'collection.should have(n).items'
183
188
  record.converted_syntax.should == 'expect(collection.size).to eq(n)'
184
189
  end
@@ -200,7 +205,7 @@ module Transpec
200
205
  rewritten_source.should == expected_source
201
206
  end
202
207
 
203
- it 'adds record "`collection.should have(n).items` -> `expect(collection.size).to eq(n)`"' do
208
+ it 'adds record `collection.should have(n).items` -> `expect(collection.size).to eq(n)`' do
204
209
  record.original_syntax.should == 'collection.should have(n).items'
205
210
  record.converted_syntax.should == 'expect(collection.size).to eq(n)'
206
211
  end
@@ -208,6 +213,41 @@ module Transpec
208
213
  end
209
214
  end
210
215
 
216
+ context 'when it is `collection.should_not have(2).items` form' do
217
+ let(:source) do
218
+ <<-END
219
+ describe 'example' do
220
+ it 'does not 2 items' do
221
+ collection.should_not have(2).items
222
+ end
223
+ end
224
+ END
225
+ end
226
+
227
+ let(:expected_source) do
228
+ <<-END
229
+ describe 'example' do
230
+ it 'does not 2 items' do
231
+ collection.size.should_not == 2
232
+ end
233
+ end
234
+ END
235
+ end
236
+
237
+ let(:have_object) { should_object.have_matcher }
238
+
239
+ it 'converts into `collection.size.should_not == 2` form' do
240
+ have_object.convert_to_standard_expectation!
241
+ rewritten_source.should == expected_source
242
+ end
243
+
244
+ it 'adds record `collection.should_not have(n).items` -> `collection.size.should_not == n`' do
245
+ have_object.convert_to_standard_expectation!
246
+ record.original_syntax.should == 'collection.should_not have(n).items'
247
+ record.converted_syntax.should == 'collection.size.should_not == n'
248
+ end
249
+ end
250
+
211
251
  context 'when it is `collection.should have_at_least(2).items` form' do
212
252
  let(:source) do
213
253
  <<-END
@@ -236,7 +276,7 @@ module Transpec
236
276
  rewritten_source.should == expected_source
237
277
  end
238
278
 
239
- it 'adds record "`collection.should have_at_least(n).items` -> `collection.size.should >= n`"' do
279
+ it 'adds record `collection.should have_at_least(n).items` -> `collection.size.should >= n`' do
240
280
  have_object.convert_to_standard_expectation!
241
281
  record.original_syntax.should == 'collection.should have_at_least(n).items'
242
282
  record.converted_syntax.should == 'collection.size.should >= n'
@@ -271,7 +311,7 @@ module Transpec
271
311
  rewritten_source.should == expected_source
272
312
  end
273
313
 
274
- it 'adds record "`collection.should have_at_most(n).items` -> `collection.size.should <= n`"' do
314
+ it 'adds record `collection.should have_at_most(n).items` -> `collection.size.should <= n`' do
275
315
  have_object.convert_to_standard_expectation!
276
316
  record.original_syntax.should == 'collection.should have_at_most(n).items'
277
317
  record.converted_syntax.should == 'collection.size.should <= n'
@@ -306,7 +346,7 @@ module Transpec
306
346
  rewritten_source.should == expected_source
307
347
  end
308
348
 
309
- it 'adds record "`expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`"' do
349
+ it 'adds record `expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`' do
310
350
  have_object.convert_to_standard_expectation!
311
351
  record.original_syntax.should == 'expect(collection).to have(n).items'
312
352
  record.converted_syntax.should == 'expect(collection.size).to eq(n)'
@@ -353,7 +393,7 @@ module Transpec
353
393
  rewritten_source.should == expected_source
354
394
  end
355
395
 
356
- it 'adds record "`expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`"' do
396
+ it 'adds record `expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`' do
357
397
  have_object.convert_to_standard_expectation!
358
398
  record.original_syntax.should == 'expect(collection).to have(n).items'
359
399
  record.converted_syntax.should == 'expect(collection.size).to eq(n)'
@@ -362,6 +402,41 @@ module Transpec
362
402
  end
363
403
  end
364
404
 
405
+ context 'when it is `expect(collection).not_to have(2).items` form' do
406
+ let(:source) do
407
+ <<-END
408
+ describe 'example' do
409
+ it 'does not have 2 items' do
410
+ expect(collection).not_to have(2).items
411
+ end
412
+ end
413
+ END
414
+ end
415
+
416
+ let(:expected_source) do
417
+ <<-END
418
+ describe 'example' do
419
+ it 'does not have 2 items' do
420
+ expect(collection.size).not_to eq(2)
421
+ end
422
+ end
423
+ END
424
+ end
425
+
426
+ let(:have_object) { expect_object.have_matcher }
427
+
428
+ it 'converts into `expect(collection.size).not_to eq(2)` form' do
429
+ have_object.convert_to_standard_expectation!
430
+ rewritten_source.should == expected_source
431
+ end
432
+
433
+ it 'adds record `expect(collection).not_to have(n).items` -> `expect(collection.size).not_to eq(n)`' do
434
+ have_object.convert_to_standard_expectation!
435
+ record.original_syntax.should == 'expect(collection).not_to have(n).items'
436
+ record.converted_syntax.should == 'expect(collection.size).not_to eq(n)'
437
+ end
438
+ end
439
+
365
440
  context 'when it is `expect(collection).to have_at_least(2).items` form' do
366
441
  let(:source) do
367
442
  <<-END
@@ -390,7 +465,7 @@ module Transpec
390
465
  rewritten_source.should == expected_source
391
466
  end
392
467
 
393
- it 'adds record "`expect(collection).to have_at_least(n).items` -> `expect(collection.size).to be >= n`"' do
468
+ it 'adds record `expect(collection).to have_at_least(n).items` -> `expect(collection.size).to be >= n`' do
394
469
  have_object.convert_to_standard_expectation!
395
470
  record.original_syntax.should == 'expect(collection).to have_at_least(n).items'
396
471
  record.converted_syntax.should == 'expect(collection.size).to be >= n'
@@ -425,7 +500,7 @@ module Transpec
425
500
  rewritten_source.should == expected_source
426
501
  end
427
502
 
428
- it 'adds record "`expect(collection).to have_at_most(n).items` -> `expect(collection.size).to be <= n`"' do
503
+ it 'adds record `expect(collection).to have_at_most(n).items` -> `expect(collection.size).to be <= n`' do
429
504
  have_object.convert_to_standard_expectation!
430
505
  record.original_syntax.should == 'expect(collection).to have_at_most(n).items'
431
506
  record.converted_syntax.should == 'expect(collection.size).to be <= n'
@@ -477,7 +552,7 @@ module Transpec
477
552
  rewritten_source.should == expected_source
478
553
  end
479
554
 
480
- it 'adds record "`expect(obj).to have(n).words` -> `expect(obj.words.size).to eq(n)`"' do
555
+ it 'adds record `expect(obj).to have(n).words` -> `expect(obj.words.size).to eq(n)`' do
481
556
  have_object.convert_to_standard_expectation!
482
557
  record.original_syntax.should == 'expect(obj).to have(n).words'
483
558
  record.converted_syntax.should == 'expect(obj.words.size).to eq(n)'
@@ -534,7 +609,7 @@ module Transpec
534
609
  rewritten_source.should == expected_source
535
610
  end
536
611
 
537
- it 'adds record "`expect(obj).to have(n).words` -> `expect(obj.words.count).to eq(n)`"' do
612
+ it 'adds record `expect(obj).to have(n).words` -> `expect(obj.words.count).to eq(n)`' do
538
613
  have_object.convert_to_standard_expectation!
539
614
  record.original_syntax.should == 'expect(obj).to have(n).words'
540
615
  record.converted_syntax.should == 'expect(obj.words.count).to eq(n)'
@@ -569,7 +644,7 @@ module Transpec
569
644
  rewritten_source.should == expected_source
570
645
  end
571
646
 
572
- it 'adds record "`expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`"' do
647
+ it 'adds record `expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`' do
573
648
  have_object.convert_to_standard_expectation!
574
649
  record.original_syntax.should == 'expect(collection).to have(n).items'
575
650
  record.converted_syntax.should == 'expect(collection.size).to eq(n)'
@@ -616,7 +691,7 @@ module Transpec
616
691
  rewritten_source.should == expected_source
617
692
  end
618
693
 
619
- it 'adds record "`expect(obj).to have(n).words` -> `expect(obj.send(:words).size).to eq(n)`"' do
694
+ it 'adds record `expect(obj).to have(n).words` -> `expect(obj.send(:words).size).to eq(n)`' do
620
695
  have_object.convert_to_standard_expectation!
621
696
  record.original_syntax.should == 'expect(obj).to have(n).words'
622
697
  record.converted_syntax.should == 'expect(obj.send(:words).size).to eq(n)'
@@ -663,7 +738,7 @@ module Transpec
663
738
  rewritten_source.should == expected_source
664
739
  end
665
740
 
666
- it 'adds record "`expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`"' do
741
+ it 'adds record `expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`' do
667
742
  have_object.convert_to_standard_expectation!
668
743
  record.original_syntax.should == 'expect(collection).to have(n).items'
669
744
  record.converted_syntax.should == 'expect(collection.size).to eq(n)'
@@ -720,7 +795,7 @@ module Transpec
720
795
  rewritten_source.should == expected_source
721
796
  end
722
797
 
723
- it 'adds record "`expect(obj).to have(n).words` -> `expect(obj.words.size).to eq(n)`"' do
798
+ it 'adds record `expect(obj).to have(n).words` -> `expect(obj.words.size).to eq(n)`' do
724
799
  have_object.convert_to_standard_expectation!
725
800
  record.original_syntax.should == 'expect(obj).to have(n).words'
726
801
  record.converted_syntax.should == 'expect(obj.words.size).to eq(n)'
@@ -765,7 +840,7 @@ module Transpec
765
840
  rewritten_source.should == expected_source
766
841
  end
767
842
 
768
- it 'adds record "`expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`"' do
843
+ it 'adds record `expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`' do
769
844
  have_object.convert_to_standard_expectation!
770
845
  record.original_syntax.should == 'expect(collection).to have(n).items'
771
846
  record.converted_syntax.should == 'expect(collection.size).to eq(n)'
@@ -816,7 +891,7 @@ module Transpec
816
891
  rewritten_source.should == expected_source
817
892
  end
818
893
 
819
- it 'adds record "`expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`"' do
894
+ it 'adds record `expect(collection).to have(n).items` -> `expect(collection.size).to eq(n)`' do
820
895
  have_object.convert_to_standard_expectation!
821
896
  record.original_syntax.should == 'expect(collection).to have(n).items'
822
897
  record.converted_syntax.should == 'expect(collection.size).to eq(n)'
@@ -869,7 +944,7 @@ module Transpec
869
944
  end
870
945
 
871
946
  it 'adds record ' +
872
- '"`expect(obj).to have(n).errors_on(...)` -> `expect(obj.errors_on(...).size).to eq(n)`"' do
947
+ '`expect(obj).to have(n).errors_on(...)` -> `expect(obj.errors_on(...).size).to eq(n)`' do
873
948
  have_object.convert_to_standard_expectation!
874
949
  record.original_syntax.should == 'expect(obj).to have(n).errors_on(...)'
875
950
  record.converted_syntax.should == 'expect(obj.errors_on(...).size).to eq(n)'
@@ -919,7 +994,7 @@ module Transpec
919
994
  end
920
995
 
921
996
  it 'adds record ' +
922
- '"`expect(obj).to have(n).errors_on(...)` -> `expect(obj.send(:errors_on, ...).size).to eq(n)`"' do
997
+ '`expect(obj).to have(n).errors_on(...)` -> `expect(obj.send(:errors_on, ...).size).to eq(n)`' do
923
998
  have_object.convert_to_standard_expectation!
924
999
  record.original_syntax.should == 'expect(obj).to have(n).errors_on(...)'
925
1000
  record.converted_syntax.should == 'expect(obj.send(:errors_on, ...).size).to eq(n)'
@@ -966,13 +1041,43 @@ module Transpec
966
1041
  end
967
1042
 
968
1043
  it 'adds record ' +
969
- '"`expect(obj).to have(n).errors_on(...)` -> `expect(obj.errors_on(...).size).to eq(n)`"' do
1044
+ '`expect(obj).to have(n).errors_on(...)` -> `expect(obj.errors_on(...).size).to eq(n)`' do
970
1045
  have_object.convert_to_standard_expectation!
971
1046
  record.original_syntax.should == 'expect(obj).to have(n).errors_on(...)'
972
1047
  record.converted_syntax.should == 'expect(obj.errors_on(...).size).to eq(n)'
973
1048
  end
974
1049
  end
975
1050
  end
1051
+
1052
+ context 'when it is `it { should have(2).items }` form' do
1053
+ let(:source) do
1054
+ <<-END
1055
+ describe 'example' do
1056
+ it { should have(2).items }
1057
+ end
1058
+ END
1059
+ end
1060
+
1061
+ let(:expected_source) do
1062
+ <<-END
1063
+ describe 'example' do
1064
+ it { should == 2 }
1065
+ end
1066
+ END
1067
+ end
1068
+
1069
+ let(:have_object) { oneliner_should_object.have_matcher }
1070
+
1071
+ it "converts into `it { should == 2 }` form since there's no subject" do
1072
+ have_object.convert_to_standard_expectation!
1073
+ rewritten_source.should == expected_source
1074
+ end
1075
+
1076
+ it 'does not add record' do
1077
+ have_object.convert_to_standard_expectation!
1078
+ record.should be_nil
1079
+ end
1080
+ end
976
1081
  end
977
1082
  end
978
1083
  end
@@ -7,16 +7,7 @@ module Transpec
7
7
  class Syntax
8
8
  describe Its do
9
9
  include_context 'parsed objects'
10
-
11
- subject(:its_object) do
12
- ast.each_node do |node|
13
- next unless Its.target_node?(node)
14
- return Its.new(node, source_rewriter, runtime_data)
15
- end
16
- fail 'No #its node is found!'
17
- end
18
-
19
- let(:runtime_data) { nil }
10
+ include_context 'syntax object', Its, :its_object
20
11
 
21
12
  let(:record) { its_object.report.records.last }
22
13
 
@@ -76,7 +67,7 @@ module Transpec
76
67
  rewritten_source.should == expected_source
77
68
  end
78
69
 
79
- it "adds record \"`its(:attr) { }` -> `describe 'attr' do subject { super().attr }; it { } end`\"" do
70
+ it "adds record `its(:attr) { }` -> `describe 'attr' do subject { super().attr }; it { } end`" do
80
71
  record.original_syntax.should == 'its(:attr) { }'
81
72
  record.converted_syntax.should == "describe 'attr' do subject { super().attr }; it { } end"
82
73
  end
@@ -173,7 +164,7 @@ module Transpec
173
164
  rewritten_source.should == expected_source
174
165
  end
175
166
 
176
- it "adds record \"`its(:attr) { }` -> `describe 'attr' do subject { super().attr }; it { } end`\"" do
167
+ it "adds record `its(:attr) { }` -> `describe 'attr' do subject { super().attr }; it { } end`" do
177
168
  record.original_syntax.should == 'its(:attr) { }'
178
169
  record.converted_syntax.should == "describe 'attr' do subject { super().attr }; it { } end"
179
170
  end
@@ -210,7 +201,7 @@ module Transpec
210
201
  rewritten_source.should == expected_source
211
202
  end
212
203
 
213
- it "adds record \"`its(:attr) { }` -> `describe 'attr' do subject { super().attr }; it { } end`\"" do
204
+ it "adds record `its(:attr) { }` -> `describe 'attr' do subject { super().attr }; it { } end`" do
214
205
  record.original_syntax.should == 'its(:attr) { }'
215
206
  record.converted_syntax.should == "describe 'attr' do subject { super().attr }; it { } end"
216
207
  end
@@ -244,7 +235,7 @@ module Transpec
244
235
  rewritten_source.should == expected_source
245
236
  end
246
237
 
247
- it "adds record \"`its([:key]) { }` -> `describe '[:key]' do subject { super()[:key] }; it { } end`\"" do
238
+ it "adds record `its([:key]) { }` -> `describe '[:key]' do subject { super()[:key] }; it { } end`" do
248
239
  record.original_syntax.should == 'its([:key]) { }'
249
240
  record.converted_syntax.should == "describe '[:key]' do subject { super()[:key] }; it { } end"
250
241
  end
@@ -278,7 +269,7 @@ module Transpec
278
269
  rewritten_source.should == expected_source
279
270
  end
280
271
 
281
- it "adds record \"`its([:key]) { }` -> `describe '[:key]' do subject { super()[:key] }; it { } end`\"" do
272
+ it "adds record `its([:key]) { }` -> `describe '[:key]' do subject { super()[:key] }; it { } end`" do
282
273
  record.original_syntax.should == 'its([:key]) { }'
283
274
  record.converted_syntax.should == "describe '[:key]' do subject { super()[:key] }; it { } end"
284
275
  end
@@ -312,7 +303,7 @@ module Transpec
312
303
  rewritten_source.should == expected_source
313
304
  end
314
305
 
315
- it "adds record \"`its([:key]) { }` -> `describe '[:key]' do subject { super()[:key] }; it { } end`\"" do
306
+ it "adds record `its([:key]) { }` -> `describe '[:key]' do subject { super()[:key] }; it { } end`" do
316
307
  record.original_syntax.should == 'its([:key]) { }'
317
308
  record.converted_syntax.should == "describe '[:key]' do subject { super()[:key] }; it { } end"
318
309
  end
@@ -356,7 +347,7 @@ module Transpec
356
347
  rewritten_source.should == expected_source
357
348
  end
358
349
 
359
- it "adds record \"`its(:attr) { }` -> `describe 'attr' do subject { super().attr }; it { } end`\"" do
350
+ it "adds record `its(:attr) { }` -> `describe 'attr' do subject { super().attr }; it { } end`" do
360
351
  record.original_syntax.should == 'its(:attr) { }'
361
352
  record.converted_syntax.should == "describe 'attr' do subject { super().attr }; it { } end"
362
353
  end
@@ -400,7 +391,7 @@ module Transpec
400
391
  rewritten_source.should == expected_source
401
392
  end
402
393
 
403
- it "adds record \"`its([:key]) { }` -> `describe '[:key]' do subject { super()[:key] }; it { } end`\"" do
394
+ it "adds record `its([:key]) { }` -> `describe '[:key]' do subject { super()[:key] }; it { } end`" do
404
395
  record.original_syntax.should == 'its([:key]) { }'
405
396
  record.converted_syntax.should == "describe '[:key]' do subject { super()[:key] }; it { } end"
406
397
  end
@@ -2,21 +2,13 @@
2
2
 
3
3
  require 'spec_helper'
4
4
  require 'transpec/syntax/method_stub'
5
+ require 'transpec'
5
6
 
6
7
  module Transpec
7
8
  class Syntax
8
9
  describe MethodStub do
9
10
  include_context 'parsed objects'
10
-
11
- subject(:method_stub_object) do
12
- ast.each_node do |node|
13
- next unless MethodStub.target_node?(node)
14
- return MethodStub.new(node, source_rewriter, runtime_data)
15
- end
16
- fail 'No method stub node is found!'
17
- end
18
-
19
- let(:runtime_data) { nil }
11
+ include_context 'syntax object', MethodStub, :method_stub_object
20
12
 
21
13
  let(:record) { method_stub_object.report.records.first }
22
14
 
@@ -106,6 +98,12 @@ module Transpec
106
98
  end
107
99
 
108
100
  describe '#allowize!' do
101
+ before do
102
+ method_stub_object.allowize!(rspec_version) unless example.metadata[:no_before_allowize!]
103
+ end
104
+
105
+ let(:rspec_version) { Transpec.required_rspec_version }
106
+
109
107
  context 'when it is `subject.stub(:method)` form' do
110
108
  let(:source) do
111
109
  <<-END
@@ -128,17 +126,15 @@ module Transpec
128
126
  end
129
127
 
130
128
  it 'converts into `allow(subject).to receive(:method)` form' do
131
- method_stub_object.allowize!
132
129
  rewritten_source.should == expected_source
133
130
  end
134
131
 
135
- it "adds record \"`obj.stub(:message)` -> `allow(obj).to receive(:message)`\"" do
136
- method_stub_object.allowize!
132
+ it 'adds record `obj.stub(:message)` -> `allow(obj).to receive(:message)`' do
137
133
  record.original_syntax.should == 'obj.stub(:message)'
138
134
  record.converted_syntax.should == 'allow(obj).to receive(:message)'
139
135
  end
140
136
 
141
- context 'and #allow and #receive are not available in the context' do
137
+ context 'and #allow and #receive are not available in the context', :no_before_allowize! do
142
138
  context 'and the context is determinable statically' do
143
139
  let(:source) do
144
140
  <<-END
@@ -160,13 +156,15 @@ module Transpec
160
156
  include_context 'dynamic analysis objects'
161
157
 
162
158
  it 'raises InvalidContextError' do
163
- -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
159
+ -> { method_stub_object.allowize!(rspec_version) }
160
+ .should raise_error(InvalidContextError)
164
161
  end
165
162
  end
166
163
 
167
164
  context 'without runtime information' do
168
165
  it 'raises InvalidContextError' do
169
- -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
166
+ -> { method_stub_object.allowize!(rspec_version) }
167
+ .should raise_error(InvalidContextError)
170
168
  end
171
169
  end
172
170
  end
@@ -190,13 +188,15 @@ module Transpec
190
188
  include_context 'dynamic analysis objects'
191
189
 
192
190
  it 'raises InvalidContextError' do
193
- -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
191
+ -> { method_stub_object.allowize!(rspec_version) }
192
+ .should raise_error(InvalidContextError)
194
193
  end
195
194
  end
196
195
 
197
196
  context 'without runtime information' do
198
197
  it 'does not raise InvalidContextError' do
199
- -> { method_stub_object.allowize! }.should_not raise_error
198
+ -> { method_stub_object.allowize!(rspec_version) }
199
+ .should_not raise_error
200
200
  end
201
201
  end
202
202
  end
@@ -225,12 +225,10 @@ module Transpec
225
225
  end
226
226
 
227
227
  it 'converts into `allow(subject).to receive(:method)` form' do
228
- method_stub_object.allowize!
229
228
  rewritten_source.should == expected_source
230
229
  end
231
230
 
232
- it "adds record \"`obj.stub!(:message)` -> `allow(obj).to receive(:message)`\"" do
233
- method_stub_object.allowize!
231
+ it 'adds record `obj.stub!(:message)` -> `allow(obj).to receive(:message)`' do
234
232
  record.original_syntax.should == 'obj.stub!(:message)'
235
233
  record.converted_syntax.should == 'allow(obj).to receive(:message)'
236
234
  end
@@ -258,7 +256,6 @@ module Transpec
258
256
  end
259
257
 
260
258
  it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
261
- method_stub_object.allowize!
262
259
  rewritten_source.should == expected_source
263
260
  end
264
261
  end
@@ -285,7 +282,6 @@ module Transpec
285
282
  end
286
283
 
287
284
  it 'converts into `allow(subject).to receive(:method).and_raise(RuntimeError)` form' do
288
- method_stub_object.allowize!
289
285
  rewritten_source.should == expected_source
290
286
  end
291
287
  end
@@ -322,13 +318,19 @@ module Transpec
322
318
  end
323
319
 
324
320
  it 'keeps the style as far as possible' do
325
- method_stub_object.allowize!
326
321
  rewritten_source.should == expected_source
327
322
  end
328
323
  end
329
324
 
330
325
  context 'when it is `subject.stub(:method => value)` form' do
331
326
  context 'and #receive_messages is available' do
327
+ # #before here does not work because #allowized! is invoked in super #before.
328
+ let(:rspec_version) do
329
+ rspec_version = Transpec.required_rspec_version
330
+ rspec_version.stub(:receive_messages_available?).and_return(true)
331
+ rspec_version
332
+ end
333
+
332
334
  let(:source) do
333
335
  <<-END
334
336
  describe 'example' do
@@ -350,13 +352,11 @@ module Transpec
350
352
  end
351
353
 
352
354
  it 'converts into `allow(subject).to receive_messages(:method => value)` form' do
353
- method_stub_object.allowize!(true)
354
355
  rewritten_source.should == expected_source
355
356
  end
356
357
 
357
358
  it 'adds record ' +
358
- "\"`obj.stub(:message => value)` -> `allow(obj).to receive_messages(:message => value)`\"" do
359
- method_stub_object.allowize!(true)
359
+ '`obj.stub(:message => value)` -> `allow(obj).to receive_messages(:message => value)`' do
360
360
  record.original_syntax.should == 'obj.stub(:message => value)'
361
361
  record.converted_syntax.should == 'allow(obj).to receive_messages(:message => value)'
362
362
  end
@@ -384,13 +384,11 @@ module Transpec
384
384
  end
385
385
 
386
386
  it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
387
- method_stub_object.allowize!
388
387
  rewritten_source.should == expected_source
389
388
  end
390
389
 
391
390
  it 'adds record ' +
392
- "\"`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
393
- method_stub_object.allowize!
391
+ '`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`' do
394
392
  record.original_syntax.should == 'obj.stub(:message => value)'
395
393
  record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
396
394
  end
@@ -419,13 +417,11 @@ module Transpec
419
417
  end
420
418
 
421
419
  it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
422
- method_stub_object.allowize!
423
420
  rewritten_source.should == expected_source
424
421
  end
425
422
 
426
423
  it 'adds record ' +
427
- "\"`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
428
- method_stub_object.allowize!
424
+ '`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`' do
429
425
  record.original_syntax.should == 'obj.stub(:message => value)'
430
426
  record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
431
427
  end
@@ -455,19 +451,24 @@ module Transpec
455
451
 
456
452
  it 'converts into `allow(subject).to receive(:a_method).and_return(a_value)` ' +
457
453
  'and `allow(subject).to receive(:b_method).and_return(b_value)` form' do
458
- method_stub_object.allowize!
459
454
  rewritten_source.should == expected_source
460
455
  end
461
456
 
462
457
  it 'adds record ' +
463
- "\"`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
464
- method_stub_object.allowize!
458
+ '`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`' do
465
459
  record.original_syntax.should == 'obj.stub(:message => value)'
466
460
  record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
467
461
  end
468
462
 
469
463
  context 'when the statement continues over multi lines' do
470
464
  context 'and #receive_messages is available' do
465
+ # #before here does not work because #allowized! is invoked in super #before.
466
+ let(:rspec_version) do
467
+ rspec_version = Transpec.required_rspec_version
468
+ rspec_version.stub(:receive_messages_available?).and_return(true)
469
+ rspec_version
470
+ end
471
+
471
472
  let(:source) do
472
473
  <<-END
473
474
  describe 'example' do
@@ -497,7 +498,6 @@ module Transpec
497
498
  end
498
499
 
499
500
  it 'keeps the style' do
500
- method_stub_object.allowize!(true)
501
501
  rewritten_source.should == expected_source
502
502
  end
503
503
  end
@@ -531,84 +531,87 @@ module Transpec
531
531
  end
532
532
 
533
533
  it 'keeps the style except around the hash' do
534
- method_stub_object.allowize!
535
534
  rewritten_source.should == expected_source
536
535
  end
537
536
  end
538
537
  end
539
538
  end
540
- end
541
539
 
542
- [:unstub, :unstub!].each do |method|
543
- context "when it is `subject.#{method}(:method)` form" do
540
+ context 'when it is `subject.stub_chain(:foo, :bar => value)` form' do
544
541
  let(:source) do
545
542
  <<-END
546
543
  describe 'example' do
547
- it 'does not respond to #foo' do
548
- subject.#{method}(:foo)
544
+ it 'responds to .foo.bar and returns 1' do
545
+ subject.stub_chain(:foo, :bar => 1)
549
546
  end
550
547
  end
551
548
  END
552
549
  end
553
550
 
554
- it 'does nothing' do
555
- method_stub_object.allowize!
556
- rewritten_source.should == source
551
+ let(:expected_source) do
552
+ <<-END
553
+ describe 'example' do
554
+ it 'responds to .foo.bar and returns 1' do
555
+ allow(subject).to receive_message_chain(:foo, :bar => 1)
556
+ end
557
+ end
558
+ END
557
559
  end
558
560
 
559
- it 'reports nothing' do
560
- method_stub_object.allowize!
561
- method_stub_object.report.records.should be_empty
561
+ context 'and #receive_message_chain is available' do
562
+ # #before here does not work because #allowized! is invoked in super #before.
563
+ let(:rspec_version) do
564
+ rspec_version = Transpec.required_rspec_version
565
+ rspec_version.stub(:receive_message_chain_available?).and_return(true)
566
+ rspec_version
567
+ end
568
+
569
+ it 'converts into `allow(subject).to receive_message_chain(:foo, :bar => value)` form' do
570
+ rewritten_source.should == expected_source
571
+ end
572
+
573
+ it "adds record `obj.stub_chain(:message1, :message2)` -> ' +
574
+ '`allow(obj).to receive_message_chain(:message1, :message2)`" do
575
+ record.original_syntax.should == 'obj.stub_chain(:message1, :message2)'
576
+ record.converted_syntax.should == 'allow(obj).to receive_message_chain(:message1, :message2)'
577
+ end
562
578
  end
563
- end
564
- end
565
579
 
566
- context 'when it is `Klass.any_instance.stub(:method)` form' do
567
- let(:source) do
568
- <<-END
569
- describe 'example' do
570
- it 'responds to #foo' do
571
- Klass.any_instance.stub(:foo)
572
- end
580
+ context 'and #receive_message_chain is not available' do
581
+ it 'does nothing' do
582
+ rewritten_source.should == source
573
583
  end
574
- END
584
+ end
575
585
  end
576
586
 
577
- let(:expected_source) do
578
- <<-END
579
- describe 'example' do
580
- it 'responds to #foo' do
581
- allow_any_instance_of(Klass).to receive(:foo)
582
- end
587
+ [:unstub, :unstub!].each do |method|
588
+ context "when it is `subject.#{method}(:method)` form" do
589
+ let(:source) do
590
+ <<-END
591
+ describe 'example' do
592
+ it 'does not respond to #foo' do
593
+ subject.#{method}(:foo)
594
+ end
595
+ end
596
+ END
583
597
  end
584
- END
585
- end
586
598
 
587
- it 'converts into `allow_any_instance_of(Klass).to receive(:method)` form' do
588
- method_stub_object.allowize!
589
- rewritten_source.should == expected_source
590
- end
599
+ it 'does nothing' do
600
+ rewritten_source.should == source
601
+ end
591
602
 
592
- it "adds record \"`Klass.any_instance.stub(:message)` " +
593
- '-> `allow_any_instance_of(obj).to receive(:message)`"' do
594
- method_stub_object.allowize!
595
- record.original_syntax.should == 'Klass.any_instance.stub(:message)'
596
- record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
603
+ it 'reports nothing' do
604
+ method_stub_object.report.records.should be_empty
605
+ end
606
+ end
597
607
  end
598
608
 
599
- context 'when the statement continues over multi lines' do
609
+ context 'when it is `Klass.any_instance.stub(:method)` form' do
600
610
  let(:source) do
601
611
  <<-END
602
612
  describe 'example' do
603
- it 'responds to #foo and returns 1' do
604
- Klass
605
- .any_instance
606
- .stub(
607
- :foo
608
- ).
609
- and_return(
610
- 1
611
- )
613
+ it 'responds to #foo' do
614
+ Klass.any_instance.stub(:foo)
612
615
  end
613
616
  end
614
617
  END
@@ -617,71 +620,69 @@ module Transpec
617
620
  let(:expected_source) do
618
621
  <<-END
619
622
  describe 'example' do
620
- it 'responds to #foo and returns 1' do
621
- allow_any_instance_of(Klass)
622
- .to receive(
623
- :foo
624
- ).
625
- and_return(
626
- 1
627
- )
623
+ it 'responds to #foo' do
624
+ allow_any_instance_of(Klass).to receive(:foo)
628
625
  end
629
626
  end
630
627
  END
631
628
  end
632
629
 
633
- it 'keeps the style as far as possible' do
634
- method_stub_object.allowize!
630
+ it 'converts into `allow_any_instance_of(Klass).to receive(:method)` form' do
635
631
  rewritten_source.should == expected_source
636
632
  end
637
- end
638
- end
639
633
 
640
- context 'when it is `described_class.any_instance.stub(:method)` form' do
641
- let(:source) do
642
- <<-END
643
- describe 'example' do
644
- it 'responds to #foo' do
645
- described_class.any_instance.stub(:foo)
646
- end
647
- end
648
- END
649
- end
634
+ it 'adds record `Klass.any_instance.stub(:message)` ' +
635
+ '-> `allow_any_instance_of(obj).to receive(:message)`' do
636
+ record.original_syntax.should == 'Klass.any_instance.stub(:message)'
637
+ record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
638
+ end
650
639
 
651
- let(:expected_source) do
652
- <<-END
653
- describe 'example' do
654
- it 'responds to #foo' do
655
- allow_any_instance_of(described_class).to receive(:foo)
656
- end
640
+ context 'when the statement continues over multi lines' do
641
+ let(:source) do
642
+ <<-END
643
+ describe 'example' do
644
+ it 'responds to #foo and returns 1' do
645
+ Klass
646
+ .any_instance
647
+ .stub(
648
+ :foo
649
+ ).
650
+ and_return(
651
+ 1
652
+ )
653
+ end
654
+ end
655
+ END
657
656
  end
658
- END
659
- end
660
657
 
661
- it 'converts into `allow_any_instance_of(described_class).to receive(:method)` form' do
662
- method_stub_object.allowize!
663
- rewritten_source.should == expected_source
664
- end
658
+ let(:expected_source) do
659
+ <<-END
660
+ describe 'example' do
661
+ it 'responds to #foo and returns 1' do
662
+ allow_any_instance_of(Klass)
663
+ .to receive(
664
+ :foo
665
+ ).
666
+ and_return(
667
+ 1
668
+ )
669
+ end
670
+ end
671
+ END
672
+ end
665
673
 
666
- it "adds record \"`Klass.any_instance.stub(:message)` " +
667
- '-> `allow_any_instance_of(obj).to receive(:message)`"' do
668
- method_stub_object.allowize!
669
- record.original_syntax.should == 'Klass.any_instance.stub(:message)'
670
- record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
674
+ it 'keeps the style as far as possible' do
675
+ rewritten_source.should == expected_source
676
+ end
677
+ end
671
678
  end
672
- end
673
-
674
- context 'when it is `variable.any_instance.stub(:method)` form ' +
675
- 'and the variable is an AnyInstance::Recorder' do
676
- context 'with runtime information' do
677
- include_context 'dynamic analysis objects'
678
679
 
680
+ context 'when it is `described_class.any_instance.stub(:method)` form' do
679
681
  let(:source) do
680
682
  <<-END
681
683
  describe 'example' do
682
684
  it 'responds to #foo' do
683
- variable = String.any_instance
684
- variable.stub(:foo)
685
+ described_class.any_instance.stub(:foo)
685
686
  end
686
687
  end
687
688
  END
@@ -691,47 +692,81 @@ module Transpec
691
692
  <<-END
692
693
  describe 'example' do
693
694
  it 'responds to #foo' do
694
- variable = String.any_instance
695
- allow_any_instance_of(String).to receive(:foo)
695
+ allow_any_instance_of(described_class).to receive(:foo)
696
696
  end
697
697
  end
698
698
  END
699
699
  end
700
700
 
701
- it 'converts into `allow_any_instance_of(Klass).to receive(:method)` form' do
702
- method_stub_object.allowize!
701
+ it 'converts into `allow_any_instance_of(described_class).to receive(:method)` form' do
703
702
  rewritten_source.should == expected_source
704
703
  end
705
704
 
706
- it "adds record \"`Klass.any_instance.stub(:message)` " +
707
- '-> `allow_any_instance_of(obj).to receive(:message)`"' do
708
- method_stub_object.allowize!
705
+ it 'adds record `Klass.any_instance.stub(:message)` ' +
706
+ '-> `allow_any_instance_of(obj).to receive(:message)`' do
709
707
  record.original_syntax.should == 'Klass.any_instance.stub(:message)'
710
708
  record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
711
709
  end
712
710
  end
713
- end
714
711
 
715
- [:unstub, :unstub!].each do |method|
716
- context "when it is `Klass.any_instance.#{method}(:method)` form" do
717
- let(:source) do
718
- <<-END
719
- describe 'example' do
720
- it 'does not respond to #foo' do
721
- Klass.any_instance.#{method}(:foo)
712
+ context 'when it is `variable.any_instance.stub(:method)` form ' +
713
+ 'and the variable is an AnyInstance::Recorder' do
714
+ context 'with runtime information' do
715
+ include_context 'dynamic analysis objects'
716
+
717
+ let(:source) do
718
+ <<-END
719
+ describe 'example' do
720
+ it 'responds to #foo' do
721
+ variable = String.any_instance
722
+ variable.stub(:foo)
723
+ end
722
724
  end
723
- end
724
- END
725
- end
725
+ END
726
+ end
726
727
 
727
- it 'does nothing' do
728
- method_stub_object.allowize!
729
- rewritten_source.should == source
728
+ let(:expected_source) do
729
+ <<-END
730
+ describe 'example' do
731
+ it 'responds to #foo' do
732
+ variable = String.any_instance
733
+ allow_any_instance_of(String).to receive(:foo)
734
+ end
735
+ end
736
+ END
737
+ end
738
+
739
+ it 'converts into `allow_any_instance_of(Klass).to receive(:method)` form' do
740
+ rewritten_source.should == expected_source
741
+ end
742
+
743
+ it 'adds record `Klass.any_instance.stub(:message)` ' +
744
+ '-> `allow_any_instance_of(obj).to receive(:message)`' do
745
+ record.original_syntax.should == 'Klass.any_instance.stub(:message)'
746
+ record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
747
+ end
730
748
  end
749
+ end
731
750
 
732
- it 'reports nothing' do
733
- method_stub_object.allowize!
734
- method_stub_object.report.records.should be_empty
751
+ [:unstub, :unstub!].each do |method|
752
+ context "when it is `Klass.any_instance.#{method}(:method)` form" do
753
+ let(:source) do
754
+ <<-END
755
+ describe 'example' do
756
+ it 'does not respond to #foo' do
757
+ Klass.any_instance.#{method}(:foo)
758
+ end
759
+ end
760
+ END
761
+ end
762
+
763
+ it 'does nothing' do
764
+ rewritten_source.should == source
765
+ end
766
+
767
+ it 'reports nothing' do
768
+ method_stub_object.report.records.should be_empty
769
+ end
735
770
  end
736
771
  end
737
772
  end
@@ -771,7 +806,7 @@ module Transpec
771
806
  end
772
807
 
773
808
  it 'adds record ' +
774
- "\"`obj.#{method}(:message)` -> `obj.#{replacement_method}(:message)`\"" do
809
+ "`obj.#{method}(:message)` -> `obj.#{replacement_method}(:message)`" do
775
810
  record.original_syntax.should == "obj.#{method}(:message)"
776
811
  record.converted_syntax.should == "obj.#{replacement_method}(:message)"
777
812
  end