From 3f29043f456af423c4ec7599040625c87321aa77 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 15 Sep 2007 21:39:04 +0000 Subject: [PATCH] Added Array#rand (closes #9170) [norbert] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7486 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 6 ++++++ activesupport/Rakefile | 1 - .../lib/active_support/core_ext/array.rb | 6 ++++-- activesupport/test/abstract_unit.rb | 9 +++++++++ activesupport/test/core_ext/array_ext_test.rb | 16 ++++++++++++++++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index cf199efd99..915b206bc7 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,11 @@ *SVN* +* Added Array#rand #9170 [norbert]. Examples: + + [].rand # => nil + ['a'].rand # => 'a' + [1,2,3].rand # => 1 or 2 or 3 + * Deprecation: removed Reloadable. [Jeremy Kemper] * Make the utf-handler return the correct value for non-matching regular expressions. Closes #9049 [manfred] diff --git a/activesupport/Rakefile b/activesupport/Rakefile index 5bccc3ca4c..65e928e811 100644 --- a/activesupport/Rakefile +++ b/activesupport/Rakefile @@ -18,7 +18,6 @@ task :default => :test Rake::TestTask.new { |t| t.pattern = 'test/**/*_test.rb' t.verbose = true - t.warning = true } # Create compressed packages diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb index 11003e2c0b..1d18900bab 100644 --- a/activesupport/lib/active_support/core_ext/array.rb +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -1,9 +1,11 @@ require File.dirname(__FILE__) + '/array/conversions' -require File.dirname(__FILE__) + '/array/grouping' require File.dirname(__FILE__) + '/array/extract_options' +require File.dirname(__FILE__) + '/array/grouping' +require File.dirname(__FILE__) + '/array/random_access' class Array #:nodoc: include ActiveSupport::CoreExtensions::Array::Conversions - include ActiveSupport::CoreExtensions::Array::Grouping include ActiveSupport::CoreExtensions::Array::ExtractOptions + include ActiveSupport::CoreExtensions::Array::Grouping + include ActiveSupport::CoreExtensions::Array::RandomAccess end diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 8e237beac2..fba9ff97b2 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -3,5 +3,14 @@ require 'test/unit' $:.unshift "#{File.dirname(__FILE__)}/../lib" require 'active_support' +# Wrap tests that use Mocha and skip if unavailable. +def uses_mocha(test_name) + require 'rubygems' + require 'mocha' + yield +rescue LoadError + $stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again." +end + # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 8c2edfc457..a5bae8c900 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -200,3 +200,19 @@ class ArrayExtractOptionsTests < Test::Unit::TestCase assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!) end end + +uses_mocha "ArrayExtRandomTests" do + +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(3).returns(1) + assert_equal 2, [1, 2, 3].rand + end +end + +end