diff options
-rw-r--r-- | array.c | 10 | ||||
-rw-r--r-- | test/ruby/test_array.rb | 6 |
2 files changed, 12 insertions, 4 deletions
@@ -1108,13 +1108,17 @@ enum ary_take_pos_flags static VALUE ary_take_first_or_last(int argc, const VALUE *argv, VALUE ary, enum ary_take_pos_flags last) { - VALUE nv; long n; long len; long offset = 0; - rb_scan_args(argc, argv, "1", &nv); - n = NUM2LONG(nv); + argc = rb_check_arity(argc, 0, 1); + /* the case optional argument is ommited should be handled in + * callers of this function. if another arity case is added, + * this arity check needs to rewrite. */ + RUBY_ASSERT_WHEN(TRUE, argc == 1); + + n = NUM2LONG(argv[0]); len = RARRAY_LEN(ary); if (n > len) { n = len; diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index aaf3eca58c..4660891e10 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -146,14 +146,17 @@ class TestArray < Test::Unit::TestCase assert_equal(1, x.first) assert_equal([1], x.first(1)) assert_equal([1, 2, 3], x.first(3)) + assert_raise_with_message(ArgumentError, /0\.\.1/) {x.first(1, 2)} assert_equal(5, x.last) assert_equal([5], x.last(1)) assert_equal([3, 4, 5], x.last(3)) + assert_raise_with_message(ArgumentError, /0\.\.1/) {x.last(1, 2)} assert_equal(1, x.shift) assert_equal([2, 3, 4], x.shift(3)) assert_equal([5], x) + assert_raise_with_message(ArgumentError, /0\.\.1/) {x.first(1, 2)} assert_equal([2, 3, 4, 5], x.unshift(2, 3, 4)) assert_equal([1, 2, 3, 4, 5], x.unshift(1)) @@ -162,6 +165,7 @@ class TestArray < Test::Unit::TestCase assert_equal(5, x.pop) assert_equal([3, 4], x.pop(2)) assert_equal([1, 2], x) + assert_raise_with_message(ArgumentError, /0\.\.1/) {x.pop(1, 2)} assert_equal([1, 2, 3, 4], x.push(3, 4)) assert_equal([1, 2, 3, 4, 5], x.push(5)) @@ -1585,7 +1589,7 @@ class TestArray < Test::Unit::TestCase def o.to_ary foo_bar() end - assert_match(/foo_bar/, assert_raise(NoMethodError) {a.concat(o)}.message) + assert_raise_with_message(NoMethodError, /foo_bar/) {a.concat(o)} end def test_to_s |