[libpeas] Port tests to creating a new engine for each test



commit 20ceafbd5c4592c411714777fac313156b7ddfe8
Author: Garrett Regier <alias301 gmail com>
Date:   Sun Feb 6 03:40:03 2011 -0800

    Port tests to creating a new engine for each test

 tests/libpeas-gtk/testing/testing.c |   58 ++++++++++++++++-----------------
 tests/libpeas/engine.c              |   16 +---------
 tests/libpeas/testing/testing.c     |   60 +++++++++++++++++------------------
 3 files changed, 58 insertions(+), 76 deletions(-)
---
diff --git a/tests/libpeas-gtk/testing/testing.c b/tests/libpeas-gtk/testing/testing.c
index 77755b0..a2a850d 100644
--- a/tests/libpeas-gtk/testing/testing.c
+++ b/tests/libpeas-gtk/testing/testing.c
@@ -32,6 +32,7 @@
 
 #include "testing.h"
 
+static PeasEngine *engine = NULL;
 static GLogFunc default_log_func;
 
 /* These are warning that just have to happen for testing
@@ -84,57 +85,54 @@ out:
 PeasEngine *
 testing_engine_new (void)
 {
-  PeasEngine *engine;
   GError *error = NULL;
   static gboolean initialized = FALSE;
 
-  if (initialized)
-    return peas_engine_get_default ();
+  if (engine != NULL)
+    return engine;
 
-  /* Don't always abort on warnings */
-  g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
+  if (!initialized)
+    {
+      /* Don't always abort on warnings */
+      g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
+
+      default_log_func = g_log_set_default_handler (log_handler, NULL);
 
-  default_log_func = g_log_set_default_handler (log_handler, NULL);
+      g_irepository_prepend_search_path (BUILDDIR "/libpeas");
+      g_irepository_prepend_search_path (BUILDDIR "/libpeas-gtk");
 
-  g_irepository_prepend_search_path (BUILDDIR "/libpeas");
-  g_irepository_prepend_search_path (BUILDDIR "/libpeas-gtk");
+      g_setenv ("PEAS_PLUGIN_LOADERS_DIR", BUILDDIR "/loaders", TRUE);
 
-  g_setenv ("PEAS_PLUGIN_LOADERS_DIR", BUILDDIR "/loaders", TRUE);
+      g_irepository_require (g_irepository_get_default (), "Peas", "1.0", 0, &error);
+      g_assert_no_error (error);
+      g_irepository_require (g_irepository_get_default (), "PeasGtk", "1.0", 0, &error);
+      g_assert_no_error (error);
 
-  g_irepository_require (g_irepository_get_default (), "Peas", "1.0", 0, &error);
-  g_assert_no_error (error);
-  g_irepository_require (g_irepository_get_default (), "PeasGtk", "1.0", 0, &error);
-  g_assert_no_error (error);
+      initialized = TRUE;
+    }
 
   /* Must be after requiring typelibs */
   engine = peas_engine_get_default ();
 
+  g_object_add_weak_pointer (G_OBJECT (engine), (gpointer *) &engine);
+
   peas_engine_add_search_path (engine, BUILDDIR "/tests/plugins", NULL);
   peas_engine_add_search_path (engine, BUILDDIR "/tests/libpeas-gtk/plugins", NULL);
 
-  initialized = TRUE;
-
   return engine;
 }
 
 void
-testing_engine_free (PeasEngine *engine)
+testing_engine_free (PeasEngine *engine_)
 {
-  const GList *plugins;
-
-  /* This causes errors during the next test:
-  g_object_unref (engine);*/
-
-  /* Otherwise the tests may cause each other to fail
-   * because they expected a plugin to be available
-   * but it failed loading making it unavailable
-   */
-  plugins = peas_engine_get_plugin_list (engine);
-  for (; plugins != NULL; plugins = plugins->next)
-    ((PeasPluginInfo *) plugins->data)->available = TRUE;
+  /* Incase a test needs to free the engine */
+  if (engine != NULL)
+    {
+      g_object_unref (engine_);
 
-  peas_engine_set_loaded_plugins (engine, NULL);
-  g_assert (peas_engine_get_loaded_plugins (engine) == NULL);
+      /* Make sure that at the end of every test the engine is freed */
+      g_assert (engine == NULL);
+    }
 }
 
 PeasPluginInfo *
diff --git a/tests/libpeas/engine.c b/tests/libpeas/engine.c
index f53e749..07a526d 100644
--- a/tests/libpeas/engine.c
+++ b/tests/libpeas/engine.c
@@ -234,12 +234,6 @@ test_engine_loaded_plugins (PeasEngine *engine)
   g_assert (loaded_plugins == NULL);
   g_assert_cmpint (loaded, ==, 0);
 
-
-#ifdef CANNOT_TEST
-  /* Cannot be done as unrefing the engine causes
-   * issues when another test is run
-   */
-
   g_assert (peas_engine_load_plugin (engine, info));
 
   g_assert_cmpint (loaded, ==, 1);
@@ -254,15 +248,7 @@ test_engine_loaded_plugins (PeasEngine *engine)
   g_assert_cmpstr (loaded_plugins[0], ==, "loadable");
   g_assert (loaded_plugins[1] == NULL);
 
-  if (loaded_plugins != NULL)
-    g_strfreev (loaded_plugins);
-#endif
-
-  g_signal_handlers_disconnect_by_func (engine, load_plugin_cb, &loaded);
-  g_signal_handlers_disconnect_by_func (engine, unload_plugin_cb, &loaded);
-  g_signal_handlers_disconnect_by_func (engine,
-                                        notify_loaded_plugins_cb,
-                                        &loaded_plugins);
+  g_strfreev (loaded_plugins);
 }
 
 static void
diff --git a/tests/libpeas/testing/testing.c b/tests/libpeas/testing/testing.c
index d0dd7e3..fbccbe0 100644
--- a/tests/libpeas/testing/testing.c
+++ b/tests/libpeas/testing/testing.c
@@ -32,6 +32,7 @@
 
 #include "testing.h"
 
+static PeasEngine *engine = NULL;
 static GLogFunc default_log_func;
 
 /* These are warning that just have to happen for testing
@@ -90,60 +91,57 @@ out:
 PeasEngine *
 testing_engine_new (void)
 {
-  PeasEngine *engine;
   GError *error = NULL;
   static gboolean initialized = FALSE;
 
-  if (initialized)
-    return peas_engine_get_default ();
+  if (engine != NULL)
+    return engine;
 
-  /* Don't always abort on warnings */
-  g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
+  if (!initialized)
+    {
+      /* Don't always abort on warnings */
+      g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
+
+      default_log_func = g_log_set_default_handler (log_handler, NULL);
 
-  default_log_func = g_log_set_default_handler (log_handler, NULL);
+      g_irepository_prepend_search_path (BUILDDIR "/libpeas");
 
-  g_irepository_prepend_search_path (BUILDDIR "/libpeas");
+      g_setenv ("PEAS_PLUGIN_LOADERS_DIR", BUILDDIR "/loaders", TRUE);
 
-  g_setenv ("PEAS_PLUGIN_LOADERS_DIR", BUILDDIR "/loaders", TRUE);
+      g_irepository_require (g_irepository_get_default (), "Peas", "1.0", 0, &error);
+      g_assert_no_error (error);
 
-  g_irepository_require (g_irepository_get_default (), "Peas", "1.0", 0, &error);
-  g_assert_no_error (error);
+      g_irepository_require_private (g_irepository_get_default (),
+                                     BUILDDIR "/tests/libpeas/introspection",
+                                     "Introspection", "1.0", 0, &error);
+      g_assert_no_error (error);
 
-  g_irepository_require_private (g_irepository_get_default (),
-                                 BUILDDIR "/tests/libpeas/introspection",
-                                 "Introspection", "1.0", 0, &error);
-  g_assert_no_error (error);
+      initialized = TRUE;
+    }
 
   /* Must be after requiring typelibs */
   engine = peas_engine_get_default ();
 
+  g_object_add_weak_pointer (G_OBJECT (engine), (gpointer *) &engine);
+
   /* Disabled here incase another tests tries to load it */
   peas_engine_disable_loader (engine, "disabled");
 
   peas_engine_add_search_path (engine, BUILDDIR "/tests/plugins", NULL);
   peas_engine_add_search_path (engine, BUILDDIR "/tests/libpeas/plugins", NULL);
 
-  initialized = TRUE;
-
   return engine;
 }
 
 void
-testing_engine_free (PeasEngine *engine)
+testing_engine_free (PeasEngine *engine_)
 {
-  const GList *plugins;
-
-  /* This causes errors during the next test:
-  g_object_unref (engine);*/
-
-  /* Otherwise the tests may cause each other to fail
-   * because they expected a plugin to be available
-   * but it failed loading making it unavailable
-   */
-  plugins = peas_engine_get_plugin_list (engine);
-  for (; plugins != NULL; plugins = plugins->next)
-    ((PeasPluginInfo *) plugins->data)->available = TRUE;
+  /* Incase a test needs to free the engine */
+  if (engine != NULL)
+    {
+      g_object_unref (engine_);
 
-  peas_engine_set_loaded_plugins (engine, NULL);
-  g_assert (peas_engine_get_loaded_plugins (engine) == NULL);
+      /* Make sure that at the end of every test the engine is freed */
+      g_assert (engine == NULL);
+    }
 }



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