summaryrefslogtreecommitdiff
path: root/spec/ruby
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-25 13:52:07 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-25 13:52:07 +0000
commitb1a8c64483b5ba5e4a391aa68234e7bde6355034 (patch)
treefc7dc594e220ba737a0138666eed8841b55ab243 /spec/ruby
parentd1ea2f9d8f10ca728d178de92a26668c64f4aff8 (diff)
Add a new #filter alias for #select
* 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
Diffstat (limited to 'spec/ruby')
-rw-r--r--spec/ruby/core/array/filter_spec.rb14
-rw-r--r--spec/ruby/core/array/select_spec.rb26
-rw-r--r--spec/ruby/core/array/shared/select.rb32
-rw-r--r--spec/ruby/core/enumerable/filter_spec.rb7
-rw-r--r--spec/ruby/core/hash/filter_spec.rb10
-rw-r--r--spec/ruby/core/hash/select_spec.rb79
-rw-r--r--spec/ruby/core/hash/shared/select.rb91
-rw-r--r--spec/ruby/library/set/filter_spec.rb6
-rw-r--r--spec/ruby/library/set/select_spec.rb40
-rw-r--r--spec/ruby/library/set/shared/select.rb42
-rw-r--r--spec/ruby/library/set/sortedset/filter_spec.rb7
-rw-r--r--spec/ruby/library/set/sortedset/select_spec.rb32
-rw-r--r--spec/ruby/library/set/sortedset/shared/select.rb35
13 files changed, 253 insertions, 168 deletions
diff --git a/spec/ruby/core/array/filter_spec.rb b/spec/ruby/core/array/filter_spec.rb
new file mode 100644
index 0000000000..d7c722e44c
--- /dev/null
+++ b/spec/ruby/core/array/filter_spec.rb
@@ -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
diff --git a/spec/ruby/core/array/select_spec.rb b/spec/ruby/core/array/select_spec.rb
index 8b83acaa5f..b7531c967d 100644
--- a/spec/ruby/core/array/select_spec.rb
+++ b/spec/ruby/core/array/select_spec.rb
@@ -1,30 +1,8 @@
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__)
+require File.expand_path('../shared/select', __FILE__)
describe "Array#select" do
- it_behaves_like :enumeratorize, :select
- it_behaves_like :enumeratorized_with_origin_size, :select, [1,2,3]
-
- it "returns a new array of elements for which block is true" do
- [1, 3, 4, 5, 6, 9].select { |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].select { true }.should be_an_instance_of(Array)
- end
-
- it "properly handles recursive arrays" do
- empty = ArraySpecs.empty_recursive_array
- empty.select { true }.should == empty
- empty.select { false }.should == []
-
- array = ArraySpecs.recursive_array
- array.select { true }.should == [1, 'two', 3.0, array, array, array, array, array]
- array.select { false }.should == []
- end
+ it_behaves_like :array_select, :select
end
describe "Array#select!" do
diff --git a/spec/ruby/core/array/shared/select.rb b/spec/ruby/core/array/shared/select.rb
new file mode 100644
index 0000000000..e09231415d
--- /dev/null
+++ b/spec/ruby/core/array/shared/select.rb
@@ -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
diff --git a/spec/ruby/core/enumerable/filter_spec.rb b/spec/ruby/core/enumerable/filter_spec.rb
new file mode 100644
index 0000000000..7f4bdd6d59
--- /dev/null
+++ b/spec/ruby/core/enumerable/filter_spec.rb
@@ -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
diff --git a/spec/ruby/core/hash/filter_spec.rb b/spec/ruby/core/hash/filter_spec.rb
new file mode 100644
index 0000000000..46c7bea8e8
--- /dev/null
+++ b/spec/ruby/core/hash/filter_spec.rb
@@ -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
diff --git a/spec/ruby/core/hash/select_spec.rb b/spec/ruby/core/hash/select_spec.rb
index 9348cc4c39..f8d66d09fd 100644
--- a/spec/ruby/core/hash/select_spec.rb
+++ b/spec/ruby/core/hash/select_spec.rb
@@ -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
diff --git a/spec/ruby/core/hash/shared/select.rb b/spec/ruby/core/hash/shared/select.rb
new file mode 100644
index 0000000000..3ffd901cea
--- /dev/null
+++ b/spec/ruby/core/hash/shared/select.rb
@@ -0,0 +1,91 @@
+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__)
+
+describe :hash_select, shared: true 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 }.send(@method) { |*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 }.send(@method) { |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.send(@method) { |*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.send(@method).should be_an_instance_of(Enumerator)
+ end
+
+ it "returns an Enumerator when called on an empty hash without a block" do
+ @empty.send(@method).should be_an_instance_of(Enumerator)
+ end
+
+ it_should_behave_like :hash_iteration_no_block
+
+ before :each do
+ @object = { 1 => 2, 3 => 4, 5 => 6 }
+ end
+ it_should_behave_like :enumeratorized_with_origin_size
+end
+
+describe :hash_select!, shared: true 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.send(@method) { |k,v| v <= 1 }.should equal h
+
+ h = { 1 => 2, 3 => 4 }
+ all_args_select = []
+ h.dup.send(@method) { |*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.send(@method) { |k,v| false }.should equal(h)
+ h.should == {}
+ end
+
+ it "returns nil if no changes were made" do
+ { a: 1 }.send(@method) { |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.send(@method) { 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.send(@method) { true } }.should raise_error(frozen_error_class)
+ end
+
+ it_should_behave_like :hash_iteration_no_block
+
+ before :each do
+ @object = { 1 => 2, 3 => 4, 5 => 6 }
+ end
+ it_should_behave_like :enumeratorized_with_origin_size
+end
diff --git a/spec/ruby/library/set/filter_spec.rb b/spec/ruby/library/set/filter_spec.rb
new file mode 100644
index 0000000000..3ac0c660b2
--- /dev/null
+++ b/spec/ruby/library/set/filter_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/select', __FILE__)
+
+describe "Set#filter!" do
+ it_behaves_like :set_select_bang, :filter!
+end
diff --git a/spec/ruby/library/set/select_spec.rb b/spec/ruby/library/set/select_spec.rb
index 34274a7c46..433fbb4617 100644
--- a/spec/ruby/library/set/select_spec.rb
+++ b/spec/ruby/library/set/select_spec.rb
@@ -1,42 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
-require 'set'
+require File.expand_path('../shared/select', __FILE__)
describe "Set#select!" do
- before :each do
- @set = Set["one", "two", "three"]
- end
-
- it "yields every element of self" do
- ret = []
- @set.select! { |x| ret << x }
- ret.sort.should == ["one", "two", "three"].sort
- end
-
- it "keeps every element from self for which the passed block returns true" do
- @set.select! { |x| x.size != 3 }
- @set.size.should eql(1)
-
- @set.should_not include("one")
- @set.should_not include("two")
- @set.should include("three")
- end
-
- it "returns self when self was modified" do
- @set.select! { false }.should equal(@set)
- end
-
- it "returns nil when self was not modified" do
- @set.select! { true }.should be_nil
- end
-
- it "returns an Enumerator when passed no block" do
- enum = @set.select!
- enum.should be_an_instance_of(Enumerator)
-
- enum.each { |x| x.size != 3 }
-
- @set.should_not include("one")
- @set.should_not include("two")
- @set.should include("three")
- end
+ it_behaves_like :set_select_bang, :select!
end
diff --git a/spec/ruby/library/set/shared/select.rb b/spec/ruby/library/set/shared/select.rb
new file mode 100644
index 0000000000..c9f01b8a25
--- /dev/null
+++ b/spec/ruby/library/set/shared/select.rb
@@ -0,0 +1,42 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require 'set'
+
+describe :set_select_bang, shared: true do
+ before :each do
+ @set = Set["one", "two", "three"]
+ end
+
+ it "yields every element of self" do
+ ret = []
+ @set.send(@method) { |x| ret << x }
+ ret.sort.should == ["one", "two", "three"].sort
+ end
+
+ it "keeps every element from self for which the passed block returns true" do
+ @set.send(@method) { |x| x.size != 3 }
+ @set.size.should eql(1)
+
+ @set.should_not include("one")
+ @set.should_not include("two")
+ @set.should include("three")
+ end
+
+ it "returns self when self was modified" do
+ @set.send(@method) { false }.should equal(@set)
+ end
+
+ it "returns nil when self was not modified" do
+ @set.send(@method) { true }.should be_nil
+ end
+
+ it "returns an Enumerator when passed no block" do
+ enum = @set.send(@method)
+ enum.should be_an_instance_of(Enumerator)
+
+ enum.each { |x| x.size != 3 }
+
+ @set.should_not include("one")
+ @set.should_not include("two")
+ @set.should include("three")
+ end
+end
diff --git a/spec/ruby/library/set/sortedset/filter_spec.rb b/spec/ruby/library/set/sortedset/filter_spec.rb
new file mode 100644
index 0000000000..59e264ed84
--- /dev/null
+++ b/spec/ruby/library/set/sortedset/filter_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require File.expand_path('../shared/select', __FILE__)
+require 'set'
+
+describe "SortedSet#filter!" do
+ it_behaves_like :sorted_set_select_bang, :filter!
+end
diff --git a/spec/ruby/library/set/sortedset/select_spec.rb b/spec/ruby/library/set/sortedset/select_spec.rb
index 3ca748350a..b00525346c 100644
--- a/spec/ruby/library/set/sortedset/select_spec.rb
+++ b/spec/ruby/library/set/sortedset/select_spec.rb
@@ -1,35 +1,7 @@
require File.expand_path('../../../../spec_helper', __FILE__)
+require File.expand_path('../shared/select', __FILE__)
require 'set'
describe "SortedSet#select!" do
- before :each do
- @set = SortedSet["one", "two", "three"]
- end
-
- it "yields each Object in self in sorted order" do
- res = []
- @set.select! { |x| res << x }
- res.should == ["one", "two", "three"].sort
- end
-
- it "keeps every element from self for which the passed block returns true" do
- @set.select! { |x| x.size != 3 }
- @set.to_a.should == ["three"]
- end
-
- it "returns self when self was modified" do
- @set.select! { false }.should equal(@set)
- end
-
- it "returns nil when self was not modified" do
- @set.select! { true }.should be_nil
- end
-
- it "returns an Enumerator when passed no block" do
- enum = @set.select!
- enum.should be_an_instance_of(Enumerator)
-
- enum.each { |x| x.size != 3 }
- @set.to_a.should == ["three"]
- end
+ it_behaves_like :sorted_set_select_bang, :select!
end
diff --git a/spec/ruby/library/set/sortedset/shared/select.rb b/spec/ruby/library/set/sortedset/shared/select.rb
new file mode 100644
index 0000000000..d3ce4ed33a
--- /dev/null
+++ b/spec/ruby/library/set/sortedset/shared/select.rb
@@ -0,0 +1,35 @@
+require File.expand_path('../../../../../spec_helper', __FILE__)
+require 'set'
+
+describe :sorted_set_select_bang, shared: true do
+ before :each do
+ @set = SortedSet["one", "two", "three"]
+ end
+
+ it "yields each Object in self in sorted order" do
+ res = []
+ @set.send(@method) { |x| res << x }
+ res.should == ["one", "two", "three"].sort
+ end
+
+ it "keeps every element from self for which the passed block returns true" do
+ @set.send(@method) { |x| x.size != 3 }
+ @set.to_a.should == ["three"]
+ end
+
+ it "returns self when self was modified" do
+ @set.send(@method) { false }.should equal(@set)
+ end
+
+ it "returns nil when self was not modified" do
+ @set.send(@method) { true }.should be_nil
+ end
+
+ it "returns an Enumerator when passed no block" do
+ enum = @set.send(@method)
+ enum.should be_an_instance_of(Enumerator)
+
+ enum.each { |x| x.size != 3 }
+ @set.to_a.should == ["three"]
+ end
+end