Class: RSpec::Matchers::Matcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers, InstanceExec, Pretty
Defined in:
lib/rspec/matchers/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RSpec::Matchers

#be, #be_a, #be_a_kind_of, #be_an_instance_of, #be_close, #change, clear_generated_description, #eq, #eql, #equal, #exist, generated_description, #have, #have_at_least, #have_at_most, #include, #raise_error, #respond_to, #satisfy, #throw_symbol

Methods included from DSL

#define

Methods included from Pretty

#_pretty_print, #split_words, #to_sentence

Methods included from InstanceExec

#instance_exec

Constructor Details

#initialize(name, *expected, &declarations) ⇒ Matcher

Returns a new instance of Matcher.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rspec/matchers/matcher.rb', line 9

def initialize(name, *expected, &declarations)
  @name     = name
  @expected = expected
  @actual   = nil
  @diffable = false
  @expected_exception, @rescued_exception = nil
  @messages = {
    :description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
    :failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
    :failure_message_for_should_not => lambda {|actual| "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"}
  }
  making_declared_methods_public do
    instance_exec(*@expected, &declarations)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



110
111
112
113
114
115
116
# File 'lib/rspec/matchers/matcher.rb', line 110

def method_missing(name, *args, &block)
  if $matcher_execution_context.respond_to?(name)
    $matcher_execution_context.send name, *args, &block
  else
    super(name, *args, &block)
  end
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



8
9
10
# File 'lib/rspec/matchers/matcher.rb', line 8

def actual
  @actual
end

#expectedObject (readonly)

Returns the value of attribute expected.



8
9
10
# File 'lib/rspec/matchers/matcher.rb', line 8

def expected
  @expected
end

#rescued_exceptionObject (readonly)

Returns the value of attribute rescued_exception.



8
9
10
# File 'lib/rspec/matchers/matcher.rb', line 8

def rescued_exception
  @rescued_exception
end

Instance Method Details

#chain(method, &block) ⇒ Object

See RSpec::Matchers



99
100
101
102
103
104
105
106
# File 'lib/rspec/matchers/matcher.rb', line 99

def chain(method, &block)
  self.class.class_eval do
    define_method method do |*args|
      block.call(*args)
      self
    end
  end
end

#define_method(name, &block) ⇒ Object

:nodoc:



52
53
54
# File 'lib/rspec/matchers/matcher.rb', line 52

def define_method(name, &block) # :nodoc:
  singleton_class.__send__(:define_method, name, &block)
end

#description(&block) ⇒ Object

See RSpec::Matchers



84
85
86
# File 'lib/rspec/matchers/matcher.rb', line 84

def description(&block)
  cache_or_call_cached(:description, &block)
end

#diffableObject

See RSpec::Matchers



94
95
96
# File 'lib/rspec/matchers/matcher.rb', line 94

def diffable
  @diffable = true
end

#diffable?Boolean

Used internally by objects returns by should and should_not.

Returns:

  • (Boolean)


89
90
91
# File 'lib/rspec/matchers/matcher.rb', line 89

def diffable?
  @diffable
end

#does_not_match?(actual) ⇒ Boolean

Used internally by should_not

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/rspec/matchers/matcher.rb', line 45

def does_not_match?(actual)
  @actual = actual
  @match_for_should_not_block ?
    instance_exec(actual, &@match_for_should_not_block) :
    !matches?(actual)
end

#failure_message_for_should(&block) ⇒ Object

See RSpec::Matchers



74
75
76
# File 'lib/rspec/matchers/matcher.rb', line 74

def failure_message_for_should(&block)
  cache_or_call_cached(:failure_message_for_should, &block)
end

#failure_message_for_should_not(&block) ⇒ Object

See RSpec::Matchers



79
80
81
# File 'lib/rspec/matchers/matcher.rb', line 79

def failure_message_for_should_not(&block)
  cache_or_call_cached(:failure_message_for_should_not, &block)
end

#match(&block) ⇒ Object Also known as: match_for_should

See RSpec::Matchers



57
58
59
# File 'lib/rspec/matchers/matcher.rb', line 57

def match(&block)
  @match_block = block
end

#match_for_should_not(&block) ⇒ Object

See RSpec::Matchers



63
64
65
# File 'lib/rspec/matchers/matcher.rb', line 63

def match_for_should_not(&block)
  @match_for_should_not_block = block
end

#match_unless_raises(exception = Exception, &block) ⇒ Object

See RSpec::Matchers



68
69
70
71
# File 'lib/rspec/matchers/matcher.rb', line 68

def match_unless_raises(exception=Exception, &block)
  @expected_exception = exception
  match(&block)
end

#matches?(actual) ⇒ Boolean

Used internally by should and should_not.

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rspec/matchers/matcher.rb', line 26

def matches?(actual)
  @actual = actual
  if @expected_exception
    begin
      instance_exec(actual, &@match_block)
      true
    rescue @expected_exception => @rescued_exception
      false
    end
  else
    begin
      instance_exec(actual, &@match_block)
    rescue RSpec::Expectations::ExpectationNotMetError
      false
    end
  end
end