[pango/log-attr-tweaks: 29/32] tests: Validate log attrs




commit 69506c740d6585e1d86c2a5569110e3f25db73e4
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Aug 21 18:39:09 2021 -0400

    tests: Validate log attrs
    
    Now that we have api to do it, we should validate
    our log attrs in test-break.

 pango/break.c      | 58 +++++++++++++++++++++++++++++++++++++-----------------
 tests/test-break.c |  1 +
 2 files changed, 41 insertions(+), 18 deletions(-)
---
diff --git a/pango/break.c b/pango/break.c
index 2a1f9b22..c52e2960 100644
--- a/pango/break.c
+++ b/pango/break.c
@@ -1909,19 +1909,19 @@ check_line_char (int            pos,
 static gboolean
 check_line_invariants (const char          *text,
                        int                  length,
-                       const PangoLogAttr  *log_attrs,
+                       const PangoLogAttr  *attrs,
                        int                  attrs_len,
                        GError             **error)
 {
   return log_attr_foreach (text, length,
-                           log_attrs, attrs_len,
+                           attrs, attrs_len,
                            check_line_char, error);
 }
 
 static gboolean
 check_grapheme_invariants (const char          *text,
                            int                  length,
-                           const PangoLogAttr  *log_attrs,
+                           const PangoLogAttr  *attrs,
                            int                  attrs_len,
                            GError             **error)
 {
@@ -1931,7 +1931,7 @@ check_grapheme_invariants (const char          *text,
 static gboolean
 check_word_invariants (const char          *text,
                        int                  length,
-                       const PangoLogAttr  *log_attrs,
+                       const PangoLogAttr  *attrs,
                        int                  attrs_len,
                        GError             **error)
 {
@@ -1942,18 +1942,19 @@ check_word_invariants (const char          *text,
 
   for (int i = 0; i < attrs_len; i++)
     {
+      /* Check that word starts and ends are alternating */
       switch (state)
         {
         case AFTER_END:
-          if (log_attrs[i].is_word_start)
+          if (attrs[i].is_word_start)
             {
-              if (log_attrs[i].is_word_end)
+              if (attrs[i].is_word_end)
                 state = AFTER_END;
               else
                 state = AFTER_START;
               break;
             }
-          if (log_attrs[i].is_word_end)
+          if (attrs[i].is_word_end)
             {
               g_set_error (error,
                            PANGO_VALIDATE_ERROR, PANGO_VALIDATE_ERROR_WORD,
@@ -1963,15 +1964,15 @@ check_word_invariants (const char          *text,
           break;
 
         case AFTER_START:
-          if (log_attrs[i].is_word_end)
+          if (attrs[i].is_word_end)
             {
-              if (log_attrs[i].is_word_start)
+              if (attrs[i].is_word_start)
                 state = AFTER_START;
               else
                 state = AFTER_END;
               break;
            }
-          if (log_attrs[i].is_word_start)
+          if (attrs[i].is_word_start)
             {
               g_set_error (error,
                            PANGO_VALIDATE_ERROR, PANGO_VALIDATE_ERROR_WORD,
@@ -1980,6 +1981,15 @@ check_word_invariants (const char          *text,
             }
           break;
         }
+
+      /* Check that words don't end in the middle of graphemes */
+      if (attrs[i].is_word_boundary && !attrs[i].is_cursor_position)
+        {
+          g_set_error (error,
+                       PANGO_VALIDATE_ERROR, PANGO_VALIDATE_ERROR_SENTENCE,
+                       "char %d: Word ends inside a grapheme", i);
+          return FALSE;
+        }
     }
 
   return TRUE;
@@ -1988,7 +1998,7 @@ check_word_invariants (const char          *text,
 static gboolean
 check_sentence_invariants (const char          *text,
                            int                  length,
-                           const PangoLogAttr  *log_attrs,
+                           const PangoLogAttr  *attrs,
                            int                  attrs_len,
                            GError             **error)
 {
@@ -1999,18 +2009,19 @@ check_sentence_invariants (const char          *text,
 
   for (int i = 0; i < attrs_len; i++)
     {
+      /* Check that word starts and ends are alternating */
       switch (state)
         {
         case AFTER_END:
-          if (log_attrs[i].is_sentence_start)
+          if (attrs[i].is_sentence_start)
             {
-              if (log_attrs[i].is_sentence_end)
+              if (attrs[i].is_sentence_end)
                 state = AFTER_END;
               else
                 state = AFTER_START;
               break;
             }
-          if (log_attrs[i].is_sentence_end)
+          if (attrs[i].is_sentence_end)
             {
               g_set_error (error,
                            PANGO_VALIDATE_ERROR, PANGO_VALIDATE_ERROR_SENTENCE,
@@ -2020,15 +2031,15 @@ check_sentence_invariants (const char          *text,
           break;
 
         case AFTER_START:
-          if (log_attrs[i].is_sentence_end)
+          if (attrs[i].is_sentence_end)
             {
-              if (log_attrs[i].is_sentence_start)
+              if (attrs[i].is_sentence_start)
                 state = AFTER_START;
               else
                 state = AFTER_END;
               break;
             }
-          if (log_attrs[i].is_sentence_start)
+          if (attrs[i].is_sentence_start)
             {
               g_set_error (error,
                            PANGO_VALIDATE_ERROR, PANGO_VALIDATE_ERROR_SENTENCE,
@@ -2037,6 +2048,15 @@ check_sentence_invariants (const char          *text,
             }
           break;
         }
+
+      /* Check that sentences don't end in the middle of words */
+      if (attrs[i].is_sentence_boundary && !attrs[i].is_word_boundary)
+        {
+          g_set_error (error,
+                       PANGO_VALIDATE_ERROR, PANGO_VALIDATE_ERROR_SENTENCE,
+                       "char %d: Sentence ends inside a word", i);
+          return FALSE;
+        }
     }
 
   return TRUE;
@@ -2241,6 +2261,8 @@ pango_get_log_attrs (const char    *text,
  * - Word starts and ends alternate
  * - Sentence starts and ends alternate
  * - Expandable spaces are spaces
+ * - Words don't end in the middle of graphemes
+ * - Sentences don't end in the middle of words
  *
  * Returns: %TRUE if @log_attrs are valid
  *
@@ -2282,6 +2304,6 @@ pango_validate_log_attrs (const char          *text,
   return TRUE;
 }
 
-/* }}} */
+ /* }}} */
 
 /* vim:set foldmethod=marker expandtab: */
diff --git a/tests/test-break.c b/tests/test-break.c
index 3fb5cdcd..beef9f63 100644
--- a/tests/test-break.c
+++ b/tests/test-break.c
@@ -95,6 +95,7 @@ test_file (const gchar *filename, GString *string)
 
   g_assert_cmpint (len, ==, len2);
   g_assert_true (memcmp (attrs, attrs2, sizeof (PangoLogAttr) * len) == 0);
+  g_assert_true (pango_validate_log_attrs (text, length, attrs, len, NULL));
 
   layout2 = pango_layout_copy (layout);
   attrs2 = pango_layout_get_log_attrs_readonly (layout2, &len2);


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