summaryrefslogtreecommitdiff
path: root/test/ruby/test_enum.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-20 15:06:56 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-20 15:06:56 +0000
commitabe75149d14d3286d3051c9e961ab6473a243a19 (patch)
treef33ad9fcd4569f51d7f0b85fefb751597211438d /test/ruby/test_enum.rb
parente76eebd79be9fa3cb59d0ea6e4ce8214cc6e56ad (diff)
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/test_enum.rb')
-rw-r--r--test/ruby/test_enum.rb30
1 files changed, 28 insertions, 2 deletions
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 })