[librsvg/wip/otte: 19/39] tests: Run crash tests on all files in directory



commit 36e5b164fda0bee3aa5dc50be4f061a3d74bbfbe
Author: Benjamin Otte <otte redhat com>
Date:   Wed Oct 14 19:16:15 2015 +0200

    tests: Run crash tests on all files in directory
    
    Instead of manually hardcoding them.
    Code copied from gdk-pixbuf.

 tests/Makefile.am  |    2 +-
 tests/crash.c      |   34 +++++++---------------
 tests/test-utils.c |   80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-utils.h |   11 ++++++-
 4 files changed, 101 insertions(+), 26 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 59c39f3..014ece4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -38,7 +38,7 @@ libtest_utils_la_LIBADD = \
        $(LIBM)
 
 EXTRA_DIST =                                           \
-       fixtures/crash/bug620238.svg                    \
+       $(wildcard $(srcdir)/fixtures/crash/*.svg)      \
        fixtures/dimensions/bug564527.svg               \
        fixtures/dimensions/bug614018.svg               \
        fixtures/dimensions/bug612951.svg               \
diff --git a/tests/crash.c b/tests/crash.c
index 101ad9d..832f9be 100644
--- a/tests/crash.c
+++ b/tests/crash.c
@@ -5,47 +5,35 @@
 #include "rsvg-compat.h"
 #include "test-utils.h"
 
-typedef struct _FixtureData
-{
-    const gchar *test_name;
-    const gchar *file_path;
-} FixtureData;
-
 static void
-test_crash (FixtureData *fixture)
+test_crash (gconstpointer data)
 {
+    GFile *file = G_FILE (data);
     RsvgHandle *handle;
-    gchar *target_file;
     GError *error = NULL;
 
-    target_file = g_build_filename (test_utils_get_test_data_path (),
-                                    fixture->file_path, NULL);
-    handle = rsvg_handle_new_from_file (target_file, &error);
-    g_free (target_file);
+    handle = rsvg_handle_new_from_gfile_sync (file, RSVG_HANDLE_FLAGS_NONE, NULL, &error);
     g_assert_no_error (error);
+    g_assert (handle != NULL);
 
     g_object_unref (handle);
 }
 
-static FixtureData fixtures[] =
-{
-    {"/crash/only style information", "crash/bug620238.svg"}
-};
-
-static const gint n_fixtures = G_N_ELEMENTS (fixtures);
-
 int
 main (int argc, char *argv[])
 {
-    gint i;
+    GFile *base, *crash;
     int result;
 
     RSVG_G_TYPE_INIT;
     g_test_init (&argc, &argv, NULL);
 
-    for (i = 0; i < n_fixtures; i++)
-        g_test_add_data_func (fixtures[i].test_name, &fixtures[i], (void*)test_crash);
-
+    base = g_file_new_for_path (test_utils_get_test_data_path ());
+    crash = g_file_get_child (base, "crash");
+    test_utils_add_test_for_all_files ("/crash", crash, crash, test_crash, NULL);
+    g_object_unref (base);
+    g_object_unref (crash);
+                                       
     result = g_test_run ();
 
     rsvg_cleanup ();
diff --git a/tests/test-utils.c b/tests/test-utils.c
index 7954da6..e2a8118 100644
--- a/tests/test-utils.c
+++ b/tests/test-utils.c
@@ -4,6 +4,8 @@
 #include "config.h"
 #include "test-utils.h"
 
+#include <string.h>
+
 static gchar *data_path = NULL;
 const gchar *
 test_utils_get_test_data_path (void)
@@ -19,3 +21,81 @@ test_utils_get_test_data_path (void)
     return data_path;
 }
 
+static int
+compare_files (gconstpointer a, gconstpointer b)
+{
+  GFile *file1 = G_FILE (a);
+  GFile *file2 = G_FILE (b);
+  char *uri1, *uri2;
+  int result;
+
+  uri1 = g_file_get_uri (file1);
+  uri2 = g_file_get_uri (file2);
+
+  result = strcmp (uri1, uri2);
+
+  g_free (uri1);
+  g_free (uri2);
+
+  return result;
+}
+
+void
+test_utils_add_test_for_all_files (const gchar   *prefix,
+                                   GFile         *base,
+                                   GFile         *file,
+                                   GTestDataFunc  test_func,
+                                   AddTestFunc    add_test_func)
+{
+  GFileEnumerator *enumerator;
+  GFileInfo *info;
+  GList *l, *files;
+  GError *error = NULL;
+
+
+  if (g_file_query_file_type (file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
+    {
+      gchar *test_path;
+      gchar *relative_path;
+
+      if (base)
+        relative_path = g_file_get_relative_path (base, file);
+      else
+        relative_path = g_file_get_path (file);
+
+      test_path = g_strconcat (prefix, "/", relative_path, NULL);
+      
+      g_test_add_data_func_full (test_path, g_object_ref (file), test_func, g_object_unref);
+      return;
+    }
+
+
+  enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
+  g_assert_no_error (error);
+  files = NULL;
+
+  while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
+    {
+      GFile *next_file = g_file_get_child (file, g_file_info_get_name (info));
+
+      if (add_test_func == NULL || add_test_func (next_file))
+        {
+          files = g_list_prepend (files, g_object_ref (next_file));
+        }
+
+      g_object_unref (next_file);
+      g_object_unref (info);
+    }
+  
+  g_assert_no_error (error);
+  g_object_unref (enumerator);
+
+  files = g_list_sort (files, compare_files);
+
+  for (l = files; l; l = l->next)
+    {
+      test_utils_add_test_for_all_files (prefix, base, l->data, test_func, add_test_func);
+    }
+
+  g_list_free_full (files, g_object_unref);
+}
diff --git a/tests/test-utils.h b/tests/test-utils.h
index 0b28764..8d7fce0 100644
--- a/tests/test-utils.h
+++ b/tests/test-utils.h
@@ -4,12 +4,19 @@
 #ifndef TEST_UTILS_H
 #define TEST_UTILS_H
 
-#include <glib.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS 
 
-const gchar *test_utils_get_test_data_path (void);
+typedef gboolean (* AddTestFunc) (GFile *file);
 
+const gchar *test_utils_get_test_data_path      (void);
+
+void         test_utils_add_test_for_all_files  (const gchar    *prefix,
+                                                 GFile          *base,
+                                                 GFile          *file,
+                                                 GTestDataFunc   test_func,
+                                                 AddTestFunc     add_test_func);
 G_END_DECLS
 
 #endif /* TEST_UTILS_H */


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