[gtksourceview/wip/regex-search] Search: unit tests for async forward/backward search



commit 3f9ce723d49c9947b766ba6e9efe85227e9617ae
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Jul 22 13:27:58 2013 +0200

    Search: unit tests for async forward/backward search
    
    They don't pass, there is the same bug as for sync forward/backward
    search.

 tests/test-search.c |  231 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 224 insertions(+), 7 deletions(-)
---
diff --git a/tests/test-search.c b/tests/test-search.c
index 3e8d80e..d6da227 100644
--- a/tests/test-search.c
+++ b/tests/test-search.c
@@ -23,6 +23,25 @@
 #include <gtksourceview/gtksource.h>
 #include "gtksourceview/gtksourcesearch.h"
 
+typedef struct
+{
+       gint match_start_offset;
+       gint match_end_offset;
+       guint found : 1;
+} SearchResult;
+
+typedef struct
+{
+       SearchResult *results;
+       gint result_num;
+       guint forward : 1;
+} AsyncData;
+
+static void check_async_search_results (GtkSourceBuffer *source_buffer,
+                                       SearchResult    *results,
+                                       gboolean         forward,
+                                       gboolean         start_check);
+
 static void
 flush_queue (void)
 {
@@ -311,13 +330,6 @@ test_search_at_word_boundaries (void)
        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,
@@ -367,6 +379,95 @@ check_search_results (GtkSourceBuffer *source_buffer,
 }
 
 static void
+finish_check_result (GtkSourceBuffer *source_buffer,
+                    GAsyncResult    *result,
+                    AsyncData       *data)
+{
+       GtkTextIter match_start;
+       GtkTextIter match_end;
+       gboolean found;
+       SearchResult search_result = data->results[data->result_num];
+
+       if (data->forward)
+       {
+               found = gtk_source_buffer_forward_search_finish (source_buffer,
+                                                                result,
+                                                                &match_start,
+                                                                &match_end,
+                                                                NULL);
+       }
+       else
+       {
+               found = gtk_source_buffer_backward_search_finish (source_buffer,
+                                                                 result,
+                                                                 &match_start,
+                                                                 &match_end,
+                                                                 NULL);
+       }
+
+       g_assert (found == search_result.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, ==, search_result.match_start_offset);
+               g_assert_cmpint (match_end_offset, ==, search_result.match_end_offset);
+       }
+
+       check_async_search_results (source_buffer,
+                                   data->results,
+                                   data->forward,
+                                   FALSE);
+
+       g_slice_free (AsyncData, data);
+}
+
+static void
+check_async_search_results (GtkSourceBuffer *source_buffer,
+                           SearchResult    *results,
+                           gboolean         forward,
+                           gboolean         start_check)
+{
+       static GtkTextIter iter;
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+       AsyncData *data;
+
+       if (start_check)
+       {
+               gtk_text_buffer_get_start_iter (text_buffer, &iter);
+       }
+       else if (!gtk_text_iter_forward_char (&iter))
+       {
+               gtk_main_quit ();
+               return;
+       }
+
+       data = g_slice_new (AsyncData);
+       data->results = results;
+       data->result_num = gtk_text_iter_get_offset (&iter);
+       data->forward = forward;
+
+       if (forward)
+       {
+               gtk_source_buffer_forward_search_async (source_buffer,
+                                                       &iter,
+                                                       NULL,
+                                                       (GAsyncReadyCallback)finish_check_result,
+                                                       data);
+       }
+       else
+       {
+               gtk_source_buffer_backward_search_async (source_buffer,
+                                                        &iter,
+                                                        NULL,
+                                                        (GAsyncReadyCallback)finish_check_result,
+                                                        data);
+       }
+}
+
+static void
 test_forward_search (void)
 {
        GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
@@ -402,6 +503,9 @@ test_forward_search (void)
 
        check_search_results (source_buffer, results1, TRUE);
 
+       g_test_trap_subprocess ("/Search/forward/subprocess/async-wrap-around", 0, 0);
+       g_test_trap_assert_passed ();
+
        /* Wrap around: FALSE */
 
        gtk_source_buffer_set_search_wrap_around (source_buffer, FALSE);
@@ -410,6 +514,59 @@ test_forward_search (void)
 
        check_search_results (source_buffer, results2, TRUE);
 
+       g_test_trap_subprocess ("/Search/forward/subprocess/async-normal", 0, 0);
+       g_test_trap_assert_passed ();
+
+       g_object_unref (source_buffer);
+}
+
+static void
+test_async_forward_search_normal (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+
+       static SearchResult results[] =
+       {
+               { 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");
+       gtk_source_buffer_set_search_wrap_around (source_buffer, FALSE);
+
+       check_async_search_results (source_buffer, results, TRUE, TRUE);
+
+       gtk_main ();
+       g_object_unref (source_buffer);
+}
+
+static void
+test_async_forward_search_wrap_around (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+
+       static SearchResult results[] =
+       {
+               { 0, 2, TRUE },
+               { 2, 4, TRUE },
+               { 2, 4, TRUE },
+               { 0, 2, TRUE },
+               { 0, 2, TRUE }
+       };
+
+       gtk_text_buffer_set_text (text_buffer, "aaaa", -1);
+       gtk_source_buffer_set_search_text (source_buffer, "aa");
+       gtk_source_buffer_set_search_wrap_around (source_buffer, TRUE);
+
+       check_async_search_results (source_buffer, results, TRUE, TRUE);
+
+       gtk_main ();
        g_object_unref (source_buffer);
 }
 
@@ -445,11 +602,67 @@ test_backward_search (void)
        gtk_source_buffer_set_search_wrap_around (source_buffer, TRUE);
        check_search_results (source_buffer, results1, FALSE);
 
+       g_test_trap_subprocess ("/Search/backward/subprocess/async-wrap-around", 0, 0);
+       g_test_trap_assert_passed ();
+
        /* Wrap around: FALSE */
 
        gtk_source_buffer_set_search_wrap_around (source_buffer, FALSE);
        check_search_results (source_buffer, results2, FALSE);
 
+       g_test_trap_subprocess ("/Search/backward/subprocess/async-normal", 0, 0);
+       g_test_trap_assert_passed ();
+
+       g_object_unref (source_buffer);
+}
+
+static void
+test_async_backward_search_normal (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+
+       static SearchResult results[] =
+       {
+               { 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");
+       gtk_source_buffer_set_search_wrap_around (source_buffer, FALSE);
+
+       check_async_search_results (source_buffer, results, FALSE, TRUE);
+
+       gtk_main ();
+       g_object_unref (source_buffer);
+}
+
+static void
+test_async_backward_search_wrap_around (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+
+       static SearchResult results[] =
+       {
+               { 2, 4, TRUE },
+               { 2, 4, TRUE },
+               { 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");
+       gtk_source_buffer_set_search_wrap_around (source_buffer, TRUE);
+
+       check_async_search_results (source_buffer, results, FALSE, TRUE);
+
+       gtk_main ();
        g_object_unref (source_buffer);
 }
 
@@ -465,7 +678,11 @@ main (int argc, char **argv)
        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/forward/subprocess/async-normal", test_async_forward_search_normal);
+       g_test_add_func ("/Search/forward/subprocess/async-wrap-around", 
test_async_forward_search_wrap_around);
        g_test_add_func ("/Search/backward", test_backward_search);
+       g_test_add_func ("/Search/backward/subprocess/async-normal", test_async_backward_search_normal);
+       g_test_add_func ("/Search/backward/subprocess/async-wrap-around", 
test_async_backward_search_wrap_around);
 
        return g_test_run ();
 }


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