Module: MiniSpec::ExceptionInspector

Extended by:
ExceptionInspector, Utils
Included in:
ExceptionInspector
Defined in:
lib/minispec/utils/raise.rb

Instance Method Summary collapse

Methods included from Utils

any_match?, array_elements_map, catch_symbol, exception_raised?, extract_thrown_symbol, match?, method_visibility, pp, rejected?, shorten_source, source, symbol_thrown?, undefine_method, valid_proxy_arguments?, zipper

Instance Method Details

#raised_as_expected?(subject, type, match, context) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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