diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | error.c | 44 | ||||
-rw-r--r-- | ext/-test-/typeddata/extconf.rb | 1 | ||||
-rw-r--r-- | ext/-test-/typeddata/typeddata.c | 20 | ||||
-rw-r--r-- | test/-ext-/typeddata/test_typeddata.rb | 21 |
5 files changed, 74 insertions, 17 deletions
@@ -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(). @@ -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); diff --git a/ext/-test-/typeddata/extconf.rb b/ext/-test-/typeddata/extconf.rb new file mode 100644 index 0000000000..02e3e41c8b --- /dev/null +++ b/ext/-test-/typeddata/extconf.rb @@ -0,0 +1 @@ +create_makefile("-test-/typeddata/typeddata") diff --git a/ext/-test-/typeddata/typeddata.c b/ext/-test-/typeddata/typeddata.c new file mode 100644 index 0000000000..1c5d677713 --- /dev/null +++ b/ext/-test-/typeddata/typeddata.c @@ -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); +} diff --git a/test/-ext-/typeddata/test_typeddata.rb b/test/-ext-/typeddata/test_typeddata.rb new file mode 100644 index 0000000000..50505850d8 --- /dev/null +++ b/test/-ext-/typeddata/test_typeddata.rb @@ -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 |