Class: Transpec::Syntax::ShouldReceive

Inherits:
Transpec::Syntax show all
Includes:
Mixin::AllowNoMessage, Mixin::AnyInstance, Mixin::Expectizable, Mixin::MonkeyPatch, Mixin::Send
Defined in:
lib/transpec/syntax/should_receive.rb

Instance Attribute Summary

Attributes inherited from Transpec::Syntax

#ancestor_nodes, #node, #report, #runtime_data, #source_rewriter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::AnyInstance

#any_instance?, #class_node_of_any_instance

Methods included from Mixin::AllowNoMessage

#allow_no_message?, #remove_allowance_for_no_message!

Methods included from Mixin::Expectizable

#wrap_subject_in_expect!

Methods included from Mixin::MonkeyPatch

#check_syntax_availability, #register_request_of_syntax_availability_inspection, #subject_node, #subject_range

Methods included from Mixin::Send

#arg_node, #arg_nodes, #arg_range, included, #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 inherited from Transpec::Syntax

all_syntaxes, #expression_range, inherited, #initialize, #parent_node, register_request_for_dynamic_analysis, snake_case_name, standalone?, standalone_syntaxes, #static_context_inspector, target_node?

Constructor Details

This class inherits a constructor from Transpec::Syntax

Class Method Details

.target_method?(receiver_node, method_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/transpec/syntax/should_receive.rb', line 21

def self.target_method?(receiver_node, method_name)
  !receiver_node.nil? && [:should_receive, :should_not_receive].include?(method_name)
end

Instance Method Details

#allow_to_receive_available?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/transpec/syntax/should_receive.rb', line 43

def allow_to_receive_available?
  check_syntax_availability(__method__)
end

#allowize_useless_expectation!(negative_form = 'not_to') ⇒ Object



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

def allowize_useless_expectation!(negative_form = 'not_to')
  return unless useless_expectation?

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

  convert_to_syntax!('allow', negative_form)
  remove_allowance_for_no_message!

  register_record(:allow, negative_form)
end

#expect_to_receive_available?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/transpec/syntax/should_receive.rb', line 39

def expect_to_receive_available?
  check_syntax_availability(__method__)
end

#expectize!(negative_form = 'not_to') ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/transpec/syntax/should_receive.rb', line 51

def expectize!(negative_form = 'not_to')
  unless expect_to_receive_available?
    fail InvalidContextError.new(selector_range, "##{method_name}", '#expect')
  end

  convert_to_syntax!('expect', negative_form)
  register_record(:expect, negative_form)
end

#positive?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/transpec/syntax/should_receive.rb', line 47

def positive?
  method_name == :should_receive
end

#register_request_for_dynamic_analysis(rewriter) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/transpec/syntax/should_receive.rb', line 25

def register_request_for_dynamic_analysis(rewriter)
  register_request_of_syntax_availability_inspection(
    rewriter,
    :expect_to_receive_available?,
    [:expect, :receive]
  )

  register_request_of_syntax_availability_inspection(
    rewriter,
    :allow_to_receive_available?,
    [:allow, :receive]
  )
end

#stubize_useless_expectation!Object



73
74
75
76
77
78
79
80
# File 'lib/transpec/syntax/should_receive.rb', line 73

def stubize_useless_expectation!
  return unless useless_expectation?

  replace(selector_range, 'stub')
  remove_allowance_for_no_message!

  register_record(:stub)
end