-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added patch and tests for issue faker-ruby/faker#94
- Loading branch information
1 parent
45ac7bd
commit 952aab0
Showing
2 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
class Array | ||
unless self.method_defined? :sample | ||
def sample | ||
choice | ||
def sample(n = nil) | ||
#based on code from https://github.com/marcandre/backports | ||
size = self.length | ||
return self[Kernel.rand(size)] if n.nil? | ||
|
||
n = n.to_int | ||
raise ArgumentError, "negative array size" if n < 0 | ||
|
||
n = size if n > size | ||
|
||
result = Array.new(self) | ||
n.times do |i| | ||
r = i + Kernel.rand(size - i) | ||
result[i], result[r] = result[r], result[i] | ||
end | ||
result[n..size] = [] | ||
result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require File.dirname(__FILE__) + '/test_helper.rb' | ||
|
||
# when these tests are run under Ruby 1.8.7, they will use the | ||
# self-defined Array#sample method in lib/extensions/array and will test whether it | ||
# behaves as the built-in Array#sample method from Ruby 1.9 or greater. | ||
# Under Ruby 1.9, they simply use the built-in Array#sample method | ||
class TestArraySampleMethodCompatibility < Test::Unit::TestCase | ||
|
||
def test_returns_nil_or_empty_array_with_empty_source | ||
source = [] | ||
|
||
result = source.sample | ||
assert result.nil? | ||
|
||
result = source.sample(1) | ||
assert_equal result, [] | ||
end | ||
|
||
def test_returns_one_array_elem_without_param | ||
source = ['foo', 'bar'] | ||
result = source.sample | ||
assert source.include? result | ||
end | ||
|
||
def test_returns_empty_array_with_param_zero | ||
source = ['foo', 'bar'] | ||
result = source.sample(0) | ||
assert_equal result, [] | ||
end | ||
|
||
def test_returns_an_array_with_integer_param | ||
source = ['foo', 'bar', 'baz'] | ||
result = source.sample(2) | ||
assert result.is_a? Array | ||
assert result.length == 2 | ||
assert((result - source).empty?) | ||
end | ||
|
||
def test_returns_source_array_with_integer_param_equal_or_bigger_than_source_length | ||
source = ['foo','bar'] | ||
result = source.sample(2) | ||
assert result.is_a? Array | ||
assert((source.sort <=> result.sort) == 0) | ||
|
||
result = source.sample(3) | ||
assert result.is_a? Array | ||
assert((source.sort <=> result.sort) == 0) | ||
end | ||
|
||
def test_raises_Argument_Error_with_negative_param | ||
source = ['foo','bar'] | ||
assert_raise ArgumentError do | ||
source.sample(-1) | ||
end | ||
end | ||
end |