* error.c (rb_check_typeddata): refine error message with

including expected struct name.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34052 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2011-12-15 05:33:34 +00:00
Родитель 57537ccb0e
Коммит 9dccfb5513
5 изменённых файлов: 74 добавлений и 17 удалений

Просмотреть файл

@ -1,3 +1,8 @@
Thu Dec 15 14:33:33 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* error.c (rb_check_typeddata): refine error message with
including expected struct name.
Thu Dec 15 13:15:51 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* regcomp.c (onig_region_memsize): implemented for memsize_of().

44
error.c
Просмотреть файл

@ -378,6 +378,30 @@ static const struct types {
{T_UNDEF, "undef"}, /* internal use: #undef; should not happen */
};
static const char *
builtin_type_name(VALUE x)
{
const char *etype;
if (NIL_P(x)) {
etype = "nil";
}
else if (FIXNUM_P(x)) {
etype = "Fixnum";
}
else if (SYMBOL_P(x)) {
etype = "Symbol";
}
else if (rb_special_const_p(x)) {
x = rb_obj_as_string(x);
etype = StringValuePtr(x);
}
else {
etype = rb_obj_classname(x);
}
return etype;
}
void
rb_check_type(VALUE x, int t)
{
@ -396,22 +420,7 @@ rb_check_type(VALUE x, int t)
if (type->type == t) {
const char *etype;
if (NIL_P(x)) {
etype = "nil";
}
else if (FIXNUM_P(x)) {
etype = "Fixnum";
}
else if (SYMBOL_P(x)) {
etype = "Symbol";
}
else if (rb_special_const_p(x)) {
x = rb_obj_as_string(x);
etype = StringValuePtr(x);
}
else {
etype = rb_obj_classname(x);
}
etype = builtin_type_name(xt);
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
etype, type->name);
}
@ -451,7 +460,8 @@ rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
static const char mesg[] = "wrong argument type %s (expected %s)";
if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA) {
Check_Type(obj, T_DATA);
etype = builtin_type_name(obj);
rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name);
}
if (!RTYPEDDATA_P(obj)) {
etype = rb_obj_classname(obj);

Просмотреть файл

@ -0,0 +1 @@
create_makefile("-test-/typeddata/typeddata")

Просмотреть файл

@ -0,0 +1,20 @@
#include <ruby.h>
static const rb_data_type_t test_data = {
"typed_data",
};
static VALUE
test_check(VALUE self, VALUE obj)
{
rb_check_typeddata(obj, &test_data);
return obj;
}
void
Init_typeddata(void)
{
VALUE mBug = rb_define_module("Bug");
VALUE klass = rb_define_class_under(mBug, "TypedData", rb_cData);
rb_define_singleton_method(klass, "check", test_check, 1);
}

Просмотреть файл

@ -0,0 +1,21 @@
require 'test/unit'
require "-test-/typeddata/typeddata"
class Test_TypedData < Test::Unit::TestCase
def test_wrong_argtype
e = assert_raise(TypeError) {Bug::TypedData.check(false)}
assert_equal("wrong argument type false (expected typed_data)", e.message)
e = assert_raise(TypeError) {Bug::TypedData.check(true)}
assert_equal("wrong argument type true (expected typed_data)", e.message)
e = assert_raise(TypeError) {Bug::TypedData.check(:e)}
assert_equal("wrong argument type Symbol (expected typed_data)", e.message)
e = assert_raise(TypeError) {Bug::TypedData.check(0)}
assert_equal("wrong argument type Fixnum (expected typed_data)", e.message)
e = assert_raise(TypeError) {Bug::TypedData.check("a")}
assert_equal("wrong argument type String (expected typed_data)", e.message)
end
end