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
This commit is contained in:
Sébastien Wilmet 2014-08-20 16:57:05 +02:00
parent 7645d201d4
commit 83bc0c4bb1
2 changed files with 50 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);
@ -488,6 +490,23 @@ test_visible_word_boundaries (void)
g_object_unref (buffer);
}
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,
@ -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);