]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/example/example_methods.rb
6dd4c9c72c6aa97cfa4d3cba27d941b5bf4f7758
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / example / example_methods.rb
1 module Spec
2   module Example
3     module ExampleMethods
4       extend ExampleGroupMethods
5       extend ModuleReopeningFix
6
7       PENDING_EXAMPLE_BLOCK = lambda {
8         raise Spec::Example::ExamplePendingError.new("Not Yet Implemented")
9       }
10
11       def execute(options, instance_variables)
12         options.reporter.example_started(self)
13         set_instance_variables_from_hash(instance_variables)
14         
15         execution_error = nil
16         Timeout.timeout(options.timeout) do
17           begin
18             before_example
19             run_with_description_capturing
20           rescue Exception => e
21             execution_error ||= e
22           end
23           begin
24             after_example
25           rescue Exception => e
26             execution_error ||= e
27           end
28         end
29
30         options.reporter.example_finished(self, execution_error)
31         success = execution_error.nil? || ExamplePendingError === execution_error
32       end
33
34       def instance_variable_hash
35         instance_variables.inject({}) do |variable_hash, variable_name|
36           variable_hash[variable_name] = instance_variable_get(variable_name)
37           variable_hash
38         end
39       end
40
41       def violated(message="")
42         raise Spec::Expectations::ExpectationNotMetError.new(message)
43       end
44
45       def eval_each_fail_fast(procs) #:nodoc:
46         procs.each do |proc|
47           instance_eval(&proc)
48         end
49       end
50
51       def eval_each_fail_slow(procs) #:nodoc:
52         first_exception = nil
53         procs.each do |proc|
54           begin
55             instance_eval(&proc)
56           rescue Exception => e
57             first_exception ||= e
58           end
59         end
60         raise first_exception if first_exception
61       end
62
63       def description
64         @_defined_description || @_matcher_description || "NO NAME"
65       end
66       
67       def set_instance_variables_from_hash(ivars)
68         ivars.each do |variable_name, value|
69           # Ruby 1.9 requires variable.to_s on the next line
70           unless ['@_implementation', '@_defined_description', '@_matcher_description', '@method_name'].include?(variable_name.to_s)
71             instance_variable_set variable_name, value
72           end
73         end
74       end
75
76       def run_with_description_capturing
77         begin
78           return instance_eval(&(@_implementation || PENDING_EXAMPLE_BLOCK))
79         ensure
80           @_matcher_description = Spec::Matchers.generated_description
81           Spec::Matchers.clear_generated_description
82         end
83       end
84       
85       protected
86       include Matchers
87       include Pending
88       
89       def before_example
90         setup_mocks_for_rspec
91         self.class.run_before_each(self)
92       end
93
94       def after_example
95         self.class.run_after_each(self)
96         verify_mocks_for_rspec
97       ensure
98         teardown_mocks_for_rspec
99       end
100     end
101   end
102 end