From 9471f4187f104f7a0942fdccc884e57cf8027d07 Mon Sep 17 00:00:00 2001 From: marcandre Date: Mon, 23 Dec 2013 03:42:29 +0000 Subject: * array.c: Have to_h raise on elements that are not key-value pairs [#9239] * enum.c: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ array.c | 13 +++++++++---- enum.c | 16 +++++++++++----- test/ruby/test_array.rb | 12 +++++++++--- test/ruby/test_enum.rb | 18 +++++++++++------- 5 files changed, 47 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index d5223fa02d..a6a57ccdbe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Mon Dec 23 12:42:13 2013 Marc-Andre Lafortune + + * array.c: Have to_h raise on elements that are not key-value pairs + [#9239] + + * enum.c: ditto + Mon Dec 23 05:01:55 2013 Zachary Scott * doc/syntax/methods.rdoc: [DOC] Added example for underscore diff --git a/array.c b/array.c index 0241d72b5f..45713c55b0 100644 --- a/array.c +++ b/array.c @@ -2130,8 +2130,7 @@ rb_ary_to_a(VALUE ary) * ary.to_h -> hash * * Returns the result of interpreting ary as an array of - * [key, value] pairs. Elements other than pairs of - * values are ignored. + * [key, value] pairs. * * [[:foo, :bar], [1, 2]].to_h * # => {:foo => :bar, 1 => 2} @@ -2144,9 +2143,15 @@ rb_ary_to_h(VALUE ary) VALUE hash = rb_hash_new(); for (i=0; i hash * * Returns the result of interpreting enum as a list of - * [key, value] pairs. Elements other than pairs of - * values are ignored. + * [key, value] pairs. * * %i[hello world].each_with_index.to_h * # => {:hello => 0, :world => 1} diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 526fcabb1b..b648c404ed 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1467,12 +1467,18 @@ class TestArray < Test::Unit::TestCase end array = [ [:key, :value], - [:ignore_me], - [:ignore, :me, :too], - :ignore_me, kvp, ] assert_equal({key: :value, obtained: :via_to_ary}, array.to_h) + + e = assert_raise(TypeError) { + [[:first_one, :ok], :not_ok].to_h + } + assert_equal "wrong element type Symbol at 1 (expected array)", e.message + e = assert_raise(ArgumentError) { + [[:first_one, :ok], [1, 2], [:not_ok]].to_h + } + assert_equal "wrong array length at 2 (expected 2, was 1)", e.message end def test_uniq diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index b86ae274d1..3ee6846b33 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -104,17 +104,11 @@ class TestEnumerable < Test::Unit::TestCase end def test_to_h - assert_equal({}, @obj.to_h) obj = Object.new def obj.each(*args) - yield args + yield *args yield [:key, :value] - yield [:ignore_me] - yield [:ignore, :me, :too] yield :other_key, :other_value - yield :ignore_me - yield :ignore, :me, :too - yield kvp = Object.new def kvp.to_ary [:obtained, :via_to_ary] @@ -128,6 +122,16 @@ class TestEnumerable < Test::Unit::TestCase :other_key => :other_value, :obtained => :via_to_ary, }, obj.to_h(:hello, :world)) + + 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 -- cgit v1.2.3