Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
make Spec::Runner::Reporter::Failure public, with rdoc for public met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
dchelimsky committed Apr 5, 2009
1 parent 8994960 commit 4f45ea2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 59 deletions.
122 changes: 66 additions & 56 deletions lib/spec/runner/reporter.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
module Spec
module Runner
class Reporter
attr_reader :options, :example_groups
attr_reader :options

def initialize(options)
@options = options
@options.reporter = self
clear
@failures = []
@pending_count = 0
@example_count = 0
@start_time = nil
@end_time = nil
end

def example_group_started(example_group)
@example_group = example_group
formatters.each do |f|
f.example_group_started(example_group)
end
example_groups << example_group
end

def example_started(example)
formatters.each{|f| f.example_started(example)}
end

def example_finished(example, error=nil)
@examples << example
@example_count += 1

if error.nil?
example_passed(example)
Expand All @@ -34,15 +38,14 @@ def example_finished(example, error=nil)

def example_failed(example, error)
backtrace_tweaker.tweak_backtrace(error)
failure = Failure.new(example_groups.empty? ? "" : example_groups.last.description, example, error)
failure = Failure.new(@example_group, example, error)
@failures << failure
formatters.each do |f|
f.example_failed(example, @failures.length, failure)
end
end

def start(number_of_examples)
clear
@start_time = Time.new
formatters.each{|f| f.start(number_of_examples)}
end
Expand All @@ -57,12 +60,56 @@ def dump
dump_pending
dump_failures
formatters.each do |f|
f.dump_summary(duration, @examples.length, @failures.length, @pending_count)
f.dump_summary(duration, @example_count, @failures.length, @pending_count)
f.close
end
@failures.length
end

class Failure
def initialize(group, example, exception) # :nodoc:
@group, @example, @exception = group, example, exception
end

# The Exception object raised
attr_reader :exception

# Header messsage for reporting this failure, including the name of the
# example and an indicator of the type of failure. FAILED indicates a
# failed expectation. FIXED indicates a pending example that passes, and
# no longer needs to be pending. RuntimeError indicates that a
# RuntimeError occured.
#
# == Examples
#
# 'A new account should have a zero balance' FAILED
# 'A new account should have a zero balance' FIXED
# RuntimeError in 'A new account should have a zero balance'
def header
if expectation_not_met?
"'#{example_name}' FAILED"
elsif pending_fixed?
"'#{example_name}' FIXED"
else
"#{@exception.class.name} in '#{example_name}'"
end
end

def pending_fixed? # :nodoc:
@exception.is_a?(Spec::Example::PendingExampleFixedError)
end

def expectation_not_met? # :nodoc:
@exception.is_a?(Spec::Expectations::ExpectationNotMetError)
end

private

def example_name
"#{@group.description} #{@example.description}"
end
end

private

def formatters
Expand All @@ -73,15 +120,6 @@ def backtrace_tweaker
@options.backtrace_tweaker
end

def clear
@example_groups = []
@failures = []
@pending_count = 0
@examples = []
@start_time = nil
@end_time = nil
end

def dump_failures
return if @failures.empty?
@failures.inject(1) do |index, failure|
Expand All @@ -104,15 +142,18 @@ def example_passed(example)
end

EXAMPLE_PENDING_DEPRECATION_WARNING = <<-WARNING
DEPRECATION WARNING: RSpec's formatters have changed example_pending
to accept two arguments instead of three. Please see the rdoc
for Spec::Runner::Formatter::BaseFormatter#example_pending
for more information.
Please update any custom formatters to accept only two arguments
to example_pending. Support for example_pending with two arguments
and this warning message will be removed after the RSpec 2.0 release.
WARNING
*********************************************************************
DEPRECATION WARNING: RSpec's formatters have changed example_pending
to accept two arguments instead of three. Please see the rdoc
for Spec::Runner::Formatter::BaseFormatter#example_pending
for more information.
Please update any custom formatters to accept only two arguments
to example_pending. Support for example_pending with two arguments
and this warning message will be removed after the RSpec 2.0 release.
*********************************************************************
WARNING

def example_pending(example, ignore, message="Not Yet Implemented")
@pending_count += 1
Expand All @@ -130,37 +171,6 @@ def formatter_uses_deprecated_example_pending_method?(formatter)
formatter.method(:example_pending).arity == 3
end

class Failure
attr_reader :example, :exception

def initialize(group, example, exception)
@group, @example, @exception = group, example, exception
end

def header
if expectation_not_met?
"'#{example_name}' FAILED"
elsif pending_fixed?
"'#{example_name}' FIXED"
else
"#{@exception.class.name} in '#{example_name}'"
end
end

def pending_fixed?
@exception.is_a?(Spec::Example::PendingExampleFixedError)
end

def expectation_not_met?
@exception.is_a?(Spec::Expectations::ExpectationNotMetError)
end

protected

def example_name
"#{@group} #{@example.description}"
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/spec/example/example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ class << example_group
ExampleGroupFactory.reset
end

it "should send reporter add_example_group" do
it "should send reporter example_group_started" do
reporter.should_receive(:example_group_started)
example_group.run(options)
reporter.example_groups.should == [example_group]
end

it "should run example on run" do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec/runner/reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def description_of(example)
formatter.should_receive(:example_passed).with(description_of(passing)).exactly(2).times
formatter.should_receive(:example_failed).with(description_of(failing), 1, failure)
formatter.should_receive(:example_failed).with(description_of(failing), 2, failure)
formatter.should_receive(:dump_failure).exactly(2).times
formatter.should_receive(:dump_failure).exactly(2).times
formatter.should_receive(:start_dump)
formatter.should_receive(:dump_pending)
formatter.should_receive(:close).with(no_args)
Expand Down

0 comments on commit 4f45ea2

Please sign in to comment.