diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-01-30 04:03:19 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-01-30 04:03:19 +0000 |
commit | 46142e472fd713023766aa2943158d7fd50011ae (patch) | |
tree | 9547d0ea035ed0b71c6b5e3a1705ffb5cca91c63 | |
parent | f989f7c7ea89bb0b4a120666f5e85c0a81c9b44c (diff) |
* hash.c (rb_hash_fetch_m): use useful message for longer key, not a
nonsense id value.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30730 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | hash.c | 3 | ||||
-rw-r--r-- | test/ruby/test_hash.rb | 10 |
3 files changed, 14 insertions, 4 deletions
@@ -1,4 +1,7 @@ -Sun Jan 30 13:01:54 2011 Nobuyoshi Nakada <nobu@ruby-lang.org> +Sun Jan 30 13:03:16 2011 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * hash.c (rb_hash_fetch_m): use useful message for longer key, not a + nonsense id value. * string.c (rb_str_ellipsize): new function to ellipsize a string. @@ -584,9 +584,10 @@ rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash) if (block_given) return rb_yield(key); if (argc == 1) { volatile VALUE desc = rb_protect(rb_inspect, key, 0); - if (NIL_P(desc) || RSTRING_LEN(desc) > 65) { + if (NIL_P(desc)) { desc = rb_any_to_s(key); } + desc = rb_str_ellipsize(desc, 65); rb_raise(rb_eKeyError, "key not found: %s", RSTRING_PTR(desc)); } return if_none; diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 4c0d556896..82aba92529 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -373,8 +373,6 @@ class TestHash < Test::Unit::TestCase end def test_fetch - assert_raise(KeyError) { @cls[].fetch(1) } - assert_raise(KeyError) { @h.fetch('gumby') } assert_equal('gumbygumby', @h.fetch('gumby') {|k| k * 2 }) assert_equal('pokey', @h.fetch('gumby', 'pokey')) @@ -383,6 +381,14 @@ class TestHash < Test::Unit::TestCase assert_equal('nil', @h.fetch(nil)) end + def test_fetch_error + assert_raise(KeyError) { @cls[].fetch(1) } + assert_raise(KeyError) { @h.fetch('gumby') } + e = assert_raise(KeyError) { @h.fetch('gumby'*20) } + assert_match(/key not found: "gumbygumby/, e.message) + assert_match(/\.\.\.\z/, e.message) + end + def test_key2? assert(!@cls[].key?(1)) assert(!@cls[].key?(nil)) |