[pango/pango2: 261/301] Improve test font setup code




commit bc6e070dc195c89eb3d4d6e6ca8ae05395cd3100
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jun 15 16:06:13 2022 -0400

    Improve test font setup code
    
    Make it so that install_fonts() just works, regardless
    whether it is called before or after g_test_init().

 tests/test-break.c    | 16 ++--------------
 tests/test-common.c   | 27 ++++++++++++++-------------
 tests/test-common.h   |  4 ++--
 tests/test-fontsets.c | 24 +-----------------------
 tests/test-itemize.c  |  2 ++
 tests/test-layout.c   | 25 +------------------------
 tests/testserialize.c |  2 +-
 7 files changed, 23 insertions(+), 77 deletions(-)
---
diff --git a/tests/test-break.c b/tests/test-break.c
index f4f75a3b1..74c8a4c65 100644
--- a/tests/test-break.c
+++ b/tests/test-break.c
@@ -363,13 +363,11 @@ main (int argc, char *argv[])
 {
   GDir *dir;
   GError *error = NULL;
-  char *opt_fonts = NULL;
   const char *name;
   char *path;
   gboolean opt_legend = 0;
   GOptionContext *option_context;
   GOptionEntry entries[] = {
-    { "fonts", 0, 0, G_OPTION_ARG_FILENAME, &opt_fonts, "Fonts to use", "DIR" },
     { "hex-chars", 0, 0, G_OPTION_ARG_NONE, &opt_hex_chars, "Print all chars in hex", NULL },
     { "legend", 0, 0, G_OPTION_ARG_NONE, &opt_legend, "Explain the output", NULL },
     { NULL, 0 },
@@ -387,12 +385,8 @@ main (int argc, char *argv[])
     }
   g_option_context_free (option_context);
 
-  if (opt_fonts)
-    {
-      install_fonts (opt_fonts);
-      context = pango_context_new ();
-      g_free (opt_fonts);
-    }
+  install_fonts ();
+  context = pango_context_new ();
 
   if (opt_legend)
     {
@@ -424,12 +418,6 @@ main (int argc, char *argv[])
 
   g_test_init (&argc, &argv, NULL);
 
-  if (!opt_fonts)
-    {
-      install_fonts (NULL);
-      context = pango_context_new ();
-    }
-
   path = g_test_build_filename (G_TEST_DIST, "breaks", NULL);
   dir = g_dir_open (path, 0, &error);
   g_free (path);
diff --git a/tests/test-common.c b/tests/test-common.c
index 861a06d62..09bc9385b 100644
--- a/tests/test-common.c
+++ b/tests/test-common.c
@@ -414,20 +414,19 @@ add_synthetic_faces (PangoFontMap *map,
 }
 
 void
-install_fonts (const char *path)
+install_fonts (void)
 {
   PangoFontMap *map;
   GDir *dir;
   GError *error = NULL;
-  const char *name;
   GPtrArray *generic;
   GPtrArray *synthetic;
-  char *testpath = NULL;
+  char *path = NULL;
+  char *name_i = NULL;
 
   map = pango_font_map_new ();
 
-  if (!path)
-    path = testpath = g_test_build_filename (G_TEST_DIST, "fonts", NULL);
+  path = g_build_filename (g_getenv ("G_TEST_SRCDIR"), "fonts", NULL);
 
   dir = g_dir_open (path, 0, &error);
 
@@ -439,7 +438,7 @@ install_fonts (const char *path)
 
   while (TRUE)
     {
-      name = g_dir_read_name (dir);
+      const char *name = g_dir_read_name (dir);
       if (name == NULL)
         break;
 
@@ -447,9 +446,9 @@ install_fonts (const char *path)
           g_str_has_suffix (name, ".otf"))
         add_file (map, path, name);
       else if (g_str_has_suffix (name, ".generic"))
-        g_ptr_array_add (generic, (gpointer) name);
+        g_ptr_array_add (generic, g_strdup (name));
       else if (g_str_has_suffix (name, ".synthetic"))
-        g_ptr_array_add (synthetic, (gpointer) name);
+        g_ptr_array_add (synthetic, g_strdup (name));
     }
 
   /* Add generics and synthetics in a second path,
@@ -457,15 +456,17 @@ install_fonts (const char *path)
    */
   for (int i = 0; i < generic->len; i++)
     {
-      name = g_ptr_array_index (generic, i);
-      add_generic_family (map, path, name);
+      name_i = g_ptr_array_index (generic, i);
+      add_generic_family (map, path, name_i);
+      g_free (name_i);
     }
   g_ptr_array_free (generic, TRUE);
 
   for (int i = 0; i < synthetic->len; i++)
     {
-      name = g_ptr_array_index (synthetic, i);
-      add_synthetic_faces (map, path, name);
+      name_i = g_ptr_array_index (synthetic, i);
+      add_synthetic_faces (map, path, name_i);
+      g_free (name_i);
     }
   g_ptr_array_free (synthetic, TRUE);
 
@@ -475,7 +476,7 @@ install_fonts (const char *path)
   g_object_unref (map);
   g_dir_close (dir);
 
-  g_free (testpath);
+  g_free (path);
 }
 
 void
diff --git a/tests/test-common.h b/tests/test-common.h
index d29db299b..2141100d7 100644
--- a/tests/test-common.h
+++ b/tests/test-common.h
@@ -25,7 +25,7 @@ void print_attr_list (PangoAttrList  *attrs,
 
 const char *get_script_name (GUnicodeScript s);
 
-void install_fonts (const char *path);
-void dump_fonts (void);
+void    install_fonts      (void);
+void    dump_fonts         (void);
 
 #endif
diff --git a/tests/test-fontsets.c b/tests/test-fontsets.c
index 62062f844..6701d6475 100644
--- a/tests/test-fontsets.c
+++ b/tests/test-fontsets.c
@@ -121,31 +121,12 @@ main (int argc, char *argv[])
 {
   GDir *dir;
   GError *error = NULL;
-  char *opt_fonts = NULL;
   const char *name;
   char *path;
-  GOptionContext *option_context;
-  GOptionEntry entries[] = {
-    { "fonts", 0, 0, G_OPTION_ARG_FILENAME, &opt_fonts, "Fonts to use", "DIR" },
-    { NULL, 0 },
-  };
 
   setlocale (LC_ALL, "");
-  option_context = g_option_context_new ("");
-  g_option_context_add_main_entries (option_context, entries, NULL);
-  g_option_context_set_ignore_unknown_options (option_context, TRUE);
-  if (!g_option_context_parse (option_context, &argc, &argv, &error))
-    {
-      g_error ("failed to parse options: %s", error->message);
-      return 1;
-    }
-  g_option_context_free (option_context);
 
-  if (opt_fonts)
-    {
-      install_fonts (opt_fonts);
-      g_free (opt_fonts);
-    }
+  install_fonts ();
 
  /* allow to easily generate expected output for new test cases */
   if (argc > 1 && argv[1][0] != '-')
@@ -170,9 +151,6 @@ main (int argc, char *argv[])
 
   g_test_init (&argc, &argv, NULL);
 
-  if (!opt_fonts)
-    install_fonts (NULL);
-
   path = g_test_build_filename (G_TEST_DIST, "fontsets", NULL);
   dir = g_dir_open (path, 0, &error);
   g_free (path);
diff --git a/tests/test-itemize.c b/tests/test-itemize.c
index daefff17b..02ebf14d4 100644
--- a/tests/test-itemize.c
+++ b/tests/test-itemize.c
@@ -332,6 +332,8 @@ main (int argc, char *argv[])
   const char *name;
   char *path;
 
+  install_fonts ();
+
   context = pango_context_new ();
   pango_context_set_language (context, pango_language_from_string ("en-us"));
 
diff --git a/tests/test-layout.c b/tests/test-layout.c
index c99b94f35..54bfbfeb3 100644
--- a/tests/test-layout.c
+++ b/tests/test-layout.c
@@ -102,32 +102,12 @@ main (int argc, char *argv[])
 {
   GDir *dir;
   GError *error = NULL;
-  char *opt_fonts = NULL;
   const char *name;
   char *path;
-  GOptionContext *option_context;
-  GOptionEntry entries[] = {
-    { "fonts", 0, 0, G_OPTION_ARG_FILENAME, &opt_fonts, "Fonts to use", "DIR" },
-    { NULL, 0 },
-  };
 
   setlocale (LC_ALL, "");
-  option_context = g_option_context_new ("");
-  g_option_context_add_main_entries (option_context, entries, NULL);
-  g_option_context_set_ignore_unknown_options (option_context, TRUE);
 
-  if (!g_option_context_parse (option_context, &argc, &argv, &error))
-    {
-      g_error ("failed to parse options: %s", error->message);
-      return 1;
-    }
-  g_option_context_free (option_context);
-
-  if (opt_fonts)
-    {
-      install_fonts (opt_fonts);
-      g_free (opt_fonts);
-    }
+  install_fonts ();
 
   /* allow to easily generate expected output for new test cases */
   if (argc > 1 && argv[1][0] != '-')
@@ -163,9 +143,6 @@ main (int argc, char *argv[])
 
   g_test_init (&argc, &argv, NULL);
 
-  if (!opt_fonts)
-    install_fonts (NULL);
-
   path = g_test_build_filename (G_TEST_DIST, "layouts", NULL);
   dir = g_dir_open (path, 0, &error);
   g_free (path);
diff --git a/tests/testserialize.c b/tests/testserialize.c
index da2c78d75..0b8e78248 100644
--- a/tests/testserialize.c
+++ b/tests/testserialize.c
@@ -437,7 +437,7 @@ main (int argc, char *argv[])
 {
   g_test_init (&argc, &argv, NULL);
 
-  install_fonts (NULL);
+  install_fonts ();
 
   g_test_add_func ("/serialize/attr-list", test_serialize_attr_list);
   g_test_add_func ("/serialize/tab-array", test_serialize_tab_array);


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