Skip to content
Newer
Older
100644 120 lines (106 sloc) 3.74 KB
a90a076 @spastorino Missing requires added
spastorino authored
1 require 'active_support/concern'
2 require 'active_support/callbacks'
3
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
4 module ActiveSupport
5 module Testing
6 module SetupAndTeardown
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
7 extend ActiveSupport::Concern
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
8
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
9 included do
10 include ActiveSupport::Callbacks
11 define_callbacks :setup, :teardown
12
13 if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
14 include ForMiniTest
15 else
16 include ForClassicTestUnit
17 end
18 end
19
20 module ClassMethods
21 def setup(*args, &block)
22 set_callback(:setup, :before, *args, &block)
23 end
24
25 def teardown(*args, &block)
26 set_callback(:teardown, :after, *args, &block)
34c51c9 @jeremy Rubinious: setup/teardown override for miniunit
jeremy authored
27 end
28 end
29
18099b0 @jeremy Rework testing extensions to reflect the recent miniunit upheaval
jeremy authored
30 module ForMiniTest
4cd3285 @floehopper Exceptions like Interrupt should not be rescued in tests.
floehopper authored
31 PASSTHROUGH_EXCEPTIONS = MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
18099b0 @jeremy Rework testing extensions to reflect the recent miniunit upheaval
jeremy authored
32 def run(runner)
33 result = '.'
34c51c9 @jeremy Rubinious: setup/teardown override for miniunit
jeremy authored
34 begin
57bc25c @jfirebaugh Use run_callbacks; the generated _run_<name>_callbacks method is not …
jfirebaugh authored
35 run_callbacks :setup do
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
36 result = super
37 end
4cd3285 @floehopper Exceptions like Interrupt should not be rescued in tests.
floehopper authored
38 rescue *PASSTHROUGH_EXCEPTIONS => e
39 raise e
34c51c9 @jeremy Rubinious: setup/teardown override for miniunit
jeremy authored
40 rescue Exception => e
c44fb4c @jeremy Ruby 1.9: use method_name to work around miniunit API changes
jeremy authored
41 result = runner.puke(self.class, method_name, e)
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
42 ensure
43 begin
57bc25c @jfirebaugh Use run_callbacks; the generated _run_<name>_callbacks method is not …
jfirebaugh authored
44 run_callbacks :teardown
4cd3285 @floehopper Exceptions like Interrupt should not be rescued in tests.
floehopper authored
45 rescue *PASSTHROUGH_EXCEPTIONS => e
46 raise e
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
47 rescue Exception => e
c44fb4c @jeremy Ruby 1.9: use method_name to work around miniunit API changes
jeremy authored
48 result = runner.puke(self.class, method_name, e)
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
49 end
34c51c9 @jeremy Rubinious: setup/teardown override for miniunit
jeremy authored
50 end
18099b0 @jeremy Rework testing extensions to reflect the recent miniunit upheaval
jeremy authored
51 result
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
52 end
53 end
21e7b84 @josh Callbacks, DeprecatedCallbacks = NewCallbacks, Callbacks
josh authored
54
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniT…
jeremy authored
55 module ForClassicTestUnit
56 # For compatibility with Ruby < 1.8.6
57 PASSTHROUGH_EXCEPTIONS = Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
58
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniT…
jeremy authored
59 # This redefinition is unfortunate but test/unit shows us no alternative.
60 # Doubly unfortunate: hax to support Mocha's hax.
61 def run(result)
62 return if @method_name.to_s == "default_test"
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
63
0591f6d @floehopper Fix 3-1-stable to work with Mocha >= v0.13.0
floehopper authored
64 mocha_counter = retrieve_mocha_counter(self, result)
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniT…
jeremy authored
65 yield(Test::Unit::TestCase::STARTED, name)
66 @_result = result
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
67
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
68 begin
69 begin
57bc25c @jfirebaugh Use run_callbacks; the generated _run_<name>_callbacks method is not …
jfirebaugh authored
70 run_callbacks :setup do
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
71 setup
72 __send__(@method_name)
73 mocha_verify(mocha_counter) if mocha_counter
74 end
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
75 rescue Mocha::ExpectationError => e
76 add_failure(e.message, e.backtrace)
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
77 rescue Test::Unit::AssertionFailedError => e
78 add_failure(e.message, e.backtrace)
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniT…
jeremy authored
79 rescue Exception => e
80 raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
81 add_error(e)
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
82 ensure
83 begin
84 teardown
57bc25c @jfirebaugh Use run_callbacks; the generated _run_<name>_callbacks method is not …
jfirebaugh authored
85 run_callbacks :teardown
0591f6d @floehopper Fix 3-1-stable to work with Mocha >= v0.13.0
floehopper authored
86 rescue Mocha::ExpectationError => e
87 add_failure(e.message, e.backtrace)
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
88 rescue Test::Unit::AssertionFailedError => e
89 add_failure(e.message, e.backtrace)
90 rescue Exception => e
91 raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
92 add_error(e)
93 end
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
94 end
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniT…
jeremy authored
95 ensure
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
96 mocha_teardown if mocha_counter
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
97 end
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
98
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniT…
jeremy authored
99 result.add_run
100 yield(Test::Unit::TestCase::FINISHED, name)
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
101 end
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
102
103 protected
104
0591f6d @floehopper Fix 3-1-stable to work with Mocha >= v0.13.0
floehopper authored
105 def retrieve_mocha_counter(test_case, result) #:nodoc:
e1d4e78 @spastorino Removes unused vars
spastorino authored
106 if respond_to?(:mocha_verify) # using mocha
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
107 if defined?(Mocha::TestCaseAdapter::AssertionCounter)
108 Mocha::TestCaseAdapter::AssertionCounter.new(result)
0591f6d @floehopper Fix 3-1-stable to work with Mocha >= v0.13.0
floehopper authored
109 elsif defined?(Mocha::Integration::TestUnit::AssertionCounter)
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
110 Mocha::Integration::TestUnit::AssertionCounter.new(result)
0591f6d @floehopper Fix 3-1-stable to work with Mocha >= v0.13.0
floehopper authored
111 else
112 Mocha::Integration::AssertionCounter.new(test_case)
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
113 end
114 end
115 end
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679
jeremy authored
116 end
117 end
118 end
119 end
Something went wrong with that request. Please try again.