]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/example/configuration.rb
added RSpec and RSpec on Rails
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / example / configuration.rb
1 module Spec
2   module Example
3     class Configuration
4       # Chooses what mock framework to use. Example:
5       #
6       #   Spec::Runner.configure do |config|
7       #     config.mock_with :rspec, :mocha, :flexmock, or :rr
8       #   end
9       #
10       # To use any other mock framework, you'll have to provide
11       # your own adapter. This is simply a module that responds to
12       # setup_mocks_for_rspec, verify_mocks_for_rspec and teardown_mocks_for_rspec.
13       # These are your hooks into the lifecycle of a given example. RSpec will
14       # call setup_mocks_for_rspec before running anything else in each Example.
15       # After executing the #after methods, RSpec will then call verify_mocks_for_rspec
16       # and teardown_mocks_for_rspec (this is guaranteed to run even if there are
17       # failures in verify_mocks_for_rspec).
18       #
19       # Once you've defined this module, you can pass that to mock_with:
20       #
21       #   Spec::Runner.configure do |config|
22       #     config.mock_with MyMockFrameworkAdapter
23       #   end
24       #
25       def mock_with(mock_framework)
26         @mock_framework = case mock_framework
27         when Symbol
28           mock_framework_path(mock_framework.to_s)
29         else
30           mock_framework
31         end
32       end
33       
34       def mock_framework # :nodoc:
35         @mock_framework ||= mock_framework_path("rspec")
36       end
37       
38       # Declares modules to be included in all example groups (<tt>describe</tt> blocks).
39       #
40       #   config.include(My::Bottle, My::Cup)
41       #
42       # If you want to restrict the inclusion to a subset of all the example groups then
43       # specify this in a Hash as the last argument:
44       #
45       #   config.include(My::Pony, My::Horse, :type => :farm)
46       #
47       # Only example groups that have that type will get the modules included:
48       #
49       #   describe "Downtown", :type => :city do
50       #     # Will *not* get My::Pony and My::Horse included
51       #   end
52       #
53       #   describe "Old Mac Donald", :type => :farm do
54       #     # *Will* get My::Pony and My::Horse included
55       #   end
56       #
57       def include(*args)
58         args << {} unless Hash === args.last
59         modules, options = args_and_options(*args)
60         required_example_group = get_type_from_options(options)
61         required_example_group = required_example_group.to_sym if required_example_group
62         modules.each do |mod|
63           ExampleGroupFactory.get(required_example_group).send(:include, mod)
64         end
65       end
66
67       # Defines global predicate matchers. Example:
68       #
69       #   config.predicate_matchers[:swim] = :can_swim?
70       #
71       # This makes it possible to say:
72       #
73       #   person.should swim # passes if person.should_swim? returns true
74       #
75       def predicate_matchers
76         @predicate_matchers ||= {}
77       end
78       
79       # Prepends a global <tt>before</tt> block to all example groups.
80       # See #append_before for filtering semantics.
81       def prepend_before(*args, &proc)
82         scope, options = scope_and_options(*args)
83         example_group = ExampleGroupFactory.get(
84           get_type_from_options(options)
85         )
86         example_group.prepend_before(scope, &proc)
87       end
88       # Appends a global <tt>before</tt> block to all example groups.
89       #
90       # If you want to restrict the block to a subset of all the example groups then
91       # specify this in a Hash as the last argument:
92       #
93       #   config.prepend_before(:all, :type => :farm)
94       #
95       # or
96       #
97       #   config.prepend_before(:type => :farm)
98       #
99       def append_before(*args, &proc)
100         scope, options = scope_and_options(*args)
101         example_group = ExampleGroupFactory.get(
102           get_type_from_options(options)
103         )
104         example_group.append_before(scope, &proc)
105       end
106       alias_method :before, :append_before
107
108       # Prepends a global <tt>after</tt> block to all example groups.
109       # See #append_before for filtering semantics.
110       def prepend_after(*args, &proc)
111         scope, options = scope_and_options(*args)
112         example_group = ExampleGroupFactory.get(
113           get_type_from_options(options)
114         )
115         example_group.prepend_after(scope, &proc)
116       end
117       alias_method :after, :prepend_after
118       # Appends a global <tt>after</tt> block to all example groups.
119       # See #append_before for filtering semantics.
120       def append_after(*args, &proc)
121         scope, options = scope_and_options(*args)
122         example_group = ExampleGroupFactory.get(
123           get_type_from_options(options)
124         )
125         example_group.append_after(scope, &proc)
126       end
127
128     private
129
130       def scope_and_options(*args)
131         args, options = args_and_options(*args)
132         scope = (args[0] || :each), options
133       end
134
135       def get_type_from_options(options)
136         options[:type] || options[:behaviour_type]
137       end
138     
139       def mock_framework_path(framework_name)
140         File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "plugins", "mock_frameworks", framework_name))
141       end
142     end
143   end
144 end