diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-09-20 15:06:56 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-09-20 15:06:56 +0000 |
commit | abe75149d14d3286d3051c9e961ab6473a243a19 (patch) | |
tree | f33ad9fcd4569f51d7f0b85fefb751597211438d /test/ruby | |
parent | e76eebd79be9fa3cb59d0ea6e4ce8214cc6e56ad (diff) | |
download | ruby-abe75149d14d3286d3051c9e961ab6473a243a19.tar.gz |
Enumerable#to_h with block and so on
[Feature #15143]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64794 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_array.rb | 39 | ||||
-rw-r--r-- | test/ruby/test_enum.rb | 30 | ||||
-rw-r--r-- | test/ruby/test_env.rb | 2 | ||||
-rw-r--r-- | test/ruby/test_hash.rb | 10 | ||||
-rw-r--r-- | test/ruby/test_struct.rb | 7 |
5 files changed, 78 insertions, 10 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index e1a77b73b1..1440d00577 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1585,15 +1585,17 @@ class TestArray < Test::Unit::TestCase $, = nil end + StubToH = [ + [:key, :value], + Object.new.tap do |kvp| + def kvp.to_ary + [:obtained, :via_to_ary] + end + end, + ] + def test_to_h - kvp = Object.new - def kvp.to_ary - [:obtained, :via_to_ary] - end - array = [ - [:key, :value], - kvp, - ] + array = StubToH assert_equal({key: :value, obtained: :via_to_ary}, array.to_h) e = assert_raise(TypeError) { @@ -1608,6 +1610,27 @@ class TestArray < Test::Unit::TestCase assert_equal "wrong array length at 2 (expected 2, was 1)", e.message end + def test_to_h_block + array = StubToH + assert_equal({"key" => "value", "obtained" => "via_to_ary"}, + array.to_h {|k, v| [k.to_s, v.to_s]}) + + assert_equal({first_one: :ok, not_ok: :ng}, + [[:first_one, :ok], :not_ok].to_h {|k, v| [k, v || :ng]}) + + e = assert_raise(TypeError) { + [[:first_one, :ok], :not_ok].to_h {|k, v| v ? [k, v] : k} + } + assert_equal "wrong element type Symbol at 1 (expected array)", e.message + array = [1] + k = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) {array.to_h {k}} + e = assert_raise(ArgumentError) { + [[:first_one, :ok], [1, 2], [:not_ok]].to_h {|kv| kv} + } + assert_equal "wrong array length at 2 (expected 2, was 1)", e.message + end + def test_min assert_equal(1, [1, 2, 3, 1, 2].min) assert_equal(3, [1, 2, 3, 1, 2].min {|a,b| b <=> a }) diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index 5c5d359b82..a4eace2d57 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -144,8 +144,7 @@ class TestEnumerable < Test::Unit::TestCase assert_equal([], inf.to_a) end - def test_to_h - obj = Object.new + StubToH = Object.new.tap do |obj| def obj.each(*args) yield(*args) yield [:key, :value] @@ -157,6 +156,12 @@ class TestEnumerable < Test::Unit::TestCase yield kvp end obj.extend Enumerable + obj.freeze + end + + def test_to_h + obj = StubToH + assert_equal({ :hello => :world, :key => :value, @@ -175,6 +180,27 @@ class TestEnumerable < Test::Unit::TestCase assert_equal "element has wrong array length (expected 2, was 1)", e.message end + def test_to_h_block + obj = StubToH + + assert_equal({ + "hello" => "world", + "key" => "value", + "other_key" => "other_value", + "obtained" => "via_to_ary", + }, obj.to_h(:hello, :world) {|k, v| [k.to_s, v.to_s]}) + + e = assert_raise(TypeError) { + obj.to_h {:not_an_array} + } + assert_equal "wrong element type Symbol (expected array)", e.message + + e = assert_raise(ArgumentError) { + obj.to_h {[1]} + } + assert_equal "element has wrong array length (expected 2, was 1)", e.message + end + def test_inject assert_equal(12, @obj.inject {|z, x| z * x }) assert_equal(48, @obj.inject {|z, x| z * 2 + x }) diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index b9b8fc8b04..54a0270967 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -399,6 +399,8 @@ class TestEnv < Test::Unit::TestCase def test_to_h assert_equal(ENV.to_hash, ENV.to_h) + assert_equal(ENV.map {|k, v| ["$#{k}", v.size]}.to_h, + ENV.to_h {|k, v| ["$#{k}", v.size]}) end def test_reject diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 11735c5620..6aaeddc9d4 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -847,6 +847,16 @@ class TestHash < Test::Unit::TestCase assert_equal("nope42", h[42]) end + def test_to_h_block + h = @h.to_h {|k, v| [k.to_s, v.to_s]} + assert_equal({ + "1"=>"one", "2"=>"two", "3"=>"three", to_s=>"self", + "true"=>"true", ""=>"nil", "nil"=>"" + }, + h) + assert_instance_of(Hash, h) + end + def test_nil_to_h h = nil.to_h assert_equal({}, h) diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index edcd111acb..4748419d6b 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -362,6 +362,13 @@ module TestStruct assert_equal({a:1, b:2, c:3, d:4, e:5, f:6}, o.to_h) end + def test_to_h_block + klass = @Struct.new(:a, :b, :c, :d, :e, :f) + o = klass.new(1, 2, 3, 4, 5, 6) + assert_equal({"a" => 1, "b" => 4, "c" => 9, "d" => 16, "e" => 25, "f" => 36}, + o.to_h {|k, v| [k.to_s, v*v]}) + end + def test_question_mark_in_member klass = @Struct.new(:a, :b?) x = Object.new |