diff options
Diffstat (limited to 'spec/ruby/core/hash/shared/each.rb')
| -rw-r--r-- | spec/ruby/core/hash/shared/each.rb | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/spec/ruby/core/hash/shared/each.rb b/spec/ruby/core/hash/shared/each.rb index d1f2e5f672..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,19 +21,39 @@ describe :hash_each, shared: true do ary.sort.should == ["a", "b", "c"] end - it "yields 2 values and not an Array of 2 elements when given a callable of arity 2" 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) - ScratchPad << key << value + end + + -> { + { "a" => 1 }.send(@method, &obj.method(:foo)) + }.should.raise(ArgumentError) + + -> { + { "a" => 1 }.send(@method, &-> key, value { }) + }.should.raise(ArgumentError) + 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 ScratchPad.record([]) { "a" => 1 }.send(@method, &obj.method(:foo)) - ScratchPad.recorded.should == ["a", 1] + ScratchPad.recorded.should == [["a", 1]] + end - ScratchPad.record([]) - { "a" => 1 }.send(@method, &-> key, value { ScratchPad << key << value }) - ScratchPad.recorded.should == ["a", 1] + 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 |
