Sha256: c626b6671701ce53c553cb76f91d857a8713cc930475396b335555319b5467be

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

class RaiseErrorMatcher
  def initialize(exception, message, &block)
    @exception = exception
    @message = message
    @block = block
  end

  def matches?(proc)
    @result = proc.call
    return false
  rescue Exception => @actual
    return false unless @exception === @actual
    if @message then
      case @message
      when String then
        return false if @message != @actual.message
      when Regexp then
        return false if @message !~ @actual.message
      end
    end

    @block[@actual] if @block

    return true
  end

  def failure_message
    message = ["Expected #{@exception}#{%[ (#{@message})] if @message}"]

    if @actual then
      message << "but got #{@actual.class}#{%[ (#{@actual.message})] if @actual.message}"
    else
      message << "but no exception was raised (#@result was returned)"
    end

    message
  end

  def negative_failure_message
    message = ["Expected to not get #{@exception}#{%[ (#{@message})] if @message}", ""]
    message[1] = "but got #{@actual.class}#{%[ (#{@actual.message})] if @actual.message}" unless @actual.class == @exception
    message
  end
end

class Object
  def raise_error(exception=Exception, message=nil, &block)
    RaiseErrorMatcher.new(exception, message, &block)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mspec-1.9.1 lib/mspec/matchers/raise_error.rb
mspec-1.9.0 lib/mspec/matchers/raise_error.rb
mspec-1.8.0 lib/mspec/matchers/raise_error.rb
mspec-1.7.0 lib/mspec/matchers/raise_error.rb
mspec-1.6.0 lib/mspec/matchers/raise_error.rb
mspec-1.5.21 lib/mspec/matchers/raise_error.rb
mspec-1.5.20 lib/mspec/matchers/raise_error.rb
mspec-1.5.19 lib/mspec/matchers/raise_error.rb
mspec-1.5.18 lib/mspec/matchers/raise_error.rb