Commit 01e61bd9 authored by Sebastian Sauer's avatar Sebastian Sauer
Browse files

raise TypeError-exceptions if convert failed.

svn path=/trunk/koffice/; revision=599906
parent 9329e625
......@@ -118,8 +118,10 @@ namespace Kross {
return INT2FIX(i);
}
inline static int toVariant(VALUE value) {
if(TYPE(value) == T_FLOAT) // ruby is not able to convert double into int :-/
return int(NUM2DBL(value));
if(TYPE(value) != T_FIXNUM) {
rb_raise(rb_eTypeError, "Integer must be a fixed number");
return 0;
}
return FIX2INT(value);
}
};
......@@ -132,8 +134,10 @@ namespace Kross {
return UINT2NUM(i);
}
inline static uint toVariant(VALUE value) {
if(TYPE(value) == T_FLOAT) // ruby is not able to convert double into uint :-/
return uint(NUM2DBL(value));
if(TYPE(value) != T_FIXNUM) {
rb_raise(rb_eTypeError, "Unsigned integer must be a fixed number");
return 0;
}
return FIX2UINT(value);
}
};
......@@ -157,8 +161,17 @@ namespace Kross {
inline static VALUE toVALUE(bool b) {
return b ? Qtrue : Qfalse;
}
inline static double toVariant(VALUE value) {
return TYPE(value) == T_TRUE;
inline static bool toVariant(VALUE value) {
switch( TYPE(value) ) {
case T_TRUE:
return true;
case T_FALSE:
return false;
default: {
rb_raise(rb_eTypeError, "Boolean value expected");
return false;
} break;
}
}
};
......@@ -194,19 +207,16 @@ namespace Kross {
return rb_str_new(ba.constData(), ba.size());
}
inline static QByteArray toVariant(VALUE value) {
switch( TYPE(value) ) {
case T_STRING: {
long length = LONG2NUM( RSTRING(value)->len );
if( length < 0 )
return QByteArray("");
char* ca = rb_str2cstr(value, &length);
return QByteArray(ca, length);
} break;
case T_NIL:
return QByteArray("");
default:
return STR2CSTR( rb_inspect(value) );
if( TYPE(value) != T_STRING ) {
rb_raise(rb_eTypeError, "ByteArray must be a string");
//return STR2CSTR( rb_inspect(value) );
return QByteArray("");
}
long length = LONG2NUM( RSTRING(value)->len );
if( length < 0 )
return QByteArray("");
char* ca = rb_str2cstr(value, &length);
return QByteArray(ca, length);
}
};
......@@ -233,6 +243,10 @@ namespace Kross {
return l;
}
inline static QStringList toVariant(VALUE value) {
if( TYPE(value) != T_ARRAY ) {
rb_raise(rb_eTypeError, "StringList must be an array");
return QStringList();
}
QStringList l;
for(int i = 0; i < RARRAY(value)->len; i++)
l.append( RubyType<QString>::toVariant( rb_ary_entry(value, i) ) );
......@@ -251,6 +265,10 @@ namespace Kross {
return l;
}
inline static QVariantList toVariant(VALUE value) {
if( TYPE(value) != T_ARRAY ) {
rb_raise(rb_eTypeError, "VariantList must be an array");
return QVariantList();
}
QVariantList l;
for(int i = 0; i < RARRAY(value)->len; i++)
l.append( RubyType<QVariant>::toVariant( rb_ary_entry(value, i) ) );
......@@ -277,6 +295,10 @@ namespace Kross {
return ST_CONTINUE;
}
inline static QVariantMap toVariant(VALUE value) {
if( TYPE(value) != T_HASH ) {
rb_raise(rb_eTypeError, "VariantMap must be a hash");
return QVariantMap();
}
QVariantMap map;
VALUE vmap = Data_Wrap_Struct(rb_cObject, 0,0, &map);
rb_hash_foreach(value, (int (*)(...))convertHash, vmap);
......
......@@ -10,28 +10,33 @@ class TestKross < Test::Unit::TestCase
end
def testBool
assert_raises(TypeError) { TestObject1.func_bool_bool(nil) }
assert( TestObject1.func_bool_bool(true) == true )
assert( TestObject1.func_bool_bool(false) == false )
end
def testInt
assert_raises(TypeError) { TestObject1.func_int_int(nil) }
assert( TestObject1.func_int_int(0) == 0 )
assert( TestObject1.func_int_int(177321) == 177321 )
assert( TestObject1.func_int_int(-98765) == -98765 )
end
def testUInt
assert_raises(TypeError) { TestObject1.func_uint_uint(nil) }
assert( TestObject1.func_uint_uint(0) == 0 )
assert( TestObject1.func_uint_uint(177321) == 177321 )
end
def testDouble
assert_raises(TypeError) { TestObject1.func_double_double(nil) }
assert( TestObject1.func_double_double(0.0) == 0.0 )
assert( TestObject1.func_double_double(1773.2177) == 1773.2177 )
assert( TestObject1.func_double_double(-548993.271993) == -548993.271993 )
end
def testLongLong
assert_raises(TypeError) { TestObject1.func_qlonglong_qlonglong(nil) }
assert( TestObject1.func_qlonglong_qlonglong(0) == 0 )
assert( TestObject1.func_qlonglong_qlonglong(7379) == 7379 )
assert( TestObject1.func_qlonglong_qlonglong(-6384673) == -6384673 )
......@@ -39,37 +44,42 @@ class TestKross < Test::Unit::TestCase
end
def testULongLong
assert_raises(TypeError) { TestObject1.func_qulonglong_qulonglong(nil) }
assert( TestObject1.func_qulonglong_qulonglong(0) == 0 )
assert( TestObject1.func_qulonglong_qulonglong(378972) == 378972 )
#assert( TestObject1.func_qulonglong_qulonglong(-8540276823902375665225676321823) == -8540276823902375665225676321823 )
end
def testByteArray
assert( TestObject1.func_qbytearray_qbytearray(nil) == "" )
assert_raises(TypeError) { TestObject1.func_qbytearray_qbytearray(nil) }
assert( TestObject1.func_qbytearray_qbytearray("") == "" )
assert( TestObject1.func_qbytearray_qbytearray(" Some String as ByteArray ") == " Some String as ByteArray " )
assert( TestObject1.func_qbytearray_qbytearray(" \0\n\r\t\s\0 test ") == " \0\n\r\t\s\0 test " )
end
def testString
assert_raises(TypeError) { TestObject1.func_qstring_qstring(nil) }
assert( TestObject1.func_qstring_qstring("") == "" )
assert( TestObject1.func_qstring_qstring(" ") == " " )
assert( TestObject1.func_qstring_qstring(" Another \n\r Test! $%&\" ") == " Another \n\r Test! $%&\" " )
end
def testStringList
assert_raises(TypeError) { TestObject1.func_qstringlist_qstringlist(nil) }
assert( TestObject1.func_qstringlist_qstringlist( [] ) == [] )
assert( TestObject1.func_qstringlist_qstringlist( ["string1"] ) == ["string1"] )
assert( TestObject1.func_qstringlist_qstringlist( [" string1","string2 "] ) == [" string1","string2 "] )
end
def testVariantList
assert_raises(TypeError) { TestObject1.func_qvariantlist_qvariantlist(nil) }
assert( TestObject1.func_qvariantlist_qvariantlist( [] ) == [] )
assert( TestObject1.func_qvariantlist_qvariantlist( [[[[]],[]]] ) == [[[[]],[]]] )
assert( TestObject1.func_qvariantlist_qvariantlist( ["A string",[17539,-8591],[5.32,-842.775]] ) == ["A string",[17539,-8591],[5.32,-842.775]] )
end
def testVariantMap
assert_raises(TypeError) { TestObject1.func_qvariantmap_qvariantmap(nil) }
#assert( {} )
#assert( {"1":73682,"2":285} )
#assert( {"a":-6892.957,"b":692.66} )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment