mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
range.c: check if exclude_end? is defined
* range.c (rb_range_values): should raise TypeError if necessary method is not defined, not NoMethodError, when trying to tell if the object is a Range and extract info. [ruby-core:83541] [Bug #14048] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60411 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
bb2aa9b343
commit
0ba049256e
3 changed files with 26 additions and 2 deletions
13
range.c
13
range.c
|
@ -987,11 +987,22 @@ rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
|
||||||
excl = EXCL(range);
|
excl = EXCL(range);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
VALUE x;
|
||||||
|
#if 0
|
||||||
|
/* TODO: fix spec/mspec/lib/mspec/mocks/mock.rb:Mock.verify_call */
|
||||||
|
b = rb_check_funcall(range, id_beg, 0, 0);
|
||||||
|
if (b == Qundef) return (int)Qfalse;
|
||||||
|
e = rb_check_funcall(range, id_end, 0, 0);
|
||||||
|
if (e == Qundef) return (int)Qfalse;
|
||||||
|
#else
|
||||||
if (!rb_respond_to(range, id_beg)) return (int)Qfalse;
|
if (!rb_respond_to(range, id_beg)) return (int)Qfalse;
|
||||||
if (!rb_respond_to(range, id_end)) return (int)Qfalse;
|
if (!rb_respond_to(range, id_end)) return (int)Qfalse;
|
||||||
b = rb_funcall(range, id_beg, 0);
|
b = rb_funcall(range, id_beg, 0);
|
||||||
e = rb_funcall(range, id_end, 0);
|
e = rb_funcall(range, id_end, 0);
|
||||||
excl = RTEST(rb_funcall(range, rb_intern("exclude_end?"), 0));
|
#endif
|
||||||
|
x = rb_check_funcall(range, rb_intern("exclude_end?"), 0, 0);
|
||||||
|
if (x == Qundef) return (int)Qfalse;
|
||||||
|
excl = RTEST(x);
|
||||||
}
|
}
|
||||||
*begp = b;
|
*begp = b;
|
||||||
*endp = e;
|
*endp = e;
|
||||||
|
|
|
@ -966,6 +966,19 @@ class TestEnumerable < Test::Unit::TestCase
|
||||||
assert_int_equal(5, (2..0).sum(5))
|
assert_int_equal(5, (2..0).sum(5))
|
||||||
assert_int_equal(2, (2..2).sum)
|
assert_int_equal(2, (2..2).sum)
|
||||||
assert_int_equal(42, (2...2).sum(42))
|
assert_int_equal(42, (2...2).sum(42))
|
||||||
|
|
||||||
|
not_a_range = Class.new do
|
||||||
|
include Enumerable # Defines the `#sum` method
|
||||||
|
def each
|
||||||
|
yield 2
|
||||||
|
yield 4
|
||||||
|
yield 6
|
||||||
|
end
|
||||||
|
|
||||||
|
def begin; end
|
||||||
|
def end; end
|
||||||
|
end
|
||||||
|
assert_equal(12, not_a_range.new.sum)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_uniq
|
def test_uniq
|
||||||
|
|
|
@ -387,7 +387,7 @@ class TestRange < Test::Unit::TestCase
|
||||||
assert_raise(TypeError) { [][o] }
|
assert_raise(TypeError) { [][o] }
|
||||||
class << o; attr_accessor :end end
|
class << o; attr_accessor :end end
|
||||||
o.end = 0
|
o.end = 0
|
||||||
assert_raise(NoMethodError) { [][o] }
|
assert_raise(TypeError) { [][o] }
|
||||||
def o.exclude_end=(v) @exclude_end = v end
|
def o.exclude_end=(v) @exclude_end = v end
|
||||||
def o.exclude_end?() @exclude_end end
|
def o.exclude_end?() @exclude_end end
|
||||||
o.exclude_end = false
|
o.exclude_end = false
|
||||||
|
|
Loading…
Add table
Reference in a new issue