mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Require mocha >= 0.9.0 for AS tests
This commit is contained in:
parent
708f4c3ae6
commit
f927a60d0f
14 changed files with 555 additions and 601 deletions
|
@ -7,10 +7,6 @@ $:.unshift "#{File.dirname(__FILE__)}/../lib"
|
|||
require 'active_support'
|
||||
require 'active_support/test_case'
|
||||
|
||||
def uses_mocha(test_name, &block)
|
||||
yield
|
||||
end
|
||||
|
||||
def uses_memcached(test_name)
|
||||
require 'memcache'
|
||||
MemCache.new('localhost').stats
|
||||
|
|
|
@ -45,29 +45,27 @@ class CacheStoreSettingTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
uses_mocha 'high-level cache store tests' do
|
||||
class CacheStoreTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@cache = ActiveSupport::Cache.lookup_store(:memory_store)
|
||||
end
|
||||
class CacheStoreTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@cache = ActiveSupport::Cache.lookup_store(:memory_store)
|
||||
end
|
||||
|
||||
def test_fetch_without_cache_miss
|
||||
@cache.stubs(:read).with('foo', {}).returns('bar')
|
||||
@cache.expects(:write).never
|
||||
assert_equal 'bar', @cache.fetch('foo') { 'baz' }
|
||||
end
|
||||
def test_fetch_without_cache_miss
|
||||
@cache.stubs(:read).with('foo', {}).returns('bar')
|
||||
@cache.expects(:write).never
|
||||
assert_equal 'bar', @cache.fetch('foo') { 'baz' }
|
||||
end
|
||||
|
||||
def test_fetch_with_cache_miss
|
||||
@cache.stubs(:read).with('foo', {}).returns(nil)
|
||||
@cache.expects(:write).with('foo', 'baz', {})
|
||||
assert_equal 'baz', @cache.fetch('foo') { 'baz' }
|
||||
end
|
||||
def test_fetch_with_cache_miss
|
||||
@cache.stubs(:read).with('foo', {}).returns(nil)
|
||||
@cache.expects(:write).with('foo', 'baz', {})
|
||||
assert_equal 'baz', @cache.fetch('foo') { 'baz' }
|
||||
end
|
||||
|
||||
def test_fetch_with_forced_cache_miss
|
||||
@cache.expects(:read).never
|
||||
@cache.expects(:write).with('foo', 'bar', :force => true)
|
||||
@cache.fetch('foo', :force => true) { 'bar' }
|
||||
end
|
||||
def test_fetch_with_forced_cache_miss
|
||||
@cache.expects(:read).never
|
||||
@cache.expects(:write).with('foo', 'bar', :force => true)
|
||||
@cache.fetch('foo', :force => true) { 'bar' }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -290,16 +290,14 @@ class ArrayExtractOptionsTests < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
uses_mocha "ArrayExtRandomTests" do
|
||||
class ArrayExtRandomTests < Test::Unit::TestCase
|
||||
def test_random_element_from_array
|
||||
assert_nil [].rand
|
||||
class ArrayExtRandomTests < Test::Unit::TestCase
|
||||
def test_random_element_from_array
|
||||
assert_nil [].rand
|
||||
|
||||
Kernel.expects(:rand).with(1).returns(0)
|
||||
assert_equal 'x', ['x'].rand
|
||||
Kernel.expects(:rand).with(1).returns(0)
|
||||
assert_equal 'x', ['x'].rand
|
||||
|
||||
Kernel.expects(:rand).with(3).returns(1)
|
||||
assert_equal 2, [1, 2, 3].rand
|
||||
end
|
||||
Kernel.expects(:rand).with(3).returns(1)
|
||||
assert_equal 2, [1, 2, 3].rand
|
||||
end
|
||||
end
|
||||
|
|
|
@ -210,50 +210,46 @@ class DateExtCalculationsTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
uses_mocha 'past?, today? and future?' do
|
||||
def test_today
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, Date.new(1999, 12, 31).today?
|
||||
assert_equal true, Date.new(2000,1,1).today?
|
||||
assert_equal false, Date.new(2000,1,2).today?
|
||||
end
|
||||
def test_today
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, Date.new(1999, 12, 31).today?
|
||||
assert_equal true, Date.new(2000,1,1).today?
|
||||
assert_equal false, Date.new(2000,1,2).today?
|
||||
end
|
||||
|
||||
def test_past
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal true, Date.new(1999, 12, 31).past?
|
||||
assert_equal false, Date.new(2000,1,1).past?
|
||||
assert_equal false, Date.new(2000,1,2).past?
|
||||
end
|
||||
def test_past
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal true, Date.new(1999, 12, 31).past?
|
||||
assert_equal false, Date.new(2000,1,1).past?
|
||||
assert_equal false, Date.new(2000,1,2).past?
|
||||
end
|
||||
|
||||
def test_future
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, Date.new(1999, 12, 31).future?
|
||||
assert_equal false, Date.new(2000,1,1).future?
|
||||
assert_equal true, Date.new(2000,1,2).future?
|
||||
def test_future
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, Date.new(1999, 12, 31).future?
|
||||
assert_equal false, Date.new(2000,1,1).future?
|
||||
assert_equal true, Date.new(2000,1,2).future?
|
||||
end
|
||||
|
||||
def test_current_returns_date_today_when_zone_default_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
|
||||
end
|
||||
end
|
||||
|
||||
uses_mocha 'TestDateCurrent' do
|
||||
def test_current_returns_date_today_when_zone_default_not_set
|
||||
def test_current_returns_time_zone_today_when_zone_default_set
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
Time.zone_default = 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(1999, 12, 31), Date.current
|
||||
assert_equal Date.new(2000, 1, 1), Date.current
|
||||
end
|
||||
end
|
||||
|
||||
def test_current_returns_time_zone_today_when_zone_default_set
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
Time.zone_default = 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
|
||||
end
|
||||
end
|
||||
ensure
|
||||
Time.zone_default = nil
|
||||
end
|
||||
ensure
|
||||
Time.zone_default = nil
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -207,69 +207,65 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
|
|||
assert_match(/^2080-02-28T15:15:10-06:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10, -0.25).xmlschema)
|
||||
end
|
||||
|
||||
uses_mocha 'Test DateTime past?, today? and future?' do
|
||||
def test_today_with_offset
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, DateTime.civil(1999,12,31,23,59,59, Rational(-18000, 86400)).today?
|
||||
assert_equal true, DateTime.civil(2000,1,1,0,0,0, Rational(-18000, 86400)).today?
|
||||
assert_equal true, DateTime.civil(2000,1,1,23,59,59, Rational(-18000, 86400)).today?
|
||||
assert_equal false, DateTime.civil(2000,1,2,0,0,0, Rational(-18000, 86400)).today?
|
||||
end
|
||||
def test_today_with_offset
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, DateTime.civil(1999,12,31,23,59,59, Rational(-18000, 86400)).today?
|
||||
assert_equal true, DateTime.civil(2000,1,1,0,0,0, Rational(-18000, 86400)).today?
|
||||
assert_equal true, DateTime.civil(2000,1,1,23,59,59, Rational(-18000, 86400)).today?
|
||||
assert_equal false, DateTime.civil(2000,1,2,0,0,0, Rational(-18000, 86400)).today?
|
||||
end
|
||||
|
||||
def test_today_without_offset
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, DateTime.civil(1999,12,31,23,59,59).today?
|
||||
assert_equal true, DateTime.civil(2000,1,1,0).today?
|
||||
assert_equal true, DateTime.civil(2000,1,1,23,59,59).today?
|
||||
assert_equal false, DateTime.civil(2000,1,2,0).today?
|
||||
end
|
||||
def test_today_without_offset
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, DateTime.civil(1999,12,31,23,59,59).today?
|
||||
assert_equal true, DateTime.civil(2000,1,1,0).today?
|
||||
assert_equal true, DateTime.civil(2000,1,1,23,59,59).today?
|
||||
assert_equal false, DateTime.civil(2000,1,2,0).today?
|
||||
end
|
||||
|
||||
def test_past_with_offset
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
|
||||
assert_equal true, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).past?
|
||||
assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).past?
|
||||
assert_equal false, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).past?
|
||||
end
|
||||
def test_past_with_offset
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
|
||||
assert_equal true, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).past?
|
||||
assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).past?
|
||||
assert_equal false, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).past?
|
||||
end
|
||||
|
||||
def test_past_without_offset
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
|
||||
assert_equal true, DateTime.civil(2005,2,10,20,30,44).past?
|
||||
assert_equal false, DateTime.civil(2005,2,10,20,30,45).past?
|
||||
assert_equal false, DateTime.civil(2005,2,10,20,30,46).past?
|
||||
end
|
||||
def test_past_without_offset
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
|
||||
assert_equal true, DateTime.civil(2005,2,10,20,30,44).past?
|
||||
assert_equal false, DateTime.civil(2005,2,10,20,30,45).past?
|
||||
assert_equal false, DateTime.civil(2005,2,10,20,30,46).past?
|
||||
end
|
||||
|
||||
def test_future_with_offset
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
|
||||
assert_equal false, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).future?
|
||||
assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).future?
|
||||
assert_equal true, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).future?
|
||||
end
|
||||
def test_future_with_offset
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
|
||||
assert_equal false, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).future?
|
||||
assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).future?
|
||||
assert_equal true, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).future?
|
||||
end
|
||||
|
||||
def test_future_without_offset
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
|
||||
assert_equal false, DateTime.civil(2005,2,10,20,30,44).future?
|
||||
assert_equal false, DateTime.civil(2005,2,10,20,30,45).future?
|
||||
assert_equal true, DateTime.civil(2005,2,10,20,30,46).future?
|
||||
def test_future_without_offset
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
|
||||
assert_equal false, DateTime.civil(2005,2,10,20,30,44).future?
|
||||
assert_equal false, DateTime.civil(2005,2,10,20,30,45).future?
|
||||
assert_equal true, DateTime.civil(2005,2,10,20,30,46).future?
|
||||
end
|
||||
|
||||
def test_current_returns_date_today_when_zone_default_not_set
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59)
|
||||
assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
|
||||
end
|
||||
end
|
||||
|
||||
uses_mocha 'TestDateTimeCurrent' do
|
||||
def test_current_returns_date_today_when_zone_default_not_set
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59)
|
||||
assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
|
||||
end
|
||||
end
|
||||
|
||||
def test_current_returns_time_zone_today_when_zone_default_set
|
||||
Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59)
|
||||
assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
|
||||
end
|
||||
ensure
|
||||
Time.zone_default = nil
|
||||
def test_current_returns_time_zone_today_when_zone_default_set
|
||||
Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59)
|
||||
assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
|
||||
end
|
||||
ensure
|
||||
Time.zone_default = nil
|
||||
end
|
||||
|
||||
def test_current_without_time_zone
|
||||
|
|
|
@ -40,78 +40,76 @@ class DurationTest < ActiveSupport::TestCase
|
|||
assert_equal 86400 * 1.7, 1.7.days
|
||||
end
|
||||
|
||||
uses_mocha 'TestDurationSinceAndAgoWithCurrentTime' do
|
||||
def test_since_and_ago_with_fractional_days
|
||||
def test_since_and_ago_with_fractional_days
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
# since
|
||||
assert_equal 36.hours.since, 1.5.days.since
|
||||
assert_equal((24 * 1.7).hours.since, 1.7.days.since)
|
||||
# ago
|
||||
assert_equal 36.hours.ago, 1.5.days.ago
|
||||
assert_equal((24 * 1.7).hours.ago, 1.7.days.ago)
|
||||
end
|
||||
|
||||
def test_since_and_ago_with_fractional_weeks
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
# since
|
||||
assert_equal((7 * 36).hours.since, 1.5.weeks.since)
|
||||
assert_equal((7 * 24 * 1.7).hours.since, 1.7.weeks.since)
|
||||
# ago
|
||||
assert_equal((7 * 36).hours.ago, 1.5.weeks.ago)
|
||||
assert_equal((7 * 24 * 1.7).hours.ago, 1.7.weeks.ago)
|
||||
end
|
||||
|
||||
def test_deprecated_fractional_years
|
||||
years_re = /Fractional years are not respected\. Convert value to integer before calling #years\./
|
||||
assert_deprecated(years_re){1.0.years}
|
||||
assert_deprecated(years_re){1.5.years}
|
||||
assert_not_deprecated{1.years}
|
||||
assert_deprecated(years_re){1.0.year}
|
||||
assert_deprecated(years_re){1.5.year}
|
||||
assert_not_deprecated{1.year}
|
||||
end
|
||||
|
||||
def test_deprecated_fractional_months
|
||||
months_re = /Fractional months are not respected\. Convert value to integer before calling #months\./
|
||||
assert_deprecated(months_re){1.5.months}
|
||||
assert_deprecated(months_re){1.0.months}
|
||||
assert_not_deprecated{1.months}
|
||||
assert_deprecated(months_re){1.5.month}
|
||||
assert_deprecated(months_re){1.0.month}
|
||||
assert_not_deprecated{1.month}
|
||||
end
|
||||
|
||||
def test_since_and_ago_anchored_to_time_now_when_time_zone_default_not_set
|
||||
Time.zone_default = nil
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
# since
|
||||
assert_equal 36.hours.since, 1.5.days.since
|
||||
assert_equal((24 * 1.7).hours.since, 1.7.days.since)
|
||||
assert_equal false, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.local(2000,1,1,0,0,5), 5.seconds.since
|
||||
# ago
|
||||
assert_equal 36.hours.ago, 1.5.days.ago
|
||||
assert_equal((24 * 1.7).hours.ago, 1.7.days.ago)
|
||||
assert_equal false, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.local(1999,12,31,23,59,55), 5.seconds.ago
|
||||
end
|
||||
end
|
||||
|
||||
def test_since_and_ago_with_fractional_weeks
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
# since
|
||||
assert_equal((7 * 36).hours.since, 1.5.weeks.since)
|
||||
assert_equal((7 * 24 * 1.7).hours.since, 1.7.weeks.since)
|
||||
# ago
|
||||
assert_equal((7 * 36).hours.ago, 1.5.weeks.ago)
|
||||
assert_equal((7 * 24 * 1.7).hours.ago, 1.7.weeks.ago)
|
||||
end
|
||||
|
||||
def test_deprecated_fractional_years
|
||||
years_re = /Fractional years are not respected\. Convert value to integer before calling #years\./
|
||||
assert_deprecated(years_re){1.0.years}
|
||||
assert_deprecated(years_re){1.5.years}
|
||||
assert_not_deprecated{1.years}
|
||||
assert_deprecated(years_re){1.0.year}
|
||||
assert_deprecated(years_re){1.5.year}
|
||||
assert_not_deprecated{1.year}
|
||||
end
|
||||
|
||||
def test_deprecated_fractional_months
|
||||
months_re = /Fractional months are not respected\. Convert value to integer before calling #months\./
|
||||
assert_deprecated(months_re){1.5.months}
|
||||
assert_deprecated(months_re){1.0.months}
|
||||
assert_not_deprecated{1.months}
|
||||
assert_deprecated(months_re){1.5.month}
|
||||
assert_deprecated(months_re){1.0.month}
|
||||
assert_not_deprecated{1.month}
|
||||
end
|
||||
|
||||
def test_since_and_ago_anchored_to_time_now_when_time_zone_default_not_set
|
||||
Time.zone_default = nil
|
||||
def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
# since
|
||||
assert_equal false, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.local(2000,1,1,0,0,5), 5.seconds.since
|
||||
assert_equal true, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time
|
||||
assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name
|
||||
# ago
|
||||
assert_equal false, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.local(1999,12,31,23,59,55), 5.seconds.ago
|
||||
assert_equal true, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time
|
||||
assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name
|
||||
end
|
||||
end
|
||||
|
||||
def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
# since
|
||||
assert_equal true, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time
|
||||
assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name
|
||||
# ago
|
||||
assert_equal true, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time
|
||||
assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name
|
||||
end
|
||||
end
|
||||
ensure
|
||||
Time.zone_default = nil
|
||||
end
|
||||
ensure
|
||||
Time.zone_default = nil
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -358,12 +358,10 @@ class HashExtTest < Test::Unit::TestCase
|
|||
assert_nothing_raised { original.except(:a) }
|
||||
end
|
||||
|
||||
uses_mocha 'except with expectation' do
|
||||
def test_except_with_mocha_expectation_on_original
|
||||
original = { :a => 'x', :b => 'y' }
|
||||
original.expects(:delete).never
|
||||
original.except(:a)
|
||||
end
|
||||
def test_except_with_mocha_expectation_on_original
|
||||
original = { :a => 'x', :b => 'y' }
|
||||
original.expects(:delete).never
|
||||
original.except(:a)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -515,16 +515,14 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
|||
assert_equal 31, Time.days_in_month(12, 2005)
|
||||
end
|
||||
|
||||
uses_mocha 'TestTimeDaysInMonthWithoutYearArg' do
|
||||
def test_days_in_month_feb_in_common_year_without_year_arg
|
||||
Time.stubs(:now).returns(Time.utc(2007))
|
||||
assert_equal 28, Time.days_in_month(2)
|
||||
end
|
||||
def test_days_in_month_feb_in_common_year_without_year_arg
|
||||
Time.stubs(:now).returns(Time.utc(2007))
|
||||
assert_equal 28, Time.days_in_month(2)
|
||||
end
|
||||
|
||||
def test_days_in_month_feb_in_leap_year_without_year_arg
|
||||
Time.stubs(:now).returns(Time.utc(2008))
|
||||
assert_equal 29, Time.days_in_month(2)
|
||||
end
|
||||
def test_days_in_month_feb_in_leap_year_without_year_arg
|
||||
Time.stubs(:now).returns(Time.utc(2008))
|
||||
assert_equal 29, Time.days_in_month(2)
|
||||
end
|
||||
|
||||
def test_time_with_datetime_fallback
|
||||
|
@ -572,71 +570,69 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
|||
assert_nothing_raised { Time.now.xmlschema }
|
||||
end
|
||||
|
||||
uses_mocha 'Test Time past?, today? and future?' do
|
||||
def test_today_with_time_local
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, Time.local(1999,12,31,23,59,59).today?
|
||||
assert_equal true, Time.local(2000,1,1,0).today?
|
||||
assert_equal true, Time.local(2000,1,1,23,59,59).today?
|
||||
assert_equal false, Time.local(2000,1,2,0).today?
|
||||
end
|
||||
def test_today_with_time_local
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, Time.local(1999,12,31,23,59,59).today?
|
||||
assert_equal true, Time.local(2000,1,1,0).today?
|
||||
assert_equal true, Time.local(2000,1,1,23,59,59).today?
|
||||
assert_equal false, Time.local(2000,1,2,0).today?
|
||||
end
|
||||
|
||||
def test_today_with_time_utc
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, Time.utc(1999,12,31,23,59,59).today?
|
||||
assert_equal true, Time.utc(2000,1,1,0).today?
|
||||
assert_equal true, Time.utc(2000,1,1,23,59,59).today?
|
||||
assert_equal false, Time.utc(2000,1,2,0).today?
|
||||
end
|
||||
def test_today_with_time_utc
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, Time.utc(1999,12,31,23,59,59).today?
|
||||
assert_equal true, Time.utc(2000,1,1,0).today?
|
||||
assert_equal true, Time.utc(2000,1,1,23,59,59).today?
|
||||
assert_equal false, Time.utc(2000,1,2,0).today?
|
||||
end
|
||||
|
||||
def test_past_with_time_current_as_time_local
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
|
||||
assert_equal true, Time.local(2005,2,10,15,30,44).past?
|
||||
assert_equal false, Time.local(2005,2,10,15,30,45).past?
|
||||
assert_equal false, Time.local(2005,2,10,15,30,46).past?
|
||||
assert_equal true, Time.utc(2005,2,10,20,30,44).past?
|
||||
assert_equal false, Time.utc(2005,2,10,20,30,45).past?
|
||||
assert_equal false, Time.utc(2005,2,10,20,30,46).past?
|
||||
end
|
||||
def test_past_with_time_current_as_time_local
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
|
||||
assert_equal true, Time.local(2005,2,10,15,30,44).past?
|
||||
assert_equal false, Time.local(2005,2,10,15,30,45).past?
|
||||
assert_equal false, Time.local(2005,2,10,15,30,46).past?
|
||||
assert_equal true, Time.utc(2005,2,10,20,30,44).past?
|
||||
assert_equal false, Time.utc(2005,2,10,20,30,45).past?
|
||||
assert_equal false, Time.utc(2005,2,10,20,30,46).past?
|
||||
end
|
||||
end
|
||||
|
||||
def test_past_with_time_current_as_time_with_zone
|
||||
with_env_tz 'US/Eastern' do
|
||||
twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)')
|
||||
Time.stubs(:current).returns(twz)
|
||||
assert_equal true, Time.local(2005,2,10,10,30,44).past?
|
||||
assert_equal false, Time.local(2005,2,10,10,30,45).past?
|
||||
assert_equal false, Time.local(2005,2,10,10,30,46).past?
|
||||
assert_equal true, Time.utc(2005,2,10,15,30,44).past?
|
||||
assert_equal false, Time.utc(2005,2,10,15,30,45).past?
|
||||
assert_equal false, Time.utc(2005,2,10,15,30,46).past?
|
||||
end
|
||||
def test_past_with_time_current_as_time_with_zone
|
||||
with_env_tz 'US/Eastern' do
|
||||
twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)')
|
||||
Time.stubs(:current).returns(twz)
|
||||
assert_equal true, Time.local(2005,2,10,10,30,44).past?
|
||||
assert_equal false, Time.local(2005,2,10,10,30,45).past?
|
||||
assert_equal false, Time.local(2005,2,10,10,30,46).past?
|
||||
assert_equal true, Time.utc(2005,2,10,15,30,44).past?
|
||||
assert_equal false, Time.utc(2005,2,10,15,30,45).past?
|
||||
assert_equal false, Time.utc(2005,2,10,15,30,46).past?
|
||||
end
|
||||
end
|
||||
|
||||
def test_future_with_time_current_as_time_local
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
|
||||
assert_equal false, Time.local(2005,2,10,15,30,44).future?
|
||||
assert_equal false, Time.local(2005,2,10,15,30,45).future?
|
||||
assert_equal true, Time.local(2005,2,10,15,30,46).future?
|
||||
assert_equal false, Time.utc(2005,2,10,20,30,44).future?
|
||||
assert_equal false, Time.utc(2005,2,10,20,30,45).future?
|
||||
assert_equal true, Time.utc(2005,2,10,20,30,46).future?
|
||||
end
|
||||
def test_future_with_time_current_as_time_local
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
|
||||
assert_equal false, Time.local(2005,2,10,15,30,44).future?
|
||||
assert_equal false, Time.local(2005,2,10,15,30,45).future?
|
||||
assert_equal true, Time.local(2005,2,10,15,30,46).future?
|
||||
assert_equal false, Time.utc(2005,2,10,20,30,44).future?
|
||||
assert_equal false, Time.utc(2005,2,10,20,30,45).future?
|
||||
assert_equal true, Time.utc(2005,2,10,20,30,46).future?
|
||||
end
|
||||
end
|
||||
|
||||
def test_future_with_time_current_as_time_with_zone
|
||||
with_env_tz 'US/Eastern' do
|
||||
twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)')
|
||||
Time.stubs(:current).returns(twz)
|
||||
assert_equal false, Time.local(2005,2,10,10,30,44).future?
|
||||
assert_equal false, Time.local(2005,2,10,10,30,45).future?
|
||||
assert_equal true, Time.local(2005,2,10,10,30,46).future?
|
||||
assert_equal false, Time.utc(2005,2,10,15,30,44).future?
|
||||
assert_equal false, Time.utc(2005,2,10,15,30,45).future?
|
||||
assert_equal true, Time.utc(2005,2,10,15,30,46).future?
|
||||
end
|
||||
def test_future_with_time_current_as_time_with_zone
|
||||
with_env_tz 'US/Eastern' do
|
||||
twz = Time.utc(2005,2,10,15,30,45).in_time_zone('Central Time (US & Canada)')
|
||||
Time.stubs(:current).returns(twz)
|
||||
assert_equal false, Time.local(2005,2,10,10,30,44).future?
|
||||
assert_equal false, Time.local(2005,2,10,10,30,45).future?
|
||||
assert_equal true, Time.local(2005,2,10,10,30,46).future?
|
||||
assert_equal false, Time.utc(2005,2,10,15,30,44).future?
|
||||
assert_equal false, Time.utc(2005,2,10,15,30,45).future?
|
||||
assert_equal true, Time.utc(2005,2,10,15,30,46).future?
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -152,50 +152,48 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
|||
assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2))
|
||||
end
|
||||
|
||||
uses_mocha 'TimeWithZone past?, today? and future?' do
|
||||
def test_today
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(1999,12,31,23,59,59) ).today?
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,0) ).today?
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,23,59,59) ).today?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,2,0) ).today?
|
||||
end
|
||||
def test_today
|
||||
Date.stubs(:current).returns(Date.new(2000, 1, 1))
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(1999,12,31,23,59,59) ).today?
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,0) ).today?
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,23,59,59) ).today?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,2,0) ).today?
|
||||
end
|
||||
|
||||
def test_past_with_time_current_as_time_local
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
|
||||
end
|
||||
end
|
||||
|
||||
def test_past_with_time_current_as_time_with_zone
|
||||
twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
|
||||
Time.stubs(:current).returns(twz)
|
||||
def test_past_with_time_current_as_time_local
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
|
||||
end
|
||||
end
|
||||
|
||||
def test_future_with_time_current_as_time_local
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
|
||||
end
|
||||
end
|
||||
def test_past_with_time_current_as_time_with_zone
|
||||
twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
|
||||
Time.stubs(:current).returns(twz)
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
|
||||
end
|
||||
|
||||
def future_with_time_current_as_time_with_zone
|
||||
twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
|
||||
Time.stubs(:current).returns(twz)
|
||||
def test_future_with_time_current_as_time_local
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
|
||||
end
|
||||
end
|
||||
|
||||
def future_with_time_current_as_time_with_zone
|
||||
twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
|
||||
Time.stubs(:current).returns(twz)
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
|
||||
assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
|
||||
assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
|
||||
end
|
||||
|
||||
def test_eql?
|
||||
assert @twz.eql?(Time.utc(2000))
|
||||
assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
|
||||
|
@ -399,28 +397,26 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
uses_mocha 'TestDatePartValueMethods' do
|
||||
def test_method_missing_with_non_time_return_value
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
@twz.time.expects(:foo).returns('bar')
|
||||
assert_equal 'bar', @twz.foo
|
||||
end
|
||||
def test_method_missing_with_non_time_return_value
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
@twz.time.expects(:foo).returns('bar')
|
||||
assert_equal 'bar', @twz.foo
|
||||
end
|
||||
end
|
||||
|
||||
def test_date_part_value_methods
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
|
||||
twz.expects(:method_missing).never
|
||||
assert_equal 1999, twz.year
|
||||
assert_equal 12, twz.month
|
||||
assert_equal 31, twz.day
|
||||
assert_equal 14, twz.hour
|
||||
assert_equal 18, twz.min
|
||||
assert_equal 17, twz.sec
|
||||
assert_equal 500, twz.usec
|
||||
assert_equal 5, twz.wday
|
||||
assert_equal 365, twz.yday
|
||||
end
|
||||
def test_date_part_value_methods
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
|
||||
twz.expects(:method_missing).never
|
||||
assert_equal 1999, twz.year
|
||||
assert_equal 12, twz.month
|
||||
assert_equal 31, twz.day
|
||||
assert_equal 14, twz.hour
|
||||
assert_equal 18, twz.min
|
||||
assert_equal 17, twz.sec
|
||||
assert_equal 500, twz.usec
|
||||
assert_equal 5, twz.wday
|
||||
assert_equal 365, twz.yday
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -885,28 +881,26 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase
|
|||
assert_equal nil, Time.zone
|
||||
end
|
||||
|
||||
uses_mocha 'TestTimeCurrent' do
|
||||
def test_current_returns_time_now_when_zone_default_not_set
|
||||
def test_current_returns_time_now_when_zone_default_not_set
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.local(2000), Time.current
|
||||
end
|
||||
end
|
||||
|
||||
def test_current_returns_time_zone_now_when_zone_default_set
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal Time.local(2000), Time.current
|
||||
assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name
|
||||
assert_equal Time.utc(2000), Time.current.time
|
||||
end
|
||||
end
|
||||
|
||||
def test_current_returns_time_zone_now_when_zone_default_set
|
||||
silence_warnings do # silence warnings raised by tzinfo gem
|
||||
Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns Time.local(2000)
|
||||
assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone)
|
||||
assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name
|
||||
assert_equal Time.utc(2000), Time.current.time
|
||||
end
|
||||
end
|
||||
ensure
|
||||
Time.zone_default = nil
|
||||
end
|
||||
ensure
|
||||
Time.zone_default = nil
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -6,11 +6,9 @@ class I18nTest < Test::Unit::TestCase
|
|||
@time = Time.utc(2008, 7, 2, 16, 47, 1)
|
||||
end
|
||||
|
||||
uses_mocha 'I18nTimeZoneTest' do
|
||||
def test_time_zone_localization_with_default_format
|
||||
Time.zone.stubs(:now).returns Time.local(2000)
|
||||
assert_equal Time.zone.now.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(Time.zone.now)
|
||||
end
|
||||
def test_time_zone_localization_with_default_format
|
||||
Time.zone.stubs(:now).returns Time.local(2000)
|
||||
assert_equal Time.zone.now.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(Time.zone.now)
|
||||
end
|
||||
|
||||
def test_date_localization_should_use_default_format
|
||||
|
|
|
@ -126,15 +126,13 @@ class TestJSONEncoding < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
uses_mocha 'JsonOptionsTests' do
|
||||
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)
|
||||
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'].to_json(json_options)
|
||||
end
|
||||
[1, 2, 'foo'].to_json(json_options)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,218 +1,216 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
uses_mocha 'Memoizable' do
|
||||
class MemoizableTest < Test::Unit::TestCase
|
||||
class Person
|
||||
extend ActiveSupport::Memoizable
|
||||
class MemoizableTest < Test::Unit::TestCase
|
||||
class Person
|
||||
extend ActiveSupport::Memoizable
|
||||
|
||||
attr_reader :name_calls, :age_calls
|
||||
def initialize
|
||||
@name_calls = 0
|
||||
@age_calls = 0
|
||||
end
|
||||
|
||||
def name
|
||||
@name_calls += 1
|
||||
"Josh"
|
||||
end
|
||||
|
||||
def name?
|
||||
true
|
||||
end
|
||||
memoize :name?
|
||||
|
||||
def update(name)
|
||||
"Joshua"
|
||||
end
|
||||
memoize :update
|
||||
|
||||
def age
|
||||
@age_calls += 1
|
||||
nil
|
||||
end
|
||||
|
||||
memoize :name, :age
|
||||
attr_reader :name_calls, :age_calls
|
||||
def initialize
|
||||
@name_calls = 0
|
||||
@age_calls = 0
|
||||
end
|
||||
|
||||
class Company
|
||||
attr_reader :name_calls
|
||||
def initialize
|
||||
@name_calls = 0
|
||||
end
|
||||
|
||||
def name
|
||||
@name_calls += 1
|
||||
"37signals"
|
||||
end
|
||||
def name
|
||||
@name_calls += 1
|
||||
"Josh"
|
||||
end
|
||||
|
||||
module Rates
|
||||
extend ActiveSupport::Memoizable
|
||||
def name?
|
||||
true
|
||||
end
|
||||
memoize :name?
|
||||
|
||||
attr_reader :sales_tax_calls
|
||||
def sales_tax(price)
|
||||
@sales_tax_calls ||= 0
|
||||
@sales_tax_calls += 1
|
||||
price * 0.1025
|
||||
end
|
||||
memoize :sales_tax
|
||||
def update(name)
|
||||
"Joshua"
|
||||
end
|
||||
memoize :update
|
||||
|
||||
def age
|
||||
@age_calls += 1
|
||||
nil
|
||||
end
|
||||
|
||||
class Calculator
|
||||
extend ActiveSupport::Memoizable
|
||||
include Rates
|
||||
memoize :name, :age
|
||||
end
|
||||
|
||||
attr_reader :fib_calls
|
||||
def initialize
|
||||
@fib_calls = 0
|
||||
end
|
||||
|
||||
def fib(n)
|
||||
@fib_calls += 1
|
||||
|
||||
if n == 0 || n == 1
|
||||
n
|
||||
else
|
||||
fib(n - 1) + fib(n - 2)
|
||||
end
|
||||
end
|
||||
memoize :fib
|
||||
|
||||
def counter
|
||||
@count ||= 0
|
||||
@count += 1
|
||||
end
|
||||
memoize :counter
|
||||
class Company
|
||||
attr_reader :name_calls
|
||||
def initialize
|
||||
@name_calls = 0
|
||||
end
|
||||
|
||||
def setup
|
||||
@person = Person.new
|
||||
@calculator = Calculator.new
|
||||
end
|
||||
|
||||
def test_memoization
|
||||
assert_equal "Josh", @person.name
|
||||
assert_equal 1, @person.name_calls
|
||||
|
||||
3.times { assert_equal "Josh", @person.name }
|
||||
assert_equal 1, @person.name_calls
|
||||
end
|
||||
|
||||
def test_memoization_with_punctuation
|
||||
assert_equal true, @person.name?
|
||||
|
||||
assert_nothing_raised(NameError) do
|
||||
@person.memoize_all
|
||||
@person.unmemoize_all
|
||||
end
|
||||
end
|
||||
|
||||
def test_memoization_with_nil_value
|
||||
assert_equal nil, @person.age
|
||||
assert_equal 1, @person.age_calls
|
||||
|
||||
3.times { assert_equal nil, @person.age }
|
||||
assert_equal 1, @person.age_calls
|
||||
end
|
||||
|
||||
def test_memorized_results_are_immutable
|
||||
assert_equal "Josh", @person.name
|
||||
assert_raise(ActiveSupport::FrozenObjectError) { @person.name.gsub!("Josh", "Gosh") }
|
||||
end
|
||||
|
||||
def test_reloadable
|
||||
counter = @calculator.counter
|
||||
assert_equal 1, @calculator.counter
|
||||
assert_equal 2, @calculator.counter(:reload)
|
||||
assert_equal 2, @calculator.counter
|
||||
assert_equal 3, @calculator.counter(true)
|
||||
assert_equal 3, @calculator.counter
|
||||
end
|
||||
|
||||
def test_unmemoize_all
|
||||
assert_equal 1, @calculator.counter
|
||||
|
||||
assert @calculator.instance_variable_get(:@_memoized_counter).any?
|
||||
@calculator.unmemoize_all
|
||||
assert @calculator.instance_variable_get(:@_memoized_counter).empty?
|
||||
|
||||
assert_equal 2, @calculator.counter
|
||||
end
|
||||
|
||||
def test_memoize_all
|
||||
@calculator.memoize_all
|
||||
assert @calculator.instance_variable_defined?(:@_memoized_counter)
|
||||
end
|
||||
|
||||
def test_memoization_cache_is_different_for_each_instance
|
||||
assert_equal 1, @calculator.counter
|
||||
assert_equal 2, @calculator.counter(:reload)
|
||||
assert_equal 1, Calculator.new.counter
|
||||
end
|
||||
|
||||
def test_memoized_is_not_affected_by_freeze
|
||||
@person.freeze
|
||||
assert_equal "Josh", @person.name
|
||||
assert_equal "Joshua", @person.update("Joshua")
|
||||
end
|
||||
|
||||
def test_memoization_with_args
|
||||
assert_equal 55, @calculator.fib(10)
|
||||
assert_equal 11, @calculator.fib_calls
|
||||
end
|
||||
|
||||
def test_reloadable_with_args
|
||||
assert_equal 55, @calculator.fib(10)
|
||||
assert_equal 11, @calculator.fib_calls
|
||||
assert_equal 55, @calculator.fib(10, :reload)
|
||||
assert_equal 12, @calculator.fib_calls
|
||||
assert_equal 55, @calculator.fib(10, true)
|
||||
assert_equal 13, @calculator.fib_calls
|
||||
end
|
||||
|
||||
def test_object_memoization
|
||||
[Company.new, Company.new, Company.new].each do |company|
|
||||
company.extend ActiveSupport::Memoizable
|
||||
company.memoize :name
|
||||
|
||||
assert_equal "37signals", company.name
|
||||
assert_equal 1, company.name_calls
|
||||
assert_equal "37signals", company.name
|
||||
assert_equal 1, company.name_calls
|
||||
end
|
||||
end
|
||||
|
||||
def test_memoized_module_methods
|
||||
assert_equal 1.025, @calculator.sales_tax(10)
|
||||
assert_equal 1, @calculator.sales_tax_calls
|
||||
assert_equal 1.025, @calculator.sales_tax(10)
|
||||
assert_equal 1, @calculator.sales_tax_calls
|
||||
assert_equal 2.5625, @calculator.sales_tax(25)
|
||||
assert_equal 2, @calculator.sales_tax_calls
|
||||
end
|
||||
|
||||
def test_object_memoized_module_methods
|
||||
company = Company.new
|
||||
company.extend(Rates)
|
||||
|
||||
assert_equal 1.025, company.sales_tax(10)
|
||||
assert_equal 1, company.sales_tax_calls
|
||||
assert_equal 1.025, company.sales_tax(10)
|
||||
assert_equal 1, company.sales_tax_calls
|
||||
assert_equal 2.5625, company.sales_tax(25)
|
||||
assert_equal 2, company.sales_tax_calls
|
||||
end
|
||||
|
||||
def test_double_memoization
|
||||
assert_raise(RuntimeError) { Person.memoize :name }
|
||||
person = Person.new
|
||||
person.extend ActiveSupport::Memoizable
|
||||
assert_raise(RuntimeError) { person.memoize :name }
|
||||
|
||||
company = Company.new
|
||||
company.extend ActiveSupport::Memoizable
|
||||
company.memoize :name
|
||||
assert_raise(RuntimeError) { company.memoize :name }
|
||||
def name
|
||||
@name_calls += 1
|
||||
"37signals"
|
||||
end
|
||||
end
|
||||
|
||||
module Rates
|
||||
extend ActiveSupport::Memoizable
|
||||
|
||||
attr_reader :sales_tax_calls
|
||||
def sales_tax(price)
|
||||
@sales_tax_calls ||= 0
|
||||
@sales_tax_calls += 1
|
||||
price * 0.1025
|
||||
end
|
||||
memoize :sales_tax
|
||||
end
|
||||
|
||||
class Calculator
|
||||
extend ActiveSupport::Memoizable
|
||||
include Rates
|
||||
|
||||
attr_reader :fib_calls
|
||||
def initialize
|
||||
@fib_calls = 0
|
||||
end
|
||||
|
||||
def fib(n)
|
||||
@fib_calls += 1
|
||||
|
||||
if n == 0 || n == 1
|
||||
n
|
||||
else
|
||||
fib(n - 1) + fib(n - 2)
|
||||
end
|
||||
end
|
||||
memoize :fib
|
||||
|
||||
def counter
|
||||
@count ||= 0
|
||||
@count += 1
|
||||
end
|
||||
memoize :counter
|
||||
end
|
||||
|
||||
def setup
|
||||
@person = Person.new
|
||||
@calculator = Calculator.new
|
||||
end
|
||||
|
||||
def test_memoization
|
||||
assert_equal "Josh", @person.name
|
||||
assert_equal 1, @person.name_calls
|
||||
|
||||
3.times { assert_equal "Josh", @person.name }
|
||||
assert_equal 1, @person.name_calls
|
||||
end
|
||||
|
||||
def test_memoization_with_punctuation
|
||||
assert_equal true, @person.name?
|
||||
|
||||
assert_nothing_raised(NameError) do
|
||||
@person.memoize_all
|
||||
@person.unmemoize_all
|
||||
end
|
||||
end
|
||||
|
||||
def test_memoization_with_nil_value
|
||||
assert_equal nil, @person.age
|
||||
assert_equal 1, @person.age_calls
|
||||
|
||||
3.times { assert_equal nil, @person.age }
|
||||
assert_equal 1, @person.age_calls
|
||||
end
|
||||
|
||||
def test_memorized_results_are_immutable
|
||||
assert_equal "Josh", @person.name
|
||||
assert_raise(ActiveSupport::FrozenObjectError) { @person.name.gsub!("Josh", "Gosh") }
|
||||
end
|
||||
|
||||
def test_reloadable
|
||||
counter = @calculator.counter
|
||||
assert_equal 1, @calculator.counter
|
||||
assert_equal 2, @calculator.counter(:reload)
|
||||
assert_equal 2, @calculator.counter
|
||||
assert_equal 3, @calculator.counter(true)
|
||||
assert_equal 3, @calculator.counter
|
||||
end
|
||||
|
||||
def test_unmemoize_all
|
||||
assert_equal 1, @calculator.counter
|
||||
|
||||
assert @calculator.instance_variable_get(:@_memoized_counter).any?
|
||||
@calculator.unmemoize_all
|
||||
assert @calculator.instance_variable_get(:@_memoized_counter).empty?
|
||||
|
||||
assert_equal 2, @calculator.counter
|
||||
end
|
||||
|
||||
def test_memoize_all
|
||||
@calculator.memoize_all
|
||||
assert @calculator.instance_variable_defined?(:@_memoized_counter)
|
||||
end
|
||||
|
||||
def test_memoization_cache_is_different_for_each_instance
|
||||
assert_equal 1, @calculator.counter
|
||||
assert_equal 2, @calculator.counter(:reload)
|
||||
assert_equal 1, Calculator.new.counter
|
||||
end
|
||||
|
||||
def test_memoized_is_not_affected_by_freeze
|
||||
@person.freeze
|
||||
assert_equal "Josh", @person.name
|
||||
assert_equal "Joshua", @person.update("Joshua")
|
||||
end
|
||||
|
||||
def test_memoization_with_args
|
||||
assert_equal 55, @calculator.fib(10)
|
||||
assert_equal 11, @calculator.fib_calls
|
||||
end
|
||||
|
||||
def test_reloadable_with_args
|
||||
assert_equal 55, @calculator.fib(10)
|
||||
assert_equal 11, @calculator.fib_calls
|
||||
assert_equal 55, @calculator.fib(10, :reload)
|
||||
assert_equal 12, @calculator.fib_calls
|
||||
assert_equal 55, @calculator.fib(10, true)
|
||||
assert_equal 13, @calculator.fib_calls
|
||||
end
|
||||
|
||||
def test_object_memoization
|
||||
[Company.new, Company.new, Company.new].each do |company|
|
||||
company.extend ActiveSupport::Memoizable
|
||||
company.memoize :name
|
||||
|
||||
assert_equal "37signals", company.name
|
||||
assert_equal 1, company.name_calls
|
||||
assert_equal "37signals", company.name
|
||||
assert_equal 1, company.name_calls
|
||||
end
|
||||
end
|
||||
|
||||
def test_memoized_module_methods
|
||||
assert_equal 1.025, @calculator.sales_tax(10)
|
||||
assert_equal 1, @calculator.sales_tax_calls
|
||||
assert_equal 1.025, @calculator.sales_tax(10)
|
||||
assert_equal 1, @calculator.sales_tax_calls
|
||||
assert_equal 2.5625, @calculator.sales_tax(25)
|
||||
assert_equal 2, @calculator.sales_tax_calls
|
||||
end
|
||||
|
||||
def test_object_memoized_module_methods
|
||||
company = Company.new
|
||||
company.extend(Rates)
|
||||
|
||||
assert_equal 1.025, company.sales_tax(10)
|
||||
assert_equal 1, company.sales_tax_calls
|
||||
assert_equal 1.025, company.sales_tax(10)
|
||||
assert_equal 1, company.sales_tax_calls
|
||||
assert_equal 2.5625, company.sales_tax(25)
|
||||
assert_equal 2, company.sales_tax_calls
|
||||
end
|
||||
|
||||
def test_double_memoization
|
||||
assert_raise(RuntimeError) { Person.memoize :name }
|
||||
person = Person.new
|
||||
person.extend ActiveSupport::Memoizable
|
||||
assert_raise(RuntimeError) { person.memoize :name }
|
||||
|
||||
company = Company.new
|
||||
company.extend ActiveSupport::Memoizable
|
||||
company.memoize :name
|
||||
assert_raise(RuntimeError) { company.memoize :name }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require 'abstract_unit'
|
||||
|
||||
uses_mocha "MultibyteUnicodeDatabaseTest" do
|
||||
|
||||
class MultibyteUnicodeDatabaseTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@ucd = ActiveSupport::Multibyte::UnicodeDatabase.new
|
||||
end
|
||||
|
@ -24,5 +20,3 @@ class MultibyteUnicodeDatabaseTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -57,46 +57,44 @@ class TimeZoneTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
uses_mocha 'TestTimeZoneNowAndToday' do
|
||||
def test_now
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns(Time.local(2000))
|
||||
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
assert_instance_of ActiveSupport::TimeWithZone, zone.now
|
||||
assert_equal Time.utc(2000,1,1,5), zone.now.utc
|
||||
assert_equal Time.utc(2000), zone.now.time
|
||||
assert_equal zone, zone.now.time_zone
|
||||
end
|
||||
def test_now
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns(Time.local(2000))
|
||||
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
assert_instance_of ActiveSupport::TimeWithZone, zone.now
|
||||
assert_equal Time.utc(2000,1,1,5), zone.now.utc
|
||||
assert_equal Time.utc(2000), zone.now.time
|
||||
assert_equal zone, zone.now.time_zone
|
||||
end
|
||||
end
|
||||
|
||||
def test_now_enforces_spring_dst_rules
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns(Time.local(2006,4,2,2)) # 2AM springs forward to 3AM
|
||||
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
assert_equal Time.utc(2006,4,2,3), zone.now.time
|
||||
assert_equal true, zone.now.dst?
|
||||
end
|
||||
def test_now_enforces_spring_dst_rules
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns(Time.local(2006,4,2,2)) # 2AM springs forward to 3AM
|
||||
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
assert_equal Time.utc(2006,4,2,3), zone.now.time
|
||||
assert_equal true, zone.now.dst?
|
||||
end
|
||||
end
|
||||
|
||||
def test_now_enforces_fall_dst_rules
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns(Time.at(1162098000)) # equivalent to 1AM DST
|
||||
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
assert_equal Time.utc(2006,10,29,1), zone.now.time
|
||||
assert_equal true, zone.now.dst?
|
||||
end
|
||||
def test_now_enforces_fall_dst_rules
|
||||
with_env_tz 'US/Eastern' do
|
||||
Time.stubs(:now).returns(Time.at(1162098000)) # equivalent to 1AM DST
|
||||
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
assert_equal Time.utc(2006,10,29,1), zone.now.time
|
||||
assert_equal true, zone.now.dst?
|
||||
end
|
||||
end
|
||||
|
||||
def test_today
|
||||
Time.stubs(:now).returns(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST
|
||||
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
||||
Time.stubs(:now).returns(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST
|
||||
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
||||
Time.stubs(:now).returns(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST
|
||||
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
||||
Time.stubs(:now).returns(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
|
||||
assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
||||
end
|
||||
def test_today
|
||||
Time.stubs(:now).returns(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST
|
||||
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
||||
Time.stubs(:now).returns(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST
|
||||
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
||||
Time.stubs(:now).returns(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST
|
||||
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
||||
Time.stubs(:now).returns(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
|
||||
assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
||||
end
|
||||
|
||||
def test_local
|
||||
|
@ -206,13 +204,11 @@ class TimeZoneTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
uses_mocha 'TestParseWithIncompleteDate' do
|
||||
def test_parse_with_incomplete_date
|
||||
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
zone.stubs(:now).returns zone.local(1999,12,31)
|
||||
twz = zone.parse('19:00:00')
|
||||
assert_equal Time.utc(1999,12,31,19), twz.time
|
||||
end
|
||||
def test_parse_with_incomplete_date
|
||||
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
||||
zone.stubs(:now).returns zone.local(1999,12,31)
|
||||
twz = zone.parse('19:00:00')
|
||||
assert_equal Time.utc(1999,12,31,19), twz.time
|
||||
end
|
||||
|
||||
def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize
|
||||
|
|
Loading…
Reference in a new issue