Class: Spec::Matchers::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/matchers/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, expected = nil, &block_passed_to_init) ⇒ Matcher

Returns a new instance of Matcher.



4
5
6
7
8
9
10
11
12
13
# File 'lib/spec/matchers/matcher.rb', line 4

def initialize(name, expected=nil, &block_passed_to_init)
  @name = name
  @expected = expected
  @block = block_passed_to_init
  @messages = {
    :description => lambda {"#{name_to_sentence} #{expected}"},
    :failure_message_for_should => lambda {|actual| "expected #{actual} to #{name_to_sentence} #{expected}"},
    :failure_message_for_should_not => lambda {|actual| "expected #{actual} not to #{name_to_sentence} #{expected}"}
  }
end

Instance Method Details

#description(&block) ⇒ Object



21
22
23
# File 'lib/spec/matchers/matcher.rb', line 21

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

#failure_message_for_should(&block) ⇒ Object



25
26
27
# File 'lib/spec/matchers/matcher.rb', line 25

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

#failure_message_for_should_not(&block) ⇒ Object



29
30
31
# File 'lib/spec/matchers/matcher.rb', line 29

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

#match(&block) ⇒ Object



33
34
35
# File 'lib/spec/matchers/matcher.rb', line 33

def match(&block)
  @match_block = block
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/spec/matchers/matcher.rb', line 15

def matches?(actual)
  @actual = actual
  instance_exec @expected, &@block
  instance_exec @actual, &@match_block
end