Module: MiniSpec::ThrowInspector

Extended by:
ThrowInspector, Utils
Included in:
ThrowInspector
Defined in:
lib/minispec/utils/throw.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

#any_symbol_thrown?(thrown_symbol, context) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
# File 'lib/minispec/utils/throw.rb', line 90

def any_symbol_thrown? thrown_symbol, context
  if context[:negation]
    return true if !thrown_symbol
    return ThrowError.new('%s symbol thrown when not expected' % pp(thrown_symbol))
  end
  return true if thrown_symbol
  ThrowError.new('Expected a symbol to be thrown')
end

#correct_symbol_thrown?(expected_symbol, thrown_symbol, context) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/minispec/utils/throw.rb', line 99

def correct_symbol_thrown? expected_symbol, thrown_symbol, context
  # needed here cause this method are invoked directly by mock validators.
  # and it's not a double check cause Utils.symbol_thrown?
  # wont arrive here if called with a block
  if context[:right_proc]
    return thrown_as_expected_by_proc?(thrown_symbol, context)
  end

  x = expected_symbol == thrown_symbol
  if context[:negation]
    return true if !x
    return ThrowError.new('Not expected %s symbol to be thrown' % pp(thrown_symbol))
  end
  return true if x
  ThrowError.new('Expected %s symbol to be thrown. Instead %s thrown.' % [
    pp(expected_symbol),
    thrown_symbol ?
      pp(thrown_symbol) :
      'nothing'
  ])
end

#correct_value_thrown?(thrown_symbol, expected_value, thrown_value, context) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/minispec/utils/throw.rb', line 121

def correct_value_thrown? thrown_symbol, expected_value, thrown_value, context
  x = expected_value.is_a?(Regexp)   ?
    (thrown_value.to_s =~ expected_value) :
    (thrown_value      == expected_value)
  if context[:negation]
    return true if !x
    return ThrowError.new('Not expected %s symbol\'s value to match %s' % [
      thrown_symbol,
      thrown_value,
    ].map(&method(:pp)))
  end
  return true if x
  ThrowError.new("Expected %s symbol's value to match %s\nActual value: %s" % [
    thrown_symbol,
    expected_value,
    thrown_value
  ].map(&method(:pp)))
end

#thrown_as_expected?(expected_symbol, expected_value, thrown_symbol, thrown_value, context) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/minispec/utils/throw.rb', line 78

def thrown_as_expected? expected_symbol, expected_value, thrown_symbol, thrown_value, context
  if expected_symbol && expected_value
    x = correct_symbol_thrown?(expected_symbol, thrown_symbol, context)
    return x if x.is_a?(ThrowError)
    correct_value_thrown?(expected_symbol, expected_value, thrown_value, context)
  elsif expected_symbol
    correct_symbol_thrown?(expected_symbol, thrown_symbol, context)
  else
    any_symbol_thrown?(thrown_symbol, context)
  end
end

#thrown_as_expected_by_proc?(thrown_symbol, context) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'lib/minispec/utils/throw.rb', line 68

def thrown_as_expected_by_proc? thrown_symbol, context
  x = context[:right_proc].call(*thrown_symbol) # splat needed on multiple expectations
  if context[:negation]
    return true if !x
    return ThrowError.new('Not expected any symbol to be thrown')
  end
  return true if x
  ThrowError.new('Expected a symbol to be thrown')
end