1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

error.c: receiver kwarg

* error.c (name_err_initialize_options): NameError#initialize
  accepts receiver.  [Feature #14313]

* error.c (nometh_err_initialize_options): pass keyword arguments
  to the super method.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62057 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-01-26 10:55:47 +00:00
parent f79d891aad
commit c30aed0817
2 changed files with 77 additions and 1 deletions

32
error.c
View file

@ -1351,6 +1351,8 @@ rb_name_error_str(VALUE str, const char *fmt, ...)
rb_exc_raise(exc);
}
static VALUE name_err_initialize_options(int argc, VALUE *argv, VALUE self, VALUE options);
/*
* call-seq:
* NameError.new([msg, *, name]) -> name_error
@ -1363,12 +1365,30 @@ rb_name_error_str(VALUE str, const char *fmt, ...)
static VALUE
name_err_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE options;
argc = rb_scan_args(argc, argv, "*:", NULL, &options);
return name_err_initialize_options(argc, argv, self, options);
}
static VALUE
name_err_initialize_options(int argc, VALUE *argv, VALUE self, VALUE options)
{
ID keywords[1];
VALUE values[numberof(keywords)];
VALUE name;
VALUE iseqw = Qnil;
int i;
keywords[0] = id_receiver;
rb_get_kwargs(options, keywords, 0, numberof(values), values);
name = (argc > 1) ? argv[--argc] : Qnil;
rb_call_super(argc, argv);
rb_ivar_set(self, id_name, name);
for (i = 0; i < numberof(keywords); ++i) {
if (values[i] != Qundef) {
rb_ivar_set(self, keywords[i], values[i]);
}
}
{
const rb_execution_context_t *ec = GET_EC();
rb_control_frame_t *cfp =
@ -1416,6 +1436,8 @@ name_err_local_variables(VALUE self)
return vars;
}
static VALUE nometh_err_initialize_options(int argc, VALUE *argv, VALUE self, VALUE options);
/*
* call-seq:
* NoMethodError.new([msg, *, name [, args [, priv]]]) -> no_method_error
@ -1428,10 +1450,18 @@ name_err_local_variables(VALUE self)
static VALUE
nometh_err_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE options;
argc = rb_scan_args(argc, argv, "*:", NULL, &options);
return nometh_err_initialize_options(argc, argv, self, options);
}
static VALUE
nometh_err_initialize_options(int argc, VALUE *argv, VALUE self, VALUE options)
{
VALUE priv = (argc > 3) && (--argc, RTEST(argv[argc])) ? Qtrue : Qfalse;
VALUE args = (argc > 2) ? argv[--argc] : Qnil;
name_err_initialize(argc, argv, self);
name_err_initialize_options(argc, argv, self, options);
rb_ivar_set(self, id_args, args);
rb_ivar_set(self, id_private_call_p, RTEST(priv) ? Qtrue : Qfalse);
return self;

View file

@ -866,6 +866,14 @@ end.join
error = NameError.new
assert_raise(ArgumentError) {error.receiver}
assert_equal("NameError", error.message)
error = NameError.new(receiver: receiver)
assert_equal(["NameError", receiver],
[error.message, error.receiver])
error = NameError.new("Message", :foo, receiver: receiver)
assert_equal(["Message", receiver, :foo],
[error.message, error.receiver, error.name])
end
def test_nomethod_error_new_default
@ -903,6 +911,44 @@ end.join
[error.name, error.args, error.private_call?])
end
def test_nomethod_error_new_receiver
receiver = Object.new
error = NoMethodError.new
assert_raise(ArgumentError) {error.receiver}
error = NoMethodError.new(receiver: receiver)
assert_equal(receiver, error.receiver)
error = NoMethodError.new("Message")
assert_raise(ArgumentError) {error.receiver}
error = NoMethodError.new("Message", receiver: receiver)
assert_equal(["Message", receiver],
[error.message, error.receiver])
error = NoMethodError.new("Message", :foo)
assert_raise(ArgumentError) {error.receiver}
error = NoMethodError.new("Message", :foo, receiver: receiver)
assert_equal(["Message", :foo, receiver],
[error.message, error.name, error.receiver])
error = NoMethodError.new("Message", :foo, [1, 2])
assert_raise(ArgumentError) {error.receiver}
error = NoMethodError.new("Message", :foo, [1, 2], receiver: receiver)
assert_equal(["Message", :foo, [1, 2], receiver],
[error.message, error.name, error.args, error.receiver])
error = NoMethodError.new("Message", :foo, [1, 2], true)
assert_raise(ArgumentError) {error.receiver}
error = NoMethodError.new("Message", :foo, [1, 2], true, receiver: receiver)
assert_equal([:foo, [1, 2], true, receiver],
[error.name, error.args, error.private_call?, error.receiver])
end
def test_name_error_info_const
obj = PrettyObject.new