aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/abstract_unit.rb5
-rw-r--r--activesupport/test/caching_test.rb41
-rw-r--r--activesupport/test/core_ext/boolean_ext_test.rb9
-rw-r--r--activesupport/test/core_ext/duration_test.rb1
-rw-r--r--activesupport/test/core_ext/module/model_naming_test.rb20
-rw-r--r--activesupport/test/core_ext/module_test.rb2
-rw-r--r--activesupport/test/core_ext/nil_ext_test.rb5
-rw-r--r--activesupport/test/core_ext/object_ext_test.rb6
-rw-r--r--activesupport/test/core_ext/regexp_ext_test.rb26
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb3
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb2
-rw-r--r--activesupport/test/fixtures/omgomg.rb2
-rw-r--r--activesupport/test/inflector_test.rb6
-rw-r--r--activesupport/test/inflector_test_cases.rb7
-rw-r--r--activesupport/test/isolation_test.rb156
-rw-r--r--activesupport/test/json/encoding_test.rb28
-rw-r--r--activesupport/test/new_callback_inheritance_test.rb10
-rw-r--r--activesupport/test/new_callbacks_test.rb78
-rw-r--r--activesupport/test/ordered_hash_test.rb25
19 files changed, 353 insertions, 79 deletions
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index 428a06b0bf..61914231ef 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -1,9 +1,8 @@
+ORIG_ARGV = ARGV.dup
+
require 'rubygems'
require 'test/unit'
-gem 'mocha', '>= 0.9.5'
-require 'mocha'
-
ENV['NO_RELOAD'] = '1'
$:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'active_support'
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 51d04d9388..7667f11343 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -1,3 +1,4 @@
+require 'logger'
require 'abstract_unit'
require 'active_support/cache'
@@ -146,6 +147,22 @@ class FileStoreTest < ActiveSupport::TestCase
end
include CacheStoreBehavior
+
+ def test_expires_in
+ time = Time.local(2008, 4, 24)
+ Time.stubs(:now).returns(time)
+ File.stubs(:mtime).returns(time)
+
+ @cache.write('foo', 'bar')
+ cache_read = lambda { @cache.read('foo', :expires_in => 1.minute) }
+ assert_equal 'bar', cache_read.call
+
+ Time.stubs(:now).returns(time + 30.seconds)
+ assert_equal 'bar', cache_read.call
+
+ Time.stubs(:now).returns(time + 2.minutes)
+ assert_nil cache_read.call
+ end
end
class MemoryStoreTest < ActiveSupport::TestCase
@@ -160,6 +177,12 @@ class MemoryStoreTest < ActiveSupport::TestCase
assert_raise(ActiveSupport::FrozenObjectError) { @cache.read('foo').gsub!(/.*/, 'baz') }
assert_equal 'bar', @cache.read('foo')
end
+
+ def test_original_store_objects_should_not_be_immutable
+ bar = 'bar'
+ @cache.write('foo', bar)
+ assert_nothing_raised { bar.gsub!(/.*/, 'baz') }
+ end
end
uses_memcached 'memcached backed store' do
@@ -168,6 +191,8 @@ uses_memcached 'memcached backed store' do
@cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)
@data = @cache.instance_variable_get(:@data)
@cache.clear
+ @cache.silence!
+ @cache.logger = Logger.new("/dev/null")
end
include CacheStoreBehavior
@@ -290,6 +315,22 @@ uses_memcached 'memcached backed store' do
app = @cache.middleware.new(app)
app.call({})
end
+
+ def test_expires_in
+ result = @cache.write('foo', 'bar', :expires_in => 1)
+ assert_equal 'bar', @cache.read('foo')
+ sleep 2
+ assert_equal nil, @cache.read('foo')
+ end
+
+ def test_expires_in_with_invalid_value
+ @cache.write('baz', 'bat')
+ assert_raise(RuntimeError) do
+ @cache.write('foo', 'bar', :expires_in => 'Mon Jun 29 13:10:40 -0700 2150')
+ end
+ assert_equal 'bat', @cache.read('baz')
+ assert_equal nil, @cache.read('foo')
+ end
end
class CompressedMemCacheStore < ActiveSupport::TestCase
diff --git a/activesupport/test/core_ext/boolean_ext_test.rb b/activesupport/test/core_ext/boolean_ext_test.rb
new file mode 100644
index 0000000000..751f703745
--- /dev/null
+++ b/activesupport/test/core_ext/boolean_ext_test.rb
@@ -0,0 +1,9 @@
+class BooleanExtAccessTests < Test::Unit::TestCase
+ def test_to_param_on_true
+ assert_equal true, true.to_param
+ end
+
+ def test_to_param_on_false
+ assert_equal false, false.to_param
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
index 6f16621ae5..42b4f10172 100644
--- a/activesupport/test/core_ext/duration_test.rb
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -3,6 +3,7 @@ require 'active_support/time'
class DurationTest < ActiveSupport::TestCase
def test_inspect
+ assert_equal '0 seconds', 0.seconds.inspect
assert_equal '1 month', 1.month.inspect
assert_equal '1 month and 1 day', (1.month + 1.day).inspect
assert_equal '6 months and -2 days', (6.months - 2.days).inspect
diff --git a/activesupport/test/core_ext/module/model_naming_test.rb b/activesupport/test/core_ext/module/model_naming_test.rb
deleted file mode 100644
index da3b6c4932..0000000000
--- a/activesupport/test/core_ext/module/model_naming_test.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-require 'abstract_unit'
-require 'active_support/core_ext/module/model_naming'
-
-class ModelNamingTest < Test::Unit::TestCase
- def setup
- @model_name = ActiveSupport::ModelName.new('Post::TrackBack')
- end
-
- def test_singular
- assert_equal 'post_track_back', @model_name.singular
- end
-
- def test_plural
- assert_equal 'post_track_backs', @model_name.plural
- end
-
- def test_partial_path
- assert_equal 'post/track_backs/track_back', @model_name.partial_path
- end
-end
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb
index c3c696f93a..f8387ae4ab 100644
--- a/activesupport/test/core_ext/module_test.rb
+++ b/activesupport/test/core_ext/module_test.rb
@@ -142,7 +142,7 @@ class ModuleTest < Test::Unit::TestCase
def test_delegation_without_allow_nil_and_nil_value
david = Someone.new("David")
- assert_raise(NoMethodError) { david.street }
+ assert_raise(RuntimeError) { david.street }
end
def test_parent
diff --git a/activesupport/test/core_ext/nil_ext_test.rb b/activesupport/test/core_ext/nil_ext_test.rb
new file mode 100644
index 0000000000..945d3af239
--- /dev/null
+++ b/activesupport/test/core_ext/nil_ext_test.rb
@@ -0,0 +1,5 @@
+class NilExtAccessTests < Test::Unit::TestCase
+ def test_to_param
+ assert_nil nil.to_param
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/core_ext/object_ext_test.rb b/activesupport/test/core_ext/object_ext_test.rb
index a413d331c4..72e3bffa4c 100644
--- a/activesupport/test/core_ext/object_ext_test.rb
+++ b/activesupport/test/core_ext/object_ext_test.rb
@@ -5,4 +5,10 @@ class ObjectExtTest < Test::Unit::TestCase
foo = Object.new
assert_equal foo, foo.tap { |x| assert_equal foo, x; :bar }
end
+
+ def test_to_param
+ foo = Object.new
+ foo.class_eval("def to_s; 'foo'; end")
+ assert_equal 'foo', foo.to_param
+ end
end
diff --git a/activesupport/test/core_ext/regexp_ext_test.rb b/activesupport/test/core_ext/regexp_ext_test.rb
new file mode 100644
index 0000000000..f71099856d
--- /dev/null
+++ b/activesupport/test/core_ext/regexp_ext_test.rb
@@ -0,0 +1,26 @@
+class RegexpExtAccessTests < Test::Unit::TestCase
+ def test_number_of_captures
+ assert_equal 0, //.number_of_captures
+ assert_equal 1, /.(.)./.number_of_captures
+ assert_equal 2, /.(.).(?:.).(.)/.number_of_captures
+ assert_equal 3, /.((.).(?:.).(.))/.number_of_captures
+ end
+
+ def test_multiline
+ assert //m.multiline?
+ assert ! //.multiline?
+ assert ! /(?m:)/.multiline?
+ end
+
+ def test_optionalize
+ assert "a?", Regexp.optionalize("a")
+ assert "(?:foo)?", Regexp.optionalize("foo")
+ assert "", Regexp.optionalize("")
+ end
+
+ def test_unoptionalize
+ assert "a", Regexp.unoptionalize("a?")
+ assert "foo", Regexp.unoptionalize("(?:foo)")
+ assert "", Regexp.unoptionalize("")
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 237a843f9a..6991b174b7 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -112,6 +112,8 @@ class StringInflectionsTest < Test::Unit::TestCase
def test_string_to_time
assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)
+ assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
+ assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local)
assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
end
@@ -120,6 +122,7 @@ class StringInflectionsTest < Test::Unit::TestCase
assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
+ assert_equal DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00"), "2039-02-27T23:50:19.275038-04:00".to_datetime
end
def test_string_to_date
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index 03ae70d420..bb60968a4f 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -1,5 +1,5 @@
require 'abstract_unit'
-require 'active_support/time_with_zone'
+require 'active_support/time'
require 'active_support/json'
class TimeWithZoneTest < Test::Unit::TestCase
diff --git a/activesupport/test/fixtures/omgomg.rb b/activesupport/test/fixtures/omgomg.rb
new file mode 100644
index 0000000000..a512a93ae4
--- /dev/null
+++ b/activesupport/test/fixtures/omgomg.rb
@@ -0,0 +1,2 @@
+class OmgOmg
+end \ No newline at end of file
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index 8c4d831a39..7d1554910e 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -202,6 +202,12 @@ class InflectorTest < Test::Unit::TestCase
end
end
+ def test_symbol_to_lower_camel
+ SymbolToLowerCamel.each do |symbol, lower_camel|
+ assert_equal(lower_camel, ActiveSupport::Inflector.camelize(symbol, false))
+ end
+ end
+
%w{plurals singulars uncountables humans}.each do |inflection_type|
class_eval "
def test_clear_#{inflection_type}
diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb
index 584cbff3e7..2fa94b8e9c 100644
--- a/activesupport/test/inflector_test_cases.rb
+++ b/activesupport/test/inflector_test_cases.rb
@@ -117,6 +117,13 @@ module InflectorTestCases
"area51_controller" => "area51Controller"
}
+ SymbolToLowerCamel = {
+ :product => 'product',
+ :special_guest => 'specialGuest',
+ :application_controller => 'applicationController',
+ :area51_controller => 'area51Controller'
+ }
+
CamelToUnderscoreWithoutReverse = {
"HTMLTidy" => "html_tidy",
"HTMLTidyGenerator" => "html_tidy_generator",
diff --git a/activesupport/test/isolation_test.rb b/activesupport/test/isolation_test.rb
new file mode 100644
index 0000000000..5a1f285476
--- /dev/null
+++ b/activesupport/test/isolation_test.rb
@@ -0,0 +1,156 @@
+require 'abstract_unit'
+
+# Does awesome
+if ENV['CHILD']
+ class ChildIsolationTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def self.setup
+ File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "a") do |f|
+ f.puts "hello"
+ end
+ end
+
+ def setup
+ @instance = "HELLO"
+ end
+
+ def teardown
+ raise if @boom
+ end
+
+ test "runs the test" do
+ assert true
+ end
+
+ test "captures errors" do
+ raise
+ end
+
+ test "captures failures" do
+ assert false
+ end
+
+ test "first runs in isolation" do
+ assert_nil $x
+ $x = 1
+ end
+
+ test "second runs in isolation" do
+ assert_nil $x
+ $x = 2
+ end
+
+ test "runs with slow tests" do
+ sleep 0.3
+ assert true
+ sleep 0.2
+ end
+
+ test "runs setup" do
+ assert "HELLO", @instance
+ end
+
+ test "runs teardown" do
+ @boom = true
+ end
+
+ test "resets requires one" do
+ assert !defined?(OmgOmg)
+ assert_equal 0, $LOADED_FEATURES.grep(/fixtures\/omgomg/).size
+ require File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "omgomg"))
+ end
+
+ test "resets requires two" do
+ assert !defined?(OmgOmg)
+ assert_equal 0, $LOADED_FEATURES.grep(/fixtures\/omgomg/).size
+ require File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "omgomg"))
+ end
+ end
+else
+ class ParentIsolationTest < ActiveSupport::TestCase
+
+ File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "w") {}
+
+ ENV["CHILD"] = "1"
+ OUTPUT = `#{Gem.ruby} -I#{File.dirname(__FILE__)} #{File.expand_path(__FILE__)} -v`
+ ENV.delete("CHILD")
+
+ def setup
+ # Extract the results
+ @results = {}
+ OUTPUT[/Started\n\s*(.*)\s*\nFinished/mi, 1].split(/\s*\n\s*/).each do |result|
+ result =~ %r'^(\w+)\(\w+\):\s*(\.|E|F)$'
+ @results[$1] = { 'E' => :error, '.' => :success, 'F' => :failure }[$2]
+ end
+
+ # Extract the backtraces
+ @backtraces = {}
+ OUTPUT.scan(/^\s*\d+\).*?\n\n/m).each do |backtrace|
+ # \n 1) Error:\ntest_captures_errors(ChildIsolationTest):
+ backtrace =~ %r'\s*\d+\)\s*(Error|Failure):\n(\w+)'i
+ @backtraces[$2] = { :type => $1, :output => backtrace }
+ end
+ end
+
+ def assert_failing(name)
+ assert_equal :failure, @results[name.to_s], "Test #{name} did not fail"
+ end
+
+ def assert_passing(name)
+ assert_equal :success, @results[name.to_s], "Test #{name} did not pass"
+ end
+
+ def assert_erroring(name)
+ assert_equal :error, @results[name.to_s], "Test #{name} did not error"
+ end
+
+ test "has all tests" do
+ assert_equal 10, @results.length
+ end
+
+ test "passing tests are still reported" do
+ assert_passing :test_runs_the_test
+ assert_passing :test_runs_with_slow_tests
+ end
+
+ test "resets global variables" do
+ assert_passing :test_first_runs_in_isolation
+ assert_passing :test_second_runs_in_isolation
+ end
+
+ test "resets requires" do
+ assert_passing :test_resets_requires_one
+ assert_passing :test_resets_requires_two
+ end
+
+ test "erroring tests are still reported" do
+ assert_erroring :test_captures_errors
+ end
+
+ test "runs setup and teardown methods" do
+ assert_passing :test_runs_setup
+ assert_erroring :test_runs_teardown
+ end
+
+ test "correct tests fail" do
+ assert_failing :test_captures_failures
+ end
+
+ test "backtrace is printed for errors" do
+ assert_equal 'Error', @backtraces["test_captures_errors"][:type]
+ assert_match %r{isolation_test.rb:\d+:in `test_captures_errors'}, @backtraces["test_captures_errors"][:output]
+ end
+
+ test "backtrace is printed for failures" do
+ assert_equal 'Failure', @backtraces["test_captures_failures"][:type]
+ assert_match %r{isolation_test.rb:\d+:in `test_captures_failures'}, @backtraces["test_captures_failures"][:output]
+ end
+
+ test "self.setup is run only once" do
+ text = File.read(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"))
+ assert_equal "hello\n", text
+ end
+
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 1a0e6d543c..5d81d09f03 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -9,6 +9,12 @@ class TestJSONEncoding < Test::Unit::TestCase
end
end
+ class Custom
+ def as_json(options)
+ 'custom'
+ end
+ end
+
TrueTests = [[ true, %(true) ]]
FalseTests = [[ false, %(false) ]]
NilTests = [[ nil, %(null) ]]
@@ -27,6 +33,7 @@ class TestJSONEncoding < Test::Unit::TestCase
[ :"a b", %("a b") ]]
ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
+ CustomTests = [[ Custom.new, '"custom"' ]]
VariableTests = [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'],
[ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']]
@@ -68,15 +75,20 @@ class TestJSONEncoding < Test::Unit::TestCase
def test_utf8_string_encoded_properly_when_kcode_is_utf8
with_kcode 'UTF8' do
- assert_equal '"\\u20ac2.99"', ActiveSupport::JSON.encode('€2.99')
- assert_equal '"\\u270e\\u263a"', ActiveSupport::JSON.encode('✎☺')
+ result = ActiveSupport::JSON.encode('€2.99')
+ assert_equal '"\\u20ac2.99"', result
+ assert_equal(Encoding::UTF_8, result.encoding) if result.respond_to?(:encoding)
+
+ result = ActiveSupport::JSON.encode('✎☺')
+ assert_equal '"\\u270e\\u263a"', result
+ assert_equal(Encoding::UTF_8, result.encoding) if result.respond_to?(:encoding)
end
end
def test_exception_raised_when_encoding_circular_reference
a = [1]
a << a
- assert_raise(ActiveSupport::JSON::CircularReferenceError) { ActiveSupport::JSON.encode(a) }
+ assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) }
end
def test_hash_key_identifiers_are_always_quoted
@@ -129,11 +141,9 @@ end
class JsonOptionsTests < Test::Unit::TestCase
def test_enumerable_should_passthrough_options_to_elements
- json_options = { :include => :posts }
- ActiveSupport::JSON.expects(:encode).with(1, json_options)
- ActiveSupport::JSON.expects(:encode).with(2, json_options)
- ActiveSupport::JSON.expects(:encode).with('foo', json_options)
-
- [1, 2, 'foo'].send(:rails_to_json, json_options)
+ value, options = Object.new, Object.new
+ def value.as_json(options) options end
+ def options.encode_json(encoder) self end
+ assert_equal options, ActiveSupport::JSON.encode(value, options)
end
end
diff --git a/activesupport/test/new_callback_inheritance_test.rb b/activesupport/test/new_callback_inheritance_test.rb
index 95020389b0..da0c19eaea 100644
--- a/activesupport/test/new_callback_inheritance_test.rb
+++ b/activesupport/test/new_callback_inheritance_test.rb
@@ -11,8 +11,8 @@ class GrandParent
end
define_callbacks :dispatch
- dispatch_callback :before, :before1, :before2, :per_key => {:if => proc {|c| c.action_name == "index" || c.action_name == "update" }}
- dispatch_callback :after, :after1, :after2, :per_key => {:if => proc {|c| c.action_name == "update" || c.action_name == "delete" }}
+ set_callback :dispatch, :before, :before1, :before2, :per_key => {:if => proc {|c| c.action_name == "index" || c.action_name == "update" }}
+ set_callback :dispatch, :after, :after1, :after2, :per_key => {:if => proc {|c| c.action_name == "update" || c.action_name == "delete" }}
def before1
@log << "before1"
@@ -39,12 +39,12 @@ class GrandParent
end
class Parent < GrandParent
- skip_dispatch_callback :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}
- skip_dispatch_callback :after, :after2, :per_key => {:unless => proc {|c| c.action_name == "delete" }}
+ skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}
+ skip_callback :dispatch, :after, :after2, :per_key => {:unless => proc {|c| c.action_name == "delete" }}
end
class Child < GrandParent
- skip_dispatch_callback :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}, :if => :state_open?
+ skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}, :if => :state_open?
def state_open?
@state == :open
diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb
index 7bec47224d..7e092b5f63 100644
--- a/activesupport/test/new_callbacks_test.rb
+++ b/activesupport/test/new_callbacks_test.rb
@@ -10,11 +10,11 @@ module NewCallbacksTest
define_callbacks :save
def self.before_save(*filters, &blk)
- save_callback(:before, *filters, &blk)
+ set_callback(:save, :before, *filters, &blk)
end
def self.after_save(*filters, &blk)
- save_callback(:after, *filters, &blk)
+ set_callback(:save, :after, *filters, &blk)
end
class << self
@@ -37,7 +37,7 @@ module NewCallbacksTest
def callback_object(callback_method)
klass = Class.new
klass.send(:define_method, callback_method) do |model|
- model.history << [callback_method, :object]
+ model.history << [:"#{callback_method}_save", :object]
end
klass.new
end
@@ -54,7 +54,7 @@ module NewCallbacksTest
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, callback_object(callback_method_sym.to_s.gsub(/_save/, '')))
send(callback_method) { |model| model.history << [callback_method_sym, :block] }
end
@@ -64,10 +64,10 @@ module NewCallbacksTest
end
class PersonSkipper < Person
- skip_save_callback :before, :before_save_method, :if => :yes
- skip_save_callback :after, :before_save_method, :unless => :yes
- skip_save_callback :after, :before_save_method, :if => :no
- skip_save_callback :before, :before_save_method, :unless => :no
+ skip_callback :save, :before, :before_save_method, :if => :yes
+ skip_callback :save, :after, :before_save_method, :unless => :yes
+ skip_callback :save, :after, :before_save_method, :if => :no
+ skip_callback :save, :before, :before_save_method, :unless => :no
def yes; true; end
def no; false; end
end
@@ -77,8 +77,8 @@ module NewCallbacksTest
define_callbacks :dispatch
- dispatch_callback :before, :log, :per_key => {:unless => proc {|c| c.action_name == :index || c.action_name == :show }}
- dispatch_callback :after, :log2
+ set_callback :dispatch, :before, :log, :per_key => {:unless => proc {|c| c.action_name == :index || c.action_name == :show }}
+ set_callback :dispatch, :after, :log2
attr_reader :action_name, :logger
def initialize(action_name)
@@ -102,8 +102,8 @@ module NewCallbacksTest
end
class Child < ParentController
- skip_dispatch_callback :before, :log, :per_key => {:if => proc {|c| c.action_name == :update} }
- skip_dispatch_callback :after, :log2
+ skip_callback :dispatch, :before, :log, :per_key => {:if => proc {|c| c.action_name == :update} }
+ skip_callback :dispatch, :after, :log2
end
class OneTimeCompile < Record
@@ -188,19 +188,19 @@ module NewCallbacksTest
class AroundPerson < MySuper
attr_reader :history
- save_callback :before, :nope, :if => :no
- save_callback :before, :nope, :unless => :yes
- save_callback :after, :tweedle
- save_callback :before, "tweedle_dee"
- save_callback :before, proc {|m| m.history << "yup" }
- save_callback :before, :nope, :if => proc { false }
- save_callback :before, :nope, :unless => proc { true }
- save_callback :before, :yup, :if => proc { true }
- save_callback :before, :yup, :unless => proc { false }
- save_callback :around, :tweedle_dum
- save_callback :around, :w0tyes, :if => :yes
- save_callback :around, :w0tno, :if => :no
- save_callback :around, :tweedle_deedle
+ set_callback :save, :before, :nope, :if => :no
+ set_callback :save, :before, :nope, :unless => :yes
+ set_callback :save, :after, :tweedle
+ set_callback :save, :before, "tweedle_dee"
+ set_callback :save, :before, proc {|m| m.history << "yup" }
+ set_callback :save, :before, :nope, :if => proc { false }
+ set_callback :save, :before, :nope, :unless => proc { true }
+ set_callback :save, :before, :yup, :if => proc { true }
+ set_callback :save, :before, :yup, :unless => proc { false }
+ set_callback :save, :around, :tweedle_dum
+ set_callback :save, :around, :w0tyes, :if => :yes
+ set_callback :save, :around, :w0tno, :if => :no
+ set_callback :save, :around, :tweedle_deedle
def no; false; end
def yes; true; end
@@ -260,7 +260,7 @@ module NewCallbacksTest
define_callbacks :save
attr_reader :stuff
- save_callback :before, :omg, :per_key => {:if => :yes}
+ set_callback :save, :before, :omg, :per_key => {:if => :yes}
def yes() true end
@@ -354,15 +354,15 @@ module NewCallbacksTest
define_callbacks :save, "result == :halt"
- save_callback :before, :first
- save_callback :before, :second
- save_callback :around, :around_it
- save_callback :before, :third
- save_callback :after, :first
- save_callback :around, :around_it
- save_callback :after, :second
- save_callback :around, :around_it
- save_callback :after, :third
+ set_callback :save, :before, :first
+ set_callback :save, :before, :second
+ set_callback :save, :around, :around_it
+ set_callback :save, :before, :third
+ set_callback :save, :after, :first
+ set_callback :save, :around, :around_it
+ set_callback :save, :after, :second
+ set_callback :save, :around, :around_it
+ set_callback :save, :after, :third
attr_reader :history, :saved
@@ -397,11 +397,11 @@ module NewCallbacksTest
end
class CallbackObject
- def before_save(caller)
+ def before(caller)
caller.record << "before"
end
- def around_save(caller)
+ def around(caller)
caller.record << "around before"
yield
caller.record << "around after"
@@ -412,7 +412,7 @@ module NewCallbacksTest
include ActiveSupport::NewCallbacks
define_callbacks :save
- save_callback :before, CallbackObject.new
+ set_callback :save, :before, CallbackObject.new
attr_accessor :record
def initialize
@@ -430,7 +430,7 @@ module NewCallbacksTest
include ActiveSupport::NewCallbacks
define_callbacks :save
- save_callback :around, CallbackObject.new
+ set_callback :save, :around, CallbackObject.new
attr_accessor :record
def initialize
diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb
index 647938dd87..15bd57181f 100644
--- a/activesupport/test/ordered_hash_test.rb
+++ b/activesupport/test/ordered_hash_test.rb
@@ -163,9 +163,32 @@ class OrderedHashTest < Test::Unit::TestCase
assert @ordered_hash.inspect.include?(@hash.inspect)
end
- def test_alternate_initialization
+ def test_alternate_initialization_with_splat
alternate = ActiveSupport::OrderedHash[1,2,3,4]
assert_kind_of ActiveSupport::OrderedHash, alternate
assert_equal [1, 3], alternate.keys
end
+
+ def test_alternate_initialization_with_array
+ alternate = ActiveSupport::OrderedHash[ [
+ [1, 2],
+ [3, 4],
+ "bad key value pair",
+ [ 'missing value' ]
+ ]]
+
+ assert_kind_of ActiveSupport::OrderedHash, alternate
+ assert_equal [1, 3, 'missing value'], alternate.keys
+ assert_equal [2, 4, nil ], alternate.values
+ end
+
+ def test_alternate_initialization_raises_exception_on_odd_length_args
+ begin
+ alternate = ActiveSupport::OrderedHash[1,2,3,4,5]
+ flunk "Hash::[] should have raised an exception on initialization " +
+ "with an odd number of parameters"
+ rescue
+ assert_equal "odd number of arguments for Hash", $!.message
+ end
+ end
end