Project

General

Profile

Feature #14313 ยป key_error_new.diff

kou (Kouhei Sutou), 01/04/2018 04:58 AM

View differences:

error.c (working copy)
/*
* 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
*
* Construct a SyntaxError exception.
......
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);
test/ruby/test_key_error.rb (working copy)
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
    (1-1/1)