[pango/matthiasc/for-master: 3/3] Improve attribute test infrastructure




commit 59be04d7104bfcef5ae34a2e467725f5ced9f983
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jul 9 13:41:48 2021 -0400

    Improve attribute test infrastructure
    
    Make attribute_from_string() parse enum attribute values
    in the same way as PangoMarkup, and use it in the
    attribute tests.
    
    Still to do: Do the same for flags. The flags parsing
    function isn't exported, so this needs our tests to be
    statically linked.

 tests/test-common.c    |  55 +++++++++++++++++----
 tests/test-common.h    |   3 ++
 tests/testattributes.c | 127 ++++++++++++++-----------------------------------
 3 files changed, 84 insertions(+), 101 deletions(-)
---
diff --git a/tests/test-common.c b/tests/test-common.c
index 3b8880e3..14e9941a 100644
--- a/tests/test-common.c
+++ b/tests/test-common.c
@@ -220,7 +220,9 @@ print_attributes (GSList *attrs, GString *string)
     }
 }
 
-static PangoAttribute *
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+
+PangoAttribute *
 attribute_from_string (const char *string)
 {
   char *s, *p;
@@ -229,6 +231,7 @@ attribute_from_string (const char *string)
   GEnumClass *class;
   int i;
   PangoColor color;
+  int val;
 
   s = string;
   g_assert (*s == '[');
@@ -278,16 +281,32 @@ attribute_from_string (const char *string)
       attr = pango_attr_font_features_new (s);
       break;
     case PANGO_ATTR_STYLE:
-      attr = pango_attr_style_new (strtol (s, &p, 10));
+      {
+        if (!pango_parse_enum (PANGO_TYPE_STYLE, s, &val, FALSE, NULL))
+          val = strtol (s, &p, 10);
+      attr = pango_attr_style_new (val);
+      }
       break;
     case PANGO_ATTR_WEIGHT:
-      attr = pango_attr_weight_new (strtol (s, &p, 10));
+      {
+        if (!pango_parse_enum (PANGO_TYPE_WEIGHT, s, &val, FALSE, NULL))
+          val = strtol (s, &p, 10);
+        attr = pango_attr_weight_new (val);
+      }
       break;
     case PANGO_ATTR_VARIANT:
-      attr = pango_attr_variant_new (strtol (s, &p, 10));
+      {
+        if (!pango_parse_enum (PANGO_TYPE_VARIANT, s, &val, FALSE, NULL))
+          val = strtol (s, &p, 10);
+        attr = pango_attr_variant_new (val);
+      }
       break;
     case PANGO_ATTR_STRETCH:
-      attr = pango_attr_stretch_new (strtol (s, &p, 10));
+      {
+        if (!pango_parse_enum (PANGO_TYPE_STRETCH, s, &val, FALSE, NULL))
+          val = strtol (s, &p, 10);
+        attr = pango_attr_stretch_new (val);
+      }
       break;
     case PANGO_ATTR_SIZE:
       attr = pango_attr_size_new (strtol (s, &p, 10));
@@ -296,10 +315,18 @@ attribute_from_string (const char *string)
       attr = pango_attr_size_new_absolute (strtol (s, &p, 10));
       break;
     case PANGO_ATTR_UNDERLINE:
-      attr = pango_attr_underline_new (strtol (s, &p, 10));
+      {
+        if (!pango_parse_enum (PANGO_TYPE_UNDERLINE, s, &val, FALSE, NULL))
+          val = strtol (s, &p, 10);
+        attr = pango_attr_underline_new (val);
+      }
       break;
     case PANGO_ATTR_OVERLINE:
-      attr = pango_attr_overline_new (strtol (s, &p, 10));
+      {
+        if (!pango_parse_enum (PANGO_TYPE_OVERLINE, s, &val, FALSE, NULL))
+          val = strtol (s, &p, 10);
+        attr = pango_attr_overline_new (val);
+      }
       break;
     case PANGO_ATTR_STRIKETHROUGH:
       attr = pango_attr_strikethrough_new (strtol (s, &p, 10));
@@ -314,10 +341,18 @@ attribute_from_string (const char *string)
       attr = pango_attr_letter_spacing_new (strtol (s, &p, 10));
       break;
     case PANGO_ATTR_GRAVITY:
-      attr = pango_attr_gravity_new (strtol (s, &p, 10));
+      {
+        if (!pango_parse_enum (PANGO_TYPE_GRAVITY, s, &val, FALSE, NULL))
+          val = strtol (s, &p, 10);
+        attr = pango_attr_gravity_new (val);
+      }
       break;
     case PANGO_ATTR_GRAVITY_HINT:
-      attr = pango_attr_gravity_hint_new (strtol (s, &p, 10));
+      {
+        if (!pango_parse_enum (PANGO_TYPE_GRAVITY_HINT, s, &val, FALSE, NULL))
+          val = strtol (s, &p, 10);
+        attr = pango_attr_gravity_hint_new (val);
+      }
       break;
     case PANGO_ATTR_FOREGROUND_ALPHA:
       attr = pango_attr_foreground_alpha_new (strtol (s, &p, 10));
@@ -393,6 +428,8 @@ attribute_from_string (const char *string)
   return attr;
 }
 
+G_GNUC_END_IGNORE_DEPRECATIONS
+
 PangoAttrList *
 attributes_from_string (const char *string)
 {
diff --git a/tests/test-common.h b/tests/test-common.h
index 22953f2d..e3f46afe 100644
--- a/tests/test-common.h
+++ b/tests/test-common.h
@@ -19,6 +19,9 @@ void print_attributes (GSList        *attrs,
 void print_attr_list (PangoAttrList  *attrs,
                       GString        *string);
 
+PangoAttribute *
+attribute_from_string (const char *string);
+
 PangoAttrList *
 attributes_from_string (const char *string);
 
diff --git a/tests/testattributes.c b/tests/testattributes.c
index 849e47cb..787303d9 100644
--- a/tests/testattributes.c
+++ b/tests/testattributes.c
@@ -178,17 +178,13 @@ test_list (void)
   list = pango_attr_list_new ();
 
   /* test that insertion respects start_index */
-  attr = pango_attr_size_new (10);
+  attr = attribute_from_string ("[0,-1]size=10");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_size_new (20);
-  attr->start_index = 10;
-  attr->end_index = 20;
+  attr = attribute_from_string ("[10,20]size=20");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_size_new (30);
+  attr = attribute_from_string ("[0,-1]size=30");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_size_new (40);
-  attr->start_index = 10;
-  attr->end_index = 40;
+  attr = attribute_from_string ("[10,40]size=40");
   pango_attr_list_insert_before (list, attr);
 
   assert_attr_list (list, "[0,-1]size=10\n"
@@ -209,9 +205,7 @@ test_list_change (void)
                                  "[20,30]size=20\n");
 
   /* no-op */
-  attr = pango_attr_variant_new (PANGO_VARIANT_SMALL_CAPS);
-  attr->start_index = 10;
-  attr->end_index = 10;
+  attr = attribute_from_string ("[10,10]variant=small-caps");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,10]size=10\n"
@@ -219,9 +213,7 @@ test_list_change (void)
                           "[20,30]size=20\n");
 
   /* simple insertion with pango_attr_list_change */
-  attr = pango_attr_variant_new (PANGO_VARIANT_SMALL_CAPS);
-  attr->start_index = 10;
-  attr->end_index = 20;
+  attr = attribute_from_string ("[10,20]variant=small-caps");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,10]size=10\n"
@@ -230,9 +222,7 @@ test_list_change (void)
                           "[20,30]size=20\n");
 
   /* insertion with splitting */
-  attr = pango_attr_weight_new (PANGO_WEIGHT_LIGHT);
-  attr->start_index = 15;
-  attr->end_index = 20;
+  attr = attribute_from_string ("[15,20]weight=light");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,10]size=10\n"
@@ -243,9 +233,7 @@ test_list_change (void)
                           "[20,30]weight=700\n");
 
   /* insertion with joining */
-  attr = pango_attr_size_new (20);
-  attr->start_index = 5;
-  attr->end_index = 20;
+  attr = attribute_from_string ("[5,20]size=20");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,5]size=10\n"
@@ -269,9 +257,7 @@ test_list_change2 (void)
                                  "[18,23]weight=700\n");
 
   /* insertion with joining */
-  attr = pango_attr_weight_new (700);
-  attr->start_index = 0;
-  attr->end_index = 29;
+  attr = attribute_from_string ("[0,29]weight=700");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,29]weight=700\n");
@@ -338,9 +324,7 @@ test_list_splice2 (void)
 
   g_assert_null (pango_attr_list_get_attributes (list));
 
-  attr = pango_attr_size_new (10);
-  attr->start_index = 0;
-  attr->end_index = -1;
+  attr = attribute_from_string ("[0,-1]size=10");
   pango_attr_list_insert (other, attr);
 
   pango_attr_list_splice (list, other, 11, 5);
@@ -435,14 +419,11 @@ test_iter (void)
   pango_attr_list_unref (list);
 
   list = pango_attr_list_new ();
-  attr = pango_attr_size_new (10);
+  attr = attribute_from_string ("[0,-1]size=10");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 10;
-  attr->end_index = 30;
+  attr = attribute_from_string ("[10,30]stretch=condensed");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
-  attr->start_index = 20;
+  attr = attribute_from_string ("[20,-1]weight=bold");
   pango_attr_list_insert (list, attr);
 
   iter = pango_attr_list_get_iterator (list);
@@ -488,12 +469,9 @@ test_iter_get (void)
   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 = 30;
+  attr = attribute_from_string ("[10,30]stretch=condensed");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
-  attr->start_index = 20;
+  attr = attribute_from_string ("[20,-1]weight=bold");
   pango_attr_list_insert (list, attr);
 
   iter = pango_attr_list_get_iterator (list);
@@ -529,25 +507,17 @@ test_iter_get_font (void)
   list = pango_attr_list_new ();
   attr = pango_attr_size_new (10 * PANGO_SCALE);
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_family_new ("Times");
+  attr = attribute_from_string ("[0,-1]family=Times");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 10;
-  attr->end_index = 30;
+  attr = attribute_from_string ("[10,30]stretch=condensed");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_size_new_absolute (10 * PANGO_SCALE);
-  attr->start_index = 10;
-  attr->end_index = 20;
+  attr = attribute_from_string ("[10,20]absolute-size=10240");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_language_new (pango_language_from_string ("ja-JP"));
-  attr->start_index = 10;
-  attr->end_index = 20;
+  attr = attribute_from_string ("[10,20]language=ja-JP");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_rise_new (100);
-  attr->start_index = 20;
+  attr = attribute_from_string ("[20,-1]rise=100");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_fallback_new (FALSE);
-  attr->start_index = 20;
+  attr = attribute_from_string ("[20,-1]fallback=0");
   pango_attr_list_insert (list, attr);
 
   iter = pango_attr_list_get_iterator (list);
@@ -598,21 +568,15 @@ test_iter_get_attrs (void)
   list = pango_attr_list_new ();
   attr = pango_attr_size_new (10 * PANGO_SCALE);
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_family_new ("Times");
+  attr = attribute_from_string ("[0,-1]family=Times");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 10;
-  attr->end_index = 30;
+  attr = attribute_from_string ("[10,30]stretch=condensed");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_language_new (pango_language_from_string ("ja-JP"));
-  attr->start_index = 10;
-  attr->end_index = 20;
+  attr = attribute_from_string ("[10,20]language=ja-JP");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_rise_new (100);
-  attr->start_index = 20;
+  attr = attribute_from_string ("[20,-1]rise=100");
   pango_attr_list_insert (list, attr);
-  attr = pango_attr_fallback_new (FALSE);
-  attr->start_index = 20;
+  attr = attribute_from_string ("[20,-1]fallback=0");
   pango_attr_list_insert (list, attr);
 
   iter = pango_attr_list_get_iterator (list);
@@ -709,18 +673,14 @@ test_list_equal (void)
   g_assert_true (pango_attr_list_equal (list1, list1));
   g_assert_true (pango_attr_list_equal (list1, list2));
 
-  attr = pango_attr_size_new (10 * PANGO_SCALE);
-  attr->start_index = 0;
-  attr->end_index = 7;
+  attr = attribute_from_string ("[0,7]size=10240");
   pango_attr_list_insert (list1, pango_attribute_copy (attr));
   pango_attr_list_insert (list2, pango_attribute_copy (attr));
   pango_attribute_destroy (attr);
 
   g_assert_true (pango_attr_list_equal (list1, list2));
 
-  attr = pango_attr_stretch_new (PANGO_STRETCH_CONDENSED);
-  attr->start_index = 0;
-  attr->end_index = 1;
+  attr = attribute_from_string ("[0,1]stretch=condensed");
   pango_attr_list_insert (list1, pango_attribute_copy (attr));
   g_assert_true (!pango_attr_list_equal (list1, list2));
 
@@ -728,10 +688,8 @@ test_list_equal (void)
   g_assert_true (pango_attr_list_equal (list1, list2));
   pango_attribute_destroy (attr);
 
-  attr = pango_attr_size_new (30 * PANGO_SCALE);
   /* Same range as the first attribute */
-  attr->start_index = 0;
-  attr->end_index = 7;
+  attr = attribute_from_string ("[0,7]size=30720");
   pango_attr_list_insert (list2, pango_attribute_copy (attr));
   g_assert_true (!pango_attr_list_equal (list1, list2));
   pango_attr_list_insert (list1, pango_attribute_copy (attr));
@@ -782,9 +740,7 @@ test_insert (void)
                                  "[11,100]fallback=0\n"
                                  "[30,60]stretch=2\n");
 
-  attr = pango_attr_family_new ("Times");
-  attr->start_index = 10;
-  attr->end_index = 25;
+  attr = attribute_from_string ("[10,25]family=Times");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,200]rise=100\n"
@@ -793,9 +749,7 @@ test_insert (void)
                           "[11,100]fallback=0\n"
                           "[30,60]stretch=2\n");
 
-  attr = pango_attr_family_new ("Futura");
-  attr->start_index = 11;
-  attr->end_index = 25;
+  attr = attribute_from_string ("[11,25]family=Futura");
   pango_attr_list_insert (list, attr);
 
   assert_attr_list (list, "[0,200]rise=100\n"
@@ -822,9 +776,7 @@ test_insert2 (void)
                                  "[30,40]family=Futura\n"
                                  "[30,60]stretch=2\n");
 
-  attr = pango_attr_family_new ("Times");
-  attr->start_index = 10;
-  attr->end_index = 35;
+  attr = attribute_from_string ("[10,35]family=Times");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,200]rise=100\n"
@@ -888,10 +840,7 @@ test_merge2 (void)
   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;
-  attr->end_index = 3;
-
+  attr = attribute_from_string ("[2,3]foreground=#ffff00000000");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,10]underline=1\n"
@@ -899,10 +848,7 @@ test_merge2 (void)
                           "[2,3]foreground=#ffff00000000\n"
                           "[3,10]foreground=#00000000ffff\n");
 
-  attr = pango_attr_foreground_new (0, 0xffff, 0);
-  attr->start_index = 3;
-  attr->end_index = 4;
-
+  attr = attribute_from_string ("[3,4]foreground=#0000ffff0000");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,10]underline=1\n"
@@ -911,10 +857,7 @@ test_merge2 (void)
                           "[3,4]foreground=#0000ffff0000\n"
                           "[4,10]foreground=#00000000ffff\n");
 
-  attr = pango_attr_foreground_new (0, 0, 0xffff);
-  attr->start_index = 4;
-  attr->end_index = 5;
-
+  attr = attribute_from_string ("[4,5]foreground=#00000000ffff");
   pango_attr_list_change (list, attr);
 
   assert_attr_list (list, "[0,10]underline=1\n"


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