Class: Given::FailureMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/given/failure_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(exception_class, message_pattern) ⇒ FailureMatcher

Returns a new instance of FailureMatcher.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/given/failure_matcher.rb', line 4

def initialize(exception_class, message_pattern)
  @no_pattern = false
  @expected_exception_class = exception_class
  @expected_message_pattern = message_pattern
  if @expected_message_pattern.nil?
    @expected_message_pattern = //
    @no_pattern = true
  elsif @expected_message_pattern.is_a?(String)
    @expected_message_pattern =
      Regexp.new("\\A" + Regexp.quote(@expected_message_pattern) + "\\z")
  end
end

Instance Method Details

#!=(other) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/given/failure_matcher.rb', line 25

def !=(other)
  if other.respond_to?(:call)
    does_not_match?(other)
  else
    super
  end
end

#==(other) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/given/failure_matcher.rb', line 17

def ==(other)
  if other.respond_to?(:call)
    matches?(other)
  else
    super
  end
end

#does_not_match?(possible_failure) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/given/failure_matcher.rb', line 41

def does_not_match?(possible_failure)
  if possible_failure.respond_to?(:call)
    mismatch_or_fail(possible_failure)
  else
    true
  end
end

#inspectObject



49
50
51
52
53
# File 'lib/given/failure_matcher.rb', line 49

def inspect
  result = "<Failure on #{@expected_exception_class}"
  result << " matching #{@expected_message_pattern.inspect}" unless @no_pattern
  result << ">"
end

#matches?(possible_failure) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/given/failure_matcher.rb', line 33

def matches?(possible_failure)
  if possible_failure.respond_to?(:call)
    match_or_fail(possible_failure)
  else
    Given.fail_with("#{description}, but nothing failed")
  end
end