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
@@ -13,8 +13,8 @@ module Transpec
13
13
  let(:rspec_command) { nil }
14
14
 
15
15
  describe '.new' do
16
- context 'when when block is passed' do
17
- it 'yields instance' do
16
+ context 'when block is passed' do
17
+ it 'yields the instance' do
18
18
  yielded = false
19
19
 
20
20
  DynamicAnalyzer.new(silent: true) do |analyzer|
@@ -63,6 +63,7 @@ module Transpec
63
63
  describe '-k/--keep option' do
64
64
  [
65
65
  ['should', :convert_should?],
66
+ ['oneliner', :convert_oneliner?],
66
67
  ['should_receive', :convert_should_receive?],
67
68
  ['stub', :convert_stub?],
68
69
  ['have_items', :convert_have_items],
@@ -68,6 +68,16 @@ module Transpec
68
68
  rspec_version.to_s.should == RSpec::Core::Version::STRING
69
69
  end
70
70
  end
71
+
72
+ context 'when failed checking version' do
73
+ before do
74
+ IO.stub(:popen).and_return(nil)
75
+ end
76
+
77
+ it 'raises error' do
78
+ -> { rspec_version }.should raise_error(/failed checking/i)
79
+ end
80
+ end
71
81
  end
72
82
  end
73
83
  end
@@ -7,38 +7,62 @@ module Transpec
7
7
  describe RSpecVersion do
8
8
  subject(:rspec_version) { RSpecVersion.new(version_string) }
9
9
 
10
- describe '#be_truthy_available?' do
11
- subject { rspec_version.be_truthy_available? }
12
-
13
- [
14
- ['2.14.0', false],
15
- ['2.99.0.beta.1', true],
16
- ['2.99.0', true],
17
- ['3.0.0.beta.1', true],
18
- ['3.0.0', true]
19
- ].each do |version, expected|
20
- context "when the version is #{version}" do
21
- let(:version_string) { version }
22
- it { should == expected }
10
+ shared_examples 'feature availability' do |method, expectations|
11
+ describe "##{method}" do
12
+ subject { rspec_version.send(method) }
13
+
14
+ expectations.each do |version, expected|
15
+ context "when the version is #{version}" do
16
+ let(:version_string) { version }
17
+
18
+ it "returns #{expected.inspect}" do
19
+ should be expected
20
+ end
21
+ end
23
22
  end
24
23
  end
25
24
  end
26
25
 
27
- describe '#receive_messages_available?' do
28
- subject { rspec_version.receive_messages_available? }
29
-
30
- [
31
- ['2.14.0', false],
32
- ['2.99.0.beta.1', false],
33
- ['2.99.0', false],
34
- ['3.0.0.beta.1', true],
35
- ['3.0.0', true]
36
- ].each do |version, expected|
37
- context "when the version is #{version}" do
38
- let(:version_string) { version }
39
- it { should == expected }
40
- end
41
- end
26
+ [:be_truthy_available?, :yielded_example_available?].each do |method|
27
+ include_examples 'feature availability', method, [
28
+ ['2.14.0', false],
29
+ ['2.99.0.beta1', true],
30
+ ['2.99.0', true],
31
+ ['3.0.0.beta1', true],
32
+ ['3.0.0', true]
33
+ ]
34
+ end
35
+
36
+ [:one_liner_is_expected_available?].each do |method|
37
+ include_examples 'feature availability', method, [
38
+ ['2.14.0', false],
39
+ ['2.99.0.beta1', false],
40
+ ['2.99.0.beta2', true],
41
+ ['2.99.0', true],
42
+ ['3.0.0.beta1', true],
43
+ ['3.0.0', true]
44
+ ]
45
+ end
46
+
47
+ [:receive_messages_available?].each do |method|
48
+ include_examples 'feature availability', method, [
49
+ ['2.14.0', false],
50
+ ['2.99.0.beta1', false],
51
+ ['2.99.0', false],
52
+ ['3.0.0.beta1', true],
53
+ ['3.0.0', true]
54
+ ]
55
+ end
56
+
57
+ [:receive_message_chain_available?].each do |method|
58
+ include_examples 'feature availability', method, [
59
+ ['2.14.0', false],
60
+ ['2.99.0.beta1', false],
61
+ ['2.99.0', false],
62
+ ['3.0.0.beta1', false],
63
+ ['3.0.0.beta2', true],
64
+ ['3.0.0', true]
65
+ ]
42
66
  end
43
67
  end
44
68
  end
@@ -143,7 +143,7 @@ module Transpec
143
143
  end
144
144
  end
145
145
 
146
- it { should == expected }
146
+ it { should be expected }
147
147
  end
148
148
 
149
149
  describe '#non_monkey_patch_mock_available?' do
@@ -156,7 +156,7 @@ module Transpec
156
156
  end
157
157
  end
158
158
 
159
- it { should == expected }
159
+ it { should be expected }
160
160
  end
161
161
  end
162
162
 
@@ -7,14 +7,7 @@ module Transpec
7
7
  class Syntax
8
8
  describe BeBoolean do
9
9
  include_context 'parsed objects'
10
-
11
- subject(:be_boolean_object) do
12
- ast.each_node do |node|
13
- next unless BeBoolean.target_node?(node)
14
- return BeBoolean.new(node, source_rewriter)
15
- end
16
- fail 'No be_boolean node is found!'
17
- end
10
+ include_context 'syntax object', BeBoolean, :be_boolean_object
18
11
 
19
12
  let(:record) { be_boolean_object.report.records.last }
20
13
 
@@ -50,7 +43,7 @@ module Transpec
50
43
  rewritten_source.should == expected_source
51
44
  end
52
45
 
53
- it 'adds record "`be_true` -> `be_truthy`"' do
46
+ it 'adds record `be_true` -> `be_truthy`' do
54
47
  record.original_syntax.should == 'be_true'
55
48
  record.converted_syntax.should == 'be_truthy'
56
49
  end
@@ -81,7 +74,7 @@ module Transpec
81
74
  rewritten_source.should == expected_source
82
75
  end
83
76
 
84
- it 'adds record "`be_false` -> `be_falsey`"' do
77
+ it 'adds record `be_false` -> `be_falsey`' do
85
78
  record.original_syntax.should == 'be_false'
86
79
  record.converted_syntax.should == 'be_falsey'
87
80
  end
@@ -103,7 +96,7 @@ module Transpec
103
96
  rewritten_source.should == expected_source
104
97
  end
105
98
 
106
- it 'adds record "`be_false` -> `be_falsy`"' do
99
+ it 'adds record `be_false` -> `be_falsy`' do
107
100
  record.original_syntax.should == 'be_false'
108
101
  record.converted_syntax.should == 'be_falsy'
109
102
  end
@@ -141,7 +134,7 @@ module Transpec
141
134
  rewritten_source.should == expected_source
142
135
  end
143
136
 
144
- it 'adds record "`be_true` -> `be true`"' do
137
+ it 'adds record `be_true` -> `be true`' do
145
138
  record.original_syntax.should == 'be_true'
146
139
  record.converted_syntax.should == 'be true'
147
140
  end
@@ -172,7 +165,7 @@ module Transpec
172
165
  rewritten_source.should == expected_source
173
166
  end
174
167
 
175
- it 'adds record "`be_false` -> `be false`"' do
168
+ it 'adds record `be_false` -> `be false`' do
176
169
  record.original_syntax.should == 'be_false'
177
170
  record.converted_syntax.should == 'be false'
178
171
  end
@@ -7,14 +7,7 @@ module Transpec
7
7
  class Syntax
8
8
  describe BeClose do
9
9
  include_context 'parsed objects'
10
-
11
- subject(:be_close_object) do
12
- ast.each_node do |node|
13
- next unless BeClose.target_node?(node)
14
- return BeClose.new(node, source_rewriter)
15
- end
16
- fail 'No be_close node is found!'
17
- end
10
+ include_context 'syntax object', BeClose, :be_close_object
18
11
 
19
12
  describe '#convert_to_be_within!' do
20
13
  before do
@@ -46,7 +39,7 @@ module Transpec
46
39
  rewritten_source.should == expected_source
47
40
  end
48
41
 
49
- it 'adds record "`be_close(expected, delta)` -> `be_within(delta).of(expected)`"' do
42
+ it 'adds record `be_close(expected, delta)` -> `be_within(delta).of(expected)`' do
50
43
  record = be_close_object.report.records.first
51
44
  record.original_syntax.should == 'be_close(expected, delta)'
52
45
  record.converted_syntax.should == 'be_within(delta).of(expected)'
@@ -7,14 +7,7 @@ module Transpec
7
7
  class Syntax
8
8
  describe Double do
9
9
  include_context 'parsed objects'
10
-
11
- subject(:double_object) do
12
- ast.each_node do |node|
13
- next unless Double.target_node?(node)
14
- return Double.new(node, source_rewriter)
15
- end
16
- fail 'No double node is found!'
17
- end
10
+ include_context 'syntax object', Double, :double_object
18
11
 
19
12
  describe '.target_node?' do
20
13
  let(:send_node) do
@@ -151,7 +144,7 @@ module Transpec
151
144
  rewritten_source.should == expected_source
152
145
  end
153
146
 
154
- it "adds record \"`#{method}('something')` -> `double('something')`\"" do
147
+ it "adds record `#{method}('something')` -> `double('something')`" do
155
148
  record = double_object.report.records.first
156
149
  record.original_syntax.should == "#{method}('something')"
157
150
  record.converted_syntax.should == "double('something')"
@@ -0,0 +1,249 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'transpec/syntax/example'
5
+
6
+ module Transpec
7
+ class Syntax
8
+ describe Example do
9
+ include_context 'parsed objects'
10
+ include_context 'syntax object', Example, :example_object
11
+
12
+ let(:record) { example_object.report.records.last }
13
+
14
+ describe '#convert!' do
15
+ before do
16
+ example_object.convert! unless example.metadata[:no_auto_convert]
17
+ end
18
+
19
+ (Util::EXAMPLE_METHODS + Util::HOOK_METHODS).each do |method|
20
+ context "when it is `#{method} do example end` form" do
21
+ let(:source) do
22
+ <<-END
23
+ describe 'example' do
24
+ #{method} do
25
+ do_something if example.metadata[:foo]
26
+ end
27
+ end
28
+ END
29
+ end
30
+
31
+ let(:expected_source) do
32
+ <<-END
33
+ describe 'example' do
34
+ #{method} do |example|
35
+ do_something if example.metadata[:foo]
36
+ end
37
+ end
38
+ END
39
+ end
40
+
41
+ it "converts into `#{method} do |example| example end` form" do
42
+ rewritten_source.should == expected_source
43
+ end
44
+
45
+ it "adds record `#{method} { example }` -> `#{method} { |example| example }`" do
46
+ record.original_syntax.should == "#{method} { example }"
47
+ record.converted_syntax.should == "#{method} { |example| example }"
48
+ end
49
+ end
50
+ end
51
+
52
+ Util::HELPER_METHODS.each do |method|
53
+ context "when it is `#{method}(:name) do example end` form" do
54
+ let(:source) do
55
+ <<-END
56
+ describe 'example' do
57
+ #{method}(:name) do
58
+ do_something if example.metadata[:foo]
59
+ end
60
+ end
61
+ END
62
+ end
63
+
64
+ let(:expected_source) do
65
+ <<-END
66
+ describe 'example' do
67
+ #{method}(:name) do |example|
68
+ do_something if example.metadata[:foo]
69
+ end
70
+ end
71
+ END
72
+ end
73
+
74
+ it "converts into `#{method}(:name) do |example| example end` form" do
75
+ rewritten_source.should == expected_source
76
+ end
77
+
78
+ it "adds record `#{method}(:name) { example }` -> `#{method}(:name) { |example| example }`" do
79
+ record.original_syntax.should == "#{method}(:name) { example }"
80
+ record.converted_syntax.should == "#{method}(:name) { |example| example }"
81
+ end
82
+ end
83
+ end
84
+
85
+ context 'when it is `after { example }` form' do
86
+ let(:source) do
87
+ <<-END
88
+ describe 'example' do
89
+ after {
90
+ do_something if example.metadata[:foo]
91
+ }
92
+ end
93
+ END
94
+ end
95
+
96
+ let(:expected_source) do
97
+ <<-END
98
+ describe 'example' do
99
+ after { |example|
100
+ do_something if example.metadata[:foo]
101
+ }
102
+ end
103
+ END
104
+ end
105
+
106
+ it 'converts into `after { |example| example }` form' do
107
+ rewritten_source.should == expected_source
108
+ end
109
+ end
110
+
111
+ context 'when it is `after do running_example end` form' do
112
+ let(:source) do
113
+ <<-END
114
+ describe 'example' do
115
+ after do
116
+ do_something if running_example.metadata[:foo]
117
+ end
118
+ end
119
+ END
120
+ end
121
+
122
+ let(:expected_source) do
123
+ <<-END
124
+ describe 'example' do
125
+ after do |example|
126
+ do_something if example.metadata[:foo]
127
+ end
128
+ end
129
+ END
130
+ end
131
+
132
+ it 'converts into `after do |example| example end` form' do
133
+ rewritten_source.should == expected_source
134
+ end
135
+ end
136
+
137
+ context 'when the wrapper block contains multiple invocation of `example`', :no_auto_convert do
138
+ let(:source) do
139
+ <<-END
140
+ describe 'example' do
141
+ after do
142
+ do_something if example.metadata[:foo]
143
+ puts example.description
144
+ end
145
+ end
146
+ END
147
+ end
148
+
149
+ let(:expected_source) do
150
+ <<-END
151
+ describe 'example' do
152
+ after do |example|
153
+ do_something if example.metadata[:foo]
154
+ puts example.description
155
+ end
156
+ end
157
+ END
158
+ end
159
+
160
+ let(:example_objects) do
161
+ ast.each_node.reduce([]) do |objects, node|
162
+ objects << Example.new(node, source_rewriter, runtime_data) if Example.target_node?(node)
163
+ objects
164
+ end
165
+ end
166
+
167
+ it 'adds only a block argument' do
168
+ example_objects.size.should eq(2)
169
+ example_objects.each(&:convert!)
170
+ rewritten_source.should == expected_source
171
+ end
172
+ end
173
+
174
+ context 'when it is `around do |ex| example end` form' do
175
+ let(:source) do
176
+ <<-END
177
+ describe 'example' do
178
+ around do |ex|
179
+ do_something if example.metadata[:foo]
180
+ end
181
+ end
182
+ END
183
+ end
184
+
185
+ let(:expected_source) do
186
+ <<-END
187
+ describe 'example' do
188
+ around do |ex|
189
+ do_something if ex.metadata[:foo]
190
+ end
191
+ end
192
+ END
193
+ end
194
+
195
+ it 'converts into `around do |ex| ex end` form' do
196
+ rewritten_source.should == expected_source
197
+ end
198
+ end
199
+
200
+ context 'when it is `def helper_method example; end` form' do
201
+ let(:source) do
202
+ <<-END
203
+ module Helper
204
+ def display_description
205
+ puts example.description
206
+ end
207
+ end
208
+
209
+ describe 'example' do
210
+ include Helper
211
+
212
+ after do
213
+ display_description
214
+ end
215
+ end
216
+ END
217
+ end
218
+
219
+ let(:expected_source) do
220
+ <<-END
221
+ module Helper
222
+ def display_description
223
+ puts RSpec.current_example.description
224
+ end
225
+ end
226
+
227
+ describe 'example' do
228
+ include Helper
229
+
230
+ after do
231
+ display_description
232
+ end
233
+ end
234
+ END
235
+ end
236
+
237
+ it 'converts into `def helper_method RSpec.current_example; end` form' do
238
+ rewritten_source.should == expected_source
239
+ end
240
+
241
+ it 'adds record `def helper_method example; end` -> `def helper_method RSpec.current_example; end`' do
242
+ record.original_syntax.should == 'def helper_method example; end'
243
+ record.converted_syntax.should == 'def helper_method RSpec.current_example; end'
244
+ end
245
+ end
246
+ end
247
+ end
248
+ end
249
+ end