Navigation Menu

Skip to content

Commit

Permalink
move to talking to shared examples directly rather than on world
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed May 15, 2013
1 parent 0d7d1c3 commit 495a888
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/rspec/core/example_group.rb
Expand Up @@ -162,7 +162,7 @@ def self.include_examples(name, *args, &block)
# @private
def self.find_and_eval_shared(label, name, *args, &customization_block)
raise ArgumentError, "Could not find shared #{label} #{name.inspect}" unless
shared_block = world.shared_example_groups[name]
shared_block = shared_example_groups[name]

module_eval_with_args(*args, &shared_block)
module_eval(&customization_block) if customization_block
Expand Down
20 changes: 14 additions & 6 deletions lib/rspec/core/shared_example_group.rb
Expand Up @@ -43,6 +43,10 @@ def share_as(name, &block)
Registry.add_const(name, &block)
end

def shared_example_groups
@shared_example_groups ||= {}
end

# @private
#
# Used internally to manage the shared example groups and
Expand All @@ -59,7 +63,7 @@ def add_group(*args, &block)
if key? args.first
key = args.shift
warn_if_key_taken key, block
RSpec.world.shared_example_groups[key] = block
add_shared_example_group source, key, block
end

unless args.empty?
Expand Down Expand Up @@ -92,11 +96,15 @@ def self.included(kls)
end

shared_const = Object.const_set(name, mod)
RSpec.world.shared_example_groups[shared_const] = block
add_shared_example_group source, shared_const, block
end

private

def add_shared_example_group source, key, block
source.shared_example_groups[key] = block
end

def key? candidate
[String, Symbol, Module].any? { |cls| cls === candidate }
end
Expand All @@ -105,8 +113,8 @@ def raise_name_error
raise NameError, "The first argument (#{name}) to share_as must be a legal name for a constant not already in use."
end

def warn_if_key_taken key, new_block
return unless existing_block = example_block_for(key)
def warn_if_key_taken source, key, new_block
return unless existing_block = example_block_for(source,key)

Kernel.warn <<-WARNING.gsub(/^ +\|/, '')
|WARNING: Shared example group '#{key}' has been previously defined at:
Expand All @@ -121,8 +129,8 @@ def formatted_location block
block.source_location.join ":"
end

def example_block_for key
RSpec.world.shared_example_groups[key]
def example_block_for source, key
source.shared_example_groups[key]
end

def ensure_block_has_source_location(block, caller_line)
Expand Down

0 comments on commit 495a888

Please sign in to comment.