Class: Transpec::Converter

Inherits:
BaseRewriter show all
Defined in:
lib/transpec/converter.rb

Overview

rubocop:disable ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRewriter

#rewrite, #rewrite_file!

Constructor Details

#initialize(configuration = nil, rspec_version = nil, runtime_data = nil) ⇒ Converter

Returns a new instance of Converter.



18
19
20
21
22
23
# File 'lib/transpec/converter.rb', line 18

def initialize(configuration = nil, rspec_version = nil, runtime_data = nil)
  @configuration = configuration || Configuration.new
  @rspec_version = rspec_version || Transpec.current_rspec_version
  @runtime_data = runtime_data
  @report = Report.new
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



13
14
15
# File 'lib/transpec/converter.rb', line 13

def configuration
  @configuration
end

#reportObject (readonly)

Returns the value of attribute report.



13
14
15
# File 'lib/transpec/converter.rb', line 13

def report
  @report
end

#rspec_versionObject (readonly)

Returns the value of attribute rspec_version.



13
14
15
# File 'lib/transpec/converter.rb', line 13

def rspec_version
  @rspec_version
end

#runtime_dataObject (readonly)

Returns the value of attribute runtime_data.



13
14
15
# File 'lib/transpec/converter.rb', line 13

def runtime_data
  @runtime_data
end

Instance Method Details

#dispatch_node(node, source_rewriter) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/transpec/converter.rb', line 32

def dispatch_node(node, source_rewriter)
  Syntax.standalone_syntaxes.each do |syntax_class|
    next unless syntax_class.conversion_target_node?(node, runtime_data)

    syntax = syntax_class.new(node, source_rewriter, runtime_data, report)

    handler_name = "process_#{syntax_class.snake_case_name}"
    send(handler_name, syntax)

    break
  end
rescue OverlappedRewriteError # rubocop:disable HandleExceptions
rescue ConversionError => error
  report.conversion_errors << error
end

#need_to_modify_expectation_syntax_configuration?(rspec_configure) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
223
224
225
# File 'lib/transpec/converter.rb', line 220

def need_to_modify_expectation_syntax_configuration?(rspec_configure)
  return false unless configuration.convert_should?
  rspec_configure.expectations.syntaxes == [:should]
rescue Syntax::RSpecConfigure::Framework::UnknownSyntaxError
  false
end

#need_to_modify_mock_syntax_configuration?(rspec_configure) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
# File 'lib/transpec/converter.rb', line 227

def need_to_modify_mock_syntax_configuration?(rspec_configure)
  return false if !configuration.convert_should_receive? &&
                  !configuration.convert_stub?
  rspec_configure.mocks.syntaxes == [:should]
rescue Syntax::RSpecConfigure::Framework::UnknownSyntaxError
  false
end

#process(ast, source_rewriter) ⇒ Object



25
26
27
28
29
30
# File 'lib/transpec/converter.rb', line 25

def process(ast, source_rewriter)
  return unless ast
  ast.each_node do |node|
    dispatch_node(node, source_rewriter)
  end
end

#process_allow(allow) ⇒ Object



89
90
91
# File 'lib/transpec/converter.rb', line 89

def process_allow(allow)
  process_messaging_host(allow.receive_matcher)
end

#process_any_instance_block(messaging_host) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/transpec/converter.rb', line 212

def process_any_instance_block(messaging_host)
  return unless messaging_host
  return unless rspec_version.rspec_2_99?
  return unless configuration.convert_deprecated_method?
  return unless configuration.add_receiver_arg_to_any_instance_implementation_block?
  messaging_host.add_receiver_arg_to_any_instance_implementation_block!
end

#process_be_boolean(be_boolean) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/transpec/converter.rb', line 133

def process_be_boolean(be_boolean)
  return unless rspec_version.be_truthy_available?
  return unless configuration.convert_deprecated_method?

  case configuration.boolean_matcher_type
  when :conditional
    be_boolean.convert_to_conditional_matcher!(configuration.form_of_be_falsey)
  when :exact
    be_boolean.convert_to_exact_matcher!
  end
end

#process_be_close(be_close) ⇒ Object



145
146
147
# File 'lib/transpec/converter.rb', line 145

def process_be_close(be_close)
  be_close.convert_to_be_within! if configuration.convert_deprecated_method?
end

#process_current_example(current_example) ⇒ Object



170
171
172
173
# File 'lib/transpec/converter.rb', line 170

def process_current_example(current_example)
  return unless rspec_version.yielded_example_available?
  current_example.convert! if configuration.convert_deprecated_method?
end

#process_double(double) ⇒ Object



111
112
113
# File 'lib/transpec/converter.rb', line 111

def process_double(double)
  double.convert_to_double! if configuration.convert_deprecated_method?
end

#process_example(example) ⇒ Object



160
161
162
163
# File 'lib/transpec/converter.rb', line 160

def process_example(example)
  return if !rspec_version.rspec_2_99? || !configuration.convert_pending?
  example.convert_pending_to_skip!
end

#process_expect(expect) ⇒ Object



83
84
85
86
87
# File 'lib/transpec/converter.rb', line 83

def process_expect(expect)
  process_have(expect.have_matcher)
  process_raise_error(expect.raise_error_matcher)
  process_messaging_host(expect.receive_matcher)
end

#process_have(have) ⇒ Object



196
197
198
199
# File 'lib/transpec/converter.rb', line 196

def process_have(have)
  return if !have || !configuration.convert_have_items?
  have.convert_to_standard_expectation!(configuration.parenthesize_matcher_arg)
end

#process_its(its) ⇒ Object



156
157
158
# File 'lib/transpec/converter.rb', line 156

def process_its(its)
  its.convert_to_describe_subject_it! if configuration.convert_its?
end

#process_matcher_definition(matcher_definition) ⇒ Object



175
176
177
178
# File 'lib/transpec/converter.rb', line 175

def process_matcher_definition(matcher_definition)
  return unless rspec_version.non_should_matcher_protocol_available?
  matcher_definition.convert_deprecated_method! if configuration.convert_deprecated_method?
end

#process_messaging_host(messaging_host) ⇒ Object



201
202
203
204
# File 'lib/transpec/converter.rb', line 201

def process_messaging_host(messaging_host)
  process_useless_and_return(messaging_host)
  process_any_instance_block(messaging_host)
end

#process_method_stub(method_stub) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/transpec/converter.rb', line 115

def process_method_stub(method_stub)
  if configuration.convert_stub?
    if !method_stub.hash_arg? ||
       rspec_version.receive_messages_available? ||
       configuration.convert_stub_with_hash_to_stub_and_return?
      method_stub.allowize!(rspec_version)
    elsif configuration.convert_deprecated_method?
      method_stub.convert_deprecated_method!
    end
  elsif configuration.convert_deprecated_method?
    method_stub.convert_deprecated_method!
  end

  method_stub.remove_no_message_allowance! if configuration.convert_deprecated_method?

  process_messaging_host(method_stub)
end

#process_oneliner_should(oneliner_should) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/transpec/converter.rb', line 60

def process_oneliner_should(oneliner_should)
  negative_form = configuration.negative_form_of_to
  parenthesize = configuration.parenthesize_matcher_arg?

  # TODO: Referencing oneliner_should.have_matcher.project_requires_collection_matcher?
  #   from this converter is considered bad design.
  should_convert_have_items = configuration.convert_have_items? &&
                              oneliner_should.have_matcher &&
                              !oneliner_should.have_matcher.project_requires_collection_matcher?

  if should_convert_have_items
    if configuration.convert_should?
      oneliner_should.convert_have_items_to_standard_expect!(negative_form, parenthesize)
    else
      oneliner_should.convert_have_items_to_standard_should!
    end
  elsif configuration.convert_oneliner? && rspec_version.oneliner_is_expected_available?
    oneliner_should.expectize!(negative_form, parenthesize)
  end

  process_raise_error(oneliner_should.raise_error_matcher)
end

#process_pending(pending) ⇒ Object



165
166
167
168
# File 'lib/transpec/converter.rb', line 165

def process_pending(pending)
  return if !rspec_version.rspec_2_99? || !configuration.convert_pending?
  pending.convert_deprecated_syntax!
end

#process_raise_error(raise_error) ⇒ Object



149
150
151
152
153
154
# File 'lib/transpec/converter.rb', line 149

def process_raise_error(raise_error)
  return unless raise_error
  if configuration.convert_deprecated_method?
    raise_error.remove_error_specification_with_negative_expectation!
  end
end

#process_rspec_configure(rspec_configure) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/transpec/converter.rb', line 180

def process_rspec_configure(rspec_configure)
  if need_to_modify_expectation_syntax_configuration?(rspec_configure)
    rspec_configure.expectations.syntaxes = :expect
  end

  if need_to_modify_mock_syntax_configuration?(rspec_configure)
    rspec_configure.mocks.syntaxes = :expect
  end

  if rspec_version.rspec_2_99? &&
       configuration.convert_deprecated_method?
    should_yield = configuration.add_receiver_arg_to_any_instance_implementation_block?
    rspec_configure.mocks.yield_receiver_to_any_instance_implementation_blocks = should_yield
  end
end

#process_should(should) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/transpec/converter.rb', line 48

def process_should(should)
  if configuration.convert_should?
    should.expectize!(
      configuration.negative_form_of_to,
      configuration.parenthesize_matcher_arg?
    )
  end

  process_have(should.have_matcher)
  process_raise_error(should.raise_error_matcher)
end

#process_should_receive(should_receive) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/transpec/converter.rb', line 93

def process_should_receive(should_receive)
  if should_receive.useless_expectation?
    if configuration.convert_deprecated_method?
      if configuration.convert_stub?
        should_receive.allowize_useless_expectation!(configuration.negative_form_of_to)
      else
        should_receive.stubize_useless_expectation!
      end
    elsif configuration.convert_should_receive?
      should_receive.expectize!(configuration.negative_form_of_to)
    end
  elsif configuration.convert_should_receive?
    should_receive.expectize!(configuration.negative_form_of_to)
  end

  process_messaging_host(should_receive)
end

#process_useless_and_return(messaging_host) ⇒ Object



206
207
208
209
210
# File 'lib/transpec/converter.rb', line 206

def process_useless_and_return(messaging_host)
  return unless messaging_host
  return unless configuration.convert_deprecated_method?
  messaging_host.remove_useless_and_return!
end