Class: Matchy::Expectations::ThrowSymbolExpectation

Inherits:
Base
  • Object
show all
Defined in:
lib/matchy/built_in/error_expectations.rb

Instance Method Summary collapse

Methods inherited from Base

#fail!, #pass!

Constructor Details

#initialize(expected, test_case) ⇒ ThrowSymbolExpectation

Returns a new instance of ThrowSymbolExpectation.



39
40
41
42
# File 'lib/matchy/built_in/error_expectations.rb', line 39

def initialize(expected, test_case)
  @thrown_symbol = nil
  super
end

Instance Method Details

#failure_messageObject



56
57
58
# File 'lib/matchy/built_in/error_expectations.rb', line 56

def failure_message
  "Expected #{@receiver.inspect} to throw :#{@expected}, but #{@thrown_symbol ? ':' + @thrown_symbol.to_s + ' was thrown instead' : 'no symbol was thrown'}."
end

#matches?(receiver) ⇒ Boolean

Returns:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/matchy/built_in/error_expectations.rb', line 44

def matches?(receiver)
  @receiver = receiver
  begin
    receiver.call
  rescue NameError => e
    raise e unless e.message =~ /uncaught throw/
    @thrown_symbol = e.name.to_sym
  ensure
    return @expected == @thrown_symbol
  end
end

#negative_failure_messageObject



60
61
62
# File 'lib/matchy/built_in/error_expectations.rb', line 60

def negative_failure_message
  "Expected #{@receiver.inspect} to not throw :#{@expected}."
end