Class: Strut::ReportPrettyFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/strut/report_pretty_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ ReportPrettyFormatter

Returns a new instance of ReportPrettyFormatter.



8
9
10
# File 'lib/strut/report_pretty_formatter.rb', line 8

def initialize(lines)
  @lines = lines
end

Instance Method Details

#format(report) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/strut/report_pretty_formatter.rb', line 12

def format(report)
  begin
    out = StringIO.new
    $stdout = out
    print_report_to_stdout(report)
  ensure
    $stdout = STDOUT
  end
  out.string
end


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/strut/report_pretty_formatter.rb', line 48

def print_annotated_line(annotations, line)
  # TODO: print all annotations, not just the first
  annotation = annotations.first

  case annotation.type
  when ANNOTATION_EXCEPTION
    print_exception_line(line, annotation.message)
  when ANNOTATION_FAIL
    print_fail_line(line, annotation.message)
  else
    print_ok_line(line)
  end
end


74
75
76
77
# File 'lib/strut/report_pretty_formatter.rb', line 74

def print_error_line(line, line_fg, line_bg, message, message_fg, message_bg)
  print_line(line, line_fg, line_bg)
  print_line(message, message_fg, message_bg)
end


83
84
85
86
87
88
89
# File 'lib/strut/report_pretty_formatter.rb', line 83

def print_errors(report)
  return if report.errors.empty?
  report.errors.each do |error|
    print red { error }, "\n"
  end
  puts
end


62
63
64
# File 'lib/strut/report_pretty_formatter.rb', line 62

def print_exception_line(line, message)
  print_error_line(line, :black, :on_yellow, message, :yellow, :on_black)
end


66
67
68
# File 'lib/strut/report_pretty_formatter.rb', line 66

def print_fail_line(line, message)
  print_error_line(line, :white, :on_red, message, :red, :on_white)
end


79
80
81
# File 'lib/strut/report_pretty_formatter.rb', line 79

def print_line(line, foreground, background)
  puts line.send(foreground).send(background)
end


36
37
38
39
40
41
42
# File 'lib/strut/report_pretty_formatter.rb', line 36

def print_line_with_annotations(line, annotations)
  if annotations.empty?
    print_unannotated_line(line)
  elsif
    print_annotated_line(annotations, line)
  end
end


70
71
72
# File 'lib/strut/report_pretty_formatter.rb', line 70

def print_ok_line(line)
  print_line(line, :black, :on_green)
end


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/strut/report_pretty_formatter.rb', line 23

def print_report_to_stdout(report)
  @lines.each_line.each_with_index do |line, index|
    annotations = report.annotations_for_line(index+1)
    print_line_with_annotations(line.chomp, annotations)
  end
  puts
  print_errors(report)
  print "#{report.number_scenarios} scenarios ("
  print green { "#{report.number_passed} passed" }, ", "
  print red { "#{report.number_failed} failed" }, ", "
  print yellow { "#{report.number_skipped} skipped" }, ")"
end


44
45
46
# File 'lib/strut/report_pretty_formatter.rb', line 44

def print_unannotated_line(line)
  puts line
end