Debian Codegraph @

tests/unit-tests/test-text-iter.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-text-iter.h"
6
7 static void
8 check_word_move (gboolean forward,
9 const gchar *text,
10 gint initial_offset,
11 gint expected_result_offset,
12 gboolean expected_ret)
13 {
14 GtkTextBuffer *buffer;
15 GtkTextIter iter;
16 gboolean ret;
17 gint result_offset;
18
19 buffer = gtk_text_buffer_new (NULL);
20 gtk_text_buffer_set_text (buffer, text, -1);
21
22 gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset);
23
24 if (forward)
25 {
26 ret = _gspell_text_iter_forward_word_end (&iter);
27 }
28 else
29 {
30 ret = _gspell_text_iter_backward_word_start (&iter);
31 }
32
33 g_assert_cmpint (ret, ==, expected_ret);
34
35 result_offset = gtk_text_iter_get_offset (&iter);
36 g_assert_cmpint (result_offset, ==, expected_result_offset);
37
38 g_object_unref (buffer);
39 }
40
41 static void
42 test_forward_word_end (void)
43 {
44 gint i;
45
46 for (i = 0; i <= 5; i++)
47 {
48 check_word_move (TRUE, " don't ", i, 6, TRUE);
49 }
50 check_word_move (TRUE, " don't ", 6, 6, FALSE);
51 check_word_move (TRUE, " don't", 0, 6, FALSE);
52
53 /* "goin'" is a valid word, but not yet supported. */
54 for (i = 0; i <= 4; i++)
55 {
56 check_word_move (TRUE, " goin' ", i, 5, TRUE);
57 }
58 check_word_move (TRUE, " goin' ", 5, 5, FALSE);
59 check_word_move (TRUE, " goin' ", 6, 6, FALSE);
60 check_word_move (TRUE, " goin' ", 7, 7, FALSE);
61
62 /* Several apostrophes in the same word. */
63 for (i = 0; i <= 11; i++)
64 {
65 check_word_move (TRUE, " rock'n'roll ", i, 12, TRUE);
66 }
67 check_word_move (TRUE, " rock'n'roll ", 12, 12, FALSE);
68 check_word_move (TRUE, " rock'n'roll", 0, 12, FALSE);
69
70 /* Dashes */
71 for (i = 0; i <= 5; i++)
72 {
73 check_word_move (TRUE, " as-is ", i, 6, TRUE);
74 }
75 check_word_move (TRUE, " as-is ", 6, 6, FALSE);
76 check_word_move (TRUE, " as-is", 0, 6, FALSE);
77
78 /* In "as-", the word boundaries should be around "as". */
79 for (i = 0; i <= 2; i++)
80 {
81 check_word_move (TRUE, " as- ", i, 3, TRUE);
82 }
83 check_word_move (TRUE, " as- ", 3, 3, FALSE);
84 check_word_move (TRUE, " as- ", 4, 4, FALSE);
85 check_word_move (TRUE, " as- ", 5, 5, FALSE);
86
87 /* Several dashes in the same word. */
88 for (i = 0; i <= 11; i++)
89 {
90 check_word_move (TRUE, " rock-n-roll ", i, 12, TRUE);
91 }
92 check_word_move (TRUE, " rock-n-roll ", 12, 12, FALSE);
93 check_word_move (TRUE, " rock-n-roll", 0, 12, FALSE);
94 }
95
96 static void
97 test_backward_word_start (void)
98 {
99 gint i;
100
101 for (i = 7; i >= 2; i--)
102 {
103 check_word_move (FALSE, " don't ", i, 1, TRUE);
104 }
105 check_word_move (FALSE, " don't ", 1, 1, FALSE);
106 check_word_move (FALSE, " don't ", 0, 0, FALSE);
107
108 /* "'til" is a valid word, but not yet supported. */
109 for (i = 6; i >= 3; i--)
110 {
111 check_word_move (FALSE, " 'til ", i, 2, TRUE);
112 }
113 check_word_move (FALSE, " 'til ", 2, 2, FALSE);
114 check_word_move (FALSE, " 'til ", 1, 1, FALSE);
115 check_word_move (FALSE, " 'til ", 0, 0, FALSE);
116
117 /* Several apostrophes in the same word. */
118 for (i = 13; i >= 2; i--)
119 {
120 check_word_move (FALSE, " rock'n'roll ", i, 1, TRUE);
121 }
122 check_word_move (FALSE, " rock'n'roll ", 1, 1, FALSE);
123 check_word_move (FALSE, " rock'n'roll ", 0, 0, FALSE);
124
125 /* Dashes */
126 for (i = 7; i >= 2; i--)
127 {
128 check_word_move (FALSE, " as-is ", i, 1, TRUE);
129 }
130 check_word_move (FALSE, " as-is ", 1, 1, FALSE);
131 check_word_move (FALSE, " as-is ", 0, 0, FALSE);
132
133 /* In "-as", the word boundaries should be around "as". */
134 for (i = 5; i >= 3; i--)
135 {
136 check_word_move (FALSE, " -as ", i, 2, TRUE);
137 }
138 check_word_move (FALSE, " -as ", 2, 2, FALSE);
139 check_word_move (FALSE, " -as ", 1, 1, FALSE);
140 check_word_move (FALSE, " -as ", 0, 0, FALSE);
141
142 /* Several dashes in the same word. */
143 for (i = 13; i >= 2; i--)
144 {
145 check_word_move (FALSE, " rock-n-roll ", i, 1, TRUE);
146 }
147 check_word_move (FALSE, " rock-n-roll ", 1, 1, FALSE);
148 check_word_move (FALSE, " rock-n-roll ", 0, 0, FALSE);
149 }
150
151 static void
152 check_properties (GtkTextBuffer *buffer,
153 gint offset,
154 gboolean expected_starts_word,
155 gboolean expected_ends_word,
156 gboolean expected_inside_word)
157 {
158 GtkTextIter iter;
159
160 gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
161
162 g_assert_cmpint (expected_starts_word, ==, _gspell_text_iter_starts_word (&iter));
163 g_assert_cmpint (expected_ends_word, ==, _gspell_text_iter_ends_word (&iter));
164 g_assert_cmpint (expected_inside_word, ==, _gspell_text_iter_inside_word (&iter));
165 }
166
167 static void
168 test_properties (void)
169 {
170 GtkTextBuffer *buffer;
171 gint i;
172
173 buffer = gtk_text_buffer_new (NULL);
174
175 gtk_text_buffer_set_text (buffer, " don't ", -1);
176 check_properties (buffer, 0, FALSE, FALSE, FALSE);
177 check_properties (buffer, 1, TRUE, FALSE, TRUE);
178 for (i = 2; i <= 5; i++)
179 {
180 check_properties (buffer, i, FALSE, FALSE, TRUE);
181 }
182 check_properties (buffer, 6, FALSE, TRUE, FALSE);
183 check_properties (buffer, 7, FALSE, FALSE, FALSE);
184
185 /* "goin'" is a valid word, but not yet supported. */
186 gtk_text_buffer_set_text (buffer, " goin' ", -1);
187 check_properties (buffer, 0, FALSE, FALSE, FALSE);
188 check_properties (buffer, 1, TRUE, FALSE, TRUE);
189 for (i = 2; i <= 4; i++)
190 {
191 check_properties (buffer, i, FALSE, FALSE, TRUE);
192 }
193 check_properties (buffer, 5, FALSE, TRUE, FALSE);
194 check_properties (buffer, 6, FALSE, FALSE, FALSE);
195 check_properties (buffer, 7, FALSE, FALSE, FALSE);
196
197 /* "'til" is a valid word, but not yet supported. */
198 gtk_text_buffer_set_text (buffer, " 'til ", -1);
199 check_properties (buffer, 0, FALSE, FALSE, FALSE);
200 check_properties (buffer, 1, FALSE, FALSE, FALSE);
201 check_properties (buffer, 2, TRUE, FALSE, TRUE);
202 check_properties (buffer, 3, FALSE, FALSE, TRUE);
203 check_properties (buffer, 4, FALSE, FALSE, TRUE);
204 check_properties (buffer, 5, FALSE, TRUE, FALSE);
205 check_properties (buffer, 6, FALSE, FALSE, FALSE);
206
207 /* Several apostrophes in the same word. */
208 gtk_text_buffer_set_text (buffer, " rock'n'roll ", -1);
209 check_properties (buffer, 0, FALSE, FALSE, FALSE);
210 check_properties (buffer, 1, TRUE, FALSE, TRUE);
211 for (i = 2; i <= 11; i++)
212 {
213 check_properties (buffer, i, FALSE, FALSE, TRUE);
214 }
215 check_properties (buffer, 12, FALSE, TRUE, FALSE);
216 check_properties (buffer, 13, FALSE, FALSE, FALSE);
217
218 g_object_unref (buffer);
219 }
220
221 gint
222 main (gint argc,
223 gchar **argv)
224 {
225 gtk_test_init (&argc, &argv);
226
227 g_test_add_func ("/text-iter/forward-word-end", test_forward_word_end);
228 g_test_add_func ("/text-iter/backward-word-start", test_backward_word_start);
229 g_test_add_func ("/text-iter/starts-ends-inside-word", test_properties);
230
231 return g_test_run ();
232 }