]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/example/example_group_factory.rb
Show whether a trace is public or private in the trace list, so that a user can easil...
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / example / example_group_factory.rb
1 module Spec
2   module Example
3     class ExampleGroupFactory
4       class << self
5         def reset
6           @example_group_types = nil
7           default(ExampleGroup)
8         end
9
10         # Registers an example group class +klass+ with the symbol
11         # +type+. For example:
12         #
13         #   Spec::Example::ExampleGroupFactory.register(:farm, Spec::Farm::Example::FarmExampleGroup)
14         #
15         # This will cause Main#describe from a file living in 
16         # <tt>spec/farm</tt> to create example group instances of type
17         # Spec::Farm::Example::FarmExampleGroup.
18         def register(id, example_group_class)
19           @example_group_types[id] = example_group_class
20         end
21         
22         # Sets the default ExampleGroup class
23         def default(example_group_class)
24           old = @example_group_types
25           @example_group_types = Hash.new(example_group_class)
26           @example_group_types.merge(old) if old
27         end
28
29         def get(id=nil)
30           if @example_group_types.values.include?(id)
31             id
32           else
33             @example_group_types[id]
34           end
35         end
36         
37         def create_example_group(*args, &block)
38           opts = Hash === args.last ? args.last : {}
39           if opts[:shared]
40             SharedExampleGroup.new(*args, &block)
41           else
42             superclass = determine_superclass(opts)
43             superclass.describe(*args, &block)
44           end
45         end
46
47         protected
48
49         def determine_superclass(opts)
50           id = if opts[:type]
51             opts[:type]
52           elsif opts[:spec_path] =~ /spec(\\|\/)(#{@example_group_types.keys.join('|')})/
53             $2 == '' ? nil : $2.to_sym
54           end
55           get(id)
56         end
57
58       end
59       self.reset
60     end
61   end
62 end