Class: Transpec::Syntax::MethodStub

Inherits:
Transpec::Syntax show all
Includes:
Transpec::Syntax::Mixin::MessagingHost, Transpec::Syntax::Mixin::MonkeyPatchAnyInstance, Util
Defined in:
lib/transpec/syntax/method_stub.rb

Defined Under Namespace

Classes: AllowRecordBuilder, DeprecatedMethodRecordBuilder, NoMessageAllowanceRecordBuilder

Constant Summary collapse

CLASSES_DEFINING_OWN_STUB_METHOD =
[
  'Typhoeus', # https://github.com/typhoeus/typhoeus/blob/6a59c62/lib/typhoeus.rb#L66-L85
  'Excon',    # https://github.com/geemus/excon/blob/6af4f9c/lib/excon.rb#L143-L178
  'Factory'   # https://github.com/thoughtbot/factory_girl/blob/v3.6.2/lib/factory_girl/syntax/vintage.rb#L112
]

Constants included from Util

Util::LITERAL_TYPES, Util::WHITESPACES

Instance Attribute Summary

Attributes inherited from Transpec::Syntax

#node, #report, #runtime_data, #source_rewriter

Instance Method Summary collapse

Methods included from Transpec::Syntax::Mixin::MonkeyPatchAnyInstance

#any_instance?

Methods included from Transpec::Syntax::Mixin::MonkeyPatch

#register_syntax_availability_analysis_request, #subject_node, #subject_range, #syntax_available?

Methods included from Transpec::Syntax::Mixin::Send

#arg_node, #arg_nodes, #arg_range, #args_range, #method_name, #parentheses_range, #range_after_arg, #range_in_between_receiver_and_selector, #range_in_between_selector_and_arg, #receiver_node, #receiver_range, #selector_range

Methods included from Transpec::Syntax::Mixin::AnyInstanceBlock

#need_to_add_receiver_arg_to_any_instance_implementation_block?

Methods included from Transpec::Syntax::Mixin::NoMessageAllowance

#allow_no_message?, #any_number_of_times?, #at_least_zero?

Methods included from Transpec::Syntax::Mixin::UselessAndReturn

#and_return?, #and_return_node, #and_return_with_block?, #useless_and_return?

Methods included from Util

beginning_of_line_range, block_node_taken_by_method, chainable_source, const_name, contain_here_document?, each_backward_chained_node, each_forward_chained_node, each_line_range, expand_range_to_adjacent_whitespaces, find_consecutive_whitespace_position, first_block_arg_name, here_document?, in_explicit_parentheses?, indentation_of_line, line_range, literal?, proc_literal?, range_from_arg

Methods inherited from Transpec::Syntax

#add_record, #dependent_syntaxes, #expression_range, #initialize, #inspect, #parent_node, snake_case_name, standalone?, #static_context_inspector

Methods included from Collection

#all_syntaxes, #inherited, #mixins, #require_all, #standalone_syntaxes

Methods included from DynamicAnalysis

#register_dynamic_analysis_request

Constructor Details

This class inherits a constructor from Transpec::Syntax

Instance Method Details

#add_receiver_arg_to_any_instance_implementation_block!Object



92
93
94
95
# File 'lib/transpec/syntax/method_stub.rb', line 92

def add_receiver_arg_to_any_instance_implementation_block!
  return unless super
  add_record(Mixin::AnyInstanceBlock::MonkeyPatchRecordBuilder.build(self))
end

#allow_to_receive_available?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/transpec/syntax/method_stub.rb', line 48

def allow_to_receive_available?
  syntax_available?(__method__)
end

#allowize!(rspec_version) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/transpec/syntax/method_stub.rb', line 60

def allowize!(rspec_version)
  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?Boolean

Returns:

  • (Boolean)


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

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!Object



75
76
77
78
79
# File 'lib/transpec/syntax/method_stub.rb', line 75

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?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# 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)
end

#hash_arg?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/transpec/syntax/method_stub.rb', line 52

def hash_arg?
  arg_node.hash_type?
end

#remove_no_message_allowance!Object



81
82
83
84
85
# File 'lib/transpec/syntax/method_stub.rb', line 81

def remove_no_message_allowance!
  return unless allow_no_message?
  super
  add_record(NoMessageAllowanceRecordBuilder.build(self))
end

#remove_useless_and_return!Object



87
88
89
90
# File 'lib/transpec/syntax/method_stub.rb', line 87

def remove_useless_and_return!
  return unless super
  add_record(Mixin::UselessAndReturn::MonkeyPatchRecordBuilder.build(self))
end

#replacement_for_deprecated_methodObject



97
98
99
100
101
102
103
# File 'lib/transpec/syntax/method_stub.rb', line 97

def replacement_for_deprecated_method
  case method_name
  when :stub!   then 'stub'
  when :unstub! then 'unstub'
  else nil
  end
end

#unstub?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/transpec/syntax/method_stub.rb', line 56

def unstub?
  [:unstub, :unstub!].include?(method_name)
end