transpec 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +0 -4
  3. data/.travis.yml +2 -1
  4. data/CHANGELOG.md +4 -0
  5. data/Guardfile +1 -1
  6. data/README.md +168 -18
  7. data/README.md.erb +154 -18
  8. data/lib/transpec/ast/node.rb +47 -0
  9. data/lib/transpec/cli.rb +1 -1
  10. data/lib/transpec/commit_message.rb +11 -1
  11. data/lib/transpec/configuration.rb +11 -10
  12. data/lib/transpec/converter.rb +36 -14
  13. data/lib/transpec/dynamic_analyzer/rewriter.rb +2 -2
  14. data/lib/transpec/option_parser.rb +11 -0
  15. data/lib/transpec/report.rb +16 -9
  16. data/lib/transpec/rspec_version.rb +9 -0
  17. data/lib/transpec/static_context_inspector.rb +4 -4
  18. data/lib/transpec/syntax/allow.rb +20 -0
  19. data/lib/transpec/syntax/example.rb +1 -1
  20. data/lib/transpec/syntax/expect.rb +5 -20
  21. data/lib/transpec/syntax/its.rb +2 -8
  22. data/lib/transpec/syntax/method_stub.rb +72 -37
  23. data/lib/transpec/syntax/mixin/allow_no_message.rb +8 -17
  24. data/lib/transpec/syntax/mixin/any_instance_block.rb +34 -0
  25. data/lib/transpec/syntax/mixin/expect_base.rb +70 -0
  26. data/lib/transpec/syntax/mixin/expectizable.rb +3 -0
  27. data/lib/transpec/syntax/mixin/have_matcher_owner.rb +5 -2
  28. data/lib/transpec/syntax/mixin/monkey_patch.rb +6 -0
  29. data/lib/transpec/syntax/mixin/{any_instance.rb → monkey_patch_any_instance.rb} +12 -8
  30. data/lib/transpec/syntax/mixin/send.rb +16 -3
  31. data/lib/transpec/syntax/mixin/should_base.rb +8 -2
  32. data/lib/transpec/syntax/oneliner_should.rb +2 -4
  33. data/lib/transpec/syntax/operator_matcher.rb +2 -2
  34. data/lib/transpec/syntax/raise_error.rb +2 -2
  35. data/lib/transpec/syntax/receive.rb +49 -0
  36. data/lib/transpec/syntax/rspec_configure.rb +8 -96
  37. data/lib/transpec/syntax/rspec_configure/expectations.rb +15 -0
  38. data/lib/transpec/syntax/rspec_configure/framework.rb +166 -0
  39. data/lib/transpec/syntax/rspec_configure/mocks.rb +19 -0
  40. data/lib/transpec/syntax/should.rb +1 -4
  41. data/lib/transpec/syntax/should_receive.rb +89 -43
  42. data/lib/transpec/util.rb +21 -7
  43. data/lib/transpec/version.rb +2 -2
  44. data/spec/transpec/ast/node_spec.rb +52 -0
  45. data/spec/transpec/commit_message_spec.rb +2 -2
  46. data/spec/transpec/configuration_spec.rb +14 -13
  47. data/spec/transpec/converter_spec.rb +151 -20
  48. data/spec/transpec/option_parser_spec.rb +10 -0
  49. data/spec/transpec/rspec_version_spec.rb +20 -6
  50. data/spec/transpec/static_context_inspector_spec.rb +2 -2
  51. data/spec/transpec/syntax/allow_spec.rb +140 -0
  52. data/spec/transpec/syntax/double_spec.rb +1 -1
  53. data/spec/transpec/syntax/expect_spec.rb +103 -10
  54. data/spec/transpec/syntax/have_spec.rb +4 -4
  55. data/spec/transpec/syntax/method_stub_spec.rb +41 -1
  56. data/spec/transpec/syntax/operator_matcher_spec.rb +6 -6
  57. data/spec/transpec/syntax/receive_spec.rb +270 -0
  58. data/spec/transpec/syntax/rspec_configure_spec.rb +241 -30
  59. data/spec/transpec/syntax/should_receive_spec.rb +93 -2
  60. data/spec/transpec/syntax/should_spec.rb +2 -2
  61. data/spec/transpec/util_spec.rb +2 -6
  62. data/transpec.gemspec +5 -3
  63. metadata +37 -14
@@ -145,6 +145,16 @@ module Transpec
145
145
  end
146
146
  end
147
147
 
148
+ describe '-a/--no-yield-any-instance option' do
149
+ let(:args) { ['--no-yield-any-instance'] }
150
+
151
+ it 'sets Configuration#add_receiver_arg_to_any_instance_implementation_block? false' do
152
+ parser.parse(args)
153
+ configuration.add_receiver_arg_to_any_instance_implementation_block?
154
+ .should be_false
155
+ end
156
+ end
157
+
148
158
  describe '-p/--no-parentheses-matcher-arg option' do
149
159
  let(:args) { ['--no-parentheses-matcher-arg'] }
150
160
 
@@ -7,7 +7,7 @@ module Transpec
7
7
  describe RSpecVersion do
8
8
  subject(:rspec_version) { RSpecVersion.new(version_string) }
9
9
 
10
- shared_examples 'feature availability' do |method, expectations|
10
+ shared_examples 'version comparisons' do |method, expectations|
11
11
  describe "##{method}" do
12
12
  subject { rspec_version.send(method) }
13
13
 
@@ -23,8 +23,12 @@ module Transpec
23
23
  end
24
24
  end
25
25
 
26
- [:be_truthy_available?, :yielded_example_available?].each do |method|
27
- include_examples 'feature availability', method, [
26
+ [
27
+ :be_truthy_available?,
28
+ :yielded_example_available?,
29
+ :yielding_receiver_to_any_instance_implementation_block_available?
30
+ ].each do |method|
31
+ include_examples 'version comparisons', method, [
28
32
  ['2.14.0', false],
29
33
  ['2.99.0.beta1', true],
30
34
  ['2.99.0.beta2', true],
@@ -36,7 +40,7 @@ module Transpec
36
40
  end
37
41
 
38
42
  [:oneliner_is_expected_available?].each do |method|
39
- include_examples 'feature availability', method, [
43
+ include_examples 'version comparisons', method, [
40
44
  ['2.14.0', false],
41
45
  ['2.99.0.beta1', false],
42
46
  ['2.99.0.beta2', true],
@@ -48,7 +52,7 @@ module Transpec
48
52
  end
49
53
 
50
54
  [:receive_messages_available?].each do |method|
51
- include_examples 'feature availability', method, [
55
+ include_examples 'version comparisons', method, [
52
56
  ['2.14.0', false],
53
57
  ['2.99.0.beta1', false],
54
58
  ['2.99.0.beta2', false],
@@ -60,7 +64,7 @@ module Transpec
60
64
  end
61
65
 
62
66
  [:receive_message_chain_available?, :non_should_matcher_protocol_available?].each do |method|
63
- include_examples 'feature availability', method, [
67
+ include_examples 'version comparisons', method, [
64
68
  ['2.14.0', false],
65
69
  ['2.99.0.beta1', false],
66
70
  ['2.99.0.beta2', false],
@@ -70,5 +74,15 @@ module Transpec
70
74
  ['3.0.0', true]
71
75
  ]
72
76
  end
77
+
78
+ include_examples 'version comparisons', :migration_term_of_any_instance_implementation_block?, [
79
+ ['2.14.0', false],
80
+ ['2.99.0.beta1', true],
81
+ ['2.99.0.beta2', true],
82
+ ['2.99.0', true],
83
+ ['3.0.0.beta1', false],
84
+ ['3.0.0.beta2', false],
85
+ ['3.0.0', false]
86
+ ]
73
87
  end
74
88
  end
@@ -530,7 +530,7 @@ module Transpec
530
530
  end
531
531
 
532
532
  context 'when in a class in a block in #describe block' do
533
- let(:source) do
533
+ let(:source) do
534
534
  <<-END
535
535
  describe 'foo' do
536
536
  it 'is an example' do
@@ -546,7 +546,7 @@ module Transpec
546
546
  end
547
547
 
548
548
  context 'when in an instance method in a class in a block in #describe block' do
549
- let(:source) do
549
+ let(:source) do
550
550
  <<-END
551
551
  describe 'foo' do
552
552
  it 'is an example' do
@@ -0,0 +1,140 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'transpec/syntax/allow'
5
+
6
+ module Transpec
7
+ class Syntax
8
+ describe Allow do
9
+ include_context 'parsed objects'
10
+ include_context 'syntax object', Allow, :allow_object
11
+
12
+ describe '#subject_node' do
13
+ let(:source) do
14
+ <<-END
15
+ describe 'example' do
16
+ it 'is empty' do
17
+ allow(subject).to be_empty
18
+ end
19
+ end
20
+ END
21
+ end
22
+
23
+ it 'returns subject node' do
24
+ method_name = allow_object.subject_node.children[1]
25
+ method_name.should == :subject
26
+ end
27
+ end
28
+
29
+ describe '#matcher_node' do
30
+ context 'when the matcher is taking a block' do
31
+ let(:source) do
32
+ <<-END
33
+ describe 'example' do
34
+ it 'is empty' do
35
+ allow(subject).to receive(:foo) { }
36
+ end
37
+ end
38
+ END
39
+ end
40
+
41
+ it 'returns send node of the matcher' do
42
+ method_name = allow_object.matcher_node.children[1]
43
+ method_name.should == :receive
44
+ end
45
+ end
46
+
47
+ context 'when the matcher is not taking a block' do
48
+ let(:source) do
49
+ <<-END
50
+ describe 'example' do
51
+ it 'is empty' do
52
+ allow(subject).to be_empty
53
+ end
54
+ end
55
+ END
56
+ end
57
+
58
+ it 'returns send node of the matcher' do
59
+ method_name = allow_object.matcher_node.children[1]
60
+ method_name.should == :be_empty
61
+ end
62
+ end
63
+ end
64
+
65
+ describe '#receive_matcher' do
66
+ subject { allow_object.receive_matcher }
67
+
68
+ context 'when it is taking #receive matcher' do
69
+ let(:source) do
70
+ <<-END
71
+ describe 'example' do
72
+ it 'receives :foo' do
73
+ allow(subject).to receive(:foo)
74
+ end
75
+ end
76
+ END
77
+ end
78
+
79
+ it 'returns an instance of Receive' do
80
+ should be_an(Receive)
81
+ end
82
+ end
83
+
84
+ context 'when it is taking any other matcher' do
85
+ let(:source) do
86
+ <<-END
87
+ describe 'example' do
88
+ it 'is empty' do
89
+ allow(subject).to be_empty
90
+ end
91
+ end
92
+ END
93
+ end
94
+
95
+ it 'returns nil' do
96
+ should be_nil
97
+ end
98
+ end
99
+ end
100
+
101
+ describe '#block_node' do
102
+ subject { allow_object.block_node }
103
+
104
+ context 'when the #to is taking a block' do
105
+ let(:source) do
106
+ <<-END
107
+ describe 'example' do
108
+ it 'receives :foo' do
109
+ allow(subject).to receive(:foo) do |arg|
110
+ end
111
+ end
112
+ end
113
+ END
114
+ end
115
+
116
+ it 'returns the block node' do
117
+ should be_block_type
118
+ end
119
+ end
120
+
121
+ context 'when the #to is not taking a block' do
122
+ let(:source) do
123
+ <<-END
124
+ describe 'example' do
125
+ it 'receives :foo' do
126
+ allow(subject).to receive(:foo) { |arg|
127
+ }
128
+ end
129
+ end
130
+ END
131
+ end
132
+
133
+ it 'returns nil' do
134
+ should be_nil
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -12,7 +12,7 @@ module Transpec
12
12
  describe '.conversion_target_node?' do
13
13
  let(:send_node) do
14
14
  ast.each_descendent_node do |node|
15
- next unless node.type == :send
15
+ next unless node.send_type?
16
16
  method_name = node.children[1]
17
17
  next unless method_name == :double
18
18
  return node
@@ -27,19 +27,38 @@ module Transpec
27
27
  end
28
28
 
29
29
  describe '#matcher_node' do
30
- let(:source) do
31
- <<-END
32
- describe 'example' do
33
- it 'is empty' do
34
- expect(subject).to be_empty
30
+ context 'when the matcher is taking a block' do
31
+ let(:source) do
32
+ <<-END
33
+ describe 'example' do
34
+ it 'is empty' do
35
+ expect(subject).to receive(:foo) { }
36
+ end
35
37
  end
36
- end
37
- END
38
+ END
39
+ end
40
+
41
+ it 'returns send node of the matcher' do
42
+ method_name = expect_object.matcher_node.children[1]
43
+ method_name.should == :receive
44
+ end
38
45
  end
39
46
 
40
- it 'returns matcher node' do
41
- method_name = expect_object.matcher_node.children[1]
42
- method_name.should == :be_empty
47
+ context 'when the matcher is not taking a block' do
48
+ let(:source) do
49
+ <<-END
50
+ describe 'example' do
51
+ it 'is empty' do
52
+ expect(subject).to be_empty
53
+ end
54
+ end
55
+ END
56
+ end
57
+
58
+ it 'returns send node of the matcher' do
59
+ method_name = expect_object.matcher_node.children[1]
60
+ method_name.should == :be_empty
61
+ end
43
62
  end
44
63
  end
45
64
 
@@ -78,6 +97,80 @@ module Transpec
78
97
  end
79
98
  end
80
99
  end
100
+
101
+ describe '#receive_matcher' do
102
+ subject { expect_object.receive_matcher }
103
+
104
+ context 'when it is taking #receive matcher' do
105
+ let(:source) do
106
+ <<-END
107
+ describe 'example' do
108
+ it 'receives :foo' do
109
+ expect(subject).to receive(:foo)
110
+ end
111
+ end
112
+ END
113
+ end
114
+
115
+ it 'returns an instance of Receive' do
116
+ should be_an(Receive)
117
+ end
118
+ end
119
+
120
+ context 'when it is taking any other matcher' do
121
+ let(:source) do
122
+ <<-END
123
+ describe 'example' do
124
+ it 'is empty' do
125
+ expect(subject).to be_empty
126
+ end
127
+ end
128
+ END
129
+ end
130
+
131
+ it 'returns nil' do
132
+ should be_nil
133
+ end
134
+ end
135
+ end
136
+
137
+ describe '#block_node' do
138
+ subject { expect_object.block_node }
139
+
140
+ context 'when the #to is taking a block' do
141
+ let(:source) do
142
+ <<-END
143
+ describe 'example' do
144
+ it 'receives :foo' do
145
+ expect(subject).to receive(:foo) do |arg|
146
+ end
147
+ end
148
+ end
149
+ END
150
+ end
151
+
152
+ it 'returns the block node' do
153
+ should be_block_type
154
+ end
155
+ end
156
+
157
+ context 'when the #to is not taking a block' do
158
+ let(:source) do
159
+ <<-END
160
+ describe 'example' do
161
+ it 'receives :foo' do
162
+ expect(subject).to receive(:foo) { |arg|
163
+ }
164
+ end
165
+ end
166
+ END
167
+ end
168
+
169
+ it 'returns nil' do
170
+ should be_nil
171
+ end
172
+ end
173
+ end
81
174
  end
82
175
  end
83
176
  end
@@ -165,13 +165,13 @@ module Transpec
165
165
  let(:parenthesize_matcher_arg) { true }
166
166
 
167
167
  let(:expected_source) do
168
- <<-END
168
+ <<-END
169
169
  describe 'example' do
170
170
  it 'has 2 items' do
171
171
  expect(collection.size).to eq(2)
172
172
  end
173
173
  end
174
- END
174
+ END
175
175
  end
176
176
 
177
177
  before do
@@ -192,13 +192,13 @@ module Transpec
192
192
  let(:parenthesize_matcher_arg) { false }
193
193
 
194
194
  let(:expected_source) do
195
- <<-END
195
+ <<-END
196
196
  describe 'example' do
197
197
  it 'has 2 items' do
198
198
  expect(collection.size).to eq 2
199
199
  end
200
200
  end
201
- END
201
+ END
202
202
  end
203
203
 
204
204
  it 'converts into `expect(collection.size).to eq 2` form' do
@@ -15,7 +15,7 @@ module Transpec
15
15
  describe '.conversion_target_node?' do
16
16
  let(:send_node) do
17
17
  ast.each_descendent_node do |node|
18
- next unless node.type == :send
18
+ next unless node.send_type?
19
19
  method_name = node.children[1]
20
20
  next unless method_name == :stub
21
21
  return node
@@ -1026,6 +1026,46 @@ module Transpec
1026
1026
  end
1027
1027
  end
1028
1028
  end
1029
+
1030
+ describe '#add_receiver_arg_to_any_instance_implementation_block!' do
1031
+ before do
1032
+ method_stub_object.add_receiver_arg_to_any_instance_implementation_block!
1033
+ end
1034
+
1035
+ context 'when it is `Klass.any_instance.stub(:method) do |arg| .. end` form' do
1036
+ let(:source) do
1037
+ <<-END
1038
+ describe 'example' do
1039
+ it 'responds to #foo' do
1040
+ Klass.any_instance.stub(:foo) do |arg|
1041
+ end
1042
+ end
1043
+ end
1044
+ END
1045
+ end
1046
+
1047
+ let(:expected_source) do
1048
+ <<-END
1049
+ describe 'example' do
1050
+ it 'responds to #foo' do
1051
+ Klass.any_instance.stub(:foo) do |instance, arg|
1052
+ end
1053
+ end
1054
+ end
1055
+ END
1056
+ end
1057
+
1058
+ it 'converts into `Klass.any_instance.stub(:method) do |instance, arg| .. end` form' do
1059
+ rewritten_source.should == expected_source
1060
+ end
1061
+
1062
+ it 'adds record `Klass.any_instance.stub(:message) { |arg| }` ' +
1063
+ '-> `Klass.any_instance.stub(:message) { |instance, arg| }`' do
1064
+ record.original_syntax.should == 'Klass.any_instance.stub(:message) { |arg| }'
1065
+ record.converted_syntax.should == 'Klass.any_instance.stub(:message) { |instance, arg| }'
1066
+ end
1067
+ end
1068
+ end
1029
1069
  end
1030
1070
  end
1031
1071
  end