Class Spec::Matchers::Matcher

  1. lib/spec/matchers/matcher.rb
Parent: Object

Attributes

actual [R]
expected [R]

Public class methods

new (name, *expected, &declarations)
[show source]
# 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

Public instance methods

chain (method, &block)
[show source]
# 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)
[show source]
# File lib/spec/matchers/matcher.rb, line 67
      def description(&block)
        cache_or_call_cached(:description, &block)
      end
diffable ()
[show source]
# File lib/spec/matchers/matcher.rb, line 77
      def diffable
        @diffable = true
      end
diffable? ()

Used internally by objects returns by should and should_not.

[show source]
# File lib/spec/matchers/matcher.rb, line 72
      def diffable?
        @diffable
      end
failure_message_for_should (&block)
[show source]
# 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)
[show source]
# 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)
[show source]
# File lib/spec/matchers/matcher.rb, line 46
      def match(&block)
        @match_block = block
      end
match_unless_raises (exception=Exception, &block)
[show source]
# File lib/spec/matchers/matcher.rb, line 51
      def match_unless_raises(exception=Exception, &block)
        @expected_exception = exception
        match(&block)
      end
matches? (actual)

Used internally by objects returns by should and should_not.

[show source]
# 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