module RSpec::Support

Constants

DEFAULT_FAILURE_NOTIFIER

@private

DEFAULT_WARNING_NOTIFIER

@private

KERNEL_METHOD_METHOD

@api private

StrictSignatureVerifier

Abstract base class for signature verifiers.

@api private

Attributes

Public Class Methods

@api private

Used internally to get a class of a given object, even if it does not respond to class.

# File rspec-support/lib/rspec/support.rb, line 62
def self.class_of(object)
  object.class
rescue NoMethodError
  singleton_class = class << object; self; end
  singleton_class.ancestors.find { |ancestor| !ancestor.equal?(singleton_class) }
end

@api private

Defines a helper method that is optimized to require files from the named lib. The passed block MUST be ‘{ |f| require_relative f }` because for `require_relative` to work properly from within the named lib the line of code must be IN that lib.

‘require_relative` is preferred when available because it is always O(1), regardless of the number of dirs in $LOAD_PATH. `require`, on the other hand, does a linear O(N) search over the dirs in the $LOAD_PATH until it can resolve the file relative to one of the dirs.

# File rspec-support/lib/rspec/support.rb, line 24
def self.define_optimized_require_for_rspec(lib, &require_relative)
  define_singleton_method("require_rspec_#{lib}") do |f|
    require_relative.call("#{lib}/#{f}")
  end
end

Remove a previously registered matcher. Useful for cleaning up after yourself in specs.

@private

# File rspec-support/lib/rspec/support/matcher_definition.rb, line 24
def self.deregister_matcher_definition(&block)
  matcher_definitions.delete(block)
end

@api private

# File rspec-support/lib/rspec/support.rb, line 83
def self.failure_notifier
  thread_local_data[:failure_notifier] || DEFAULT_FAILURE_NOTIFIER
end

@api private

# File rspec-support/lib/rspec/support.rb, line 75
def self.failure_notifier=(callable)
  thread_local_data[:failure_notifier] = callable
end

@private

# File rspec-support/lib/rspec/support/matcher_definition.rb, line 29
def self.is_a_matcher?(object)
  matcher_definitions.any? { |md| md.call(object) }
end

@private

# File rspec-support/lib/rspec/support/matcher_definition.rb, line 6
def self.matcher_definitions
  @matcher_definitions ||= []
end

@api private

Used internally to get a method handle for a particular object and method name.

Includes handling for a few special cases:

- Objects that redefine #method (e.g. an HTTPRequest struct)
- BasicObject subclasses that mixin a Kernel dup (e.g. SimpleDelegator)
- Objects that undefine method and delegate everything to another
  object (e.g. Mongoid association objects)
# File rspec-support/lib/rspec/support.rb, line 47
def self.method_handle_for(object, method_name)
  KERNEL_METHOD_METHOD.bind(object).call(method_name)
rescue NameError => original
  begin
    handle = object.method(method_name)
    raise original unless handle.is_a? Method
    handle
  rescue Support::AllExceptionsExceptOnesWeMustNotRescue
    raise original
  end
end

@api private

# File rspec-support/lib/rspec/support.rb, line 88
def self.notify_failure(failure, options={})
  failure_notifier.call(failure, options)
end

Used internally to break cyclic dependency between mocks, expectations, and support. We don’t currently have a consistent implementation of our matchers, though we are considering changing that: github.com/rspec/rspec-mocks/issues/513

@private

# File rspec-support/lib/rspec/support/matcher_definition.rb, line 16
def self.register_matcher_definition(&block)
  matcher_definitions << block
end

@api private

gives a string representation of an object for use in RSpec descriptions

# File rspec-support/lib/rspec/support/matcher_definition.rb, line 36
def self.rspec_description_for_object(object)
  if RSpec::Support.is_a_matcher?(object) && object.respond_to?(:description)
    object.description
  else
    object
  end
end

A single thread local variable so we don’t excessively pollute that namespace.

# File rspec-support/lib/rspec/support.rb, line 70
def self.thread_local_data
  Thread.__rspec_current_thread.__rspec_local_data ||= {}
end

@api private

# File rspec-support/lib/rspec/support.rb, line 110
def self.warning_notifier
  @warning_notifier ||= DEFAULT_WARNING_NOTIFIER
end

@api private

# File rspec-support/lib/rspec/support.rb, line 93
def self.with_failure_notifier(callable)
  orig_notifier = failure_notifier
  self.failure_notifier = callable
  yield
ensure
  self.failure_notifier = orig_notifier
end