Module: RSpec::Matchers::Composable

### Home Classes Methods

### In Files

- lib/rspec/matchers/composable.rb

### Methods

- ::enumerable?
- ::surface_descriptions_in
- #&
- #===
- #and
- #or
- #|

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Expectations
- RSpec::Expectations::BlockExpectationTarget
- RSpec::Expectations::Configuration
- RSpec::Expectations::ExpectationHelper
- RSpec::Expectations::ExpectationNotMetError
- RSpec::Expectations::ExpectationTarget
- RSpec::Expectations::LegacyMacherAdapter
- RSpec::Expectations::LegacyMatcherAdapter
- RSpec::Expectations::LegacyMatcherAdapter::RSpec1
- RSpec::Expectations::LegacyMatcherAdapter::RSpec2
- RSpec::Expectations::NegativeExpectationHandler
- RSpec::Expectations::PositiveExpectationHandler
- RSpec::Expectations::Syntax
- RSpec::Expectations::Version
- RSpec::Matchers
- RSpec::Matchers::AliasedMatcher
- RSpec::Matchers::AliasedMatcherWithOperatorSupport
- RSpec::Matchers::AliasedNegatedMatcher
- RSpec::Matchers::BuiltIn
- RSpec::Matchers::BuiltIn::All
- RSpec::Matchers::BuiltIn::BaseMatcher
- RSpec::Matchers::BuiltIn::BaseMatcher::DefaultFailureMessages
- RSpec::Matchers::BuiltIn::Be
- RSpec::Matchers::BuiltIn::BeAKindOf
- RSpec::Matchers::BuiltIn::BeAnInstanceOf
- RSpec::Matchers::BuiltIn::BeBetween
- RSpec::Matchers::BuiltIn::BeComparedTo
- RSpec::Matchers::BuiltIn::BeFalsey
- RSpec::Matchers::BuiltIn::BeHelpers
- RSpec::Matchers::BuiltIn::BeNil
- RSpec::Matchers::BuiltIn::BePredicate
- RSpec::Matchers::BuiltIn::BeTruthy
- RSpec::Matchers::BuiltIn::BeWithin
- RSpec::Matchers::BuiltIn::CaptureStderr
- RSpec::Matchers::BuiltIn::CaptureStdout
- RSpec::Matchers::BuiltIn::CaptureStreamToTempfile
- RSpec::Matchers::BuiltIn::Change
- RSpec::Matchers::BuiltIn::ChangeDetails
- RSpec::Matchers::BuiltIn::ChangeFromValue
- RSpec::Matchers::BuiltIn::ChangeRelatively
- RSpec::Matchers::BuiltIn::ChangeToValue
- RSpec::Matchers::BuiltIn::Compound
- RSpec::Matchers::BuiltIn::Compound::And
- RSpec::Matchers::BuiltIn::Compound::NestedEvaluator
- RSpec::Matchers::BuiltIn::Compound::Or
- RSpec::Matchers::BuiltIn::Compound::SequentialEvaluator
- RSpec::Matchers::BuiltIn::ContainExactly
- RSpec::Matchers::BuiltIn::ContainExactly::PairingsMaximizer
- RSpec::Matchers::BuiltIn::Cover
- RSpec::Matchers::BuiltIn::EndWith
- RSpec::Matchers::BuiltIn::Eq
- RSpec::Matchers::BuiltIn::Eql
- RSpec::Matchers::BuiltIn::Equal
- RSpec::Matchers::BuiltIn::Exist
- RSpec::Matchers::BuiltIn::Exist::ExistenceTest
- RSpec::Matchers::BuiltIn::Has
- RSpec::Matchers::BuiltIn::HaveAttributes
- RSpec::Matchers::BuiltIn::Include
- RSpec::Matchers::BuiltIn::Match
- RSpec::Matchers::BuiltIn::NegativeOperatorMatcher
- RSpec::Matchers::BuiltIn::NullCapture
- RSpec::Matchers::BuiltIn::OperatorMatcher
- RSpec::Matchers::BuiltIn::Output
- RSpec::Matchers::BuiltIn::PositiveOperatorMatcher
- RSpec::Matchers::BuiltIn::RaiseError
- RSpec::Matchers::BuiltIn::RespondTo
- RSpec::Matchers::BuiltIn::Satisfy
- RSpec::Matchers::BuiltIn::SpecificValuesChange
- RSpec::Matchers::BuiltIn::StartAndEndWith
- RSpec::Matchers::BuiltIn::StartOrEndWith
- RSpec::Matchers::BuiltIn::StartWith
- RSpec::Matchers::BuiltIn::ThrowSymbol
- RSpec::Matchers::BuiltIn::YieldControl
- RSpec::Matchers::BuiltIn::YieldProbe
- RSpec::Matchers::BuiltIn::YieldSuccessiveArgs
- RSpec::Matchers::BuiltIn::YieldWithArgs
- RSpec::Matchers::BuiltIn::YieldWithNoArgs
- RSpec::Matchers::Composable
- RSpec::Matchers::DSL
- RSpec::Matchers::DSL::DefaultImplementations
- RSpec::Matchers::DSL::Macros
- RSpec::Matchers::DSL::Macros::Deprecated
- RSpec::Matchers::DSL::Matcher
- RSpec::Matchers::ExpectedsForMultipleDiffs
- RSpec::Matchers::MatcherDelegator
- RSpec::Matchers::MatcherProtocol
- RSpec::Matchers::Pretty
- BasicObject
- Object

No matching classes.

# RSpec::Matchers::Composable

Mixin designed to support the composable matcher features of RSpec 3+. Mix it into your custom matcher classes to allow them to be used in a composable fashion.

@api public

### Constants

DescribableItem

  Wraps an item in order to surface its `description` via `inspect`. @api private

### Public Class Methods

enumerable?(item) click to toggle source

Strings are not enumerable on 1.9, and on 1.8 they are an infinitely nested enumerable: since ruby lacks a character class, it yields 1-character strings, which are themselves enumerable, composed of a a single 1-character string, which is an enumerable, etc.

@api private

    # File lib/rspec/matchers/composable.rb, line 158
    def enumerable?(item)
      return false if String === item
      Enumerable === item
    end

surface_descriptions_in(item) click to toggle source

Transforms the given data structue (typically a hash or array) into a new data structure that, when `inspect` is called on it, will provide descriptions of any contained matchers rather than the normal `inspect` output.

You are encouraged to use this in your custom matcher's `description`, `failure_message` or `failure_message_when_negated` implementation if you are supporting any arguments which may be a data structure containing matchers.

@!visibility public

    # File lib/rspec/matchers/composable.rb, line 99
    def surface_descriptions_in(item)
      if Matchers.is_a_describable_matcher?(item)
        DescribableItem.new(item)
      elsif Hash === item
        Hash[surface_descriptions_in(item.to_a)]
      elsif Struct === item
        item.inspect
      elsif enumerable?(item)
        begin
          item.map { |subitem| surface_descriptions_in(subitem) }
        rescue IOError # STDOUT is enumerable but `map` raises an error
          item.inspect
        end
      else
        item
      end
    end

### Public Instance Methods

&(matcher) click to toggle source
Alias for: and
===(value) click to toggle source

Delegates to `matches?`. Allows matchers to be used in composable fashion and also supports using matchers in case statements.

    # File lib/rspec/matchers/composable.rb, line 45
    def ===(value)
      matches?(value)
    end

and(matcher) click to toggle source

Creates a compound `and` expectation. The matcher will only pass if both sub-matchers pass. This can be chained together to form an arbitrarily long chain of matchers.

@example

    expect(alphabet).to start_with("a").and end_with("z")
    expect(alphabet).to start_with("a") & end_with("z")

@note The negative form (`expect(...).not_to matcher.and other`)

    is not supported at this time.

    # File lib/rspec/matchers/composable.rb, line 22
    def and(matcher)
      BuiltIn::Compound::And.new self, matcher
    end

Also aliased as: &
or(matcher) click to toggle source

Creates a compound `or` expectation. The matcher will pass if either sub-matcher passes. This can be chained together to form an arbitrarily long chain of matchers.

@example

    expect(stoplight.color).to eq("red").or eq("green").or eq("yellow")
    expect(stoplight.color).to eq("red") | eq("green") | eq("yellow")

@note The negative form (`expect(...).not_to matcher.or other`)

    is not supported at this time.

    # File lib/rspec/matchers/composable.rb, line 38
    def or(matcher)
      BuiltIn::Compound::Or.new self, matcher
    end

Also aliased as: |
|(matcher) click to toggle source
Alias for: or

[Validate]

Generated with the Darkfish Rdoc Generator 2.