class Transpec::Syntax::ShouldReceive

Public Instance Methods

add_receiver_arg_to_any_instance_implementation_block!() click to toggle source
Calls superclass method
# File lib/transpec/syntax/should_receive.rb, line 85
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/should_receive.rb, line 40
def allow_to_receive_available?
  syntax_available?(__method__)
end
allowize_useless_expectation!(negative_form = 'not_to') click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 58
def allowize_useless_expectation!(negative_form = 'not_to')
  return unless useless_expectation?

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

  convert_to_syntax!('allow', negative_form)
  remove_no_message_allowance!

  add_record(AllowRecordBuilder.build(self, negative_form))
end
dynamic_analysis_target?() click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 30
def dynamic_analysis_target?
  super &&
    receiver_node &&
    [:should_receive, :should_not_receive].include?(method_name)
end
expect_to_receive_available?() click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 36
def expect_to_receive_available?
  syntax_available?(__method__)
end
expectize!(negative_form = 'not_to') click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 48
def expectize!(negative_form = 'not_to')
  unless expect_to_receive_available?
    fail ContextError.new("##{method_name}", '#expect', selector_range)
  end

  convert_to_syntax!('expect', negative_form)

  add_record(ExpectRecordBuilder.build(self, negative_form))
end
positive?() click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 44
def positive?
  method_name == :should_receive
end
remove_useless_and_return!() click to toggle source
Calls superclass method
# File lib/transpec/syntax/should_receive.rb, line 80
def remove_useless_and_return!
  return unless super
  add_record(Mixin::UselessAndReturn::MonkeyPatchRecordBuilder.build(self))
end
stubize_useless_expectation!() click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 71
def stubize_useless_expectation!
  return unless useless_expectation?

  replace(selector_range, 'stub')
  remove_no_message_allowance!

  add_record(StubRecordBuilder.build(self))
end

Private Instance Methods

block_node_followed_by_fluent_method() click to toggle source

subject.should_receive(:method_name) do |block_arg| end.once

(send

(block
  (send
    (send nil :subject) :should_receive
    (sym :method_name))
  (args
    (arg :block_arg)) nil) :once)
# File lib/transpec/syntax/should_receive.rb, line 157
def block_node_followed_by_fluent_method
  each_backward_chained_node(node, :child_as_second_arg) do |chained_node, child_node|
    next unless chained_node.send_type?
    return child_node if child_node.block_type?
  end
end
block_node_taken_by_with_method_with_no_normal_args() click to toggle source

subject.should_receive(:method_name).once.with do |block_arg| end

(block

(send
  (send
    (send
      (send nil :subject) :should_receive
      (sym :method_name)) :once) :with)
(args
  (arg :block_arg)) nil)
# File lib/transpec/syntax/should_receive.rb, line 138
def block_node_taken_by_with_method_with_no_normal_args
  each_backward_chained_node(node, :child_as_second_arg) do |chained_node, child_node|
    next unless chained_node.block_type?
    return nil unless child_node.children[1] == :with
    return nil if child_node.children[2]
    return chained_node
  end
end
broken_block_nodes() click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 120
def broken_block_nodes
  @broken_block_nodes ||= [
    block_node_taken_by_with_method_with_no_normal_args,
    block_node_followed_by_fluent_method
  ].compact.uniq
end
convert_to_syntax!(syntax, negative_form) click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 92
def convert_to_syntax!(syntax, negative_form)
  if any_instance?
    wrap_subject_with_any_instance_of!(syntax)
  else
    wrap_subject_with_method!(syntax)
  end

  replace(selector_range, "#{positive? ? 'to' : negative_form} receive")

  correct_block_style!
end
correct_block_style!() click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 104
def correct_block_style!
  return if broken_block_nodes.empty?

  broken_block_nodes.each do |block_node|
    map = block_node.loc
    next if map.begin.source == '{'
    replace(map.begin, '{')
    replace(map.end, '}')
  end
end
wrap_subject_with_any_instance_of!(syntax) click to toggle source
# File lib/transpec/syntax/should_receive.rb, line 115
def wrap_subject_with_any_instance_of!(syntax)
  expression = "#{syntax}_any_instance_of(#{any_instance_target_class_source})"
  replace(subject_range, expression)
end