Sha256: a408c9b836bd4c1e1563e84cbc8df6932a22593ebfac66bc23c93aeb64476c87

Contents?: true

Size: 1.2 KB

Versions: 46

Compression:

Stored size: 1.2 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 => @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"
    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

46 entries across 46 versions & 2 rubygems

Version Path
rhodes-5.5.18 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-5.5.17 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-5.5.15 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-5.5.0.22 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-5.5.2 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-5.5.0.7 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-5.5.0.3 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-5.5.0 lib/extensions/mspec/mspec/matchers/raise_error.rb
tauplatform-1.0.3 lib/extensions/mspec/mspec/matchers/raise_error.rb
tauplatform-1.0.2 lib/extensions/mspec/mspec/matchers/raise_error.rb
tauplatform-1.0.1 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.5.1.12 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.3.5 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.4.2 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.3.4 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.3.3 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.3.3.beta.4 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.3.3.beta.3 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.3.3.beta.2 lib/extensions/mspec/mspec/matchers/raise_error.rb
rhodes-3.3.3.beta.1 lib/extensions/mspec/mspec/matchers/raise_error.rb