Class: Transpec::Converter

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

Overview

rubocop:disable ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Syntax::Dispatcher

#dispatch_node, #dispatch_syntax, #handler_names, #invoke_handler

Methods inherited from BaseRewriter

#rewrite, #rewrite_file!, #rewrite_source

Constructor Details

#initialize(spec_suite = nil, config = nil, rspec_version = nil) ⇒ Converter

Returns a new instance of Converter.



22
23
24
25
26
27
# File 'lib/transpec/converter.rb', line 22

def initialize(spec_suite = nil, config = nil, rspec_version = nil)
  @spec_suite = spec_suite || SpecSuite.new
  @config = config || Config.new
  @rspec_version = rspec_version || Transpec.required_rspec_version
  @report = Report.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/transpec/converter.rb', line 16

def config
  @config
end

#reportObject (readonly)

Returns the value of attribute report.



16
17
18
# File 'lib/transpec/converter.rb', line 16

def report
  @report
end

#rspec_versionObject (readonly)

Returns the value of attribute rspec_version.



16
17
18
# File 'lib/transpec/converter.rb', line 16

def rspec_version
  @rspec_version
end

#spec_suiteObject (readonly)

Returns the value of attribute spec_suite.



16
17
18
# File 'lib/transpec/converter.rb', line 16

def spec_suite
  @spec_suite
end

Instance Method Details

#need_to_modify_yield_receiver_to_any_instance_implementation_blocks_config?Boolean

Returns:

  • (Boolean)


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

def need_to_modify_yield_receiver_to_any_instance_implementation_blocks_config?
  rspec_version.rspec_2_99? && config.convert_deprecated_method? &&
    spec_suite.need_to_modify_yield_receiver_to_any_instance_implementation_blocks_config?
end

#process(ast, source_rewriter) ⇒ Object



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

def process(ast, source_rewriter)
  return unless ast

  ast.each_node do |node|
    begin
      dispatch_node(node, source_rewriter, runtime_data, report)
    rescue ConversionError => error
      report.conversion_errors << error
    end
  end
end

#process_any_instance_block(messaging_host) ⇒ Object



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

def process_any_instance_block(messaging_host)
  return unless messaging_host
  return unless rspec_version.rspec_2_99?
  return unless config.convert_deprecated_method?
  return unless config.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



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/transpec/converter.rb', line 109

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

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

#process_be_close(be_close) ⇒ Object



121
122
123
# File 'lib/transpec/converter.rb', line 121

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

#process_current_example(current_example) ⇒ Object



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

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

#process_double(double) ⇒ Object



82
83
84
# File 'lib/transpec/converter.rb', line 82

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

#process_example(example) ⇒ Object



136
137
138
139
# File 'lib/transpec/converter.rb', line 136

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

#process_example_group(example_group) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/transpec/converter.rb', line 156

def process_example_group(example_group)
  if rspec_version.non_monkey_patch_example_group_available? && config.convert_example_group?
    example_group.convert_to_non_monkey_patch!
  end

  if rspec_version.implicit_spec_type_disablement_available? &&
     config.
    example_group.add_explicit_type_metadata!
  end
end

#process_have(have) ⇒ Object



190
191
192
193
# File 'lib/transpec/converter.rb', line 190

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

#process_hook(hook) ⇒ Object



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

def process_hook(hook)
  return if !config.convert_hook_scope? || !rspec_version.hook_scope_alias_available?
  hook.convert_scope_name!
end

#process_its(its) ⇒ Object



132
133
134
# File 'lib/transpec/converter.rb', line 132

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

#process_matcher_definition(matcher_definition) ⇒ Object



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

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

#process_method_stub(method_stub) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/transpec/converter.rb', line 86

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

  method_stub.remove_no_message_allowance! if config.convert_deprecated_method?
end

#process_oneliner_should(oneliner_should) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/transpec/converter.rb', line 50

def process_oneliner_should(oneliner_should)
  negative_form = config.negative_form_of_to
  should_convert_have_items = config.convert_have_items? &&
                              oneliner_should.have_matcher.conversion_target?

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

#process_operator(operator) ⇒ Object



102
103
104
105
106
107
# File 'lib/transpec/converter.rb', line 102

def process_operator(operator)
  return unless config.convert_should?
  return if operator.expectation.is_a?(Syntax::OnelinerShould) &&
    !rspec_version.oneliner_is_expected_available?
  operator.convert_operator!(config.parenthesize_matcher_arg?)
end

#process_pending(pending) ⇒ Object



141
142
143
144
# File 'lib/transpec/converter.rb', line 141

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

#process_raise_error(raise_error) ⇒ Object



125
126
127
128
129
130
# File 'lib/transpec/converter.rb', line 125

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

#process_rspec_configure(rspec_configure) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/transpec/converter.rb', line 167

def process_rspec_configure(rspec_configure)
  if config.convert_deprecated_method?
    rspec_configure.convert_deprecated_options!(rspec_version)
  end

  if spec_suite.main_rspec_configure_node?(rspec_configure.node)
    if rspec_version.non_monkey_patch_example_group_available? &&
       config.convert_example_group?
      rspec_configure.expose_dsl_globally = false
    end

    if need_to_modify_yield_receiver_to_any_instance_implementation_blocks_config?
      should_yield = config.add_receiver_arg_to_any_instance_implementation_block?
      rspec_configure.mocks.yield_receiver_to_any_instance_implementation_blocks = should_yield
    end

    if rspec_version.implicit_spec_type_disablement_available? &&
       !config.
      rspec_configure.infer_spec_type_from_file_location!
    end
  end
end

#process_should(should) ⇒ Object



45
46
47
48
# File 'lib/transpec/converter.rb', line 45

def process_should(should)
  return unless config.convert_should?
  should.expectize!(config.negative_form_of_to)
end

#process_should_receive(should_receive) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/transpec/converter.rb', line 66

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

#process_useless_and_return(messaging_host) ⇒ Object



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

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

#runtime_dataObject



29
30
31
# File 'lib/transpec/converter.rb', line 29

def runtime_data
  spec_suite.runtime_data
end