class Transpec::Syntax::CurrentExample

Constants

METHODS_YIELD_EXAMPLE

Public Instance Methods

block_method_name() click to toggle source
# File lib/transpec/syntax/current_example.rb, line 43
def block_method_name
  method_name_of_block_node(block_node)
end
block_node() click to toggle source
# File lib/transpec/syntax/current_example.rb, line 34
def block_node
  return @block_node if instance_variable_defined?(:@block_node)

  @block_node = node.each_ancestor(:block).find do |block_node|
    method_name = method_name_of_block_node(block_node)
    METHODS_YIELD_EXAMPLE.include?(method_name)
  end
end
convert!() click to toggle source
# File lib/transpec/syntax/current_example.rb, line 22
def convert!
  if block_node
    insert_after(block_node.loc.begin, " |#{block_arg_name}|") unless block_has_argument?
    replace(selector_range, block_arg_name.to_s) unless method_name == block_arg_name
    block_node.metadata[:added_example_block_arg] = true
  else
    replace(selector_range, 'RSpec.current_example')
  end

  add_record(RecordBuilder.build(self))
end
dynamic_analysis_target?() click to toggle source
# File lib/transpec/syntax/current_example.rb, line 15
def dynamic_analysis_target?
  super &&
    receiver_node.nil? &&
    [:example, :running_example].include?(method_name) &&
    block_node_taken_by_method(node).nil?
end

Private Instance Methods

block_arg_name() click to toggle source
# File lib/transpec/syntax/current_example.rb, line 58
def block_arg_name
  if block_arg_node
    block_arg_node.children.first
  else
    :example
  end
end
block_arg_node() click to toggle source
# File lib/transpec/syntax/current_example.rb, line 53
def block_arg_node
  args_node = block_node.children[1]
  args_node.children.first
end
block_has_argument?() click to toggle source
# File lib/transpec/syntax/current_example.rb, line 49
def block_has_argument?
  block_arg_node || block_node.metadata[:added_example_block_arg]
end
method_name_of_block_node(block_node) click to toggle source
# File lib/transpec/syntax/current_example.rb, line 66
def method_name_of_block_node(block_node)
  send_node = block_node.children.first
  send_node.children[1]
end