Class: RSpec::Matchers::Matcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers, Extensions::InstanceEvalWithArgs, 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_true, #be_within, #change, clear_generated_description, #cover, #eq, #eql, #equal, #exist, #expect, generated_description, #have, #have_at_least, #have_at_most, #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 Extensions::InstanceEvalWithArgs

#instance_eval_with_args

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_eval_with_args(*@expected, &declarations)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



114
115
116
117
118
119
120
# File 'lib/rspec/matchers/matcher.rb', line 114

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



105
106
107
108
109
110
# File 'lib/rspec/matchers/matcher.rb', line 105

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

#define_method(name, &block) ⇒ Object



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

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

#description(&block) ⇒ Object

See RSpec::Matchers



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

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

#diffableObject

See RSpec::Matchers



100
101
102
# File 'lib/rspec/matchers/matcher.rb', line 100

def diffable
  @diffable = true
end

#diffable?Boolean

Used internally by objects returns by should and should_not.

Returns:

  • (Boolean)


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

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_eval_with_args(actual, &@match_for_should_not_block) :
    !matches?(actual)
end

#failure_message_for_should(&block) ⇒ Object

See RSpec::Matchers



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

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



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

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

#include(*args) ⇒ Object



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

def include(*args)
  singleton_class.__send__(:include, *args)
end

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

See RSpec::Matchers



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

def match(&block)
  @match_block = block
end

#match_for_should_not(&block) ⇒ Object

See RSpec::Matchers



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

def match_for_should_not(&block)
  @match_for_should_not_block = block
end

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

See RSpec::Matchers



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

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_eval_with_args(actual, &@match_block)
      true
    rescue @expected_exception => @rescued_exception
      false
    end
  else
    begin
      instance_eval_with_args(actual, &@match_block)
    rescue RSpec::Expectations::ExpectationNotMetError
      false
    end
  end
end