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