TextEditorTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. using System.Collections;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. public class TextEditorTests
  6. {
  7. TextEditor m_TextEditor;
  8. static IEnumerable textWithCodePointBoundaryIndices
  9. {
  10. get
  11. {
  12. yield return new TestCaseData("", new[] { 0 });
  13. yield return new TestCaseData("abc", new[] { 0, 1, 2, 3 });
  14. yield return new TestCaseData("\U0001f642", new[] { 0, 2 }).SetName("(U+1F642)");
  15. yield return new TestCaseData("\U0001f642\U0001f643", new[] { 0, 2, 4 }).SetName("(U+1F642)(U+1F643)");
  16. yield return new TestCaseData("a\U0001f642b\U0001f643c", new[] { 0, 1, 3, 4, 6, 7 }).SetName("a(U+1F642)b(U+1F643)c");
  17. 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");
  18. 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");
  19. }
  20. }
  21. static IEnumerable textWithWordStartAndEndIndices
  22. {
  23. get
  24. {
  25. yield return new TestCaseData("", new int[0], new int[0]);
  26. yield return new TestCaseData(" ", new int[0], new int[0]);
  27. yield return new TestCaseData("one two three", new[] { 0, 4, 8 }, new[] { 3, 7, 13 });
  28. 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)");
  29. yield return new TestCaseData("Hello 😁 World", new[] { 0, 6, 9 }, new[] { 5, 8, 14 }).SetName("Hello (U+1F601) World");
  30. yield return new TestCaseData("見ざる🙈、聞かざる🙉、言わざる🙊", new[] { 0, 3, 6, 10, 13, 17 }, new[] { 3, 6, 10, 13, 17, 19 }).SetName("Three wise monkeys");
  31. }
  32. }
  33. // A sequences of punctuation characters is currently considered a word when deleting
  34. static IEnumerable textWithWordStartAndEndIndicesWherePunctuationIsAWord
  35. {
  36. get
  37. {
  38. yield return new TestCaseData(" ,. abc,. ", new[] { 1, 4, 7 }, new[] { 3, 7, 9 });
  39. }
  40. }
  41. // but not when moving/selecting
  42. static IEnumerable textWithWordStartAndEndIndicesWherePunctuationIsNotAWord
  43. {
  44. get
  45. {
  46. yield return new TestCaseData(" ,. abc,. ", new[] { 4 }, new[] { 7 });
  47. }
  48. }
  49. static IEnumerable textWithLineStartIndices
  50. {
  51. get
  52. {
  53. 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) ");
  54. 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) ");
  55. }
  56. }
  57. static IEnumerable textWithLineEndIndices
  58. {
  59. get
  60. {
  61. 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) ");
  62. 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) ");
  63. }
  64. }
  65. static IEnumerable textWithExpectedCursorAndSelectIndicesWhenSelectingCurrentWordAtIndex
  66. {
  67. get
  68. {
  69. yield return new TestCaseData("", new[] { 0 }, new[] { 0 });
  70. yield return new TestCaseData(" ", new[] { 1, 1 }, new[] { 0, 0 });
  71. yield return new TestCaseData("a", new[] { 1, 1 }, new[] { 0, 0 });
  72. yield return new TestCaseData("ab", new[] { 2, 2, 2 }, new[] { 0, 0, 0 });
  73. yield return new TestCaseData("ab cd", new[] { 2, 2, 4, 4, 6, 6, 6 }, new[] { 0, 0, 2, 2, 4, 4, 4 });
  74. 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 });
  75. }
  76. }
  77. [SetUp]
  78. public void TestSetup()
  79. {
  80. m_TextEditor = new TextEditor();
  81. m_TextEditor.DetectFocusChange();
  82. }
  83. [Test]
  84. public void SetText_MovesCursorAndSelectIndicesToNextCodePointIndexIfInvalid()
  85. {
  86. m_TextEditor.text = "ab";
  87. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 1;
  88. m_TextEditor.text = "\U0001f642";
  89. Assert.AreEqual(2, m_TextEditor.cursorIndex, "cursorIndex at invalid code point index");
  90. Assert.AreEqual(2, m_TextEditor.selectIndex, "selectIndex at invalid code point index");
  91. }
  92. [Test, TestCaseSource("textWithCodePointBoundaryIndices")]
  93. public void SetCursorAndSelectIndices_MovesToNextCodePointIndexIfInvalid(string text, int[] codePointIndices)
  94. {
  95. m_TextEditor.text = text;
  96. for (var index = 0; index <= text.Length; index++)
  97. {
  98. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  99. var nextCodePointIndex = index == text.Length ? index : codePointIndices.First(codePointIndex => codePointIndex > index);
  100. if (codePointIndices.Contains(index))
  101. Assert.AreEqual(index, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} should not change if it's already at a valid code point index", index));
  102. else
  103. Assert.AreEqual(nextCodePointIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to next code point index", index));
  104. if (codePointIndices.Contains(index))
  105. Assert.AreEqual(index, m_TextEditor.selectIndex, string.Format("selectIndex {0} should not change if it's already at a valid code point index", index));
  106. else
  107. Assert.AreEqual(nextCodePointIndex, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to next code point index", index));
  108. }
  109. }
  110. [Test]
  111. [TestCaseSource("textWithWordStartAndEndIndices")]
  112. [TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
  113. public void DeleteWordBack_DeletesBackToPreviousWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
  114. {
  115. for (var index = 0; index <= text.Length; index++)
  116. {
  117. m_TextEditor.text = text;
  118. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  119. var oldCursorIndex = m_TextEditor.cursorIndex;
  120. var oldSelectIndex = m_TextEditor.selectIndex;
  121. m_TextEditor.DeleteWordBack();
  122. var previousWordStart = wordStartIndices.Reverse().FirstOrDefault(i => i < oldCursorIndex);
  123. Assert.AreEqual(previousWordStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous word start", oldCursorIndex));
  124. Assert.AreEqual(previousWordStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to previous word start", oldSelectIndex));
  125. Assert.AreEqual(text.Remove(previousWordStart, oldCursorIndex - previousWordStart), m_TextEditor.text, string.Format("wrong resulting text for cursorIndex {0}", oldCursorIndex));
  126. }
  127. }
  128. [Test]
  129. [TestCaseSource("textWithWordStartAndEndIndices")]
  130. [TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
  131. public void DeleteWordForward_DeletesForwardToNextWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
  132. {
  133. for (var index = 0; index <= text.Length; index++)
  134. {
  135. m_TextEditor.text = text;
  136. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  137. var oldCursorIndex = m_TextEditor.cursorIndex;
  138. var oldSelectIndex = m_TextEditor.selectIndex;
  139. m_TextEditor.DeleteWordForward();
  140. var nextWordStart = oldCursorIndex == text.Length ? text.Length : wordStartIndices.Concat(new[] { text.Length }).First(i => i > oldCursorIndex);
  141. Assert.AreEqual(oldCursorIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} should not change", oldCursorIndex));
  142. Assert.AreEqual(oldSelectIndex, m_TextEditor.selectIndex, string.Format("selectIndex {0} should not change", oldSelectIndex));
  143. Assert.AreEqual(text.Remove(oldCursorIndex, nextWordStart - oldCursorIndex), m_TextEditor.text, string.Format("wrong resulting text for cursorIndex {0}", oldCursorIndex));
  144. }
  145. }
  146. [Test, TestCaseSource("textWithCodePointBoundaryIndices")]
  147. public void Delete_RemovesCodePointRightOfCursor(string text, int[] codePointIndices)
  148. {
  149. for (var i = 0; i < codePointIndices.Length; i++)
  150. {
  151. var codePointIndex = codePointIndices[i];
  152. m_TextEditor.text = text;
  153. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = codePointIndex;
  154. m_TextEditor.Delete();
  155. var nextCodePointIndex = i < codePointIndices.Length - 1 ? codePointIndices[i + 1] : codePointIndex;
  156. Assert.AreEqual(codePointIndex, m_TextEditor.cursorIndex, "cursorIndex should not change");
  157. Assert.AreEqual(codePointIndex, m_TextEditor.selectIndex, "selectIndex should not change");
  158. Assert.AreEqual(text.Remove(codePointIndex, nextCodePointIndex - codePointIndex), m_TextEditor.text, string.Format("wrong resulting text for cursorIndex {0}", codePointIndex));
  159. }
  160. }
  161. [Test, TestCaseSource("textWithCodePointBoundaryIndices")]
  162. public void Backspace_RemovesCodePointLeftOfCursor(string text, int[] codePointIndices)
  163. {
  164. for (var i = codePointIndices.Length - 1; i >= 0; i--)
  165. {
  166. var codePointIndex = codePointIndices[i];
  167. m_TextEditor.text = text;
  168. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = codePointIndex;
  169. var oldCursorIndex = m_TextEditor.cursorIndex;
  170. var oldSelectIndex = m_TextEditor.selectIndex;
  171. m_TextEditor.Backspace();
  172. var previousCodePointIndex = i > 0 ? codePointIndices[i - 1] : codePointIndex;
  173. var codePointLength = codePointIndex - previousCodePointIndex;
  174. Assert.AreEqual(oldCursorIndex - codePointLength, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to before removed code point", oldCursorIndex));
  175. Assert.AreEqual(oldSelectIndex - codePointLength, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to before removed code point", oldSelectIndex));
  176. Assert.AreEqual(text.Remove(previousCodePointIndex, codePointLength), m_TextEditor.text);
  177. }
  178. }
  179. [Test, TestCaseSource("textWithCodePointBoundaryIndices")]
  180. public void MoveRight_SkipsInvalidCodePointIndices(string text, int[] codePointIndices)
  181. {
  182. m_TextEditor.text = text;
  183. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 0;
  184. foreach (var expectedIndex in codePointIndices.Skip(1))
  185. {
  186. var oldCursorIndex = m_TextEditor.cursorIndex;
  187. var oldSelectIndex = m_TextEditor.selectIndex;
  188. m_TextEditor.MoveRight();
  189. Assert.AreEqual(expectedIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to next code point index", oldCursorIndex));
  190. Assert.AreEqual(expectedIndex, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to next code point index", oldSelectIndex));
  191. }
  192. Assert.AreEqual(text.Length, m_TextEditor.cursorIndex, "cursorIndex did not reach end");
  193. Assert.AreEqual(text.Length, m_TextEditor.selectIndex, "selectIndex did not reach end");
  194. m_TextEditor.MoveRight();
  195. Assert.AreEqual(text.Length, m_TextEditor.cursorIndex, "cursorIndex at end should not change");
  196. Assert.AreEqual(text.Length, m_TextEditor.selectIndex, "selectIndex at end should not change");
  197. }
  198. [Test, TestCaseSource("textWithCodePointBoundaryIndices")]
  199. public void MoveLeft_SkipsInvalidCodePointIndices(string text, int[] codePointIndices)
  200. {
  201. m_TextEditor.text = text;
  202. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = text.Length;
  203. foreach (var expectedIndex in codePointIndices.Reverse().Skip(1))
  204. {
  205. var oldCursorIndex = m_TextEditor.cursorIndex;
  206. var oldSelectIndex = m_TextEditor.selectIndex;
  207. m_TextEditor.MoveLeft();
  208. Assert.AreEqual(expectedIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous code point index", oldCursorIndex));
  209. Assert.AreEqual(expectedIndex, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to previous code point index", oldSelectIndex));
  210. }
  211. Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex did not reach start");
  212. Assert.AreEqual(0, m_TextEditor.selectIndex, "selectIndex did not reach start");
  213. m_TextEditor.MoveLeft();
  214. Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex at start should not change");
  215. Assert.AreEqual(0, m_TextEditor.selectIndex, "selectIndex at start should not change");
  216. }
  217. [Test, TestCaseSource("textWithLineStartIndices")]
  218. public void MoveLineStart_MovesCursorAfterPreviousLineFeed(string text, int[] lineStartIndices)
  219. {
  220. m_TextEditor.text = text;
  221. for (var index = 0; index <= text.Length; index++)
  222. {
  223. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  224. var oldCursorIndex = m_TextEditor.cursorIndex;
  225. var oldSelectIndex = m_TextEditor.selectIndex;
  226. m_TextEditor.MoveLineStart();
  227. var lineStart = lineStartIndices.Reverse().FirstOrDefault(i => i <= oldCursorIndex);
  228. Assert.AreEqual(lineStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to line start", oldCursorIndex));
  229. Assert.AreEqual(lineStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to line start", oldSelectIndex));
  230. }
  231. }
  232. [Test, TestCaseSource("textWithLineEndIndices")]
  233. public void MoveLineEnd_MovesCursorBeforeNextLineFeed(string text, int[] lineEndIndices)
  234. {
  235. m_TextEditor.text = text;
  236. for (var index = 0; index <= text.Length; index++)
  237. {
  238. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  239. var oldCursorIndex = m_TextEditor.cursorIndex;
  240. var oldSelectIndex = m_TextEditor.selectIndex;
  241. m_TextEditor.MoveLineEnd();
  242. var lineEnd = lineEndIndices.First(i => i >= oldCursorIndex);
  243. Assert.AreEqual(lineEnd, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to line end", oldCursorIndex));
  244. Assert.AreEqual(lineEnd, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to line end", oldSelectIndex));
  245. }
  246. }
  247. [Test]
  248. public void MoveTextStart_MovesCursorToStartOfText()
  249. {
  250. m_TextEditor.text = "Hello World";
  251. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 5;
  252. m_TextEditor.MoveTextStart();
  253. Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex did not move to start of text");
  254. Assert.AreEqual(0, m_TextEditor.selectIndex, "selectIndex did not move to start of text");
  255. }
  256. [Test]
  257. public void MoveTextEnd_MovesCursorToEndOfText()
  258. {
  259. m_TextEditor.text = "Hello World";
  260. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 5;
  261. m_TextEditor.MoveTextEnd();
  262. Assert.AreEqual(m_TextEditor.text.Length, m_TextEditor.cursorIndex, "cursorIndex did not move to end of text");
  263. Assert.AreEqual(m_TextEditor.text.Length, m_TextEditor.selectIndex, "selectIndex did not move to end of text");
  264. }
  265. [Test, TestCaseSource("textWithCodePointBoundaryIndices")]
  266. public void SelectLeft_ExpandSelectionToPreviousCodePoint(string text, int[] codePointIndices)
  267. {
  268. m_TextEditor.text = text;
  269. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = text.Length;
  270. foreach (var expectedCursorIndex in codePointIndices.Reverse().Skip(1))
  271. {
  272. var oldCursorIndex = m_TextEditor.cursorIndex;
  273. var oldSelectIndex = m_TextEditor.selectIndex;
  274. m_TextEditor.SelectLeft();
  275. Assert.AreEqual(expectedCursorIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous code point index", oldCursorIndex));
  276. Assert.AreEqual(oldSelectIndex, m_TextEditor.selectIndex, "selectIndex should not change");
  277. }
  278. Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex did not reach start");
  279. m_TextEditor.SelectLeft();
  280. Assert.AreEqual(0, m_TextEditor.cursorIndex, "cursorIndex at start should not change");
  281. }
  282. [Test, TestCaseSource("textWithCodePointBoundaryIndices")]
  283. public void SelectRight_ExpandSelectionToNextCodePoint(string text, int[] codePointIndices)
  284. {
  285. m_TextEditor.text = text;
  286. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = 0;
  287. foreach (var expectedCursorIndex in codePointIndices.Skip(1))
  288. {
  289. var oldCursorIndex = m_TextEditor.cursorIndex;
  290. var oldSelectIndex = m_TextEditor.selectIndex;
  291. m_TextEditor.SelectRight();
  292. Assert.AreEqual(expectedCursorIndex, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to next code point index", oldCursorIndex));
  293. Assert.AreEqual(oldSelectIndex, m_TextEditor.selectIndex, "selectIndex should not change");
  294. }
  295. Assert.AreEqual(text.Length, m_TextEditor.cursorIndex, "cursorIndex did not reach end");
  296. m_TextEditor.SelectRight();
  297. Assert.AreEqual(text.Length, m_TextEditor.cursorIndex, "cursorIndex at end should not change");
  298. }
  299. [Test]
  300. [TestCaseSource("textWithWordStartAndEndIndices")]
  301. [TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsNotAWord")]
  302. public void MoveWordRight_MovesCursorToNextWordEnd(string text, int[] wordStartIndices, int[] wordEndIndices)
  303. {
  304. if (text.Any(char.IsSurrogate))
  305. Assert.Ignore("char.IsLetterOrDigit(string, int) does not currently work correctly with surrogates");
  306. m_TextEditor.text = text;
  307. for (var index = 0; index <= text.Length; index++)
  308. {
  309. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  310. var oldCursorIndex = m_TextEditor.cursorIndex;
  311. var oldSelectIndex = m_TextEditor.selectIndex;
  312. m_TextEditor.MoveWordRight();
  313. var nextWordEnd = wordEndIndices.FirstOrDefault(i => i > oldCursorIndex);
  314. if (nextWordEnd == 0)
  315. nextWordEnd = text.Length;
  316. Assert.AreEqual(nextWordEnd, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to next word start", oldCursorIndex));
  317. Assert.AreEqual(nextWordEnd, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to next word start", oldSelectIndex));
  318. }
  319. }
  320. [Test]
  321. [TestCaseSource("textWithWordStartAndEndIndices")]
  322. [TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
  323. public void MoveToStartOfNextWord_MovesCursorToNextWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
  324. {
  325. m_TextEditor.text = text;
  326. for (var index = 0; index <= text.Length; index++)
  327. {
  328. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  329. var oldCursorIndex = m_TextEditor.cursorIndex;
  330. var oldSelectIndex = m_TextEditor.selectIndex;
  331. m_TextEditor.MoveToStartOfNextWord();
  332. var nextWordStart = oldCursorIndex == text.Length ? text.Length : wordStartIndices.Concat(new[] { text.Length }).First(i => i > oldCursorIndex);
  333. Assert.AreEqual(nextWordStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to start of next word", oldCursorIndex));
  334. Assert.AreEqual(nextWordStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to start of next word", oldSelectIndex));
  335. }
  336. }
  337. [Test]
  338. [TestCaseSource("textWithWordStartAndEndIndices")]
  339. [TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
  340. public void MoveToEndOfPreviousWord_MovesCursorToPreviousWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
  341. {
  342. m_TextEditor.text = text;
  343. for (var index = 0; index <= text.Length; index++)
  344. {
  345. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  346. var oldCursorIndex = m_TextEditor.cursorIndex;
  347. var oldSelectIndex = m_TextEditor.selectIndex;
  348. m_TextEditor.MoveToEndOfPreviousWord();
  349. var previousWordStart = wordStartIndices.Reverse().FirstOrDefault(i => i < oldCursorIndex);
  350. Assert.AreEqual(previousWordStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous word start", oldCursorIndex));
  351. Assert.AreEqual(previousWordStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to previous word start", oldSelectIndex));
  352. }
  353. }
  354. [Test]
  355. [TestCaseSource("textWithWordStartAndEndIndices")]
  356. [TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsAWord")]
  357. public void FindStartOfNextWord_ReturnsIndexOfNextWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
  358. {
  359. if (text.Any(char.IsSurrogate))
  360. Assert.Ignore("char.IsLetterOrDigit(string, int) does not currently work correctly with surrogates");
  361. m_TextEditor.text = text;
  362. for (var index = 0; index <= text.Length; index++)
  363. {
  364. var nextWordStart = index == text.Length ? text.Length : wordStartIndices.Concat(new[] {text.Length}).First(i => i > index);
  365. Assert.AreEqual(nextWordStart, m_TextEditor.FindStartOfNextWord(index));
  366. }
  367. }
  368. [Test]
  369. [TestCaseSource("textWithWordStartAndEndIndices")]
  370. [TestCaseSource("textWithWordStartAndEndIndicesWherePunctuationIsNotAWord")]
  371. public void MoveWordLeft_MovesCursorToPreviousWordStart(string text, int[] wordStartIndices, int[] wordEndIndices)
  372. {
  373. if (text.Any(char.IsSurrogate))
  374. Assert.Ignore("char.IsLetterOrDigit(string, int) does not currently work correctly with surrogates");
  375. m_TextEditor.text = text;
  376. for (var index = 0; index <= text.Length; index++)
  377. {
  378. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  379. var oldCursorIndex = m_TextEditor.cursorIndex;
  380. var oldSelectIndex = m_TextEditor.selectIndex;
  381. m_TextEditor.MoveWordLeft();
  382. var previousWordStart = wordStartIndices.Reverse().FirstOrDefault(i => i < oldCursorIndex);
  383. Assert.AreEqual(previousWordStart, m_TextEditor.cursorIndex, string.Format("cursorIndex {0} did not move to previous word start", oldCursorIndex));
  384. Assert.AreEqual(previousWordStart, m_TextEditor.selectIndex, string.Format("selectIndex {0} did not move to previous word start", oldSelectIndex));
  385. }
  386. }
  387. [Test, TestCaseSource("textWithExpectedCursorAndSelectIndicesWhenSelectingCurrentWordAtIndex")]
  388. public void SelectCurrentWord(string text, int[] expectedCursorIndices, int[] expectedSelectIndices)
  389. {
  390. m_TextEditor.text = text;
  391. for (var index = 0; index <= text.Length; index++)
  392. {
  393. m_TextEditor.cursorIndex = m_TextEditor.selectIndex = index;
  394. var oldCursorIndex = m_TextEditor.cursorIndex;
  395. m_TextEditor.SelectCurrentWord();
  396. Assert.AreEqual(expectedCursorIndices[index], m_TextEditor.cursorIndex, string.Format("wrong cursorIndex for initial cursorIndex {0}", oldCursorIndex));
  397. Assert.AreEqual(expectedSelectIndices[index], m_TextEditor.selectIndex, string.Format("wrong selectIndex for initial cursorIndex {0}", oldCursorIndex));
  398. }
  399. }
  400. [Test]
  401. public void HandleKeyEvent_WithControlAKeyDownEvent_MovesCursorToStartOfLineOnMacOS_SelectsAllElsewhere()
  402. {
  403. const string text = "foo";
  404. m_TextEditor.text = text;
  405. m_TextEditor.MoveLineEnd();
  406. var controlAKeyDownEvent = new Event { type = EventType.KeyDown, keyCode = KeyCode.A, modifiers = EventModifiers.Control };
  407. m_TextEditor.HandleKeyEvent(controlAKeyDownEvent);
  408. if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX)
  409. {
  410. Assert.That(m_TextEditor.SelectedText, Is.Empty, "Selected text was not empty");
  411. Assert.That(m_TextEditor.cursorIndex, Is.EqualTo(0), "Cursor did not move to start of line");
  412. }
  413. else
  414. Assert.That(m_TextEditor.SelectedText, Is.EqualTo(text), "Text was not selected");
  415. }
  416. [Test]
  417. public void HandleKeyEvent_WithCommandAKeyDownEvent_SelectsAllOnMacOS()
  418. {
  419. if (SystemInfo.operatingSystemFamily != OperatingSystemFamily.MacOSX)
  420. Assert.Ignore("Test is only applicable on macOS");
  421. const string text = "foo";
  422. m_TextEditor.text = text;
  423. var commandAKeyDownEvent = new Event { type = EventType.KeyDown, keyCode = KeyCode.A, modifiers = EventModifiers.Command };
  424. m_TextEditor.HandleKeyEvent(commandAKeyDownEvent);
  425. Assert.That(m_TextEditor.SelectedText, Is.EqualTo(text), "Text was not selected");
  426. }
  427. }