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, #be_within, #change, clear_generated_description, #eq, #eql, #equal, #exist, #expect, 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
24
25
# 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, nil
  @match_for_should_not_block = 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(method, *args, &block) ⇒ Object (private)



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

def method_missing(method, *args, &block)
  if $matcher_execution_context.respond_to?(method)
    $matcher_execution_context.send method, *args, &block
  else
    super(method, *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



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

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

#define_method(name, &block) ⇒ Object

:nodoc:



54
55
56
# File 'lib/rspec/matchers/matcher.rb', line 54

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

#description(&block) ⇒ Object

See RSpec::Matchers



86
87
88
# File 'lib/rspec/matchers/matcher.rb', line 86

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

#diffableObject

See RSpec::Matchers



96
97
98
# File 'lib/rspec/matchers/matcher.rb', line 96

def diffable
  @diffable = true
end

#diffable?Boolean

Used internally by objects returns by should and should_not.

Returns:

  • (Boolean)


91
92
93
# File 'lib/rspec/matchers/matcher.rb', line 91

def diffable?
  @diffable
end

#does_not_match?(actual) ⇒ Boolean

Used internally by should_not

Returns:

  • (Boolean)


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

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



76
77
78
# File 'lib/rspec/matchers/matcher.rb', line 76

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



81
82
83
# File 'lib/rspec/matchers/matcher.rb', line 81

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



59
60
61
# File 'lib/rspec/matchers/matcher.rb', line 59

def match(&block)
  @match_block = block
end

#match_for_should_not(&block) ⇒ Object

See RSpec::Matchers



65
66
67
# File 'lib/rspec/matchers/matcher.rb', line 65

def match_for_should_not(&block)
  @match_for_should_not_block = block
end

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

See RSpec::Matchers



70
71
72
73
# File 'lib/rspec/matchers/matcher.rb', line 70

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)


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

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