Debian Codegraph @

tests/unit-tests/test-entry.c

raw
1 /* SPDX-FileCopyrightText: 2016 - Sébastien Wilmet <[email protected]>
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 */
4
5 #include <gspell/gspell.h>
6 #include "gspell/gspell-entry-utils.h"
7 #include "gspell/gspell-entry-private.h"
8 #include "gspell/gspell-checker-private.h"
9
10 /* Returns: (transfer full) */
11 static GtkEntry *
12 create_entry (void)
13 {
14 GtkEntry *gtk_entry;
15 GtkEntryBuffer *gtk_buffer;
16 const GspellLanguage *lang;
17 GspellChecker *checker;
18 GspellEntryBuffer *gspell_buffer;
19 GspellEntry *gspell_entry;
20
21 gtk_entry = GTK_ENTRY (gtk_entry_new ());
22 g_object_ref_sink (gtk_entry);
23
24 lang = gspell_language_lookup ("en_US");
25 g_assert_true (lang != NULL);
26
27 checker = gspell_checker_new (lang);
28 gtk_buffer = gtk_entry_get_buffer (gtk_entry);
29 gspell_buffer = gspell_entry_buffer_get_from_gtk_entry_buffer (gtk_buffer);
30 gspell_entry_buffer_set_spell_checker (gspell_buffer, checker);
31 g_object_unref (checker);
32
33 gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
34 gspell_entry_set_inline_spell_checking (gspell_entry, TRUE);
35
36 return gtk_entry;
37 }
38
39 static GSList *
40 add_word (GSList *list,
41 const gchar *word_str,
42 gint byte_start,
43 gint byte_end)
44 {
45 GspellEntryWord *word;
46
47 word = _gspell_entry_word_new ();
48 word->word_str = g_strdup (word_str);
49 word->byte_start = byte_start;
50 word->byte_end = byte_end;
51 word->char_start = -1;
52 word->char_end = -1;
53
54 return g_slist_append (list, word);
55 }
56
57 static GSList *
58 add_word_full (GSList *list,
59 const gchar *word_str,
60 gint byte_start,
61 gint byte_end,
62 gint char_start,
63 gint char_end)
64 {
65 GspellEntryWord *word;
66
67 word = _gspell_entry_word_new ();
68 word->word_str = g_strdup (word_str);
69 word->byte_start = byte_start;
70 word->byte_end = byte_end;
71 word->char_start = char_start;
72 word->char_end = char_end;
73
74 return g_slist_append (list, word);
75 }
76
77 static void
78 free_word_list (GSList *list)
79 {
80 g_slist_free_full (list, _gspell_entry_word_free);
81 }
82
83 static void
84 check_entry_word_equal (GspellEntryWord *word1,
85 GspellEntryWord *word2)
86 {
87 g_assert_true (word1 != NULL);
88 g_assert_true (word2 != NULL);
89
90 g_assert_cmpstr (word1->word_str, ==, word2->word_str);
91 g_assert_cmpint (word1->byte_start, ==, word2->byte_start);
92 g_assert_cmpint (word1->byte_end, ==, word2->byte_end);
93
94 if (word1->char_start != -1 &&
95 word2->char_start != -1)
96 {
97 g_assert_cmpint (word1->char_start, ==, word2->char_start);
98 }
99
100 if (word1->char_end != -1 &&
101 word2->char_end != -1)
102 {
103 g_assert_cmpint (word1->char_end, ==, word2->char_end);
104 }
105 }
106
107 static void
108 check_entry_word_list_equal (const GSList *list1,
109 const GSList *list2)
110 {
111 const GSList *l1;
112 const GSList *l2;
113
114 for (l1 = list1, l2 = list2;
115 l1 != NULL && l2 != NULL;
116 l1 = l1->next, l2 = l2->next)
117 {
118 GspellEntryWord *word1 = l1->data;
119 GspellEntryWord *word2 = l2->data;
120
121 check_entry_word_equal (word1, word2);
122 }
123
124 g_assert_true (l1 == NULL);
125 g_assert_true (l2 == NULL);
126 }
127
128 static void
129 test_get_words (void)
130 {
131 GtkEntry *entry;
132 GSList *expected_list;
133 GSList *received_list;
134
135 entry = GTK_ENTRY (gtk_entry_new ());
136 g_object_ref_sink (entry);
137
138 expected_list = NULL;
139 received_list = _gspell_entry_utils_get_words (entry);
140 check_entry_word_list_equal (expected_list, received_list);
141
142 /* Only one word */
143 gtk_entry_set_text (entry, "Finntroll");
144 expected_list = add_word_full (NULL, "Finntroll", 0, 9, 0, 9);
145 received_list = _gspell_entry_utils_get_words (entry);
146 check_entry_word_list_equal (expected_list, received_list);
147 free_word_list (expected_list);
148 free_word_list (received_list);
149
150 /* Only one word, not at the start and end */
151 gtk_entry_set_text (entry, " Finntroll ");
152 expected_list = add_word_full (NULL, "Finntroll", 1, 10, 1, 10);
153 received_list = _gspell_entry_utils_get_words (entry);
154 check_entry_word_list_equal (expected_list, received_list);
155 free_word_list (expected_list);
156 free_word_list (received_list);
157
158 /* Several words */
159 gtk_entry_set_text (entry, "Finntroll - Svart Djup");
160 expected_list = add_word_full (NULL, "Finntroll", 0, 9, 0, 9);
161 expected_list = add_word_full (expected_list, "Svart", 12, 17, 12, 17);
162 expected_list = add_word_full (expected_list, "Djup", 18, 22, 18, 22);
163 received_list = _gspell_entry_utils_get_words (entry);
164 check_entry_word_list_equal (expected_list, received_list);
165 free_word_list (expected_list);
166 free_word_list (received_list);
167
168 /* Multi-byte UTF-8 words */
169 // å takes two bytes.
170 // ö takes two bytes.
171 gtk_entry_set_text (entry, "Asfågelns Död");
172 expected_list = add_word_full (NULL, "Asfågelns", 0, 10, 0, 9);
173 expected_list = add_word_full (expected_list, "Död", 11, 15, 10, 13);
174 received_list = _gspell_entry_utils_get_words (entry);
175 check_entry_word_list_equal (expected_list, received_list);
176 free_word_list (expected_list);
177 free_word_list (received_list);
178
179 /* One apostrophe */
180 gtk_entry_set_text (entry, "doesn't");
181 expected_list = add_word_full (NULL, "doesn't", 0, 7, 0, 7);
182 received_list = _gspell_entry_utils_get_words (entry);
183 check_entry_word_list_equal (expected_list, received_list);
184 free_word_list (expected_list);
185 free_word_list (received_list);
186
187 /* Several apostrophes */
188 gtk_entry_set_text (entry, "rock'n'roll");
189 expected_list = add_word_full (NULL, "rock'n'roll", 0, 11, 0, 11);
190 received_list = _gspell_entry_utils_get_words (entry);
191 check_entry_word_list_equal (expected_list, received_list);
192 free_word_list (expected_list);
193 free_word_list (received_list);
194
195 /* Apostrophe at end of word. Not yet supported. */
196 gtk_entry_set_text (entry, "doin'");
197 expected_list = add_word_full (NULL, "doin", 0, 4, 0, 4);
198 received_list = _gspell_entry_utils_get_words (entry);
199 check_entry_word_list_equal (expected_list, received_list);
200 free_word_list (expected_list);
201 free_word_list (received_list);
202
203 /* Apostrophe at beginning of word. Not yet supported. */
204 gtk_entry_set_text (entry, "'til");
205 expected_list = add_word_full (NULL, "til", 1, 4, 1, 4);
206 received_list = _gspell_entry_utils_get_words (entry);
207 check_entry_word_list_equal (expected_list, received_list);
208 free_word_list (expected_list);
209 free_word_list (received_list);
210
211 /* Dash */
212 gtk_entry_set_text (entry, "spell-checking");
213 expected_list = add_word_full (NULL, "spell-checking", 0, 14, 0, 14);
214 received_list = _gspell_entry_utils_get_words (entry);
215 check_entry_word_list_equal (expected_list, received_list);
216 free_word_list (expected_list);
217 free_word_list (received_list);
218
219 g_object_unref (entry);
220 }
221
222 static void
223 test_init (void)
224 {
225 GtkEntry *gtk_entry;
226 GtkEntryBuffer *gtk_buffer;
227 const GspellLanguage *lang;
228 GspellChecker *checker;
229 GspellEntryBuffer *gspell_buffer;
230 GspellEntry *gspell_entry;
231 GSList *expected_list;
232 const GSList *received_list;
233
234 gtk_entry = GTK_ENTRY (gtk_entry_new ());
235 g_object_ref_sink (gtk_entry);
236
237 /* Set initial text before initializing GspellEntry. */
238 gtk_entry_set_text (gtk_entry, "auienrst");
239
240 lang = gspell_language_lookup ("en_US");
241 g_assert_true (lang != NULL);
242
243 checker = gspell_checker_new (lang);
244 gtk_buffer = gtk_entry_get_buffer (gtk_entry);
245 gspell_buffer = gspell_entry_buffer_get_from_gtk_entry_buffer (gtk_buffer);
246 gspell_entry_buffer_set_spell_checker (gspell_buffer, checker);
247 g_object_unref (checker);
248
249 /* Test just after creating the GspellEntry.
250 * The inline-spell-checking property is FALSE by default, so there are
251 * no misspelled words.
252 */
253 gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
254 expected_list = NULL;
255 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
256 check_entry_word_list_equal (expected_list, received_list);
257
258 gspell_entry_set_inline_spell_checking (gspell_entry, TRUE);
259 expected_list = add_word (NULL, "auienrst", 0, 8);
260 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
261 check_entry_word_list_equal (expected_list, received_list);
262 free_word_list (expected_list);
263
264 g_object_unref (gtk_entry);
265 }
266
267 static void
268 test_inline_spell_checking_property (void)
269 {
270 GtkEntry *gtk_entry;
271 GspellEntry *gspell_entry;
272 GSList *expected_list;
273 const GSList *received_list;
274
275 gtk_entry = create_entry ();
276 gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
277
278 expected_list = NULL;
279 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
280 check_entry_word_list_equal (expected_list, received_list);
281
282 gspell_entry_set_inline_spell_checking (gspell_entry, FALSE);
283 expected_list = NULL;
284 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
285 check_entry_word_list_equal (expected_list, received_list);
286
287 gtk_entry_set_text (gtk_entry, "auienrst");
288 expected_list = NULL;
289 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
290 check_entry_word_list_equal (expected_list, received_list);
291
292 gspell_entry_set_inline_spell_checking (gspell_entry, TRUE);
293 expected_list = add_word (NULL, "auienrst", 0, 8);
294 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
295 check_entry_word_list_equal (expected_list, received_list);
296 free_word_list (expected_list);
297
298 gtk_entry_set_text (gtk_entry, "Hello Död");
299 expected_list = add_word (NULL, "Död", 6, 10);
300 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
301 check_entry_word_list_equal (expected_list, received_list);
302 free_word_list (expected_list);
303
304 gspell_entry_set_inline_spell_checking (gspell_entry, FALSE);
305 expected_list = NULL;
306 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
307 check_entry_word_list_equal (expected_list, received_list);
308
309 g_object_unref (gtk_entry);
310 }
311
312 static void
313 test_buffer_change (void)
314 {
315 GtkEntry *gtk_entry;
316 GtkEntryBuffer *other_buffer;
317 GspellEntry *gspell_entry;
318 GSList *expected_list;
319 const GSList *received_list;
320
321 gtk_entry = create_entry ();
322 gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
323
324 gtk_entry_set_text (gtk_entry, "auienrst");
325 expected_list = add_word (NULL, "auienrst", 0, 8);
326 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
327 check_entry_word_list_equal (expected_list, received_list);
328 free_word_list (expected_list);
329
330 other_buffer = gtk_entry_buffer_new (NULL, -1);
331 gtk_entry_set_buffer (gtk_entry, other_buffer);
332 g_object_unref (other_buffer);
333 expected_list = NULL;
334 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
335 check_entry_word_list_equal (expected_list, received_list);
336
337 g_object_unref (gtk_entry);
338 }
339
340 static void
341 test_spell_checker_change (void)
342 {
343 GtkEntry *gtk_entry;
344 GspellEntry *gspell_entry;
345 gint i;
346
347 gtk_entry = GTK_ENTRY (gtk_entry_new ());
348 g_object_ref_sink (gtk_entry);
349
350 gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
351 gspell_entry_set_inline_spell_checking (gspell_entry, TRUE);
352
353 for (i = 0; i < 2; i++)
354 {
355 GtkEntryBuffer *gtk_buffer;
356 GspellEntryBuffer *gspell_buffer;
357 GSList *expected_list;
358 const GSList *received_list;
359 const GspellLanguage *lang;
360 GspellChecker *checker;
361
362 gtk_buffer = gtk_entry_get_buffer (gtk_entry);
363 gspell_buffer = gspell_entry_buffer_get_from_gtk_entry_buffer (gtk_buffer);
364
365 /* Not yet a spell checker */
366 gtk_entry_set_text (gtk_entry, "auienrst");
367 expected_list = NULL;
368 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
369 check_entry_word_list_equal (expected_list, received_list);
370
371 /* Set a spell checker */
372 lang = gspell_language_lookup ("en_US");
373 g_assert_true (lang != NULL);
374
375 checker = gspell_checker_new (lang);
376 gspell_entry_buffer_set_spell_checker (gspell_buffer, checker);
377 g_object_unref (checker);
378
379 expected_list = add_word (NULL, "auienrst", 0, 8);
380 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
381 check_entry_word_list_equal (expected_list, received_list);
382 free_word_list (expected_list);
383
384 /* Set NULL spell checker */
385 gspell_entry_buffer_set_spell_checker (gspell_buffer, NULL);
386 expected_list = NULL;
387 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
388 check_entry_word_list_equal (expected_list, received_list);
389
390 /* Change buffer for next iteration, to see if the signal
391 * connection for spell-checker changes still works.
392 */
393 {
394 GtkEntryBuffer *new_gtk_buffer;
395
396 new_gtk_buffer = gtk_entry_buffer_new (NULL, -1);
397 gtk_entry_set_buffer (gtk_entry, new_gtk_buffer);
398 g_object_unref (new_gtk_buffer);
399 }
400 }
401
402 g_object_unref (gtk_entry);
403 }
404
405 static void
406 test_language_change (void)
407 {
408 GtkEntry *gtk_entry;
409 GspellEntry *gspell_entry;
410 GtkEntryBuffer *gtk_buffer;
411 GspellEntryBuffer *gspell_buffer;
412 const GspellLanguage *lang;
413 GspellChecker *checker;
414 GSList *expected_list;
415 const GSList *received_list;
416
417 gtk_entry = GTK_ENTRY (gtk_entry_new ());
418 g_object_ref_sink (gtk_entry);
419
420 gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
421 gspell_entry_set_inline_spell_checking (gspell_entry, TRUE);
422
423 gtk_buffer = gtk_entry_get_buffer (gtk_entry);
424 gspell_buffer = gspell_entry_buffer_get_from_gtk_entry_buffer (gtk_buffer);
425
426 lang = gspell_language_lookup ("en_US");
427 g_assert_true (lang != NULL);
428
429 checker = gspell_checker_new (lang);
430 gspell_entry_buffer_set_spell_checker (gspell_buffer, checker);
431
432 gtk_entry_set_text (gtk_entry, "auienrst");
433 expected_list = add_word (NULL, "auienrst", 0, 8);
434 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
435 check_entry_word_list_equal (expected_list, received_list);
436 free_word_list (expected_list);
437
438 _gspell_checker_force_set_language (checker, NULL);
439 expected_list = NULL;
440 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
441 check_entry_word_list_equal (expected_list, received_list);
442
443 g_object_unref (gtk_entry);
444 g_object_unref (checker);
445 }
446
447 static void
448 test_password_mode (void)
449 {
450 GtkEntry *gtk_entry;
451 GspellEntry *gspell_entry;
452 GSList *expected_list;
453 const GSList *received_list;
454
455 gtk_entry = create_entry ();
456 gspell_entry = gspell_entry_get_from_gtk_entry (gtk_entry);
457
458 g_assert_true (gtk_entry_get_visibility (gtk_entry));
459
460 gtk_entry_set_text (gtk_entry, "auienrst");
461 expected_list = add_word (NULL, "auienrst", 0, 8);
462 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
463 check_entry_word_list_equal (expected_list, received_list);
464 free_word_list (expected_list);
465
466 gtk_entry_set_visibility (gtk_entry, FALSE);
467 expected_list = NULL;
468 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
469 check_entry_word_list_equal (expected_list, received_list);
470
471 gtk_entry_set_visibility (gtk_entry, TRUE);
472 expected_list = add_word (NULL, "auienrst", 0, 8);
473 received_list = _gspell_entry_get_misspelled_words (gspell_entry);
474 check_entry_word_list_equal (expected_list, received_list);
475 free_word_list (expected_list);
476
477 g_object_unref (gtk_entry);
478 }
479
480 gint
481 main (gint argc,
482 gchar **argv)
483 {
484 gtk_test_init (&argc, &argv);
485
486 g_test_add_func ("/entry-utils/get-words", test_get_words);
487 g_test_add_func ("/entry/init", test_init);
488 g_test_add_func ("/entry/inline-spell-checking-property", test_inline_spell_checking_property);
489 g_test_add_func ("/entry/buffer-change", test_buffer_change);
490 g_test_add_func ("/entry/spell-checker-change", test_spell_checker_change);
491 g_test_add_func ("/entry/language-change", test_language_change);
492 g_test_add_func ("/entry/password-mode", test_password_mode);
493
494 return g_test_run ();
495 }