123456789_123456789_123456789_123456789_123456789_

Class: Test::Unit::Assertions::AssertExceptionHelper

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/test/unit/assertions.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(test_case, expected_exceptions) ⇒ AssertExceptionHelper

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2135

def initialize(test_case, expected_exceptions)
  @test_case = test_case
  @expected_exceptions = expected_exceptions
  @expected_classes, @expected_modules, @expected_objects =
    split_expected_exceptions(expected_exceptions)
end

Instance Method Details

#exception_class?(exception_type) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2201

def exception_class?(exception_type)
  return true if exception_type <= Exception

  if Object.const_defined?(:Java)
    return true if exception_type <= Java::JavaLang::Throwable
  end

  false
end

#exception_object?(exception_type) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2191

def exception_object?(exception_type)
  return true if exception_type.is_a?(Exception)

  if Object.const_defined?(:Java)
    return true if exception_type.is_a?(Java::JavaLang::Throwable)
  end

  false
end

#expected?(actual_exception, equality = nil) ⇒ Boolean

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2157

def expected?(actual_exception, equality=nil)
  equality ||= :instance_of?
  expected_class?(actual_exception, equality) or
    expected_module?(actual_exception) or
    expected_object?(actual_exception)
end

#expected_class?(actual_exception, equality) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2211

def expected_class?(actual_exception, equality)
  @expected_classes.any? do |expected_class|
    actual_exception.__send__(equality, expected_class)
  end
end

#expected_exceptions

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2142

def expected_exceptions
  exceptions = @expected_exceptions.collect do |exception|
    if exception.is_a?(Exception)
      WrappedException.new(exception)
    else
      exception
    end
  end
  if exceptions.size == 1
    exceptions[0]
  else
    exceptions
  end
end

#expected_module?(actual_exception) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2217

def expected_module?(actual_exception)
  @expected_modules.any? do |expected_module|
    actual_exception.is_a?(expected_module)
  end
end

#expected_object?(actual_exception) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2223

def expected_object?(actual_exception)
  @expected_objects.any? do |expected_object|
    expected_object == actual_exception or
      fallback_exception_object_equal(expected_object, actual_exception)
  end
end

#fallback_exception_object_equal(expected_object, actual_exception) (private)

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2230

def fallback_exception_object_equal(expected_object, actual_exception)
  owner = Util::MethodOwnerFinder.find(expected_object, :==)
  if owner == Kernel or owner == Exception
    expected_object.class == actual_exception.class and
      expected_object.message == actual_exception.message
  else
    false
  end
end

#split_expected_exceptions(expected_exceptions) (private)

[ GitHub ]

  
# File 'lib/test/unit/assertions.rb', line 2165

def split_expected_exceptions(expected_exceptions)
  exception_modules = []
  exception_objects = []
  exception_classes = []
  expected_exceptions.each do |exception_type|
    if exception_type.instance_of?(Module)
      exception_modules << exception_type
    elsif exception_object?(exception_type)
      exception_objects << exception_type
    elsif exception_class?(exception_type)
      exception_classes << exception_type
    else
      full_message =
        @test_case.__send__(:build_message,
                            nil,
                            "<?> must be " +
                            "a subclass of Exception, " +
                            "an object of Exception subclasses " +
                            "or a Module",
                            exception_type)
      @test_case.flunk(full_message)
    end
  end
  [exception_classes, exception_modules, exception_objects]
end