[gtksourceview/wip/regex-search] Search: more unit tests



commit edf613b3f6dc5b994c37d4888c25e44caed5a160
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Jul 21 21:51:08 2013 +0200

    Search: more unit tests
    
    There is a bug in the forward/backward search (even for non-regex
    searches).

 tests/test-search.c |  206 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 206 insertions(+), 0 deletions(-)
---
diff --git a/tests/test-search.c b/tests/test-search.c
index 1ef5fa8..3e8d80e 100644
--- a/tests/test-search.c
+++ b/tests/test-search.c
@@ -251,6 +251,208 @@ test_occurrences_count_multiple_lines (void)
        g_object_unref (source_buffer);
 }
 
+static void
+test_case_sensitivity (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+       gboolean case_sensitive;
+       gint occurrences_count;
+
+       gtk_text_buffer_set_text (text_buffer, "Case", -1);
+       gtk_source_buffer_set_search_text (source_buffer, "case");
+
+       gtk_source_buffer_set_case_sensitive_search (source_buffer, TRUE);
+       case_sensitive = gtk_source_buffer_get_case_sensitive_search (source_buffer);
+       g_assert (case_sensitive);
+
+       flush_queue ();
+       occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
+       g_assert_cmpint (occurrences_count, ==, 0);
+
+       gtk_source_buffer_set_case_sensitive_search (source_buffer, FALSE);
+       case_sensitive = gtk_source_buffer_get_case_sensitive_search (source_buffer);
+       g_assert (!case_sensitive);
+
+       flush_queue ();
+       occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
+       g_assert_cmpint (occurrences_count, ==, 1);
+
+       g_object_unref (source_buffer);
+}
+
+static void
+test_search_at_word_boundaries (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+       gboolean at_word_boundaries;
+       gint occurrences_count;
+
+       gtk_text_buffer_set_text (text_buffer, "AtWordBoundaries AtWord", -1);
+       gtk_source_buffer_set_search_text (source_buffer, "AtWord");
+
+       gtk_source_buffer_set_search_at_word_boundaries (source_buffer, TRUE);
+       at_word_boundaries = gtk_source_buffer_get_search_at_word_boundaries (source_buffer);
+       g_assert (at_word_boundaries);
+
+       flush_queue ();
+       occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
+       g_assert_cmpint (occurrences_count, ==, 1);
+
+       gtk_source_buffer_set_search_at_word_boundaries (source_buffer, FALSE);
+       at_word_boundaries = gtk_source_buffer_get_search_at_word_boundaries (source_buffer);
+       g_assert (!at_word_boundaries);
+
+       flush_queue ();
+       occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
+       g_assert_cmpint (occurrences_count, ==, 2);
+
+       g_object_unref (source_buffer);
+}
+
+typedef struct
+{
+       gint match_start_offset;
+       gint match_end_offset;
+       guint found : 1;
+} SearchResult;
+
+static void
+check_search_results (GtkSourceBuffer *source_buffer,
+                     SearchResult    *results,
+                     gboolean         forward)
+{
+       GtkTextIter iter;
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+
+       gtk_text_buffer_get_start_iter (text_buffer, &iter);
+
+       do
+       {
+               gint i;
+               gboolean found;
+               GtkTextIter match_start;
+               GtkTextIter match_end;
+
+               i = gtk_text_iter_get_offset (&iter);
+
+               if (forward)
+               {
+                       found = gtk_source_buffer_forward_search (source_buffer,
+                                                                 &iter,
+                                                                 &match_start,
+                                                                 &match_end);
+               }
+               else
+               {
+                       found = gtk_source_buffer_backward_search (source_buffer,
+                                                                  &iter,
+                                                                  &match_start,
+                                                                  &match_end);
+               }
+
+               g_assert (found == results[i].found);
+
+               if (found)
+               {
+                       gint match_start_offset = gtk_text_iter_get_offset (&match_start);
+                       gint match_end_offset = gtk_text_iter_get_offset (&match_end);
+
+                       g_assert_cmpint (match_start_offset, ==, results[i].match_start_offset);
+                       g_assert_cmpint (match_end_offset, ==, results[i].match_end_offset);
+               }
+       }
+       while (gtk_text_iter_forward_char (&iter));
+}
+
+static void
+test_forward_search (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+       gboolean wrap_around;
+
+       static SearchResult results1[] =
+       {
+               { 0, 2, TRUE },
+               { 2, 4, TRUE },
+               { 2, 4, TRUE },
+               { 0, 2, TRUE },
+               { 0, 2, TRUE }
+       };
+
+       static SearchResult results2[] =
+       {
+               { 0, 2, TRUE },
+               { 2, 4, TRUE },
+               { 2, 4, TRUE },
+               { 0, 0, FALSE },
+               { 0, 0, FALSE }
+       };
+
+       gtk_text_buffer_set_text (text_buffer, "aaaa", -1);
+       gtk_source_buffer_set_search_text (source_buffer, "aa");
+
+       /* Wrap around: TRUE */
+
+       gtk_source_buffer_set_search_wrap_around (source_buffer, TRUE);
+       wrap_around = gtk_source_buffer_get_search_wrap_around (source_buffer);
+       g_assert (wrap_around);
+
+       check_search_results (source_buffer, results1, TRUE);
+
+       /* Wrap around: FALSE */
+
+       gtk_source_buffer_set_search_wrap_around (source_buffer, FALSE);
+       wrap_around = gtk_source_buffer_get_search_wrap_around (source_buffer);
+       g_assert (!wrap_around);
+
+       check_search_results (source_buffer, results2, TRUE);
+
+       g_object_unref (source_buffer);
+}
+
+static void
+test_backward_search (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+
+       static SearchResult results1[] =
+       {
+               { 2, 4, TRUE },
+               { 2, 4, TRUE },
+               { 0, 2, TRUE },
+               { 0, 2, TRUE },
+               { 2, 4, TRUE }
+       };
+
+       static SearchResult results2[] =
+       {
+               { 0, 0, FALSE },
+               { 0, 0, FALSE },
+               { 0, 2, TRUE },
+               { 0, 2, TRUE },
+               { 2, 4, TRUE }
+       };
+
+       gtk_text_buffer_set_text (text_buffer, "aaaa", -1);
+       gtk_source_buffer_set_search_text (source_buffer, "aa");
+
+       /* Wrap around: TRUE */
+
+       gtk_source_buffer_set_search_wrap_around (source_buffer, TRUE);
+       check_search_results (source_buffer, results1, FALSE);
+
+       /* Wrap around: FALSE */
+
+       gtk_source_buffer_set_search_wrap_around (source_buffer, FALSE);
+       check_search_results (source_buffer, results2, FALSE);
+
+       g_object_unref (source_buffer);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -260,6 +462,10 @@ main (int argc, char **argv)
        g_test_add_func ("/Search/occurrences-count/with-insert", test_occurrences_count_with_insert);
        g_test_add_func ("/Search/occurrences-count/with-delete", test_occurrences_count_with_delete);
        g_test_add_func ("/Search/occurrences-count/multiple-lines", test_occurrences_count_multiple_lines);
+       g_test_add_func ("/Search/case-sensitivity", test_case_sensitivity);
+       g_test_add_func ("/Search/at-word-boundaries", test_search_at_word_boundaries);
+       g_test_add_func ("/Search/forward", test_forward_search);
+       g_test_add_func ("/Search/backward", test_backward_search);
 
        return g_test_run ();
 }


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