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(*args) ⇒ IOReporter

Creates a new IOReporter. You can give it your own IO writer or it will default to STDOUT. If you want to specifically turn colorization off in the output, pass the plain option.

Parameters:

  • writer (IO)

    the writer to use for results

  • options (Hash)

    options for reporter



15
16
17
18
19
# File 'lib/riot/reporter/io.rb', line 15

def initialize(*args)
  super
  @options = args.extract_options!
  @writer = (args.shift || STDOUT)
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



63
64
65
66
67
# File 'lib/riot/reporter/io.rb', line 63

def filter_backtrace(backtrace, &line_handler)
  backtrace.reverse_each do |bt|
    yield bt unless (bt =~ /(\/lib\/riot|rake_test_loader)/)
  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



53
54
55
56
57
# File 'lib/riot/reporter/io.rb', line 53

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

#green(str) ⇒ Object (protected)



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

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



44
45
46
# File 'lib/riot/reporter/io.rb', line 44

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

#plain?Boolean (protected)

Returns:

  • (Boolean)


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

def plain?
  (@options[:plain] || @options["plain"])
end

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

Parameters:

  • message (String)

    the message to be printed



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

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



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

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

#red(str) ⇒ Object (protected)

Color output



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

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



22
23
24
25
# File 'lib/riot/reporter/io.rb', line 22

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)
  plain? ? str : "\e[#{code}m#{str}\e[0m"
end

#yellow(str) ⇒ Object (protected)



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

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