module MiniSpec::ExceptionInspector

Public Instance Methods

raised_as_expected?(subject, type, match, context) click to toggle source
# File lib/minispec/utils/raise.rb, line 57
def raised_as_expected? subject, type, match, context
  if type && match
    x = validate_type(subject, type, context)
    return x if x.is_a?(ExceptionError)
    validate_message(subject, match, context)
  elsif type
    validate_type(subject, type, context)
  elsif match
    validate_message(subject, match, context)
  else
    validate(context)
  end
end
raised_as_expected_by_proc?(subject, context) click to toggle source
# File lib/minispec/utils/raise.rb, line 45
def raised_as_expected_by_proc? subject, context
  x = context[:right_proc].call(*subject) # splat needed on multiple expectations
  if context[:negation]
    # return true if block returns false or nil
    return true if !x
    return ExceptionError.new('Not expected any error to be raised')
  end
  # return true if block returns a positive value
  return true if x
  ExceptionError.new('Expected some error to be raised')
end
validate(context) click to toggle source
# File lib/minispec/utils/raise.rb, line 71
def validate context
  x = context[:is_a_exception]
  if context[:negation]
    # return true if no exception raised
    return true if !x
    # return ExceptionError cause a exception raised but not expected
    return ExceptionError.new('Not expected a error to be raised')
  end
  # return true if some exception raised
  return true if x
  # return ExceptionError cause no exception raised
  ExceptionError.new('Expected some error to be raised')
end
validate_message(subject, match, context) click to toggle source
# File lib/minispec/utils/raise.rb, line 104
def validate_message subject, match, context
  x = context[:valid_exception_message]
  if context[:negation]
    # return true if exception message does not match expected value OR no exception raised et all
    return true if !x
    # return ExceptionError cause exception message should NOT match given value
    return ExceptionError.new('Not expected raised error to match %s' % pp(match))
  end
  # return true if exception message matched expected value
  return true if x
  # return ExceptionError cause exception message does NOT match given value OR no exception raised et all
  ExceptionError.new("Expected a error that match %s to be raised.\nInstead %s." % [
    pp(match),
    context[:is_a_exception] ?
      'a error with following message raised: %s' % pp(subject) :
      'nothing raised'
  ])
end
validate_type(subject, type, context) click to toggle source
# File lib/minispec/utils/raise.rb, line 85
def validate_type subject, type, context
  x = context[:valid_exception_type]
  if context[:negation]
    # return true if raised exception is not of expected type OR no exception raised et all
    return true if !x
    # return ExceptionError cause exception should NOT be of given type
    return ExceptionError.new("Not expected a %s error to be raised." % pp(type))
  end
  # return true if raised exception is of expected type
  return true if x
  # return ExceptionError cause raised exception is NOT of given type OR no exception raised et all
  ExceptionError.new("Expected a %s error to be raised.\nInstead %s" % [
    pp(type),
    context[:is_a_exception] ?
      'a %s error raised' % pp(subject.class) :
      'nothing raised'
  ])
end