Class: Riot::Reporter
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
Instance Attribute Summary collapse
-
#current_context ⇒ Object
The context that is currently being reported on.
-
#errors ⇒ Object
Count of errored assertions so far.
-
#failures ⇒ Object
Count of failed assertions so far.
-
#passes ⇒ Object
Count of successful assertions so far.
Instance Method Summary collapse
-
#describe_context(context) ⇒ Object
Called when a new context is about to execute to set the state for this Reporter instance.
-
#error(description, result) ⇒ Object
Called if the assertion had an unexpected error.
-
#fail(description, message, line, file) ⇒ Object
Called if the assertion failed.
-
#initialize(*args) ⇒ Reporter
constructor
Creates a new Reporter instance and initializes counts to zero.
- #new(*args, &block) ⇒ Object
-
#pass(description, result) ⇒ Object
Called if the assertion passed.
-
#report(description, response) ⇒ Object
Called immediately after an assertion has been evaluated.
-
#results(time_taken) ⇒ Object
Called after all contexts have finished.
-
#success? ⇒ Boolean
Returns true if no failures or errors have been produced yet.
-
#summarize(&block) ⇒ Object
Starts a timer, execute the provided block, then reports the results.
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. @passes = @failures = @errors = 0 @current_context = Riot::RootContext.new end |
Instance Attribute Details
#current_context ⇒ Object
The context that is currently being reported on
22 23 24 |
# File 'lib/riot/reporter.rb', line 22 def current_context @current_context end |
#errors ⇒ Object
Count of errored assertions so far
19 20 21 |
# File 'lib/riot/reporter.rb', line 19 def errors @errors end |
#failures ⇒ Object
Count of failed assertions so far
16 17 18 |
# File 'lib/riot/reporter.rb', line 16 def failures @failures end |
#passes ⇒ Object
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.
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.
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.
91 92 93 |
# File 'lib/riot/reporter.rb', line 91 def fail(description, , 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.
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.
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 , line, file = *response[1..-1] fail(description, , 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.
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.
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).
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 |