Class: Riot::Reporter

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

Overview

A Reporter decides how to output the result of a test. When a context is set to be executed, the #describe_context method is called with the context that will be running; this remains so until the next context is executed. After each assertion is evaluated, #report is called with the description of the assertion and the resulting response.

The general idea is that a sub-class of Reporter should be defined that knows specifically how to output the reported results. In the sub-class, you simply need to implement a pass, fail, error, and results method.

Direct Known Subclasses

IOReporter, SilentReporter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Reporter

Creates a new Reporter instance and initializes counts to zero



25
26
27
28
29
# File 'lib/riot/reporter.rb', line 25

def initialize(*args)
  @options = args.extract_options!
  @passes = @failures = @errors = 0
  @current_context = Riot::RootContext.new
end

Instance Attribute Details

#current_contextObject

The context that is currently being reported on



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

def current_context
  @current_context
end

#errorsObject

Count of errored assertions so far



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

def errors
  @errors
end

#failuresObject

Count of failed assertions so far



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

def failures
  @failures
end

#passesObject

Count of successful assertions so far



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

def passes
  @passes
end

Instance Method Details

#describe_context(context) ⇒ Object

Called when a new context is about to execute to set the state for this Reporter instance.

Parameters:

  • context (Riot::Context)

    the context that is about to execute



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

def describe_context(context)
  @current_context = context
end

#error(description, result) ⇒ Object

Called if the assertion had an unexpected error.

Parameters:

  • description (String)

    the description of the assertion

  • ] (Array<Symbol, Exception])

    result the exception from the assertion



99
100
101
# File 'lib/riot/reporter.rb', line 99

def error(description, result)
  raise "Implement this in a sub-class"
end

#fail(description, message, line, file) ⇒ Object

Called if the assertion failed.

Parameters:

  • description (String)

    the description of the assertion

  • ] (Array<Symbol, String, Number, String])

    response the evaluation response from the assertion



91
92
93
# File 'lib/riot/reporter.rb', line 91

def fail(description, message, line, file)
  raise "Implement this in a sub-class"
end

#new(*args, &block) ⇒ Object



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

def new(*args, &block); self; end

#pass(description, result) ⇒ Object

Called if the assertion passed.

Parameters:

  • description (String)

    the description of the assertion

  • ] (Array<Symbol, String])

    result the evaluation response from the assertion



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

def pass(description, result)
  raise "Implement this in a sub-class"
end

#report(description, response) ⇒ Object

Called immediately after an assertion has been evaluated. From this method either pass, fail, or error will be called.

Parameters:

  • description (String)

    the description of the assertion

  • response (Array<Symbol, *[Object]>)

    the evaluation response from the assertion



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/riot/reporter.rb', line 63

def report(description, response)
  code, result = *response
  case code
  when :pass then
    @passes += 1
    pass(description, result)
  when :fail then
    @failures += 1
    message, line, file = *response[1..-1]
    fail(description, message, line, file)
  when :error, :setup_error, :context_error then
    @errors += 1
    error(description, result)
  end
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



106
107
108
# File 'lib/riot/reporter.rb', line 106

def results(time_taken)
  raise "Implement this in a sub-class"
end

#success?Boolean

Returns true if no failures or errors have been produced yet.

Returns:

  • (Boolean)


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

def success?
  (@failures + @errors) == 0
end

#summarize(&block) ⇒ Object

Starts a timer, execute the provided block, then reports the results. Useful for timing context execution(s).

Parameters:

  • &block (lambda)

    the contexts to run



44
45
46
47
48
49
# File 'lib/riot/reporter.rb', line 44

def summarize(&block)
  started = Time.now
  yield
ensure
  results(Time.now - started)
end