Module: RSpec::Support
Constant Summary
-
DEFAULT_FAILURE_NOTIFIER =
Internal use only
# File 'rspec-support/lib/rspec/support.rb', line 110lambda { |failure, _opts| raise failure }
-
DEFAULT_WARNING_NOTIFIER =
Internal use only
# File 'rspec-support/lib/rspec/support.rb', line 137lambda { |warning| ::Kernel.warn warning }
-
KERNEL_METHOD_METHOD =
Internal use only
# File 'rspec-support/lib/rspec/support.rb', line 40::Kernel.instance_method(:method)
-
StrictSignatureVerifier =
Internal use only
Figures out whether a given method can accept various arguments. Surprisingly non-trivial.
MethodSignatureVerifier
Class Attribute Summary
- .failure_notifier rw Internal use only Internal use only
- .failure_notifier=(callable) rw Internal use only Internal use only
- .warning_notifier rw Internal use only Internal use only
- .warning_notifier=(value) rw Internal use only Internal use only
Class Method Summary
-
.class_of(object)
Internal use only
Internal use only
Used internally to get a class of a given object, even if it does not respond to #class.
-
.define_optimized_require_for_rspec(lib, &require_relative)
Internal use only
Internal use only
Defines a helper method that is optimized to require files from the named lib.
-
.deregister_matcher_definition(&block)
Internal use only
Internal use only
Remove a previously registered matcher.
- .is_a_matcher?(object) ⇒ Boolean Internal use only Internal use only
- .matcher_definitions Internal use only Internal use only
-
.method_handle_for(object, method_name)
See additional method definition at line 54.
- .notify_failure(failure, options = {}) Internal use only Internal use only
-
.register_matcher_definition(&block)
Internal use only
Internal use only
Used internally to break cyclic dependency between mocks, expectations, and support.
-
.rspec_description_for_object(object)
Internal use only
Internal use only
gives a string representation of an object for use in
::RSpec
descriptions. -
.thread_local_data
See additional method definition at line 95.
- .with_failure_notifier(callable) Internal use only Internal use only
Class Attribute Details
.failure_notifier (rw)
# File 'rspec-support/lib/rspec/support.rb', line 113
def self.failure_notifier thread_local_data[:failure_notifier] || DEFAULT_FAILURE_NOTIFIER end
.failure_notifier=(callable) (rw)
# File 'rspec-support/lib/rspec/support.rb', line 105
def self.failure_notifier=(callable) thread_local_data[:failure_notifier] = callable end
.warning_notifier (rw)
# File 'rspec-support/lib/rspec/support.rb', line 140
def self.warning_notifier @warning_notifier ||= DEFAULT_WARNING_NOTIFIER end
.warning_notifier=(value) (rw)
# File 'rspec-support/lib/rspec/support.rb', line 133
attr_writer :warning_notifier
Class Method Details
.class_of(object)
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 86
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
.define_optimized_require_for_rspec(lib, &require_relative)
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 16
def self.define_optimized_require_for_rspec(lib, &require_relative) name = "require_rspec_#{lib}" if RUBY_PLATFORM == 'java' && !Kernel.respond_to?(:require) # JRuby 9.1.17.0 has developed a regression for require (class << self; self; end).__send__(:define_method, name) do |f| Kernel.send(:require, "rspec/#{lib}/#{f}") end elsif Kernel.respond_to?(:require_relative) (class << self; self; end).__send__(:define_method, name) do |f| require_relative.call("#{lib}/#{f}") end else (class << self; self; end).__send__(:define_method, name) do |f| require "rspec/#{lib}/#{f}" end end end
.deregister_matcher_definition(&block)
Remove a previously registered matcher. Useful for cleaning up after yourself in specs.
# File 'rspec-support/lib/rspec/support/matcher_definition.rb', line 24
def self.deregister_matcher_definition(&block) matcher_definitions.delete(block) end
.is_a_matcher?(object) ⇒ Boolean
# 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
.matcher_definitions
# File 'rspec-support/lib/rspec/support/matcher_definition.rb', line 6
def self.matcher_definitions @matcher_definitions ||= [] end
.method_handle_for(object, method_name)
See additional method definition at line 54.
# File 'rspec-support/lib/rspec/support.rb', line 66
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
.notify_failure(failure, options = {})
# File 'rspec-support/lib/rspec/support.rb', line 118
def self.notify_failure(failure, ={}) failure_notifier.call(failure, ) end
.register_matcher_definition(&block)
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
# File 'rspec-support/lib/rspec/support/matcher_definition.rb', line 16
def self.register_matcher_definition(&block) matcher_definitions << block end
.rspec_description_for_object(object)
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
.thread_local_data
See additional method definition at line 95.
# File 'rspec-support/lib/rspec/support.rb', line 99
def self.thread_local_data Thread.current.thread_variable_get(:__rspec) || Thread.current.thread_variable_set(:__rspec, {}) end
.with_failure_notifier(callable)
# File 'rspec-support/lib/rspec/support.rb', line 123
def self.with_failure_notifier(callable) orig_notifier = failure_notifier self.failure_notifier = callable yield ensure self.failure_notifier = orig_notifier end