[gspell: 2/4] inline-checker-text-buffer: make it unit testable



commit dbeb1656555e7866c2a90c3a7b1cf5612630506d
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Feb 4 12:24:57 2016 +0100

    inline-checker-text-buffer: make it unit testable

 gspell/gspell-inline-checker-text-buffer.c |   64 ++++++++++++++++++++++++++--
 gspell/gspell-inline-checker-text-buffer.h |   20 +++++++--
 2 files changed, 75 insertions(+), 9 deletions(-)
---
diff --git a/gspell/gspell-inline-checker-text-buffer.c b/gspell/gspell-inline-checker-text-buffer.c
index 822c1ea..37ceaa7 100644
--- a/gspell/gspell-inline-checker-text-buffer.c
+++ b/gspell/gspell-inline-checker-text-buffer.c
@@ -49,6 +49,14 @@ struct _GspellInlineCheckerTextBuffer
 
        GspellTextRegion *scan_region;
        guint timeout_id;
+
+       /* If the unit test mode is enabled, there is no timeouts, and the whole
+        * buffer is scanned synchronously.
+        * The unit test mode tries to follow as most as possible the same code
+        * paths as the real code paths, otherwise the unit tests would be
+        * useless. As such, the function names reflect the real code paths.
+        */
+       guint unit_test_mode : 1;
 };
 
 enum
@@ -264,7 +272,15 @@ check_visible_region_in_view (GspellInlineCheckerTextBuffer *spell,
                return;
        }
 
-       get_visible_region (view, &visible_start, &visible_end);
+       if (view != NULL)
+       {
+               get_visible_region (view, &visible_start, &visible_end);
+       }
+       else
+       {
+               g_assert_true (spell->unit_test_mode);
+               gtk_text_buffer_get_bounds (spell->buffer, &visible_start, &visible_end);
+       }
 
        intersect = _gspell_text_region_intersect (spell->scan_region,
                                                   &visible_start,
@@ -315,6 +331,12 @@ check_visible_region (GspellInlineCheckerTextBuffer *spell)
                return;
        }
 
+       if (spell->unit_test_mode)
+       {
+               check_visible_region_in_view (spell, NULL);
+               return;
+       }
+
        for (l = spell->views; l != NULL; l = l->next)
        {
                GtkTextView *view = GTK_TEXT_VIEW (l->data);
@@ -338,11 +360,19 @@ install_timeout (GspellInlineCheckerTextBuffer *spell,
        if (spell->timeout_id != 0)
        {
                g_source_remove (spell->timeout_id);
+               spell->timeout_id = 0;
        }
 
-       spell->timeout_id = g_timeout_add (duration,
-                                          (GSourceFunc) timeout_cb,
-                                          spell);
+       if (spell->unit_test_mode)
+       {
+               timeout_cb (spell);
+       }
+       else
+       {
+               spell->timeout_id = g_timeout_add (duration,
+                                                  (GSourceFunc) timeout_cb,
+                                                  spell);
+       }
 }
 
 static void
@@ -1187,4 +1217,30 @@ _gspell_inline_checker_text_buffer_detach_view (GspellInlineCheckerTextBuffer *s
        spell->views = g_slist_remove (spell->views, view);
 }
 
+void
+_gspell_inline_checker_text_buffer_set_unit_test_mode (GspellInlineCheckerTextBuffer *spell,
+                                                      gboolean                       unit_test_mode)
+{
+       g_return_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (spell));
+
+       spell->unit_test_mode = unit_test_mode != FALSE;
+
+       if (spell->timeout_id != 0)
+       {
+               g_source_remove (spell->timeout_id);
+               spell->timeout_id = 0;
+               timeout_cb (spell);
+       }
+
+       check_visible_region (spell);
+}
+
+GtkTextTag *
+_gspell_inline_checker_text_buffer_get_highlight_tag (GspellInlineCheckerTextBuffer *spell)
+{
+       g_return_val_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (spell), NULL);
+
+       return spell->highlight_tag;
+}
+
 /* ex:set ts=8 noet: */
diff --git a/gspell/gspell-inline-checker-text-buffer.h b/gspell/gspell-inline-checker-text-buffer.h
index 1a0308c..0febf9c 100644
--- a/gspell/gspell-inline-checker-text-buffer.h
+++ b/gspell/gspell-inline-checker-text-buffer.h
@@ -36,15 +36,25 @@ G_DECLARE_FINAL_TYPE (GspellInlineCheckerTextBuffer, _gspell_inline_checker_text
 
 G_GNUC_INTERNAL
 GspellInlineCheckerTextBuffer *
-       _gspell_inline_checker_text_buffer_new          (GtkTextBuffer *buffer);
+       _gspell_inline_checker_text_buffer_new                  (GtkTextBuffer *buffer);
 
 G_GNUC_INTERNAL
-void   _gspell_inline_checker_text_buffer_attach_view  (GspellInlineCheckerTextBuffer *spell,
-                                                        GtkTextView                   *view);
+void   _gspell_inline_checker_text_buffer_attach_view          (GspellInlineCheckerTextBuffer *spell,
+                                                                GtkTextView                   *view);
 
 G_GNUC_INTERNAL
-void   _gspell_inline_checker_text_buffer_detach_view  (GspellInlineCheckerTextBuffer *spell,
-                                                        GtkTextView                   *view);
+void   _gspell_inline_checker_text_buffer_detach_view          (GspellInlineCheckerTextBuffer *spell,
+                                                                GtkTextView                   *view);
+
+/* For unit tests */
+
+G_GNUC_INTERNAL
+void   _gspell_inline_checker_text_buffer_set_unit_test_mode   (GspellInlineCheckerTextBuffer *spell,
+                                                                gboolean                       
unit_test_mode);
+
+G_GNUC_INTERNAL
+GtkTextTag *
+       _gspell_inline_checker_text_buffer_get_highlight_tag    (GspellInlineCheckerTextBuffer *spell);
 
 #endif  /* __GSPELL_INLINE_CHECKER_TEXT_BUFFER_H__ */
 


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