class Given::FailureMatcher

Public Class Methods

new(exception_class, message_pattern) click to toggle source
   # File lib/given/failure_matcher.rb
 4 def initialize(exception_class, message_pattern)
 5   @no_pattern = false
 6   @expected_exception_class = exception_class
 7   @expected_message_pattern = message_pattern
 8   if @expected_message_pattern.nil?
 9     @expected_message_pattern = //
10     @no_pattern = true
11   elsif @expected_message_pattern.is_a?(String)
12     @expected_message_pattern =
13       Regexp.new("\\A" + Regexp.quote(@expected_message_pattern) + "\\z")
14   end
15 end

Public Instance Methods

!=(other) click to toggle source
Calls superclass method
   # File lib/given/failure_matcher.rb
25 def !=(other)
26   if other.respond_to?(:call)
27     does_not_match?(other)
28   else
29     super
30   end
31 end
==(other) click to toggle source
Calls superclass method
   # File lib/given/failure_matcher.rb
17 def ==(other)
18   if other.respond_to?(:call)
19     matches?(other)
20   else
21     super
22   end
23 end
does_not_match?(possible_failure) click to toggle source
   # File lib/given/failure_matcher.rb
41 def does_not_match?(possible_failure)
42   if possible_failure.respond_to?(:call)
43     mismatch_or_fail(possible_failure)
44   else
45     true
46   end
47 end
inspect() click to toggle source
   # File lib/given/failure_matcher.rb
49 def inspect
50   result = "<Failure on #{@expected_exception_class}"
51   result << " matching #{@expected_message_pattern.inspect}" unless @no_pattern
52   result << ">"
53 end
matches?(possible_failure) click to toggle source
   # File lib/given/failure_matcher.rb
33 def matches?(possible_failure)
34   if possible_failure.respond_to?(:call)
35     match_or_fail(possible_failure)
36   else
37     Given.fail_with("#{description}, but nothing failed")
38   end
39 end

Private Instance Methods

description() click to toggle source
   # File lib/given/failure_matcher.rb
81 def description
82   result = "Expected failure with #{@expected_exception_class}"
83   result << " matching #{@expected_message_pattern.inspect}" unless @no_pattern
84   result
85 end
extract_exception(possible_failure) click to toggle source
   # File lib/given/failure_matcher.rb
73 def extract_exception(possible_failure)
74   possible_failure.call
75   Given.fail_with("Expected an exception")
76   return nil
77 rescue Exception => ex
78   return ex
79 end
match_exception(ex) click to toggle source
   # File lib/given/failure_matcher.rb
69 def match_exception(ex)
70   ex.is_a?(@expected_exception_class) && @expected_message_pattern =~ ex.message
71 end
match_or_fail(possible_failure) click to toggle source
   # File lib/given/failure_matcher.rb
57 def match_or_fail(possible_failure)
58   ex = extract_exception(possible_failure)
59   match_exception(ex) ||
60     Given.fail_with("#{description}, but got #{ex.inspect}")
61 end
mismatch_or_fail(possible_failure) click to toggle source
   # File lib/given/failure_matcher.rb
63 def mismatch_or_fail(possible_failure)
64   ex = extract_exception(possible_failure)
65   (! match_exception(ex)) ||
66     Given.fail_with("#{unexpected_description}, but got #{ex.inspect}")
67 end
unexpected_description() click to toggle source
   # File lib/given/failure_matcher.rb
87 def unexpected_description
88   result = "Did not expect failure with #{@expected_exception_class}"
89   result << " matching #{@expected_message_pattern.inspect}" unless @no_pattern
90   result
91 end