[gnome-builder/wip/chergert/unittests: 2/2] testing: stub out interface for unit tests



commit afcac03c6fccf9b9436fbdc5a4efc2d9c9bd149b
Author: Christian Hergert <chergert redhat com>
Date:   Fri Oct 13 17:40:19 2017 -0700

    testing: stub out interface for unit tests

 src/libide/meson.build                 |    1 +
 src/libide/testing/ide-test-provider.c |  117 ++++++++++++++++++++++
 src/libide/testing/ide-test-provider.h |   52 ++++++++++
 src/libide/testing/ide-test.c          |  166 ++++++++++++++++++++++++++++++++
 src/libide/testing/ide-test.h          |   45 +++++++++
 src/libide/testing/meson.build         |   14 +++
 6 files changed, 395 insertions(+), 0 deletions(-)
---
diff --git a/src/libide/meson.build b/src/libide/meson.build
index 5e1473f..67fc7af 100644
--- a/src/libide/meson.build
+++ b/src/libide/meson.build
@@ -87,6 +87,7 @@ subdir('sourceview')
 subdir('subprocess')
 subdir('symbols')
 subdir('template')
+subdir('testing')
 subdir('threading')
 subdir('transfers')
 subdir('util')
diff --git a/src/libide/testing/ide-test-provider.c b/src/libide/testing/ide-test-provider.c
new file mode 100644
index 0000000..ce71dc2
--- /dev/null
+++ b/src/libide/testing/ide-test-provider.c
@@ -0,0 +1,117 @@
+/* ide-test-provider.c
+ *
+ * Copyright © 2017 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "ide-test-provider"
+
+#include "ide-test-provider.h"
+
+G_DEFINE_INTERFACE (IdeTestProvider, ide_test_provider, IDE_TYPE_OBJECT)
+
+static void
+ide_test_provider_real_load_tests_async (IdeTestProvider     *self,
+                                         GCancellable        *cancellable,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             user_data)
+{
+  g_autoptr(GTask) task = NULL;
+
+  g_return_if_fail (IDE_IS_TEST_PROVIDER (self));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, ide_test_provider_load_tests_async);
+  g_task_set_priority (task, G_PRIORITY_LOW);
+  g_task_return_new_error (task,
+                           G_IO_ERROR,
+                           G_IO_ERROR_NOT_SUPPORTED,
+                           "Loading tasks is not currently supported");
+}
+
+static GPtrArray *
+ide_test_provider_real_load_tests_finish (IdeTestProvider  *self,
+                                          GAsyncResult     *result,
+                                          GError          **error)
+{
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+static void
+ide_test_provider_default_init (IdeTestProviderInterface *iface)
+{
+  iface->load_tests_async = ide_test_provider_real_load_tests_async;
+  iface->load_tests_finish = ide_test_provider_real_load_tests_finish;
+}
+
+/**
+ * ide_test_provider_load_tests_async:
+ * @self: An #IdeTestProvider
+ * @cancellable: (nullable): A #GCancellable, or %NULL
+ * @callback: A callback for asynchronous notification
+ * @user_data: user data for @callback
+ *
+ * This function will asynchronously load the unit tests that are known
+ * about by a particular #IdeTestProvider plugin.
+ *
+ * @callback is expected to call ide_test_provider_load_tests_finish()
+ * to retrieve the result of this asynchronous operation.
+ *
+ * Since: 3.28
+ */
+void
+ide_test_provider_load_tests_async (IdeTestProvider     *self,
+                                    GCancellable        *cancellable,
+                                    GAsyncReadyCallback  callback,
+                                    gpointer             user_data)
+{
+  g_return_if_fail (IDE_IS_TEST_PROVIDER (self));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  IDE_TEST_PROVIDER_GET_IFACE (self)->load_tests_async (self,
+                                                        cancellable,
+                                                        callback,
+                                                        user_data);
+}
+
+/**
+ * ide_test_provider_load_tests_finish:
+ * @self: An #IdeTestProvider
+ * @result: A #GAsyncResult provided to callback
+ * @error: A location for a #GError, or %NULL
+ *
+ * Completes an asynchronous operation to
+ * ide_test_provider_load_tests_async().
+ *
+ * If no tests were discovered, and empty #GPtrArray is returned
+ * instead of %NULL.
+ *
+ * Returns: (transfer container) (element-type Ide.Test): An array of
+ *   #IdeTest that were discovered by the test provider; or %NULL upon
+ *   failure and @error is set.
+ *
+ * Since: 3.28
+ */
+GPtrArray *
+ide_test_provider_load_tests_finish (IdeTestProvider  *self,
+                                     GAsyncResult     *result,
+                                     GError          **error)
+{
+  g_return_val_if_fail (IDE_IS_TEST_PROVIDER (self), FALSE);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
+
+  return IDE_TEST_PROVIDER_GET_IFACE (self)->load_tests_finish (self, result, error);
+}
diff --git a/src/libide/testing/ide-test-provider.h b/src/libide/testing/ide-test-provider.h
new file mode 100644
index 0000000..b629c24
--- /dev/null
+++ b/src/libide/testing/ide-test-provider.h
@@ -0,0 +1,52 @@
+/* ide-test-provider.h
+ *
+ * Copyright (C) 2017 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "ide-object.h"
+
+#include "testing/ide-test.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TEST_PROVIDER (ide_test_provider_get_type ())
+
+G_DECLARE_INTERFACE (IdeTestProvider, ide_test_provider, IDE, TEST_PROVIDER, IdeObject)
+
+struct _IdeTestProviderInterface
+{
+  GTypeInterface parent;
+
+  void (*load_tests_async)        (IdeTestProvider      *self,
+                                   GCancellable         *cancellable,
+                                   GAsyncReadyCallback   callback,
+                                   gpointer              user_data);
+  GPtrArray *(*load_tests_finish) (IdeTestProvider      *self,
+                                   GAsyncResult         *result,
+                                   GError              **error);
+};
+
+void       ide_test_provider_load_tests_async  (IdeTestProvider      *self,
+                                                GCancellable         *cancellable,
+                                                GAsyncReadyCallback   callback,
+                                                gpointer              user_data);
+GPtrArray *ide_test_provider_load_tests_finish (IdeTestProvider      *self,
+                                                GAsyncResult         *result,
+                                                GError              **error);
+
+G_END_DECLS
diff --git a/src/libide/testing/ide-test.c b/src/libide/testing/ide-test.c
new file mode 100644
index 0000000..4c30037
--- /dev/null
+++ b/src/libide/testing/ide-test.c
@@ -0,0 +1,166 @@
+/* ide-test.c
+ *
+ * Copyright © 2017 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ide-test.h"
+
+typedef struct
+{
+  gchar *name;
+} IdeTestPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (IdeTest, ide_test, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_NAME,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+IdeTest *
+ide_test_new (void)
+{
+  return g_object_new (IDE_TYPE_TEST, NULL);
+}
+
+static void
+ide_test_finalize (GObject *object)
+{
+  IdeTest *self = (IdeTest *)object;
+  IdeTestPrivate *priv = ide_test_get_instance_private (self);
+
+  g_clear_pointer (&priv->name, g_free);
+
+  G_OBJECT_CLASS (ide_test_parent_class)->finalize (object);
+}
+
+static void
+ide_test_get_property (GObject    *object,
+                       guint       prop_id,
+                       GValue     *value,
+                       GParamSpec *pspec)
+{
+  IdeTest *self = IDE_TEST (object);
+
+  switch (prop_id)
+    {
+    case PROP_NAME:
+      g_value_set_string (value, ide_test_get_name (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_test_set_property (GObject      *object,
+                       guint         prop_id,
+                       const GValue *value,
+                       GParamSpec   *pspec)
+{
+  IdeTest *self = IDE_TEST (object);
+
+  switch (prop_id)
+    {
+    case PROP_NAME:
+      ide_test_set_name (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_test_class_init (IdeTestClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = ide_test_finalize;
+  object_class->get_property = ide_test_get_property;
+  object_class->set_property = ide_test_set_property;
+
+  /**
+   * IdeTest:name:
+   *
+   * The "name" property contains the display name of the test as
+   * the user is expected to read in UI elements.
+   *
+   * Since: 3.28
+   */
+  properties [PROP_NAME] =
+    g_param_spec_string ("name",
+                         "Name",
+                         "The name of the unit test",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_test_init (IdeTest *self)
+{
+}
+
+/**
+ * ide_test_get_name:
+ * @self: An #IdeTest
+ *
+ * Gets the "name" property of the test.
+ *
+ * Returns: (nullable): The name of the test or %NULL
+ *
+ * Since: 3.28
+ */
+const gchar *
+ide_test_get_name (IdeTest *self)
+{
+  IdeTestPrivate *priv = ide_test_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_TEST (self), NULL);
+
+  return priv->name;
+}
+
+/**
+ * ide_test_set_name:
+ * @self: An #IdeTest
+ * @name: (nullable): The name of the test, or %NULL to unset
+ *
+ * Sets the "name" property of the unit test.
+ *
+ * Since: 3.28
+ */
+void
+ide_test_set_name (IdeTest     *self,
+                   const gchar *name)
+{
+  IdeTestPrivate *priv = ide_test_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_TEST (self));
+
+  if (g_strcmp0 (name, priv->name) != 0)
+    {
+      g_free (priv->name);
+      priv->name = g_strdup (priv->name);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_NAME]);
+    }
+}
diff --git a/src/libide/testing/ide-test.h b/src/libide/testing/ide-test.h
new file mode 100644
index 0000000..7469815
--- /dev/null
+++ b/src/libide/testing/ide-test.h
@@ -0,0 +1,45 @@
+/* ide-test.h
+ *
+ * Copyright © 2017 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TEST (ide_test_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (IdeTest, ide_test, IDE, TEST, GObject)
+
+struct _IdeTestClass
+{
+  GObjectClass parent;
+
+  /*< private >*/
+  gpointer _reserved1;
+  gpointer _reserved2;
+  gpointer _reserved3;
+  gpointer _reserved4;
+};
+
+IdeTest     *ide_test_new      (void);
+const gchar *ide_test_get_name (IdeTest     *self);
+void         ide_test_set_name (IdeTest     *self,
+                                const gchar *name);
+
+G_END_DECLS
diff --git a/src/libide/testing/meson.build b/src/libide/testing/meson.build
new file mode 100644
index 0000000..50d0227
--- /dev/null
+++ b/src/libide/testing/meson.build
@@ -0,0 +1,14 @@
+testing_headers = [
+  'ide-test.h',
+  'ide-test-provider.h',
+]
+
+testing_sources = [
+  'ide-test.c',
+  'ide-test-provider.c',
+]
+
+libide_public_headers += files(testing_headers)
+libide_public_sources += files(testing_sources)
+
+install_headers(testing_headers, subdir: join_paths(libide_header_subdir, 'testing'))


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