aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/caching_test.rb6
-rw-r--r--activesupport/test/callback_inheritance_test.rb30
-rw-r--r--activesupport/test/callbacks_test.rb34
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb13
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb16
-rw-r--r--activesupport/test/json/decoding_test.rb8
-rw-r--r--activesupport/test/whiny_nil_test.rb2
7 files changed, 90 insertions, 19 deletions
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 579d5dad24..e5668e29d7 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -679,12 +679,12 @@ class CacheEntryTest < ActiveSupport::TestCase
def test_expired
entry = ActiveSupport::Cache::Entry.new("value")
- assert_equal false, entry.expired?
+ assert !entry.expired?, 'entry not expired'
entry = ActiveSupport::Cache::Entry.new("value", :expires_in => 60)
- assert_equal false, entry.expired?
+ assert !entry.expired?, 'entry not expired'
time = Time.now + 61
Time.stubs(:now).returns(time)
- assert_equal true, entry.expired?
+ assert entry.expired?, 'entry is expired'
end
def test_compress_values
diff --git a/activesupport/test/callback_inheritance_test.rb b/activesupport/test/callback_inheritance_test.rb
index 71249050fc..d569cbb4fb 100644
--- a/activesupport/test/callback_inheritance_test.rb
+++ b/activesupport/test/callback_inheritance_test.rb
@@ -82,6 +82,30 @@ class EmptyChild < EmptyParent
end
end
+class CountingParent
+ include ActiveSupport::Callbacks
+
+ attr_reader :count
+
+ define_callbacks :dispatch
+
+ def initialize
+ @count = 0
+ end
+
+ def count!
+ @count += 1
+ end
+
+ def dispatch
+ run_callbacks(:dispatch)
+ self
+ end
+end
+
+class CountingChild < CountingParent
+end
+
class BasicCallbacksTest < Test::Unit::TestCase
def setup
@index = GrandParent.new("index").dispatch
@@ -147,4 +171,10 @@ class DynamicInheritedCallbacks < Test::Unit::TestCase
child = EmptyChild.new.dispatch
assert child.performed?
end
+
+ def test_callbacks_should_be_performed_once_in_child_class
+ CountingParent.set_callback(:dispatch, :before) { count! }
+ child = CountingChild.new.dispatch
+ assert_equal 1, child.count
+ end
end
diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb
index cff914f4ae..816dcad968 100644
--- a/activesupport/test/callbacks_test.rb
+++ b/activesupport/test/callbacks_test.rb
@@ -299,6 +299,32 @@ module CallbacksTest
end
end
end
+
+ class AroundPersonResult < MySuper
+ attr_reader :result
+
+ set_callback :save, :after, :tweedle_1
+ set_callback :save, :around, :tweedle_dum
+ set_callback :save, :after, :tweedle_2
+
+ def tweedle_dum
+ @result = yield
+ end
+
+ def tweedle_1
+ :tweedle_1
+ end
+
+ def tweedle_2
+ :tweedle_2
+ end
+
+ def save
+ run_callbacks :save do
+ :running
+ end
+ end
+ end
class HyphenatedCallbacks
include ActiveSupport::Callbacks
@@ -338,6 +364,14 @@ module CallbacksTest
], around.history
end
end
+
+ class AroundCallbackResultTest < Test::Unit::TestCase
+ def test_save_around
+ around = AroundPersonResult.new
+ around.save
+ assert_equal :running, around.result
+ end
+ end
class SkipCallbacksTest < Test::Unit::TestCase
def test_skip_person
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index 03b84ae2e5..d81693209f 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -262,8 +262,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
def test_yesterday_constructor_when_zone_is_not_set
with_env_tz 'UTC' do
with_tz_default do
- Time.stubs(:now).returns Time.local(2000, 1, 1)
- assert_equal Date.new(1999, 12, 31), Date.yesterday
+ assert_equal(Date.today - 1, Date.yesterday)
end
end
end
@@ -284,8 +283,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
def test_tomorrow_constructor_when_zone_is_not_set
with_env_tz 'UTC' do
with_tz_default do
- Time.stubs(:now).returns Time.local(1999, 12, 31)
- assert_equal Date.new(2000, 1, 1), Date.tomorrow
+ assert_equal(Date.today + 1, Date.tomorrow)
end
end
end
@@ -410,17 +408,14 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
def test_current_returns_date_today_when_zone_not_set
with_env_tz 'US/Central' do
Time.stubs(:now).returns Time.local(1999, 12, 31, 23)
- assert_equal Date.new(1999, 12, 31), Date.today
- assert_equal Date.new(1999, 12, 31), Date.current
+ assert_equal Date.today, Date.current
end
end
def test_current_returns_time_zone_today_when_zone_is_set
Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'US/Central' do
- Time.stubs(:now).returns Time.local(1999, 12, 31, 23)
- assert_equal Date.new(1999, 12, 31), Date.today
- assert_equal Date.new(2000, 1, 1), Date.current
+ assert_equal ::Time.zone.today, Date.current
end
ensure
Time.zone = nil
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 891a6badac..53d497013a 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -533,9 +533,19 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
Time::DATE_FORMATS.delete(:custom)
end
- def test_conversion_methods_are_publicized
- assert Time.public_instance_methods.include?(:to_date) || Time.public_instance_methods.include?('to_date')
- assert Time.public_instance_methods.include?(:to_datetime) || Time.public_instance_methods.include?('to_datetime')
+ def test_to_date
+ assert_equal Date.new(2005, 2, 21), Time.local(2005, 2, 21, 17, 44, 30).to_date
+ end
+
+ def test_to_datetime
+ assert_equal Time.utc(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, 0, 0)
+ with_env_tz 'US/Eastern' do
+ assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0)
+ end
+ with_env_tz 'NZ' do
+ assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0)
+ end
+ assert_equal ::Date::ITALY, Time.utc(2005, 2, 21, 17, 44, 30).to_datetime.start # use Ruby's default start value
end
def test_to_time
diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb
index 24d9f88c09..88cf97de7e 100644
--- a/activesupport/test/json/decoding_test.rb
+++ b/activesupport/test/json/decoding_test.rb
@@ -17,6 +17,8 @@ class TestJSONDecoding < ActiveSupport::TestCase
%({"matzue": "松江", "asakusa": "浅草"}) => {"matzue" => "松江", "asakusa" => "浅草"},
%({"a": "2007-01-01"}) => {'a' => Date.new(2007, 1, 1)},
%({"a": "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
+ %(["2007-01-01 01:12:34 Z"]) => [Time.utc(2007, 1, 1, 1, 12, 34)],
+ %(["2007-01-01 01:12:34 Z", "2007-01-01 01:12:35 Z"]) => [Time.utc(2007, 1, 1, 1, 12, 34), Time.utc(2007, 1, 1, 1, 12, 35)],
# no time zone
%({"a": "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
# invalid date
@@ -72,13 +74,11 @@ class TestJSONDecoding < ActiveSupport::TestCase
end
end
end
- end
- if backends.include?("JSONGem")
- test "json decodes time json with time parsing disabled" do
+ test "json decodes time json with time parsing disabled with the #{backend} backend" do
ActiveSupport.parse_json_times = false
expected = {"a" => "2007-01-01 01:12:34 Z"}
- ActiveSupport::JSON.with_backend "JSONGem" do
+ ActiveSupport::JSON.with_backend backend do
assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"}))
end
end
diff --git a/activesupport/test/whiny_nil_test.rb b/activesupport/test/whiny_nil_test.rb
index 4b9f06dead..ec3ca99ee6 100644
--- a/activesupport/test/whiny_nil_test.rb
+++ b/activesupport/test/whiny_nil_test.rb
@@ -33,9 +33,11 @@ class WhinyNilTest < Test::Unit::TestCase
end
def test_id
+ nil.stubs(:object_id).returns(999)
nil.id
rescue RuntimeError => nme
assert_no_match(/nil:NilClass/, nme.message)
+ assert_match(/999/, nme.message)
end
def test_no_to_ary_coercion