seed r192 - in trunk: libseed tests



Author: racarr
Date: Sat Nov  8 03:56:50 2008
New Revision: 192
URL: http://svn.gnome.org/viewvc/seed?rev=192&view=rev

Log:
Make constructors more robust. Add test for constructor exceptions.


Added:
   trunk/tests/constructor-args.js   (contents, props changed)
Modified:
   trunk/libseed/seed-engine.c
   trunk/tests/Makefile.am

Modified: trunk/libseed/seed-engine.c
==============================================================================
--- trunk/libseed/seed-engine.c	(original)
+++ trunk/libseed/seed-engine.c	Sat Nov  8 03:56:50 2008
@@ -87,24 +87,38 @@
 
     type = (GType) JSObjectGetPrivate(constructor);
     if (!type)
-	return 0;
+			return 0;
+
     oclass = g_type_class_ref(type);
 
-    g_assert(argumentCount <= 1);
+    if (argumentCount > 1)
+    {
+	    gchar * mes = g_strdup_printf("Constructor expects"
+					  " 1 argument, got %d", argumentCount);
+	    seed_make_exception(exception, "ArgumentError", mes);
+	    g_free(mes);
+	    
+	    return (JSObjectRef)JSValueMakeNull(eng->context);
+    }
 
     if (argumentCount == 1)
     {
-	jsprops = JSObjectCopyPropertyNames(eng->context,
-					    (JSObjectRef) arguments[0]);
-	nparams = JSPropertyNameArrayGetCount(jsprops);
+	    if (!JSValueIsObject(eng->context, arguments[0]))
+		{
+				seed_make_exception(exception, "ArgmuentError",
+									"Constructor expects object as argument");
+                g_type_class_unref(oclass);
+				return (JSObjectRef)JSValueMakeNull(eng->context);
+		}
+		
+		jsprops = JSObjectCopyPropertyNames(eng->context,
+											(JSObjectRef) arguments[0]);
+		nparams = JSPropertyNameArrayGetCount(jsprops);
     }
     i = 0;
 
     params = g_new0(GParameter, nparams + 1);
 
-    // TODO: make sure we don't die if we get passed something other than
-    // an object
-
     while (i < nparams)
     {
 	GType type;

Modified: trunk/tests/Makefile.am
==============================================================================
--- trunk/tests/Makefile.am	(original)
+++ trunk/tests/Makefile.am	Sat Nov  8 03:56:50 2008
@@ -1,6 +1,7 @@
 EXTRA_DIST = \
 	argv.js \
     compare.js \
+    constructor-args.js \
     signal-this.js \
     signal-userdata.js \
     signal-expects.js \

Added: trunk/tests/constructor-args.js
==============================================================================
--- (empty file)
+++ trunk/tests/constructor-args.js	Sat Nov  8 03:56:50 2008
@@ -0,0 +1,34 @@
+#!/usr/local/bin/seed
+// Returns: 0
+// STDIN:
+// STDOUT:Constructor expects 1 argument, got 2\nConstructor expects object as argument
+// STDERR:
+Seed.import_namespace("Gtk");
+Gtk.init(null, null);
+
+try
+{
+	w = new Gtk.Window();
+}
+catch (e)
+{
+	Seed.print(e.message);
+}
+
+try
+{
+	w = new Gtk.Window(1, 2);
+}
+catch (e)
+{
+	Seed.print(e.message);
+}
+
+try
+{
+	w = new Gtk.Window("safA");
+}
+catch (e)
+{
+	Seed.print(e.message);
+}
\ No newline at end of file



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]