diff options
Diffstat (limited to 'spec/ruby/core/enumerable')
62 files changed, 453 insertions, 312 deletions
diff --git a/spec/ruby/core/enumerable/all_spec.rb b/spec/ruby/core/enumerable/all_spec.rb index 0ded1e8eba..cbdd63f86a 100644 --- a/spec/ruby/core/enumerable/all_spec.rb +++ b/spec/ruby/core/enumerable/all_spec.rb @@ -21,19 +21,19 @@ describe "Enumerable#all?" do end it "raises an ArgumentError when more than 1 argument is provided" do - -> { @enum.all?(1, 2, 3) }.should raise_error(ArgumentError) - -> { [].all?(1, 2, 3) }.should raise_error(ArgumentError) - -> { {}.all?(1, 2, 3) }.should raise_error(ArgumentError) + -> { @enum.all?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].all?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.all?(1, 2, 3) }.should.raise(ArgumentError) end it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.all? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) -> { EnumerableSpecs::ThrowingEach.new.all? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do @@ -60,7 +60,7 @@ describe "Enumerable#all?" do it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithFalse.new - multi.all?.should be_true + multi.all?.should == true end end @@ -106,7 +106,7 @@ describe "Enumerable#all?" do it "does not hide exceptions out of the block" do -> { @enum.all? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do @@ -125,7 +125,7 @@ describe "Enumerable#all?" do end describe 'when given a pattern argument' do - it "calls `===` on the pattern the return value " do + it "calls `===` on the pattern the return value" do pattern = EnumerableSpecs::Pattern.new { |x| x >= 0 } @enum1.all?(pattern).should == false pattern.yielded.should == [[0], [1], [2], [-1]] @@ -140,7 +140,7 @@ describe "Enumerable#all?" do it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.all?(Integer) - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "returns true if the pattern never returns false or nil" do @@ -168,7 +168,7 @@ describe "Enumerable#all?" do pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } -> { @enum.all?(pattern) - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "calls the pattern with gathered array when yielded with multiple arguments" do @@ -177,5 +177,11 @@ describe "Enumerable#all?" do multi.all?(pattern).should == true pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).all?(String) { true }.should == false + }.should complain(/given block not used/) + end end end diff --git a/spec/ruby/core/enumerable/any_spec.rb b/spec/ruby/core/enumerable/any_spec.rb index 355cd0c290..4405d4740a 100644 --- a/spec/ruby/core/enumerable/any_spec.rb +++ b/spec/ruby/core/enumerable/any_spec.rb @@ -21,19 +21,19 @@ describe "Enumerable#any?" do end it "raises an ArgumentError when more than 1 argument is provided" do - -> { @enum.any?(1, 2, 3) }.should raise_error(ArgumentError) - -> { [].any?(1, 2, 3) }.should raise_error(ArgumentError) - -> { {}.any?(1, 2, 3) }.should raise_error(ArgumentError) + -> { @enum.any?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].any?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.any?(1, 2, 3) }.should.raise(ArgumentError) end it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.any? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) -> { EnumerableSpecs::ThrowingEach.new.any? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do @@ -60,7 +60,7 @@ describe "Enumerable#any?" do it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithFalse.new - multi.any?.should be_true + multi.any?.should == true end end @@ -120,7 +120,7 @@ describe "Enumerable#any?" do it "does not hide exceptions out of the block" do -> { @enum.any? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do @@ -139,7 +139,7 @@ describe "Enumerable#any?" do end describe 'when given a pattern argument' do - it "calls `===` on the pattern the return value " do + it "calls `===` on the pattern the return value" do pattern = EnumerableSpecs::Pattern.new { |x| x == 2 } @enum1.any?(pattern).should == true pattern.yielded.should == [[0], [1], [2]] @@ -154,7 +154,7 @@ describe "Enumerable#any?" do it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.any?(Integer) - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "returns true if the pattern ever returns a truthy value" do @@ -181,7 +181,7 @@ describe "Enumerable#any?" do pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } -> { @enum.any?(pattern) - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "calls the pattern with gathered array when yielded with multiple arguments" do @@ -190,5 +190,11 @@ describe "Enumerable#any?" do multi.any?(pattern).should == false pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).any?(String) { true }.should == false + }.should complain(/given block not used/) + end end end diff --git a/spec/ruby/core/enumerable/chain_spec.rb b/spec/ruby/core/enumerable/chain_spec.rb index 5e2105d294..a0597e46a1 100644 --- a/spec/ruby/core/enumerable/chain_spec.rb +++ b/spec/ruby/core/enumerable/chain_spec.rb @@ -18,6 +18,6 @@ describe "Enumerable#chain" do end it "returns an Enumerator::Chain if given a block" do - EnumerableSpecs::Numerous.new.chain.should be_an_instance_of(Enumerator::Chain) + EnumerableSpecs::Numerous.new.chain.should.instance_of?(Enumerator::Chain) end end diff --git a/spec/ruby/core/enumerable/chunk_spec.rb b/spec/ruby/core/enumerable/chunk_spec.rb index c5579d67fa..7c9b31c991 100644 --- a/spec/ruby/core/enumerable/chunk_spec.rb +++ b/spec/ruby/core/enumerable/chunk_spec.rb @@ -8,13 +8,13 @@ describe "Enumerable#chunk" do it "returns an Enumerator if called without a block" do chunk = EnumerableSpecs::Numerous.new(1, 2, 3, 1, 2).chunk - chunk.should be_an_instance_of(Enumerator) + chunk.should.instance_of?(Enumerator) result = chunk.with_index {|elt, i| elt - i }.to_a result.should == [[1, [1, 2, 3]], [-2, [1, 2]]] end it "returns an Enumerator if given a block" do - EnumerableSpecs::Numerous.new.chunk {}.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.chunk {}.should.instance_of?(Enumerator) end it "yields the current element and the current chunk to the block" do @@ -29,6 +29,11 @@ describe "Enumerable#chunk" do result.should == [[1, [1, 2]], [0, [3]], [1, [2]], [0, [3]], [1, [2, 1]]] end + it "returns a partitioned Array of values" do + e = EnumerableSpecs::Numerous.new(1,2,3) + e.chunk { |x| x > 2 }.map(&:last).should == [[1, 2], [3]] + end + it "returns elements for which the block returns :_alone in separate Arrays" do e = EnumerableSpecs::Numerous.new(1, 2, 3, 2, 1) result = e.chunk { |x| x < 2 && :_alone }.to_a @@ -54,14 +59,14 @@ describe "Enumerable#chunk" do it "raises a RuntimeError if the block returns a Symbol starting with an underscore other than :_alone or :_separator" do e = EnumerableSpecs::Numerous.new(1, 2, 3, 2, 1) - -> { e.chunk { |x| :_arbitrary }.to_a }.should raise_error(RuntimeError) + -> { e.chunk { |x| :_arbitrary }.to_a }.should.raise(RuntimeError) end it "does not accept arguments" do e = EnumerableSpecs::Numerous.new(1, 2, 3) -> { e.chunk(1) {} - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it 'returned Enumerator size returns nil' do diff --git a/spec/ruby/core/enumerable/chunk_while_spec.rb b/spec/ruby/core/enumerable/chunk_while_spec.rb index 26bcc983db..286f717442 100644 --- a/spec/ruby/core/enumerable/chunk_while_spec.rb +++ b/spec/ruby/core/enumerable/chunk_while_spec.rb @@ -11,7 +11,7 @@ describe "Enumerable#chunk_while" do context "when given a block" do it "returns an enumerator" do - @result.should be_an_instance_of(Enumerator) + @result.should.instance_of?(Enumerator) end it "splits chunks between adjacent elements i and j where the block returns false" do @@ -30,7 +30,7 @@ describe "Enumerable#chunk_while" do context "when not given a block" do it "raises an ArgumentError" do - -> { @enum.chunk_while }.should raise_error(ArgumentError) + -> { @enum.chunk_while }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerable/collect_concat_spec.rb b/spec/ruby/core/enumerable/collect_concat_spec.rb index 6e34c9eb93..59317cfe34 100644 --- a/spec/ruby/core/enumerable/collect_concat_spec.rb +++ b/spec/ruby/core/enumerable/collect_concat_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/collect_concat' describe "Enumerable#collect_concat" do - it_behaves_like :enumerable_collect_concat , :collect_concat + it_behaves_like :enumerable_collect_concat, :collect_concat end diff --git a/spec/ruby/core/enumerable/collect_spec.rb b/spec/ruby/core/enumerable/collect_spec.rb index 1016b67798..cfa2895cce 100644 --- a/spec/ruby/core/enumerable/collect_spec.rb +++ b/spec/ruby/core/enumerable/collect_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/collect' describe "Enumerable#collect" do - it_behaves_like :enumerable_collect , :collect + it_behaves_like :enumerable_collect, :collect end diff --git a/spec/ruby/core/enumerable/compact_spec.rb b/spec/ruby/core/enumerable/compact_spec.rb index 86e95dce08..1895430c4e 100644 --- a/spec/ruby/core/enumerable/compact_spec.rb +++ b/spec/ruby/core/enumerable/compact_spec.rb @@ -1,11 +1,9 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -ruby_version_is '3.1' do - describe "Enumerable#compact" do - it 'returns array without nil elements' do - arr = EnumerableSpecs::Numerous.new(nil, 1, 2, nil, true) - arr.compact.should == [1, 2, true] - end +describe "Enumerable#compact" do + it 'returns array without nil elements' do + arr = EnumerableSpecs::Numerous.new(nil, 1, 2, nil, true) + arr.compact.should == [1, 2, true] end end diff --git a/spec/ruby/core/enumerable/cycle_spec.rb b/spec/ruby/core/enumerable/cycle_spec.rb index 487086cba3..1fb3cc3d41 100644 --- a/spec/ruby/core/enumerable/cycle_spec.rb +++ b/spec/ruby/core/enumerable/cycle_spec.rb @@ -17,7 +17,7 @@ describe "Enumerable#cycle" do it "returns nil if there are no elements" do out = EnumerableSpecs::Empty.new.cycle { break :nope } - out.should be_nil + out.should == nil end it "yields successive elements of the array repeatedly" do @@ -44,8 +44,8 @@ describe "Enumerable#cycle" do describe "passed a number n as an argument" do it "returns nil and does nothing for non positive n" do - EnumerableSpecs::ThrowingEach.new.cycle(0) {}.should be_nil - EnumerableSpecs::NoEach.new.cycle(-22) {}.should be_nil + EnumerableSpecs::ThrowingEach.new.cycle(0) {}.should == nil + EnumerableSpecs::NoEach.new.cycle(-22) {}.should == nil end it "calls each at most once" do @@ -71,12 +71,12 @@ describe "Enumerable#cycle" do it "raises a TypeError when the passed n cannot be coerced to Integer" do enum = EnumerableSpecs::Numerous.new - ->{ enum.cycle("cat"){} }.should raise_error(TypeError) + ->{ enum.cycle("cat"){} }.should.raise(TypeError) end it "raises an ArgumentError if more arguments are passed" do enum = EnumerableSpecs::Numerous.new - ->{ enum.cycle(1, 2) {} }.should raise_error(ArgumentError) + ->{ enum.cycle(1, 2) {} }.should.raise(ArgumentError) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/detect_spec.rb b/spec/ruby/core/enumerable/detect_spec.rb index e912134fed..6959aadc44 100644 --- a/spec/ruby/core/enumerable/detect_spec.rb +++ b/spec/ruby/core/enumerable/detect_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find' describe "Enumerable#detect" do - it_behaves_like :enumerable_find , :detect + it_behaves_like :enumerable_find, :detect end diff --git a/spec/ruby/core/enumerable/drop_spec.rb b/spec/ruby/core/enumerable/drop_spec.rb index 423cc0088b..8d95f464b3 100644 --- a/spec/ruby/core/enumerable/drop_spec.rb +++ b/spec/ruby/core/enumerable/drop_spec.rb @@ -7,13 +7,13 @@ describe "Enumerable#drop" do end it "requires exactly one argument" do - ->{ @enum.drop{} }.should raise_error(ArgumentError) - ->{ @enum.drop(1, 2){} }.should raise_error(ArgumentError) + ->{ @enum.drop{} }.should.raise(ArgumentError) + ->{ @enum.drop(1, 2){} }.should.raise(ArgumentError) end describe "passed a number n as an argument" do it "raises ArgumentError if n < 0" do - ->{ @enum.drop(-1) }.should raise_error(ArgumentError) + ->{ @enum.drop(-1) }.should.raise(ArgumentError) end it "tries to convert n to an Integer using #to_int" do @@ -35,8 +35,8 @@ describe "Enumerable#drop" do end it "raises a TypeError when the passed n cannot be coerced to Integer" do - ->{ @enum.drop("hat") }.should raise_error(TypeError) - ->{ @enum.drop(nil) }.should raise_error(TypeError) + ->{ @enum.drop("hat") }.should.raise(TypeError) + ->{ @enum.drop(nil) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/enumerable/drop_while_spec.rb b/spec/ruby/core/enumerable/drop_while_spec.rb index 636c3d284a..4b4fdf2d4f 100644 --- a/spec/ruby/core/enumerable/drop_while_spec.rb +++ b/spec/ruby/core/enumerable/drop_while_spec.rb @@ -8,7 +8,7 @@ describe "Enumerable#drop_while" do end it "returns an Enumerator if no block given" do - @enum.drop_while.should be_an_instance_of(Enumerator) + @enum.drop_while.should.instance_of?(Enumerator) end it "returns no/all elements for {true/false} block" do @@ -38,7 +38,7 @@ describe "Enumerable#drop_while" do it "doesn't return self when it could" do a = [1,2,3] - a.drop_while{false}.should_not equal(a) + a.drop_while{false}.should_not.equal?(a) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/each_cons_spec.rb b/spec/ruby/core/enumerable/each_cons_spec.rb index ba658203a2..c5e299fd00 100644 --- a/spec/ruby/core/enumerable/each_cons_spec.rb +++ b/spec/ruby/core/enumerable/each_cons_spec.rb @@ -15,14 +15,14 @@ describe "Enumerable#each_cons" do end it "raises an ArgumentError if there is not a single parameter > 0" do - ->{ @enum.each_cons(0){} }.should raise_error(ArgumentError) - ->{ @enum.each_cons(-2){} }.should raise_error(ArgumentError) - ->{ @enum.each_cons{} }.should raise_error(ArgumentError) - ->{ @enum.each_cons(2,2){} }.should raise_error(ArgumentError) - ->{ @enum.each_cons(0) }.should raise_error(ArgumentError) - ->{ @enum.each_cons(-2) }.should raise_error(ArgumentError) - ->{ @enum.each_cons }.should raise_error(ArgumentError) - ->{ @enum.each_cons(2,2) }.should raise_error(ArgumentError) + ->{ @enum.each_cons(0){} }.should.raise(ArgumentError) + ->{ @enum.each_cons(-2){} }.should.raise(ArgumentError) + ->{ @enum.each_cons{} }.should.raise(ArgumentError) + ->{ @enum.each_cons(2,2){} }.should.raise(ArgumentError) + ->{ @enum.each_cons(0) }.should.raise(ArgumentError) + ->{ @enum.each_cons(-2) }.should.raise(ArgumentError) + ->{ @enum.each_cons }.should.raise(ArgumentError) + ->{ @enum.each_cons(2,2) }.should.raise(ArgumentError) end it "tries to convert n to an Integer using #to_int" do @@ -56,10 +56,14 @@ describe "Enumerable#each_cons" do multi.each_cons(2).to_a.should == [[[1, 2], [3, 4, 5]], [[3, 4, 5], [6, 7, 8, 9]]] end + it "returns self when a block is given" do + @enum.each_cons(3){}.should == @enum + end + describe "when no block is given" do it "returns an enumerator" do e = @enum.each_cons(3) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == @in_threes end diff --git a/spec/ruby/core/enumerable/each_entry_spec.rb b/spec/ruby/core/enumerable/each_entry_spec.rb index edf00f3137..9dc89ec28e 100644 --- a/spec/ruby/core/enumerable/each_entry_spec.rb +++ b/spec/ruby/core/enumerable/each_entry_spec.rb @@ -11,13 +11,13 @@ describe "Enumerable#each_entry" do it "yields multiple arguments as an array" do acc = [] - @enum.each_entry {|g| acc << g}.should equal(@enum) + @enum.each_entry {|g| acc << g}.should.equal?(@enum) acc.should == @entries end it "returns an enumerator if no block" do e = @enum.each_entry - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == @entries end @@ -27,8 +27,8 @@ describe "Enumerable#each_entry" do end it "raises an ArgumentError when extra arguments" do - -> { @enum.each_entry("one").to_a }.should raise_error(ArgumentError) - -> { @enum.each_entry("one"){}.to_a }.should raise_error(ArgumentError) + -> { @enum.each_entry("one").to_a }.should.raise(ArgumentError) + -> { @enum.each_entry("one"){}.to_a }.should.raise(ArgumentError) end it "passes extra arguments to #each" do diff --git a/spec/ruby/core/enumerable/each_slice_spec.rb b/spec/ruby/core/enumerable/each_slice_spec.rb index 2ea89f5e72..d05abad1e9 100644 --- a/spec/ruby/core/enumerable/each_slice_spec.rb +++ b/spec/ruby/core/enumerable/each_slice_spec.rb @@ -15,14 +15,14 @@ describe "Enumerable#each_slice" do end it "raises an ArgumentError if there is not a single parameter > 0" do - ->{ @enum.each_slice(0){} }.should raise_error(ArgumentError) - ->{ @enum.each_slice(-2){} }.should raise_error(ArgumentError) - ->{ @enum.each_slice{} }.should raise_error(ArgumentError) - ->{ @enum.each_slice(2,2){} }.should raise_error(ArgumentError) - ->{ @enum.each_slice(0) }.should raise_error(ArgumentError) - ->{ @enum.each_slice(-2) }.should raise_error(ArgumentError) - ->{ @enum.each_slice }.should raise_error(ArgumentError) - ->{ @enum.each_slice(2,2) }.should raise_error(ArgumentError) + ->{ @enum.each_slice(0){} }.should.raise(ArgumentError) + ->{ @enum.each_slice(-2){} }.should.raise(ArgumentError) + ->{ @enum.each_slice{} }.should.raise(ArgumentError) + ->{ @enum.each_slice(2,2){} }.should.raise(ArgumentError) + ->{ @enum.each_slice(0) }.should.raise(ArgumentError) + ->{ @enum.each_slice(-2) }.should.raise(ArgumentError) + ->{ @enum.each_slice }.should.raise(ArgumentError) + ->{ @enum.each_slice(2,2) }.should.raise(ArgumentError) end it "tries to convert n to an Integer using #to_int" do @@ -53,10 +53,14 @@ describe "Enumerable#each_slice" do it "returns an enumerator if no block" do e = @enum.each_slice(3) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == @sliced end + it "returns self when a block is given" do + @enum.each_slice(3){}.should == @enum + end + it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMulti.new multi.each_slice(2).to_a.should == [[[1, 2], [3, 4, 5]], [[6, 7, 8, 9]]] @@ -65,7 +69,7 @@ describe "Enumerable#each_slice" do describe "when no block is given" do it "returns an enumerator" do e = @enum.each_slice(3) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == @sliced end diff --git a/spec/ruby/core/enumerable/each_with_index_spec.rb b/spec/ruby/core/enumerable/each_with_index_spec.rb index 122e88eab7..fcb2f82f84 100644 --- a/spec/ruby/core/enumerable/each_with_index_spec.rb +++ b/spec/ruby/core/enumerable/each_with_index_spec.rb @@ -26,19 +26,19 @@ describe "Enumerable#each_with_index" do acc = [] res = @b.each_with_index {|a,i| acc << [a,i]} [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]].should == acc - res.should eql(@b) + res.should.eql?(@b) end it "binds splat arguments properly" do acc = [] res = @b.each_with_index { |*b| c,d = b; acc << c; acc << d } [2, 0, 5, 1, 3, 2, 6, 3, 1, 4, 4, 5].should == acc - res.should eql(@b) + res.should.eql?(@b) end it "returns an enumerator if no block" do e = @b.each_with_index - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]] end diff --git a/spec/ruby/core/enumerable/each_with_object_spec.rb b/spec/ruby/core/enumerable/each_with_object_spec.rb index 35665e7019..1760d3b267 100644 --- a/spec/ruby/core/enumerable/each_with_object_spec.rb +++ b/spec/ruby/core/enumerable/each_with_object_spec.rb @@ -12,10 +12,10 @@ describe "Enumerable#each_with_object" do it "passes each element and its argument to the block" do acc = [] @enum.each_with_object(@initial) do |elem, obj| - obj.should equal(@initial) + obj.should.equal?(@initial) obj = 42 acc << elem - end.should equal(@initial) + end.should.equal?(@initial) acc.should == @values end @@ -23,10 +23,10 @@ describe "Enumerable#each_with_object" do acc = [] e = @enum.each_with_object(@initial) e.each do |elem, obj| - obj.should equal(@initial) + obj.should.equal?(@initial) obj = 42 acc << elem - end.should equal(@initial) + end.should.equal?(@initial) acc.should == @values end diff --git a/spec/ruby/core/enumerable/entries_spec.rb b/spec/ruby/core/enumerable/entries_spec.rb index 83232cfa06..2de4fc756a 100644 --- a/spec/ruby/core/enumerable/entries_spec.rb +++ b/spec/ruby/core/enumerable/entries_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/entries' describe "Enumerable#entries" do - it_behaves_like :enumerable_entries , :entries + it_behaves_like :enumerable_entries, :entries end diff --git a/spec/ruby/core/enumerable/filter_map_spec.rb b/spec/ruby/core/enumerable/filter_map_spec.rb index aa4894230b..1ed131a960 100644 --- a/spec/ruby/core/enumerable/filter_map_spec.rb +++ b/spec/ruby/core/enumerable/filter_map_spec.rb @@ -19,6 +19,6 @@ describe 'Enumerable#filter_map' do end it 'returns an enumerator when no block given' do - @numerous.filter_map.should be_an_instance_of(Enumerator) + @numerous.filter_map.should.instance_of?(Enumerator) end end diff --git a/spec/ruby/core/enumerable/filter_spec.rb b/spec/ruby/core/enumerable/filter_spec.rb index 7e4f8c0b50..1c3a7e9ff5 100644 --- a/spec/ruby/core/enumerable/filter_spec.rb +++ b/spec/ruby/core/enumerable/filter_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find_all' describe "Enumerable#filter" do - it_behaves_like(:enumerable_find_all , :filter) + it_behaves_like :enumerable_find_all, :filter end diff --git a/spec/ruby/core/enumerable/find_all_spec.rb b/spec/ruby/core/enumerable/find_all_spec.rb index ce9058fe77..9cd635f247 100644 --- a/spec/ruby/core/enumerable/find_all_spec.rb +++ b/spec/ruby/core/enumerable/find_all_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find_all' describe "Enumerable#find_all" do - it_behaves_like :enumerable_find_all , :find_all + it_behaves_like :enumerable_find_all, :find_all end diff --git a/spec/ruby/core/enumerable/find_index_spec.rb b/spec/ruby/core/enumerable/find_index_spec.rb index 542660fe04..2e714367ba 100644 --- a/spec/ruby/core/enumerable/find_index_spec.rb +++ b/spec/ruby/core/enumerable/find_index_spec.rb @@ -47,7 +47,7 @@ describe "Enumerable#find_index" do end it "returns an Enumerator if no block given" do - @numerous.find_index.should be_an_instance_of(Enumerator) + @numerous.find_index.should.instance_of?(Enumerator) end it "uses #== for testing equality" do diff --git a/spec/ruby/core/enumerable/find_spec.rb b/spec/ruby/core/enumerable/find_spec.rb index 25aa3bf103..5ddebc05f8 100644 --- a/spec/ruby/core/enumerable/find_spec.rb +++ b/spec/ruby/core/enumerable/find_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find' describe "Enumerable#find" do - it_behaves_like :enumerable_find , :find + it_behaves_like :enumerable_find, :find end diff --git a/spec/ruby/core/enumerable/first_spec.rb b/spec/ruby/core/enumerable/first_spec.rb index ed1ba599b4..592dff1ebc 100644 --- a/spec/ruby/core/enumerable/first_spec.rb +++ b/spec/ruby/core/enumerable/first_spec.rb @@ -19,7 +19,7 @@ describe "Enumerable#first" do it "raises a RangeError when passed a Bignum" do enum = EnumerableSpecs::Empty.new - -> { enum.first(bignum_value) }.should raise_error(RangeError) + -> { enum.first(bignum_value) }.should.raise(RangeError) end describe "when passed an argument" do diff --git a/spec/ruby/core/enumerable/fixtures/classes.rb b/spec/ruby/core/enumerable/fixtures/classes.rb index fb4951c6e6..b5feafcfb7 100644 --- a/spec/ruby/core/enumerable/fixtures/classes.rb +++ b/spec/ruby/core/enumerable/fixtures/classes.rb @@ -38,12 +38,14 @@ module EnumerableSpecs class Empty include Enumerable def each + self end end class EmptyWithSize include Enumerable def each + self end def size 0 @@ -342,4 +344,7 @@ module EnumerableSpecs @block.call(*args) end end + + class SetSubclass < Set + end end # EnumerableSpecs utility classes diff --git a/spec/ruby/core/enumerable/flat_map_spec.rb b/spec/ruby/core/enumerable/flat_map_spec.rb index a294b9ddad..bd07eab6c5 100644 --- a/spec/ruby/core/enumerable/flat_map_spec.rb +++ b/spec/ruby/core/enumerable/flat_map_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/collect_concat' describe "Enumerable#flat_map" do - it_behaves_like :enumerable_collect_concat , :flat_map + it_behaves_like :enumerable_collect_concat, :flat_map end diff --git a/spec/ruby/core/enumerable/grep_spec.rb b/spec/ruby/core/enumerable/grep_spec.rb index b81075291f..965e183766 100644 --- a/spec/ruby/core/enumerable/grep_spec.rb +++ b/spec/ruby/core/enumerable/grep_spec.rb @@ -40,43 +40,28 @@ describe "Enumerable#grep" do $~.should == nil end - ruby_version_is ""..."3.0.0" do - it "sets $~ to the last match when given no block" do - "z" =~ /z/ # Reset $~ - ["abc", "def"].grep(/b/).should == ["abc"] - - # Set by the failed match of "def" - $~.should == nil - - ["abc", "def"].grep(/e/) - $&.should == "e" - end + it "does not set $~ when given no block" do + "z" =~ /z/ # Reset $~ + ["abc", "def"].grep(/b/).should == ["abc"] + $&.should == "z" end - ruby_version_is "3.0.0" do - it "does not set $~ when given no block" do - "z" =~ /z/ # Reset $~ - ["abc", "def"].grep(/b/).should == ["abc"] - $&.should == "z" - end - - it "does not modify Regexp.last_match without block" do - "z" =~ /z/ # Reset last match - ["abc", "def"].grep(/b/).should == ["abc"] - Regexp.last_match[0].should == "z" - end + it "does not modify Regexp.last_match without block" do + "z" =~ /z/ # Reset last match + ["abc", "def"].grep(/b/).should == ["abc"] + Regexp.last_match[0].should == "z" + end - it "correctly handles non-string elements" do - 'set last match' =~ /set last (.*)/ - [:a, 'b', 'z', :c, 42, nil].grep(/[a-d]/).should == [:a, 'b', :c] - $1.should == 'match' + it "correctly handles non-string elements" do + 'set last match' =~ /set last (.*)/ + [:a, 'b', 'z', :c, 42, nil].grep(/[a-d]/).should == [:a, 'b', :c] + $1.should == 'match' - o = Object.new - def o.to_str - 'hello' - end - [o].grep(/ll/).first.should.equal?(o) + o = Object.new + def o.to_str + 'hello' end + [o].grep(/ll/).first.should.equal?(o) end describe "with a block" do @@ -96,7 +81,7 @@ describe "Enumerable#grep" do end it "raises an ArgumentError when not given a pattern" do - -> { @numerous.grep { |e| e } }.should raise_error(ArgumentError) + -> { @numerous.grep { |e| e } }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/grep_v_spec.rb b/spec/ruby/core/enumerable/grep_v_spec.rb index 35fde27eb6..ee99a77ac1 100644 --- a/spec/ruby/core/enumerable/grep_v_spec.rb +++ b/spec/ruby/core/enumerable/grep_v_spec.rb @@ -20,43 +20,28 @@ describe "Enumerable#grep_v" do $&.should == "e" end - ruby_version_is ""..."3.0.0" do - it "sets $~ to the last match when given no block" do - "z" =~ /z/ # Reset $~ - ["abc", "def"].grep_v(/e/).should == ["abc"] - - # Set by the match of "def" - $&.should == "e" - - ["abc", "def"].grep_v(/b/) - $&.should == nil - end + it "does not set $~ when given no block" do + "z" =~ /z/ # Reset $~ + ["abc", "def"].grep_v(/e/).should == ["abc"] + $&.should == "z" end - ruby_version_is "3.0.0" do - it "does not set $~ when given no block" do - "z" =~ /z/ # Reset $~ - ["abc", "def"].grep_v(/e/).should == ["abc"] - $&.should == "z" - end - - it "does not modify Regexp.last_match without block" do - "z" =~ /z/ # Reset last match - ["abc", "def"].grep_v(/e/).should == ["abc"] - Regexp.last_match[0].should == "z" - end + it "does not modify Regexp.last_match without block" do + "z" =~ /z/ # Reset last match + ["abc", "def"].grep_v(/e/).should == ["abc"] + Regexp.last_match[0].should == "z" + end - it "correctly handles non-string elements" do - 'set last match' =~ /set last (.*)/ - [:a, 'b', 'z', :c, 42, nil].grep_v(/[a-d]/).should == ['z', 42, nil] - $1.should == 'match' + it "correctly handles non-string elements" do + 'set last match' =~ /set last (.*)/ + [:a, 'b', 'z', :c, 42, nil].grep_v(/[a-d]/).should == ['z', 42, nil] + $1.should == 'match' - o = Object.new - def o.to_str - 'hello' - end - [o].grep_v(/mm/).first.should.equal?(o) + o = Object.new + def o.to_str + 'hello' end + [o].grep_v(/mm/).first.should.equal?(o) end describe "without block" do @@ -70,7 +55,7 @@ describe "Enumerable#grep_v" do end it "raises an ArgumentError when not given a pattern" do - -> { @numerous.grep_v }.should raise_error(ArgumentError) + -> { @numerous.grep_v }.should.raise(ArgumentError) end end @@ -85,7 +70,7 @@ describe "Enumerable#grep_v" do end it "raises an ArgumentError when not given a pattern" do - -> { @numerous.grep_v { |e| e } }.should raise_error(ArgumentError) + -> { @numerous.grep_v { |e| e } }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/group_by_spec.rb b/spec/ruby/core/enumerable/group_by_spec.rb index 4fd1603819..904e5d6c68 100644 --- a/spec/ruby/core/enumerable/group_by_spec.rb +++ b/spec/ruby/core/enumerable/group_by_spec.rb @@ -16,13 +16,13 @@ describe "Enumerable#group_by" do it "returns a hash without default_proc" do e = EnumerableSpecs::Numerous.new("foo", "bar", "baz") h = e.group_by { |word| word[0..0].to_sym } - h[:some].should be_nil - h.default_proc.should be_nil - h.default.should be_nil + h[:some].should == nil + h.default_proc.should == nil + h.default.should == nil end it "returns an Enumerator if called without a block" do - EnumerableSpecs::Numerous.new.group_by.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.group_by.should.instance_of?(Enumerator) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/lazy_spec.rb b/spec/ruby/core/enumerable/lazy_spec.rb index 9a9ead81a0..935e574067 100644 --- a/spec/ruby/core/enumerable/lazy_spec.rb +++ b/spec/ruby/core/enumerable/lazy_spec.rb @@ -5,6 +5,6 @@ require_relative 'fixtures/classes' describe "Enumerable#lazy" do it "returns an instance of Enumerator::Lazy" do - EnumerableSpecs::Numerous.new.lazy.should be_an_instance_of(Enumerator::Lazy) + EnumerableSpecs::Numerous.new.lazy.should.instance_of?(Enumerator::Lazy) end end diff --git a/spec/ruby/core/enumerable/map_spec.rb b/spec/ruby/core/enumerable/map_spec.rb index d65aec238c..98a70781cf 100644 --- a/spec/ruby/core/enumerable/map_spec.rb +++ b/spec/ruby/core/enumerable/map_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/collect' describe "Enumerable#map" do - it_behaves_like :enumerable_collect , :map + it_behaves_like :enumerable_collect, :map end diff --git a/spec/ruby/core/enumerable/max_by_spec.rb b/spec/ruby/core/enumerable/max_by_spec.rb index ec1738ea3b..f67b5d15ea 100644 --- a/spec/ruby/core/enumerable/max_by_spec.rb +++ b/spec/ruby/core/enumerable/max_by_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#max_by" do it "returns an enumerator if no block" do - EnumerableSpecs::Numerous.new(42).max_by.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new(42).max_by.should.instance_of?(Enumerator) end it "returns nil if #each yields no objects" do @@ -18,7 +18,7 @@ describe "Enumerable#max_by" do it "returns the object that appears first in #each in case of a tie" do a, b, c = '1', '2', '2' - EnumerableSpecs::Numerous.new(a, b, c).max_by {|obj| obj.to_i }.should equal(b) + EnumerableSpecs::Numerous.new(a, b, c).max_by {|obj| obj.to_i }.should.equal?(b) end it "uses max.<=>(current) to determine order" do @@ -48,7 +48,7 @@ describe "Enumerable#max_by" do context "without a block" do it "returns an enumerator" do - @enum.max_by(2).should be_an_instance_of(Enumerator) + @enum.max_by(2).should.instance_of?(Enumerator) end end @@ -67,7 +67,7 @@ describe "Enumerable#max_by" do context "when n is negative" do it "raises an ArgumentError" do - -> { @enum.max_by(-1) { |i| i.to_s } }.should raise_error(ArgumentError) + -> { @enum.max_by(-1) { |i| i.to_s } }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/max_spec.rb b/spec/ruby/core/enumerable/max_spec.rb index 0c11ca0969..d92700258b 100644 --- a/spec/ruby/core/enumerable/max_spec.rb +++ b/spec/ruby/core/enumerable/max_spec.rb @@ -38,16 +38,16 @@ describe "Enumerable#max" do it "raises a NoMethodError for elements without #<=>" do -> do EnumerableSpecs::EachDefiner.new(BasicObject.new, BasicObject.new).max - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "raises an ArgumentError for incomparable elements" do -> do EnumerableSpecs::EachDefiner.new(11,"22").max - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) -> do EnumerableSpecs::EachDefiner.new(11,12,22,33).max{|a, b| nil} - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end context "when passed a block" do @@ -106,7 +106,7 @@ describe "Enumerable#max" do context "that is negative" do it "raises an ArgumentError" do - -> { @e_ints.max(-1) }.should raise_error(ArgumentError) + -> { @e_ints.max(-1) }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/min_by_spec.rb b/spec/ruby/core/enumerable/min_by_spec.rb index 3ff87e49d8..4f949e2130 100644 --- a/spec/ruby/core/enumerable/min_by_spec.rb +++ b/spec/ruby/core/enumerable/min_by_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#min_by" do it "returns an enumerator if no block" do - EnumerableSpecs::Numerous.new(42).min_by.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new(42).min_by.should.instance_of?(Enumerator) end it "returns nil if #each yields no objects" do @@ -18,7 +18,7 @@ describe "Enumerable#min_by" do it "returns the object that appears first in #each in case of a tie" do a, b, c = '2', '1', '1' - EnumerableSpecs::Numerous.new(a, b, c).min_by {|obj| obj.to_i }.should equal(b) + EnumerableSpecs::Numerous.new(a, b, c).min_by {|obj| obj.to_i }.should.equal?(b) end it "uses min.<=>(current) to determine order" do @@ -48,7 +48,7 @@ describe "Enumerable#min_by" do context "without a block" do it "returns an enumerator" do - @enum.min_by(2).should be_an_instance_of(Enumerator) + @enum.min_by(2).should.instance_of?(Enumerator) end end @@ -67,7 +67,7 @@ describe "Enumerable#min_by" do context "when n is negative" do it "raises an ArgumentError" do - -> { @enum.min_by(-1) { |i| i.to_s } }.should raise_error(ArgumentError) + -> { @enum.min_by(-1) { |i| i.to_s } }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/min_spec.rb b/spec/ruby/core/enumerable/min_spec.rb index 4b6ae248fa..f05d59c2c9 100644 --- a/spec/ruby/core/enumerable/min_spec.rb +++ b/spec/ruby/core/enumerable/min_spec.rb @@ -32,22 +32,22 @@ describe "Enumerable#min" do end it "returns nil for an empty Enumerable" do - EnumerableSpecs::EachDefiner.new.min.should be_nil + EnumerableSpecs::EachDefiner.new.min.should == nil end it "raises a NoMethodError for elements without #<=>" do -> do EnumerableSpecs::EachDefiner.new(BasicObject.new, BasicObject.new).min - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "raises an ArgumentError for incomparable elements" do -> do EnumerableSpecs::EachDefiner.new(11,"22").min - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) -> do EnumerableSpecs::EachDefiner.new(11,12,22,33).min{|a, b| nil} - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "returns the minimum when using a block rule" do @@ -110,7 +110,7 @@ describe "Enumerable#min" do context "that is negative" do it "raises an ArgumentError" do - -> { @e_ints.min(-1) }.should raise_error(ArgumentError) + -> { @e_ints.min(-1) }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/minmax_by_spec.rb b/spec/ruby/core/enumerable/minmax_by_spec.rb index a6a9249270..30c88cfcfb 100644 --- a/spec/ruby/core/enumerable/minmax_by_spec.rb +++ b/spec/ruby/core/enumerable/minmax_by_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#minmax_by" do it "returns an enumerator if no block" do - EnumerableSpecs::Numerous.new(42).minmax_by.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new(42).minmax_by.should.instance_of?(Enumerator) end it "returns nil if #each yields no objects" do @@ -19,8 +19,8 @@ describe "Enumerable#minmax_by" do it "returns the object that appears first in #each in case of a tie" do a, b, c, d = '1', '1', '2', '2' mm = EnumerableSpecs::Numerous.new(a, b, c, d).minmax_by {|obj| obj.to_i } - mm[0].should equal(a) - mm[1].should equal(c) + mm[0].should.equal?(a) + mm[1].should.equal?(c) end it "uses min/max.<=>(current) to determine order" do diff --git a/spec/ruby/core/enumerable/none_spec.rb b/spec/ruby/core/enumerable/none_spec.rb index 99bb7f7a4e..d9ee0b441e 100644 --- a/spec/ruby/core/enumerable/none_spec.rb +++ b/spec/ruby/core/enumerable/none_spec.rb @@ -15,35 +15,35 @@ describe "Enumerable#none?" do end it "raises an ArgumentError when more than 1 argument is provided" do - -> { @enum.none?(1, 2, 3) }.should raise_error(ArgumentError) - -> { [].none?(1, 2, 3) }.should raise_error(ArgumentError) - -> { {}.none?(1, 2, 3) }.should raise_error(ArgumentError) + -> { @enum.none?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].none?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.none?(1, 2, 3) }.should.raise(ArgumentError) end it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.none? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) -> { EnumerableSpecs::ThrowingEach.new.none? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do it "returns true if none of the elements in self are true" do e = EnumerableSpecs::Numerous.new(false, nil, false) - e.none?.should be_true + e.none?.should == true end it "returns false if at least one of the elements in self are true" do e = EnumerableSpecs::Numerous.new(false, nil, true, false) - e.none?.should be_false + e.none?.should == false end it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithFalse.new - multi.none?.should be_false + multi.none?.should == false end end @@ -65,17 +65,17 @@ describe "Enumerable#none?" do end it "returns true if the block never returns true" do - @e.none? {|e| false }.should be_true + @e.none? {|e| false }.should == true end it "returns false if the block ever returns true" do - @e.none? {|e| e == 3 ? true : false }.should be_false + @e.none? {|e| e == 3 ? true : false }.should == false end it "does not hide exceptions out of the block" do -> { @enum.none? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do @@ -94,7 +94,7 @@ describe "Enumerable#none?" do end describe 'when given a pattern argument' do - it "calls `===` on the pattern the return value " do + it "calls `===` on the pattern the return value" do pattern = EnumerableSpecs::Pattern.new { |x| x == 3 } @enum1.none?(pattern).should == true pattern.yielded.should == [[0], [1], [2], [-1]] @@ -109,7 +109,7 @@ describe "Enumerable#none?" do it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.none?(Integer) - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "returns true if the pattern never returns a truthy value" do @@ -134,7 +134,7 @@ describe "Enumerable#none?" do pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } -> { @enum.none?(pattern) - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "calls the pattern with gathered array when yielded with multiple arguments" do @@ -143,5 +143,11 @@ describe "Enumerable#none?" do multi.none?(pattern).should == true pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).none?(String) { true }.should == true + }.should complain(/given block not used/) + end end end diff --git a/spec/ruby/core/enumerable/one_spec.rb b/spec/ruby/core/enumerable/one_spec.rb index 47bfd65a0f..cf966d4a9b 100644 --- a/spec/ruby/core/enumerable/one_spec.rb +++ b/spec/ruby/core/enumerable/one_spec.rb @@ -15,57 +15,57 @@ describe "Enumerable#one?" do end it "raises an ArgumentError when more than 1 argument is provided" do - -> { @enum.one?(1, 2, 3) }.should raise_error(ArgumentError) - -> { [].one?(1, 2, 3) }.should raise_error(ArgumentError) - -> { {}.one?(1, 2, 3) }.should raise_error(ArgumentError) + -> { @enum.one?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].one?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.one?(1, 2, 3) }.should.raise(ArgumentError) end it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.one? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) -> { EnumerableSpecs::ThrowingEach.new.one? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do it "returns true if only one element evaluates to true" do - [false, nil, true].one?.should be_true + [false, nil, true].one?.should == true end it "returns false if two elements evaluate to true" do - [false, :value, nil, true].one?.should be_false + [false, :value, nil, true].one?.should == false end it "returns false if all elements evaluate to false" do - [false, nil, false].one?.should be_false + [false, nil, false].one?.should == false end it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithSingleTrue.new - multi.one?.should be_false + multi.one?.should == false end end describe "with a block" do it "returns true if block returns true once" do - [:a, :b, :c].one? { |s| s == :a }.should be_true + [:a, :b, :c].one? { |s| s == :a }.should == true end it "returns false if the block returns true more than once" do - [:a, :b, :c].one? { |s| s == :a || s == :b }.should be_false + [:a, :b, :c].one? { |s| s == :a || s == :b }.should == false end it "returns false if the block only returns false" do - [:a, :b, :c].one? { |s| s == :d }.should be_false + [:a, :b, :c].one? { |s| s == :d }.should == false end it "does not hide exceptions out of the block" do -> { @enum.one? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do @@ -83,9 +83,8 @@ describe "Enumerable#one?" do end end - describe 'when given a pattern argument' do - it "calls `===` on the pattern the return value " do + it "calls `===` on the pattern the return value" do pattern = EnumerableSpecs::Pattern.new { |x| x == 1 } @enum1.one?(pattern).should == true pattern.yielded.should == [[0], [1], [2], [-1]] @@ -100,7 +99,7 @@ describe "Enumerable#one?" do it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.one?(Integer) - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "returns true if the pattern returns a truthy value only once" do @@ -136,7 +135,7 @@ describe "Enumerable#one?" do pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } -> { @enum.one?(pattern) - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "calls the pattern with gathered array when yielded with multiple arguments" do @@ -145,5 +144,11 @@ describe "Enumerable#one?" do multi.one?(pattern).should == false pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, "5").one?(String) { false }.should == true + }.should complain(/given block not used/) + end end end diff --git a/spec/ruby/core/enumerable/partition_spec.rb b/spec/ruby/core/enumerable/partition_spec.rb index d3d220b4b4..8272ee189e 100644 --- a/spec/ruby/core/enumerable/partition_spec.rb +++ b/spec/ruby/core/enumerable/partition_spec.rb @@ -8,7 +8,7 @@ describe "Enumerable#partition" do end it "returns an Enumerator if called without a block" do - EnumerableSpecs::Numerous.new.partition.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.partition.should.instance_of?(Enumerator) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/reject_spec.rb b/spec/ruby/core/enumerable/reject_spec.rb index 0d86b49ea2..31e89f5b0e 100644 --- a/spec/ruby/core/enumerable/reject_spec.rb +++ b/spec/ruby/core/enumerable/reject_spec.rb @@ -13,7 +13,7 @@ describe "Enumerable#reject" do end it "returns an Enumerator if called without a block" do - EnumerableSpecs::Numerous.new.reject.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.reject.should.instance_of?(Enumerator) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/reverse_each_spec.rb b/spec/ruby/core/enumerable/reverse_each_spec.rb index 2b1c233488..4753956724 100644 --- a/spec/ruby/core/enumerable/reverse_each_spec.rb +++ b/spec/ruby/core/enumerable/reverse_each_spec.rb @@ -11,7 +11,7 @@ describe "Enumerable#reverse_each" do it "returns an Enumerator if no block given" do enum = EnumerableSpecs::Numerous.new.reverse_each - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == [4, 1, 6, 3, 5, 2] end diff --git a/spec/ruby/core/enumerable/select_spec.rb b/spec/ruby/core/enumerable/select_spec.rb index 11168eb42e..7fc61926f9 100644 --- a/spec/ruby/core/enumerable/select_spec.rb +++ b/spec/ruby/core/enumerable/select_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find_all' describe "Enumerable#select" do - it_behaves_like :enumerable_find_all , :select + it_behaves_like :enumerable_find_all, :select end diff --git a/spec/ruby/core/enumerable/shared/collect.rb b/spec/ruby/core/enumerable/shared/collect.rb index 6df1a616eb..4696d32454 100644 --- a/spec/ruby/core/enumerable/shared/collect.rb +++ b/spec/ruby/core/enumerable/shared/collect.rb @@ -30,7 +30,7 @@ describe :enumerable_collect, shared: true do it "returns an enumerator when no block given" do enum = EnumerableSpecs::Numerous.new.send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each { |i| -i }.should == [-2, -5, -3, -6, -1, -4] end @@ -86,7 +86,7 @@ describe :enumerable_collect, shared: true do -> do { 1 => 'a', 2 => 'b' }.map(&m) - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "calls the each method on sub-classes" do diff --git a/spec/ruby/core/enumerable/shared/collect_concat.rb b/spec/ruby/core/enumerable/shared/collect_concat.rb index ddd431baeb..1694e3fdce 100644 --- a/spec/ruby/core/enumerable/shared/collect_concat.rb +++ b/spec/ruby/core/enumerable/shared/collect_concat.rb @@ -41,12 +41,12 @@ describe :enumerable_collect_concat, shared: true do obj = mock("to_ary defined") obj.should_receive(:to_ary).and_return("array") - -> { [1, obj, 3].send(@method) { |i| i } }.should raise_error(TypeError) + -> { [1, obj, 3].send(@method) { |i| i } }.should.raise(TypeError) end it "returns an enumerator when no block given" do enum = EnumerableSpecs::Numerous.new(1, 2).send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each{ |i| [i] * i }.should == [1, 2, 2] end diff --git a/spec/ruby/core/enumerable/shared/find.rb b/spec/ruby/core/enumerable/shared/find.rb index 61d63ba3d5..cdff640415 100644 --- a/spec/ruby/core/enumerable/shared/find.rb +++ b/spec/ruby/core/enumerable/shared/find.rb @@ -59,7 +59,7 @@ describe :enumerable_find, shared: true do end it "returns an enumerator when no block given" do - @numerous.send(@method).should be_an_instance_of(Enumerator) + @numerous.send(@method).should.instance_of?(Enumerator) end it "passes the ifnone proc to the enumerator" do diff --git a/spec/ruby/core/enumerable/shared/find_all.rb b/spec/ruby/core/enumerable/shared/find_all.rb index 1bbe71f372..27f01de6e0 100644 --- a/spec/ruby/core/enumerable/shared/find_all.rb +++ b/spec/ruby/core/enumerable/shared/find_all.rb @@ -14,7 +14,7 @@ describe :enumerable_find_all, shared: true do end it "returns an enumerator when no block given" do - @numerous.send(@method).should be_an_instance_of(Enumerator) + @numerous.send(@method).should.instance_of?(Enumerator) end it "passes through the values yielded by #each_with_index" do diff --git a/spec/ruby/core/enumerable/shared/include.rb b/spec/ruby/core/enumerable/shared/include.rb index 569f350fd5..ea250f032b 100644 --- a/spec/ruby/core/enumerable/shared/include.rb +++ b/spec/ruby/core/enumerable/shared/include.rb @@ -28,7 +28,7 @@ describe :enumerable_include, shared: true do it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMulti.new - multi.send(@method, [1,2]).should be_true + multi.send(@method, [1,2]).should == true end end diff --git a/spec/ruby/core/enumerable/shared/inject.rb b/spec/ruby/core/enumerable/shared/inject.rb index c5907f92d8..7da4f0ca99 100644 --- a/spec/ruby/core/enumerable/shared/inject.rb +++ b/spec/ruby/core/enumerable/shared/inject.rb @@ -1,3 +1,5 @@ +require_relative '../../array/shared/iterable_and_tolerating_size_increasing' + describe :enumerable_inject, shared: true do it "with argument takes a block with an accumulator (with argument as initial value) and the current element. Value of block becomes new accumulator" do a = [] @@ -14,15 +16,65 @@ describe :enumerable_inject, shared: true do it "can take two argument" do EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, :-).should == 4 + EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, "-").should == 4 + + [1, 2, 3].send(@method, 10, :-).should == 4 + [1, 2, 3].send(@method, 10, "-").should == 4 + end + + it "converts non-Symbol method name argument to String with #to_str if two arguments" do + name = Object.new + def name.to_str; "-"; end + + EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, name).should == 4 + [1, 2, 3].send(@method, 10, name).should == 4 + end + + it "raises TypeError when the second argument is not Symbol or String and it cannot be converted to String if two arguments" do + -> { EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) + -> { [1, 2, 3].send(@method, 10, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) end it "ignores the block if two arguments" do - EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, :-) { raise "we never get here"}.should == 4 - [].send(@method, 3, :+) { raise "we never get here"}.should == 3 + -> { + EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, :-) { raise "we never get here"}.should == 4 + }.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true) + + -> { + [1, 2, 3].send(@method, 10, :-) { raise "we never get here"}.should == 4 + }.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true) + end + + it "does not warn when given a Symbol with $VERBOSE true" do + -> { + [1, 2].send(@method, 0, :+) + [1, 2].send(@method, :+) + EnumerableSpecs::Numerous.new(1, 2).send(@method, 0, :+) + EnumerableSpecs::Numerous.new(1, 2).send(@method, :+) + }.should_not complain(verbose: true) end it "can take a symbol argument" do EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, :-).should == 4 + [10, 1, 2, 3].send(@method, :-).should == 4 + end + + it "can take a String argument" do + EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, "-").should == 4 + [10, 1, 2, 3].send(@method, "-").should == 4 + end + + it "converts non-Symbol method name argument to String with #to_str" do + name = Object.new + def name.to_str; "-"; end + + EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, name).should == 4 + [10, 1, 2, 3].send(@method, name).should == 4 + end + + it "raises TypeError when passed not Symbol or String method name argument and it cannot be converted to String" do + -> { EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) + -> { [10, 1, 2, 3].send(@method, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) end it "without argument takes a block with an accumulator (with first element as initial value) and the current element. Value of block becomes new accumulator" do @@ -31,10 +83,10 @@ describe :enumerable_inject, shared: true do a.should == [[2, 5], [5, 3], [3, 6], [6, 1], [1, 4]] end - it "gathers whole arrays as elements when each yields multiple" do - multi = EnumerableSpecs::YieldsMulti.new - multi.send(@method, []) {|acc, e| acc << e }.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]] - end + it "gathers whole arrays as elements when each yields multiple" do + multi = EnumerableSpecs::YieldsMulti.new + multi.send(@method, []) {|acc, e| acc << e }.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]] + end it "with inject arguments(legacy rubycon)" do # with inject argument @@ -51,7 +103,7 @@ describe :enumerable_inject, shared: true do it "without inject arguments(legacy rubycon)" do # no inject argument - EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| 999 } .should == 2 + EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| 999 }.should == 2 EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| acc }.should == 2 EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| x }.should == 2 @@ -61,17 +113,30 @@ describe :enumerable_inject, shared: true do EnumerableSpecs::EachDefiner.new('a','b','c').send(@method) {|result, i| i+result}.should == "cba" EnumerableSpecs::EachDefiner.new(3, 4, 5).send(@method) {|result, i| result*i}.should == 60 EnumerableSpecs::EachDefiner.new([1], 2, 'a','b').send(@method){|r,i| r<<i}.should == [1, 2, 'a', 'b'] - end it "returns nil when fails(legacy rubycon)" do EnumerableSpecs::EachDefiner.new().send(@method) {|acc,x| 999 }.should == nil end - ruby_bug '#18635', ''...'3.2' do - it "raises an ArgumentError when no parameters or block is given" do - -> { [1,2].send(@method) }.should raise_error(ArgumentError) - -> { {one: 1, two: 2}.send(@method) }.should raise_error(ArgumentError) + it "tolerates increasing a collection size during iterating Array" do + array = [:a, :b, :c] + ScratchPad.record [] + i = 0 + + array.send(@method, nil) do |_, e| + ScratchPad << e + array << i if i < 100 + i += 1 end + + actual = ScratchPad.recorded + expected = [:a, :b, :c] + (0..99).to_a + actual.sort_by(&:to_s).should == expected.sort_by(&:to_s) + end + + it "raises an ArgumentError when no parameters or block is given" do + -> { [1,2].send(@method) }.should.raise(ArgumentError) + -> { {one: 1, two: 2}.send(@method) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerable/shared/take.rb b/spec/ruby/core/enumerable/shared/take.rb index ce2ace20fa..a6da06325f 100644 --- a/spec/ruby/core/enumerable/shared/take.rb +++ b/spec/ruby/core/enumerable/shared/take.rb @@ -25,7 +25,7 @@ describe :enumerable_take, shared: true do end it "raises an ArgumentError when count is negative" do - -> { @enum.send(@method, -1) }.should raise_error(ArgumentError) + -> { @enum.send(@method, -1) }.should.raise(ArgumentError) end it "returns the entire array when count > length" do @@ -40,11 +40,11 @@ describe :enumerable_take, shared: true do end it "raises a TypeError if the passed argument is not numeric" do - -> { @enum.send(@method, nil) }.should raise_error(TypeError) - -> { @enum.send(@method, "a") }.should raise_error(TypeError) + -> { @enum.send(@method, nil) }.should.raise(TypeError) + -> { @enum.send(@method, "a") }.should.raise(TypeError) obj = mock("nonnumeric") - -> { @enum.send(@method, obj) }.should raise_error(TypeError) + -> { @enum.send(@method, obj) }.should.raise(TypeError) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/slice_after_spec.rb b/spec/ruby/core/enumerable/slice_after_spec.rb index 0e46688db1..1ef37195e5 100644 --- a/spec/ruby/core/enumerable/slice_after_spec.rb +++ b/spec/ruby/core/enumerable/slice_after_spec.rb @@ -11,7 +11,7 @@ describe "Enumerable#slice_after" do arg = mock("filter") arg.should_receive(:===).and_return(false, true, false, false, false, true, false) e = @enum.slice_after(arg) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[7, 6], [5, 4, 3, 2], [1]] end @@ -34,21 +34,21 @@ describe "Enumerable#slice_after" do describe "and no argument" do it "calls the block to determine when to yield" do e = @enum.slice_after{ |i| i == 6 || i == 2 } - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[7, 6], [5, 4, 3, 2], [1]] end end describe "and an argument" do it "raises an ArgumentError" do - -> { @enum.slice_after(42) { |i| i == 6 } }.should raise_error(ArgumentError) + -> { @enum.slice_after(42) { |i| i == 6 } }.should.raise(ArgumentError) end end end it "raises an ArgumentError when given an incorrect number of arguments" do - -> { @enum.slice_after("one", "two") }.should raise_error(ArgumentError) - -> { @enum.slice_after }.should raise_error(ArgumentError) + -> { @enum.slice_after("one", "two") }.should.raise(ArgumentError) + -> { @enum.slice_after }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerable/slice_before_spec.rb b/spec/ruby/core/enumerable/slice_before_spec.rb index f9b33f7b28..7eb4410a25 100644 --- a/spec/ruby/core/enumerable/slice_before_spec.rb +++ b/spec/ruby/core/enumerable/slice_before_spec.rb @@ -12,7 +12,7 @@ describe "Enumerable#slice_before" do arg = mock "filter" arg.should_receive(:===).and_return(false, true, false, false, false, true, false) e = @enum.slice_before(arg) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[7], [6, 5, 4, 3], [2, 1]] end @@ -35,7 +35,7 @@ describe "Enumerable#slice_before" do describe "and no argument" do it "calls the block to determine when to yield" do e = @enum.slice_before{|i| i == 6 || i == 2} - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[7], [6, 5, 4, 3], [2, 1]] end end @@ -43,13 +43,13 @@ describe "Enumerable#slice_before" do it "does not accept arguments" do -> { @enum.slice_before(1) {} - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end it "raises an ArgumentError when given an incorrect number of arguments" do - -> { @enum.slice_before("one", "two") }.should raise_error(ArgumentError) - -> { @enum.slice_before }.should raise_error(ArgumentError) + -> { @enum.slice_before("one", "two") }.should.raise(ArgumentError) + -> { @enum.slice_before }.should.raise(ArgumentError) end describe "when an iterator method yields more than one value" do diff --git a/spec/ruby/core/enumerable/slice_when_spec.rb b/spec/ruby/core/enumerable/slice_when_spec.rb index 6b8ea0923e..fe1ecd31e2 100644 --- a/spec/ruby/core/enumerable/slice_when_spec.rb +++ b/spec/ruby/core/enumerable/slice_when_spec.rb @@ -11,7 +11,7 @@ describe "Enumerable#slice_when" do context "when given a block" do it "returns an enumerator" do - @result.should be_an_instance_of(Enumerator) + @result.should.instance_of?(Enumerator) end it "splits chunks between adjacent elements i and j where the block returns true" do @@ -39,7 +39,7 @@ describe "Enumerable#slice_when" do context "when not given a block" do it "raises an ArgumentError" do - -> { @enum.slice_when }.should raise_error(ArgumentError) + -> { @enum.slice_when }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerable/sort_by_spec.rb b/spec/ruby/core/enumerable/sort_by_spec.rb index 8fdd923fb4..62cf38ce3e 100644 --- a/spec/ruby/core/enumerable/sort_by_spec.rb +++ b/spec/ruby/core/enumerable/sort_by_spec.rb @@ -18,7 +18,7 @@ describe "Enumerable#sort_by" do it "returns an Enumerator when a block is not supplied" do a = EnumerableSpecs::Numerous.new("a","b") - a.sort_by.should be_an_instance_of(Enumerator) + a.sort_by.should.instance_of?(Enumerator) a.to_a.should == ["a", "b"] end diff --git a/spec/ruby/core/enumerable/sort_spec.rb b/spec/ruby/core/enumerable/sort_spec.rb index 6fc64f325e..427b1cd8f1 100644 --- a/spec/ruby/core/enumerable/sort_spec.rb +++ b/spec/ruby/core/enumerable/sort_spec.rb @@ -16,7 +16,7 @@ describe "Enumerable#sort" do it "raises a NoMethodError if elements do not define <=>" do -> do EnumerableSpecs::Numerous.new(BasicObject.new, BasicObject.new, BasicObject.new).sort - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "sorts enumerables that contain nils" do @@ -35,12 +35,12 @@ describe "Enumerable#sort" do }.should == [6, 5, 4, 3, 2, 1] -> { EnumerableSpecs::Numerous.new.sort { |n, m| (n <=> m).to_s } - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an error if objects can't be compared" do a=EnumerableSpecs::Numerous.new(EnumerableSpecs::Uncomparable.new, EnumerableSpecs::Uncomparable.new) - -> {a.sort}.should raise_error(ArgumentError) + -> {a.sort}.should.raise(ArgumentError) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/sum_spec.rb b/spec/ruby/core/enumerable/sum_spec.rb index 4a978794e5..2eb74db6ac 100644 --- a/spec/ruby/core/enumerable/sum_spec.rb +++ b/spec/ruby/core/enumerable/sum_spec.rb @@ -22,12 +22,25 @@ describe 'Enumerable#sum' do @enum.sum.should == 5/3r end - it 'takes a block to transform the elements' do - @enum.sum { |element| element * 2 }.should == 10/3r + context 'with a block' do + it 'transforms the elements' do + @enum.sum { |element| element * 2 }.should == 10/3r + end + + it 'does not destructure array elements' do + class << @enum + def each + yield [1,2] + yield [3] + end + end + + @enum.sum(&:last).should == 5 + end end # https://bugs.ruby-lang.org/issues/12217 - # https://github.com/ruby/ruby/blob/master/doc/ChangeLog-2.4.0#L6208-L6214 + # https://github.com/ruby/ruby/blob/master/doc/ChangeLog/ChangeLog-2.4.0#L6208-L6214 it "uses Kahan's compensated summation algorithm for precise sum of float numbers" do floats = [2.7800000000000002, 5.0, 2.5, 4.44, 3.89, 3.89, 4.44, 7.78, 5.0, 2.7800000000000002, 5.0, 2.5].to_enum naive_sum = floats.reduce { |sum, e| sum + e } diff --git a/spec/ruby/core/enumerable/take_spec.rb b/spec/ruby/core/enumerable/take_spec.rb index 41a7438330..8cc746f88d 100644 --- a/spec/ruby/core/enumerable/take_spec.rb +++ b/spec/ruby/core/enumerable/take_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/take' describe "Enumerable#take" do it "requires an argument" do - ->{ EnumerableSpecs::Numerous.new.take}.should raise_error(ArgumentError) + ->{ EnumerableSpecs::Numerous.new.take}.should.raise(ArgumentError) end describe "when passed an argument" do diff --git a/spec/ruby/core/enumerable/take_while_spec.rb b/spec/ruby/core/enumerable/take_while_spec.rb index 26db39ac4b..918bfc897d 100644 --- a/spec/ruby/core/enumerable/take_while_spec.rb +++ b/spec/ruby/core/enumerable/take_while_spec.rb @@ -8,7 +8,7 @@ describe "Enumerable#take_while" do end it "returns an Enumerator if no block given" do - @enum.take_while.should be_an_instance_of(Enumerator) + @enum.take_while.should.instance_of?(Enumerator) end it "returns no/all elements for {true/false} block" do @@ -38,7 +38,7 @@ describe "Enumerable#take_while" do it "doesn't return self when it could" do a = [1,2,3] - a.take_while{true}.should_not equal(a) + a.take_while{true}.should_not.equal?(a) end it "calls the block with initial args when yielded with multiple arguments" do diff --git a/spec/ruby/core/enumerable/tally_spec.rb b/spec/ruby/core/enumerable/tally_spec.rb index f09a8f533a..deef741407 100644 --- a/spec/ruby/core/enumerable/tally_spec.rb +++ b/spec/ruby/core/enumerable/tally_spec.rb @@ -13,8 +13,8 @@ describe "Enumerable#tally" do it "returns a hash without default" do hash = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz').tally - hash.default_proc.should be_nil - hash.default.should be_nil + hash.default_proc.should == nil + hash.default.should == nil end it "returns an empty hash for empty enumerables" do @@ -32,49 +32,60 @@ describe "Enumerable#tally" do end end -ruby_version_is "3.1" do - describe "Enumerable#tally with a hash" do - before :each do - ScratchPad.record [] - end - - it "returns a hash with counts according to the value" do - enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') - enum.tally({ 'foo' => 1 }).should == { 'foo' => 3, 'bar' => 1, 'baz' => 1} - end - - it "returns the given hash" do - enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') - hash = { 'foo' => 1 } - enum.tally(hash).should equal(hash) - end - - it "raises a FrozenError and does not update the given hash when the hash is frozen" do - enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') - hash = { 'foo' => 1 }.freeze - -> { enum.tally(hash) }.should raise_error(FrozenError) - hash.should == { 'foo' => 1 } - end - - it "does not call given block" do - enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') - enum.tally({ 'foo' => 1 }) { |v| ScratchPad << v } - ScratchPad.recorded.should == [] - end - - it "ignores the default value" do - enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') - enum.tally(Hash.new(100)).should == { 'foo' => 2, 'bar' => 1, 'baz' => 1} - end - - it "ignores the default proc" do - enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') - enum.tally(Hash.new {100}).should == { 'foo' => 2, 'bar' => 1, 'baz' => 1} - end - - it "needs the values counting each elements to be an integer" do - enum = EnumerableSpecs::Numerous.new('foo') - -> { enum.tally({ 'foo' => 'bar' }) }.should raise_error(TypeError) - end +describe "Enumerable#tally with a hash" do + before :each do + ScratchPad.record [] + end + + it "returns a hash with counts according to the value" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally({ 'foo' => 1 }).should == { 'foo' => 3, 'bar' => 1, 'baz' => 1} + end + + it "returns the given hash" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + hash = { 'foo' => 1 } + enum.tally(hash).should.equal?(hash) + end + + it "calls #to_hash to convert argument to Hash implicitly if passed not a Hash" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + object = Object.new + def object.to_hash; { 'foo' => 1 }; end + enum.tally(object).should == { 'foo' => 3, 'bar' => 1, 'baz' => 1} + end + + it "raises a FrozenError and does not update the given hash when the hash is frozen" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + hash = { 'foo' => 1 }.freeze + -> { enum.tally(hash) }.should.raise(FrozenError) + hash.should == { 'foo' => 1 } + end + + it "raises a FrozenError even if enumerable is empty" do + enum = EnumerableSpecs::Numerous.new() + hash = { 'foo' => 1 }.freeze + -> { enum.tally(hash) }.should.raise(FrozenError) + end + + it "does not call given block" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally({ 'foo' => 1 }) { |v| ScratchPad << v } + ScratchPad.recorded.should == [] + end + + it "ignores the default value" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally(Hash.new(100)).should == { 'foo' => 2, 'bar' => 1, 'baz' => 1} + end + + it "ignores the default proc" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally(Hash.new {100}).should == { 'foo' => 2, 'bar' => 1, 'baz' => 1} + end + + it "needs the values counting each elements to be an integer" do + enum = EnumerableSpecs::Numerous.new('foo') + -> { enum.tally({ 'foo' => 'bar' }) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/enumerable/to_a_spec.rb b/spec/ruby/core/enumerable/to_a_spec.rb index 0f3060dc48..723f922574 100644 --- a/spec/ruby/core/enumerable/to_a_spec.rb +++ b/spec/ruby/core/enumerable/to_a_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/entries' describe "Enumerable#to_a" do - it_behaves_like :enumerable_entries , :to_a + it_behaves_like :enumerable_entries, :to_a end diff --git a/spec/ruby/core/enumerable/to_h_spec.rb b/spec/ruby/core/enumerable/to_h_spec.rb index 0489134552..38847eccfb 100644 --- a/spec/ruby/core/enumerable/to_h_spec.rb +++ b/spec/ruby/core/enumerable/to_h_spec.rb @@ -36,12 +36,12 @@ describe "Enumerable#to_h" do it "raises TypeError if an element is not an array" do enum = EnumerableSpecs::EachDefiner.new(:x) - -> { enum.to_h }.should raise_error(TypeError) + -> { enum.to_h }.should.raise(TypeError) end it "raises ArgumentError if an element is not a [key, value] pair" do enum = EnumerableSpecs::EachDefiner.new([:x]) - -> { enum.to_h }.should raise_error(ArgumentError) + -> { enum.to_h }.should.raise(ArgumentError) end context "with block" do @@ -53,20 +53,28 @@ describe "Enumerable#to_h" do @enum.to_h { |k| [k, k.to_s] }.should == { a: 'a', b: 'b' } end + it "passes to a block each element as a single argument" do + enum_of_arrays = EnumerableSpecs::EachDefiner.new([:a, 1], [:b, 2]) + + ScratchPad.record [] + enum_of_arrays.to_h { |*args| ScratchPad << args; [args[0], args[1]] } + ScratchPad.recorded.sort.should == [[[:a, 1]], [[:b, 2]]] + end + it "raises ArgumentError if block returns longer or shorter array" do -> do @enum.to_h { |k| [k, k.to_s, 1] } - end.should raise_error(ArgumentError, /element has wrong array length/) + end.should.raise(ArgumentError, /element has wrong array length/) -> do @enum.to_h { |k| [k] } - end.should raise_error(ArgumentError, /element has wrong array length/) + end.should.raise(ArgumentError, /element has wrong array length/) end it "raises TypeError if block returns something other than Array" do -> do @enum.to_h { |k| "not-array" } - end.should raise_error(TypeError, /wrong element type String/) + end.should.raise(TypeError, /wrong element type String/) end it "coerces returned pair to Array with #to_ary" do @@ -82,7 +90,7 @@ describe "Enumerable#to_h" do -> do @enum.to_h { |k| x } - end.should raise_error(TypeError, /wrong element type MockObject/) + end.should.raise(TypeError, /wrong element type MockObject/) end end end diff --git a/spec/ruby/core/enumerable/to_set_spec.rb b/spec/ruby/core/enumerable/to_set_spec.rb new file mode 100644 index 0000000000..7b04c72bce --- /dev/null +++ b/spec/ruby/core/enumerable/to_set_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Enumerable#to_set" do + it "returns a new Set created from self" do + [1, 2, 3].to_set.should == Set[1, 2, 3] + {a: 1, b: 2}.to_set.should == Set[[:b, 2], [:a, 1]] + end + + it "passes down passed blocks" do + [1, 2, 3].to_set { |x| x * x }.should == Set[1, 4, 9] + end + + ruby_version_is "4.0"..."4.1" do + it "instantiates an object of provided as the first argument set class" do + set = nil + proc{set = [1, 2, 3].to_set(EnumerableSpecs::SetSubclass)}.should complain(/Enumerable#to_set/) + set.should.is_a?(EnumerableSpecs::SetSubclass) + set.to_a.sort.should == [1, 2, 3] + end + end + + ruby_version_is ""..."4.0" do + it "instantiates an object of provided as the first argument set class" do + set = [1, 2, 3].to_set(EnumerableSpecs::SetSubclass) + set.should.is_a?(EnumerableSpecs::SetSubclass) + set.to_a.sort.should == [1, 2, 3] + end + end +end diff --git a/spec/ruby/core/enumerable/zip_spec.rb b/spec/ruby/core/enumerable/zip_spec.rb index 9ec15aa030..c5f9a3e4d4 100644 --- a/spec/ruby/core/enumerable/zip_spec.rb +++ b/spec/ruby/core/enumerable/zip_spec.rb @@ -38,4 +38,9 @@ describe "Enumerable#zip" do multi.zip(multi).should == [[[1, 2], [1, 2]], [[3, 4, 5], [3, 4, 5]], [[6, 7, 8, 9], [6, 7, 8, 9]]] end + it "raises TypeError when some argument isn't Array and doesn't respond to #to_ary and #to_enum" do + -> { EnumerableSpecs::Numerous.new(1,2,3).zip(Object.new) }.should.raise(TypeError, "wrong argument type Object (must respond to :each)") + -> { EnumerableSpecs::Numerous.new(1,2,3).zip(1) }.should.raise(TypeError, "wrong argument type Integer (must respond to :each)") + -> { EnumerableSpecs::Numerous.new(1,2,3).zip(true) }.should.raise(TypeError, "wrong argument type TrueClass (must respond to :each)") + end end |
