| 1 |
/* GTK - The GIMP Toolkit |
| 2 |
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
#include <stdio.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <gtk/gtk.h> |
| 23 |
|
| 24 |
static void |
| 25 |
test_empty_search () |
| 26 |
{ |
| 27 |
GtkTextBuffer *buffer; |
| 28 |
GtkTextIter it, s, e; |
| 29 |
gboolean res; |
| 30 |
|
| 31 |
buffer = gtk_text_buffer_new (NULL); |
| 32 |
gtk_text_buffer_set_text (buffer, "This is some foo text", -1); |
| 33 |
|
| 34 |
/* search from start forward */ |
| 35 |
gtk_text_buffer_get_start_iter (buffer, &it); |
| 36 |
res = gtk_text_iter_forward_search (&it, "", 0, &s, &e, NULL); |
| 37 |
g_assert (res); |
| 38 |
g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, gtk_text_iter_get_offset (&e)); |
| 39 |
g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, 1); |
| 40 |
|
| 41 |
/* search from end backward */ |
| 42 |
gtk_text_buffer_get_end_iter (buffer, &it); |
| 43 |
res = gtk_text_iter_backward_search (&it, "", 0, &s, &e, NULL); |
| 44 |
g_assert (res); |
| 45 |
g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, gtk_text_iter_get_offset (&e)); |
| 46 |
g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, 20); |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
check_found_forward (const gchar *haystack, |
| 51 |
const gchar *needle, |
| 52 |
GtkTextSearchFlags flags, |
| 53 |
int expected_start, |
| 54 |
int expected_end, |
| 55 |
const gchar *expected_string) |
| 56 |
{ |
| 57 |
GtkTextBuffer *buffer; |
| 58 |
GtkTextIter i, s, e; |
| 59 |
gboolean res; |
| 60 |
gchar *text; |
| 61 |
|
| 62 |
buffer = gtk_text_buffer_new (NULL); |
| 63 |
|
| 64 |
gtk_text_buffer_set_text (buffer, haystack, -1); |
| 65 |
|
| 66 |
/* TODO: add test with limit before, after and in the middle |
| 67 |
of expected start and end */ |
| 68 |
|
| 69 |
/* search from start forward */ |
| 70 |
gtk_text_buffer_get_start_iter (buffer, &i); |
| 71 |
res = gtk_text_iter_forward_search (&i, needle, flags, &s, &e, NULL); |
| 72 |
g_assert (res); |
| 73 |
g_assert_cmpint (expected_start, ==, gtk_text_iter_get_offset (&s)); |
| 74 |
g_assert_cmpint (expected_end, ==, gtk_text_iter_get_offset (&e)); |
| 75 |
text = gtk_text_iter_get_text (&s, &e); |
| 76 |
g_assert_cmpstr (expected_string, ==, text); |
| 77 |
g_free (text); |
| 78 |
|
| 79 |
g_object_unref (buffer); |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
check_found_backward (const gchar *haystack, |
| 84 |
const gchar *needle, |
| 85 |
GtkTextSearchFlags flags, |
| 86 |
int expected_start, |
| 87 |
int expected_end, |
| 88 |
const gchar *expected_string) |
| 89 |
{ |
| 90 |
GtkTextBuffer *buffer; |
| 91 |
GtkTextIter i, s, e; |
| 92 |
gboolean res; |
| 93 |
gchar *text; |
| 94 |
|
| 95 |
buffer = gtk_text_buffer_new (NULL); |
| 96 |
|
| 97 |
gtk_text_buffer_set_text (buffer, haystack, -1); |
| 98 |
|
| 99 |
/* search from end backward */ |
| 100 |
gtk_text_buffer_get_end_iter (buffer, &i); |
| 101 |
res = gtk_text_iter_backward_search (&i, needle, flags, &s, &e, NULL); |
| 102 |
g_assert (res); |
| 103 |
g_assert_cmpint (expected_start, ==, gtk_text_iter_get_offset (&s)); |
| 104 |
g_assert_cmpint (expected_end, ==, gtk_text_iter_get_offset (&e)); |
| 105 |
text = gtk_text_iter_get_text (&s, &e); |
| 106 |
g_assert_cmpstr (expected_string, ==, text); |
| 107 |
g_free (text); |
| 108 |
|
| 109 |
g_object_unref (buffer); |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
check_not_found (const gchar *haystack, |
| 114 |
const gchar *needle, |
| 115 |
GtkTextSearchFlags flags) |
| 116 |
{ |
| 117 |
GtkTextBuffer *buffer; |
| 118 |
GtkTextIter i, s, e; |
| 119 |
gboolean res; |
| 120 |
|
| 121 |
buffer = gtk_text_buffer_new (NULL); |
| 122 |
|
| 123 |
gtk_text_buffer_set_text (buffer, haystack, -1); |
| 124 |
|
| 125 |
/* search from start forward */ |
| 126 |
gtk_text_buffer_get_start_iter (buffer, &i); |
| 127 |
res = gtk_text_iter_forward_search (&i, needle, flags, &s, &e, NULL); |
| 128 |
g_assert (res == FALSE); |
| 129 |
|
| 130 |
/* search from end backward */ |
| 131 |
gtk_text_buffer_get_end_iter (buffer, &i); |
| 132 |
res = gtk_text_iter_backward_search (&i, needle, flags, &s, &e, NULL); |
| 133 |
g_assert (res == FALSE); |
| 134 |
|
| 135 |
g_object_unref (buffer); |
| 136 |
} |
| 137 |
|
| 138 |
static void |
| 139 |
test_search_full_buffer (void) |
| 140 |
{ |
| 141 |
check_found_forward ("foo", "foo", 0, 0, 3, "foo"); |
| 142 |
check_found_backward ("foo", "foo", 0, 0, 3, "foo"); |
| 143 |
check_found_forward ("foo", "foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo"); |
| 144 |
check_found_backward ("foo", "foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo"); |
| 145 |
check_found_forward ("foo", "Foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo"); |
| 146 |
check_found_backward ("foo", "Foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo"); |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
test_search (void) |
| 151 |
{ |
| 152 |
/* simple match */ |
| 153 |
check_found_forward ("This is some foo text", "foo", 0, 13, 16, "foo"); |
| 154 |
check_found_backward ("This is some foo text", "foo", 0, 13, 16, "foo"); |
| 155 |
check_not_found ("This is some foo text", "Foo", 0); |
| 156 |
|
| 157 |
/* different matches for forward and backward */ |
| 158 |
check_found_forward ("This is some foo foo text", "foo", 0, 13, 16, "foo"); |
| 159 |
check_found_backward ("This is some foo foo text", "foo", 0, 17, 20, "foo"); |
| 160 |
|
| 161 |
/* new lines in the haystack */ |
| 162 |
check_found_forward ("This is some\nfoo text", "foo", 0, 13, 16, "foo"); |
| 163 |
check_found_backward ("This is some\nfoo text", "foo", 0, 13, 16, "foo"); |
| 164 |
check_found_forward ("This is some foo\nfoo text", "foo", 0, 13, 16, "foo"); |
| 165 |
check_found_backward ("This is some foo\nfoo text", "foo", 0, 17, 20, "foo"); |
| 166 |
check_not_found ("This is some\nfoo text", "Foo", 0); |
| 167 |
|
| 168 |
/* end of buffer */ |
| 169 |
check_found_forward ("This is some\ntext foo", "foo", 0, 18, 21, "foo"); |
| 170 |
check_found_backward ("This is some\ntext foo", "foo", 0, 18, 21, "foo"); |
| 171 |
check_not_found ("This is some\ntext foo", "Foo", 0); |
| 172 |
|
| 173 |
/* multiple lines in the needle */ |
| 174 |
check_found_forward ("This is some foo\nfoo text", "foo\nfoo", 0, 13, 20, "foo\nfoo"); |
| 175 |
check_found_backward ("This is some foo\nfoo text", "foo\nfoo", 0, 13, 20, "foo\nfoo"); |
| 176 |
check_not_found ("This is some foo\nfoo text", "Foo\nfoo", 0); |
| 177 |
|
| 178 |
/* check also that different composition of utf8 characters |
| 179 |
(e.g. accented letters) match */ |
| 180 |
|
| 181 |
check_found_forward ("This is some \303\200 text", "\303\200", 0, 13, 14, "\303\200"); |
| 182 |
check_found_forward ("This is some \303\200 text", "some \303\200", 0, 8, 14, "some \303\200"); |
| 183 |
check_found_forward ("This is some \303\200 text", "\303\200 text", 0, 13, 19, "\303\200 text"); |
| 184 |
check_found_forward ("This is some \303\200 text", "some \303\200 text", 0, 8, 19, "some \303\200 text"); |
| 185 |
check_found_backward ("This is some \303\240 text", "\303\240", 0, 13, 14, "\303\240"); |
| 186 |
check_found_backward ("This is some \303\240 text", "some \303\240", 0, 8, 14, "some \303\240"); |
| 187 |
check_found_backward ("This is some \303\240 text", "\303\240 text", 0, 13, 19, "\303\240 text"); |
| 188 |
check_found_backward ("This is some \303\240 text", "some \303\240 text", 0, 8, 19, "some \303\240 text"); |
| 189 |
|
| 190 |
/* multi-byte characters outside the needle */ |
| 191 |
check_found_forward ("\303\200 aa", "aa", 0, 2, 4, "aa"); |
| 192 |
check_found_forward ("aa \303\200", "aa", 0, 0, 2, "aa"); |
| 193 |
check_found_backward ("\303\200 aa", "aa", 0, 2, 4, "aa"); |
| 194 |
check_found_backward ("aa \303\200", "aa", 0, 0, 2, "aa"); |
| 195 |
} |
| 196 |
|
| 197 |
static void |
| 198 |
test_search_caseless (void) |
| 199 |
{ |
| 200 |
GtkTextSearchFlags flags; |
| 201 |
|
| 202 |
flags = GTK_TEXT_SEARCH_CASE_INSENSITIVE; |
| 203 |
|
| 204 |
/* simple match */ |
| 205 |
check_found_forward ("This is some foo text", "foo", flags, 13, 16, "foo"); |
| 206 |
check_found_forward ("This is some foo text", "Foo", flags, 13, 16, "foo"); |
| 207 |
check_found_forward ("This is some Foo text", "foo", flags, 13, 16, "Foo"); |
| 208 |
check_found_backward ("This is some foo text", "foo", flags, 13, 16, "foo"); |
| 209 |
check_found_backward ("This is some foo text", "Foo", flags, 13, 16, "foo"); |
| 210 |
check_found_backward ("This is some Foo text", "foo", flags, 13, 16, "Foo"); |
| 211 |
|
| 212 |
/* check also that different composition of utf8 characters |
| 213 |
(e.g. accented letters) match */ |
| 214 |
|
| 215 |
/* different matches for forward and backward */ |
| 216 |
check_found_forward ("This is some foo foo text", "foo", flags, 13, 16, "foo"); |
| 217 |
check_found_forward ("This is some foo foo text", "Foo", flags, 13, 16, "foo"); |
| 218 |
check_found_forward ("This is some Foo foo text", "foo", flags, 13, 16, "Foo"); |
| 219 |
check_found_forward ("This is some \303\200 \303\240 text", "\303\240", flags, 13, 14, "\303\200"); |
| 220 |
check_found_forward ("This is some \303\200 \303\240 text", "\303\200", flags, 13, 14, "\303\200"); |
| 221 |
check_found_forward ("This is some \303\200 \303\240 text", "a\314\200", flags, 13, 14, "\303\200"); |
| 222 |
check_found_backward ("This is some foo foo text", "foo", flags, 17, 20, "foo"); |
| 223 |
check_found_backward ("This is some foo foo text", "Foo", flags, 17, 20, "foo"); |
| 224 |
check_found_backward ("This is some foo Foo text", "foo", flags, 17, 20, "Foo"); |
| 225 |
check_found_backward ("This is some \303\200 \303\240 text", "\303\240", flags, 15, 16, "\303\240"); |
| 226 |
check_found_backward ("This is some \303\200 \303\240 text", "\303\200", flags, 15, 16, "\303\240"); |
| 227 |
check_found_backward ("This is some \303\200 \303\240 text", "a\314\200", flags, 15, 16, "\303\240"); |
| 228 |
|
| 229 |
/* new lines in the haystack */ |
| 230 |
check_found_forward ("This is some\nfoo text", "foo", flags, 13, 16, "foo"); |
| 231 |
check_found_forward ("This is some\nfoo text", "Foo", flags, 13, 16, "foo"); |
| 232 |
check_found_forward ("This is some\nFoo text", "foo", flags, 13, 16, "Foo"); |
| 233 |
check_found_forward ("This is some\n\303\200 text", "\303\240", flags, 13, 14, "\303\200"); |
| 234 |
check_found_forward ("This is some\n\303\200 text", "a\314\200", flags, 13, 14, "\303\200"); |
| 235 |
check_found_backward ("This is some\nfoo text", "foo", flags, 13, 16, "foo"); |
| 236 |
check_found_backward ("This is some\nfoo text", "Foo", flags, 13, 16, "foo"); |
| 237 |
check_found_backward ("This is some\nFoo text", "foo", flags, 13, 16, "Foo"); |
| 238 |
check_found_backward ("This is some\n\303\200 text", "\303\240", flags, 13, 14, "\303\200"); |
| 239 |
check_found_backward ("This is some\n\303\200 text", "a\314\200", flags, 13, 14, "\303\200"); |
| 240 |
check_found_forward ("This is some foo\nfoo text", "foo", flags, 13, 16, "foo"); |
| 241 |
check_found_forward ("This is some foo\nfoo text", "Foo", flags, 13, 16, "foo"); |
| 242 |
check_found_forward ("This is some Foo\nfoo text", "foo", flags, 13, 16, "Foo"); |
| 243 |
check_found_forward ("This is some \303\200\n\303\200 text", "\303\240", flags, 13, 14, "\303\200"); |
| 244 |
check_found_forward ("This is some \303\200\n\303\200 text", "a\314\200", flags, 13, 14, "\303\200"); |
| 245 |
check_found_backward ("This is some foo\nfoo text", "foo", flags, 17, 20, "foo"); |
| 246 |
check_found_backward ("This is some foo\nfoo text", "Foo", flags, 17, 20, "foo"); |
| 247 |
check_found_backward ("This is some foo\nFoo text", "foo", flags, 17, 20, "Foo"); |
| 248 |
check_found_backward ("This is some \303\200\n\303\200 text", "\303\240", flags, 15, 16, "\303\200"); |
| 249 |
check_found_backward ("This is some \303\200\n\303\200 text", "a\314\200", flags, 15, 16, "\303\200"); |
| 250 |
|
| 251 |
/* end of buffer */ |
| 252 |
check_found_forward ("This is some\ntext foo", "foo", flags, 18, 21, "foo"); |
| 253 |
check_found_forward ("This is some\ntext foo", "Foo", flags, 18, 21, "foo"); |
| 254 |
check_found_forward ("This is some\ntext Foo", "foo", flags, 18, 21, "Foo"); |
| 255 |
check_found_forward ("This is some\ntext \303\200", "\303\240", flags, 18, 19, "\303\200"); |
| 256 |
check_found_forward ("This is some\ntext \303\200", "a\314\200", flags, 18, 19, "\303\200"); |
| 257 |
check_found_backward ("This is some\ntext foo", "foo", flags, 18, 21, "foo"); |
| 258 |
check_found_backward ("This is some\ntext foo", "Foo", flags, 18, 21, "foo"); |
| 259 |
check_found_backward ("This is some\ntext Foo", "foo", flags, 18, 21, "Foo"); |
| 260 |
check_found_backward ("This is some\ntext \303\200", "\303\240", flags, 18, 19, "\303\200"); |
| 261 |
check_found_backward ("This is some\ntext \303\200", "a\314\200", flags, 18, 19, "\303\200"); |
| 262 |
|
| 263 |
/* multiple lines in the needle */ |
| 264 |
check_found_forward ("This is some foo\nfoo text", "foo\nfoo", flags, 13, 20, "foo\nfoo"); |
| 265 |
check_found_forward ("This is some foo\nfoo text", "Foo\nFoo", flags, 13, 20, "foo\nfoo"); |
| 266 |
check_found_forward ("This is some Foo\nFoo text", "foo\nfoo", flags, 13, 20, "Foo\nFoo"); |
| 267 |
check_found_forward ("This is some \303\200\n\303\200 text", "\303\240\n\303\240", flags, 13, 16, "\303\200\n\303\200"); |
| 268 |
check_found_forward ("This is some \303\200\n\303\200 text", "a\314\200\na\314\200", flags, 13, 16, "\303\200\n\303\200"); |
| 269 |
check_found_backward ("This is some foo\nfoo text", "foo\nfoo", flags, 13, 20, "foo\nfoo"); |
| 270 |
check_found_backward ("This is some foo\nfoo text", "Foo\nFoo", flags, 13, 20, "foo\nfoo"); |
| 271 |
check_found_backward ("This is some Foo\nFoo text", "foo\nfoo", flags, 13, 20, "Foo\nFoo"); |
| 272 |
check_found_backward ("This is some \303\200\n\303\200 text", "\303\240\n\303\240", flags, 13, 16, "\303\200\n\303\200"); |
| 273 |
check_found_backward ("This is some \303\200\n\303\200 text", "a\314\200\na\314\200", flags, 13, 16, "\303\200\n\303\200"); |
| 274 |
|
| 275 |
/* multi-byte characters outside the needle */ |
| 276 |
check_found_forward ("\303\200 aa", "aa", flags, 2, 4, "aa"); |
| 277 |
check_found_forward ("aa \303\200", "aa", flags, 0, 2, "aa"); |
| 278 |
check_found_backward ("\303\200 aa", "aa", flags, 2, 4, "aa"); |
| 279 |
check_found_backward ("aa \303\200", "aa", flags, 0, 2, "aa"); |
| 280 |
} |
| 281 |
|
| 282 |
static void |
| 283 |
test_forward_to_tag_toggle (void) |
| 284 |
{ |
| 285 |
GtkTextBuffer *buffer; |
| 286 |
GtkTextTag *bold_tag; |
| 287 |
GtkTextTag *editable_tag; |
| 288 |
GtkTextIter iter; |
| 289 |
gint offset; |
| 290 |
|
| 291 |
buffer = gtk_text_buffer_new (NULL); |
| 292 |
|
| 293 |
bold_tag = gtk_text_buffer_create_tag (buffer, "bold", |
| 294 |
"weight", PANGO_WEIGHT_BOLD, |
| 295 |
NULL); |
| 296 |
|
| 297 |
editable_tag = gtk_text_buffer_create_tag (buffer, "not-editable", |
| 298 |
"editable", FALSE, |
| 299 |
NULL); |
| 300 |
|
| 301 |
gtk_text_buffer_get_start_iter (buffer, &iter); |
| 302 |
|
| 303 |
gtk_text_buffer_insert (buffer, &iter, "a", -1); |
| 304 |
gtk_text_buffer_insert_with_tags (buffer, &iter, "b", -1, bold_tag, NULL); |
| 305 |
gtk_text_buffer_insert_with_tags (buffer, &iter, "c", -1, editable_tag, NULL); |
| 306 |
|
| 307 |
/* Go to the first "on" toggle */ |
| 308 |
gtk_text_buffer_get_start_iter (buffer, &iter); |
| 309 |
g_assert (gtk_text_iter_forward_to_tag_toggle (&iter, NULL)); |
| 310 |
offset = gtk_text_iter_get_offset (&iter); |
| 311 |
g_assert_cmpint (offset, ==, 1); |
| 312 |
|
| 313 |
/* Go to the last "off" toggle for the bold tag */ |
| 314 |
g_assert (gtk_text_iter_forward_to_tag_toggle (&iter, bold_tag)); |
| 315 |
offset = gtk_text_iter_get_offset (&iter); |
| 316 |
g_assert_cmpint (offset, ==, 2); |
| 317 |
|
| 318 |
g_assert (!gtk_text_iter_forward_to_tag_toggle (&iter, bold_tag)); |
| 319 |
|
| 320 |
/* Go to the first "on" toggle for the editable tag */ |
| 321 |
gtk_text_buffer_get_start_iter (buffer, &iter); |
| 322 |
g_assert (gtk_text_iter_forward_to_tag_toggle (&iter, editable_tag)); |
| 323 |
offset = gtk_text_iter_get_offset (&iter); |
| 324 |
g_assert_cmpint (offset, ==, 2); |
| 325 |
|
| 326 |
/* Test with the end iter */ |
| 327 |
gtk_text_buffer_get_end_iter (buffer, &iter); |
| 328 |
g_assert (!gtk_text_iter_forward_to_tag_toggle (&iter, editable_tag)); |
| 329 |
|
| 330 |
g_object_unref (buffer); |
| 331 |
} |
| 332 |
|
| 333 |
static void |
| 334 |
check_forward_line_end (const gchar *buffer_text, |
| 335 |
gint initial_offset, |
| 336 |
gint result_offset, |
| 337 |
gboolean ret) |
| 338 |
{ |
| 339 |
GtkTextBuffer *buffer; |
| 340 |
GtkTextIter iter; |
| 341 |
|
| 342 |
buffer = gtk_text_buffer_new (NULL); |
| 343 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 344 |
|
| 345 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 346 |
|
| 347 |
g_assert_cmpint (ret, ==, gtk_text_iter_forward_to_line_end (&iter)); |
| 348 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 349 |
|
| 350 |
g_object_unref (buffer); |
| 351 |
} |
| 352 |
|
| 353 |
static void |
| 354 |
test_forward_to_line_end (void) |
| 355 |
{ |
| 356 |
check_forward_line_end("a", 0, 1, FALSE); |
| 357 |
check_forward_line_end("a\n", 0, 1, TRUE); |
| 358 |
check_forward_line_end("a\r\n", 0, 1, TRUE); |
| 359 |
check_forward_line_end("a\na\n", 1, 3, TRUE); |
| 360 |
check_forward_line_end("a\na\n\n", 1, 3, TRUE); |
| 361 |
check_forward_line_end("a\r\na\n", 1, 4, TRUE); |
| 362 |
check_forward_line_end("a\r\na\r\n\r\n", 1, 4, TRUE); |
| 363 |
} |
| 364 |
|
| 365 |
static void |
| 366 |
check_word_boundaries (const gchar *buffer_text, |
| 367 |
gint offset, |
| 368 |
gboolean starts_word, |
| 369 |
gboolean ends_word, |
| 370 |
gboolean inside_word) |
| 371 |
{ |
| 372 |
GtkTextBuffer *buffer; |
| 373 |
GtkTextIter iter; |
| 374 |
|
| 375 |
buffer = gtk_text_buffer_new (NULL); |
| 376 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 377 |
|
| 378 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset); |
| 379 |
|
| 380 |
g_assert_cmpint (starts_word, ==, gtk_text_iter_starts_word (&iter)); |
| 381 |
g_assert_cmpint (ends_word, ==, gtk_text_iter_ends_word (&iter)); |
| 382 |
g_assert_cmpint (inside_word, ==, gtk_text_iter_inside_word (&iter)); |
| 383 |
|
| 384 |
g_object_unref (buffer); |
| 385 |
} |
| 386 |
|
| 387 |
static void |
| 388 |
check_forward_word_end (const gchar *buffer_text, |
| 389 |
gint initial_offset, |
| 390 |
gint result_offset, |
| 391 |
gboolean ret) |
| 392 |
{ |
| 393 |
GtkTextBuffer *buffer; |
| 394 |
GtkTextIter iter; |
| 395 |
|
| 396 |
buffer = gtk_text_buffer_new (NULL); |
| 397 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 398 |
|
| 399 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 400 |
|
| 401 |
g_assert_cmpint (ret, ==, gtk_text_iter_forward_word_end (&iter)); |
| 402 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 403 |
|
| 404 |
g_object_unref (buffer); |
| 405 |
} |
| 406 |
|
| 407 |
static void |
| 408 |
check_backward_word_start (const gchar *buffer_text, |
| 409 |
gint initial_offset, |
| 410 |
gint result_offset, |
| 411 |
gboolean ret) |
| 412 |
{ |
| 413 |
GtkTextBuffer *buffer; |
| 414 |
GtkTextIter iter; |
| 415 |
|
| 416 |
buffer = gtk_text_buffer_new (NULL); |
| 417 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 418 |
|
| 419 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 420 |
|
| 421 |
g_assert_cmpint (ret, ==, gtk_text_iter_backward_word_start (&iter)); |
| 422 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 423 |
|
| 424 |
g_object_unref (buffer); |
| 425 |
} |
| 426 |
|
| 427 |
static void |
| 428 |
test_word_boundaries (void) |
| 429 |
{ |
| 430 |
/* Test with trivial content. The word boundaries are anyway determined by |
| 431 |
* Pango and can change in the future for corner cases. |
| 432 |
*/ |
| 433 |
|
| 434 |
check_word_boundaries ("ab ", 0, TRUE, FALSE, TRUE); |
| 435 |
check_word_boundaries ("ab ", 1, FALSE, FALSE, TRUE); |
| 436 |
check_word_boundaries ("ab ", 2, FALSE, TRUE, FALSE); |
| 437 |
check_word_boundaries ("ab ", 3, FALSE, FALSE, FALSE); |
| 438 |
check_word_boundaries ("", 0, FALSE, FALSE, FALSE); |
| 439 |
|
| 440 |
check_forward_word_end ("ab ", 0, 2, TRUE); |
| 441 |
check_forward_word_end ("ab ", 1, 2, TRUE); |
| 442 |
check_forward_word_end ("ab ", 2, 2, FALSE); |
| 443 |
check_forward_word_end ("ab ", 3, 3, FALSE); |
| 444 |
check_forward_word_end ("ab", 0, 2, FALSE); |
| 445 |
check_forward_word_end ("ab\n", 2, 2, FALSE); |
| 446 |
|
| 447 |
check_backward_word_start (" ab", 3, 1, TRUE); |
| 448 |
check_backward_word_start (" ab", 2, 1, TRUE); |
| 449 |
check_backward_word_start (" ab", 1, 1, FALSE); |
| 450 |
check_backward_word_start (" ab", 0, 0, FALSE); |
| 451 |
check_backward_word_start ("ab", 2, 0, TRUE); |
| 452 |
} |
| 453 |
|
| 454 |
static void |
| 455 |
check_forward_visible_word_end (GtkTextBuffer *buffer, |
| 456 |
gint initial_offset, |
| 457 |
gint result_offset, |
| 458 |
gboolean ret) |
| 459 |
{ |
| 460 |
GtkTextIter iter; |
| 461 |
|
| 462 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 463 |
|
| 464 |
g_assert_cmpint (ret, ==, gtk_text_iter_forward_visible_word_end (&iter)); |
| 465 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 466 |
} |
| 467 |
|
| 468 |
static void |
| 469 |
check_backward_visible_word_start (GtkTextBuffer *buffer, |
| 470 |
gint initial_offset, |
| 471 |
gint result_offset, |
| 472 |
gboolean ret) |
| 473 |
{ |
| 474 |
GtkTextIter iter; |
| 475 |
|
| 476 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 477 |
|
| 478 |
g_assert_cmpint (ret, ==, gtk_text_iter_backward_visible_word_start (&iter)); |
| 479 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 480 |
} |
| 481 |
|
| 482 |
static void |
| 483 |
test_visible_word_boundaries (void) |
| 484 |
{ |
| 485 |
/* Test with trivial content. The word boundaries are anyway determined by |
| 486 |
* Pango and can change in the future for corner cases. |
| 487 |
*/ |
| 488 |
|
| 489 |
GtkTextBuffer *buffer; |
| 490 |
GtkTextTag *invisible_tag; |
| 491 |
GtkTextIter iter; |
| 492 |
|
| 493 |
buffer = gtk_text_buffer_new (NULL); |
| 494 |
|
| 495 |
invisible_tag = gtk_text_buffer_create_tag (buffer, NULL, |
| 496 |
"invisible", TRUE, |
| 497 |
NULL); |
| 498 |
|
| 499 |
/* Buffer contents: " a b c " with " b " invisible */ |
| 500 |
gtk_text_buffer_get_start_iter (buffer, &iter); |
| 501 |
gtk_text_buffer_insert (buffer, &iter, " a", -1); |
| 502 |
gtk_text_buffer_insert_with_tags (buffer, &iter, " b ", -1, invisible_tag, NULL); |
| 503 |
gtk_text_buffer_insert (buffer, &iter, "c ", -1); |
| 504 |
|
| 505 |
check_forward_visible_word_end (buffer, 0, 6, TRUE); |
| 506 |
check_forward_visible_word_end (buffer, 1, 6, TRUE); |
| 507 |
check_forward_visible_word_end (buffer, 2, 6, TRUE); |
| 508 |
check_forward_visible_word_end (buffer, 3, 6, TRUE); |
| 509 |
check_forward_visible_word_end (buffer, 4, 6, TRUE); |
| 510 |
check_forward_visible_word_end (buffer, 5, 6, TRUE); |
| 511 |
check_forward_visible_word_end (buffer, 6, 6, FALSE); |
| 512 |
check_forward_visible_word_end (buffer, 7, 7, FALSE); |
| 513 |
|
| 514 |
check_backward_visible_word_start (buffer, 7, 5, TRUE); /* FIXME result_offset should be 1 */ |
| 515 |
check_backward_visible_word_start (buffer, 6, 5, TRUE); /* FIXME result_offset should be 1 */ |
| 516 |
check_backward_visible_word_start (buffer, 5, 1, TRUE); |
| 517 |
check_backward_visible_word_start (buffer, 4, 1, TRUE); |
| 518 |
check_backward_visible_word_start (buffer, 3, 1, TRUE); |
| 519 |
check_backward_visible_word_start (buffer, 2, 1, TRUE); |
| 520 |
check_backward_visible_word_start (buffer, 1, 1, FALSE); |
| 521 |
check_backward_visible_word_start (buffer, 0, 0, FALSE); |
| 522 |
|
| 523 |
gtk_text_buffer_set_text (buffer, "ab", -1); |
| 524 |
check_forward_visible_word_end (buffer, 0, 2, FALSE); |
| 525 |
|
| 526 |
/* Buffer contents: "b c " with "b" invisible */ |
| 527 |
gtk_text_buffer_set_text (buffer, "", -1); |
| 528 |
gtk_text_buffer_get_start_iter (buffer, &iter); |
| 529 |
gtk_text_buffer_insert_with_tags (buffer, &iter, "b", -1, invisible_tag, NULL); |
| 530 |
gtk_text_buffer_insert (buffer, &iter, " c ", -1); |
| 531 |
|
| 532 |
check_forward_visible_word_end (buffer, 0, 1, TRUE); /* FIXME result_offset should be 3 */ |
| 533 |
|
| 534 |
g_object_unref (buffer); |
| 535 |
} |
| 536 |
|
| 537 |
static void |
| 538 |
check_is_cursor_position (const gchar *buffer_text, |
| 539 |
gint offset, |
| 540 |
gboolean ret) |
| 541 |
{ |
| 542 |
GtkTextBuffer *buffer; |
| 543 |
GtkTextIter iter; |
| 544 |
|
| 545 |
buffer = gtk_text_buffer_new (NULL); |
| 546 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 547 |
|
| 548 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset); |
| 549 |
g_assert_cmpint (ret, ==, gtk_text_iter_is_cursor_position (&iter)); |
| 550 |
|
| 551 |
g_object_unref (buffer); |
| 552 |
} |
| 553 |
|
| 554 |
static void |
| 555 |
check_cursor_position (const gchar *buffer_text, |
| 556 |
gboolean forward, |
| 557 |
gint initial_offset, |
| 558 |
gint result_offset, |
| 559 |
gboolean ret) |
| 560 |
{ |
| 561 |
GtkTextBuffer *buffer; |
| 562 |
GtkTextIter iter; |
| 563 |
|
| 564 |
buffer = gtk_text_buffer_new (NULL); |
| 565 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 566 |
|
| 567 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 568 |
|
| 569 |
if (forward) |
| 570 |
g_assert_cmpint (ret, ==, gtk_text_iter_forward_cursor_position (&iter)); |
| 571 |
else |
| 572 |
g_assert_cmpint (ret, ==, gtk_text_iter_backward_cursor_position (&iter)); |
| 573 |
|
| 574 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 575 |
|
| 576 |
g_object_unref (buffer); |
| 577 |
} |
| 578 |
|
| 579 |
static void |
| 580 |
test_cursor_positions (void) |
| 581 |
{ |
| 582 |
check_is_cursor_position ("a\r\n", 0, TRUE); |
| 583 |
check_is_cursor_position ("a\r\n", 1, TRUE); |
| 584 |
check_is_cursor_position ("a\r\n", 2, FALSE); |
| 585 |
check_is_cursor_position ("a\r\n", 3, TRUE); |
| 586 |
check_is_cursor_position ("", 0, TRUE); |
| 587 |
|
| 588 |
/* forward */ |
| 589 |
check_cursor_position ("a\r\nb", TRUE, 0, 1, TRUE); |
| 590 |
check_cursor_position ("a\r\nb", TRUE, 1, 3, TRUE); |
| 591 |
check_cursor_position ("a\r\nb", TRUE, 2, 3, TRUE); |
| 592 |
check_cursor_position ("a\r\nb", TRUE, 3, 4, FALSE); |
| 593 |
check_cursor_position ("a\r\nb", TRUE, 4, 4, FALSE); |
| 594 |
check_cursor_position ("a\n", TRUE, 1, 2, FALSE); |
| 595 |
|
| 596 |
/* backward */ |
| 597 |
check_cursor_position ("a\r\nb", FALSE, 4, 3, TRUE); |
| 598 |
check_cursor_position ("a\r\nb", FALSE, 3, 1, TRUE); |
| 599 |
check_cursor_position ("a\r\nb", FALSE, 2, 1, TRUE); |
| 600 |
check_cursor_position ("a\r\nb", FALSE, 1, 0, TRUE); |
| 601 |
check_cursor_position ("a\r\nb", FALSE, 0, 0, FALSE); |
| 602 |
} |
| 603 |
|
| 604 |
static void |
| 605 |
check_visible_cursor_position (GtkTextBuffer *buffer, |
| 606 |
gboolean forward, |
| 607 |
gint initial_offset, |
| 608 |
gint result_offset, |
| 609 |
gboolean ret) |
| 610 |
{ |
| 611 |
GtkTextIter iter; |
| 612 |
|
| 613 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 614 |
|
| 615 |
if (forward) |
| 616 |
g_assert_cmpint (ret, ==, gtk_text_iter_forward_visible_cursor_position (&iter)); |
| 617 |
else |
| 618 |
g_assert_cmpint (ret, ==, gtk_text_iter_backward_visible_cursor_position (&iter)); |
| 619 |
|
| 620 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 621 |
} |
| 622 |
|
| 623 |
static void |
| 624 |
test_visible_cursor_positions (void) |
| 625 |
{ |
| 626 |
GtkTextBuffer *buffer; |
| 627 |
GtkTextTag *invisible_tag; |
| 628 |
GtkTextIter iter; |
| 629 |
|
| 630 |
buffer = gtk_text_buffer_new (NULL); |
| 631 |
|
| 632 |
invisible_tag = gtk_text_buffer_create_tag (buffer, NULL, |
| 633 |
"invisible", TRUE, |
| 634 |
NULL); |
| 635 |
|
| 636 |
/* Buffer contents: "abcd" with 'bc' invisible */ |
| 637 |
gtk_text_buffer_get_start_iter (buffer, &iter); |
| 638 |
gtk_text_buffer_insert (buffer, &iter, "a", -1); |
| 639 |
gtk_text_buffer_insert_with_tags (buffer, &iter, "bc", -1, invisible_tag, NULL); |
| 640 |
gtk_text_buffer_insert (buffer, &iter, "d", -1); |
| 641 |
|
| 642 |
/* forward */ |
| 643 |
check_visible_cursor_position (buffer, TRUE, 0, 3, TRUE); |
| 644 |
check_visible_cursor_position (buffer, TRUE, 1, 3, TRUE); |
| 645 |
check_visible_cursor_position (buffer, TRUE, 2, 3, TRUE); |
| 646 |
check_visible_cursor_position (buffer, TRUE, 3, 4, FALSE); |
| 647 |
check_visible_cursor_position (buffer, TRUE, 4, 4, FALSE); |
| 648 |
|
| 649 |
/* backward */ |
| 650 |
check_visible_cursor_position (buffer, FALSE, 4, 3, TRUE); |
| 651 |
check_visible_cursor_position (buffer, FALSE, 3, 0, TRUE); |
| 652 |
check_visible_cursor_position (buffer, FALSE, 2, 0, TRUE); |
| 653 |
check_visible_cursor_position (buffer, FALSE, 1, 0, TRUE); |
| 654 |
check_visible_cursor_position (buffer, FALSE, 0, 0, FALSE); |
| 655 |
|
| 656 |
g_object_unref (buffer); |
| 657 |
} |
| 658 |
|
| 659 |
static void |
| 660 |
check_sentence_boundaries (const gchar *buffer_text, |
| 661 |
gint offset, |
| 662 |
gboolean starts_sentence, |
| 663 |
gboolean ends_sentence, |
| 664 |
gboolean inside_sentence) |
| 665 |
{ |
| 666 |
GtkTextBuffer *buffer; |
| 667 |
GtkTextIter iter; |
| 668 |
|
| 669 |
buffer = gtk_text_buffer_new (NULL); |
| 670 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 671 |
|
| 672 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset); |
| 673 |
|
| 674 |
g_assert_cmpint (starts_sentence, ==, gtk_text_iter_starts_sentence (&iter)); |
| 675 |
g_assert_cmpint (ends_sentence, ==, gtk_text_iter_ends_sentence (&iter)); |
| 676 |
g_assert_cmpint (inside_sentence, ==, gtk_text_iter_inside_sentence (&iter)); |
| 677 |
|
| 678 |
g_object_unref (buffer); |
| 679 |
} |
| 680 |
|
| 681 |
static void |
| 682 |
check_forward_sentence_end (const gchar *buffer_text, |
| 683 |
gint initial_offset, |
| 684 |
gint result_offset, |
| 685 |
gboolean ret) |
| 686 |
{ |
| 687 |
GtkTextBuffer *buffer; |
| 688 |
GtkTextIter iter; |
| 689 |
|
| 690 |
buffer = gtk_text_buffer_new (NULL); |
| 691 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 692 |
|
| 693 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 694 |
|
| 695 |
g_assert_cmpint (ret, ==, gtk_text_iter_forward_sentence_end (&iter)); |
| 696 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 697 |
|
| 698 |
g_object_unref (buffer); |
| 699 |
} |
| 700 |
|
| 701 |
static void |
| 702 |
check_backward_sentence_start (const gchar *buffer_text, |
| 703 |
gint initial_offset, |
| 704 |
gint result_offset, |
| 705 |
gboolean ret) |
| 706 |
{ |
| 707 |
GtkTextBuffer *buffer; |
| 708 |
GtkTextIter iter; |
| 709 |
|
| 710 |
buffer = gtk_text_buffer_new (NULL); |
| 711 |
gtk_text_buffer_set_text (buffer, buffer_text, -1); |
| 712 |
|
| 713 |
gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset); |
| 714 |
|
| 715 |
g_assert_cmpint (ret, ==, gtk_text_iter_backward_sentence_start (&iter)); |
| 716 |
g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter)); |
| 717 |
|
| 718 |
g_object_unref (buffer); |
| 719 |
} |
| 720 |
|
| 721 |
static void |
| 722 |
test_sentence_boundaries (void) |
| 723 |
{ |
| 724 |
check_sentence_boundaries ("Hi. ", 0, TRUE, FALSE, TRUE); |
| 725 |
check_sentence_boundaries ("Hi. ", 1, FALSE, FALSE, TRUE); |
| 726 |
check_sentence_boundaries ("Hi. ", 2, FALSE, FALSE, TRUE); |
| 727 |
check_sentence_boundaries ("Hi. ", 3, FALSE, TRUE, FALSE); |
| 728 |
check_sentence_boundaries ("Hi. ", 4, FALSE, FALSE, FALSE); |
| 729 |
check_sentence_boundaries ("", 0, FALSE, FALSE, FALSE); |
| 730 |
|
| 731 |
check_forward_sentence_end ("Hi. ", 0, 3, TRUE); |
| 732 |
check_forward_sentence_end ("Hi. ", 1, 3, TRUE); |
| 733 |
check_forward_sentence_end ("Hi. ", 2, 3, TRUE); |
| 734 |
check_forward_sentence_end ("Hi. ", 3, 3, FALSE); |
| 735 |
check_forward_sentence_end ("Hi. ", 4, 4, FALSE); |
| 736 |
check_forward_sentence_end ("Hi.", 0, 3, FALSE); |
| 737 |
check_forward_sentence_end ("Hi.\n", 3, 3, FALSE); |
| 738 |
|
| 739 |
check_backward_sentence_start (" Hi.", 4, 1, TRUE); |
| 740 |
check_backward_sentence_start (" Hi.", 3, 1, TRUE); |
| 741 |
check_backward_sentence_start (" Hi.", 2, 1, TRUE); |
| 742 |
check_backward_sentence_start (" Hi.", 1, 1, FALSE); |
| 743 |
check_backward_sentence_start (" Hi.", 0, 0, FALSE); |
| 744 |
} |
| 745 |
|
| 746 |
static void |
| 747 |
test_backward_line (void) |
| 748 |
{ |
| 749 |
GtkTextBuffer *buffer; |
| 750 |
GtkTextIter iter, start, end; |
| 751 |
gboolean ret; |
| 752 |
gint offset; |
| 753 |
|
| 754 |
buffer = gtk_text_buffer_new (NULL); |
| 755 |
gtk_text_buffer_get_start_iter (buffer, &iter); |
| 756 |
gtk_text_buffer_insert (buffer, &iter, "Hi line 1\nHi line 2", -1); |
| 757 |
|
| 758 |
/* Go to middle of first line */ |
| 759 |
gtk_text_iter_backward_line (&iter); |
| 760 |
gtk_text_iter_set_line_offset (&iter, 4); |
| 761 |
|
| 762 |
/* Now insert some chars with gtk_text_buffer_insert_range() */ |
| 763 |
gtk_text_buffer_get_end_iter (buffer, &end); |
| 764 |
start = end; |
| 765 |
gtk_text_iter_backward_cursor_positions (&start, 5); |
| 766 |
gtk_text_buffer_insert_range (buffer, &iter, &start, &end); |
| 767 |
|
| 768 |
/* Check we are still at the first line */ |
| 769 |
g_assert_cmpint (gtk_text_iter_get_line (&iter), ==, 0); |
| 770 |
|
| 771 |
/* Now a call to gtk_text_iter_backward_line() should return TRUE |
| 772 |
and move &iter to start of the line, or return FALSE if &iter |
| 773 |
was already at start of the line, so in both cases &iter should |
| 774 |
be at the start of the line, so check that */ |
| 775 |
ret = gtk_text_iter_backward_line (&iter); |
| 776 |
g_assert_true (ret); |
| 777 |
offset = gtk_text_iter_get_line_offset (&iter); |
| 778 |
g_assert_cmpint (offset, ==, 0); |
| 779 |
|
| 780 |
g_object_unref (buffer); |
| 781 |
} |
| 782 |
|
| 783 |
int |
| 784 |
main (int argc, char** argv) |
| 785 |
{ |
| 786 |
gtk_test_init (&argc, &argv); |
| 787 |
|
| 788 |
g_test_add_func ("/TextIter/Search Empty", test_empty_search); |
| 789 |
g_test_add_func ("/TextIter/Search Full Buffer", test_search_full_buffer); |
| 790 |
g_test_add_func ("/TextIter/Search", test_search); |
| 791 |
g_test_add_func ("/TextIter/Search Caseless", test_search_caseless); |
| 792 |
g_test_add_func ("/TextIter/Forward To Tag Toggle", test_forward_to_tag_toggle); |
| 793 |
g_test_add_func ("/TextIter/Forward To Line End", test_forward_to_line_end); |
| 794 |
g_test_add_func ("/TextIter/Word Boundaries", test_word_boundaries); |
| 795 |
g_test_add_func ("/TextIter/Visible Word Boundaries", test_visible_word_boundaries); |
| 796 |
g_test_add_func ("/TextIter/Cursor Positions", test_cursor_positions); |
| 797 |
g_test_add_func ("/TextIter/Visible Cursor Positions", test_visible_cursor_positions); |
| 798 |
g_test_add_func ("/TextIter/Sentence Boundaries", test_sentence_boundaries); |
| 799 |
g_test_add_func ("/TextIter/Backward line", test_backward_line); |
| 800 |
|
| 801 |
return g_test_run(); |
| 802 |
} |