Class: Riot::IOReporter

Inherits:
Reporter show all
Defined in:
lib/riot/reporter/io.rb

Overview

An IOReporter is one that expects to use an IO object to output results to. Thus, whatever is available by an instance of an IO object should be available to whatever is given to this reporter to use.

This is an abstract class. You should use some other or define your own sub-class that knows how to handle pass, fail, and error.

Direct Known Subclasses

DotMatrixReporter, StoryReporter

Instance Attribute Summary

Attributes inherited from Reporter

#current_context, #errors, #failures, #passes

Instance Method Summary collapse

Methods inherited from Reporter

#describe_context, #error, #fail, #new, #pass, #report, #success?, #summarize

Constructor Details

#initialize(writer = STDOUT) ⇒ IOReporter

Creates a new IOReporter. You can give it your own IO writer or it will default to STDOUT.

Parameters:

  • writer (IO) (defaults to: STDOUT)

    the writer to use for results



12
13
14
15
# File 'lib/riot/reporter/io.rb', line 12

def initialize(writer=STDOUT)
  super()
  @writer = writer
end

Instance Method Details

#filter_backtrace(backtrace, &line_handler) ⇒ Object (protected)

Filters Riot and Rake method calls from an exception backtrace.

Parameters:

  • backtrace (Array)

    an exception’s backtrace

  • &line_handler (lambda)

    called each time a good line is found



62
63
64
65
66
67
68
69
70
71
# File 'lib/riot/reporter/io.rb', line 62

def filter_backtrace(backtrace, &line_handler)
  cleansed, bad = [], true

  # goal is to filter all the riot stuff/rake before the first non riot thing
  backtrace.reverse_each do |bt|
    # make sure we are still in the bad part
    bad = (bt =~ /\/lib\/riot/ || bt =~ /rake_test_loader/) if bad
    yield bt unless bad
  end
end

#format_error(e) ⇒ String (protected)

Generates a message for assertions that error out. However, in the additional stacktrace, any mentions of Riot and Rake framework methods calls are removed. Makes for a more readable error response.

Parameters:

  • e (Exception)

    the exception to generate the backtrace from

Returns:

  • (String)

    the error response message



49
50
51
52
53
54
55
56
# File 'lib/riot/reporter/io.rb', line 49

def format_error(e)
  format = []
  format << "    #{e.class.name} occurred"
  format << "#{e.to_s}"
  filter_backtrace(e.backtrace) { |line| format << "      at #{line}" }

  format.join("\n")
end

#green(str) ⇒ Object (protected)



76
# File 'lib/riot/reporter/io.rb', line 76

def green(str);  with_color(32, str); end

#line_info(line, file) ⇒ String (protected)

Takes a line number, the file it corresponds to, and generates a formatted string for use in failure responses.

Parameters:

  • line (Number)

    the line number of the failure

  • file (String)

    the name of the file the failure was in

Returns:

  • (String)

    formatted failure line



40
41
42
# File 'lib/riot/reporter/io.rb', line 40

def line_info(line, file)
  line ? "(on line #{line} in #{file})" : ""
end

Helper that knows how to write output to the writer without a newline.

Parameters:

  • message (String)

    the message to be printed



32
# File 'lib/riot/reporter/io.rb', line 32

def print(message) @writer.print(message); end

#puts(message) ⇒ Object (protected)

Helper that knows how to write output to the writer with a newline.

Parameters:

  • message (String)

    the message to be printed



27
# File 'lib/riot/reporter/io.rb', line 27

def puts(message) @writer.puts(message); end

#red(str) ⇒ Object (protected)

Color output



74
# File 'lib/riot/reporter/io.rb', line 74

def red(str);    with_color(31, str); end

#results(time_taken) ⇒ Object

Called after all contexts have finished. This is where the final results can be output.

Parameters:

  • time_taken (Number)

    number of seconds taken to run everything



18
19
20
21
# File 'lib/riot/reporter/io.rb', line 18

def results(time_taken)
  values = [passes, failures, errors, ("%0.6f" % time_taken)]
  puts "\n%d passes, %d failures, %d errors in %s seconds" % values
end

#with_color(code, str) ⇒ Object (protected)



80
81
82
# File 'lib/riot/reporter/io.rb', line 80

def with_color(code,str)
  "\e[#{code}m#{str}\e[0m"
end

#yellow(str) ⇒ Object (protected)



75
# File 'lib/riot/reporter/io.rb', line 75

def yellow(str); with_color(33, str); end