No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextEditorTests.cs 25KB

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