class Transpec::Syntax::MethodStub

Constants

CLASSES_DEFINING_OWN_STUB_METHOD

Public Instance Methods

add_receiver_arg_to_any_instance_implementation_block!() click to toggle source
Calls superclass method
# File lib/transpec/syntax/method_stub.rb, line 93
def add_receiver_arg_to_any_instance_implementation_block!
  return unless super
  add_record(Mixin::AnyInstanceBlock::MonkeyPatchRecordBuilder.build(self))
end
allow_to_receive_available?() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 49
def allow_to_receive_available?
  syntax_available?(__method__)
end
allowize!() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 61
def allowize!
  return if method_name == :stub_chain && !rspec_version.receive_message_chain_available?

  unless allow_to_receive_available?
    fail ContextError.new("##{method_name}", '#allow', selector_range)
  end

  source, type = replacement_source_and_conversion_type(rspec_version)
  return unless source

  replace(expression_range, source)

  add_record(AllowRecordBuilder.build(self, type))
end
conversion_target?() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 35
def conversion_target?
  return false unless dynamic_analysis_target?

  # Check if the method is RSpec's one or not.
  if runtime_data.run?(send_analysis_target_node)
    # If we have runtime data, check with it.
    defined_by_rspec?
  else
    # Otherwise check with a static whitelist.
    const_name = const_name(receiver_node)
    !CLASSES_DEFINING_OWN_STUB_METHOD.include?(const_name)
  end
end
convert_deprecated_method!() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 76
def convert_deprecated_method!
  return unless replacement_for_deprecated_method
  replace(selector_range, replacement_for_deprecated_method)
  add_record(DeprecatedMethodRecordBuilder.build(self))
end
dynamic_analysis_target?() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 28
def dynamic_analysis_target?
  super &&
    receiver_node &&
    [:stub, :stub!, :stub_chain, :unstub, :unstub!].include?(method_name) &&
    arg_node
end
hash_arg?() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 53
def hash_arg?
  arg_node.hash_type?
end
remove_no_message_allowance!() click to toggle source
Calls superclass method
# File lib/transpec/syntax/method_stub.rb, line 82
def remove_no_message_allowance!
  return unless allow_no_message?
  super
  add_record(NoMessageAllowanceRecordBuilder.build(self))
end
remove_useless_and_return!() click to toggle source
Calls superclass method
# File lib/transpec/syntax/method_stub.rb, line 88
def remove_useless_and_return!
  return unless super
  add_record(Mixin::UselessAndReturn::MonkeyPatchRecordBuilder.build(self))
end
replacement_for_deprecated_method() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 98
def replacement_for_deprecated_method
  case method_name
  when :stub!   then 'stub'
  when :unstub! then 'unstub'
  else nil
  end
end
unstub?() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 57
def unstub?
  [:unstub, :unstub!].include?(method_name)
end

Private Instance Methods

allow_source() click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 157
def allow_source
  if any_instance?
    "allow_any_instance_of(#{any_instance_target_class_source})"
  else
    "allow(#{subject_range.source})"
  end
end
build_allow_to(method) click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 149
def build_allow_to(method)
  expression =  allow_source
  expression << range_in_between_receiver_and_selector.source
  expression << "to #{method}"
  expression << parentheses_range.source
  expression
end
build_allow_to_receive(message_node, value_node = nil, keep_form_around_arg = true) click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 137
def build_allow_to_receive(message_node, value_node = nil, keep_form_around_arg = true)
  expression =  allow_source
  expression << range_in_between_receiver_and_selector.source
  expression << 'to receive'
  expression << (keep_form_around_arg ? range_in_between_selector_and_arg.source : '(')
  expression << message_source(message_node)
  expression << (keep_form_around_arg ? range_after_arg.source : ')')
  expression << ".and_return(#{value_node.loc.expression.source})" if value_node
  expression << '.and_call_original' if unstub?
  expression
end
build_multiple_allow_to_receive_with_hash(hash_node) click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 124
def build_multiple_allow_to_receive_with_hash(hash_node)
  expressions = []

  hash_node.children.each_with_index do |pair_node, index|
    key_node, value_node = *pair_node
    expression = build_allow_to_receive(key_node, value_node, false)
    expression.prepend(indentation_of_line(node)) if index > 0
    expressions << expression
  end

  expressions.join($RS)
end
message_source(node) click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 165
def message_source(node)
  message_source = node.loc.expression.source
  message_source.prepend(':') if node.sym_type? && !message_source.start_with?(':')
  message_source
end
replacement_source_and_conversion_type(rspec_version) click to toggle source
# File lib/transpec/syntax/method_stub.rb, line 108
def replacement_source_and_conversion_type(rspec_version)
  if method_name == :stub_chain
    [build_allow_to(:receive_message_chain), :allow_to_receive_message_chain]
  else
    if hash_arg?
      if rspec_version.receive_messages_available?
        [build_allow_to(:receive_messages), :allow_to_receive_messages]
      else
        [build_multiple_allow_to_receive_with_hash(arg_node), :allow_to_receive]
      end
    else
      [build_allow_to_receive(arg_node, nil, !unstub?), :allow_to_receive]
    end
  end
end