3 class SharedExampleGroup < Module
5 def add_shared_example_group(new_example_group)
6 guard_against_redefining_existing_example_group(new_example_group)
7 shared_example_groups << new_example_group
10 def find_shared_example_group(example_group_description)
11 shared_example_groups.find do |b|
12 b.description == example_group_description
16 def shared_example_groups
17 # TODO - this needs to be global, or at least accessible from
18 # from subclasses of Example in a centralized place. I'm not loving
19 # this as a solution, but it works for now.
20 $shared_example_groups ||= []
24 def guard_against_redefining_existing_example_group(new_example_group)
25 existing_example_group = find_shared_example_group(new_example_group.description)
26 return unless existing_example_group
27 return if new_example_group.equal?(existing_example_group)
28 return if spec_path(new_example_group) == spec_path(existing_example_group)
29 raise ArgumentError.new("Shared Example '#{existing_example_group.description}' already exists")
32 def spec_path(example_group)
33 File.expand_path(example_group.spec_path)
36 include ExampleGroupMethods
39 def initialize(*args, &example_group_block)
41 @example_group_block = example_group_block
42 self.class.add_shared_example_group(self)
45 def included(mod) # :nodoc:
46 mod.module_eval(&@example_group_block)
49 def execute_in_class_hierarchy(superclass_last=false)
51 superclass_last ? classes << ExampleMethods : classes.unshift(ExampleMethods)
52 classes.each do |example_group|