Class: Minitest::Heat::Output::SourceCode

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

Overview

Generates the tokens representing a specific set of source code lines

Constant Summary collapse

DEFAULT_LINE_COUNT =
3
DEFAULT_INDENTATION_SPACES =
2
HIGHLIGHT_KEY_LINE =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, line_number, max_line_count: DEFAULT_LINE_COUNT) ⇒ self

Provides a collection of tokens representing the output of source code

Parameters:

  • filename (String)

    the absolute path to the file containing the source code

  • line_number (Integer, String)

    the primary line number of interest for the file

  • max_line_count: (defaults to: DEFAULT_LINE_COUNT)

    DEFAULT_LINE_COUNT [Integer] maximum total number of lines to retrieve around the target line (including the target line)



21
22
23
24
25
26
# File 'lib/minitest/heat/output/source_code.rb', line 21

def initialize(filename, line_number, max_line_count: DEFAULT_LINE_COUNT)
  @filename = filename
  @line_number = line_number.to_s
  @max_line_count = max_line_count
  @tokens = []
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



12
13
14
# File 'lib/minitest/heat/output/source_code.rb', line 12

def filename
  @filename
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



12
13
14
# File 'lib/minitest/heat/output/source_code.rb', line 12

def line_number
  @line_number
end

#max_line_countObject (readonly)

Returns the value of attribute max_line_count.



12
13
14
# File 'lib/minitest/heat/output/source_code.rb', line 12

def max_line_count
  @max_line_count
end

Instance Method Details

#highlight_key_line?Boolean

Whether to visually highlight the target line when displaying the source code. Currently

defauls to true, but long-term, this is a likely candidate to be configurable. For
example, in the future, highlighting could only be used if the source includes more than
three lines. Or it could be something end users could disable in order to reduce noise.

Returns:

  • (Boolean)

    true if the target line should be highlighted



66
67
68
# File 'lib/minitest/heat/output/source_code.rb', line 66

def highlight_key_line?
  HIGHLIGHT_KEY_LINE
end

#indentationtype

The number of spaces each line of code should be indented. Currently defaults to 2 in

order to provide visual separation between test failures, but in the future, it could
be configurable in order to save horizontal space and create more compact output. For
example, it could be smart based on line length and total available horizontal terminal
space, or there could be higher-level "display" setting that could have a `:compact`
option that would reduce the space used.

Returns:

  • (type)
    description


78
79
80
# File 'lib/minitest/heat/output/source_code.rb', line 78

def indentation
  DEFAULT_INDENTATION_SPACES
end

#max_line_number_digitsInteger

The number of digits for the largest line number returned. This is used for formatting and

text justification so that line numbers are right-aligned

Returns:

  • (Integer)

    the number of digits in the longest line number returned



52
53
54
55
56
57
58
# File 'lib/minitest/heat/output/source_code.rb', line 52

def max_line_number_digits
  source
    .line_numbers
    .map(&:to_s)
    .map(&:length)
    .max
end

#tokensArray<Array<Token>>

The collection of style content tokens to print

Returns:

  • (Array<Array<Token>>)

    an array of arrays of tokens where each top-level array represents a line where the first element is the line_number and the second is the line of code to display



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/minitest/heat/output/source_code.rb', line 33

def tokens
  source.lines.each_index do |i|
    current_line_number  = source.line_numbers[i]
    current_line_of_code = source.lines[i]

    number_style, line_style = styles_for(current_line_of_code)

    @tokens << [
      line_number_token(number_style, current_line_number),
      line_of_code_token(line_style, current_line_of_code)
    ]
  end
  @tokens
end