class Test::Unit::Assertions::AssertExceptionHelper - test-unit: Ruby Standard Library Documentation

# class Test::Unit::Assertions::AssertExceptionHelper

### Public Class Methods

new(test_case, expected_exceptions) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2432
    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

### Public Instance Methods

expected?(actual_exception, equality=nil) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2454
    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_exceptions() click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2439
    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

### Private Instance Methods

exception_class?(exception_type) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2498
    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) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2488
    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_class?(actual_exception, equality) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2508
    def expected_class?(actual_exception, equality)
      @expected_classes.any? do |expected_class|
        actual_exception.__send__(equality, expected_class)
      end
    end

expected_module?(actual_exception) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2514
    def expected_module?(actual_exception)
      @expected_modules.any? do |expected_module|
        actual_exception.is_a?(expected_module)
      end
    end

expected_object?(actual_exception) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2520
    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) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2527
    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) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2462
    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