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

- (Reporter) initialize(*args)

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

- (Object) current_context

The context that is currently being reported on



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

def current_context
  @current_context
end

- (Object) errors

Count of errored assertions so far



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

def errors
  @errors
end

- (Object) failures

Count of failed assertions so far



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

def failures
  @failures
end

- (Object) passes

Count of successful assertions so far



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

def passes
  @passes
end

Instance Method Details

- (Object) describe_context(context)

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



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

def describe_context(context)
  @current_context = context
end

- (Object) error(description, result)

Called if the assertion had an unexpected error.



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

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

- (Object) fail(description, message, line, file)

Called if the assertion failed.



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

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

- (Object) new(*args, &block)



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

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

- (Object) pass(description, result)

Called if the assertion passed.



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

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

- (Object) report(description, response)

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



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

- (Object) results(time_taken)

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



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

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

- (Boolean) success?

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



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

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

- (Object) summarize(&block)

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



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