Class: RSpec::Core::Formatters::SnippetExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb

Constant Summary collapse

NoSuchFileError =
Class.new(StandardError)
NoSuchLineError =
Class.new(StandardError)
NoExpressionAtLineError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, beginning_line_number, max_line_count = nil) ⇒ SnippetExtractor

Returns a new instance of SnippetExtractor.



35
36
37
38
39
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 35

def initialize(source, beginning_line_number, max_line_count=nil)
  @source = source
  @beginning_line_number = beginning_line_number
  @max_line_count = max_line_count
end

Instance Attribute Details

#beginning_line_numberObject (readonly)

Returns the value of attribute beginning_line_number.



24
25
26
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 24

def beginning_line_number
  @beginning_line_number
end

#max_line_countObject (readonly)

Returns the value of attribute max_line_count.



24
25
26
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 24

def max_line_count
  @max_line_count
end

#sourceObject (readonly)

Returns the value of attribute source.



24
25
26
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 24

def source
  @source
end

Class Method Details

.extract_expression_lines_at(file_path, beginning_line_number) ⇒ Object

:nocov:



122
123
124
125
126
127
128
129
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 122

def self.extract_expression_lines_at(file_path, beginning_line_number, max_line_count=nil)
  if max_line_count == 1
    [extract_line_at(file_path, beginning_line_number)]
  else
    source = source_from_file(file_path)
    new(source, beginning_line_number, max_line_count).expression_lines
  end
end

.extract_line_at(file_path, line_number) ⇒ Object

Raises:



9
10
11
12
13
14
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 9

def self.extract_line_at(file_path, line_number)
  source = source_from_file(file_path)
  line = source.lines[line_number - 1]
  raise NoSuchLineError unless line
  line
end

.least_indentation_from(lines) ⇒ Object



128
129
130
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 128

def self.least_indentation_from(lines)
  lines.map { |line| line[/^[ \t]*/] }.min
end

.source_from_file(path) ⇒ Object

Raises:



16
17
18
19
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 16

def self.source_from_file(path)
  raise NoSuchFileError unless File.exist?(path)
  RSpec.world.source_from_file(path)
end

Instance Method Details

#expression_linesObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/formatters/snippet_extractor.rb', line 41

def expression_lines
  line_range = line_range_of_expression

  if max_line_count && line_range.count > max_line_count
    line_range = (line_range.begin)..(line_range.begin + max_line_count - 1)
  end

  source.lines[(line_range.begin - 1)..(line_range.end - 1)]
rescue SyntaxError, NoExpressionAtLineError
  [self.class.extract_line_at(source.path, beginning_line_number)]
end