module RSpec::SleepingKingStudios::Deferred::Provider::ClassMethods
Class methods for registering deferred examples.
Public Instance Methods
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 42 def deferred_examples(description, &block) raise ArgumentError, 'block is required' unless block_given? tools.assertions.validate_name(description, as: 'description') defined_deferred_examples[description.to_s] = block nil end
Defines deferred examples in the current context.
@param description [String] the name of the deferred examples.
@yield [*arguments, **keywords, &block] the definition for the deferred
examples. Supports the same DSL as an RSpec::Core::ExampleGroup. If the block takes parameters, these can be used to customize the behavior of the deferred examples when they are included in an example group.
@yieldparam arguments [Array] arguments passed to the deferred examples. @yieldparam keywords [Hash] keywords passed to the deferred examples. @yieldparam block [Block] a block passed to the deferred examples.
@example Defining Deferred
Examples
deferred_examples 'should be a Rocket' do it { expect(subject).to be_a Rocket } end
@example Defining Parameterized Examples
deferred_examples 'should be a Vehicle' do |expected_type:| it { expect(subject).to be_a Vehicle } it { expect(subject.tyoe).to be == expected_type } end
Also aliased as: deferred_context
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 54 def defined_deferred_examples @defined_deferred_examples ||= {} end
@private
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 64 def defined_deferred_examples?(description) ancestors.any? do |ancestor| next false unless ancestor.respond_to?(:defined_deferred_examples) ancestor.deferred_definition_exists?(description) || ancestor.deferred_module_exists?(description) end end
Checks if the given deferred example group is defined.
@param description [String] the name of the deferred examples.
@return [true, false] true if a deferred example group with the given
description is defined in the current context; otherwise false.
Also aliased as: defined_deferred_context?
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 75 def find_deferred_examples(description) tools.assertions.validate_name(description, as: 'description') deferred = find_deferred_by_description(description.to_s) return deferred if deferred message = 'deferred examples not found with description ' \ "#{description.to_s.inspect}" raise DeferredExamplesNotFoundError, message end
@api private
Protected Instance Methods
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 91 def deferred_definition_exists?(description) defined_deferred_examples.key?(description) end
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 95 def deferred_module_exists?(description) constants(false) .any? do |const_name| value = const_get(const_name) next false unless module_is_deferred_examples?(value) return true if matches_description?(description, const_name, value) end end
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 106 def find_deferred_definition(description) defined_deferred_examples.fetch(description, nil) end
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 110 def find_deferred_module(description) constants(false) .each do |const_name| value = const_get(const_name) next false unless module_is_deferred_examples?(value) return value if matches_description?(description, const_name, value) end nil end
Private Instance Methods
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 125 def find_deferred_by_description(description) ancestors.each do |ancestor| next unless ancestor.respond_to?(:defined_deferred_examples) deferred = ancestor.find_deferred_definition(description) || ancestor.find_deferred_module(description) return deferred if deferred end nil end
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 139 def matches_description?(description, const_name, value) return true if value.description == description const_name = const_name.to_s return true if const_name == description const_name = const_name.gsub(/(Context|Examples?)\z/, '') const_name == description end
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 151 def module_is_deferred_examples?(value) return false unless value.is_a?(Module) return false unless value.respond_to?(:deferred_examples?) value.deferred_examples? end
Source
# File lib/rspec/sleeping_king_studios/deferred/provider.rb, line 159 def tools SleepingKingStudios::Tools::Toolbelt.instance end