[pango/log-attr-tweaks: 19/19] tests: Validate log attrs
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pango/log-attr-tweaks: 19/19] tests: Validate log attrs
- Date: Tue, 24 Aug 2021 02:47:00 +0000 (UTC)
commit 6383cccbd7eec371e5124d3a17fac73506bd5854
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 | 5 +++++
2 files changed, 45 insertions(+), 18 deletions(-)
---
diff --git a/pango/break.c b/pango/break.c
index 943c2a4a..fdbafa99 100644
--- a/pango/break.c
+++ b/pango/break.c
@@ -1974,19 +1974,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)
{
@@ -1996,7 +1996,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)
{
@@ -2007,18 +2007,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,
@@ -2028,15 +2029,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,
@@ -2048,6 +2049,15 @@ check_word_invariants (const char *text,
default:
g_assert_not_reached ();
}
+
+ /* 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;
@@ -2056,7 +2066,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)
{
@@ -2067,18 +2077,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,
@@ -2088,15 +2099,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,
@@ -2108,6 +2119,15 @@ check_sentence_invariants (const char *text,
default:
g_assert_not_reached ();
}
+
+ /* 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;
@@ -2375,6 +2395,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
*
@@ -2416,6 +2438,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..cd530280 100644
--- a/tests/test-break.c
+++ b/tests/test-break.c
@@ -95,6 +95,11 @@ test_file (const gchar *filename, GString *string)
g_assert_cmpint (len, ==, len2);
g_assert_true (memcmp (attrs, attrs2, sizeof (PangoLogAttr) * len) == 0);
+ if (!pango_validate_log_attrs (text, length, attrs, len, &error))
+ {
+ g_warning ("%s: Log attrs invalid: %s", filename, error->message);
+ g_assert_not_reached ();
+ }
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]