changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/lib/spec/example/example_methods.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1module 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 def implementation_backtrace
86 eval("caller", @_implementation)
87 end
88
89 protected
90 include Matchers
91 include Pending
92
93 def before_example
94 setup_mocks_for_rspec
95 self.class.run_before_each(self)
96 end
97
98 def after_example
99 self.class.run_after_each(self)
100 verify_mocks_for_rspec
101 ensure
102 teardown_mocks_for_rspec
103 end
104 end
105 end
106end