Froze rails gems
[depot.git] / vendor / rails / activesupport / lib / active_support / testing / setup_and_teardown.rb
1 module ActiveSupport
2 module Testing
3 module SetupAndTeardown
4 # For compatibility with Ruby < 1.8.6
5 PASSTHROUGH_EXCEPTIONS =
6 if defined?(Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS)
7 Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
8 else
9 [NoMemoryError, SignalException, Interrupt, SystemExit]
10 end
11
12 def self.included(base)
13 base.class_eval do
14 include ActiveSupport::Callbacks
15 define_callbacks :setup, :teardown
16
17 if defined?(::Mini)
18 undef_method :run
19 alias_method :run, :run_with_callbacks_and_miniunit
20 else
21 begin
22 require 'mocha'
23 undef_method :run
24 alias_method :run, :run_with_callbacks_and_mocha
25 rescue LoadError
26 undef_method :run
27 alias_method :run, :run_with_callbacks_and_testunit
28 end
29 end
30 end
31 end
32
33 def run_with_callbacks_and_miniunit(runner)
34 result = '.'
35 begin
36 run_callbacks :setup
37 result = super
38 rescue Exception => e
39 result = runner.puke(self.class, self.name, e)
40 ensure
41 begin
42 teardown
43 run_callbacks :teardown, :enumerator => :reverse_each
44 rescue Exception => e
45 result = runner.puke(self.class, self.name, e)
46 end
47 end
48 result
49 end
50
51 # This redefinition is unfortunate but test/unit shows us no alternative.
52 def run_with_callbacks_and_testunit(result) #:nodoc:
53 return if @method_name.to_s == "default_test"
54
55 yield(Test::Unit::TestCase::STARTED, name)
56 @_result = result
57 begin
58 run_callbacks :setup
59 setup
60 __send__(@method_name)
61 rescue Test::Unit::AssertionFailedError => e
62 add_failure(e.message, e.backtrace)
63 rescue *PASSTHROUGH_EXCEPTIONS
64 raise
65 rescue Exception
66 add_error($!)
67 ensure
68 begin
69 teardown
70 run_callbacks :teardown, :enumerator => :reverse_each
71 rescue Test::Unit::AssertionFailedError => e
72 add_failure(e.message, e.backtrace)
73 rescue *PASSTHROUGH_EXCEPTIONS
74 raise
75 rescue Exception
76 add_error($!)
77 end
78 end
79 result.add_run
80 yield(Test::Unit::TestCase::FINISHED, name)
81 end
82
83 # Doubly unfortunate: mocha does the same so we have to hax their hax.
84 def run_with_callbacks_and_mocha(result)
85 return if @method_name.to_s == "default_test"
86
87 yield(Test::Unit::TestCase::STARTED, name)
88 @_result = result
89 begin
90 mocha_setup
91 begin
92 run_callbacks :setup
93 setup
94 __send__(@method_name)
95 mocha_verify { add_assertion }
96 rescue Mocha::ExpectationError => e
97 add_failure(e.message, e.backtrace)
98 rescue Test::Unit::AssertionFailedError => e
99 add_failure(e.message, e.backtrace)
100 rescue StandardError, ScriptError
101 add_error($!)
102 ensure
103 begin
104 teardown
105 run_callbacks :teardown, :enumerator => :reverse_each
106 rescue Test::Unit::AssertionFailedError => e
107 add_failure(e.message, e.backtrace)
108 rescue StandardError, ScriptError
109 add_error($!)
110 end
111 end
112 ensure
113 mocha_teardown
114 end
115 result.add_run
116 yield(Test::Unit::TestCase::FINISHED, name)
117 end
118 end
119 end
120 end