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.last.kind_of?(Hash) ? args.pop : {})
  @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



66
67
68
69
70
71
72
73
74
75
# File 'lib/riot/reporter/io.rb', line 66

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



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

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)



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

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)


82
83
84
# File 'lib/riot/reporter/io.rb', line 82

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



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

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)



88
89
90
# File 'lib/riot/reporter/io.rb', line 88

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

#yellow(str) ⇒ Object (protected)



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

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