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 2136

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

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

[ GitHub ]

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

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 2186

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 2143

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 2192

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 2198

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 2205

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 2166

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_type.is_a?(Exception)
      exception_objects << exception_type
    else
      @test_case.__send__(:assert,
                          Exception >= exception_type,
                          "Should expect a class of exception, " +
                          "#{exception_type}")
      exception_classes << exception_type
    end
  end
  [exception_classes, exception_modules, exception_objects]
end