diff options
Diffstat (limited to 'spec/ruby/core/hash/shared/each.rb')
| -rw-r--r-- | spec/ruby/core/hash/shared/each.rb | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/spec/ruby/core/hash/shared/each.rb b/spec/ruby/core/hash/shared/each.rb index 04a26b5c45..657c5d2c52 100644 --- a/spec/ruby/core/hash/shared/each.rb +++ b/spec/ruby/core/hash/shared/each.rb @@ -10,7 +10,7 @@ describe :hash_each, shared: true do it "yields the key and value of each pair to a block expecting |key, value|" do r = {} h = { a: 1, b: 2, c: 3, d: 5 } - h.send(@method) { |k,v| r[k.to_s] = v.to_s }.should equal(h) + h.send(@method) { |k,v| r[k.to_s] = v.to_s }.should.equal?(h) r.should == { "a" => "1", "b" => "2", "c" => "3", "d" => "5" } end @@ -21,37 +21,39 @@ describe :hash_each, shared: true do ary.sort.should == ["a", "b", "c"] end - ruby_version_is ""..."3.0" do - it "yields 2 values and not an Array of 2 elements when given a callable of arity 2" do - obj = Object.new - def obj.foo(key, value) - ScratchPad << key << value - end + it "always yields an Array of 2 elements, even when given a callable of arity 2" do + obj = Object.new + def obj.foo(key, value) + end - ScratchPad.record([]) + -> { { "a" => 1 }.send(@method, &obj.method(:foo)) - ScratchPad.recorded.should == ["a", 1] + }.should.raise(ArgumentError) - ScratchPad.record([]) - { "a" => 1 }.send(@method, &-> key, value { ScratchPad << key << value }) - ScratchPad.recorded.should == ["a", 1] - end + -> { + { "a" => 1 }.send(@method, &-> key, value { }) + }.should.raise(ArgumentError) end - ruby_version_is "3.0" do - it "always yields an Array of 2 elements, even when given a callable of arity 2" do - obj = Object.new - def obj.foo(key, value) - end + it "yields an Array of 2 elements when given a callable of arity 1" do + obj = Object.new + def obj.foo(key_value) + ScratchPad << key_value + end - -> { - { "a" => 1 }.send(@method, &obj.method(:foo)) - }.should raise_error(ArgumentError) + ScratchPad.record([]) + { "a" => 1 }.send(@method, &obj.method(:foo)) + ScratchPad.recorded.should == [["a", 1]] + end - -> { - { "a" => 1 }.send(@method, &-> key, value { }) - }.should raise_error(ArgumentError) + it "raises an error for a Hash when an arity enforcing callable of arity >2 is passed in" do + obj = Object.new + def obj.foo(key, value, extra) end + + -> { + { "a" => 1 }.send(@method, &obj.method(:foo)) + }.should.raise(ArgumentError) end it "uses the same order as keys() and values()" do |
