[glib] Add some volume monitor tests



commit 9fc3c980ccbaa6772f9b97b60c786546b67ae8b4
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jul 26 17:59:18 2010 -0400

    Add some volume monitor tests
    
    Although not much of this is easily testable.

 gio/tests/Makefile.am     |    4 +
 gio/tests/volumemonitor.c |  178 +++++++++++++++++++++++++++
 glib/tests/Makefile.am    |    3 +
 glib/tests/shell.c        |  229 ++++++++++++++++++++++++++++++++++
 tests/Makefile.am         |    2 -
 tests/shell-test.c        |  297 ---------------------------------------------
 6 files changed, 414 insertions(+), 299 deletions(-)
---
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index b724fe2..03da206 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -35,6 +35,7 @@ TEST_PROGS +=	 		\
 	buffered-output-stream	\
 	sleepy-stream		\
 	filter-streams		\
+	volumemonitor		\
 	simple-async-result	\
 	srvtarget		\
 	contexts		\
@@ -161,6 +162,9 @@ sleepy_stream_LDADD		= $(progs_ldadd)
 filter_streams_SOURCES		= filter-streams.c
 filter_streams_LDADD		= $(progs_ldadd)
 
+volumemonitor_SOURCES		= volumemonitor.c
+volumemonitor_LDADD		= $(progs_ldadd)
+
 resolver_SOURCES	  = resolver.c
 resolver_LDADD		  = $(progs_ldadd) \
 	$(top_builddir)/gthread/libgthread-2.0.la
diff --git a/gio/tests/volumemonitor.c b/gio/tests/volumemonitor.c
new file mode 100644
index 0000000..921e848
--- /dev/null
+++ b/gio/tests/volumemonitor.c
@@ -0,0 +1,178 @@
+#include <gio/gio.h>
+
+static GVolumeMonitor *monitor;
+
+static void
+do_mount_tests (GDrive *drive, GVolume *volume, GMount *mount)
+{
+  GDrive *d;
+  GVolume *v;
+  gchar *name;
+  gchar *uuid;
+
+  name = g_mount_get_name (mount);
+  g_assert (name != NULL);
+  g_free (name);
+
+  v = g_mount_get_volume (mount);
+  g_assert (v == volume);
+  g_object_unref (v);
+
+  d = g_mount_get_drive (mount);
+  g_assert (d == drive);
+  g_object_unref (d);
+
+  uuid = g_mount_get_uuid (mount);
+  if (uuid)
+    {
+      GMount *m;
+      m = g_volume_monitor_get_mount_for_uuid (monitor, uuid);
+      g_assert (m == mount);
+      g_object_unref (m);
+      g_free (uuid);
+    }
+}
+
+static void
+do_volume_tests (GDrive *drive, GVolume *volume)
+{
+  GDrive *d;
+  gchar *name;
+  GMount *mount;
+  gchar *uuid;
+
+  name = g_volume_get_name (volume);
+  g_assert (name != NULL);
+  g_free (name);
+
+  d = g_volume_get_drive (volume);
+  g_assert (d == drive);
+  g_object_unref (d);
+
+  mount = g_volume_get_mount (volume);
+  if (mount != NULL)
+    {
+      do_mount_tests (drive, volume, mount);
+      g_object_unref (mount);
+    }
+
+  uuid = g_volume_get_uuid (volume);
+  if (uuid)
+    {
+      GVolume *v;
+      v = g_volume_monitor_get_volume_for_uuid (monitor, uuid);
+      g_assert (v == volume);
+      g_object_unref (v);
+      g_free (uuid);
+    }
+}
+
+static void
+do_drive_tests (GDrive *drive)
+{
+  GList *volumes, *l;
+  gchar *name;
+  gboolean has_volumes;
+
+  g_assert (G_IS_DRIVE (drive));
+  name = g_drive_get_name (drive);
+  g_assert (name != NULL);
+  g_free (name);
+
+  has_volumes = g_drive_has_volumes (drive);
+  volumes = g_drive_get_volumes (drive);
+  g_assert (has_volumes == (volumes != NULL));
+  for (l = volumes; l; l = l->next)
+    {
+      GVolume *volume = l->data;
+      do_volume_tests (drive, volume);
+    }
+
+  g_list_foreach (volumes, (GFunc)g_object_unref,  NULL);
+  g_list_free (volumes);
+}
+
+static void
+test_connected_drives (void)
+{
+  GList *drives;
+  GList *l;
+
+  drives = g_volume_monitor_get_connected_drives (monitor);
+
+  for (l = drives; l; l = l->next)
+    {
+      GDrive *drive = l->data;
+      do_drive_tests (drive);
+    }
+
+  g_list_foreach (drives, (GFunc)g_object_unref,  NULL);
+  g_list_free (drives);
+}
+
+static void
+test_volumes (void)
+{
+  GList *volumes, *l;
+
+  volumes = g_volume_monitor_get_volumes (monitor);
+
+  for (l = volumes; l; l = l->next)
+    {
+      GVolume *volume = l->data;
+      GDrive *drive;
+
+      drive = g_volume_get_drive (volume);
+      do_volume_tests (drive, volume);
+      g_object_unref (drive);
+    }
+
+  g_list_foreach (volumes, (GFunc)g_object_unref,  NULL);
+  g_list_free (volumes);
+}
+
+static void
+test_mounts (void)
+{
+  GList *mounts, *l;
+
+  mounts = g_volume_monitor_get_mounts (monitor);
+
+  for (l = mounts; l; l = l->next)
+    {
+      GMount *mount = l->data;
+      GVolume *volume;
+      GDrive *drive;
+
+      drive = g_mount_get_drive (mount);
+      volume = g_mount_get_volume (mount);
+      do_mount_tests (drive, volume, mount);
+      g_object_unref (drive);
+      g_object_unref (volume);
+    }
+
+  g_list_foreach (mounts, (GFunc)g_object_unref,  NULL);
+  g_list_free (mounts);
+}
+int
+main (int argc, char *argv[])
+{
+  gboolean ret;
+
+  g_type_init ();
+
+  g_test_init (&argc, &argv, NULL);
+
+  monitor = g_volume_monitor_get ();
+
+  g_test_add_func ("/volumemonitor/connected_drives", test_connected_drives);
+  g_test_add_func ("/volumemonitor/volumes", test_volumes);
+  g_test_add_func ("/volumemonitor/mounts", test_mounts);
+
+  ret = g_test_run ();
+
+  g_object_unref (monitor);
+
+  return ret;
+}
+
diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am
index 0205194..ade1951 100644
--- a/glib/tests/Makefile.am
+++ b/glib/tests/Makefile.am
@@ -77,6 +77,9 @@ sequence_LDADD      = $(progs_ldadd)
 TEST_PROGS         += scannerapi
 scannerapi_LDADD    = $(progs_ldadd)
 
+TEST_PROGS         += shell
+shell_LDADD         = $(progs_ldadd)
+
 TEST_PROGS         += collate
 collate_LDADD       = $(progs_ldadd)
 
diff --git a/glib/tests/shell.c b/glib/tests/shell.c
new file mode 100644
index 0000000..e3f21bd
--- /dev/null
+++ b/glib/tests/shell.c
@@ -0,0 +1,229 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/*
+ * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GLib Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
+#undef G_DISABLE_ASSERT
+#undef G_LOG_DOMAIN
+
+#include <glib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+typedef struct _CmdlineTest CmdlineTest;
+
+struct _CmdlineTest
+{
+  const gchar *cmdline;
+  gint argc;
+  const gchar *argv[10];
+  gint error_code;
+};
+
+static CmdlineTest cmdline_tests[] =
+{
+  { "foo bar", 2, { "foo", "bar", NULL }, -1 },
+  { "foo 'bar'", 2, { "foo", "bar", NULL }, -1 },
+  { "foo \"bar\"", 2, { "foo", "bar", NULL }, -1 },
+  { "foo '' 'bar'", 3, { "foo", "", "bar", NULL }, -1 },
+  { "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"", 2, { "foo", "barbazblahfoo'blahboo", NULL }, -1 },
+  { "foo \t \tblah\tfoo\t\tbar  baz", 5, { "foo", "blah", "foo", "bar", "baz", NULL }, -1 },
+  { "foo '    spaces more spaces lots of     spaces in this   '  \t", 2, { "foo", "    spaces more spaces lots of     spaces in this   ", NULL }, -1 },
+  { "foo \\\nbar", 2, { "foo", "bar", NULL }, -1 },
+  { "foo '' ''", 3, { "foo", "", "", NULL }, -1 },
+  { "foo \\\" la la la", 5, { "foo", "\"", "la", "la", "la", NULL }, -1 },
+  { "foo \\ foo woo woo\\ ", 4, { "foo", " foo", "woo", "woo ", NULL }, -1 },
+  { "foo \"yada yada \\$\\\"\"", 2, { "foo", "yada yada $\"", NULL }, -1 },
+  { "foo \"c:\\\\\"", 2, { "foo", "c:\\", NULL }, -1 },
+  { "foo # bla bla bla\n bar", 2, { "foo", "bar", NULL }, -1 },
+  { "foo bar \\", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
+  { "foo 'bar baz", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
+  { "foo '\"bar\" baz", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
+  { "", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
+  { "  ", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
+  { "# foo bar", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING }
+};
+
+static gboolean
+strv_equal (const gchar **a, const gchar **b)
+{
+  gint i;
+
+  if (g_strv_length ((gchar**)a) != g_strv_length ((gchar **)b))
+    return FALSE;
+
+  for (i = 0; a[i]; i++)
+    if (g_strcmp0 (a[i], b[i]) != 0)
+      return FALSE;
+
+  return TRUE;
+}
+
+static void
+do_cmdline_test (gconstpointer d)
+{
+  const CmdlineTest *test = d;
+  gint argc;
+  gchar **argv;
+  GError *err;
+  gboolean res;
+
+  err = NULL;
+  res = g_shell_parse_argv (test->cmdline, &argc, &argv, &err);
+  if (test->error_code == -1)
+    {
+      g_assert (res);
+      g_assert_cmpint (argc, ==, test->argc);
+      g_assert (strv_equal (argv, (gchar **)test->argv));
+      g_assert_no_error (err);
+    }
+  else
+    {
+      g_assert (!res);
+      g_assert_error (err, G_SHELL_ERROR, test->error_code);
+    }
+
+  if (err)
+    g_error_free (err);
+  if (res)
+    g_strfreev (argv);
+}
+
+typedef struct _QuoteTest QuoteTest;
+
+struct _QuoteTest
+{
+  const gchar *in;
+  const gchar *out;
+};
+
+static QuoteTest quote_tests[] =
+{
+  { "", "''" },
+  { "a", "'a'" },
+  { "(", "'('" },
+  { "'", "''\\'''" },
+  { "'a", "''\\''a'" },
+  { "a'", "'a'\\'''" },
+  { "a'a", "'a'\\''a'" }
+};
+
+static void
+do_quote_test (gconstpointer d)
+{
+  const QuoteTest *test = d;
+  gchar *out;
+
+  out = g_shell_quote (test->in);
+  g_assert_cmpstr (out, ==, test->out);
+  g_free (out);
+}
+
+typedef struct _UnquoteTest UnquoteTest;
+
+struct _UnquoteTest
+{
+  const gchar *in;
+  const gchar *out;
+  gint error_code;
+};
+
+static UnquoteTest unquote_tests[] =
+{
+  { "", "", -1 },
+  { "a", "a", -1 },
+  { "'a'", "a", -1 },
+  { "'('", "(", -1 },
+  { "''\\'''", "'", -1 },
+  { "''\\''a'", "'a", -1 },
+  { "'a'\\'''", "a'", -1 },
+  { "'a'\\''a'", "a'a", -1 },
+  { "\\\\", "\\", -1 },
+  { "\\\n", "", -1 },
+  { "'\\''", NULL, G_SHELL_ERROR_BAD_QUOTING },
+  { "\"\\\"\"", "\"", -1 },
+  { "\"", NULL, G_SHELL_ERROR_BAD_QUOTING },
+  { "'", NULL, G_SHELL_ERROR_BAD_QUOTING },
+  { "\x22\\\\\"", "\\", -1 },
+  { "\x22\\`\"", "`", -1 },
+  { "\x22\\$\"", "$", -1 },
+  { "\x22\\\n\"", "\n", -1 },
+  { "\"\\'\"", "\\'", -1 },
+  { "\x22\\\r\"", "\\\r", -1 },
+  { "\x22\\n\"", "\\n", -1 }
+};
+
+static void
+do_unquote_test (gconstpointer d)
+{
+  const UnquoteTest *test = d;
+  gchar *out;
+  GError *error;
+
+  error = NULL;
+  out = g_shell_unquote (test->in, &error);
+  g_assert_cmpstr (out, ==, test->out);
+  if (test->error_code == -1)
+    g_assert_no_error (error);
+  else
+    g_assert_error (error, G_SHELL_ERROR, test->error_code);
+
+  g_free (out);
+  if (error)
+    g_error_free (error);
+}
+
+int
+main (int   argc, char *argv[])
+{
+  gint i;
+  gchar *path;
+
+  g_test_init (&argc, &argv, NULL);
+
+  for (i = 0; i < G_N_ELEMENTS (cmdline_tests); i++)
+    {
+      path = g_strdup_printf ("/shell/cmdline/%d", i);
+      g_test_add_data_func (path, &cmdline_tests[i], do_cmdline_test);
+      g_free (path);
+    }
+
+  for (i = 0; i < G_N_ELEMENTS (quote_tests); i++)
+    {
+      path = g_strdup_printf ("/shell/quote/%d", i);
+      g_test_add_data_func (path, &quote_tests[i], do_quote_test);
+      g_free (path);
+    }
+
+  for (i = 0; i < G_N_ELEMENTS (unquote_tests); i++)
+    {
+      path = g_strdup_printf ("/shell/unquote/%d", i);
+      g_test_add_data_func (path, &unquote_tests[i], do_unquote_test);
+      g_free (path);
+    }
+
+  return g_test_run ();
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e07f982..768a7a8 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -104,7 +104,6 @@ test_programs =					\
 	asyncqueue-test				\
 	qsort-test				\
 	relation-test				\
-	shell-test				\
 	slice-test				\
 	slice-color				\
 	slice-concurrent			\
@@ -155,7 +154,6 @@ onceinit_LDADD = $(thread_ldadd)
 asyncqueue_test_LDADD = $(thread_ldadd)
 qsort_test_LDADD = $(progs_ldadd)
 relation_test_LDADD = $(progs_ldadd)
-shell_test_LDADD = $(progs_ldadd)
 slice_test_SOURCES = slice-test.c memchunks.c
 slice_test_LDADD = $(thread_ldadd)
 slice_color_SOURCES = slice-color.c memchunks.c



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