[gspell] testsuite: more tests for _gspell_text_iter functions



commit c82ea0c0733d2bc005b8072c9aef10ee7f9c62cd
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Mar 9 11:27:12 2016 +0100

    testsuite: more tests for _gspell_text_iter functions

 testsuite/test-text-iter.c |  163 ++++++++++++++++++++++++++++++++++++++------
 1 files changed, 142 insertions(+), 21 deletions(-)
---
diff --git a/testsuite/test-text-iter.c b/testsuite/test-text-iter.c
index 3278a8e..073a2b4 100644
--- a/testsuite/test-text-iter.c
+++ b/testsuite/test-text-iter.c
@@ -20,10 +20,11 @@
 #include "gspell/gspell-text-iter.h"
 
 static void
-check_forward_word_end (const gchar *text,
-                       gint         initial_offset,
-                       gint         expected_result_offset,
-                       gboolean     expected_ret)
+check_word_move (gboolean     forward,
+                const gchar *text,
+                gint         initial_offset,
+                gint         expected_result_offset,
+                gboolean     expected_ret)
 {
        GtkTextBuffer *buffer;
        GtkTextIter iter;
@@ -34,7 +35,16 @@ check_forward_word_end (const gchar *text,
        gtk_text_buffer_set_text (buffer, text, -1);
 
        gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset);
-       ret = _gspell_text_iter_forward_word_end (&iter);
+
+       if (forward)
+       {
+               ret = _gspell_text_iter_forward_word_end (&iter);
+       }
+       else
+       {
+               ret = _gspell_text_iter_backward_word_start (&iter);
+       }
+
        g_assert_cmpint (ret, ==, expected_ret);
 
        result_offset = gtk_text_iter_get_offset (&iter);
@@ -46,22 +56,131 @@ check_forward_word_end (const gchar *text,
 static void
 test_forward_word_end (void)
 {
-       check_forward_word_end (" don't ", 0, 6, TRUE);
-       check_forward_word_end (" don't ", 1, 6, TRUE);
-       check_forward_word_end (" don't ", 2, 6, TRUE);
-       check_forward_word_end (" don't ", 4, 6, TRUE);
-       check_forward_word_end (" don't ", 5, 6, TRUE);
-       check_forward_word_end (" don't ", 6, 6, FALSE);
-       check_forward_word_end (" don't", 0, 6, FALSE);
-
-       /* "goin'" is a valid word, but it is not supported yet. */
-       check_forward_word_end (" goin' ", 0, 5, TRUE);
-       check_forward_word_end (" goin' ", 1, 5, TRUE);
-       check_forward_word_end (" goin' ", 2, 5, TRUE);
-       check_forward_word_end (" goin' ", 4, 5, TRUE);
-       check_forward_word_end (" goin' ", 5, 5, FALSE);
-       check_forward_word_end (" goin' ", 6, 6, FALSE);
-       check_forward_word_end (" goin' ", 7, 7, FALSE);
+       gint i;
+
+       for (i = 0; i <= 5; i++)
+       {
+               check_word_move (TRUE, " don't ", i, 6, TRUE);
+       }
+       check_word_move (TRUE, " don't ", 6, 6, FALSE);
+       check_word_move (TRUE, " don't", 0, 6, FALSE);
+
+       /* "goin'" is a valid word, but not yet supported. */
+       for (i = 0; i <= 4; i++)
+       {
+               check_word_move (TRUE, " goin' ", i, 5, TRUE);
+       }
+       check_word_move (TRUE, " goin' ", 5, 5, FALSE);
+       check_word_move (TRUE, " goin' ", 6, 6, FALSE);
+       check_word_move (TRUE, " goin' ", 7, 7, FALSE);
+
+       /* Several apostrophes in the same word. */
+       for (i = 0; i <= 11; i++)
+       {
+               check_word_move (TRUE, " rock'n'roll ", i, 12, TRUE);
+       }
+       check_word_move (TRUE, " rock'n'roll ", 12, 12, FALSE);
+       check_word_move (TRUE, " rock'n'roll", 0, 12, FALSE);
+}
+
+static void
+test_backward_word_start (void)
+{
+       gint i;
+
+       for (i = 7; i >= 2; i--)
+       {
+               check_word_move (FALSE, " don't ", i, 1, TRUE);
+       }
+       check_word_move (FALSE, " don't ", 1, 1, FALSE);
+       check_word_move (FALSE, " don't ", 0, 0, FALSE);
+
+       /* "'til" is a valid word, but not yet supported. */
+       for (i = 6; i >= 3; i--)
+       {
+               check_word_move (FALSE, " 'til ", i, 2, TRUE);
+       }
+       check_word_move (FALSE, " 'til ", 2, 2, FALSE);
+       check_word_move (FALSE, " 'til ", 1, 1, FALSE);
+       check_word_move (FALSE, " 'til ", 0, 0, FALSE);
+
+       /* Several apostrophes in the same word. */
+       for (i = 13; i >= 2; i--)
+       {
+               check_word_move (FALSE, " rock'n'roll ", i, 1, TRUE);
+       }
+       check_word_move (FALSE, " rock'n'roll ", 1, 1, FALSE);
+       check_word_move (FALSE, " rock'n'roll ", 0, 0, FALSE);
+}
+
+static void
+check_properties (GtkTextBuffer *buffer,
+                 gint           offset,
+                 gboolean       expected_starts_word,
+                 gboolean       expected_ends_word,
+                 gboolean       expected_inside_word)
+{
+       GtkTextIter iter;
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
+
+       g_assert_cmpint (expected_starts_word, ==, _gspell_text_iter_starts_word (&iter));
+       g_assert_cmpint (expected_ends_word, ==, _gspell_text_iter_ends_word (&iter));
+       g_assert_cmpint (expected_inside_word, ==, _gspell_text_iter_inside_word (&iter));
+}
+
+static void
+test_properties (void)
+{
+       GtkTextBuffer *buffer;
+       gint i;
+
+       buffer = gtk_text_buffer_new (NULL);
+
+       gtk_text_buffer_set_text (buffer, " don't ", -1);
+       check_properties (buffer, 0, FALSE, FALSE, FALSE);
+       check_properties (buffer, 1, TRUE, FALSE, TRUE);
+       for (i = 2; i <= 5; i++)
+       {
+               check_properties (buffer, i, FALSE, FALSE, TRUE);
+       }
+       check_properties (buffer, 6, FALSE, TRUE, FALSE);
+       check_properties (buffer, 7, FALSE, FALSE, FALSE);
+
+       /* "goin'" is a valid word, but not yet supported. */
+       gtk_text_buffer_set_text (buffer, " goin' ", -1);
+       check_properties (buffer, 0, FALSE, FALSE, FALSE);
+       check_properties (buffer, 1, TRUE, FALSE, TRUE);
+       for (i = 2; i <= 4; i++)
+       {
+               check_properties (buffer, i, FALSE, FALSE, TRUE);
+       }
+       check_properties (buffer, 5, FALSE, TRUE, FALSE);
+       check_properties (buffer, 6, FALSE, FALSE, FALSE);
+       check_properties (buffer, 7, FALSE, FALSE, FALSE);
+
+       /* "'til" is a valid word, but not yet supported. */
+       gtk_text_buffer_set_text (buffer, " 'til ", -1);
+       check_properties (buffer, 0, FALSE, FALSE, FALSE);
+       check_properties (buffer, 1, FALSE, FALSE, FALSE);
+       check_properties (buffer, 2, TRUE, FALSE, TRUE);
+       check_properties (buffer, 3, FALSE, FALSE, TRUE);
+       check_properties (buffer, 4, FALSE, FALSE, TRUE);
+       check_properties (buffer, 5, FALSE, TRUE, FALSE);
+       check_properties (buffer, 6, FALSE, FALSE, FALSE);
+
+       /* Several apostrophes in the same word. */
+       gtk_text_buffer_set_text (buffer, " rock'n'roll ", -1);
+       check_properties (buffer, 0, FALSE, FALSE, FALSE);
+       check_properties (buffer, 1, TRUE, FALSE, TRUE);
+       for (i = 2; i <= 11; i++)
+       {
+               check_properties (buffer, i, FALSE, FALSE, TRUE);
+       }
+       check_properties (buffer, 12, FALSE, TRUE, FALSE);
+       check_properties (buffer, 13, FALSE, FALSE, FALSE);
+
+       g_object_unref (buffer);
 }
 
 gint
@@ -71,6 +190,8 @@ main (gint    argc,
        gtk_test_init (&argc, &argv);
 
        g_test_add_func ("/text-iter/forward-word-end", test_forward_word_end);
+       g_test_add_func ("/text-iter/backward-word-start", test_backward_word_start);
+       g_test_add_func ("/text-iter/starts-ends-inside-word", test_properties);
 
        return g_test_run ();
 }


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