diff options
author | Xavier Noria <fxn@hashref.com> | 2010-06-05 01:13:37 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-06-05 01:15:17 +0200 |
commit | 67a43554f153a3ddb97039b5fac305c0619dd372 (patch) | |
tree | defe65bc4dec8844525c93a348dfc9a97eacf237 /activesupport/test/core_ext | |
parent | 6401ab587021b78c3dc5e4a5ac831823a9258481 (diff) | |
download | rails-67a43554f153a3ddb97039b5fac305c0619dd372.tar.gz rails-67a43554f153a3ddb97039b5fac305c0619dd372.tar.bz2 rails-67a43554f153a3ddb97039b5fac305c0619dd372.zip |
removes Array#random_element and backports Array#sample from Ruby 1.9, thanks to Marc-Andre Lafortune
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 30 |
1 files changed, 23 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 |