Class: Spec::Matchers::Matcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Spec::Matchers

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

Methods included from DSL

#create, #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.



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

def initialize(name, *expected, &declarations)
  @name     = name
  @expected = expected
  @actual   = nil
  @diffable = false
  @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 in the class Spec::Matchers

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



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

def actual
  @actual
end

#expectedObject (readonly)

Returns the value of attribute expected.



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

def expected
  @expected
end

Instance Method Details

#description(&block) ⇒ Object



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

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

#diffableObject



49
50
51
# File 'lib/spec/matchers/matcher.rb', line 49

def diffable
  @diffable = true
end

#diffable?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/spec/matchers/matcher.rb', line 45

def diffable?
  @diffable
end

#failure_message_for_should(&block) ⇒ Object



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

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

#failure_message_for_should_not(&block) ⇒ Object



37
38
39
# File 'lib/spec/matchers/matcher.rb', line 37

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

#match(&block) ⇒ Object



41
42
43
# File 'lib/spec/matchers/matcher.rb', line 41

def match(&block)
  @match_block = block
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


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

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