diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-19 02:44:45 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-19 02:44:45 +0000 |
commit | aae37bb4f7edd6a1820e420a60560369c6064f33 (patch) | |
tree | 5642ff690036cac09bc697a0798dd984128d6e73 /activesupport/test | |
parent | 3ffdfa84fc1f9bebc578fe957646af5f194ca625 (diff) | |
download | rails-aae37bb4f7edd6a1820e420a60560369c6064f33.tar.gz rails-aae37bb4f7edd6a1820e420a60560369c6064f33.tar.bz2 rails-aae37bb4f7edd6a1820e420a60560369c6064f33.zip |
Extract ActiveSupport::Callbacks from Active Record, test case setup and teardown, and ActionController::Dispatcher. Closes #10727.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8664 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/callbacks_test.rb | 96 | ||||
-rw-r--r-- | activesupport/test/test_test.rb | 8 |
2 files changed, 100 insertions, 4 deletions
diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb new file mode 100644 index 0000000000..6d390bbc5c --- /dev/null +++ b/activesupport/test/callbacks_test.rb @@ -0,0 +1,96 @@ +require 'abstract_unit' + +class Record + include ActiveSupport::Callbacks + + define_callbacks :before_save, :after_save + + class << self + def callback_symbol(callback_method) + returning("#{callback_method}_method") do |method_name| + define_method(method_name) do + history << [callback_method, :symbol] + end + end + end + + def callback_string(callback_method) + "history << [#{callback_method.to_sym.inspect}, :string]" + end + + def callback_proc(callback_method) + Proc.new { |model| model.history << [callback_method, :proc] } + end + + def callback_object(callback_method) + klass = Class.new + klass.send(:define_method, callback_method) do |model| + model.history << [callback_method, :object] + end + klass.new + end + end + + def history + @history ||= [] + end +end + +class Person < Record + [:before_save, :after_save].each do |callback_method| + callback_method_sym = callback_method.to_sym + send(callback_method, callback_symbol(callback_method_sym)) + send(callback_method, callback_string(callback_method_sym)) + send(callback_method, callback_proc(callback_method_sym)) + send(callback_method, callback_object(callback_method_sym)) + send(callback_method) { |model| model.history << [callback_method_sym, :block] } + end + + def save + run_callbacks(:before_save) + run_callbacks(:after_save) + end +end + +class ConditionalPerson < Record + before_save Proc.new { |r| r.history << [:before_save, :proc] }, :if => Proc.new { |r| true } + before_save Proc.new { |r| r.history << "b00m" }, :if => Proc.new { |r| false } + before_save Proc.new { |r| r.history << [:before_save, :proc] }, :unless => Proc.new { |r| false } + before_save Proc.new { |r| r.history << "b00m" }, :unless => Proc.new { |r| true } + + def save + run_callbacks(:before_save) + run_callbacks(:after_save) + end +end + +class CallbacksTest < Test::Unit::TestCase + def test_save_person + person = Person.new + assert_equal [], person.history + person.save + assert_equal [ + [:before_save, :symbol], + [:before_save, :string], + [:before_save, :proc], + [:before_save, :object], + [:before_save, :block], + [:after_save, :symbol], + [:after_save, :string], + [:after_save, :proc], + [:after_save, :object], + [:after_save, :block] + ], person.history + end +end + +class ConditionalCallbackTest < Test::Unit::TestCase + def test_save_conditional_person + person = ConditionalPerson.new + person.save + assert_equal [ + [:before_save, :proc], + [:before_save, :proc] + ], person.history + end +end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 88b505e59c..1e75e18602 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -79,9 +79,9 @@ class SetupAndTeardownTest < Test::Unit::TestCase teardown :foo, :sentinel, :foo def test_inherited_setup_callbacks - assert_equal [:reset_callback_record, :foo], self.class.setup_callback_chain + assert_equal [:reset_callback_record, :foo], self.class.setup_callback_chain.map(&:method) assert_equal [:foo], @called_back - assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain + assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method) end protected @@ -104,9 +104,9 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest teardown :bar def test_inherited_setup_callbacks - assert_equal [:reset_callback_record, :foo, :bar], self.class.setup_callback_chain + assert_equal [:reset_callback_record, :foo, :bar], self.class.setup_callback_chain.map(&:method) assert_equal [:foo, :bar], @called_back - assert_equal [:foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain + assert_equal [:foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain.map(&:method) end protected |