* 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
This commit is contained in:
nobu 2011-01-30 04:03:19 +00:00
Родитель f989f7c7ea
Коммит 46142e472f
3 изменённых файлов: 14 добавлений и 4 удалений

Просмотреть файл

@ -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.

3
hash.c
Просмотреть файл

@ -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;

Просмотреть файл

@ -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))