[pango/test-coverage: 4/22] Add better attribute test infrastructure




commit e028b8997775ddade1c4d94156b0b7c7262959a8
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jun 26 11:01:35 2021 -0400

    Add better attribute test infrastructure
    
    Add a way to deserialize a PangoAttrList from a string,
    for less cumbersome tests.
    
    And use it in testattributes.

 tests/test-common.c    | 198 +++++++++++++++++++++++++++++++++++
 tests/test-common.h    |   3 +
 tests/testattributes.c | 274 +++++++++----------------------------------------
 3 files changed, 249 insertions(+), 226 deletions(-)
---
diff --git a/tests/test-common.c b/tests/test-common.c
index 514058cc..18ee4a40 100644
--- a/tests/test-common.c
+++ b/tests/test-common.c
@@ -201,6 +201,204 @@ print_attributes (GSList *attrs, GString *string)
     }
 }
 
+static PangoAttribute *
+attribute_from_string (const char *string)
+{
+  char *s, *p;
+  PangoAttribute *attr;
+  long start, end;
+  GEnumClass *class;
+  int i;
+  PangoColor color;
+
+  s = string;
+  g_assert (*s == '[');
+
+  s++;
+  start = strtol (s, &p, 10);
+  g_assert (p > s);
+  g_assert (*p == ',');
+  s = p + 1;
+
+  g_assert (start >= 0);
+
+  end = strtol (s, &p, 10);
+  g_assert (p > s);
+  g_assert (*p == ']');
+  s = p + 1;
+
+  if (end == -1)
+    end = G_MAXUINT;
+
+  g_assert (start >= 0);
+  g_assert (end >= 0);
+
+  class = g_type_class_ref (pango_attr_type_get_type ());
+
+  for (i = 0; i < class->n_values; i++)
+    {
+      if (g_str_has_prefix (s, class->values[i].value_nick))
+        break;
+    }
+
+  g_assert (i < class->n_values);
+
+  s += strlen (class->values[i].value_nick);
+  g_assert (*s == '=');
+  s++;
+
+  switch (class->values[i].value)
+    {
+    case PANGO_ATTR_LANGUAGE:
+      attr = pango_attr_language_new (pango_language_from_string (s));
+      break;
+    case PANGO_ATTR_FAMILY:
+      attr = pango_attr_family_new (s);
+      break;
+    case PANGO_ATTR_FONT_FEATURES:
+      attr = pango_attr_font_features_new (s);
+      break;
+    case PANGO_ATTR_STYLE:
+      attr = pango_attr_style_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_WEIGHT:
+      attr = pango_attr_weight_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_VARIANT:
+      attr = pango_attr_variant_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_STRETCH:
+      attr = pango_attr_stretch_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_SIZE:
+      attr = pango_attr_size_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_ABSOLUTE_SIZE:
+      attr = pango_attr_size_new_absolute (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_UNDERLINE:
+      attr = pango_attr_underline_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_OVERLINE:
+      attr = pango_attr_overline_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_STRIKETHROUGH:
+      attr = pango_attr_strikethrough_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_RISE:
+      attr = pango_attr_rise_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_FALLBACK:
+      attr = pango_attr_fallback_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_LETTER_SPACING:
+      attr = pango_attr_letter_spacing_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_GRAVITY:
+      attr = pango_attr_gravity_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_GRAVITY_HINT:
+      attr = pango_attr_gravity_hint_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_FOREGROUND_ALPHA:
+      attr = pango_attr_foreground_alpha_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_BACKGROUND_ALPHA:
+      attr = pango_attr_background_alpha_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_ALLOW_BREAKS:
+      attr = pango_attr_allow_breaks_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_INSERT_HYPHENS:
+      attr = pango_attr_insert_hyphens_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_SHOW:
+      attr = pango_attr_show_new (strtol (s, &p, 10));
+      break;
+    case PANGO_ATTR_FONT_DESC:
+      {
+        PangoFontDescription *desc = pango_font_description_from_string (s);
+        attr = pango_attr_font_desc_new (desc);
+        pango_font_description_free (desc);
+      }
+      break;
+    case PANGO_ATTR_FOREGROUND:
+      {
+        pango_color_parse (&color, s);
+        attr = pango_attr_foreground_new (color.red, color.green, color.blue);
+      }
+      break;
+    case PANGO_ATTR_BACKGROUND:
+      {
+        pango_color_parse (&color, s);
+        attr = pango_attr_background_new (color.red, color.green, color.blue);
+      }
+      break;
+    case PANGO_ATTR_UNDERLINE_COLOR:
+      {
+        pango_color_parse (&color, s);
+        attr = pango_attr_underline_color_new (color.red, color.green, color.blue);
+      }
+      break;
+    case PANGO_ATTR_OVERLINE_COLOR:
+      {
+        pango_color_parse (&color, s);
+        attr = pango_attr_overline_color_new (color.red, color.green, color.blue);
+      }
+      break;
+    case PANGO_ATTR_STRIKETHROUGH_COLOR:
+      {
+        pango_color_parse (&color, s);
+        attr = pango_attr_strikethrough_color_new (color.red, color.green, color.blue);
+      }
+      break;
+    case PANGO_ATTR_SHAPE:
+      {
+        PangoRectangle rect = { 0, };
+        attr = pango_attr_shape_new (&rect, &rect);
+      }
+      break;
+    case PANGO_ATTR_SCALE:
+      attr = pango_attr_scale_new (strtod (s, &p));
+      break;
+    default:
+      g_assert_not_reached ();
+      break;
+    }
+
+  g_type_class_unref (class);
+
+  attr->start_index = start;
+  attr->end_index = end;
+
+  return attr;
+}
+
+PangoAttrList *
+attributes_from_string (const char *string)
+{
+  PangoAttrList *attrs;
+  char **lines;
+
+  attrs = pango_attr_list_new ();
+
+  lines = g_strsplit (string, "\n", 0);
+  for (int i = 0; lines[i]; i++)
+    {
+      PangoAttribute *attr;
+
+      if (lines[i][0] == '\0')
+        continue;
+
+      attr = attribute_from_string (lines[i]);
+      g_assert (attr);
+      pango_attr_list_insert (attrs, attr);
+    }
+  g_strfreev (lines);
+
+  return attrs;
+}
+
 const char *
 get_script_name (GUnicodeScript s)
 {
diff --git a/tests/test-common.h b/tests/test-common.h
index 28a850ae..a357c2a9 100644
--- a/tests/test-common.h
+++ b/tests/test-common.h
@@ -15,6 +15,9 @@ void print_attributes (GSList        *attrs,
 void print_attr_list (PangoAttrList  *attrs,
                       GString        *string);
 
+PangoAttrList *
+attributes_from_string (const char *string);
+
 const char *get_script_name (GUnicodeScript s);
 
 
diff --git a/tests/testattributes.c b/tests/testattributes.c
index 66d149cd..2371e50f 100644
--- a/tests/testattributes.c
+++ b/tests/testattributes.c
@@ -133,7 +133,7 @@ assert_attr_iterator (PangoAttrIterator *iter,
 static void
 test_list (void)
 {
-  PangoAttrList *list;
+  PangoAttrList *list, *list2;
   PangoAttribute *attr;
 
   list = pango_attr_list_new ();
@@ -145,6 +145,12 @@ test_list (void)
   attr = pango_attr_size_new (30);
   pango_attr_list_insert (list, attr);
 
+  list2 = attributes_from_string ("[0,-1]size=10\n"
+                                  "[0,-1]size=20\n"
+                                  "[0,-1]size=30\n");
+  g_assert_true (pango_attr_list_equal (list, list2));
+  pango_attr_list_unref (list2);
+
   assert_attr_list (list, "[0,-1]size=10\n"
                           "[0,-1]size=20\n"
                           "[0,-1]size=30\n");
@@ -179,24 +185,9 @@ test_list_change (void)
   PangoAttrList *list;
   PangoAttribute *attr;
 
-  list = pango_attr_list_new ();
-
-  attr = pango_attr_size_new (10);
-  attr->start_index = 0;
-  attr->end_index = 10;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_size_new (20);
-  attr->start_index = 20;
-  attr->end_index = 30;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
-  attr->start_index = 0;
-  attr->end_index = 30;
-  pango_attr_list_insert (list, attr);
-
-  assert_attr_list (list, "[0,10]size=10\n"
-                          "[0,30]weight=700\n"
-                          "[20,30]size=20\n");
+  list = attributes_from_string ("[0,10]size=10\n"
+                                 "[0,30]weight=700\n"
+                                 "[20,30]size=20\n");
 
   /* simple insertion with pango_attr_list_change */
   attr = pango_attr_variant_new (PANGO_VARIANT_SMALL_CAPS);
@@ -244,25 +235,10 @@ test_list_splice (void)
   PangoAttrList *base;
   PangoAttrList *list;
   PangoAttrList *other;
-  PangoAttribute *attr;
 
-  base = pango_attr_list_new ();
-  attr = pango_attr_size_new (10);
-  attr->start_index = 0;
-  attr->end_index = -1;
-  pango_attr_list_insert (base, attr);
-  attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
-  attr->start_index = 10;
-  attr->end_index = 15;
-  pango_attr_list_insert (base, attr);
-  attr = pango_attr_variant_new (PANGO_VARIANT_SMALL_CAPS);
-  attr->start_index = 20;
-  attr->end_index = 30;
-  pango_attr_list_insert (base, attr);
-
-  assert_attr_list (base, "[0,-1]size=10\n"
-                          "[10,15]weight=700\n"
-                          "[20,30]variant=1\n");
+  base = attributes_from_string ("[0,-1]size=10\n"
+                                 "[10,15]weight=700\n"
+                                 "[20,30]variant=1\n");
 
   /* splice in an empty list */
   list = pango_attr_list_copy (base);
@@ -278,15 +254,9 @@ test_list_splice (void)
 
   /* splice in some attributes */
   list = pango_attr_list_copy (base);
-  other = pango_attr_list_new ();
-  attr = pango_attr_size_new (20);
-  attr->start_index = 0;
-  attr->end_index = 3;
-  pango_attr_list_insert (other, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 2;
-  attr->end_index = 4;
-  pango_attr_list_insert (other, attr);
+
+  other = attributes_from_string ("[0,3]size=20\n"
+                                  "[2,4]stretch=2\n");
 
   pango_attr_list_splice (list, other, 11, 5);
 
@@ -344,23 +314,9 @@ test_list_splice3 (void)
 {
   PangoAttrList *list;
   PangoAttrList *other;
-  PangoAttribute *attr;
 
-  list = pango_attr_list_new ();
-  other = pango_attr_list_new ();
-
-  attr = pango_attr_variant_new (PANGO_VARIANT_SMALL_CAPS);
-  attr->start_index = 10;
-  attr->end_index = 30;
-  pango_attr_list_insert (list, attr);
-
-  attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
-  attr->start_index = PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING;
-  attr->end_index = PANGO_ATTR_INDEX_TO_TEXT_END;
-  pango_attr_list_insert (other, attr);
-
-  assert_attr_list (list, "[10,30]variant=1\n");
-  assert_attr_list (other, "[0,-1]weight=700\n");
+  list = attributes_from_string ("[10,30]variant=1\n");
+  other = attributes_from_string ("[0,-1]weight=700\n");
 
   pango_attr_list_splice (list, other, 20, 5);
 
@@ -391,22 +347,10 @@ test_list_filter (void)
 {
   PangoAttrList *list;
   PangoAttrList *out;
-  PangoAttribute *attr;
-
-  list = pango_attr_list_new ();
-  attr = pango_attr_size_new (10);
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 10;
-  attr->end_index = 20;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
-  attr->start_index = 20;
-  pango_attr_list_insert (list, attr);
 
-  assert_attr_list (list, "[0,-1]size=10\n"
-                          "[10,20]stretch=2\n"
-                          "[20,-1]weight=700\n");
+  list = attributes_from_string ("[0,-1]size=10\n"
+                                 "[10,20]stretch=2\n"
+                                 "[20,-1]weight=700\n");
 
   out = pango_attr_list_filter (list, never_true, NULL);
   g_assert_null (out);
@@ -651,35 +595,12 @@ static void
 test_list_update (void)
 {
   PangoAttrList *list;
-  PangoAttribute *attr;
 
-  list = pango_attr_list_new ();
-  attr = pango_attr_size_new (10 * PANGO_SCALE);
-  attr->start_index = 10;
-  attr->end_index = 11;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_rise_new (100);
-  attr->start_index = 0;
-  attr->end_index = 200;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_family_new ("Times");
-  attr->start_index = 5;
-  attr->end_index = 15;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_fallback_new (FALSE);
-  attr->start_index = 11;
-  attr->end_index = 100;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 30;
-  attr->end_index = 60;
-  pango_attr_list_insert (list, attr);
-
-  assert_attr_list (list, "[0,200]rise=100\n"
-                          "[5,15]family=Times\n"
-                          "[10,11]size=10240\n"
-                          "[11,100]fallback=0\n"
-                          "[30,60]stretch=2\n");
+  list = attributes_from_string ("[0,200]rise=100\n"
+                                 "[5,15]family=Times\n"
+                                 "[10,11]size=10240\n"
+                                 "[11,100]fallback=0\n"
+                                 "[30,60]stretch=2\n");
 
   pango_attr_list_update (list, 8, 10, 20);
 
@@ -787,33 +708,11 @@ test_insert (void)
   PangoAttrList *list;
   PangoAttribute *attr;
 
-  list = pango_attr_list_new ();
-  attr = pango_attr_size_new (10 * PANGO_SCALE);
-  attr->start_index = 10;
-  attr->end_index = 11;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_rise_new (100);
-  attr->start_index = 0;
-  attr->end_index = 200;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_family_new ("Times");
-  attr->start_index = 5;
-  attr->end_index = 15;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_fallback_new (FALSE);
-  attr->start_index = 11;
-  attr->end_index = 100;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 30;
-  attr->end_index = 60;
-  pango_attr_list_insert (list, attr);
-
-  assert_attr_list (list, "[0,200]rise=100\n"
-                          "[5,15]family=Times\n"
-                          "[10,11]size=10240\n"
-                          "[11,100]fallback=0\n"
-                          "[30,60]stretch=2\n");
+  list = attributes_from_string ("[0,200]rise=100\n"
+                                 "[5,15]family=Times\n"
+                                 "[10,11]size=10240\n"
+                                 "[11,100]fallback=0\n"
+                                 "[30,60]stretch=2\n");
 
   attr = pango_attr_family_new ("Times");
   attr->start_index = 10;
@@ -847,43 +746,13 @@ test_insert2 (void)
   PangoAttrList *list;
   PangoAttribute *attr;
 
-  list = pango_attr_list_new ();
-  attr = pango_attr_size_new (10 * PANGO_SCALE);
-  attr->start_index = 10;
-  attr->end_index = 11;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_rise_new (100);
-  attr->start_index = 0;
-  attr->end_index = 200;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_family_new ("Times");
-  attr->start_index = 5;
-  attr->end_index = 15;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_family_new ("Times");
-  attr->start_index = 20;
-  attr->end_index = 30;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_family_new ("Futura");
-  attr->start_index = 30;
-  attr->end_index = 40;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_fallback_new (FALSE);
-  attr->start_index = 11;
-  attr->end_index = 100;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 30;
-  attr->end_index = 60;
-  pango_attr_list_insert (list, attr);
-
-  assert_attr_list (list, "[0,200]rise=100\n"
-                          "[5,15]family=Times\n"
-                          "[10,11]size=10240\n"
-                          "[11,100]fallback=0\n"
-                          "[20,30]family=Times\n"
-                          "[30,40]family=Futura\n"
-                          "[30,60]stretch=2\n");
+  list = attributes_from_string ("[0,200]rise=100\n"
+                                 "[5,15]family=Times\n"
+                                 "[10,11]size=10240\n"
+                                 "[11,100]fallback=0\n"
+                                 "[20,30]family=Times\n"
+                                 "[30,40]family=Futura\n"
+                                 "[30,60]stretch=2\n");
 
   attr = pango_attr_family_new ("Times");
   attr->start_index = 10;
@@ -914,53 +783,16 @@ test_merge (void)
 {
   PangoAttrList *list;
   PangoAttrList *list2;
-  PangoAttribute *attr;
-
-  list = pango_attr_list_new ();
-  attr = pango_attr_size_new (10 * PANGO_SCALE);
-  attr->start_index = 10;
-  attr->end_index = 11;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_rise_new (100);
-  attr->start_index = 0;
-  attr->end_index = 200;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_family_new ("Times");
-  attr->start_index = 5;
-  attr->end_index = 15;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_fallback_new (FALSE);
-  attr->start_index = 11;
-  attr->end_index = 100;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 30;
-  attr->end_index = 60;
-  pango_attr_list_insert (list, attr);
 
-  assert_attr_list (list, "[0,200]rise=100\n"
-                          "[5,15]family=Times\n"
-                          "[10,11]size=10240\n"
-                          "[11,100]fallback=0\n"
-                          "[30,60]stretch=2\n");
+  list = attributes_from_string ("[0,200]rise=100\n"
+                                 "[5,15]family=Times\n"
+                                 "[10,11]size=10240\n"
+                                 "[11,100]fallback=0\n"
+                                 "[30,60]stretch=2\n");
 
-  list2 = pango_attr_list_new ();
-  attr = pango_attr_size_new (10 * PANGO_SCALE);
-  attr->start_index = 11;
-  attr->end_index = 13;
-  pango_attr_list_insert (list2, attr);
-  attr = pango_attr_size_new (11 * PANGO_SCALE);
-  attr->start_index = 13;
-  attr->end_index = 15;
-  pango_attr_list_insert (list2, attr);
-  attr = pango_attr_size_new (12 * PANGO_SCALE);
-  attr->start_index = 40;
-  attr->end_index = 50;
-  pango_attr_list_insert (list2, attr);
-
-  assert_attr_list (list2, "[11,13]size=10240\n"
-                           "[13,15]size=11264\n"
-                           "[40,50]size=12288\n");
+  list2 = attributes_from_string ("[11,13]size=10240\n"
+                                  "[13,15]size=11264\n"
+                                  "[40,50]size=12288\n");
 
   pango_attr_list_filter (list2, attr_list_merge_filter, list);
 
@@ -985,18 +817,8 @@ test_merge2 (void)
   PangoAttrList *list;
   PangoAttribute *attr;
 
-  list = pango_attr_list_new ();
-  attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
-  attr->start_index = 0;
-  attr->end_index = 10;
-  pango_attr_list_insert (list, attr);
-  attr = pango_attr_foreground_new (0, 0, 0xffff);
-  attr->start_index = 0;
-  attr->end_index = 10;
-  pango_attr_list_insert (list, attr);
-
-  assert_attr_list (list, "[0,10]underline=1\n"
-                          "[0,10]foreground=#00000000ffff\n");
+  list = attributes_from_string ("[0,10]underline=1\n"
+                                 "[0,10]foreground=#00000000ffff\n");
 
   attr = pango_attr_foreground_new (0xffff, 0, 0);
   attr->start_index = 2;


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