Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source
Show
Ignore:
Timestamp:
01/19/08 02:44:45 (8 months ago)
Author:
bitsweat
Message:

Extract ActiveSupport::Callbacks from Active Record, test case setup and teardown, and ActionController::Dispatcher. Closes #10727.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/testing/setup_and_teardown.rb

    r8570 r8664  
    33    module SetupAndTeardown 
    44      def self.included(base) 
    5         base.extend ClassMethods 
     5        base.send :include, ActiveSupport::Callbacks 
     6        base.define_callbacks :setup, :teardown 
    67 
    78        begin 
     
    1011        rescue LoadError 
    1112          base.alias_method_chain :run, :callbacks 
    12         end 
    13       end 
    14  
    15       module ClassMethods 
    16         def setup(*method_names, &block) 
    17           method_names << block if block_given? 
    18           (@setup_callbacks ||= []).concat method_names 
    19         end 
    20  
    21         def teardown(*method_names, &block) 
    22           method_names << block if block_given? 
    23           (@teardown_callbacks ||= []).concat method_names 
    24         end 
    25  
    26         def setup_callback_chain 
    27           @setup_callbacks ||= [] 
    28  
    29           if superclass.respond_to?(:setup_callback_chain) 
    30             superclass.setup_callback_chain + @setup_callbacks 
    31           else 
    32             @setup_callbacks 
    33           end 
    34         end 
    35  
    36         def teardown_callback_chain 
    37           @teardown_callbacks ||= [] 
    38  
    39           if superclass.respond_to?(:teardown_callback_chain) 
    40             superclass.teardown_callback_chain + @teardown_callbacks 
    41           else 
    42             @teardown_callbacks 
    43           end 
    4413        end 
    4514      end 
     
    6433          begin 
    6534            teardown 
    66             run_callbacks :teardown, :reverse_each 
     35            run_callbacks :teardown, :enumerator => :reverse_each 
    6736          rescue Test::Unit::AssertionFailedError => e 
    6837            add_failure(e.message, e.backtrace) 
     
    9968            begin 
    10069              teardown 
    101               run_callbacks :teardown, :reverse_each 
     70              run_callbacks :teardown, :enumerator => :reverse_each 
    10271            rescue Test::Unit::AssertionFailedError => e 
    10372              add_failure(e.message, e.backtrace) 
     
    11281        yield(Test::Unit::TestCase::FINISHED, name) 
    11382      end 
    114  
    115       protected 
    116         def run_callbacks(kind, enumerator = :each) 
    117           self.class.send("#{kind}_callback_chain").send(enumerator) do |callback| 
    118             case callback 
    119             when Proc; callback.call(self) 
    120             when String, Symbol; send!(callback) 
    121             else raise ArgumentError, "Unrecognized callback #{callback.inspect}" 
    122             end 
    123           end 
    124         end 
    12583    end 
    12684  end