class RSpec::Expectations::BlockSnippetExtractor
@private
Constants
- AmbiguousTargetError
- BlockLocator
-
@private Locates target block with node information (semantics), which tokens don’t have.
- BlockTokenExtractor
-
@private Performs extraction of block body snippet using tokens, which cannot be done with node information.
- Error
- TargetNotFoundError
Attributes
rubocop should properly handle ‘Struct.new {}` as an inner class definition.
rubocop should properly handle ‘Struct.new {}` as an inner class definition.
Public Class Methods
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 17 def initialize(proc, method_name) @proc = proc @method_name = method_name.to_s.freeze end
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 9 def self.try_extracting_single_line_body_of(proc, method_name) lines = new(proc, method_name).body_content_lines return nil unless lines.count == 1 lines.first rescue Error nil end
Public Instance Methods
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 25 def body_content_lines raw_body_lines.map(&:strip).reject(&:empty?) end
Ideally we should properly handle indentations of multiline snippet, but it’s not implemented yet since because we use result of this method only when it’s a single line and implementing the logic introduces additional complexity.
Private Instance Methods
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 62 def beginning_line_number source_location.last end
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 39 def block_token_extractor @block_token_extractor ||= BlockTokenExtractor.new(method_name, source, beginning_line_number) end
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 58 def file_path source_location.first end
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 31 def raw_body_lines raw_body_snippet.split("\n") end
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 35 def raw_body_snippet block_token_extractor.body_tokens.map(&:string).join end
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 44 def source raise TargetNotFoundError unless File.exist?(file_path) RSpec.world.source_from_file(file_path) end
Source
# File lib/rspec/expectations/block_snippet_extractor.rb, line 66 def source_location proc.source_location || raise(TargetNotFoundError) end