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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
/*
* This file is part of GtkSourceView
*
* Copyright (C) 2003 - Gustavo Giráldez
* Copyright (C) 2006, 2013 - Paolo Borelli
* Copyright (C) 2013, 2016 - Sébastien Wilmet
*
* GtkSourceView 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.
*
* GtkSourceView 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/>.
*/
#include <gtksourceview/gtksource.h>
static void
test_weak_ref (void)
{
GtkTextBuffer *buffer;
GtkSourceRegion *region;
GtkSourceRegionIter region_iter;
GtkTextIter start;
GtkTextIter end;
buffer = gtk_text_buffer_new (NULL);
region = gtk_source_region_new (buffer);
gtk_text_buffer_set_text (buffer, "test_weak_ref", -1);
gtk_text_buffer_get_bounds (buffer, &start, &end);
gtk_source_region_add_subregion (region, &start, &end);
g_assert (!gtk_source_region_is_empty (region));
g_object_unref (buffer);
g_assert (gtk_source_region_is_empty (region));
g_assert (!gtk_source_region_get_bounds (region, &start, &end));
gtk_source_region_get_start_region_iter (region, ®ion_iter);
g_assert (!gtk_source_region_iter_get_subregion (®ion_iter, &start, &end));
g_object_unref (region);
}
static void
add_subregion (GtkSourceRegion *region,
gint start_offset,
gint end_offset)
{
GtkTextBuffer *buffer;
GtkTextIter start_iter;
GtkTextIter end_iter;
buffer = gtk_source_region_get_buffer (region);
gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_offset);
gtk_source_region_add_subregion (region, &start_iter, &end_iter);
}
static void
subtract_subregion (GtkSourceRegion *region,
gint start_offset,
gint end_offset)
{
GtkTextBuffer *buffer;
GtkTextIter start_iter;
GtkTextIter end_iter;
buffer = gtk_source_region_get_buffer (region);
gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_offset);
gtk_source_region_subtract_subregion (region, &start_iter, &end_iter);
}
static void
check_result (GtkSourceRegion *region,
const gchar *expected_result)
{
gchar *expected_region_str;
gchar *region_str;
if (expected_result == NULL)
{
g_assert (gtk_source_region_is_empty (region));
return;
}
g_assert (!gtk_source_region_is_empty (region));
expected_region_str = g_strconcat ("Subregions: ", expected_result, NULL);
region_str = gtk_source_region_to_string (region);
g_assert_cmpstr (region_str, ==, expected_region_str);
g_free (expected_region_str);
g_free (region_str);
}
static void
test_add_subtract_subregion (void)
{
GtkTextBuffer *buffer;
GtkSourceRegion *region;
buffer = gtk_text_buffer_new (NULL);
region = gtk_source_region_new (buffer);
gtk_text_buffer_set_text (buffer, "This is a test of GtkSourceRegion", -1);
g_assert (gtk_source_region_is_empty (region));
/* Add/remove 0-length subregions */
add_subregion (region, 5, 5);
g_assert (gtk_source_region_is_empty (region));
subtract_subregion (region, 5, 5);
g_assert (gtk_source_region_is_empty (region));
/* Add subregion */
add_subregion (region, 5, 10);
check_result (region, "5-10");
/* Add two adjacent subregions */
add_subregion (region, 3, 5);
check_result (region, "3-10");
add_subregion (region, 10, 12);
check_result (region, "3-12");
/* Remove all */
subtract_subregion (region, 1, 15);
g_assert (gtk_source_region_is_empty (region));
/* Add two separate subregions */
add_subregion (region, 5, 10);
add_subregion (region, 15, 20);
check_result (region, "5-10 15-20");
/* Join them */
add_subregion (region, 7, 17);
check_result (region, "5-20");
/* Remove from the middle */
subtract_subregion (region, 10, 15);
check_result (region, "5-10 15-20");
/* Exactly remove a subregion */
subtract_subregion (region, 15, 20);
check_result (region, "5-10");
/* Try to remove an adjacent subregion */
subtract_subregion (region, 10, 20);
check_result (region, "5-10");
subtract_subregion (region, 0, 5);
check_result (region, "5-10");
/* Add another separate subregion */
add_subregion (region, 15, 20);
check_result (region, "5-10 15-20");
/* Join with excess */
add_subregion (region, 0, 25);
check_result (region, "0-25");
/* Do two holes */
subtract_subregion (region, 5, 10);
check_result (region, "0-5 10-25");
subtract_subregion (region, 15, 20);
check_result (region, "0-5 10-15 20-25");
/* Remove the middle subregion */
subtract_subregion (region, 8, 22);
check_result (region, "0-5 22-25");
/* Add the subregion we just removed */
add_subregion (region, 10, 15);
check_result (region, "0-5 10-15 22-25");
/* Remove the middle subregion */
subtract_subregion (region, 3, 17);
check_result (region, "0-3 22-25");
/* Add the subregion we just removed */
add_subregion (region, 10, 15);
check_result (region, "0-3 10-15 22-25");
/* Remove the middle subregion */
subtract_subregion (region, 2, 23);
check_result (region, "0-2 23-25");
/* Add the subregion we just removed */
add_subregion (region, 10, 15);
check_result (region, "0-2 10-15 23-25");
g_object_unref (buffer);
g_object_unref (region);
}
static void
do_intersection_subregion (GtkSourceRegion *region,
gint start_offset,
gint end_offset,
const gchar *expected_result)
{
GtkTextBuffer *buffer;
GtkTextIter start_iter;
GtkTextIter end_iter;
GtkSourceRegion *intersection;
buffer = gtk_source_region_get_buffer (region);
gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_offset);
intersection = gtk_source_region_intersect_subregion (region, &start_iter, &end_iter);
check_result (intersection, expected_result);
g_clear_object (&intersection);
}
static void
test_intersect_subregion (void)
{
GtkTextBuffer *buffer;
GtkSourceRegion *region;
buffer = gtk_text_buffer_new (NULL);
region = gtk_source_region_new (buffer);
gtk_text_buffer_set_text (buffer, "This is a test of GtkSourceRegion", -1);
g_assert (gtk_source_region_is_empty (region));
add_subregion (region, 0, 2);
add_subregion (region, 10, 15);
add_subregion (region, 23, 25);
check_result (region, "0-2 10-15 23-25");
do_intersection_subregion (region, 0, 25, "0-2 10-15 23-25");
do_intersection_subregion (region, 10, 15, "10-15");
do_intersection_subregion (region, 8, 17, "10-15");
do_intersection_subregion (region, 1, 24, "1-2 10-15 23-24");
do_intersection_subregion (region, 3, 7, NULL);
g_object_unref (buffer);
g_object_unref (region);
}
static void
test_add_subtract_intersect_region (void)
{
GtkTextBuffer *buffer;
GtkSourceRegion *main_region = NULL;
GtkSourceRegion *region_to_add = NULL;
GtkSourceRegion *region_to_subtract = NULL;
GtkSourceRegion *intersection = NULL;
buffer = gtk_text_buffer_new (NULL);
main_region = gtk_source_region_new (buffer);
gtk_text_buffer_set_text (buffer, "This is a test of GtkSourceRegion", -1);
g_assert (gtk_source_region_is_empty (main_region));
/* Basic tests */
region_to_add = gtk_source_region_new (buffer);
add_subregion (region_to_add, 0, 5);
add_subregion (region_to_add, 10, 15);
check_result (region_to_add, "0-5 10-15");
gtk_source_region_add_region (main_region, region_to_add);
check_result (main_region, "0-5 10-15");
region_to_subtract = gtk_source_region_new (buffer);
add_subregion (region_to_subtract, 2, 3);
add_subregion (region_to_subtract, 10, 15);
gtk_source_region_subtract_region (main_region, region_to_subtract);
check_result (main_region, "0-2 3-5");
add_subregion (main_region, 20, 25);
check_result (main_region, "0-2 3-5 20-25");
check_result (region_to_add, "0-5 10-15");
intersection = gtk_source_region_intersect_region (main_region, region_to_add);
check_result (intersection, "0-2 3-5");
g_object_unref (buffer);
g_clear_object (&main_region);
g_clear_object (®ion_to_add);
g_clear_object (®ion_to_subtract);
g_clear_object (&intersection);
}
int
main (int argc,
char **argv)
{
gint ret;
gtk_test_init (&argc, &argv);
gtk_source_init ();
g_test_add_func ("/Region/weak-ref", test_weak_ref);
g_test_add_func ("/Region/add-subtract-subregion", test_add_subtract_subregion);
g_test_add_func ("/Region/intersect-subregion", test_intersect_subregion);
g_test_add_func ("/Region/add-subtract-intersect-region", test_add_subtract_intersect_region);
ret = g_test_run ();
gtk_source_finalize ();
return ret;
}
|