diff options
Diffstat (limited to 'spec/ruby/core/enumerable')
22 files changed, 464 insertions, 499 deletions
diff --git a/spec/ruby/core/enumerable/collect_concat_spec.rb b/spec/ruby/core/enumerable/collect_concat_spec.rb index 59317cfe34..5024aaddab 100644 --- a/spec/ruby/core/enumerable/collect_concat_spec.rb +++ b/spec/ruby/core/enumerable/collect_concat_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/collect_concat' describe "Enumerable#collect_concat" do - it_behaves_like :enumerable_collect_concat, :collect_concat + it "is an alias of Enumerable#flat_map" do + Enumerable.instance_method(:collect_concat).should == Enumerable.instance_method(:flat_map) + end end diff --git a/spec/ruby/core/enumerable/collect_spec.rb b/spec/ruby/core/enumerable/collect_spec.rb index cfa2895cce..319b1b263d 100644 --- a/spec/ruby/core/enumerable/collect_spec.rb +++ b/spec/ruby/core/enumerable/collect_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/collect' describe "Enumerable#collect" do - it_behaves_like :enumerable_collect, :collect + it "is an alias of Enumerable#map" do + Enumerable.instance_method(:collect).should == Enumerable.instance_method(:map) + end end diff --git a/spec/ruby/core/enumerable/detect_spec.rb b/spec/ruby/core/enumerable/detect_spec.rb index 6959aadc44..0669c50c58 100644 --- a/spec/ruby/core/enumerable/detect_spec.rb +++ b/spec/ruby/core/enumerable/detect_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/find' describe "Enumerable#detect" do - it_behaves_like :enumerable_find, :detect + it "is an alias of Enumerable#find" do + Enumerable.instance_method(:detect).should == Enumerable.instance_method(:find) + end end diff --git a/spec/ruby/core/enumerable/entries_spec.rb b/spec/ruby/core/enumerable/entries_spec.rb index 2de4fc756a..8cb29b7b47 100644 --- a/spec/ruby/core/enumerable/entries_spec.rb +++ b/spec/ruby/core/enumerable/entries_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/entries' describe "Enumerable#entries" do - it_behaves_like :enumerable_entries, :entries + it "is an alias of Enumerable#to_a" do + Enumerable.instance_method(:entries).should == Enumerable.instance_method(:to_a) + end end diff --git a/spec/ruby/core/enumerable/filter_spec.rb b/spec/ruby/core/enumerable/filter_spec.rb index 1c3a7e9ff5..d075b39396 100644 --- a/spec/ruby/core/enumerable/filter_spec.rb +++ b/spec/ruby/core/enumerable/filter_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/find_all' describe "Enumerable#filter" do - it_behaves_like :enumerable_find_all, :filter + it "is an alias of Enumerable#select" do + Enumerable.instance_method(:filter).should == Enumerable.instance_method(:select) + end end diff --git a/spec/ruby/core/enumerable/find_all_spec.rb b/spec/ruby/core/enumerable/find_all_spec.rb index 9cd635f247..1095a19569 100644 --- a/spec/ruby/core/enumerable/find_all_spec.rb +++ b/spec/ruby/core/enumerable/find_all_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/find_all' describe "Enumerable#find_all" do - it_behaves_like :enumerable_find_all, :find_all + it "is an alias of Enumerable#select" do + Enumerable.instance_method(:find_all).should == Enumerable.instance_method(:select) + end end diff --git a/spec/ruby/core/enumerable/find_spec.rb b/spec/ruby/core/enumerable/find_spec.rb index 5ddebc05f8..4ac4b75c47 100644 --- a/spec/ruby/core/enumerable/find_spec.rb +++ b/spec/ruby/core/enumerable/find_spec.rb @@ -1,7 +1,78 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/find' +require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#find" do - it_behaves_like :enumerable_find, :find + before :each do + ScratchPad.record [] + @elements = [2, 4, 6, 8, 10] + @numerous = EnumerableSpecs::Numerous.new(*@elements) + @empty = [] + end + + it "passes each entry in enum to block while block when block is false" do + visited_elements = [] + @numerous.find do |element| + visited_elements << element + false + end + visited_elements.should == @elements + end + + it "returns nil when the block is false and there is no ifnone proc given" do + @numerous.find {|e| false }.should == nil + end + + it "returns the first element for which the block is not false" do + @elements.each do |element| + @numerous.find {|e| e > element - 1 }.should == element + end + end + + it "returns the value of the ifnone proc if the block is false" do + fail_proc = -> { "cheeseburgers" } + @numerous.find(fail_proc) {|e| false }.should == "cheeseburgers" + end + + it "doesn't call the ifnone proc if an element is found" do + fail_proc = -> { raise "This shouldn't have been called" } + @numerous.find(fail_proc) {|e| e == @elements.first }.should == 2 + end + + it "calls the ifnone proc only once when the block is false" do + times = 0 + fail_proc = -> { times += 1; raise if times > 1; "cheeseburgers" } + @numerous.find(fail_proc) {|e| false }.should == "cheeseburgers" + end + + it "calls the ifnone proc when there are no elements" do + fail_proc = -> { "yay" } + @empty.find(fail_proc) {|e| true}.should == "yay" + end + + it "ignores the ifnone argument when nil" do + @numerous.find(nil) {|e| false }.should == nil + end + + it "passes through the values yielded by #each_with_index" do + [:a, :b].each_with_index.find { |x, i| ScratchPad << [x, i]; nil } + ScratchPad.recorded.should == [[:a, 0], [:b, 1]] + end + + it "returns an enumerator when no block given" do + @numerous.find.should.instance_of?(Enumerator) + end + + it "passes the ifnone proc to the enumerator" do + times = 0 + fail_proc = -> { times += 1; raise if times > 1; "cheeseburgers" } + @numerous.find(fail_proc).each {|e| false }.should == "cheeseburgers" + end + + it "gathers whole arrays as elements when each yields multiple" do + multi = EnumerableSpecs::YieldsMulti.new + multi.find {|e| e == [1, 2] }.should == [1, 2] + end + + it_behaves_like :enumerable_enumeratorized_with_unknown_size, :find end diff --git a/spec/ruby/core/enumerable/flat_map_spec.rb b/spec/ruby/core/enumerable/flat_map_spec.rb index bd07eab6c5..ef50cb2696 100644 --- a/spec/ruby/core/enumerable/flat_map_spec.rb +++ b/spec/ruby/core/enumerable/flat_map_spec.rb @@ -1,7 +1,56 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/collect_concat' +require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#flat_map" do - it_behaves_like :enumerable_collect_concat, :flat_map + it "yields elements to the block and flattens one level" do + numerous = EnumerableSpecs::Numerous.new(1, [2, 3], [4, [5, 6]], {foo: :bar}) + numerous.flat_map { |i| i }.should == [1, 2, 3, 4, [5, 6], {foo: :bar}] + end + + it "appends non-Array elements that do not define #to_ary" do + obj = mock("to_ary undefined") + + numerous = EnumerableSpecs::Numerous.new(1, obj, 2) + numerous.flat_map { |i| i }.should == [1, obj, 2] + end + + it "concatenates the result of calling #to_ary if it returns an Array" do + obj = mock("to_ary defined") + obj.should_receive(:to_ary).and_return([:a, :b]) + + numerous = EnumerableSpecs::Numerous.new(1, obj, 2) + numerous.flat_map { |i| i }.should == [1, :a, :b, 2] + end + + it "does not call #to_a" do + obj = mock("to_ary undefined") + obj.should_not_receive(:to_a) + + numerous = EnumerableSpecs::Numerous.new(1, obj, 2) + numerous.flat_map { |i| i }.should == [1, obj, 2] + end + + it "appends an element that defines #to_ary that returns nil" do + obj = mock("to_ary defined") + obj.should_receive(:to_ary).and_return(nil) + + numerous = EnumerableSpecs::Numerous.new(1, obj, 2) + numerous.flat_map { |i| i }.should == [1, obj, 2] + end + + it "raises a TypeError if an element defining #to_ary does not return an Array or nil" do + obj = mock("to_ary defined") + obj.should_receive(:to_ary).and_return("array") + + -> { [1, obj, 3].flat_map { |i| i } }.should.raise(TypeError) + end + + it "returns an enumerator when no block given" do + enum = EnumerableSpecs::Numerous.new(1, 2).flat_map + enum.should.instance_of?(Enumerator) + enum.each{ |i| [i] * i }.should == [1, 2, 2] + end + + it_behaves_like :enumerable_enumeratorized_with_origin_size, :flat_map end diff --git a/spec/ruby/core/enumerable/include_spec.rb b/spec/ruby/core/enumerable/include_spec.rb index dab1b04451..d59b351486 100644 --- a/spec/ruby/core/enumerable/include_spec.rb +++ b/spec/ruby/core/enumerable/include_spec.rb @@ -1,7 +1,36 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/include' describe "Enumerable#include?" do - it_behaves_like :enumerable_include, :include? + it "returns true if any element == argument for numbers" do + class EnumerableSpecIncludeP; def ==(obj) obj == 5; end; end + + elements = (0..5).to_a + EnumerableSpecs::Numerous.new(*elements).include?(5).should == true + EnumerableSpecs::Numerous.new(*elements).include?(10).should == false + EnumerableSpecs::Numerous.new(*elements).include?(EnumerableSpecIncludeP.new).should == true + end + + it "returns true if any element == argument for other objects" do + class EnumerableSpecIncludeP11; def ==(obj); obj == '11'; end; end + + elements = ('0'..'5').to_a + [EnumerableSpecIncludeP11.new] + EnumerableSpecs::Numerous.new(*elements).include?('5').should == true + EnumerableSpecs::Numerous.new(*elements).include?('10').should == false + EnumerableSpecs::Numerous.new(*elements).include?(EnumerableSpecIncludeP11.new).should == true + EnumerableSpecs::Numerous.new(*elements).include?('11').should == true + end + + + it "returns true if any member of enum equals obj when == compare different classes (legacy rubycon)" do + # equality is tested with == + EnumerableSpecs::Numerous.new(2,4,6,8,10).include?(2.0).should == true + EnumerableSpecs::Numerous.new(2,4,[6,8],10).include?([6, 8]).should == true + EnumerableSpecs::Numerous.new(2,4,[6,8],10).include?([6.0, 8.0]).should == true + end + + it "gathers whole arrays as elements when each yields multiple" do + multi = EnumerableSpecs::YieldsMulti.new + multi.include?([1,2]).should == true + end end diff --git a/spec/ruby/core/enumerable/inject_spec.rb b/spec/ruby/core/enumerable/inject_spec.rb index e1fe216144..10de321395 100644 --- a/spec/ruby/core/enumerable/inject_spec.rb +++ b/spec/ruby/core/enumerable/inject_spec.rb @@ -1,7 +1,144 @@ require_relative '../../spec_helper' +require_relative '../array/shared/iterable_and_tolerating_size_increasing' require_relative 'fixtures/classes' -require_relative 'shared/inject' describe "Enumerable#inject" do - it_behaves_like :enumerable_inject, :inject + 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 = [] + EnumerableSpecs::Numerous.new.inject(0) { |memo, i| a << [memo, i]; i } + a.should == [[0, 2], [2, 5], [5, 3], [3, 6], [6, 1], [1, 4]] + EnumerableSpecs::EachDefiner.new(true, true, true).inject(nil) {|result, i| i && result}.should == nil + end + + it "produces an array of the accumulator and the argument when given a block with a *arg" do + a = [] + [1,2].inject(0) {|*args| a << args; args[0] + args[1]} + a.should == [[0, 1], [1, 2]] + end + + it "can take two argument" do + EnumerableSpecs::Numerous.new(1, 2, 3).inject(10, :-).should == 4 + EnumerableSpecs::Numerous.new(1, 2, 3).inject(10, "-").should == 4 + + [1, 2, 3].inject(10, :-).should == 4 + [1, 2, 3].inject(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).inject(10, name).should == 4 + [1, 2, 3].inject(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).inject(10, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) + -> { [1, 2, 3].inject(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).inject(10, :-) { raise "we never get here"}.should == 4 + }.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true) + + -> { + [1, 2, 3].inject(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].inject(0, :+) + [1, 2].inject(:+) + EnumerableSpecs::Numerous.new(1, 2).inject(0, :+) + EnumerableSpecs::Numerous.new(1, 2).inject(:+) + }.should_not complain(verbose: true) + end + + it "can take a symbol argument" do + EnumerableSpecs::Numerous.new(10, 1, 2, 3).inject(:-).should == 4 + [10, 1, 2, 3].inject(:-).should == 4 + end + + it "can take a String argument" do + EnumerableSpecs::Numerous.new(10, 1, 2, 3).inject("-").should == 4 + [10, 1, 2, 3].inject("-").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).inject(name).should == 4 + [10, 1, 2, 3].inject(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).inject(Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) + -> { [10, 1, 2, 3].inject(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 + a = [] + EnumerableSpecs::Numerous.new.inject { |memo, i| a << [memo, i]; i } + 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.inject([]) {|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 + EnumerableSpecs::EachDefiner.new().inject(1) {|acc,x| 999 }.should == 1 + EnumerableSpecs::EachDefiner.new(2).inject(1) {|acc,x| 999 }.should == 999 + EnumerableSpecs::EachDefiner.new(2).inject(1) {|acc,x| acc }.should == 1 + EnumerableSpecs::EachDefiner.new(2).inject(1) {|acc,x| x }.should == 2 + + EnumerableSpecs::EachDefiner.new(1,2,3,4).inject(100) {|acc,x| acc + x }.should == 110 + EnumerableSpecs::EachDefiner.new(1,2,3,4).inject(100) {|acc,x| acc * x }.should == 2400 + + EnumerableSpecs::EachDefiner.new('a','b','c').inject("z") {|result, i| i+result}.should == "cbaz" + end + + it "without inject arguments(legacy rubycon)" do + # no inject argument + EnumerableSpecs::EachDefiner.new(2).inject {|acc,x| 999 }.should == 2 + EnumerableSpecs::EachDefiner.new(2).inject {|acc,x| acc }.should == 2 + EnumerableSpecs::EachDefiner.new(2).inject {|acc,x| x }.should == 2 + + EnumerableSpecs::EachDefiner.new(1,2,3,4).inject {|acc,x| acc + x }.should == 10 + EnumerableSpecs::EachDefiner.new(1,2,3,4).inject {|acc,x| acc * x }.should == 24 + + EnumerableSpecs::EachDefiner.new('a','b','c').inject {|result, i| i+result}.should == "cba" + EnumerableSpecs::EachDefiner.new(3, 4, 5).inject {|result, i| result*i}.should == 60 + EnumerableSpecs::EachDefiner.new([1], 2, 'a','b').inject {|r,i| r<<i}.should == [1, 2, 'a', 'b'] + end + + it "returns nil when fails(legacy rubycon)" do + EnumerableSpecs::EachDefiner.new().inject {|acc,x| 999 }.should == nil + end + + it "tolerates increasing a collection size during iterating Array" do + array = [:a, :b, :c] + ScratchPad.record [] + i = 0 + + array.inject(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].inject }.should.raise(ArgumentError) + -> { {one: 1, two: 2}.inject }.should.raise(ArgumentError) + end end diff --git a/spec/ruby/core/enumerable/map_spec.rb b/spec/ruby/core/enumerable/map_spec.rb index 98a70781cf..e6447f5c23 100644 --- a/spec/ruby/core/enumerable/map_spec.rb +++ b/spec/ruby/core/enumerable/map_spec.rb @@ -1,7 +1,109 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/collect' +require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#map" do - it_behaves_like :enumerable_collect, :map + before :each do + ScratchPad.record [] + end + + it "returns a new array with the results of passing each element to block" do + entries = [0, 1, 3, 4, 5, 6] + numerous = EnumerableSpecs::Numerous.new(*entries) + numerous.map { |i| i % 2 }.should == [0, 1, 1, 0, 1, 0] + numerous.map { |i| i }.should == entries + end + + it "passes through the values yielded by #each_with_index" do + [:a, :b].each_with_index.map { |x, i| ScratchPad << [x, i]; nil } + ScratchPad.recorded.should == [[:a, 0], [:b, 1]] + end + + it "gathers initial args as elements when each yields multiple" do + multi = EnumerableSpecs::YieldsMulti.new + multi.map {|e| e}.should == [1,3,6] + end + + it "only yields increasing values for a Range" do + (1..0).map { |x| x }.should == [] + (1..1).map { |x| x }.should == [1] + (1..2).map { |x| x }.should == [1, 2] + end + + it "returns an enumerator when no block given" do + enum = EnumerableSpecs::Numerous.new.map + enum.should.instance_of?(Enumerator) + enum.each { |i| -i }.should == [-2, -5, -3, -6, -1, -4] + end + + it "reports the same arity as the given block" do + entries = [0, 1, 3, 4, 5, 6] + numerous = EnumerableSpecs::Numerous.new(*entries) + + def numerous.each(&block) + ScratchPad << block.arity + super + end + + numerous.map { |a, b| a % 2 }.should == [0, 1, 1, 0, 1, 0] + ScratchPad.recorded.should == [2] + ScratchPad.clear + ScratchPad.record [] + numerous.map { |i| i }.should == entries + ScratchPad.recorded.should == [1] + end + + it "yields an Array of 2 elements for a Hash when block arity is 1" do + c = Class.new do + def register(a) + ScratchPad << a + end + end + m = c.new.method(:register) + + ScratchPad.record [] + { 1 => 'a', 2 => 'b' }.map(&m) + ScratchPad.recorded.should == [[1, 'a'], [2, 'b']] + end + + it "yields 2 arguments for a Hash when block arity is 2" do + c = Class.new do + def register(a, b) + ScratchPad << [a, b] + end + end + m = c.new.method(:register) + + ScratchPad.record [] + { 1 => 'a', 2 => 'b' }.map(&m) + ScratchPad.recorded.should == [[1, 'a'], [2, 'b']] + end + + it "raises an error for a Hash when an arity enforcing block of arity >2 is passed in" do + c = Class.new do + def register(a, b, c) + end + end + m = c.new.method(:register) + + -> do + { 1 => 'a', 2 => 'b' }.map(&m) + end.should.raise(ArgumentError) + end + + it "calls the each method on sub-classes" do + c = Class.new(Hash) do + def each + ScratchPad << 'in each' + super + end + end + h = c.new + h[1] = 'a' + ScratchPad.record [] + h.map { |k,v| v } + ScratchPad.recorded.should == ['in each'] + end + + it_behaves_like :enumerable_enumeratorized_with_origin_size, :map end diff --git a/spec/ruby/core/enumerable/member_spec.rb b/spec/ruby/core/enumerable/member_spec.rb index 1fe3cebd28..be06880ebb 100644 --- a/spec/ruby/core/enumerable/member_spec.rb +++ b/spec/ruby/core/enumerable/member_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/include' describe "Enumerable#member?" do - it_behaves_like :enumerable_include, :member? + it "is an alias of Enumerable#include?" do + Enumerable.instance_method(:member?).should == Enumerable.instance_method(:include?) + end end diff --git a/spec/ruby/core/enumerable/reduce_spec.rb b/spec/ruby/core/enumerable/reduce_spec.rb index bc8691c1b0..40452b66a1 100644 --- a/spec/ruby/core/enumerable/reduce_spec.rb +++ b/spec/ruby/core/enumerable/reduce_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/inject' describe "Enumerable#reduce" do - it_behaves_like :enumerable_inject, :reduce + it "is an alias of Enumerable#inject" do + Enumerable.instance_method(:reduce).should == Enumerable.instance_method(:inject) + end end diff --git a/spec/ruby/core/enumerable/select_spec.rb b/spec/ruby/core/enumerable/select_spec.rb index 7fc61926f9..a53c228a44 100644 --- a/spec/ruby/core/enumerable/select_spec.rb +++ b/spec/ruby/core/enumerable/select_spec.rb @@ -1,7 +1,33 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/find_all' +require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#select" do - it_behaves_like :enumerable_find_all, :select + before :each do + ScratchPad.record [] + @elements = (1..10).to_a + @numerous = EnumerableSpecs::Numerous.new(*@elements) + end + + it "returns all elements for which the block is not false" do + @numerous.select {|i| i % 3 == 0 }.should == [3, 6, 9] + @numerous.select {|i| true }.should == @elements + @numerous.select {|i| false }.should == [] + end + + it "returns an enumerator when no block given" do + @numerous.select.should.instance_of?(Enumerator) + end + + it "passes through the values yielded by #each_with_index" do + [:a, :b].each_with_index.select { |x, i| ScratchPad << [x, i] } + ScratchPad.recorded.should == [[:a, 0], [:b, 1]] + end + + it "gathers whole arrays as elements when each yields multiple" do + multi = EnumerableSpecs::YieldsMulti.new + multi.select {|e| e == [3, 4, 5] }.should == [[3, 4, 5]] + end + + it_behaves_like :enumerable_enumeratorized_with_origin_size, :select end diff --git a/spec/ruby/core/enumerable/shared/collect.rb b/spec/ruby/core/enumerable/shared/collect.rb deleted file mode 100644 index 4696d32454..0000000000 --- a/spec/ruby/core/enumerable/shared/collect.rb +++ /dev/null @@ -1,107 +0,0 @@ -require_relative 'enumerable_enumeratorized' - -describe :enumerable_collect, shared: true do - before :each do - ScratchPad.record [] - end - - it "returns a new array with the results of passing each element to block" do - entries = [0, 1, 3, 4, 5, 6] - numerous = EnumerableSpecs::Numerous.new(*entries) - numerous.send(@method) { |i| i % 2 }.should == [0, 1, 1, 0, 1, 0] - numerous.send(@method) { |i| i }.should == entries - end - - it "passes through the values yielded by #each_with_index" do - [:a, :b].each_with_index.send(@method) { |x, i| ScratchPad << [x, i]; nil } - ScratchPad.recorded.should == [[:a, 0], [:b, 1]] - end - - it "gathers initial args as elements when each yields multiple" do - multi = EnumerableSpecs::YieldsMulti.new - multi.send(@method) {|e| e}.should == [1,3,6] - end - - it "only yields increasing values for a Range" do - (1..0).send(@method) { |x| x }.should == [] - (1..1).send(@method) { |x| x }.should == [1] - (1..2).send(@method) { |x| x }.should == [1, 2] - end - - it "returns an enumerator when no block given" do - enum = EnumerableSpecs::Numerous.new.send(@method) - enum.should.instance_of?(Enumerator) - enum.each { |i| -i }.should == [-2, -5, -3, -6, -1, -4] - end - - it "reports the same arity as the given block" do - entries = [0, 1, 3, 4, 5, 6] - numerous = EnumerableSpecs::Numerous.new(*entries) - - def numerous.each(&block) - ScratchPad << block.arity - super - end - - numerous.send(@method) { |a, b| a % 2 }.should == [0, 1, 1, 0, 1, 0] - ScratchPad.recorded.should == [2] - ScratchPad.clear - ScratchPad.record [] - numerous.send(@method) { |i| i }.should == entries - ScratchPad.recorded.should == [1] - end - - it "yields an Array of 2 elements for a Hash when block arity is 1" do - c = Class.new do - def register(a) - ScratchPad << a - end - end - m = c.new.method(:register) - - ScratchPad.record [] - { 1 => 'a', 2 => 'b' }.map(&m) - ScratchPad.recorded.should == [[1, 'a'], [2, 'b']] - end - - it "yields 2 arguments for a Hash when block arity is 2" do - c = Class.new do - def register(a, b) - ScratchPad << [a, b] - end - end - m = c.new.method(:register) - - ScratchPad.record [] - { 1 => 'a', 2 => 'b' }.map(&m) - ScratchPad.recorded.should == [[1, 'a'], [2, 'b']] - end - - it "raises an error for a Hash when an arity enforcing block of arity >2 is passed in" do - c = Class.new do - def register(a, b, c) - end - end - m = c.new.method(:register) - - -> do - { 1 => 'a', 2 => 'b' }.map(&m) - end.should.raise(ArgumentError) - end - - it "calls the each method on sub-classes" do - c = Class.new(Hash) do - def each - ScratchPad << 'in each' - super - end - end - h = c.new - h[1] = 'a' - ScratchPad.record [] - h.send(@method) { |k,v| v } - ScratchPad.recorded.should == ['in each'] - end - - it_should_behave_like :enumerable_enumeratorized_with_origin_size -end diff --git a/spec/ruby/core/enumerable/shared/collect_concat.rb b/spec/ruby/core/enumerable/shared/collect_concat.rb deleted file mode 100644 index 1694e3fdce..0000000000 --- a/spec/ruby/core/enumerable/shared/collect_concat.rb +++ /dev/null @@ -1,54 +0,0 @@ -require_relative 'enumerable_enumeratorized' - -describe :enumerable_collect_concat, shared: true do - it "yields elements to the block and flattens one level" do - numerous = EnumerableSpecs::Numerous.new(1, [2, 3], [4, [5, 6]], {foo: :bar}) - numerous.send(@method) { |i| i }.should == [1, 2, 3, 4, [5, 6], {foo: :bar}] - end - - it "appends non-Array elements that do not define #to_ary" do - obj = mock("to_ary undefined") - - numerous = EnumerableSpecs::Numerous.new(1, obj, 2) - numerous.send(@method) { |i| i }.should == [1, obj, 2] - end - - it "concatenates the result of calling #to_ary if it returns an Array" do - obj = mock("to_ary defined") - obj.should_receive(:to_ary).and_return([:a, :b]) - - numerous = EnumerableSpecs::Numerous.new(1, obj, 2) - numerous.send(@method) { |i| i }.should == [1, :a, :b, 2] - end - - it "does not call #to_a" do - obj = mock("to_ary undefined") - obj.should_not_receive(:to_a) - - numerous = EnumerableSpecs::Numerous.new(1, obj, 2) - numerous.send(@method) { |i| i }.should == [1, obj, 2] - end - - it "appends an element that defines #to_ary that returns nil" do - obj = mock("to_ary defined") - obj.should_receive(:to_ary).and_return(nil) - - numerous = EnumerableSpecs::Numerous.new(1, obj, 2) - numerous.send(@method) { |i| i }.should == [1, obj, 2] - end - - it "raises a TypeError if an element defining #to_ary does not return an Array or nil" do - obj = mock("to_ary defined") - obj.should_receive(:to_ary).and_return("array") - - -> { [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.instance_of?(Enumerator) - enum.each{ |i| [i] * i }.should == [1, 2, 2] - end - - it_should_behave_like :enumerable_enumeratorized_with_origin_size -end diff --git a/spec/ruby/core/enumerable/shared/entries.rb b/spec/ruby/core/enumerable/shared/entries.rb deleted file mode 100644 index e32eb23d2a..0000000000 --- a/spec/ruby/core/enumerable/shared/entries.rb +++ /dev/null @@ -1,16 +0,0 @@ -describe :enumerable_entries, shared: true do - it "returns an array containing the elements" do - numerous = EnumerableSpecs::Numerous.new(1, nil, 'a', 2, false, true) - numerous.send(@method).should == [1, nil, "a", 2, false, true] - end - - it "passes through the values yielded by #each_with_index" do - [:a, :b].each_with_index.send(@method).should == [[:a, 0], [:b, 1]] - end - - it "passes arguments to each" do - count = EnumerableSpecs::EachCounter.new(1, 2, 3) - count.send(@method, :hello, "world").should == [1, 2, 3] - count.arguments_passed.should == [:hello, "world"] - end -end diff --git a/spec/ruby/core/enumerable/shared/find.rb b/spec/ruby/core/enumerable/shared/find.rb deleted file mode 100644 index cdff640415..0000000000 --- a/spec/ruby/core/enumerable/shared/find.rb +++ /dev/null @@ -1,77 +0,0 @@ -require_relative 'enumerable_enumeratorized' - -describe :enumerable_find, shared: true do - # #detect and #find are aliases, so we only need one function - before :each do - ScratchPad.record [] - @elements = [2, 4, 6, 8, 10] - @numerous = EnumerableSpecs::Numerous.new(*@elements) - @empty = [] - end - - it "passes each entry in enum to block while block when block is false" do - visited_elements = [] - @numerous.send(@method) do |element| - visited_elements << element - false - end - visited_elements.should == @elements - end - - it "returns nil when the block is false and there is no ifnone proc given" do - @numerous.send(@method) {|e| false }.should == nil - end - - it "returns the first element for which the block is not false" do - @elements.each do |element| - @numerous.send(@method) {|e| e > element - 1 }.should == element - end - end - - it "returns the value of the ifnone proc if the block is false" do - fail_proc = -> { "cheeseburgers" } - @numerous.send(@method, fail_proc) {|e| false }.should == "cheeseburgers" - end - - it "doesn't call the ifnone proc if an element is found" do - fail_proc = -> { raise "This shouldn't have been called" } - @numerous.send(@method, fail_proc) {|e| e == @elements.first }.should == 2 - end - - it "calls the ifnone proc only once when the block is false" do - times = 0 - fail_proc = -> { times += 1; raise if times > 1; "cheeseburgers" } - @numerous.send(@method, fail_proc) {|e| false }.should == "cheeseburgers" - end - - it "calls the ifnone proc when there are no elements" do - fail_proc = -> { "yay" } - @empty.send(@method, fail_proc) {|e| true}.should == "yay" - end - - it "ignores the ifnone argument when nil" do - @numerous.send(@method, nil) {|e| false }.should == nil - end - - it "passes through the values yielded by #each_with_index" do - [:a, :b].each_with_index.send(@method) { |x, i| ScratchPad << [x, i]; nil } - ScratchPad.recorded.should == [[:a, 0], [:b, 1]] - end - - it "returns an enumerator when no block given" do - @numerous.send(@method).should.instance_of?(Enumerator) - end - - it "passes the ifnone proc to the enumerator" do - times = 0 - fail_proc = -> { times += 1; raise if times > 1; "cheeseburgers" } - @numerous.send(@method, fail_proc).each {|e| false }.should == "cheeseburgers" - end - - it "gathers whole arrays as elements when each yields multiple" do - multi = EnumerableSpecs::YieldsMulti.new - multi.send(@method) {|e| e == [1, 2] }.should == [1, 2] - end - - it_should_behave_like :enumerable_enumeratorized_with_unknown_size -end diff --git a/spec/ruby/core/enumerable/shared/find_all.rb b/spec/ruby/core/enumerable/shared/find_all.rb deleted file mode 100644 index 27f01de6e0..0000000000 --- a/spec/ruby/core/enumerable/shared/find_all.rb +++ /dev/null @@ -1,31 +0,0 @@ -require_relative 'enumerable_enumeratorized' - -describe :enumerable_find_all, shared: true do - before :each do - ScratchPad.record [] - @elements = (1..10).to_a - @numerous = EnumerableSpecs::Numerous.new(*@elements) - end - - it "returns all elements for which the block is not false" do - @numerous.send(@method) {|i| i % 3 == 0 }.should == [3, 6, 9] - @numerous.send(@method) {|i| true }.should == @elements - @numerous.send(@method) {|i| false }.should == [] - end - - it "returns an enumerator when no block given" do - @numerous.send(@method).should.instance_of?(Enumerator) - end - - it "passes through the values yielded by #each_with_index" do - [:a, :b].each_with_index.send(@method) { |x, i| ScratchPad << [x, i] } - ScratchPad.recorded.should == [[:a, 0], [:b, 1]] - end - - it "gathers whole arrays as elements when each yields multiple" do - multi = EnumerableSpecs::YieldsMulti.new - multi.send(@method) {|e| e == [3, 4, 5] }.should == [[3, 4, 5]] - end - - it_should_behave_like :enumerable_enumeratorized_with_origin_size -end diff --git a/spec/ruby/core/enumerable/shared/include.rb b/spec/ruby/core/enumerable/shared/include.rb deleted file mode 100644 index ea250f032b..0000000000 --- a/spec/ruby/core/enumerable/shared/include.rb +++ /dev/null @@ -1,34 +0,0 @@ -describe :enumerable_include, shared: true do - it "returns true if any element == argument for numbers" do - class EnumerableSpecIncludeP; def ==(obj) obj == 5; end; end - - elements = (0..5).to_a - EnumerableSpecs::Numerous.new(*elements).send(@method,5).should == true - EnumerableSpecs::Numerous.new(*elements).send(@method,10).should == false - EnumerableSpecs::Numerous.new(*elements).send(@method,EnumerableSpecIncludeP.new).should == true - end - - it "returns true if any element == argument for other objects" do - class EnumerableSpecIncludeP11; def ==(obj); obj == '11'; end; end - - elements = ('0'..'5').to_a + [EnumerableSpecIncludeP11.new] - EnumerableSpecs::Numerous.new(*elements).send(@method,'5').should == true - EnumerableSpecs::Numerous.new(*elements).send(@method,'10').should == false - EnumerableSpecs::Numerous.new(*elements).send(@method,EnumerableSpecIncludeP11.new).should == true - EnumerableSpecs::Numerous.new(*elements).send(@method,'11').should == true - end - - - it "returns true if any member of enum equals obj when == compare different classes (legacy rubycon)" do - # equality is tested with == - EnumerableSpecs::Numerous.new(2,4,6,8,10).send(@method, 2.0).should == true - EnumerableSpecs::Numerous.new(2,4,[6,8],10).send(@method, [6, 8]).should == true - EnumerableSpecs::Numerous.new(2,4,[6,8],10).send(@method, [6.0, 8.0]).should == true - end - - it "gathers whole arrays as elements when each yields multiple" do - multi = EnumerableSpecs::YieldsMulti.new - 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 deleted file mode 100644 index 7da4f0ca99..0000000000 --- a/spec/ruby/core/enumerable/shared/inject.rb +++ /dev/null @@ -1,142 +0,0 @@ -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 = [] - EnumerableSpecs::Numerous.new.send(@method, 0) { |memo, i| a << [memo, i]; i } - a.should == [[0, 2], [2, 5], [5, 3], [3, 6], [6, 1], [1, 4]] - EnumerableSpecs::EachDefiner.new(true, true, true).send(@method, nil) {|result, i| i && result}.should == nil - end - - it "produces an array of the accumulator and the argument when given a block with a *arg" do - a = [] - [1,2].send(@method, 0) {|*args| a << args; args[0] + args[1]} - a.should == [[0, 1], [1, 2]] - end - - 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 - }.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 - a = [] - EnumerableSpecs::Numerous.new.send(@method) { |memo, i| a << [memo, i]; i } - 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 "with inject arguments(legacy rubycon)" do - # with inject argument - EnumerableSpecs::EachDefiner.new().send(@method, 1) {|acc,x| 999 }.should == 1 - EnumerableSpecs::EachDefiner.new(2).send(@method, 1) {|acc,x| 999 }.should == 999 - EnumerableSpecs::EachDefiner.new(2).send(@method, 1) {|acc,x| acc }.should == 1 - EnumerableSpecs::EachDefiner.new(2).send(@method, 1) {|acc,x| x }.should == 2 - - EnumerableSpecs::EachDefiner.new(1,2,3,4).send(@method, 100) {|acc,x| acc + x }.should == 110 - EnumerableSpecs::EachDefiner.new(1,2,3,4).send(@method, 100) {|acc,x| acc * x }.should == 2400 - - EnumerableSpecs::EachDefiner.new('a','b','c').send(@method, "z") {|result, i| i+result}.should == "cbaz" - end - - 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| acc }.should == 2 - EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| x }.should == 2 - - EnumerableSpecs::EachDefiner.new(1,2,3,4).send(@method) {|acc,x| acc + x }.should == 10 - EnumerableSpecs::EachDefiner.new(1,2,3,4).send(@method) {|acc,x| acc * x }.should == 24 - - 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 - - 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/to_a_spec.rb b/spec/ruby/core/enumerable/to_a_spec.rb index 723f922574..f1796070f0 100644 --- a/spec/ruby/core/enumerable/to_a_spec.rb +++ b/spec/ruby/core/enumerable/to_a_spec.rb @@ -1,7 +1,19 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/entries' describe "Enumerable#to_a" do - it_behaves_like :enumerable_entries, :to_a + it "returns an array containing the elements" do + numerous = EnumerableSpecs::Numerous.new(1, nil, 'a', 2, false, true) + numerous.to_a.should == [1, nil, "a", 2, false, true] + end + + it "passes through the values yielded by #each_with_index" do + [:a, :b].each_with_index.to_a.should == [[:a, 0], [:b, 1]] + end + + it "passes arguments to each" do + count = EnumerableSpecs::EachCounter.new(1, 2, 3) + count.to_a(:hello, "world").should == [1, 2, 3] + count.arguments_passed.should == [:hello, "world"] + end end |
