[gspell] Entry: make the class unit-testable
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gspell] Entry: make the class unit-testable
- Date: Mon, 31 Oct 2016 10:38:25 +0000 (UTC)
commit 611d6d079e242a6bd55fe051cbe5a7481e2b432c
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sun Oct 30 11:58:33 2016 +0100
Entry: make the class unit-testable
docs/reference/Makefile.am | 1 +
gspell/Makefile.am | 1 +
gspell/gspell-entry-private.h | 34 ++++++++++++++++++++
gspell/gspell-entry.c | 69 +++++++++++++++++++++++++++++++++-------
4 files changed, 93 insertions(+), 12 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 088c496..6be59bd 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -31,6 +31,7 @@ EXTRA_HFILES = \
IGNORE_HFILES = \
gspell.h \
gspellregion.h \
+ gspell-entry-private.h \
gspell-entry-utils.h \
gspell-init.h \
gspell-inline-checker-text-buffer.h \
diff --git a/gspell/Makefile.am b/gspell/Makefile.am
index 253698f..f0d0685 100644
--- a/gspell/Makefile.am
+++ b/gspell/Makefile.am
@@ -45,6 +45,7 @@ gspell_public_c_files = \
gspell_private_headers = \
gconstructor.h \
gspellregion.h \
+ gspell-entry-private.h \
gspell-entry-utils.h \
gspell-init.h \
gspell-inline-checker-text-buffer.h \
diff --git a/gspell/gspell-entry-private.h b/gspell/gspell-entry-private.h
new file mode 100644
index 0000000..8e5c050
--- /dev/null
+++ b/gspell/gspell-entry-private.h
@@ -0,0 +1,34 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2016 - Sébastien Wilmet
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GSPELL_ENTRY_PRIVATE_H
+#define GSPELL_ENTRY_PRIVATE_H
+
+#include "gspell/gspell-entry.h"
+
+G_BEGIN_DECLS
+
+G_GNUC_INTERNAL
+const GSList * _gspell_entry_get_misspelled_words (GspellEntry *gspell_entry);
+
+G_END_DECLS
+
+#endif /* GSPELL_ENTRY_PRIVATE_H */
+
+/* ex:set ts=8 noet: */
diff --git a/gspell/gspell-entry.c b/gspell/gspell-entry.c
index ae289ed..c2a0d17 100644
--- a/gspell/gspell-entry.c
+++ b/gspell/gspell-entry.c
@@ -18,6 +18,7 @@
*/
#include "gspell-entry.h"
+#include "gspell-entry-private.h"
#include "gspell-entry-buffer.h"
#include "gspell-entry-utils.h"
@@ -40,6 +41,11 @@ struct _GspellEntry
GtkEntry *entry;
+ /* List elements: GspellEntryWord*.
+ * Used for unit tests.
+ */
+ GSList *misspelled_words;
+
gulong notify_attributes_handler_id;
guint notify_attributes_idle_id;
@@ -163,11 +169,13 @@ insert_underline (GspellEntry *gspell_entry,
}
static void
-recheck_all (GspellEntry *gspell_entry)
+update_misspelled_words_list (GspellEntry *gspell_entry)
{
GspellChecker *checker;
- GSList *words;
- GSList *l;
+ GSList *all_words;
+
+ g_slist_free_full (gspell_entry->misspelled_words, _gspell_entry_word_free);
+ gspell_entry->misspelled_words = NULL;
if (!gspell_entry->inline_spell_checking)
{
@@ -181,13 +189,13 @@ recheck_all (GspellEntry *gspell_entry)
return;
}
- words = _gspell_entry_utils_get_words (gspell_entry->entry);
+ all_words = _gspell_entry_utils_get_words (gspell_entry->entry);
- for (l = words; l != NULL; l = l->next)
+ while (all_words != NULL)
{
- GspellEntryWord *cur_word = l->data;
- GError *error = NULL;
+ GspellEntryWord *cur_word = all_words->data;
gboolean correctly_spelled;
+ GError *error = NULL;
correctly_spelled = gspell_checker_check_word (checker,
cur_word->word_str, -1,
@@ -197,18 +205,44 @@ recheck_all (GspellEntry *gspell_entry)
{
g_warning ("Inline spell checker: %s", error->message);
g_clear_error (&error);
+ g_slist_free_full (all_words, _gspell_entry_word_free);
+ all_words = NULL;
break;
}
- if (!correctly_spelled)
+ if (correctly_spelled)
{
- insert_underline (gspell_entry,
- cur_word->byte_start,
- cur_word->byte_end);
+ _gspell_entry_word_free (cur_word);
}
+ else
+ {
+ gspell_entry->misspelled_words = g_slist_prepend (gspell_entry->misspelled_words,
+ cur_word);
+ }
+
+ all_words = g_slist_delete_link (all_words, all_words);
}
- g_slist_free_full (words, _gspell_entry_word_free);
+ g_assert (all_words == NULL);
+
+ gspell_entry->misspelled_words = g_slist_reverse (gspell_entry->misspelled_words);
+}
+
+static void
+recheck_all (GspellEntry *gspell_entry)
+{
+ GSList *l;
+
+ update_misspelled_words_list (gspell_entry);
+
+ for (l = gspell_entry->misspelled_words; l != NULL; l = l->next)
+ {
+ GspellEntryWord *cur_word = l->data;
+
+ insert_underline (gspell_entry,
+ cur_word->byte_start,
+ cur_word->byte_end);
+ }
update_attributes (gspell_entry);
}
@@ -505,4 +539,15 @@ gspell_entry_set_inline_spell_checking (GspellEntry *gspell_entry,
}
}
+/* For unit tests.
+ * Returns: (transfer none) (element-type GspellEntryWord).
+ */
+const GSList *
+_gspell_entry_get_misspelled_words (GspellEntry *gspell_entry)
+{
+ g_return_val_if_fail (GSPELL_IS_ENTRY (gspell_entry), NULL);
+
+ return gspell_entry->misspelled_words;
+}
+
/* ex:set ts=8 noet: */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]