Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 112 lines (97 sloc) 3.289 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
31 def run(runner)
32 result = '.'
34c51c9 @jeremy Rubinious: setup/teardown override for miniunit
jeremy authored
33 begin
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
34 _run_setup_callbacks do
35 result = super
36 end
34c51c9 @jeremy Rubinious: setup/teardown override for miniunit
jeremy authored
37 rescue Exception => e
c44fb4c @jeremy Ruby 1.9: use method_name to work around miniunit API changes
jeremy authored
38 result = runner.puke(self.class, method_name, e)
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
39 ensure
40 begin
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
41 _run_teardown_callbacks
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
42 rescue Exception => e
c44fb4c @jeremy Ruby 1.9: use method_name to work around miniunit API changes
jeremy authored
43 result = runner.puke(self.class, method_name, e)
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
44 end
34c51c9 @jeremy Rubinious: setup/teardown override for miniunit
jeremy authored
45 end
18099b0 @jeremy Rework testing extensions to reflect the recent miniunit upheaval
jeremy authored
46 result
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
47 end
48 end
21e7b84 @josh Callbacks, DeprecatedCallbacks = NewCallbacks, Callbacks
josh authored
49
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniTest...
jeremy authored
50 module ForClassicTestUnit
51 # For compatibility with Ruby < 1.8.6
52 PASSTHROUGH_EXCEPTIONS = Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
53
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniTest...
jeremy authored
54 # This redefinition is unfortunate but test/unit shows us no alternative.
55 # Doubly unfortunate: hax to support Mocha's hax.
56 def run(result)
57 return if @method_name.to_s == "default_test"
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
58
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
59 mocha_counter = retrieve_mocha_counter(result)
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniTest...
jeremy authored
60 yield(Test::Unit::TestCase::STARTED, name)
61 @_result = result
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
62
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
63 begin
64 begin
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
65 _run_setup_callbacks do
66 setup
67 __send__(@method_name)
68 mocha_verify(mocha_counter) if mocha_counter
69 end
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
70 rescue Mocha::ExpectationError => e
71 add_failure(e.message, e.backtrace)
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
72 rescue Test::Unit::AssertionFailedError => e
73 add_failure(e.message, e.backtrace)
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniTest...
jeremy authored
74 rescue Exception => e
75 raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
76 add_error(e)
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
77 ensure
78 begin
79 teardown
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
80 _run_teardown_callbacks
efdc062 @jeremy Revert "Rewrite AS::TestCase setup/teardown as a single callback chain"
jeremy authored
81 rescue Test::Unit::AssertionFailedError => e
82 add_failure(e.message, e.backtrace)
83 rescue Exception => e
84 raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
85 add_error(e)
86 end
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
87 end
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniTest...
jeremy authored
88 ensure
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
89 mocha_teardown if mocha_counter
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
90 end
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
91
e7208d3 @jeremy Get ActiveSupport::TestCase working with classic Test::Unit and MiniTest...
jeremy authored
92 result.add_run
93 yield(Test::Unit::TestCase::FINISHED, name)
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
94 end
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
95
96 protected
97
98 def retrieve_mocha_counter(result) #:nodoc:
e1d4e78 @spastorino Removes unused vars
spastorino authored
99 if respond_to?(:mocha_verify) # using mocha
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
100 if defined?(Mocha::TestCaseAdapter::AssertionCounter)
101 Mocha::TestCaseAdapter::AssertionCounter.new(result)
102 else
103 Mocha::Integration::TestUnit::AssertionCounter.new(result)
104 end
105 end
106 end
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
107 end
e58ad8e @josevalim Setup and teardown now use new callbacks.
josevalim authored
108
139b924 @jeremy * Continue evolution toward ActiveSupport::TestCase and friends. #10679...
jeremy authored
109 end
110 end
111 end
Something went wrong with that request. Please try again.