Class: SeeingIsBelieving::Binary::AnnotateEveryLine

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/binary/annotate_every_line.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, results, options = {}) ⇒ AnnotateEveryLine

Returns a new instance of AnnotateEveryLine.



8
9
10
11
12
# File 'lib/seeing_is_believing/binary/annotate_every_line.rb', line 8

def initialize(body, results, options={})
  @options = options
  @body    = body
  @results = results
end

Class Method Details

.call(body, results, options) ⇒ Object



4
5
6
# File 'lib/seeing_is_believing/binary/annotate_every_line.rb', line 4

def self.call(body, results, options)
  new(body, results, options).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/seeing_is_believing/binary/annotate_every_line.rb', line 14

def call
  @new_body ||= begin
    require 'seeing_is_believing/binary/comment_lines'
    require 'seeing_is_believing/binary/format_comment'
    exception_text = @options[:markers][:exception][:prefix]
    value_text     = @options[:markers][:value][:prefix]

    alignment_strategy = @options[:alignment_strategy].new(@body)
    exception_lineno   = @results.has_exception? ? @results.exception.line_number : -1
    new_body = CommentLines.call @body do |line, line_number|
      options = @options.merge pad_to: alignment_strategy.line_length_for(line_number)
      if exception_lineno == line_number
        result = sprintf "%s: %s", @results.exception.class_name, @results.exception.message.gsub("\n", '\n')
        FormatComment.call(line.size, exception_text, result, options)
      elsif @results[line_number].any?
        result  = @results[line_number].map { |result| result.gsub "\n", '\n' }.join(', ')
        FormatComment.call(line.size, value_text, result, options)
      else
        ''
      end
    end

    require 'seeing_is_believing/binary/annotate_end_of_file'
    AnnotateEndOfFile.add_stdout_stderr_and_exceptions_to new_body, @results, @options

    new_body
  end
end