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_exception, #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
24
# File 'lib/spec/matchers/matcher.rb', line 10

def initialize(name, *expected, &declarations)
  @name     = name
  @expected = expected
  @actual   = nil
  @diffable = false
  @expected_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 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

#chain(method, &block) ⇒ Object

See Spec::Matchers



82
83
84
85
86
87
88
89
# File 'lib/spec/matchers/matcher.rb', line 82

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

#description(&block) ⇒ Object

See Spec::Matchers



67
68
69
# File 'lib/spec/matchers/matcher.rb', line 67

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

#diffableObject

See Spec::Matchers



77
78
79
# File 'lib/spec/matchers/matcher.rb', line 77

def diffable
  @diffable = true
end

#diffable?Boolean

Used internally by objects returns by should and should_not.

Returns:

  • (Boolean)


72
73
74
# File 'lib/spec/matchers/matcher.rb', line 72

def diffable?
  @diffable
end

#failure_message_for_should(&block) ⇒ Object

See Spec::Matchers



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

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

#failure_message_for_should_not(&block) ⇒ Object

See Spec::Matchers



62
63
64
# File 'lib/spec/matchers/matcher.rb', line 62

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

#match(&block) ⇒ Object

See Spec::Matchers



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

def match(&block)
  @match_block = block
end

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

See Spec::Matchers



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

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

#matches?(actual) ⇒ Boolean

Used internally by objects returns by should and should_not.

Returns:

  • (Boolean)


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

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