]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/example/shared_example_group.rb
added RSpec and RSpec on Rails
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / example / shared_example_group.rb
1 module Spec
2   module Example
3     class SharedExampleGroup < Module
4       class << self
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
8         end
9
10         def find_shared_example_group(example_group_description)
11           shared_example_groups.find do |b|
12             b.description == example_group_description
13           end
14         end
15
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 ||= []
21         end
22
23         private
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")
30         end
31
32         def spec_path(example_group)
33           File.expand_path(example_group.spec_path)
34         end
35       end
36       include ExampleGroupMethods
37       public :include
38
39       def initialize(*args, &example_group_block)
40         describe(*args)
41         @example_group_block = example_group_block
42         self.class.add_shared_example_group(self)
43       end
44
45       def included(mod) # :nodoc:
46         mod.module_eval(&@example_group_block)
47       end
48
49       def execute_in_class_hierarchy(superclass_last=false)
50         classes = [self]
51         superclass_last ? classes << ExampleMethods : classes.unshift(ExampleMethods)
52         classes.each do |example_group|
53           yield example_group
54         end
55       end
56     end
57   end
58 end