[gtksourceview/wip/custom-word-boundaries-2] iter: unit tests



commit a99547aa9458b979f764ef27ecbb993a577aa902
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Dec 21 17:40:08 2014 +0100

    iter: unit tests

 gtksourceview/gtksourceiter.c |   92 ++++++++++++++++++++++++++++++++++++-----
 1 files changed, 81 insertions(+), 11 deletions(-)
---
diff --git a/gtksourceview/gtksourceiter.c b/gtksourceview/gtksourceiter.c
index 9bc6241..6775463 100644
--- a/gtksourceview/gtksourceiter.c
+++ b/gtksourceview/gtksourceiter.c
@@ -191,7 +191,7 @@ starts_extra_natural_word (const GtkTextIter *iter)
        prev = *iter;
        if (!gtk_text_iter_backward_visible_cursor_position (&prev))
        {
-               return starts_word;
+               return starts_word || gtk_text_iter_get_char (iter) == '_';
        }
 
        if (starts_word)
@@ -207,12 +207,20 @@ starts_extra_natural_word (const GtkTextIter *iter)
 static gboolean
 ends_extra_natural_word (const GtkTextIter *iter)
 {
-       gboolean ends_word = gtk_text_iter_ends_word (iter);
-       GtkTextIter next;
+       GtkTextIter prev;
+       gboolean ends_word;
+
+       prev = *iter;
+       if (!gtk_text_iter_backward_visible_cursor_position (&prev))
+       {
+               return FALSE;
+       }
+
+       ends_word = gtk_text_iter_ends_word (iter);
 
        if (gtk_text_iter_is_end (iter))
        {
-               return ends_word;
+               return ends_word || gtk_text_iter_get_char (&prev) == '_';
        }
 
        if (ends_word)
@@ -220,13 +228,9 @@ ends_extra_natural_word (const GtkTextIter *iter)
                return gtk_text_iter_get_char (iter) != '_';
        }
 
-       next = *iter;
-       gtk_text_iter_forward_visible_cursor_position (&next);
-
-       return (gtk_text_iter_get_char (iter) == '_' &&
-               (gtk_text_iter_is_end (&next) ||
-                (gtk_text_iter_get_char (&next) != '_' &&
-                 !gtk_text_iter_starts_word (&next))));
+       return (gtk_text_iter_get_char (&prev) == '_' &&
+               gtk_text_iter_get_char (iter) != '_' &&
+               !gtk_text_iter_starts_word (iter));
 }
 
 /* Similar to gtk_text_iter_forward_visible_word_end, but with a custom
@@ -691,6 +695,70 @@ test_backward_extra_natural_word_start (void)
        check_extra_natural_word_boundaries (FALSE, str, 0, 0);
 }
 
+static void
+check_starts_extra_natural_word (const gchar *buffer_text,
+                                gint         offset,
+                                gboolean     starts)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, buffer_text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
+       g_assert_cmpint (starts, ==, starts_extra_natural_word (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_starts_extra_natural_word (void)
+{
+       check_starts_extra_natural_word ("hello", 0, TRUE);
+       check_starts_extra_natural_word ("__", 0, TRUE);
+       check_starts_extra_natural_word (" hello", 0, FALSE);
+       check_starts_extra_natural_word (" hello", 1, TRUE);
+       check_starts_extra_natural_word ("_hello", 1, FALSE);
+       check_starts_extra_natural_word ("()", 1, FALSE);
+       check_starts_extra_natural_word ("__", 1, FALSE);
+       check_starts_extra_natural_word (" __", 1, TRUE);
+       check_starts_extra_natural_word (" __hello", 1, TRUE);
+       check_starts_extra_natural_word ("hello_", 5, FALSE);
+}
+
+static void
+check_ends_extra_natural_word (const gchar *buffer_text,
+                              gint         offset,
+                              gboolean     ends)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, buffer_text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
+       g_assert_cmpint (ends, ==, ends_extra_natural_word (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_ends_extra_natural_word (void)
+{
+       check_ends_extra_natural_word ("ab", 0, FALSE);
+       check_ends_extra_natural_word ("ab", 2, TRUE);
+       check_ends_extra_natural_word ("__", 2, TRUE);
+       check_ends_extra_natural_word ("ab ", 3, FALSE);
+       check_ends_extra_natural_word ("ab ", 2, TRUE);
+       check_ends_extra_natural_word ("ab_", 2, FALSE);
+       check_ends_extra_natural_word ("()", 1, FALSE);
+       check_ends_extra_natural_word ("__ ", 1, FALSE);
+       check_ends_extra_natural_word ("__ab ", 2, FALSE);
+       check_ends_extra_natural_word ("__ ", 2, TRUE);
+}
+
 void
 _gtk_source_iter_run_internal_unit_tests (void)
 {
@@ -700,4 +768,6 @@ _gtk_source_iter_run_internal_unit_tests (void)
        test_ends_full_word ();
        test_forward_extra_natural_word_end ();
        test_backward_extra_natural_word_start ();
+       test_starts_extra_natural_word ();
+       test_ends_extra_natural_word ();
 }


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