lib/test/unit/failure.rb in test-unit-2.5.1 vs lib/test/unit/failure.rb in test-unit-2.5.2
- old
+ new
@@ -9,10 +9,11 @@
# Encapsulates a test failure. Created by Test::Unit::TestCase
# when an assertion fails.
class Failure
attr_reader :test_name, :location, :message
+ attr_reader :method_name, :source_location
attr_reader :expected, :actual, :user_message
attr_reader :inspected_expected, :inspected_actual
SINGLE_CHARACTER = 'F'
LABEL = "Failure"
@@ -21,10 +22,12 @@
# message.
def initialize(test_name, location, message, options={})
@test_name = test_name
@location = location
@message = message
+ @method_name = options[:method_name]
+ @source_location = options[:source_location]
@expected = options[:expected]
@actual = options[:actual]
@inspected_expected = options[:inspected_expected]
@inspected_actual = options[:inspected_actual]
@user_message = options[:user_message]
@@ -78,10 +81,46 @@
def included(base)
base.exception_handler(:handle_assertion_failed_error)
end
end
+ # Report a failure.
+ #
+ # This is a public API for developers who extend test-unit.
+ #
+ # @param message [String] The description about the failure.
+ # @param backtrace [Array<String>] The backtrace for the failure.
+ # @option options [Object] :expected
+ # The expected value of the assertion.
+ # @option options [Object] :actual
+ # The actual value of the assertion.
+ # @option options [String] :inspected_expected
+ # The inspected expected value of the assertion.
+ # It is used for diff between expected and actual of the failure.
+ # @option options [String] :inspected_actual
+ # The inspected actual value of the assertion.
+ # It is used for diff between expected and actual of the failure.
+ # @option options [String] :user_message
+ # The message of the assertion from user.
+ # @option options [String] :method_name (@method_name)
+ # The method name of the test.
+ # @option options [Array<String, Integer>] :source_location
+ # The location where the test is defined. It is the same
+ # format as Proc#source_location. That is, it's an array of
+ # path and and line number where the test definition is
+ # started.
+ # @return [void]
+ def add_failure(message, backtrace, options={})
+ default_options = {
+ :method_name => @method_name,
+ :source_location => self[:source_location],
+ }
+ failure = Failure.new(name, filter_backtrace(backtrace), message,
+ default_options.merge(options))
+ current_result.add_failure(failure)
+ end
+
private
def handle_assertion_failed_error(exception)
return false unless exception.is_a?(AssertionFailedError)
problem_occurred
add_failure(exception.message, exception.backtrace,
@@ -89,15 +128,9 @@
:actual => exception.actual,
:inspected_expected => exception.inspected_expected,
:inspected_actual => exception.inspected_actual,
:user_message => exception.user_message)
true
- end
-
- def add_failure(message, backtrace, options={})
- failure = Failure.new(name, filter_backtrace(backtrace), message,
- options)
- current_result.add_failure(failure)
end
end
module TestResultFailureSupport
attr_reader :failures