public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Get ActiveSupport::TestCase working with classic Test::Unit and MiniTest. Fix 
broken Mocha + MiniTest. Assume ruby-core applies patch #771 fixing libraries 
which extend Test::Unit.
jeremy (author)
Sat Nov 22 19:18:30 -0800 2008
commit  e7208d382a3d8bae9ab13d8a380b1a2a05fd99b0
tree    ea69c208bfce8f360189ffd95eedf9f550e65810
parent  c79fb32e93c7ed9d5c0f39194ee48183faf3b5c9
...
 
1
2
3
4
5
6
7
8
 
 
 
9
10
11
12
13
14
15
16
17
18
 
 
 
 
 
 
 
 
19
20
 
 
 
21
22
23
24
25
26
27
28
29
30
31
32
33
 
 
 
 
 
 
34
35
36
 
 
37
38
39
 
40
41
42
43
44
45
46
47
 
...
1
2
3
4
5
 
 
 
 
6
7
8
9
 
 
 
 
 
 
 
 
 
10
11
12
13
14
15
16
17
18
 
19
20
21
22
 
 
 
 
 
 
 
 
 
 
 
 
23
24
25
26
27
28
29
 
30
31
32
33
 
 
34
35
 
36
37
38
39
 
40
41
0
@@ -1,46 +1,40 @@
0
+require 'test/unit/testcase'
0
 require 'active_support/testing/setup_and_teardown'
0
 require 'active_support/testing/assertions'
0
 require 'active_support/testing/declarative'
0
 
0
-module ActiveSupport
0
-  # Prefer MiniTest with Test::Unit compatibility.
0
-  begin
0
-    require 'minitest/unit'
0
+begin
0
+  gem 'mocha', '>= 0.9.0'
0
+  require 'mocha'
0
 
0
-    # Hack around the test/unit autorun.
0
-    autorun_enabled = MiniTest::Unit.send(:class_variable_get, '@@installed_at_exit')
0
-    if MiniTest::Unit.respond_to?(:disable_autorun)
0
-      MiniTest::Unit.disable_autorun
0
-    else
0
-      MiniTest::Unit.send(:class_variable_set, '@@installed_at_exit', false)
0
-    end
0
-    require 'test/unit'
0
-    MiniTest::Unit.send(:class_variable_set, '@@installed_at_exit', autorun_enabled)
0
+  if defined?(MiniTest)
0
+    require 'active_support/testing/mocha_minitest_adapter'
0
+  end
0
+rescue LoadError
0
+  # Fake Mocha::ExpectationError so we can rescue it in #run. Bleh.
0
+  Object.const_set :Mocha, Module.new
0
+  Mocha.const_set :ExpectationError, Class.new(StandardError)
0
+end
0
 
0
-    class TestCase < ::Test::Unit::TestCase
0
+module ActiveSupport
0
+  class TestCase < ::Test::Unit::TestCase
0
+    if defined? MiniTest
0
       Assertion = MiniTest::Assertion
0
-    end
0
-
0
-    # TODO: Figure out how to get the Rails::BacktraceFilter into minitest/unit
0
-  # Test::Unit compatibility.
0
-  rescue LoadError
0
-    require 'test/unit/testcase'
0
-    require 'active_support/testing/default'
0
-
0
-    if defined?(Rails)
0
-      require 'rails/backtrace_cleaner'
0
-      Test::Unit::Util::BacktraceFilter.module_eval { include Rails::BacktraceFilterForTestUnit }
0
-    end
0
+    else
0
+      # TODO: Figure out how to get the Rails::BacktraceFilter into minitest/unit
0
+      if defined?(Rails)
0
+        require 'rails/backtrace_cleaner'
0
+        Test::Unit::Util::BacktraceFilter.module_eval { include Rails::BacktraceFilterForTestUnit }
0
+      end
0
 
0
-    class TestCase < ::Test::Unit::TestCase
0
       Assertion = Test::Unit::AssertionFailedError
0
+
0
+      require 'active_support/testing/default'
0
       include ActiveSupport::Testing::Default
0
-    end    
0
-  end
0
+    end
0
 
0
-  class TestCase
0
     include ActiveSupport::Testing::SetupAndTeardown
0
     include ActiveSupport::Testing::Assertions
0
     extend ActiveSupport::Testing::Declarative
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
20
21
22
23
24
25
26
27
28
29
 
30
31
32
...
50
51
52
53
54
55
 
 
 
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
 
 
 
84
85
86
87
 
 
 
88
89
90
91
92
 
 
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
 
 
 
 
 
 
109
110
111
112
113
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
 
115
116
117
 
 
118
119
120
121
122
123
...
3
4
5
 
 
 
 
 
 
 
 
6
7
8
9
10
 
11
12
13
 
 
 
 
 
 
 
 
14
15
16
17
...
35
36
37
 
 
 
38
39
40
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
43
44
45
46
 
 
 
47
48
49
50
 
 
 
 
51
52
53
 
 
 
 
 
 
 
 
 
 
 
 
54
 
 
55
56
57
58
59
60
61
62
 
 
 
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
 
80
81
82
 
 
83
84
85
0
@@ -3,30 +3,15 @@ require 'active_support/callbacks'
0
 module ActiveSupport
0
   module Testing
0
     module SetupAndTeardown
0
-      # For compatibility with Ruby < 1.8.6
0
-      PASSTHROUGH_EXCEPTIONS =
0
-        if defined?(Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS)
0
-          Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
0
-        else
0
-          [NoMemoryError, SignalException, Interrupt, SystemExit]
0
-        end
0
-
0
       def self.included(base)
0
         base.class_eval do
0
           include ActiveSupport::Callbacks
0
           define_callbacks :setup, :teardown
0
 
0
-          if defined?(::MiniTest)
0
+          if defined? MiniTest
0
             include ForMiniTest
0
           else
0
-            begin
0
-              require 'mocha'
0
-              undef_method :run
0
-              alias_method :run, :run_with_callbacks_and_mocha
0
-            rescue LoadError
0
-              undef_method :run
0
-              alias_method :run, :run_with_callbacks_and_testunit
0
-            end
0
+            include ForClassicTestUnit
0
           end
0
         end
0
       end
0
@@ -50,74 +35,51 @@ module ActiveSupport
0
         end
0
       end
0
 
0
-      # This redefinition is unfortunate but test/unit shows us no alternative.
0
-      def run_with_callbacks_and_testunit(result) #:nodoc:
0
-        return if @method_name.to_s == "default_test"
0
+      module ForClassicTestUnit
0
+        # For compatibility with Ruby < 1.8.6
0
+        PASSTHROUGH_EXCEPTIONS = Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
0
 
0
-        yield(Test::Unit::TestCase::STARTED, name)
0
-        @_result = result
0
-        begin
0
-          run_callbacks :setup
0
-          setup
0
-          __send__(@method_name)
0
-        rescue Test::Unit::AssertionFailedError => e
0
-          add_failure(e.message, e.backtrace)
0
-        rescue *PASSTHROUGH_EXCEPTIONS
0
-          raise
0
-        rescue Exception
0
-          add_error($!)
0
-        ensure
0
-          begin
0
-            teardown
0
-            run_callbacks :teardown, :enumerator => :reverse_each
0
-          rescue Test::Unit::AssertionFailedError => e
0
-            add_failure(e.message, e.backtrace)
0
-          rescue *PASSTHROUGH_EXCEPTIONS
0
-            raise
0
-          rescue Exception
0
-            add_error($!)
0
-          end
0
-        end
0
-        result.add_run
0
-        yield(Test::Unit::TestCase::FINISHED, name)
0
-      end
0
+        # This redefinition is unfortunate but test/unit shows us no alternative.
0
+        # Doubly unfortunate: hax to support Mocha's hax.
0
+        def run(result)
0
+          return if @method_name.to_s == "default_test"
0
 
0
-      # Doubly unfortunate: mocha does the same so we have to hax their hax.
0
-      def run_with_callbacks_and_mocha(result)
0
-        return if @method_name.to_s == "default_test"
0
+          if using_mocha = respond_to?(:mocha_verify)
0
+            assertion_counter = Mocha::TestCaseAdapter::AssertionCounter.new(result)
0
+          end
0
 
0
-        assertion_counter = Mocha::TestCaseAdapter::AssertionCounter.new(result)
0
-        yield(Test::Unit::TestCase::STARTED, name)
0
-        @_result = result
0
-        begin
0
+          yield(Test::Unit::TestCase::STARTED, name)
0
+          @_result = result
0
           begin
0
-            run_callbacks :setup
0
-            setup
0
-            __send__(@method_name)
0
-            mocha_verify(assertion_counter)
0
-          rescue Mocha::ExpectationError => e
0
-            add_failure(e.message, e.backtrace)
0
-          rescue Test::Unit::AssertionFailedError => e
0
-            add_failure(e.message, e.backtrace)
0
-          rescue Exception
0
-            raise if Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.include? $!.class
0
-            add_error($!)
0
-          ensure
0
             begin
0
-              teardown
0
-              run_callbacks :teardown, :enumerator => :reverse_each
0
+              run_callbacks :setup
0
+              setup
0
+              __send__(@method_name)
0
+              mocha_verify(assertion_counter) if using_mocha
0
+            rescue Mocha::ExpectationError => e
0
+              add_failure(e.message, e.backtrace)
0
             rescue Test::Unit::AssertionFailedError => e
0
               add_failure(e.message, e.backtrace)
0
-            rescue Exception
0
-              raise if Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.include? $!.class
0
-              add_error($!)
0
+            rescue Exception => e
0
+              raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
0
+              add_error(e)
0
+            ensure
0
+              begin
0
+                teardown
0
+                run_callbacks :teardown, :enumerator => :reverse_each
0
+              rescue Test::Unit::AssertionFailedError => e
0
+                add_failure(e.message, e.backtrace)
0
+              rescue Exception => e
0
+                raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
0
+                add_error(e)
0
+              end
0
             end
0
+          ensure
0
+            mocha_teardown if using_mocha
0
           end
0
-        ensure
0
-          mocha_teardown
0
+          result.add_run
0
+          yield(Test::Unit::TestCase::FINISHED, name)
0
         end
0
-        result.add_run
0
-        yield(Test::Unit::TestCase::FINISHED, name)
0
       end
0
     end
0
   end

Comments