|
1 | 1 | require_relative '../../spec_helper' |
2 | | -require_relative 'fixtures/classes' |
3 | 2 | require_relative 'shared/iteration' |
4 | | -require_relative 'shared/each' |
5 | 3 | require_relative '../enumerable/shared/enumeratorized' |
6 | 4 |
|
7 | 5 | describe "Hash#each_pair" do |
8 | | - it_behaves_like :hash_each, :each_pair |
9 | 6 | it_behaves_like :hash_iteration_no_block, :each_pair |
10 | 7 | it_behaves_like :enumeratorized_with_origin_size, :each_pair, { 1 => 2, 3 => 4, 5 => 6 } |
| 8 | + |
| 9 | + # This is inconsistent with below, MRI checks the block arity in rb_hash_each_pair() |
| 10 | + it "yields a [[key, value]] Array for each pair to a block expecting |*args|" do |
| 11 | + all_args = [] |
| 12 | + { 1 => 2, 3 => 4 }.each_pair { |*args| all_args << args } |
| 13 | + all_args.sort.should == [[[1, 2]], [[3, 4]]] |
| 14 | + end |
| 15 | + |
| 16 | + it "yields the key and value of each pair to a block expecting |key, value|" do |
| 17 | + r = {} |
| 18 | + h = { a: 1, b: 2, c: 3, d: 5 } |
| 19 | + h.each_pair { |k,v| r[k.to_s] = v.to_s }.should.equal?(h) |
| 20 | + r.should == { "a" => "1", "b" => "2", "c" => "3", "d" => "5" } |
| 21 | + end |
| 22 | + |
| 23 | + it "yields the key only to a block expecting |key,|" do |
| 24 | + ary = [] |
| 25 | + h = { "a" => 1, "b" => 2, "c" => 3 } |
| 26 | + h.each_pair { |k,| ary << k } |
| 27 | + ary.sort.should == ["a", "b", "c"] |
| 28 | + end |
| 29 | + |
| 30 | + it "always yields an Array of 2 elements, even when given a callable of arity 2" do |
| 31 | + obj = Object.new |
| 32 | + def obj.foo(key, value) |
| 33 | + end |
| 34 | + |
| 35 | + -> { |
| 36 | + { "a" => 1 }.each_pair(&obj.method(:foo)) |
| 37 | + }.should.raise(ArgumentError) |
| 38 | + |
| 39 | + -> { |
| 40 | + { "a" => 1 }.each_pair(&-> key, value { }) |
| 41 | + }.should.raise(ArgumentError) |
| 42 | + end |
| 43 | + |
| 44 | + it "yields an Array of 2 elements when given a callable of arity 1" do |
| 45 | + obj = Object.new |
| 46 | + def obj.foo(key_value) |
| 47 | + ScratchPad << key_value |
| 48 | + end |
| 49 | + |
| 50 | + ScratchPad.record([]) |
| 51 | + { "a" => 1 }.each_pair(&obj.method(:foo)) |
| 52 | + ScratchPad.recorded.should == [["a", 1]] |
| 53 | + end |
| 54 | + |
| 55 | + it "raises an error for a Hash when an arity enforcing callable of arity >2 is passed in" do |
| 56 | + obj = Object.new |
| 57 | + def obj.foo(key, value, extra) |
| 58 | + end |
| 59 | + |
| 60 | + -> { |
| 61 | + { "a" => 1 }.each_pair(&obj.method(:foo)) |
| 62 | + }.should.raise(ArgumentError) |
| 63 | + end |
| 64 | + |
| 65 | + it "uses the same order as keys() and values()" do |
| 66 | + h = { a: 1, b: 2, c: 3, d: 5 } |
| 67 | + keys = [] |
| 68 | + values = [] |
| 69 | + |
| 70 | + h.each_pair do |k, v| |
| 71 | + keys << k |
| 72 | + values << v |
| 73 | + end |
| 74 | + |
| 75 | + keys.should == h.keys |
| 76 | + values.should == h.values |
| 77 | + end |
| 78 | + |
| 79 | + # Confirming the argument-splatting works from child class for both k, v and [k, v] |
| 80 | + it "properly expands (or not) child class's 'each'-yielded args" do |
| 81 | + cls1 = Class.new(Hash) do |
| 82 | + attr_accessor :k_v |
| 83 | + def each |
| 84 | + super do |k, v| |
| 85 | + @k_v = [k, v] |
| 86 | + yield k, v |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + cls2 = Class.new(Hash) do |
| 92 | + attr_accessor :k_v |
| 93 | + def each |
| 94 | + super do |k, v| |
| 95 | + @k_v = [k, v] |
| 96 | + yield([k, v]) |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + obj1 = cls1.new |
| 102 | + obj1['a'] = 'b' |
| 103 | + obj1.map {|k, v| [k, v]}.should == [['a', 'b']] |
| 104 | + obj1.k_v.should == ['a', 'b'] |
| 105 | + |
| 106 | + obj2 = cls2.new |
| 107 | + obj2['a'] = 'b' |
| 108 | + obj2.map {|k, v| [k, v]}.should == [['a', 'b']] |
| 109 | + obj2.k_v.should == ['a', 'b'] |
| 110 | + end |
11 | 111 | end |
0 commit comments