-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsplit.c
258 lines (210 loc) · 7.34 KB
/
split.c
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
/* split.c
*
* Copyright 2022 Matthew Jakeman <[email protected]>
*
* This file is dual-licensed under the terms of the Mozilla Public
* License 2.0 and the Lesser General Public License 2.1 (or any
* later version).
*
* SPDX-License-Identifier: MPL-2.0 OR LGPL-2.1-or-later
*/
#include <glib.h>
#include <locale.h>
#include <model/document.h>
#include <model/paragraph.h>
#include <model/run.h>
#include <editor/editor.h>
typedef struct {
TextDocument *doc;
TextEditor *editor;
TextRun *run1;
TextRun *run2;
TextRun *run3;
TextRun *run4;
TextRun *run5;
TextParagraph *para1;
TextParagraph *para2;
TextParagraph *para3;
} SplitFixture;
#define RUN1 "abcdefghij"
#define RUN2 "1234567890"
#define RUN3 "!@#$%^&*()"
#define RUN4 "zxcvbnm,./"
#define RUN5 "0987654321"
static void
split_fixture_set_up (SplitFixture *fixture,
gconstpointer user_data)
{
TextFrame *frame;
TextParagraph *para1, *para2, *para3;
TextRun *run1, *run2, *run3, *run4, *run5;
frame = text_frame_new ();
para1 = text_paragraph_new ();
run1 = text_run_new (RUN1);
run2 = text_run_new (RUN2);
run3 = text_run_new (RUN3);
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run1));
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run2));
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run3));
text_frame_append_block (frame, TEXT_BLOCK (para1));
para2 = text_paragraph_new ();
run4 = text_run_new (RUN4);
text_paragraph_append_fragment(para2, TEXT_FRAGMENT (run4));
text_frame_append_block (frame, TEXT_BLOCK (para2));
para3 = text_paragraph_new ();
run5 = text_run_new (RUN5);
text_paragraph_append_fragment(para3, TEXT_FRAGMENT (run5));
text_frame_append_block (frame, TEXT_BLOCK (para3));
fixture->doc = text_document_new ();
fixture->doc->frame = frame;
fixture->editor = text_editor_new (fixture->doc);
text_editor_move_first (fixture->editor, TEXT_EDITOR_CURSOR);
fixture->run1 = run1;
fixture->run2 = run2;
fixture->run3 = run3;
fixture->run4 = run4;
fixture->run5 = run5;
fixture->para1 = para1;
fixture->para2 = para2;
fixture->para3 = para3;
}
static void
split_fixture_tear_down (SplitFixture *fixture,
gconstpointer user_data)
{
g_object_unref (fixture->editor);
g_object_unref (fixture->doc);
}
static void
test_end_of_paragraph (SplitFixture *fixture,
gconstpointer user_data)
{
gchar *text;
int length;
TextParagraph *new;
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 30);
text_editor_split (fixture->editor, TEXT_EDITOR_CURSOR);
// before:
// abcdefghij1234567890!@#$%^&*()
// zxcvbnm,./
// 0987654321
// after:
// abcdefghij1234567890!@#$%^&*()
// <empty run> <-- new paragraph
// zxcvbnm,./
// 0987654321
new = TEXT_PARAGRAPH (text_node_get_next (TEXT_NODE (fixture->para1)));
// check paragraph contents
text = text_paragraph_get_text (fixture->para1);
g_assert_cmpstr (text, ==, "abcdefghij1234567890!@#$%^&*()");
g_free (text);
text = text_paragraph_get_text (new);
g_assert_cmpstr (text, ==, "");
g_free (text);
text = text_paragraph_get_text (fixture->para2);
g_assert_cmpstr (text, ==, "zxcvbnm,./");
g_free (text);
// check new length
length = text_paragraph_get_length (new);
g_assert_cmpint (length, ==, 0);
// assert cursor is on new paragraph
g_assert_cmpint (fixture->doc->cursor->index, ==, 0);
g_assert_true (fixture->doc->cursor->paragraph == new);
}
static void
test_start_of_paragraph (SplitFixture *fixture,
gconstpointer user_data)
{
gchar *text;
int length;
TextParagraph *new;
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 31);
text_editor_split (fixture->editor, TEXT_EDITOR_CURSOR);
// before:
// abcdefghij1234567890!@#$%^&*()
// zxcvbnm,./
// 0987654321
// after:
// abcdefghij1234567890!@#$%^&*()
// <empty run> <-- modified paragraph two
// zxcvbnm,./ <-- new paragraph
// 0987654321
// check paragraph contents
text = text_paragraph_get_text (fixture->para1);
g_assert_cmpstr (text, ==, "abcdefghij1234567890!@#$%^&*()");
g_free (text);
// check that paragraph two is now empty
text = text_paragraph_get_text (fixture->para2);
g_assert_cmpstr (text, ==, "");
g_free (text);
// check paragraph two length
length = text_paragraph_get_length (fixture->para2);
g_assert_cmpint (length, ==, 0);
// get newly-inserted paragraph
new = TEXT_PARAGRAPH (text_node_get_next (TEXT_NODE (fixture->para2)));
// check contents
text = text_paragraph_get_text (new);
g_assert_cmpstr (text, ==, "zxcvbnm,./");
g_free (text);
// check length
length = text_paragraph_get_length (new);
g_assert_cmpint (length, ==, 10);
// assert cursor is at start of new paragraph
g_assert_cmpint (fixture->doc->cursor->index, ==, 0);
g_assert_true (fixture->doc->cursor->paragraph == new);
}
static void
test_middle_of_paragraph (SplitFixture *fixture,
gconstpointer user_data)
{
gchar *text;
int length;
TextParagraph *new;
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 15);
text_editor_split (fixture->editor, TEXT_EDITOR_CURSOR);
// before:
// abcdefghij1234567890!@#$%^&*()
// zxcvbnm,./
// 0987654321
// after:
// abcdefghij12345 <-- modified paragraph one
// 67890!@#$%^&*() <-- new paragraph
// zxcvbnm,./
// 0987654321
new = TEXT_PARAGRAPH (text_node_get_next (TEXT_NODE (fixture->para1)));
// check paragraph contents
text = text_paragraph_get_text (fixture->para1);
g_assert_cmpstr (text, ==, "abcdefghij12345");
g_free (text);
text = text_paragraph_get_text (new);
g_assert_cmpstr (text, ==, "67890!@#$%^&*()");
g_free (text);
text = text_paragraph_get_text (fixture->para2);
g_assert_cmpstr (text, ==, "zxcvbnm,./");
g_free (text);
// check lengths
length = text_paragraph_get_length (fixture->para1);
g_assert_cmpint (length, ==, 15);
length = text_paragraph_get_length (new);
g_assert_cmpint (length, ==, 15);
// assert cursor is at start of new paragraph
g_assert_cmpint (fixture->doc->cursor->index, ==, 0);
g_assert_true (fixture->doc->cursor->paragraph == new);
}
int
main (int argc, char *argv[])
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
// Define the tests.
g_test_add ("/text-engine/editor/split/test-end-of-paragraph", SplitFixture, NULL,
split_fixture_set_up, test_end_of_paragraph,
split_fixture_tear_down);
g_test_add ("/text-engine/editor/split/test-start-of-paragraph", SplitFixture, NULL,
split_fixture_set_up, test_start_of_paragraph,
split_fixture_tear_down);
g_test_add ("/text-engine/editor/split/test-middle-of-paragraph", SplitFixture, NULL,
split_fixture_set_up, test_middle_of_paragraph,
split_fixture_tear_down);
return g_test_run ();
}