Class: Minitest::Heat::Source
- Inherits:
-
Object
- Object
- Minitest::Heat::Source
- 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
-
#context ⇒ Object
Returns the value of attribute context.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#line_number ⇒ Object
Returns the value of attribute line_number.
-
#max_line_count ⇒ Object
Returns the value of attribute max_line_count.
Instance Method Summary collapse
-
#file_lines ⇒ type
Reads (and chomps) the lines of the target file.
-
#initialize(filename, line_number:, max_line_count: 1, context: :around) ⇒ Source
constructor
A new instance of Source.
-
#line ⇒ String
Looks up the line of code referenced.
-
#line_numbers ⇒ Array<Integer>
Line numbers for the returned lines.
-
#lines ⇒ Array<String>
Looks up the available lines of code around the referenced line number.
-
#to_h ⇒ Hash
Returns relevant lines as a hash with line numbers as the keys.
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
#context ⇒ Object
Returns the value of attribute context.
9 10 11 |
# File 'lib/minitest/heat/source.rb', line 9 def context @context end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
7 8 9 |
# File 'lib/minitest/heat/source.rb', line 7 def filename @filename end |
#line_number ⇒ Object
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_count ⇒ Object
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_lines ⇒ type
Reads (and chomps) the lines of the target file
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 |
#line ⇒ String
Looks up the line of code referenced
30 31 32 |
# File 'lib/minitest/heat/source.rb', line 30 def line file_lines[line_number - 1] end |
#line_numbers ⇒ Array<Integer>
Line numbers for the returned lines
46 47 48 |
# File 'lib/minitest/heat/source.rb', line 46 def line_numbers (first_line_number..last_line_number).to_a.uniq end |
#lines ⇒ Array<String>
Looks up the available lines of code around the referenced line number
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_h ⇒ Hash
Returns relevant lines as a hash with line numbers as the 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 |