Sha256: 72e0155107d71d945f9807d329c630fe91bfbfa7d733e76df813a48c4e353d0b

Contents?: true

Size: 1.07 KB

Versions: 110

Compression:

Stored size: 1.07 KB

Contents

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

  def matches?(proc)
    proc.call
    return false
  rescue Exception => err
    @actual = err
    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"
    end

    message
  end

  def negative_failure_message
    ["Expected to not get #{@exception}#{%[ (#{@message})] if @message}", ""]
  end
end

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

Version data entries

110 entries across 86 versions & 2 rubygems

Version Path
rhodes-3.1.1 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.1.1.beta lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.1.0 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.1.0.beta.5 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.1.0.beta.4 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.1.0.beta.3 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.1.0.beta.2 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.1.0.beta.1 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.2 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.2.beta.1 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.1 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.1.beta.8 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.1.beta.7 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.1.beta.6 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.1.beta.5 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.1.beta.4 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.1.beta.3 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.1.beta.2 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.0 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.0.0.beta.7 lib/extensions/mspec/mspec/matchers/raise_error.rb