Class: Problem

Inherits:
Object
  • Object
show all
Defined in:
lib/specify_html_reporter/problem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example, file_path) ⇒ Problem

Returns a new instance of Problem.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/specify_html_reporter/problem.rb', line 7

def initialize(example, file_path)
  @example = example
  @file_path = file_path
  @exception = @example.exception

  return if @exception.nil?

  @problem = @exception.class
  @message = @exception.message.encode("utf-8")
  @backtrace_message = format_backtrace
  @highlighted_source = format_source
  @explanation = format_message
end

Instance Attribute Details

#backtrace_messageObject (readonly)

Returns the value of attribute backtrace_message.



5
6
7
# File 'lib/specify_html_reporter/problem.rb', line 5

def backtrace_message
  @backtrace_message
end

#explanationObject (readonly)

Returns the value of attribute explanation.



5
6
7
# File 'lib/specify_html_reporter/problem.rb', line 5

def explanation
  @explanation
end

#highlighted_sourceObject (readonly)

Returns the value of attribute highlighted_source.



5
6
7
# File 'lib/specify_html_reporter/problem.rb', line 5

def highlighted_source
  @highlighted_source
end

#problemObject (readonly)

Returns the value of attribute problem.



5
6
7
# File 'lib/specify_html_reporter/problem.rb', line 5

def problem
  @problem
end

Instance Method Details

#format_backtraceObject



27
28
29
30
31
# File 'lib/specify_html_reporter/problem.rb', line 27

def format_backtrace
  formatted_backtrace(@example, @exception).map do |entry|
    ERB::Util.html_escape(entry)
  end
end

#format_messageObject



21
22
23
24
25
# File 'lib/specify_html_reporter/problem.rb', line 21

def format_message
  formatter = Rouge::Formatters::HTML.new(css_class: 'highlight')
  lexer = Rouge::Lexers::Ruby.new
  formatter.format(lexer.lex(@message))
end

#format_sourceObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/specify_html_reporter/problem.rb', line 42

def format_source
  return '' if @backtrace_message.empty?

  data = @backtrace_message.first.split(':')

  return if data.empty?

  if os == :windows
    file_path = data[0] + ':' + data[1]
    line_number = data[2].to_i
  else
    file_path = data.first
    line_number = data[1].to_i
  end

  format_source_context(file_path, line_number)
end

#format_source_context(file_path, line_number) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/specify_html_reporter/problem.rb', line 60

def format_source_context(file_path, line_number)
  lines = File.readlines(file_path)
  start_line = line_number - 2
  end_line = line_number + 3

  source = lines[start_line..end_line].join("")

  formatter = Rouge::Formatters::HTML.new(
    css_class: 'highlight', line_numbers: true, start_line: start_line + 1
  )

  lexer = Rouge::Lexers::Ruby.new
  formatter.format(lexer.lex(source.encode('utf-8')))
end

#formatted_backtrace(example, exception) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/specify_html_reporter/problem.rb', line 33

def formatted_backtrace(example, exception)
  # This logic is in place to avoid an error in the format_backtrace method
  # for the RSpec formatter. RSpec versions below 3.5 will throw an exception.
  return [] unless example

  formatter = RSpec.configuration.backtrace_formatter
  formatter.format_backtrace(exception.backtrace, example.)
end

#osObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/specify_html_reporter/problem.rb', line 75

def os
  @os ||= begin
    host_os = RbConfig::CONFIG['host_os']

    case host_os
      when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
        :windows
      when /darwin|mac os/
        :macosx
      when /linux/
        :linux
      when /solaris|bsd/
        :unix
      else
        raise Exception, "Unknown operating system: #{host_os.inspect}"
    end
  end
end