[gtk+] textbuffer: unit tests for the empty last line



commit 83bc0c4bb19ac0a61553aba6e9a370aba0ab6e13
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Aug 20 16:57:05 2014 +0200

    textbuffer: unit tests for the empty last line
    
    For functions using _gtk_text_buffer_get_line_log_attrs():
    - gtk_text_buffer_backspace()
    - some gtk_text_iter functions (word/sentence/cursor boundaries)
    
    As the FIXME comments show, there is a bug with
    gtk_text_iter_is_cursor_position() for an empty last line.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=156164

 testsuite/gtk/textbuffer.c |   22 ++++++++++++++++++++++
 testsuite/gtk/textiter.c   |   28 ++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 0 deletions(-)
---
diff --git a/testsuite/gtk/textbuffer.c b/testsuite/gtk/textbuffer.c
index 3d8d872..6e16ff2 100644
--- a/testsuite/gtk/textbuffer.c
+++ b/testsuite/gtk/textbuffer.c
@@ -947,6 +947,28 @@ test_backspace (void)
   g_assert_cmpint (0, ==, gtk_text_iter_get_line (&iter));
   g_assert_cmpint (8, ==, gtk_text_buffer_get_char_count (buffer));
 
+  /* test empty last line */
+  gtk_text_buffer_set_text (buffer, "", -1);
+  gtk_text_buffer_get_end_iter (buffer, &iter);
+  ret = gtk_text_buffer_backspace (buffer, &iter, TRUE, TRUE);
+  g_assert (!ret);
+  g_assert_cmpint (0, ==, gtk_text_iter_get_offset (&iter));
+  g_assert_cmpint (0, ==, gtk_text_buffer_get_char_count (buffer));
+
+  gtk_text_buffer_set_text (buffer, "foo\n", -1);
+  gtk_text_buffer_get_end_iter (buffer, &iter);
+  ret = gtk_text_buffer_backspace (buffer, &iter, TRUE, TRUE);
+  g_assert (ret);
+  g_assert_cmpint (3, ==, gtk_text_iter_get_offset (&iter));
+  g_assert_cmpint (3, ==, gtk_text_buffer_get_char_count (buffer));
+
+  gtk_text_buffer_set_text (buffer, "foo\r\n", -1);
+  gtk_text_buffer_get_end_iter (buffer, &iter);
+  ret = gtk_text_buffer_backspace (buffer, &iter, TRUE, TRUE);
+  g_assert (ret);
+  g_assert_cmpint (3, ==, gtk_text_iter_get_offset (&iter));
+  g_assert_cmpint (3, ==, gtk_text_buffer_get_char_count (buffer));
+
   g_object_unref (buffer);
 }
 
diff --git a/testsuite/gtk/textiter.c b/testsuite/gtk/textiter.c
index ef8790d..21c1869 100644
--- a/testsuite/gtk/textiter.c
+++ b/testsuite/gtk/textiter.c
@@ -391,12 +391,14 @@ test_word_boundaries (void)
   check_word_boundaries ("ab ", 1, FALSE, FALSE, TRUE);
   check_word_boundaries ("ab ", 2, FALSE, TRUE, FALSE);
   check_word_boundaries ("ab ", 3, FALSE, FALSE, FALSE);
+  check_word_boundaries ("", 0, FALSE, FALSE, FALSE);
 
   check_forward_word_end ("ab ", 0, 2, TRUE);
   check_forward_word_end ("ab ", 1, 2, TRUE);
   check_forward_word_end ("ab ", 2, 2, FALSE);
   check_forward_word_end ("ab ", 3, 3, FALSE);
   check_forward_word_end ("ab", 0, 2, FALSE);
+  check_forward_word_end ("ab\n", 2, 2, FALSE);
 
   check_backward_word_start (" ab", 3, 1, TRUE);
   check_backward_word_start (" ab", 2, 1, TRUE);
@@ -489,6 +491,23 @@ test_visible_word_boundaries (void)
 }
 
 static void
+check_is_cursor_position (const gchar *buffer_text,
+                          gint         offset,
+                          gboolean     ret)
+{
+  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 (ret, ==, gtk_text_iter_is_cursor_position (&iter));
+
+  g_object_unref (buffer);
+}
+
+static void
 check_cursor_position (const gchar *buffer_text,
                        gboolean     forward,
                        gint         initial_offset,
@@ -516,12 +535,19 @@ check_cursor_position (const gchar *buffer_text,
 static void
 test_cursor_positions (void)
 {
+  check_is_cursor_position ("a\r\n", 0, TRUE);
+  check_is_cursor_position ("a\r\n", 1, TRUE);
+  check_is_cursor_position ("a\r\n", 2, FALSE);
+  check_is_cursor_position ("a\r\n", 3, FALSE); /* FIXME should be TRUE */
+  check_is_cursor_position ("", 0, FALSE);      /* FIXME should be TRUE */
+
   /* forward */
   check_cursor_position ("a\r\nb", TRUE, 0, 1, TRUE);
   check_cursor_position ("a\r\nb", TRUE, 1, 3, TRUE);
   check_cursor_position ("a\r\nb", TRUE, 2, 3, TRUE);
   check_cursor_position ("a\r\nb", TRUE, 3, 4, FALSE);
   check_cursor_position ("a\r\nb", TRUE, 4, 4, FALSE);
+  check_cursor_position ("a\n", TRUE, 1, 2, FALSE);
 
   /* backward */
   check_cursor_position ("a\r\nb", FALSE, 4, 3, TRUE);
@@ -656,6 +682,7 @@ test_sentence_boundaries (void)
   check_sentence_boundaries ("Hi. ", 2, FALSE, FALSE, TRUE);
   check_sentence_boundaries ("Hi. ", 3, FALSE, TRUE, FALSE);
   check_sentence_boundaries ("Hi. ", 4, FALSE, FALSE, FALSE);
+  check_sentence_boundaries ("", 0, FALSE, FALSE, FALSE);
 
   check_forward_sentence_end ("Hi. ", 0, 3, TRUE);
   check_forward_sentence_end ("Hi. ", 1, 3, TRUE);
@@ -663,6 +690,7 @@ test_sentence_boundaries (void)
   check_forward_sentence_end ("Hi. ", 3, 3, FALSE);
   check_forward_sentence_end ("Hi. ", 4, 4, FALSE);
   check_forward_sentence_end ("Hi.", 0, 3, FALSE);
+  check_forward_sentence_end ("Hi.\n", 3, 3, FALSE);
 
   check_backward_sentence_start (" Hi.", 4, 1, TRUE);
   check_backward_sentence_start (" Hi.", 3, 1, TRUE);


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