Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* In Enumerable, Enumerator::Lazy, Array, Hash and Set [Feature #13784] [ruby-core:82285] * Share specs for the various #select#select! methods and reuse them for #filter/#filter!. * Add corresponding filter tests for select tests. * Update NEWS. [Fix GH-1824] From: Alexander Patrick <adp90@case.edu> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
- Loading branch information
Showing
24 changed files
with
406 additions
and
171 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
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
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
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
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
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
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,14 @@ | ||
require File.expand_path('../../../spec_helper', __FILE__) | ||
require File.expand_path('../shared/select', __FILE__) | ||
|
||
describe "Array#filter" do | ||
it_behaves_like :array_select, :filter | ||
end | ||
|
||
describe "Array#filter!" do | ||
it "returns nil if no changes were made in the array" do | ||
[1, 2, 3].filter! { true }.should be_nil | ||
end | ||
|
||
it_behaves_like :keep_if, :filter! | ||
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
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,32 @@ | ||
require File.expand_path('../../../../spec_helper', __FILE__) | ||
require File.expand_path('../../fixtures/classes', __FILE__) | ||
require File.expand_path('../../shared/enumeratorize', __FILE__) | ||
require File.expand_path('../../shared/keep_if', __FILE__) | ||
require File.expand_path('../../../enumerable/shared/enumeratorized', __FILE__) | ||
|
||
describe :array_select, shared: true do | ||
it_should_behave_like :enumeratorize | ||
|
||
before :each do | ||
@object = [1,2,3] | ||
end | ||
it_should_behave_like :enumeratorized_with_origin_size | ||
|
||
it "returns a new array of elements for which block is true" do | ||
[1, 3, 4, 5, 6, 9].send(@method) { |i| i % ((i + 1) / 2) == 0}.should == [1, 4, 6] | ||
end | ||
|
||
it "does not return subclass instance on Array subclasses" do | ||
ArraySpecs::MyArray[1, 2, 3].send(@method) { true }.should be_an_instance_of(Array) | ||
end | ||
|
||
it "properly handles recursive arrays" do | ||
empty = ArraySpecs.empty_recursive_array | ||
empty.send(@method) { true }.should == empty | ||
empty.send(@method) { false }.should == [] | ||
|
||
array = ArraySpecs.recursive_array | ||
array.send(@method) { true }.should == [1, 'two', 3.0, array, array, array, array, array] | ||
array.send(@method) { false }.should == [] | ||
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,7 @@ | ||
require File.expand_path('../../../spec_helper', __FILE__) | ||
require File.expand_path('../fixtures/classes', __FILE__) | ||
require File.expand_path('../shared/find_all', __FILE__) | ||
|
||
describe "Enumerable#filter" do | ||
it_behaves_like(:enumerable_find_all , :filter) | ||
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,10 @@ | ||
require File.expand_path('../../../spec_helper', __FILE__) | ||
require File.expand_path('../shared/select', __FILE__) | ||
|
||
describe "Hash#filter" do | ||
it_behaves_like :hash_select, :filter | ||
end | ||
|
||
describe "Hash#filter!" do | ||
it_behaves_like :hash_select!, :filter! | ||
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 |
---|---|---|
@@ -1,83 +1,10 @@ | ||
require File.expand_path('../../../spec_helper', __FILE__) | ||
require File.expand_path('../fixtures/classes', __FILE__) | ||
require File.expand_path('../shared/iteration', __FILE__) | ||
require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) | ||
require File.expand_path('../shared/select', __FILE__) | ||
|
||
describe "Hash#select" do | ||
before :each do | ||
@hsh = { 1 => 2, 3 => 4, 5 => 6 } | ||
@empty = {} | ||
end | ||
|
||
it "yields two arguments: key and value" do | ||
all_args = [] | ||
{ 1 => 2, 3 => 4 }.select { |*args| all_args << args } | ||
all_args.sort.should == [[1, 2], [3, 4]] | ||
end | ||
|
||
it "returns a Hash of entries for which block is true" do | ||
a_pairs = { 'a' => 9, 'c' => 4, 'b' => 5, 'd' => 2 }.select { |k,v| v % 2 == 0 } | ||
a_pairs.should be_an_instance_of(Hash) | ||
a_pairs.sort.should == [['c', 4], ['d', 2]] | ||
end | ||
|
||
it "processes entries with the same order as reject" do | ||
h = { a: 9, c: 4, b: 5, d: 2 } | ||
|
||
select_pairs = [] | ||
reject_pairs = [] | ||
h.dup.select { |*pair| select_pairs << pair } | ||
h.reject { |*pair| reject_pairs << pair } | ||
|
||
select_pairs.should == reject_pairs | ||
end | ||
|
||
it "returns an Enumerator when called on a non-empty hash without a block" do | ||
@hsh.select.should be_an_instance_of(Enumerator) | ||
end | ||
|
||
it "returns an Enumerator when called on an empty hash without a block" do | ||
@empty.select.should be_an_instance_of(Enumerator) | ||
end | ||
|
||
it_behaves_like :hash_iteration_no_block, :select | ||
it_behaves_like :enumeratorized_with_origin_size, :select, { 1 => 2, 3 => 4, 5 => 6 } | ||
it_behaves_like :hash_select, :select | ||
end | ||
|
||
describe "Hash#select!" do | ||
before :each do | ||
@hsh = { 1 => 2, 3 => 4, 5 => 6 } | ||
@empty = {} | ||
end | ||
|
||
it "is equivalent to keep_if if changes are made" do | ||
h = { a: 2 } | ||
h.select! { |k,v| v <= 1 }.should equal h | ||
|
||
h = { 1 => 2, 3 => 4 } | ||
all_args_select = [] | ||
h.dup.select! { |*args| all_args_select << args } | ||
all_args_select.should == [[1, 2], [3, 4]] | ||
end | ||
|
||
it "removes all entries if the block is false" do | ||
h = { a: 1, b: 2, c: 3 } | ||
h.select! { |k,v| false }.should equal(h) | ||
h.should == {} | ||
end | ||
|
||
it "returns nil if no changes were made" do | ||
{ a: 1 }.select! { |k,v| v <= 1 }.should == nil | ||
end | ||
|
||
it "raises a #{frozen_error_class} if called on an empty frozen instance" do | ||
lambda { HashSpecs.empty_frozen_hash.select! { false } }.should raise_error(frozen_error_class) | ||
end | ||
|
||
it "raises a #{frozen_error_class} if called on a frozen instance that would not be modified" do | ||
lambda { HashSpecs.frozen_hash.select! { true } }.should raise_error(frozen_error_class) | ||
end | ||
|
||
it_behaves_like :hash_iteration_no_block, :select! | ||
it_behaves_like :enumeratorized_with_origin_size, :select!, { 1 => 2, 3 => 4, 5 => 6 } | ||
it_behaves_like :hash_select!, :select! | ||
end |
Oops, something went wrong.