Class: Transpec::Syntax::MethodStub

Inherits:
Syntax
  • Object
show all
Includes:
AbleToAllowNoMessage, AbleToTargetAnyInstance, Util
Defined in:
lib/transpec/syntax/method_stub.rb

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
]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AbleToAllowNoMessage

#allow_no_message?, #remove_allowance_for_no_message!

Methods included from AbleToTargetAnyInstance

#any_instance?, #class_node_of_any_instance

Methods included from SendNodeSyntax

#arg_node, #arg_nodes, #arg_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 Util

const_name, here_document?, in_parentheses?, indentation_of_line, proc_literal?

Class Method Details

.target_method_names ⇒ Object



62
63
64
# File 'lib/transpec/syntax/method_stub.rb', line 62

def self.target_method_names
  [:stub, :unstub, :stub!, :unstub!]
end

.target_receiver_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.target_receiver_node?(node)
  return false if node.nil?
  const_name = Util.const_name(node)
  !CLASSES_DEFINING_OWN_STUB_METHOD.include?(const_name)
end

Instance Method Details

#allowize! ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/transpec/syntax/method_stub.rb', line 19

def allowize!
  # There's no way of unstubbing in #allow syntax.
  return unless [:stub, :stub!].include?(method_name)

  fail 'Already replaced deprecated method, cannot allowize.' if @replaced_deprecated_method

  unless context.in_example_group?
    fail InvalidContextError.new(selector_range, "##{method_name}", '#allow')
  end

  if arg_node.type == :hash
    expressions = build_allow_expressions_from_hash_node(arg_node)
    replace(expression_range, expressions)
  else
    expression = build_allow_expression(arg_node)
    replace(expression_range, expression)
  end

  register_record(:allow)

  @allowized = true
end

#replace_deprecated_method! ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/transpec/syntax/method_stub.rb', line 42

def replace_deprecated_method!
  return unless replacement_method_for_deprecated_method

  fail 'Already allowized, cannot replace deprecated method.' if @allowized

  replace(selector_range, replacement_method_for_deprecated_method)

  register_record(:deprecated)

  @replaced_deprecated_method = true
end