TextEditorTests.cs
25.0 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
using System.Collections;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
public class TextEditorTests
{
TextEditor m_TextEditor;
static IEnumerable textWithCodePointBoundaryIndices
{
get
{
yield return new TestCaseData("", new[] { 0 });
yield return new TestCaseData("abc", new[] { 0, 1, 2, 3 });
yield return new TestCaseData("\U0001f642", new[] { 0, 2 }).SetName("(U+1F642)");
yield return new TestCaseData("\U0001f642\U0001f643", new[] { 0, 2, 4 }).SetName("(U+1F642)(U+1F643)");
yield return new TestCaseData("a\U0001f642b\U0001f643c", new[] { 0, 1, 3, 4, 6, 7 }).SetName("a(U+1F642)b(U+1F643)c");
yield return new TestCaseData("Hello 😁 World", new[] { 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14 }).SetName("Hello (U+1F601) World");
yield return new TestCaseData("見ざる🙈、聞かざる🙉、言わざる🙊", new[] { 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 19 }).SetName("Three wise monkeys");
}
}
static IEnumerable textWithWordStartAndEndIndices
{
get
{
yield return new TestCaseData("", new int[0], new int[0]);
yield return new TestCaseData(" ", new int[0], new int[0]);
yield return new TestCaseData("one two three", new[] { 0, 4, 8 }, new[] { 3, 7, 13 });
yield return new TestCaseData("\U00010000 \U00010001 \U00010002\U00010003", new[] { 0, 3, 6 }, new[] { 2, 5, 10 }).SetName("(U+10000) (U+10001) (U+10002)(U+10003)");
yield return new TestCaseData("Hello 😁 World", new[] { 0, 6, 9 }, new[] { 5, 8, 14 }).SetName("Hello (U+1F601) World");
yield return new TestCaseData("見ざる🙈、聞かざる🙉、言わざる🙊", new[] { 0, 3, 6, 10, 13, 17 }, new[] { 3, 6, 10, 13, 17, 19 }).SetName("Three wise monkeys");
}
}
// A sequences of punctuation characters is currently considered a word when deleting
static IEnumerable textWithWordStartAndEndIndicesWherePunctuationIsAWord
{
get
{
yield return new TestCaseData(" ,. abc,. ", new[] { 1, 4, 7 }, new[] { 3, 7, 9 });
}
}
// but not when moving/selecting
static IEnumerable textWithWordStartAndEndIndicesWherePunctuationIsNotAWord
{
get
{
yield return new TestCaseData(" ,. abc,. ", new[] { 4 }, new[] { 7 });
}
}
static IEnumerable textWithLineStartIndices
{
get
{
yield return new TestCaseData("\n\na\nbc\n\U0001f642\n\U0001f642\U0001f643\n\n ", new[] { 0, 1, 2, 4, 7, 10, 15, 16 }).SetName("(LF)(LF)a(LF)bc(LF)(U+1F642)(LF)(U+1F642)(U+1F643)(LF)(LF) ");
yield return new TestCaseData("\n\na\nbc\n🙂\n🙂🙃\n\n ", new[] { 0, 1, 2, 4, 7, 10, 15, 16 }).SetName("(LF)(LF)a(LF)bc(LF)(U+1F642)(LF)(U+1F642)(U+1F643)(LF)(LF) ");
}
}
static IEnumerable textWithLineEndIndices
{
get
{
yield return new TestCaseData("\n\na\nbc\n\U0001f642\n\U0001f642\U0001f643\n\n ", new[] { 0, 1, 3, 6, 9, 14, 15, 17 }).SetName("(LF)(LF)a(LF)bc(LF)(U+1F642)(LF)(U+1F642)(U+1F643)(LF)(LF) ");
yield return new TestCaseData("\n\na\nbc\n🙂\n🙂🙃\n\n ", new[] { 0, 1, 3, 6, 9, 14, 15, 17 }).SetName("(LF)(LF)a(LF)bc(LF)(U+1F642)(LF)(U+1F642)(U+1F643)(LF)(LF) ");
}
}
static IEnumerable textWithExpectedCursorAndSelectIndicesWhenSelectingCurrentWordAtIndex
{
get
{
yield return new TestCaseData("", new[] { 0 }, new[] { 0 });
yield return new TestCaseData(" ", new[] { 1, 1 }, new[] { 0, 0 });
yield return new TestCaseData("a", new[] { 1, 1 }, new[] { 0, 0 });
yield return new TestCaseData("ab", new[] { 2, 2, 2 }, new[] { 0, 0, 0 });
yield return new TestCaseData("ab cd", new[] { 2, 2, 4, 4, 6, 6, 6 }, new[] { 0, 0, 2, 2, 4, 4, 4 });
yield return new TestCaseData("a,, ,, ,,b", new[] { 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11, 12, 12 }, new[] { 0, 1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11 });
}
}
[SetUp]
public void TestSetup()
{
m_TextEditor = new TextEditor();
m_TextEditor.DetectFocusChange();
}
[Test]
public void SetText_MovesCursorAndSelectIndicesToNextCodePointIndexIfInvalid()
{
m_TextEditor.text = "ab";
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 1;
m_TextEditor.text = "\U0001f642";
Assert.AreEqual(2, m_TextEditor.cursorIndex, "cursorIndex at invalid code point index");
Assert.AreEqual(2, m_TextEditor.selectIndex, "selectIndex at invalid code point index");
}
[Test, TestCaseSource("textWithCodePointBoundaryIndices")]
public void SetCursorAndSelectIndices_MovesToNextCodePointIndexIfInvalid(string text, int[] codePointIndices)
{
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var nextCodePointIndex = index == text.Length ? index : codePointIndices.First(codePointIndex => codePointIndex > index);
if (codePointIndices.Contains(index))
Assert.AreEqual(index, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} should not change if it's already at a valid code point index", index));
else
Assert.AreEqual(nextCodePointIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to next code point index", index));
if (codePointIndices.Contains(index))
Assert.AreEqual(index, m_TextEditor.selectIndex, string.Format("selectIndex {0} should not change if it's already at a valid code point index", index));
else
Assert.AreEqual(nextCodePointIndex, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to next code point index", index));
}
}
[Test]
[TestCaseSource("textWithWordStartAndEndIndices")]
[TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
public void DeleteWordBack_DeletesBackToPreviousWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
{
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.text = text;
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.DeleteWordBack();
var previousWordStart = wordStartIndices.Reverse().FirstOrDefault(i => i < oldCursorIndex);
Assert.AreEqual(previousWordStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous word start", oldCursorIndex));
Assert.AreEqual(previousWordStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to previous word start", oldSelectIndex));
Assert.AreEqual(text.Remove(previousWordStart, oldCursorIndex - previousWordStart), m_TextEditor.text, string.Format("wrong resulting text for cursorIndex {0}", oldCursorIndex));
}
}
[Test]
[TestCaseSource("textWithWordStartAndEndIndices")]
[TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
public void DeleteWordForward_DeletesForwardToNextWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
{
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.text = text;
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.DeleteWordForward();
var nextWordStart = oldCursorIndex == text.Length ? text.Length : wordStartIndices.Concat(new[] { text.Length }).First(i => i > oldCursorIndex);
Assert.AreEqual(oldCursorIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} should not change", oldCursorIndex));
Assert.AreEqual(oldSelectIndex, m_TextEditor.selectIndex, string.Format("selectIndex {0} should not change", oldSelectIndex));
Assert.AreEqual(text.Remove(oldCursorIndex, nextWordStart - oldCursorIndex), m_TextEditor.text, string.Format("wrong resulting text for cursorIndex {0}", oldCursorIndex));
}
}
[Test, TestCaseSource("textWithCodePointBoundaryIndices")]
public void Delete_RemovesCodePointRightOfCursor(string text, int[] codePointIndices)
{
for (var i = 0; i < codePointIndices.Length; i++)
{
var codePointIndex = codePointIndices[i];
m_TextEditor.text = text;
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = codePointIndex;
m_TextEditor.Delete();
var nextCodePointIndex = i < codePointIndices.Length - 1 ? codePointIndices[i + 1] : codePointIndex;
Assert.AreEqual(codePointIndex, m_TextEditor.cursorIndex, "cursorIndex should not change");
Assert.AreEqual(codePointIndex, m_TextEditor.selectIndex, "selectIndex should not change");
Assert.AreEqual(text.Remove(codePointIndex, nextCodePointIndex - codePointIndex), m_TextEditor.text, string.Format("wrong resulting text for cursorIndex {0}", codePointIndex));
}
}
[Test, TestCaseSource("textWithCodePointBoundaryIndices")]
public void Backspace_RemovesCodePointLeftOfCursor(string text, int[] codePointIndices)
{
for (var i = codePointIndices.Length - 1; i >= 0; i--)
{
var codePointIndex = codePointIndices[i];
m_TextEditor.text = text;
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = codePointIndex;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.Backspace();
var previousCodePointIndex = i > 0 ? codePointIndices[i - 1] : codePointIndex;
var codePointLength = codePointIndex - previousCodePointIndex;
Assert.AreEqual(oldCursorIndex - codePointLength, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to before removed code point", oldCursorIndex));
Assert.AreEqual(oldSelectIndex - codePointLength, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to before removed code point", oldSelectIndex));
Assert.AreEqual(text.Remove(previousCodePointIndex, codePointLength), m_TextEditor.text);
}
}
[Test, TestCaseSource("textWithCodePointBoundaryIndices")]
public void MoveRight_SkipsInvalidCodePointIndices(string text, int[] codePointIndices)
{
m_TextEditor.text = text;
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 0;
foreach (var expectedIndex in codePointIndices.Skip(1))
{
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.MoveRight();
Assert.AreEqual(expectedIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to next code point index", oldCursorIndex));
Assert.AreEqual(expectedIndex, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to next code point index", oldSelectIndex));
}
Assert.AreEqual(text.Length, m_TextEditor.cursorIndex, "cursorIndex did not reach end");
Assert.AreEqual(text.Length, m_TextEditor.selectIndex, "selectIndex did not reach end");
m_TextEditor.MoveRight();
Assert.AreEqual(text.Length, m_TextEditor.cursorIndex, "cursorIndex at end should not change");
Assert.AreEqual(text.Length, m_TextEditor.selectIndex, "selectIndex at end should not change");
}
[Test, TestCaseSource("textWithCodePointBoundaryIndices")]
public void MoveLeft_SkipsInvalidCodePointIndices(string text, int[] codePointIndices)
{
m_TextEditor.text = text;
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = text.Length;
foreach (var expectedIndex in codePointIndices.Reverse().Skip(1))
{
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.MoveLeft();
Assert.AreEqual(expectedIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous code point index", oldCursorIndex));
Assert.AreEqual(expectedIndex, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to previous code point index", oldSelectIndex));
}
Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex did not reach start");
Assert.AreEqual(0, m_TextEditor.selectIndex, "selectIndex did not reach start");
m_TextEditor.MoveLeft();
Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex at start should not change");
Assert.AreEqual(0, m_TextEditor.selectIndex, "selectIndex at start should not change");
}
[Test, TestCaseSource("textWithLineStartIndices")]
public void MoveLineStart_MovesCursorAfterPreviousLineFeed(string text, int[] lineStartIndices)
{
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.MoveLineStart();
var lineStart = lineStartIndices.Reverse().FirstOrDefault(i => i <= oldCursorIndex);
Assert.AreEqual(lineStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to line start", oldCursorIndex));
Assert.AreEqual(lineStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to line start", oldSelectIndex));
}
}
[Test, TestCaseSource("textWithLineEndIndices")]
public void MoveLineEnd_MovesCursorBeforeNextLineFeed(string text, int[] lineEndIndices)
{
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.MoveLineEnd();
var lineEnd = lineEndIndices.First(i => i >= oldCursorIndex);
Assert.AreEqual(lineEnd, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to line end", oldCursorIndex));
Assert.AreEqual(lineEnd, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to line end", oldSelectIndex));
}
}
[Test]
public void MoveTextStart_MovesCursorToStartOfText()
{
m_TextEditor.text = "Hello World";
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 5;
m_TextEditor.MoveTextStart();
Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex did not move to start of text");
Assert.AreEqual(0, m_TextEditor.selectIndex, "selectIndex did not move to start of text");
}
[Test]
public void MoveTextEnd_MovesCursorToEndOfText()
{
m_TextEditor.text = "Hello World";
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 5;
m_TextEditor.MoveTextEnd();
Assert.AreEqual(m_TextEditor.text.Length, m_TextEditor.cursorIndex, "cursorIndex did not move to end of text");
Assert.AreEqual(m_TextEditor.text.Length, m_TextEditor.selectIndex, "selectIndex did not move to end of text");
}
[Test, TestCaseSource("textWithCodePointBoundaryIndices")]
public void SelectLeft_ExpandSelectionToPreviousCodePoint(string text, int[] codePointIndices)
{
m_TextEditor.text = text;
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = text.Length;
foreach (var expectedCursorIndex in codePointIndices.Reverse().Skip(1))
{
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.SelectLeft();
Assert.AreEqual(expectedCursorIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous code point index", oldCursorIndex));
Assert.AreEqual(oldSelectIndex, m_TextEditor.selectIndex, "selectIndex should not change");
}
Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex did not reach start");
m_TextEditor.SelectLeft();
Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex at start should not change");
}
[Test, TestCaseSource("textWithCodePointBoundaryIndices")]
public void SelectRight_ExpandSelectionToNextCodePoint(string text, int[] codePointIndices)
{
m_TextEditor.text = text;
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 0;
foreach (var expectedCursorIndex in codePointIndices.Skip(1))
{
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.SelectRight();
Assert.AreEqual(expectedCursorIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to next code point index", oldCursorIndex));
Assert.AreEqual(oldSelectIndex, m_TextEditor.selectIndex, "selectIndex should not change");
}
Assert.AreEqual(text.Length, m_TextEditor.cursorIndex, "cursorIndex did not reach end");
m_TextEditor.SelectRight();
Assert.AreEqual(text.Length, m_TextEditor.cursorIndex, "cursorIndex at end should not change");
}
[Test]
[TestCaseSource("textWithWordStartAndEndIndices")]
[TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsNotAWord")]
public void MoveWordRight_MovesCursorToNextWordEnd(string text, int[] wordStartIndices, int[] wordEndIndices)
{
if (text.Any(char.IsSurrogate))
Assert.Ignore("char.IsLetterOrDigit(string, int) does not currently work correctly with surrogates");
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.MoveWordRight();
var nextWordEnd = wordEndIndices.FirstOrDefault(i => i > oldCursorIndex);
if (nextWordEnd == 0)
nextWordEnd = text.Length;
Assert.AreEqual(nextWordEnd, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to next word start", oldCursorIndex));
Assert.AreEqual(nextWordEnd, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to next word start", oldSelectIndex));
}
}
[Test]
[TestCaseSource("textWithWordStartAndEndIndices")]
[TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
public void MoveToStartOfNextWord_MovesCursorToNextWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
{
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.MoveToStartOfNextWord();
var nextWordStart = oldCursorIndex == text.Length ? text.Length : wordStartIndices.Concat(new[] { text.Length }).First(i => i > oldCursorIndex);
Assert.AreEqual(nextWordStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to start of next word", oldCursorIndex));
Assert.AreEqual(nextWordStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to start of next word", oldSelectIndex));
}
}
[Test]
[TestCaseSource("textWithWordStartAndEndIndices")]
[TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
public void MoveToEndOfPreviousWord_MovesCursorToPreviousWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
{
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.MoveToEndOfPreviousWord();
var previousWordStart = wordStartIndices.Reverse().FirstOrDefault(i => i < oldCursorIndex);
Assert.AreEqual(previousWordStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous word start", oldCursorIndex));
Assert.AreEqual(previousWordStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to previous word start", oldSelectIndex));
}
}
[Test]
[TestCaseSource("textWithWordStartAndEndIndices")]
[TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
public void FindStartOfNextWord_ReturnsIndexOfNextWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
{
if (text.Any(char.IsSurrogate))
Assert.Ignore("char.IsLetterOrDigit(string, int) does not currently work correctly with surrogates");
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
var nextWordStart = index == text.Length ? text.Length : wordStartIndices.Concat(new[] {text.Length}).First(i => i > index);
Assert.AreEqual(nextWordStart, m_TextEditor.FindStartOfNextWord(index));
}
}
[Test]
[TestCaseSource("textWithWordStartAndEndIndices")]
[TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsNotAWord")]
public void MoveWordLeft_MovesCursorToPreviousWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
{
if (text.Any(char.IsSurrogate))
Assert.Ignore("char.IsLetterOrDigit(string, int) does not currently work correctly with surrogates");
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
var oldSelectIndex = m_TextEditor.selectIndex;
m_TextEditor.MoveWordLeft();
var previousWordStart = wordStartIndices.Reverse().FirstOrDefault(i => i < oldCursorIndex);
Assert.AreEqual(previousWordStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous word start", oldCursorIndex));
Assert.AreEqual(previousWordStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to previous word start", oldSelectIndex));
}
}
[Test, TestCaseSource("textWithExpectedCursorAndSelectIndicesWhenSelectingCurrentWordAtIndex")]
public void SelectCurrentWord(string text, int[] expectedCursorIndices, int[] expectedSelectIndices)
{
m_TextEditor.text = text;
for (var index = 0; index <= text.Length; index++)
{
m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
var oldCursorIndex = m_TextEditor.cursorIndex;
m_TextEditor.SelectCurrentWord();
Assert.AreEqual(expectedCursorIndices[index], m_TextEditor.cursorIndex, string.Format("wrong cursorIndex for initial cursorIndex {0}", oldCursorIndex));
Assert.AreEqual(expectedSelectIndices[index], m_TextEditor.selectIndex, string.Format("wrong selectIndex for initial cursorIndex {0}", oldCursorIndex));
}
}
[Test]
public void HandleKeyEvent_WithControlAKeyDownEvent_MovesCursorToStartOfLineOnMacOS_SelectsAllElsewhere()
{
const string text = "foo";
m_TextEditor.text = text;
m_TextEditor.MoveLineEnd();
var controlAKeyDownEvent = new Event { type = EventType.KeyDown, keyCode = KeyCode.A, modifiers = EventModifiers.Control };
m_TextEditor.HandleKeyEvent(controlAKeyDownEvent);
if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX)
{
Assert.That(m_TextEditor.SelectedText, Is.Empty, "Selected text was not empty");
Assert.That(m_TextEditor.cursorIndex, Is.EqualTo(0), "Cursor did not move to start of line");
}
else
Assert.That(m_TextEditor.SelectedText, Is.EqualTo(text), "Text was not selected");
}
[Test]
public void HandleKeyEvent_WithCommandAKeyDownEvent_SelectsAllOnMacOS()
{
if (SystemInfo.operatingSystemFamily != OperatingSystemFamily.MacOSX)
Assert.Ignore("Test is only applicable on macOS");
const string text = "foo";
m_TextEditor.text = text;
var commandAKeyDownEvent = new Event { type = EventType.KeyDown, keyCode = KeyCode.A, modifiers = EventModifiers.Command };
m_TextEditor.HandleKeyEvent(commandAKeyDownEvent);
Assert.That(m_TextEditor.SelectedText, Is.EqualTo(text), "Text was not selected");
}
}