transpec 2.2.5 → 2.3.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 (62) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/lib/transpec/conversion_error.rb +5 -5
  4. data/lib/transpec/converter.rb +14 -12
  5. data/lib/transpec/record.rb +88 -31
  6. data/lib/transpec/report.rb +19 -11
  7. data/lib/transpec/rspec_version.rb +9 -0
  8. data/lib/transpec/syntax/be_boolean.rb +2 -2
  9. data/lib/transpec/syntax/be_close.rb +1 -1
  10. data/lib/transpec/syntax/current_example.rb +35 -17
  11. data/lib/transpec/syntax/double.rb +1 -1
  12. data/lib/transpec/syntax/example.rb +0 -4
  13. data/lib/transpec/syntax/example_group.rb +11 -11
  14. data/lib/transpec/syntax/have/{have_record.rb → record_builder.rb} +15 -17
  15. data/lib/transpec/syntax/have.rb +2 -2
  16. data/lib/transpec/syntax/hook.rb +3 -3
  17. data/lib/transpec/syntax/its.rb +25 -17
  18. data/lib/transpec/syntax/matcher_definition.rb +1 -2
  19. data/lib/transpec/syntax/method_stub.rb +44 -51
  20. data/lib/transpec/syntax/mixin/any_instance_block.rb +8 -6
  21. data/lib/transpec/syntax/mixin/useless_and_return.rb +9 -7
  22. data/lib/transpec/syntax/oneliner_should.rb +29 -27
  23. data/lib/transpec/syntax/operator.rb +3 -3
  24. data/lib/transpec/syntax/pending.rb +0 -4
  25. data/lib/transpec/syntax/raise_error.rb +35 -15
  26. data/lib/transpec/syntax/receive.rb +6 -6
  27. data/lib/transpec/syntax/rspec_configure/config_modification.rb +39 -5
  28. data/lib/transpec/syntax/rspec_configure/framework.rb +27 -7
  29. data/lib/transpec/syntax/rspec_configure/mocks.rb +2 -2
  30. data/lib/transpec/syntax/rspec_configure.rb +35 -4
  31. data/lib/transpec/syntax/should.rb +35 -20
  32. data/lib/transpec/syntax/should_receive.rb +30 -27
  33. data/lib/transpec/syntax.rb +10 -0
  34. data/lib/transpec/version.rb +2 -2
  35. data/spec/integration/configuration_modification_spec.rb +43 -3
  36. data/spec/transpec/record_spec.rb +113 -18
  37. data/spec/transpec/report_spec.rb +15 -6
  38. data/spec/transpec/rspec_version_spec.rb +5 -0
  39. data/spec/transpec/syntax/be_boolean_spec.rb +10 -10
  40. data/spec/transpec/syntax/be_close_spec.rb +2 -2
  41. data/spec/transpec/syntax/current_example_spec.rb +6 -6
  42. data/spec/transpec/syntax/double_spec.rb +2 -2
  43. data/spec/transpec/syntax/example_group_spec.rb +4 -4
  44. data/spec/transpec/syntax/example_spec.rb +8 -8
  45. data/spec/transpec/syntax/have_spec.rb +44 -44
  46. data/spec/transpec/syntax/hook_spec.rb +8 -8
  47. data/spec/transpec/syntax/its_spec.rb +16 -16
  48. data/spec/transpec/syntax/matcher_definition_spec.rb +2 -2
  49. data/spec/transpec/syntax/method_stub_spec.rb +36 -36
  50. data/spec/transpec/syntax/oneliner_should_spec.rb +26 -26
  51. data/spec/transpec/syntax/operator_spec.rb +18 -18
  52. data/spec/transpec/syntax/pending_spec.rb +6 -6
  53. data/spec/transpec/syntax/raise_error_spec.rb +10 -10
  54. data/spec/transpec/syntax/receive_spec.rb +20 -20
  55. data/spec/transpec/syntax/rspec_configure_spec.rb +202 -18
  56. data/spec/transpec/syntax/should_receive_spec.rb +30 -30
  57. data/spec/transpec/syntax/should_spec.rb +8 -8
  58. data/tasks/fixtures/mail/2.99.0/COMMIT_EDITMSG +1 -1
  59. data/tasks/fixtures/mail/3.0.0/COMMIT_EDITMSG +5 -2
  60. data/tasks/fixtures/twitter/2.99.0/COMMIT_EDITMSG +1 -1
  61. data/tasks/fixtures/twitter/3.0.0/COMMIT_EDITMSG +2 -2
  62. metadata +2 -2
@@ -22,8 +22,7 @@ module Transpec
22
22
  def convert_deprecated_method!
23
23
  replacement_method_name = CONVERSION_CORRESPONDENCE[method_name].to_s
24
24
  replace(selector_range, replacement_method_name)
25
-
26
- report.records << Record.new("#{method_name} { }", "#{replacement_method_name} { }")
25
+ add_record("#{method_name} { }", "#{replacement_method_name} { }")
27
26
  end
28
27
  end
29
28
  end
@@ -71,29 +71,37 @@ module Transpec
71
71
 
72
72
  replace(expression_range, source)
73
73
 
74
- add_record(type)
74
+ add_record(AllowRecordBuilder.build(self, type))
75
75
  end
76
76
 
77
77
  def convert_deprecated_method!
78
- return unless replacement_method_for_deprecated_method
79
-
80
- replace(selector_range, replacement_method_for_deprecated_method)
81
-
82
- add_record(:deprecated)
78
+ return unless replacement_for_deprecated_method
79
+ replace(selector_range, replacement_for_deprecated_method)
80
+ add_record(DeprecatedMethodRecordBuilder.build(self))
83
81
  end
84
82
 
85
83
  def remove_no_message_allowance!
86
84
  return unless allow_no_message?
87
85
  super
88
- add_record(:no_message_allowance)
86
+ add_record(NoMessageAllowanceRecordBuilder.build(self))
89
87
  end
90
88
 
91
89
  def remove_useless_and_return!
92
- super && add_record(:useless_and_return)
90
+ return unless super
91
+ add_record(Mixin::UselessAndReturn::MonkeyPatchRecordBuilder.build(self))
93
92
  end
94
93
 
95
94
  def add_receiver_arg_to_any_instance_implementation_block!
96
- super && add_record(:any_instance_block)
95
+ return unless super
96
+ add_record(Mixin::AnyInstanceBlock::MonkeyPatchRecordBuilder.build(self))
97
+ end
98
+
99
+ def replacement_for_deprecated_method
100
+ case method_name
101
+ when :stub! then 'stub'
102
+ when :unstub! then 'unstub'
103
+ else nil
104
+ end
97
105
  end
98
106
 
99
107
  private
@@ -161,51 +169,34 @@ module Transpec
161
169
  message_source
162
170
  end
163
171
 
164
- def replacement_method_for_deprecated_method
165
- case method_name
166
- when :stub! then 'stub'
167
- when :unstub! then 'unstub'
168
- else nil
169
- end
170
- end
171
-
172
- def add_record(conversion_type)
173
- record_class = case conversion_type
174
- when :deprecated then DeprecatedMethodRecord
175
- when :no_message_allowance then NoMessageAllowanceRecord
176
- when :useless_and_return then MonkeyPatchUselessAndReturnRecord
177
- when :any_instance_block then MonkeyPatchAnyInstanceBlockRecord
178
- else AllowRecord
179
- end
180
- report.records << record_class.new(self, conversion_type)
181
- end
172
+ class AllowRecordBuilder < RecordBuilder
173
+ attr_reader :method_stub, :conversion_type
182
174
 
183
- class AllowRecord < Record
184
175
  def initialize(method_stub, conversion_type)
185
176
  @method_stub = method_stub
186
177
  @conversion_type = conversion_type
187
178
  end
188
179
 
189
- def build_original_syntax
190
- syntax = @method_stub.any_instance? ? 'Klass.any_instance' : 'obj'
191
- syntax << ".#{@method_stub.method_name}"
180
+ def old_syntax
181
+ syntax = method_stub.any_instance? ? 'Klass.any_instance' : 'obj'
182
+ syntax << ".#{method_stub.method_name}"
192
183
 
193
- if @method_stub.method_name == :stub_chain
184
+ if method_stub.method_name == :stub_chain
194
185
  syntax << '(:message1, :message2)'
195
186
  else
196
- syntax << (@method_stub.hash_arg? ? '(:message => value)' : '(:message)')
187
+ syntax << (method_stub.hash_arg? ? '(:message => value)' : '(:message)')
197
188
  end
198
189
  end
199
190
 
200
- def build_converted_syntax
201
- syntax = @method_stub.any_instance? ? 'allow_any_instance_of(Klass)' : 'allow(obj)'
191
+ def new_syntax
192
+ syntax = method_stub.any_instance? ? 'allow_any_instance_of(Klass)' : 'allow(obj)'
202
193
  syntax << '.to '
203
194
 
204
- case @conversion_type
195
+ case conversion_type
205
196
  when :allow_to_receive
206
197
  syntax << 'receive(:message)'
207
- syntax << '.and_return(value)' if @method_stub.hash_arg?
208
- syntax << '.and_call_original' if @method_stub.unstub?
198
+ syntax << '.and_return(value)' if method_stub.hash_arg?
199
+ syntax << '.and_call_original' if method_stub.unstub?
209
200
  when :allow_to_receive_messages
210
201
  syntax << 'receive_messages(:message => value)'
211
202
  when :allow_to_receive_message_chain
@@ -216,38 +207,40 @@ module Transpec
216
207
  end
217
208
  end
218
209
 
219
- class DeprecatedMethodRecord < Record
220
- def initialize(method_stub, *)
210
+ class DeprecatedMethodRecordBuilder < RecordBuilder
211
+ attr_reader :method_stub
212
+
213
+ def initialize(method_stub)
221
214
  @method_stub = method_stub
222
215
  end
223
216
 
224
- def build_original_syntax
225
- syntax = @method_stub.any_instance? ? 'Klass.any_instance' : 'obj'
226
- syntax << ".#{@method_stub.method_name}(:message)"
217
+ def old_syntax
218
+ syntax = method_stub.any_instance? ? 'Klass.any_instance' : 'obj'
219
+ syntax << ".#{method_stub.method_name}(:message)"
227
220
  end
228
221
 
229
- def build_converted_syntax
222
+ def new_syntax
230
223
  syntax = 'obj.'
231
- syntax << @method_stub.send(:replacement_method_for_deprecated_method)
224
+ syntax << method_stub.replacement_for_deprecated_method
232
225
  syntax << '(:message)'
233
226
  end
234
227
  end
235
228
 
236
- class NoMessageAllowanceRecord < Record
237
- def initialize(method_stub, *)
229
+ class NoMessageAllowanceRecordBuilder < RecordBuilder
230
+ attr_reader :method_stub
231
+
232
+ def initialize(method_stub)
238
233
  @method_stub = method_stub
239
234
  end
240
235
 
241
- private
242
-
243
- def build_original_syntax
236
+ def old_syntax
244
237
  syntax = base_syntax
245
238
  syntax << '.any_number_of_times' if @method_stub.any_number_of_times?
246
239
  syntax << '.at_least(0)' if @method_stub.at_least_zero?
247
240
  syntax
248
241
  end
249
242
 
250
- def build_converted_syntax
243
+ def new_syntax
251
244
  base_syntax
252
245
  end
253
246
 
@@ -36,16 +36,18 @@ module Transpec
36
36
  Util.each_backward_chained_node(node).find(&:block_type?)
37
37
  end
38
38
 
39
- class AnyInstanceBlockRecord < Record
40
- def initialize(host, *)
39
+ class RecordBuilder < Transpec::RecordBuilder
40
+ attr_reader :host
41
+
42
+ def initialize(host)
41
43
  @host = host
42
44
  end
43
45
 
44
- def build_original_syntax
46
+ def old_syntax
45
47
  "#{base_syntax} { |arg| }"
46
48
  end
47
49
 
48
- def build_converted_syntax
50
+ def new_syntax
49
51
  "#{base_syntax} { |instance, arg| }"
50
52
  end
51
53
 
@@ -54,9 +56,9 @@ module Transpec
54
56
  end
55
57
  end
56
58
 
57
- class MonkeyPatchAnyInstanceBlockRecord < AnyInstanceBlockRecord
59
+ class MonkeyPatchRecordBuilder < AnyInstanceBlock::RecordBuilder
58
60
  def base_syntax
59
- "Klass.any_instance.#{@host.method_name}(:message)"
61
+ "Klass.any_instance.#{host.method_name}(:message)"
60
62
  end
61
63
  end
62
64
  end
@@ -48,21 +48,23 @@ module Transpec
48
48
  receiver_node.loc.expression.end.join(and_return_node.loc.expression.end)
49
49
  end
50
50
 
51
- class UselessAndReturnRecord < Record
51
+ class RecordBuilder < Transpec::RecordBuilder
52
+ attr_reader :host
53
+
52
54
  def initialize(host, *)
53
55
  @host = host
54
56
  end
55
57
 
56
- def build_original_syntax
58
+ def old_syntax
57
59
  syntax = base_syntax
58
60
  syntax << '.and_return'
59
- syntax << ' { value }' if @host.and_return_with_block?
61
+ syntax << ' { value }' if host.and_return_with_block?
60
62
  syntax
61
63
  end
62
64
 
63
- def build_converted_syntax
65
+ def new_syntax
64
66
  syntax = base_syntax
65
- syntax << ' { value }' if @host.and_return_with_block?
67
+ syntax << ' { value }' if host.and_return_with_block?
66
68
  syntax
67
69
  end
68
70
 
@@ -71,9 +73,9 @@ module Transpec
71
73
  end
72
74
  end
73
75
 
74
- class MonkeyPatchUselessAndReturnRecord < UselessAndReturnRecord
76
+ class MonkeyPatchRecordBuilder < UselessAndReturn::RecordBuilder
75
77
  def base_syntax
76
- "obj.#{@host.method_name}(:message)"
78
+ "obj.#{host.method_name}(:message)"
77
79
  end
78
80
  end
79
81
  end
@@ -3,6 +3,7 @@
3
3
  require 'transpec/syntax'
4
4
  require 'transpec/syntax/mixin/should_base'
5
5
  require 'transpec/syntax/example'
6
+ require 'transpec/syntax/its'
6
7
  require 'transpec/util'
7
8
  require 'active_support/inflector/methods'
8
9
  require 'active_support/inflector/inflections'
@@ -41,7 +42,7 @@ module Transpec
41
42
 
42
43
  @current_syntax_type = :expect
43
44
 
44
- add_record(negative_form)
45
+ add_record(ExpectRecordBuilder.build(self, negative_form))
45
46
  end
46
47
 
47
48
  def convert_have_items_to_standard_should!
@@ -52,7 +53,7 @@ module Transpec
52
53
  subject_source = have_matcher.replacement_subject_source('subject')
53
54
  insert_before(expression_range, "#{subject_source}.")
54
55
 
55
- report.records << OnelinerShouldHaveRecord.new(self, have_matcher)
56
+ add_record(HaveRecordBuilder.build(self, have_matcher))
56
57
  end
57
58
 
58
59
  def convert_have_items_to_standard_expect!(negative_form = 'not_to')
@@ -67,7 +68,7 @@ module Transpec
67
68
 
68
69
  @current_syntax_type = :expect
69
70
 
70
- report.records << OnelinerShouldHaveRecord.new(self, have_matcher, negative_form)
71
+ add_record(HaveRecordBuilder.build(self, have_matcher, negative_form))
71
72
  end
72
73
 
73
74
  def example
@@ -131,25 +132,28 @@ module Transpec
131
132
  example.is_a?(Its)
132
133
  end
133
134
 
134
- def add_record(negative_form_of_to)
135
- original_syntax = 'it { should'
136
- converted_syntax = 'it { is_expected.'
135
+ class ExpectRecordBuilder < RecordBuilder
136
+ attr_reader :should, :negative_form_of_to
137
137
 
138
- if positive?
139
- converted_syntax << 'to'
140
- else
141
- original_syntax << '_not'
142
- converted_syntax << negative_form_of_to
138
+ def initialize(should, negative_form_of_to = nil)
139
+ @should = should
140
+ @negative_form_of_to = negative_form_of_to
143
141
  end
144
142
 
145
- [original_syntax, converted_syntax].each do |syntax|
143
+ def old_syntax
144
+ syntax = 'it { should'
145
+ syntax << '_not' unless should.positive?
146
146
  syntax << ' ... }'
147
147
  end
148
148
 
149
- report.records << Record.new(original_syntax, converted_syntax)
149
+ def new_syntax
150
+ syntax = 'it { is_expected.'
151
+ syntax << (should.positive? ? 'to' : negative_form_of_to)
152
+ syntax << ' ... }'
153
+ end
150
154
  end
151
155
 
152
- class OnelinerShouldHaveRecord < Have::HaveRecord
156
+ class HaveRecordBuilder < Have::RecordBuilder
153
157
  attr_reader :should, :negative_form_of_to
154
158
 
155
159
  def initialize(should, have, negative_form_of_to = nil)
@@ -158,24 +162,22 @@ module Transpec
158
162
  @negative_form_of_to = negative_form_of_to
159
163
  end
160
164
 
161
- private
162
-
163
- def build_original_syntax
165
+ def old_syntax
164
166
  syntax = should.example.description? ? "it '...' do" : 'it {'
165
- syntax << " #{should.method_name} #{have.method_name}(n).#{original_items} "
167
+ syntax << " #{should.method_name} #{have.method_name}(n).#{old_items} "
166
168
  syntax << (should.example.description? ? 'end' : '}')
167
169
  end
168
170
 
169
- def build_converted_syntax
170
- syntax = converted_description
171
+ def new_syntax
172
+ syntax = new_description
171
173
  syntax << ' '
172
- syntax << converted_expectation
174
+ syntax << new_expectation
173
175
  syntax << ' '
174
176
  syntax << source_builder.replacement_matcher_source
175
177
  syntax << ' end'
176
178
  end
177
179
 
178
- def converted_description
180
+ def new_description
179
181
  if should.example.description?
180
182
  "it '...' do"
181
183
  else
@@ -183,17 +185,17 @@ module Transpec
183
185
  end
184
186
  end
185
187
 
186
- def converted_expectation
188
+ def new_expectation
187
189
  case should.current_syntax_type
188
190
  when :should
189
- "#{converted_subject}.#{should.method_name}"
191
+ "#{new_subject}.#{should.method_name}"
190
192
  when :expect
191
- "expect(#{converted_subject})." + (should.positive? ? 'to' : negative_form_of_to)
193
+ "expect(#{new_subject})." + (should.positive? ? 'to' : negative_form_of_to)
192
194
  end
193
195
  end
194
196
 
195
- def converted_subject
196
- build_converted_subject('subject')
197
+ def new_subject
198
+ build_new_subject('subject')
197
199
  end
198
200
  end
199
201
  end
@@ -157,10 +157,10 @@ module Transpec
157
157
  end
158
158
  end
159
159
 
160
- def add_record(original_syntax, converted_syntax, accurate = true)
161
- original_syntax ||= "#{method_name} expected"
160
+ def add_record(old_syntax, new_syntax, accurate = true)
161
+ old_syntax ||= "#{method_name} expected"
162
162
  annotation = AccuracyAnnotation.new(matcher_range) unless accurate
163
- report.records << Record.new(original_syntax, converted_syntax, annotation)
163
+ super(old_syntax, new_syntax, { annotation: annotation })
164
164
  end
165
165
  end
166
166
  end
@@ -81,10 +81,6 @@ module Transpec
81
81
  def range_from_body_end_to_block_close
82
82
  block_body_node.loc.expression.end.join(block_node.loc.end)
83
83
  end
84
-
85
- def add_record(original_syntax, converted_syntax)
86
- report.records << Record.new(original_syntax, converted_syntax)
87
- end
88
84
  end
89
85
  end
90
86
  end
@@ -15,30 +15,50 @@ module Transpec
15
15
 
16
16
  def remove_error_specification_with_negative_expectation!
17
17
  return if expectation.positive?
18
-
19
- _receiver_node, _method_name, *arg_nodes = *node
20
- return if arg_nodes.empty?
21
-
18
+ return unless specific_error?
22
19
  remove(parentheses_range)
23
-
24
- add_record
20
+ add_record(RecordBuilder.build(self))
25
21
  end
26
22
 
27
- private
23
+ def specific_error?
24
+ !arg_nodes.empty?
25
+ end
28
26
 
29
- def add_record
30
- original_syntax = 'expect { }.not_to raise_error('
27
+ def specific_class?
28
+ specific_error? && arg_nodes.first.const_type?
29
+ end
31
30
 
32
- if arg_nodes.first.const_type?
33
- original_syntax << 'SpecificErrorClass'
34
- original_syntax << ', message' if arg_nodes.count >= 2
31
+ def specific_message?
32
+ if specific_class?
33
+ arg_nodes.count >= 2
35
34
  else
36
- original_syntax << 'message'
35
+ specific_error?
36
+ end
37
+ end
38
+
39
+ class RecordBuilder < Transpec::RecordBuilder
40
+ attr_reader :raise_error
41
+
42
+ def initialize(raise_error)
43
+ @raise_error = raise_error
37
44
  end
38
45
 
39
- original_syntax << ')'
46
+ def old_syntax
47
+ syntax = 'expect { }.not_to raise_error('
48
+
49
+ if raise_error.specific_class?
50
+ syntax << 'SpecificErrorClass'
51
+ syntax << ', ' if raise_error.specific_message?
52
+ end
53
+
54
+ syntax << 'message' if raise_error.specific_message?
40
55
 
41
- report.records << Record.new(original_syntax, 'expect { }.not_to raise_error')
56
+ syntax << ')'
57
+ end
58
+
59
+ def new_syntax
60
+ 'expect { }.not_to raise_error'
61
+ end
42
62
  end
43
63
  end
44
64
  end
@@ -17,13 +17,13 @@ module Transpec
17
17
  def remove_useless_and_return!
18
18
  removed = super
19
19
  return unless removed
20
- report.records << ReceiveUselessAndReturnRecord.new(self)
20
+ add_record(UselessAndReturnRecordBuilder.build(self))
21
21
  end
22
22
 
23
23
  def add_receiver_arg_to_any_instance_implementation_block!
24
24
  added = super
25
25
  return unless added
26
- report.records << ReceiveAnyInstanceBlockRecord.new(self)
26
+ add_record(AnyInstanceBlockRecordBuilder.build(self))
27
27
  end
28
28
 
29
29
  def any_instance?
@@ -35,15 +35,15 @@ module Transpec
35
35
  super || expectation.block_node
36
36
  end
37
37
 
38
- class ReceiveAnyInstanceBlockRecord < AnyInstanceBlockRecord
38
+ class UselessAndReturnRecordBuilder < Mixin::UselessAndReturn::RecordBuilder
39
39
  def base_syntax
40
- "#{@host.expectation.method_name}(Klass).to receive(:message)"
40
+ "#{host.expectation.method_name_for_instance}(obj).to receive(:message)"
41
41
  end
42
42
  end
43
43
 
44
- class ReceiveUselessAndReturnRecord < UselessAndReturnRecord
44
+ class AnyInstanceBlockRecordBuilder < Mixin::AnyInstanceBlock::RecordBuilder
45
45
  def base_syntax
46
- "#{@host.expectation.method_name_for_instance}(obj).to receive(:message)"
46
+ "#{host.expectation.method_name}(Klass).to receive(:message)"
47
47
  end
48
48
  end
49
49
  end
@@ -15,12 +15,13 @@ module Transpec
15
15
 
16
16
  private
17
17
 
18
- def set_config!(config_name, value, comment = nil)
19
- setter_node = find_config_node("#{config_name}=")
18
+ def set_config_value!(config_name, value, comment = nil)
19
+ config_node = find_config_node("#{config_name}=")
20
20
 
21
- if setter_node
22
- arg_node = setter_node.children[2]
23
- source_rewriter.replace(arg_node.loc.expression, value.to_s)
21
+ if config_node
22
+ current_value = config_node.children[2].loc.expression.source
23
+ return if value.to_s == current_value
24
+ modify_config_value!(config_node, value)
24
25
  else
25
26
  add_config!(config_name, value, comment)
26
27
  end
@@ -39,11 +40,42 @@ module Transpec
39
40
  end
40
41
  end
41
42
 
43
+ def modify_config_value!(config_node, value)
44
+ arg_range = config_node.children[2].loc.expression
45
+ source_rewriter.replace(arg_range, value.to_s)
46
+
47
+ config_name = config_node.loc.selector.source
48
+ old_syntax = config_record_syntax(config_name, arg_range.source)
49
+ new_syntax = config_record_syntax(config_name, value)
50
+ add_record(old_syntax, new_syntax, type: :modification)
51
+ end
52
+
53
+ def replace_config!(old_config_name, new_config_name)
54
+ config_node = find_config_node(old_config_name)
55
+ return unless config_node
56
+ new_selector = new_config_name.to_s.sub(/=$/, '')
57
+ source_rewriter.replace(config_node.loc.selector, new_selector)
58
+
59
+ old_syntax = config_record_syntax(old_config_name)
60
+ new_syntax = config_record_syntax(new_config_name)
61
+ add_record(old_syntax, new_syntax)
62
+ end
63
+
42
64
  def block_arg_name
43
65
  return nil unless block_node
44
66
  first_block_arg_name(block_node)
45
67
  end
46
68
 
69
+ def config_record_syntax(config_name, value = nil)
70
+ selector = config_name.to_s.sub(/=$/, '')
71
+ syntax = "RSpec.configure { |c| c.#{selector}"
72
+
73
+ value = 'something' if config_name.to_s.end_with?('=')
74
+ syntax << " = #{value}" unless value.nil?
75
+
76
+ syntax << ' }'
77
+ end
78
+
47
79
  # TODO: Refactor this to remove messy overrides in Framework.
48
80
  module ConfigAddition
49
81
  def add_config!(config_name, value = nil, comment = nil)
@@ -55,6 +87,8 @@ module Transpec
55
87
  source_rewriter.insert_before(insertion_position, lines.join(''))
56
88
 
57
89
  block_node_to_insert_code.metadata[:added_config] = true
90
+
91
+ add_record(nil, config_record_syntax(config_name, value))
58
92
  end
59
93
 
60
94
  def generate_config_lines(config_name, value = nil, comment = nil)
@@ -5,14 +5,23 @@ require 'transpec/syntax/rspec_configure/config_modification'
5
5
  module Transpec
6
6
  class Syntax
7
7
  class RSpecConfigure
8
+ # This cannot be a Syntax class since this can be instanciated and used even when there's no
9
+ # corresponding node in the existing code.
8
10
  class Framework
9
11
  include ConfigModification
10
12
 
11
- attr_reader :rspec_configure, :source_rewriter
13
+ attr_reader :rspec_configure
12
14
 
13
- def initialize(rspec_configure, source_rewriter)
15
+ def initialize(rspec_configure)
14
16
  @rspec_configure = rspec_configure
15
- @source_rewriter = source_rewriter
17
+ end
18
+
19
+ def source_rewriter
20
+ rspec_configure.source_rewriter
21
+ end
22
+
23
+ def add_record(*args)
24
+ rspec_configure.add_record(*args)
16
25
  end
17
26
 
18
27
  def block_node
@@ -50,14 +59,17 @@ module Transpec
50
59
  end
51
60
 
52
61
  def new_config_variable_name
53
- framework_name = self.class.name.split('::').last.downcase
54
- if rspec_configure.block_arg_name.to_s == framework_name
62
+ if rspec_configure.block_arg_name.to_s == framework_type_name
55
63
  'config'
56
64
  else
57
- framework_name
65
+ framework_type_name
58
66
  end
59
67
  end
60
68
 
69
+ def framework_type_name
70
+ @framework_type_name ||= self.class.name.split('::').last.downcase
71
+ end
72
+
61
73
  def body_indentation
62
74
  if block_node
63
75
  super
@@ -86,6 +98,14 @@ module Transpec
86
98
  indentation_of_line(rspec_configure.node) + (' ' * 2)
87
99
  end
88
100
 
101
+ def config_record_syntax(config_name, value = nil)
102
+ inner_block_arg = framework_type_name[0]
103
+ syntax = "RSpec.configure { |c| c.#{block_method_name} :rspec "
104
+ syntax << "{ |#{inner_block_arg}| #{inner_block_arg}.#{config_name}"
105
+ syntax << " = #{value}" unless value.nil?
106
+ syntax << ' } }'
107
+ end
108
+
89
109
  module SyntaxConfig
90
110
  def syntaxes
91
111
  return [] unless syntaxes_node
@@ -107,7 +127,7 @@ module Transpec
107
127
  fail ArgumentError, 'Syntaxes must be either an array or a symbol.'
108
128
  end
109
129
 
110
- set_config!(:syntax, syntaxes.inspect)
130
+ set_config_value!(:syntax, syntaxes.inspect)
111
131
  end
112
132
 
113
133
  private
@@ -10,7 +10,7 @@ module Transpec
10
10
  :mock_with
11
11
  end
12
12
 
13
- def yield_receiver_to_any_instance_implementation_blocks=(boolean)
13
+ def yield_receiver_to_any_instance_implementation_blocks=(value)
14
14
  # rubocop:disable LineLength
15
15
  #
16
16
  # Based on the deprecation warning in RSpec 2.99:
@@ -26,7 +26,7 @@ module Transpec
26
26
  |`any_instance` implementation blocks to account for the first block argument
27
27
  |being the receiving instance.
28
28
  END
29
- set_config!(:yield_receiver_to_any_instance_implementation_blocks, boolean, comment)
29
+ set_config_value!(:yield_receiver_to_any_instance_implementation_blocks, value, comment)
30
30
  end
31
31
  end
32
32
  end