Project

General

Profile

Feature #14313 ยป key_error_new.diff

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

View differences:

error.c (working copy)
1663 1663

  
1664 1664
/*
1665 1665
 * call-seq:
1666
 *   KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
1667
 *
1668
 * Construct a new +KeyError+ exception with the given message,
1669
 * receiver and key.
1670
 */
1671

  
1672
static VALUE
1673
key_err_initialize(int argc, VALUE *argv, VALUE self)
1674
{
1675
    VALUE message;
1676
    VALUE options;
1677
    VALUE receiver = Qnil;
1678
    VALUE key = Qnil;
1679

  
1680
    rb_scan_args(argc, argv, "01:", &message, &options);
1681

  
1682
    if (NIL_P(message)) {
1683
	rb_call_super(0, NULL);
1684
    }
1685
    else {
1686
	rb_call_super(1, &message);
1687
    }
1688

  
1689
    if (!NIL_P(options)) {
1690
	static ID keywords[2];
1691
	VALUE values[2];
1692
	if (!keywords[0]) {
1693
	    CONST_ID(keywords[0], "receiver");
1694
	}
1695
	if (!keywords[1]) {
1696
	    CONST_ID(keywords[1], "key");
1697
	}
1698
	rb_get_kwargs(options, keywords, 0, 2, values);
1699
	if (values[0] != Qundef) {
1700
	    receiver = values[0];
1701
	}
1702
	if (values[1] != Qundef) {
1703
	    key = values[1];
1704
	}
1705
    }
1706

  
1707
    rb_ivar_set(self, id_receiver, receiver);
1708
    rb_ivar_set(self, id_key, key);
1709

  
1710
    return self;
1711
}
1712

  
1713
/*
1714
 * call-seq:
1666 1715
 *   SyntaxError.new([msg])  -> syntax_error
1667 1716
 *
1668 1717
 * Construct a SyntaxError exception.
......
2281 2330
    rb_eArgError      = rb_define_class("ArgumentError", rb_eStandardError);
2282 2331
    rb_eIndexError    = rb_define_class("IndexError", rb_eStandardError);
2283 2332
    rb_eKeyError      = rb_define_class("KeyError", rb_eIndexError);
2333
    rb_define_method(rb_eKeyError, "initialize", key_err_initialize, -1);
2284 2334
    rb_define_method(rb_eKeyError, "receiver", key_err_receiver, 0);
2285 2335
    rb_define_method(rb_eKeyError, "key", key_err_key, 0);
2286 2336
    rb_eRangeError    = rb_define_class("RangeError", rb_eStandardError);
test/ruby/test_key_error.rb (working copy)
1
require 'test/unit'
2

  
3
class TestKeyError < Test::Unit::TestCase
4
  def test_default
5
    error = KeyError.new
6
    assert_equal("KeyError", error.message)
7
  end
8

  
9
  def test_message
10
    error = KeyError.new("Message")
11
    assert_equal("Message", error.message)
12
  end
13

  
14
  def test_receiver
15
    receiver = Object.new
16
    error = KeyError.new(receiver: receiver)
17
    assert_equal(receiver, error.receiver)
18
  end
19

  
20
  def test_key
21
    error = KeyError.new(key: :key)
22
    assert_equal(:key, error.key)
23
  end
24

  
25
  def test_receiver_and_key
26
    receiver = Object.new
27
    error = KeyError.new(receiver: receiver, key: :key)
28
    assert_equal([receiver, :key],
29
                 [error.receiver, error.key])
30
  end
31

  
32
  def test_all
33
    receiver = Object.new
34
    error = KeyError.new("Message", receiver: receiver, key: :key)
35
    assert_equal(["Message", receiver, :key],
36
                 [error.message, error.receiver, error.key])
37
  end
38
end