зеркало из https://github.com/github/ruby.git
KeyError#initialize accepts receiver and key.
[Feature #14313][ruby-core:84626] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62049 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
4fb218f9a0
Коммит
cd83d26731
50
error.c
50
error.c
|
@ -1679,6 +1679,55 @@ rb_key_err_new(VALUE mesg, VALUE recv, VALUE key)
|
|||
return exc;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
|
||||
*
|
||||
* Construct a new +KeyError+ exception with the given message,
|
||||
* receiver and key.
|
||||
*/
|
||||
|
||||
static VALUE
|
||||
key_err_initialize(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
VALUE message;
|
||||
VALUE options;
|
||||
VALUE receiver = Qnil;
|
||||
VALUE key = Qnil;
|
||||
|
||||
rb_scan_args(argc, argv, "01:", &message, &options);
|
||||
|
||||
if (NIL_P(message)) {
|
||||
rb_call_super(0, NULL);
|
||||
}
|
||||
else {
|
||||
rb_call_super(1, &message);
|
||||
}
|
||||
|
||||
if (!NIL_P(options)) {
|
||||
static ID keywords[2];
|
||||
VALUE values[2];
|
||||
if (!keywords[0]) {
|
||||
CONST_ID(keywords[0], "receiver");
|
||||
}
|
||||
if (!keywords[1]) {
|
||||
CONST_ID(keywords[1], "key");
|
||||
}
|
||||
rb_get_kwargs(options, keywords, 0, 2, values);
|
||||
if (values[0] != Qundef) {
|
||||
receiver = values[0];
|
||||
}
|
||||
if (values[1] != Qundef) {
|
||||
key = values[1];
|
||||
}
|
||||
}
|
||||
|
||||
rb_ivar_set(self, id_receiver, receiver);
|
||||
rb_ivar_set(self, id_key, key);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* SyntaxError.new([msg]) -> syntax_error
|
||||
|
@ -2299,6 +2348,7 @@ Init_Exception(void)
|
|||
rb_eArgError = rb_define_class("ArgumentError", rb_eStandardError);
|
||||
rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
|
||||
rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
|
||||
rb_define_method(rb_eKeyError, "initialize", key_err_initialize, -1);
|
||||
rb_define_method(rb_eKeyError, "receiver", key_err_receiver, 0);
|
||||
rb_define_method(rb_eKeyError, "key", key_err_key, 0);
|
||||
rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
require 'test/unit'
|
||||
|
||||
class TestKeyError < Test::Unit::TestCase
|
||||
def test_default
|
||||
error = KeyError.new
|
||||
assert_equal("KeyError", error.message)
|
||||
end
|
||||
|
||||
def test_message
|
||||
error = KeyError.new("Message")
|
||||
assert_equal("Message", error.message)
|
||||
end
|
||||
|
||||
def test_receiver
|
||||
receiver = Object.new
|
||||
error = KeyError.new(receiver: receiver)
|
||||
assert_equal(receiver, error.receiver)
|
||||
end
|
||||
|
||||
def test_key
|
||||
error = KeyError.new(key: :key)
|
||||
assert_equal(:key, error.key)
|
||||
end
|
||||
|
||||
def test_receiver_and_key
|
||||
receiver = Object.new
|
||||
error = KeyError.new(receiver: receiver, key: :key)
|
||||
assert_equal([receiver, :key],
|
||||
[error.receiver, error.key])
|
||||
end
|
||||
|
||||
def test_all
|
||||
receiver = Object.new
|
||||
error = KeyError.new("Message", receiver: receiver, key: :key)
|
||||
assert_equal(["Message", receiver, :key],
|
||||
[error.message, error.receiver, error.key])
|
||||
end
|
||||
end
|
Загрузка…
Ссылка в новой задаче