Permalink
Browse files

Added error message for Test Unit and RSpec 2.

  • Loading branch information...
1 parent 816b2be commit 51bfaf0d60cb585dc8d4e8f46ae84a4e8904363c @fnando committed Aug 28, 2010
View
@@ -16,7 +16,8 @@ class UnsupportedNotifierError < StandardError; end
TITLES = {
:fail => "Failed!",
- :success => "Passed!"
+ :success => "Passed!",
+ :error => "Error!"
}
def notify(options)
@@ -39,6 +40,7 @@ def notifier
end
end
- autoload :Notifier, "test_notifier/notifier"
- autoload :Runner, "test_notifier/runner"
+ autoload :Notifier, "test_notifier/notifier"
+ autoload :Runner, "test_notifier/runner"
+ autoload :Stats, "test_notifier/stats"
end
@@ -9,7 +9,8 @@ module OsdCat
ALIGN = "center"
COLORS = {
:fail => "orange",
- :success => "green"
+ :success => "green",
+ :error => "red"
}
def supported?
@@ -16,7 +16,14 @@
elsif test_unit_matches
_, tests, assertions, failures, errors = *test_unit_matches
- status = failures.to_i > 0 ? :fail : :success
+ if errors.to_i.nonzero?
+ status = :error
+ elsif failures.to_i.nonzero?
+ status = :fail
+ else
+ status = :success
+ end
+
message = "#{tests} tests, #{assertions} assertions, #{failures} failures, #{errors} errors"
TestNotifier.notify(:status => status, :message => message) unless tests.to_i.zero?
end
@@ -4,15 +4,18 @@
class RSpec::Core::Formatters::BaseTextFormatter
alias dump_summary_original dump_summary
- def dump_summary(duration, examples, failed, pending)
- dump_summary_original(duration, examples, failed, pending)
+ def dump_summary(duration, example_count, failure_count, pending_count)
+ dump_summary_original(duration, example_count, failure_count, pending_count)
- begin
- return if examples == 0
- status = failed > 0 ? :fail : :success
- message = "#{examples} examples, #{failed} failed, #{pending} pending"
- TestNotifier.notify(:status => status, :message => message)
- rescue
- end
+ return if example_count.zero?
+
+ stats = TestNotifier::Stats.new(:rspec, {
+ :total => example_count,
+ :fail => failure_count,
+ :pending => pending_count,
+ :error => examples.reject {|e| e.instance_variable_get("@exception").nil?}.count
+ })
+
+ TestNotifier.notify(:status => stats.status, :message => stats.message)
end
end
@@ -4,15 +4,18 @@
class Spec::Runner::Formatter::BaseTextFormatter
alias dump_summary_original dump_summary
- def dump_summary(duration, examples, failed, pending)
- dump_summary_original(duration, examples, failed, pending)
+ def dump_summary(duration, example_count, failure_count, pending_count)
+ dump_summary_original(duration, example_count, failure_count, pending_count)
- begin
- return if examples == 0
- status = failed > 0 ? :fail : :success
- message = "#{examples} examples, #{failed} failed, #{pending} pending"
- TestNotifier.notify(:status => status, :message => message)
- rescue
- end
+ return if example_count.zero?
+
+ stats = TestNotifier::Stats.new(:spec, {
+ :total => example_count,
+ :fail => failure_count,
+ :pending => pending_count,
+ :error => nil
+ })
+
+ TestNotifier.notify(:status => stats.status, :message => stats.message) if example_count > 0
end
end
@@ -10,12 +10,11 @@ def finished(elapsed_time)
begin
re = /(\d+) tests, (\d+) assertions, (\d+) failures, (\d+) errors/
_, tests, assertions, failures, errors = *@result.to_s.match(re)
+ return if tests.to_i.zero?
- return if tests.to_i == 0
- status = (failures.to_i + errors.to_i) > 0 ? :fail : :success
- message = "#{tests} tests, #{assertions} assertions, #{failures} failures, #{errors} errors"
- TestNotifier.notify(:status => status, :message => message)
+ stats = TestNotifier::Stats.new(:test_unit, :total => tests, :assertions => assertions, :fail => failures, :error => errors)
+ TestNotifier.notify(:status => stats.status, :message => stats.message)
rescue
end
end
View
@@ -0,0 +1,89 @@
+module TestNotifier
+ class Stats
+ attr_accessor :adapter, :options
+
+ def initialize(adapter, options = {})
+ @adapter = adapter
+ @options = normalize(options)
+ end
+
+ def status
+ @options = normalize(options)
+ send("status_for_#{adapter}")
+ end
+
+ def message
+ @options = normalize(options)
+ send("message_for_#{adapter}")
+ end
+
+ private
+ def normalize(options)
+ [:total, :success, :fail, :pending, :error, :assertions].inject({}) do |buffer, key|
+ buffer[key] = options[key].to_i
+ buffer
+ end
+ end
+
+ def status_for_test_unit
+ if options[:error].nonzero?
+ :error
+ elsif options[:fail].nonzero?
+ :fail
+ else
+ :success
+ end
+ end
+
+ def status_for_rspec
+ if options[:error].nonzero?
+ :error
+ elsif options[:fail].nonzero?
+ :fail
+ else
+ :success
+ end
+ end
+
+ def status_for_spec
+ if options[:fail].nonzero?
+ :fail
+ else
+ :success
+ end
+ end
+
+ def message_for_rspec
+ options[:success] = options[:total] - options[:fail]
+ options[:fail] = options[:fail] - options[:error]
+
+ text = []
+ text << "#{options[:total]} " + pluralize(options[:total], "example")
+ text << "#{options[:fail]} failed" unless options[:fail].zero?
+ text << "#{options[:pending]} pending" unless options[:pending].zero?
+ text << "#{options[:error]} " + pluralize(options[:error], "error") unless options[:error].zero?
+ text.join(", ")
+ end
+
+ def message_for_spec
+ text = []
+ text << "#{options[:total]} " + pluralize(options[:total], "example")
+ text << "#{options[:fail]} failed" unless options[:fail].zero?
+ text << "#{options[:pending]} pending" unless options[:pending].zero?
+ text.join(", ")
+ end
+
+ def message_for_test_unit
+ text = []
+ text << "#{options[:total]} " + pluralize(options[:total], "test")
+ text << "#{options[:assertions]} " + pluralize(options[:assertions], "assertion")
+ text << "#{options[:fail]} failed" unless options[:fail].zero?
+ text << "#{options[:error]} " + pluralize(options[:error], "error") unless options[:error].zero?
+ text.join(", ")
+ end
+
+ def pluralize(count, text)
+ count > 1 ? "#{text}s" : text
+ end
+ end
+end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,84 @@
+require "test_helper"
+
+class TestNotifier::Stats::RSpecTest < Test::Unit::TestCase
+ def setup
+ @stats = TestNotifier::Stats.new(:rspec)
+ end
+
+ test "success message" do
+ @stats.options = { :total => 10 }
+ assert_equal "10 examples", @stats.message
+ end
+
+ test "message with failing examples" do
+ @stats.options = { :total => 10, :fail => 5 }
+ assert_equal "10 examples, 5 failed", @stats.message
+ end
+
+ test "message with pending examples" do
+ @stats.options = { :total => 10, :pending => 5 }
+ assert_equal "10 examples, 5 pending", @stats.message
+ end
+
+ test "message with error examples" do
+ @stats.options = { :total => 10, :fail => 5, :error => 5 }
+ assert_equal "10 examples, 5 errors", @stats.message
+ end
+
+ test "message with all types" do
+ @stats.options = { :total => 6, :fail => 3, :error => 2, :pending => 3 }
+ assert_equal "6 examples, 1 failed, 3 pending, 2 errors", @stats.message
+ end
+end
+
+class TestNotifier::Stats::SpecTest < Test::Unit::TestCase
+ def setup
+ @stats = TestNotifier::Stats.new(:spec)
+ end
+
+ test "success message" do
+ @stats.options = { :total => 10 }
+ assert_equal "10 examples", @stats.message
+ end
+
+ test "message with failing examples" do
+ @stats.options = { :total => 10, :fail => 5 }
+ assert_equal "10 examples, 5 failed", @stats.message
+ end
+
+ test "message with pending examples" do
+ @stats.options = { :total => 10, :pending => 5 }
+ assert_equal "10 examples, 5 pending", @stats.message
+ end
+
+ test "message with all types" do
+ @stats.options = { :total => 6, :fail => 2, :pending => 3 }
+ assert_equal "6 examples, 2 failed, 3 pending", @stats.message
+ end
+end
+
+class TestNotifier::Stats::TestUnitTest < Test::Unit::TestCase
+ def setup
+ @stats = TestNotifier::Stats.new(:test_unit)
+ end
+
+ test "success message" do
+ @stats.options = { :total => 10, :assertions => 20 }
+ assert_equal "10 tests, 20 assertions", @stats.message
+ end
+
+ test "message with failing examples" do
+ @stats.options = { :total => 10, :assertions => 20, :fail => 5 }
+ assert_equal "10 tests, 20 assertions, 5 failed", @stats.message
+ end
+
+ test "message with error examples" do
+ @stats.options = { :total => 10, :assertions => 20, :error => 5 }
+ assert_equal "10 tests, 20 assertions, 5 errors", @stats.message
+ end
+
+ test "message with all types" do
+ @stats.options = { :total => 6, :fail => 2, :error => 3, :assertions => 20 }
+ assert_equal "6 tests, 20 assertions, 2 failed, 3 errors", @stats.message
+ end
+end
View
@@ -35,8 +35,10 @@ with Autotest ZenTest gem for Rails apps.
"lib/test_notifier/runner/rspec.rb",
"lib/test_notifier/runner/spec.rb",
"lib/test_notifier/runner/test_unit.rb",
+ "lib/test_notifier/stats.rb",
"lib/test_notifier/test_unit.rb",
"lib/test_notifier/version.rb",
+ "resources/error.png",
"resources/fail.png",
"resources/register-growl.scpt",
"resources/success.png"
@@ -49,6 +51,7 @@ with Autotest ZenTest gem for Rails apps.
s.summary = %q{Display system notifications (dbus, growl and snarl) after running tests.}
s.test_files = [
"test/notifier_test.rb",
+ "test/stats_test.rb",
"test/test_helper.rb",
"test/test_notifier_test.rb"
]

0 comments on commit 51bfaf0

Please sign in to comment.