aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb30
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb10
-rw-r--r--activesupport/test/dependencies_test.rb21
3 files changed, 54 insertions, 7 deletions
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb
index 1f7cdb8ec1..54376deee5 100644
--- a/activesupport/test/core_ext/array_ext_test.rb
+++ b/activesupport/test/core_ext/array_ext_test.rb
@@ -359,14 +359,30 @@ class ArrayUniqByTests < Test::Unit::TestCase
end
class ArrayExtRandomTests < ActiveSupport::TestCase
- def test_random_element_from_array
- assert_nil [].random_element
-
- Kernel.expects(:rand).with(1).returns(0)
- assert_equal 'x', ['x'].random_element
+ def test_sample_from_array
+ assert_nil [].sample
+ assert_equal [], [].sample(5)
+ assert_equal 42, [42].sample
+ assert_equal [42], [42].sample(5)
+
+ a = [:foo, :bar, 42]
+ s = a.sample(2)
+ assert_equal 2, s.size
+ assert_equal 1, (a-s).size
+ assert_equal [], a-(0..20).sum{a.sample(2)}
+
+ o = Object.new
+ def o.to_int; 1; end
+ assert_equal [0], [0].sample(o)
+
+ o = Object.new
+ assert_raises(TypeError) { [0].sample(o) }
+
+ o = Object.new
+ def o.to_int; ''; end
+ assert_raises(TypeError) { [0].sample(o) }
- Kernel.expects(:rand).with(3).returns(1)
- assert_equal 2, [1, 2, 3].random_element
+ assert_raises(ArgumentError) { [0].sample(-7) }
end
end
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index a3a2f13436..3e003b3ed4 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -198,6 +198,16 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal Date.new(2005,2,28), Date.new(2004,2,29).advance(:years => 1) #leap day plus one year
end
+ def test_advance_does_first_years_and_then_days
+ assert_equal Date.new(2012, 2, 29), Date.new(2011, 2, 28).advance(:years => 1, :days => 1)
+ # If day was done first we would jump to 2012-03-01 instead.
+ end
+
+ def test_advance_does_first_months_and_then_days
+ assert_equal Date.new(2010, 3, 29), Date.new(2010, 2, 28).advance(:months => 1, :days => 1)
+ # If day was done first we would jump to 2010-04-01 instead.
+ end
+
def test_advance_in_calendar_reform
assert_equal Date.new(1582,10,15), Date.new(1582,10,4).advance(:days => 1)
assert_equal Date.new(1582,10,4), Date.new(1582,10,15).advance(:days => -1)
diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb
index 75ff88505d..5422c75f5f 100644
--- a/activesupport/test/dependencies_test.rb
+++ b/activesupport/test/dependencies_test.rb
@@ -431,6 +431,27 @@ class DependenciesTest < Test::Unit::TestCase
end
end
+ def test_references_should_work
+ with_loading 'dependencies' do
+ root = ActiveSupport::Dependencies.load_paths.first
+ c = ActiveSupport::Dependencies.ref("ServiceOne")
+ service_one_first = ServiceOne
+ assert_equal service_one_first, c.get
+ ActiveSupport::Dependencies.clear
+ assert ! defined?(ServiceOne)
+
+ service_one_second = ServiceOne
+ assert_not_equal service_one_first, c.get
+ assert_equal service_one_second, c.get
+ end
+ end
+
+ def test_constantize_shortcut_for_cached_constant_lookups
+ with_loading 'dependencies' do
+ assert_equal ServiceOne, ActiveSupport::Dependencies.constantize("ServiceOne")
+ end
+ end
+
def test_nested_load_error_isnt_rescued
with_loading 'dependencies' do
assert_raise(MissingSourceFile) do