Class: Minitest::Heat::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/heat/source.rb

Overview

Gets the most relevant lines of code surrounding the specified line of code

Constant Summary collapse

CONTEXTS =
%i[before around after].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, line_number:, max_line_count: 1, context: :around) ⇒ Source

Returns a new instance of Source.



13
14
15
16
17
18
# File 'lib/minitest/heat/source.rb', line 13

def initialize(filename, line_number:, max_line_count: 1, context: :around)
  @filename = filename
  @line_number = Integer(line_number)
  @max_line_count = max_line_count
  @context = context
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



9
10
11
# File 'lib/minitest/heat/source.rb', line 9

def context
  @context
end

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/minitest/heat/source.rb', line 7

def filename
  @filename
end

#line_numberObject

Returns the value of attribute line_number.



9
10
11
# File 'lib/minitest/heat/source.rb', line 9

def line_number
  @line_number
end

#max_line_countObject

Returns the value of attribute max_line_count.



9
10
11
# File 'lib/minitest/heat/source.rb', line 9

def max_line_count
  @max_line_count
end

Instance Method Details

#file_linestype

Reads (and chomps) the lines of the target file

Returns:

  • (type)
    description


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/minitest/heat/source.rb', line 53

def file_lines
  @raw_lines ||= File.readlines(filename, chomp: true)
  @raw_lines.pop while @raw_lines.last.strip.empty?

  @raw_lines
rescue Errno::ENOENT
  # Occasionally, for a variety of reasons, a file can't be read. In those cases, it's best to
  # return no source code lines rather than have the test suite raise an error unrelated to
  # the code being tested becaues that gets confusing.
  []
end

#lineString

Looks up the line of code referenced

Returns:

  • (String)

    the line of code at filename:line_number



30
31
32
# File 'lib/minitest/heat/source.rb', line 30

def line
  file_lines[line_number - 1]
end

#line_numbersArray<Integer>

Line numbers for the returned lines

Returns:

  • (Array<Integer>)

    the line numbers corresponding to the lines returned



46
47
48
# File 'lib/minitest/heat/source.rb', line 46

def line_numbers
  (first_line_number..last_line_number).to_a.uniq
end

#linesArray<String>

Looks up the available lines of code around the referenced line number

Returns:

  • (Array<String>)

    the range of lines of code around



37
38
39
40
41
# File 'lib/minitest/heat/source.rb', line 37

def lines
  return [line].compact if max_line_count == 1

  file_lines[(line_numbers.first - 1)..(line_numbers.last - 1)]
end

#to_hHash

Returns relevant lines as a hash with line numbers as the keys

Returns:

  • (Hash)

    hash of relevant lines with line numbers as keys



23
24
25
# File 'lib/minitest/heat/source.rb', line 23

def to_h
  line_numbers.map(&:to_s).zip(lines).to_h
end