Blob of rspec/lib/spec/example/example_group_factory.rb (raw blob data)

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 +type+. For
11 # example:
12 #
13 # Spec::Example::ExampleGroupFactory.register(:farm, FarmExampleGroup)
14 #
15 # With that you can append a hash with :type => :farm to the describe
16 # method and it will load an instance of FarmExampleGroup.
17 #
18 # describe Pig, :type => :farm do
19 # ...
20 #
21 # If you don't use the hash explicitly, <tt>describe</tt> will
22 # implicitly use an instance of FarmExampleGroup for any file loaded
23 # from the <tt>./spec/farm</tt> directory.
24 def register(key, example_group_class)
25 @example_group_types[key] = example_group_class
26 end
27
28 # Sets the default ExampleGroup class
29 def default(example_group_class)
30 old = @example_group_types
31 @example_group_types = Hash.new(example_group_class)
32 @example_group_types.merge!(old) if old
33 end
34
35 def get(key=nil)
36 if @example_group_types.values.include?(key)
37 key
38 else
39 @example_group_types[key]
40 end
41 end
42
43 def create_example_group(*args, &block)
44 opts = Hash === args.last ? args.last : {}
45 superclass = determine_superclass(opts)
46 superclass.describe(*args, &block)
47 end
48
49 protected
50
51 def determine_superclass(opts)
52 key = if opts[:type]
53 opts[:type]
54 elsif opts[:spec_path] =~ /spec(\\|\/)(#{@example_group_types.keys.join('|')})/
55 $2 == '' ? nil : $2.to_sym
56 end
57 get(key)
58 end
59
60 end
61 self.reset
62 end
63 end
64 end