Class: Transpec::Syntax::CurrentExample

Inherits:
Transpec::Syntax show all
Includes:
RSpecDSL, Mixin::Send, Util
Defined in:
lib/transpec/syntax/current_example.rb

Defined Under Namespace

Classes: RecordBuilder

Constant Summary collapse

METHODS_YIELD_EXAMPLE =
(EXAMPLE_METHODS + HOOK_METHODS + HELPER_METHODS).freeze

Constants included from RSpecDSL

RSpecDSL::EXAMPLE_GROUP_METHODS, RSpecDSL::EXAMPLE_METHODS, RSpecDSL::HELPER_METHODS, RSpecDSL::HOOK_METHODS

Constants included from Util

Util::LITERAL_TYPES, Util::WHITESPACES

Instance Attribute Summary

Attributes inherited from Transpec::Syntax

#node, #project, #report, #runtime_data, #source_rewriter

Instance Method Summary collapse

Methods included from 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 Mixin::Send::TargetDetection

#conversion_target?

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, #conversion_target?, #dependent_syntaxes, #expression_range, #initialize, #inspect, #parent_node, #rspec_version, 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

#block_method_nameObject



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

def block_method_name
  method_name_of_block_node(block_node)
end

#block_nodeObject



34
35
36
37
38
39
40
41
# 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!Object



22
23
24
25
26
27
28
29
30
31
32
# 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.[:added_example_block_arg] = true
  else
    replace(selector_range, 'RSpec.current_example')
  end

  add_record(RecordBuilder.build(self))
end

#dynamic_analysis_target?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# 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