File: test-view.c

package info (click to toggle)
tepl 6.4.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,692 kB
  • sloc: ansic: 16,879; xml: 542; sh: 20; makefile: 9
file content (151 lines) | stat: -rw-r--r-- 4,771 bytes parent folder | download | duplicates (3)
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
/* SPDX-FileCopyrightText: 2022 - Sébastien Wilmet <swilmet@gnome.org>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <tepl/tepl.h>

static void
delete_current_line (TeplView *view)
{
	g_signal_emit_by_name (view, "delete-from-cursor", GTK_DELETE_PARAGRAPHS, 1);
}

static void
select_range (GtkTextBuffer *buffer,
	      gint           selection_start_offset,
	      gint           selection_end_offset)
{
	GtkTextIter selection_start_iter;
	GtkTextIter selection_end_iter;

	gtk_text_buffer_get_iter_at_offset (buffer, &selection_start_iter, selection_start_offset);
	gtk_text_buffer_get_iter_at_offset (buffer, &selection_end_iter, selection_end_offset);

	gtk_text_buffer_select_range (buffer, &selection_start_iter, &selection_end_iter);
}

static void
check_buffer_content (GtkTextBuffer *buffer,
		      const gchar   *expected_content)
{
	GtkTextIter start;
	GtkTextIter end;
	gchar *content;

	gtk_text_buffer_get_bounds (buffer, &start, &end);
	content = gtk_text_buffer_get_slice (buffer, &start, &end, TRUE);
	g_assert_cmpstr (content, ==, expected_content);

	g_free (content);
}

static void
check_delete_current_line (TeplView    *view,
			   const gchar *content_before,
			   gint         selection_start_offset_before,
			   gint         selection_end_offset_before,
			   const gchar *content_after,
			   gint         cursor_offset_after)
{
	GtkTextBuffer *buffer;
	GtkTextIter cursor_pos;

	/* Prepa */

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
	gtk_text_buffer_set_text (buffer, content_before, -1);
	select_range (buffer, selection_start_offset_before, selection_end_offset_before);

	/* Operation */

	delete_current_line (view);

	/* Checks */

	check_buffer_content (buffer, content_after);
	g_assert_false (gtk_text_buffer_get_has_selection (buffer));

	gtk_text_buffer_get_iter_at_mark (buffer, &cursor_pos, gtk_text_buffer_get_insert (buffer));
	g_assert_cmpint (cursor_offset_after, ==, gtk_text_iter_get_offset (&cursor_pos));
}

static void
test_delete_current_line (void)
{
	TeplView *view;

	view = TEPL_VIEW (tepl_view_new ());
	g_object_ref_sink (view);

	/* Empty buffer */
	check_delete_current_line (view, "", 0, 0, "", 0);

	/* Single line without trailing \n */
	// Without selection
	check_delete_current_line (view, "ab", 0, 0, "", 0);
	check_delete_current_line (view, "ab", 1, 1, "", 0);
	check_delete_current_line (view, "ab", 2, 2, "", 0);
	// With selection
	check_delete_current_line (view, "ab", 0, 1, "", 0);
	check_delete_current_line (view, "ab", 0, 2, "", 0);
	check_delete_current_line (view, "ab", 1, 2, "", 0);

	/* Single line with trailing \n (so two lines with the second line
	 * empty).
	 */
	// Without selection
	check_delete_current_line (view, "ab\n", 0, 0, "", 0);
	check_delete_current_line (view, "ab\n", 1, 1, "", 0);
	check_delete_current_line (view, "ab\n", 2, 2, "", 0);
	check_delete_current_line (view, "ab\n", 3, 3, "ab", 0);
	// With selection
	check_delete_current_line (view, "ab\n", 0, 1, "", 0);
	check_delete_current_line (view, "ab\n", 0, 2, "", 0);
	check_delete_current_line (view, "ab\n", 1, 2, "", 0);
	check_delete_current_line (view, "ab\n", 0, 3, "", 0);
	check_delete_current_line (view, "ab\n", 1, 3, "", 0);
	check_delete_current_line (view, "ab\n", 2, 3, "", 0);

	/* Two lines, delete second line */
	// Without selection
	check_delete_current_line (view, "ab\nc", 3, 3, "ab", 0);
	check_delete_current_line (view, "ab\nc", 4, 4, "ab", 0);
	// With selection
	check_delete_current_line (view, "ab\nc", 3, 4, "ab", 0);

	/* Two lines with selection */
	check_delete_current_line (view, "a\nb", 0, 1, "b", 0);
	check_delete_current_line (view, "a\nb", 0, 2, "b", 0);
	check_delete_current_line (view, "a\nb", 0, 3, "", 0);

	// This one is a bit more difficult. We select only the \n, but in the
	// TextView the selection is visible only on the first line, after the
	// 'a'. So 'b' remains.
	check_delete_current_line (view, "a\nb", 1, 2, "b", 0);

	check_delete_current_line (view, "a\nb", 1, 3, "", 0);
	check_delete_current_line (view, "a\nb", 2, 3, "a", 0);

	/* Three lines */
	// Without selection, delete second line
	check_delete_current_line (view, "ab\nc\nde", 3, 3, "ab\nde", 3);
	check_delete_current_line (view, "ab\nc\nde", 4, 4, "ab\nde", 3);
	// With selection
	check_delete_current_line (view, "ab\nc\nde", 3, 4, "ab\nde", 3);
	check_delete_current_line (view, "ab\nc\nde", 3, 5, "ab\nde", 3);
	check_delete_current_line (view, "ab\nc\nde", 2, 3, "c\nde", 0);
	check_delete_current_line (view, "ab\nc\nde", 2, 4, "de", 0);

	g_object_unref (view);
}

int
main (int    argc,
      char **argv)
{
	gtk_test_init (&argc, &argv);

	g_test_add_func ("/view/delete_current_line", test_delete_current_line);

	return g_test_run ();
}