File: test-engine.c

package info (click to toggle)
libspelling 0.4.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: ansic: 7,367; javascript: 33; sh: 21; makefile: 14
file content (333 lines) | stat: -rw-r--r-- 7,768 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* test-engine.c
 *
 * Copyright 2024 Christian Hergert <chergert@redhat.com>
 *
 * 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 General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <libspelling.h>

#include "spelling-engine-private.h"

static GString *buffer;
static GtkBitset *mispelled;
static SpellingDictionary *dictionary;
static guint cursor;

typedef struct
{
  GString *str;
  const char *pos;
  guint offset;
  gunichar ch;
} StringIter;

static inline gboolean
string_iter_is_end (StringIter *iter)
{
  return iter->pos >= iter->str->str + iter->str->len;
}

static inline void
string_iter_init (StringIter *iter,
                  GString    *str,
                  guint       offset)
{
  iter->str = str;
  iter->pos = g_utf8_offset_to_pointer (str->str, offset);
  iter->offset = offset;
  iter->ch = g_utf8_get_char (iter->pos);
}

static inline gboolean
string_iter_backward (StringIter *iter)
{
  if (iter->pos <= iter->str->str)
    return FALSE;

  iter->pos = g_utf8_prev_char (iter->pos);
  iter->ch = g_utf8_get_char (iter->pos);
  iter->offset--;

  return TRUE;
}

static inline gboolean
string_iter_forward (StringIter *iter)
{
  if (iter->pos >= iter->str->str + iter->str->len)
    return FALSE;

  iter->pos = g_utf8_next_char (iter->pos);
  iter->ch = g_utf8_get_char (iter->pos);
  iter->offset++;

  return TRUE;
}

static char *
copy_text (gpointer instance,
           guint    position,
           guint    length)
{
  const char *begin = g_utf8_offset_to_pointer (buffer->str, position);
  const char *end = g_utf8_offset_to_pointer (begin, length);

  return g_strndup (begin, end - begin);
}

static void
clear_tag (gpointer instance,
           guint    position,
           guint    length)
{
  gtk_bitset_remove_range (mispelled, position, length);
}

static void
apply_tag (gpointer instance,
           guint    position,
           guint    length)
{
  gtk_bitset_add_range (mispelled, position, length);
}

static gboolean
is_word_char (gunichar ch)
{
  const char *extra_word_chars = spelling_dictionary_get_extra_word_chars (dictionary);

  for (const char *c = extra_word_chars; c && *c; c = g_utf8_next_char (c))
    {
      if (ch == g_utf8_get_char (c))
        return TRUE;
    }

  return g_unichar_isalnum (ch) || ch == '_';
}

static gboolean
backward_word_start (gpointer  instance,
                     guint    *position)
{
  StringIter iter;
  StringIter peek;

  string_iter_init (&iter, buffer, *position);

  /* Move back one char first */
  if (!string_iter_backward (&iter))
    return FALSE;

  /* If we have left a word (into space, etc), walk back until
   * we get to the end of a word.
   */
  if (!is_word_char (iter.ch))
    {
      while (string_iter_backward (&iter))
        {
          if (!is_word_char (iter.ch))
            continue;
          break;
        }

      if (iter.offset == 0 && !is_word_char (iter.ch))
        return FALSE;
    }

  /* Walk backwards saving our last valid position */
  peek = iter;
  while (string_iter_backward (&peek) && is_word_char (peek.ch))
    iter = peek;

  *position = iter.offset;

  return TRUE;
}

static gboolean
forward_word_end (gpointer  instance,
                  guint    *position)
{
  StringIter iter;

  string_iter_init (&iter, buffer, *position);

  /* Move forward one char first */
  if (!string_iter_forward (&iter))
    return FALSE;

  /* If we are no longer on a word character, then walk forward
   * until we reach a word character.
   */
  if (!is_word_char (iter.ch))
    {
      while (string_iter_forward (&iter))
        {
          if (!is_word_char (iter.ch))
            continue;
          break;
        }

      if (string_iter_is_end (&iter))
        return FALSE;
    }

  /* Walk forward saving our last valid position */
  while (string_iter_forward (&iter))
    if (!is_word_char (iter.ch))
      break;

  *position = iter.offset;

  return TRUE;
}

static void
intersect_spellcheck_region (gpointer   instance,
                             GtkBitset *bitset)
{
}

static guint
get_cursor (gpointer instance)
{
  return cursor;
}

static PangoLanguage *
get_language (gpointer instance)
{
  return pango_language_get_default ();
}

static SpellingDictionary *
get_dictionary (gpointer instance)
{
  return dictionary;
}

static gboolean
check_enabled (gpointer instance)
{
  return TRUE;
}

static const SpellingAdapter adapter = {
  .check_enabled = check_enabled,
  .get_cursor = get_cursor,
  .copy_text = copy_text,
  .clear_tag = clear_tag,
  .apply_tag = apply_tag,
  .backward_word_start = backward_word_start,
  .forward_word_end = forward_word_end,
  .intersect_spellcheck_region = intersect_spellcheck_region,
  .get_dictionary = get_dictionary,
  .get_language = get_language,
};

static inline void
assert_string (const char *str)
{
  g_assert_cmpstr (buffer->str, ==, str);
}

static void
insert (SpellingEngine *engine,
        const char     *text,
        guint           position,
        const char     *expected)
{
  const char *ptr;
  guint n_chars;

  if (position == 0)
    ptr = buffer->str;
  else
    ptr = g_utf8_offset_to_pointer (buffer->str, position);

  n_chars = g_utf8_strlen (text, -1);

  spelling_engine_before_insert_text (engine, position, n_chars);
  g_string_insert (buffer, ptr - buffer->str, text);
  gtk_bitset_splice (mispelled, position, 0, n_chars);
  cursor = position + n_chars;
  spelling_engine_after_insert_text (engine, position, n_chars);

  if (expected)
    assert_string (expected);

  spelling_engine_iteration (engine);
}

static void
delete (SpellingEngine *engine,
        guint           position,
        guint           n_chars,
        const char     *expected)
{
  const char *ptr = g_utf8_offset_to_pointer (buffer->str, position);
  const char *endptr = g_utf8_offset_to_pointer (ptr, n_chars);

  cursor = position;
  spelling_engine_before_delete_range (engine, position, n_chars);
  gtk_bitset_splice (mispelled, position, n_chars, 0);
  g_string_erase (buffer, ptr - buffer->str, endptr - ptr);
  spelling_engine_after_delete_range (engine, position);

  if (expected)
    assert_string (expected);

  spelling_engine_iteration (engine);
}

static void
test_engine_basic (void)
{
  g_autoptr(SpellingEngine) engine = NULL;
  g_autoptr(GObject) instance = g_object_new (G_TYPE_OBJECT, NULL);
  SpellingProvider *provider = spelling_provider_get_default ();
  const char *default_code = spelling_provider_get_default_code (provider);

  dictionary = spelling_provider_load_dictionary (provider, default_code);

  buffer = g_string_new (NULL);
  mispelled = gtk_bitset_new_empty ();
  engine = spelling_engine_new (&adapter, instance);

  insert (engine, "2", 0, "2");
  insert (engine, "1", 0, "12");
  insert (engine, "0", 0, "012");

  delete (engine, 2, 1, "01");
  delete (engine, 1, 1, "0");
  delete (engine, 0, 1, "");

  g_string_free (buffer, TRUE);
  gtk_bitset_unref (mispelled);
  g_object_unref (dictionary);
}

int
main (int argc,
      char *argv[])
{
  g_test_init (&argc, &argv, NULL);
  g_test_add_func ("/Spelling/Engine/basic", test_engine_basic);
  return g_test_run ();
}