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
|
/** @file testSelection.cxx
** Unit Tests for Scintilla internal data structures
**/
#include <cstdint>
#include <stdexcept>
#include <string_view>
#include <vector>
#include "Debugging.h"
#include "Position.h"
#include "Selection.h"
#include "catch.hpp"
using namespace Scintilla;
using namespace Scintilla::Internal;
// Test Selection.
namespace {
constexpr SelectionPosition invalid;
constexpr SelectionPosition zero(0);
constexpr SelectionRange rangeInvalid;
constexpr SelectionRange rangeZero(0);
}
TEST_CASE("SelectionPosition") {
SECTION("SelectionPosition") {
SelectionPosition sel;
REQUIRE(sel.Position() == Sci::invalidPosition);
REQUIRE(sel.VirtualSpace() == 0);
REQUIRE(!sel.IsValid());
REQUIRE(sel.VirtualSpace() == 0);
REQUIRE(sel == invalid);
REQUIRE(sel != zero);
sel.Reset();
REQUIRE(sel != invalid);
REQUIRE(sel == zero);
}
SECTION("Comparison") {
constexpr SelectionPosition sel(2,3);
REQUIRE(sel > invalid);
REQUIRE(sel > zero);
REQUIRE(sel >= zero);
REQUIRE(zero < sel);
REQUIRE(zero <= sel);
SelectionPosition virtuous(0, 4);
REQUIRE(virtuous > zero);
REQUIRE(virtuous >= zero);
REQUIRE(zero < virtuous);
REQUIRE(zero <= virtuous);
REQUIRE(virtuous.Position() == 0);
REQUIRE(virtuous.VirtualSpace() == 4);
virtuous.SetPosition(1); // Also resets virtualSpace
REQUIRE(virtuous.Position() == 1);
REQUIRE(virtuous.VirtualSpace() == 0);
virtuous.SetVirtualSpace(3); // Does not reset position
REQUIRE(virtuous.Position() == 1);
REQUIRE(virtuous.VirtualSpace() == 3);
}
SECTION("Add") {
SelectionPosition sel(2,3);
sel.Add(1);
REQUIRE(sel.Position() == 3);
REQUIRE(sel.VirtualSpace() == 3);
sel.AddVirtualSpace(2);
REQUIRE(sel.Position() == 3);
REQUIRE(sel.VirtualSpace() == 5);
}
SECTION("MoveForInsertDelete") {
// There are multiple details implemented in MoveForInsertDelete that are supposed to
// move selections in a way that appears to be natural to a user.
SelectionPosition sel(2,3);
sel.MoveForInsertDelete(true, 0,1, false);
REQUIRE(sel == SelectionPosition(3,3));
// Converts a virtual space to real space
sel.MoveForInsertDelete(true, 3,1, false);
REQUIRE(sel == SelectionPosition(4,2));
// Deletion at position clears virtual space
sel.MoveForInsertDelete(false, 4,1, false);
REQUIRE(sel == SelectionPosition(4,0));
sel.MoveForInsertDelete(false, 3,1, false);
REQUIRE(sel == SelectionPosition(3,0));
// Insert at position with and without move for equal
sel.MoveForInsertDelete(true, 3, 1, false);
REQUIRE(sel == SelectionPosition(3, 0));
sel.MoveForInsertDelete(true, 3, 1, true);
REQUIRE(sel == SelectionPosition(4, 0));
// Deletion over the position moves to start of deletion
sel.MoveForInsertDelete(false, 2, 5, false);
REQUIRE(sel == SelectionPosition(2, 0));
}
SECTION("Serialization") {
// Conversion to/from string form
const std::string invalidString(invalid.ToString());
REQUIRE(invalidString == "-1");
const SelectionPosition invalidReturned(invalidString);
REQUIRE(invalidReturned == invalid);
const std::string zeroString(zero.ToString());
REQUIRE(zeroString == "0");
const SelectionPosition zeroReturned(zeroString);
REQUIRE(zeroReturned == zero);
const SelectionPosition virtue(2, 3);
const std::string virtueString(virtue.ToString());
REQUIRE(virtueString == "2v3");
const SelectionPosition virtueReturned(virtueString);
REQUIRE(virtueReturned == virtue);
}
}
TEST_CASE("SelectionSegment") {
SECTION("SelectionSegment") {
const SelectionSegment ss;
REQUIRE(ss.start == invalid);
REQUIRE(ss.end == invalid);
}
}
TEST_CASE("SelectionRange") {
SECTION("SelectionRange") {
const SelectionRange sr;
REQUIRE(sr.anchor == invalid);
REQUIRE(sr.caret == invalid);
}
SECTION("Serialization") {
// Conversion to/from string form
// Range from 1 to 2 with 3 virtual spaces
const SelectionRange range123(SelectionPosition(2, 3), SelectionPosition(1));
const std::string range123String(range123.ToString());
// Opposite order to constructor: from anchor to caret
REQUIRE(range123String == "1-2v3");
const SelectionRange range123Returned(range123String);
REQUIRE(range123Returned == range123);
}
SECTION("Intersect") {
constexpr SelectionSegment segmentEmpty;
// Range from 1 to 2 with 3 virtual spaces
const SelectionRange range123(SelectionPosition(2, 3), SelectionPosition(1));
const SelectionSegment segment12(1, 2);
const SelectionSegment inside = range123.Intersect(segment12);
REQUIRE(inside == segment12);
const SelectionSegment segment121(SelectionPosition(1), SelectionPosition(2, 1));
const SelectionSegment withVirtual = range123.Intersect(segment121);
REQUIRE(withVirtual == segment121);
const SelectionSegment segment052(SelectionPosition(0), SelectionPosition(5, 2));
const SelectionSegment internal = range123.Intersect(segment052); // All inside
REQUIRE(internal == range123.AsSegment());
const SelectionSegment wayOut(SelectionPosition(100), SelectionPosition(105, 2));
const SelectionSegment nothing = range123.Intersect(wayOut);
REQUIRE(nothing == segmentEmpty);
const SelectionSegment edge(1, 1);
const SelectionSegment nowt = range123.Intersect(edge);
REQUIRE(nowt == edge);
// (0, 1) and (1, 2v3) touch so intersection is a single position.
const SelectionSegment front(0, 1);
const SelectionSegment single(1, 1);
const SelectionSegment thin = range123.Intersect(front);
REQUIRE(thin == single);
}
}
TEST_CASE("Selection") {
SECTION("Selection") {
Selection sel;
REQUIRE(sel.selType == Selection::SelTypes::stream);
REQUIRE(!sel.IsRectangular());
REQUIRE(sel.Count() == 1);
REQUIRE(sel.Main() == 0);
REQUIRE(sel.Range(0) == rangeZero);
REQUIRE(sel.RangeMain() == rangeZero);
REQUIRE(sel.Rectangular() == rangeInvalid);
REQUIRE(sel.Empty());
}
SECTION("Serialization") {
// Conversion to/from string form
// Range from 5 with 3 virtual spaces to 2
const SelectionRange range532(SelectionPosition(2), SelectionPosition(5, 3));
Selection selection;
selection.SetSelection(range532);
const std::string selectionString(selection.ToString());
// Opposite order to constructor: from anchor to caret
REQUIRE(selectionString == "5v3-2");
const SelectionRange selectionReturned(selectionString);
REQUIRE(selection.selType == Selection::SelTypes::stream);
REQUIRE(!selection.IsRectangular());
REQUIRE(selection.Count() == 1);
REQUIRE(selection.Main() == 0);
REQUIRE(selection.Range(0) == range532);
REQUIRE(selection.RangeMain() == range532);
REQUIRE(selection.Rectangular() == rangeInvalid);
REQUIRE(!selection.Empty());
}
SECTION("SerializationMultiple") {
// Conversion to/from string form
// Range from 5 with 3 virtual spaces to 2
const SelectionRange range532(SelectionPosition(2), SelectionPosition(5, 3));
const SelectionRange range1(SelectionPosition(1));
Selection selection;
selection.SetSelection(range532);
selection.AddSelection(range1);
selection.SetMain(1);
const std::string selectionString(selection.ToString());
REQUIRE(selectionString == "5v3-2,1#1");
const SelectionRange selectionReturned(selectionString);
REQUIRE(selection.selType == Selection::SelTypes::stream);
REQUIRE(!selection.IsRectangular());
REQUIRE(selection.Count() == 2);
REQUIRE(selection.Main() == 1);
REQUIRE(selection.Range(0) == range532);
REQUIRE(selection.Range(1) == range1);
REQUIRE(selection.RangeMain() == range1);
REQUIRE(selection.Rectangular() == rangeInvalid);
REQUIRE(!selection.Empty());
}
SECTION("SerializationRectangular") {
// Conversion to/from string form
// Range from 5 with 3 virtual spaces to 2
const SelectionRange range532(SelectionPosition(2), SelectionPosition(5, 3));
// Create a single-line rectangular selection
Selection selection;
selection.selType = Selection::SelTypes::rectangle;
selection.Rectangular() = range532;
// Set arbitrary realized range - inside editor ranges would be calculated from line layout
selection.SetSelection(rangeZero);
const std::string selectionString(selection.ToString());
REQUIRE(selectionString == "R5v3-2");
const Selection selectionReturned(selectionString);
REQUIRE(selection.selType == Selection::SelTypes::rectangle);
REQUIRE(selection.IsRectangular());
REQUIRE(selection.Count() == 1);
REQUIRE(selection.Main() == 0);
REQUIRE(selection.Range(0) == rangeZero);
REQUIRE(selection.RangeMain() == rangeZero);
REQUIRE(selection.Rectangular() == range532);
selection.selType = Selection::SelTypes::thin;
const std::string thinString(selection.ToString());
REQUIRE(thinString == "T5v3-2");
}
}
|