TextViewTests.cs 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. using System.Text;
  2. namespace UnitTests_Parallelizable.ViewsTests;
  3. public class TextViewTests
  4. {
  5. [Fact]
  6. public void CloseFile_Throws_If_FilePath_Is_Null ()
  7. {
  8. var tv = new TextView ();
  9. Assert.Throws<ArgumentNullException> (() => tv.CloseFile ());
  10. }
  11. [Fact]
  12. public void ContentsChanged_Event_Fires_ClearHistoryChanges ()
  13. {
  14. var eventcount = 0;
  15. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  16. var tv = new TextView { Width = 50, Height = 10, Text = text };
  17. tv.ContentsChanged += (s, e) => { eventcount++; };
  18. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  19. Assert.Equal (
  20. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  21. tv.Text
  22. );
  23. Assert.Equal (4, tv.Lines);
  24. var expectedEventCount = 1; // for ENTER key
  25. Assert.Equal (expectedEventCount, eventcount);
  26. tv.ClearHistoryChanges ();
  27. expectedEventCount = 2;
  28. Assert.Equal (expectedEventCount, eventcount);
  29. }
  30. [Fact]
  31. public void ContentsChanged_Event_Fires_LoadStream_By_Calling_HistoryText_Clear ()
  32. {
  33. var eventcount = 0;
  34. var tv = new TextView { Width = 50, Height = 10 };
  35. tv.ContentsChanged += (s, e) => { eventcount++; };
  36. var text = "This is the first line.\r\nThis is the second line.\r\n";
  37. tv.Load (new MemoryStream (Encoding.ASCII.GetBytes (text)));
  38. Assert.Equal (
  39. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  40. tv.Text
  41. );
  42. Assert.Equal (1, eventcount);
  43. }
  44. [Fact]
  45. public void ContentsChanged_Event_Fires_On_LoadFile_By_Calling_HistoryText_Clear ()
  46. {
  47. var eventcount = 0;
  48. var tv = new TextView { Width = 50, Height = 10 };
  49. tv.BeginInit ();
  50. tv.EndInit ();
  51. tv.ContentsChanged += (s, e) => { eventcount++; };
  52. var fileName = "textview.txt";
  53. File.WriteAllText (fileName, "This is the first line.\r\nThis is the second line.\r\n");
  54. tv.Load (fileName);
  55. Assert.Equal (1, eventcount);
  56. Assert.Equal (
  57. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  58. tv.Text
  59. );
  60. }
  61. [Fact]
  62. public void GetRegion_StringFromRunes_Environment_NewLine ()
  63. {
  64. var tv = new TextView { Text = $"1{Environment.NewLine}2" };
  65. Assert.Equal ($"1{Environment.NewLine}2", tv.Text);
  66. Assert.Equal ("", tv.SelectedText);
  67. tv.SelectAll ();
  68. Assert.Equal ($"1{Environment.NewLine}2", tv.Text);
  69. Assert.Equal ($"1{Environment.NewLine}2", tv.SelectedText);
  70. }
  71. [Fact]
  72. public void HistoryText_ClearHistoryChanges ()
  73. {
  74. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  75. var tv = new TextView { Text = text };
  76. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  77. Assert.Equal (
  78. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  79. tv.Text
  80. );
  81. Assert.Equal (4, tv.Lines);
  82. Assert.Equal (new (0, 1), tv.CursorPosition);
  83. Assert.True (tv.IsDirty);
  84. Assert.True (tv.HasHistoryChanges);
  85. tv.ClearHistoryChanges ();
  86. Assert.Equal (
  87. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  88. tv.Text
  89. );
  90. Assert.Equal (4, tv.Lines);
  91. Assert.Equal (new (0, 1), tv.CursorPosition);
  92. Assert.False (tv.IsDirty);
  93. Assert.False (tv.HasHistoryChanges);
  94. }
  95. [Fact]
  96. public void HistoryText_Exceptions ()
  97. {
  98. var ht = new HistoryText ();
  99. foreach (object ls in Enum.GetValues (typeof (TextEditingLineStatus)))
  100. {
  101. if ((TextEditingLineStatus)ls != TextEditingLineStatus.Original)
  102. {
  103. Assert.Throws<ArgumentException> (
  104. () => ht.Add (
  105. new List<List<Cell>> (),
  106. Point.Empty,
  107. (TextEditingLineStatus)ls
  108. )
  109. );
  110. }
  111. }
  112. Assert.Null (Record.Exception (() => ht.Add (new () { new () }, Point.Empty)));
  113. }
  114. [Fact]
  115. public void HistoryText_IsDirty_HasHistoryChanges ()
  116. {
  117. var tv = new TextView ();
  118. Assert.Equal ("", tv.Text);
  119. Assert.Equal (1, tv.Lines);
  120. Assert.Equal (Point.Empty, tv.CursorPosition);
  121. Assert.False (tv.IsDirty);
  122. Assert.False (tv.HasHistoryChanges);
  123. Assert.True (tv.NewKeyDownEvent (Key.D1));
  124. Assert.Equal ("1", tv.Text);
  125. Assert.Equal (1, tv.Lines);
  126. Assert.Equal (new (1, 0), tv.CursorPosition);
  127. Assert.True (tv.IsDirty);
  128. Assert.True (tv.HasHistoryChanges);
  129. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  130. Assert.Equal ($"1{Environment.NewLine}", tv.Text);
  131. Assert.Equal (2, tv.Lines);
  132. Assert.Equal (new (0, 1), tv.CursorPosition);
  133. Assert.True (tv.IsDirty);
  134. Assert.True (tv.HasHistoryChanges);
  135. Assert.True (tv.NewKeyDownEvent (Key.D2));
  136. Assert.Equal ($"1{Environment.NewLine}2", tv.Text);
  137. Assert.Equal (2, tv.Lines);
  138. Assert.Equal (new (1, 1), tv.CursorPosition);
  139. Assert.True (tv.IsDirty);
  140. Assert.True (tv.HasHistoryChanges);
  141. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  142. Assert.Equal ($"1{Environment.NewLine}", tv.Text);
  143. Assert.Equal (2, tv.Lines);
  144. Assert.Equal (new (0, 1), tv.CursorPosition);
  145. Assert.True (tv.IsDirty);
  146. Assert.True (tv.HasHistoryChanges);
  147. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  148. Assert.Equal ("1", tv.Text);
  149. Assert.Equal (1, tv.Lines);
  150. Assert.Equal (new (1, 0), tv.CursorPosition);
  151. Assert.True (tv.IsDirty);
  152. Assert.True (tv.HasHistoryChanges);
  153. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  154. Assert.Equal ("", tv.Text);
  155. Assert.Equal (1, tv.Lines);
  156. Assert.Equal (Point.Empty, tv.CursorPosition);
  157. // IsDirty cannot be based on HasHistoryChanges because HasHistoryChanges is greater than 0
  158. // The only way is comparing from the original text
  159. Assert.False (tv.IsDirty);
  160. // Still true because HasHistoryChanges is greater than 0
  161. Assert.True (tv.HasHistoryChanges);
  162. }
  163. [Fact]
  164. public void HistoryText_Undo_Redo_Changing_On_Middle_Clear_History_Forwards ()
  165. {
  166. var tv = new TextView ();
  167. Assert.True (tv.NewKeyDownEvent (Key.D1));
  168. Assert.Equal ("1", tv.Text);
  169. Assert.Equal (1, tv.Lines);
  170. Assert.Equal (new (1, 0), tv.CursorPosition);
  171. Assert.True (tv.NewKeyDownEvent (Key.D2));
  172. Assert.Equal ("12", tv.Text);
  173. Assert.Equal (1, tv.Lines);
  174. Assert.Equal (new (2, 0), tv.CursorPosition);
  175. Assert.True (tv.NewKeyDownEvent (Key.D3));
  176. Assert.Equal ("123", tv.Text);
  177. Assert.Equal (1, tv.Lines);
  178. Assert.Equal (new (3, 0), tv.CursorPosition);
  179. // Undo
  180. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  181. Assert.Equal ("12", tv.Text);
  182. Assert.Equal (1, tv.Lines);
  183. Assert.Equal (new (2, 0), tv.CursorPosition);
  184. Assert.True (tv.NewKeyDownEvent (Key.D4));
  185. Assert.Equal ("124", tv.Text);
  186. Assert.Equal (1, tv.Lines);
  187. Assert.Equal (new (3, 0), tv.CursorPosition);
  188. // Redo
  189. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  190. Assert.Equal ("124", tv.Text);
  191. Assert.Equal (1, tv.Lines);
  192. Assert.Equal (new (3, 0), tv.CursorPosition);
  193. }
  194. [Fact]
  195. public void HistoryText_Undo_Redo_Disabled_On_WordWrap ()
  196. {
  197. var text = "This is the first line.\nThis is the second line.\nThis is the third line.\n";
  198. var tv = new TextView { Width = 80, Height = 5, Text = text };
  199. Assert.False (tv.WordWrap);
  200. tv.WordWrap = true;
  201. tv.SelectionStartColumn = 12;
  202. tv.CursorPosition = new (12, 2);
  203. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  204. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  205. Assert.Equal (3, tv.Lines);
  206. Assert.Equal (new (0, 1), tv.CursorPosition);
  207. Assert.True (tv.NewKeyDownEvent (Key.A));
  208. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  209. Assert.Equal (3, tv.Lines);
  210. Assert.Equal (new (1, 1), tv.CursorPosition);
  211. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  212. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  213. Assert.Equal (3, tv.Lines);
  214. Assert.Equal (new (0, 1), tv.CursorPosition);
  215. Assert.True (tv.IsDirty);
  216. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  217. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  218. Assert.Equal (3, tv.Lines);
  219. Assert.Equal (new (1, 1), tv.CursorPosition);
  220. }
  221. [Fact]
  222. public void HistoryText_Undo_Redo_Ending_With_Newline_Multi_Line_Selected_Almost_All_Return_And_InsertText ()
  223. {
  224. var text = "This is the first line.\nThis is the second line.\nThis is the third line.\n";
  225. var tv = new TextView { Text = text };
  226. tv.SelectionStartColumn = 12;
  227. tv.CursorPosition = new (12, 2);
  228. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  229. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  230. Assert.Equal (3, tv.Lines);
  231. Assert.Equal (new (0, 1), tv.CursorPosition);
  232. Assert.True (tv.NewKeyDownEvent (Key.A));
  233. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  234. Assert.Equal (3, tv.Lines);
  235. Assert.Equal (new (1, 1), tv.CursorPosition);
  236. // Undo
  237. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  238. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  239. Assert.Equal (3, tv.Lines);
  240. Assert.Equal (new (0, 1), tv.CursorPosition);
  241. Assert.True (tv.IsDirty);
  242. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  243. Assert.Equal (
  244. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.{Environment.NewLine}",
  245. tv.Text
  246. );
  247. Assert.Equal (4, tv.Lines);
  248. Assert.Equal (new (12, 2), tv.CursorPosition);
  249. Assert.False (tv.IsDirty);
  250. // Redo
  251. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  252. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  253. Assert.Equal (3, tv.Lines);
  254. Assert.Equal (new (0, 1), tv.CursorPosition);
  255. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  256. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  257. Assert.Equal (3, tv.Lines);
  258. Assert.Equal (new (1, 1), tv.CursorPosition);
  259. // Undo
  260. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  261. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  262. Assert.Equal (3, tv.Lines);
  263. Assert.Equal (new (0, 1), tv.CursorPosition);
  264. Assert.True (tv.IsDirty);
  265. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  266. Assert.Equal (
  267. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.{Environment.NewLine}",
  268. tv.Text
  269. );
  270. Assert.Equal (4, tv.Lines);
  271. Assert.Equal (new (12, 2), tv.CursorPosition);
  272. Assert.False (tv.IsDirty);
  273. // Redo
  274. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  275. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  276. Assert.Equal (3, tv.Lines);
  277. Assert.Equal (new (0, 1), tv.CursorPosition);
  278. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  279. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  280. Assert.Equal (3, tv.Lines);
  281. Assert.Equal (new (1, 1), tv.CursorPosition);
  282. }
  283. [Fact]
  284. public void HistoryText_Undo_Redo_First_Line_Selected_Return_And_InsertText ()
  285. {
  286. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  287. var tv = new TextView { Text = text };
  288. tv.SelectionStartColumn = 12;
  289. tv.CursorPosition = new (17, 0);
  290. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  291. Assert.Equal (
  292. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  293. tv.Text
  294. );
  295. Assert.Equal (4, tv.Lines);
  296. Assert.Equal (new (0, 1), tv.CursorPosition);
  297. Assert.True (tv.NewKeyDownEvent (Key.A));
  298. Assert.Equal (
  299. $"This is the {Environment.NewLine}a line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  300. tv.Text
  301. );
  302. Assert.Equal (4, tv.Lines);
  303. Assert.Equal (new (1, 1), tv.CursorPosition);
  304. // Undo
  305. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  306. Assert.Equal (
  307. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  308. tv.Text
  309. );
  310. Assert.Equal (4, tv.Lines);
  311. Assert.Equal (new (0, 1), tv.CursorPosition);
  312. Assert.True (tv.IsDirty);
  313. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  314. Assert.Equal (
  315. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  316. tv.Text
  317. );
  318. Assert.Equal (3, tv.Lines);
  319. Assert.Equal (new (17, 0), tv.CursorPosition);
  320. Assert.False (tv.IsDirty);
  321. // Redo
  322. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  323. Assert.Equal (
  324. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  325. tv.Text
  326. );
  327. Assert.Equal (4, tv.Lines);
  328. Assert.Equal (new (0, 1), tv.CursorPosition);
  329. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  330. Assert.Equal (
  331. $"This is the {Environment.NewLine}a line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  332. tv.Text
  333. );
  334. Assert.Equal (4, tv.Lines);
  335. Assert.Equal (new (1, 1), tv.CursorPosition);
  336. // Undo
  337. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  338. Assert.Equal (
  339. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  340. tv.Text
  341. );
  342. Assert.Equal (4, tv.Lines);
  343. Assert.Equal (new (0, 1), tv.CursorPosition);
  344. Assert.True (tv.IsDirty);
  345. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  346. Assert.Equal (
  347. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  348. tv.Text
  349. );
  350. Assert.Equal (3, tv.Lines);
  351. Assert.Equal (new (17, 0), tv.CursorPosition);
  352. Assert.False (tv.IsDirty);
  353. // Redo
  354. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  355. Assert.Equal (
  356. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  357. tv.Text
  358. );
  359. Assert.Equal (4, tv.Lines);
  360. Assert.Equal (new (0, 1), tv.CursorPosition);
  361. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  362. Assert.Equal (
  363. $"This is the {Environment.NewLine}a line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  364. tv.Text
  365. );
  366. Assert.Equal (4, tv.Lines);
  367. Assert.Equal (new (1, 1), tv.CursorPosition);
  368. }
  369. [Fact]
  370. public void HistoryText_Undo_Redo_KillWordBackward ()
  371. {
  372. var text = "First line.\nSecond line.";
  373. var tv = new TextView { Text = text };
  374. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl));
  375. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  376. Assert.Equal ("", tv.SelectedText);
  377. Assert.Equal (2, tv.Lines);
  378. Assert.Equal (new (12, 1), tv.CursorPosition);
  379. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  380. Assert.Equal ($"First line.{Environment.NewLine}Second line", tv.Text);
  381. Assert.Equal ("", tv.SelectedText);
  382. Assert.Equal (2, tv.Lines);
  383. Assert.Equal (new (11, 1), tv.CursorPosition);
  384. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  385. Assert.Equal ($"First line.{Environment.NewLine}Second ", tv.Text);
  386. Assert.Equal ("", tv.SelectedText);
  387. Assert.Equal (2, tv.Lines);
  388. Assert.Equal (new (7, 1), tv.CursorPosition);
  389. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  390. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  391. Assert.Equal ("", tv.SelectedText);
  392. Assert.Equal (2, tv.Lines);
  393. Assert.Equal (new (0, 1), tv.CursorPosition);
  394. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  395. Assert.Equal ("First line.", tv.Text);
  396. Assert.Equal ("", tv.SelectedText);
  397. Assert.Equal (1, tv.Lines);
  398. Assert.Equal (new (11, 0), tv.CursorPosition);
  399. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  400. Assert.Equal ("First line", tv.Text);
  401. Assert.Equal ("", tv.SelectedText);
  402. Assert.Equal (1, tv.Lines);
  403. Assert.Equal (new (10, 0), tv.CursorPosition);
  404. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  405. Assert.Equal ("First ", tv.Text);
  406. Assert.Equal ("", tv.SelectedText);
  407. Assert.Equal (1, tv.Lines);
  408. Assert.Equal (new (6, 0), tv.CursorPosition);
  409. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  410. Assert.Equal ("", tv.Text);
  411. Assert.Equal ("", tv.SelectedText);
  412. Assert.Equal (1, tv.Lines);
  413. Assert.Equal (Point.Empty, tv.CursorPosition);
  414. // Undo
  415. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  416. Assert.Equal ("First ", tv.Text);
  417. Assert.Equal (1, tv.Lines);
  418. Assert.Equal (new (6, 0), tv.CursorPosition);
  419. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  420. Assert.Equal ("First line", tv.Text);
  421. Assert.Equal (1, tv.Lines);
  422. Assert.Equal (new (10, 0), tv.CursorPosition);
  423. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  424. Assert.Equal ("First line.", tv.Text);
  425. Assert.Equal (1, tv.Lines);
  426. Assert.Equal (new (11, 0), tv.CursorPosition);
  427. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  428. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  429. Assert.Equal (2, tv.Lines);
  430. Assert.Equal (new (0, 1), tv.CursorPosition);
  431. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  432. Assert.Equal ($"First line.{Environment.NewLine}Second ", tv.Text);
  433. Assert.Equal (2, tv.Lines);
  434. Assert.Equal (new (7, 1), tv.CursorPosition);
  435. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  436. Assert.Equal ($"First line.{Environment.NewLine}Second line", tv.Text);
  437. Assert.Equal (2, tv.Lines);
  438. Assert.Equal (new (11, 1), tv.CursorPosition);
  439. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  440. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  441. Assert.Equal (2, tv.Lines);
  442. Assert.Equal (new (12, 1), tv.CursorPosition);
  443. // Redo
  444. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  445. Assert.Equal ($"First line.{Environment.NewLine}Second line", tv.Text);
  446. Assert.Equal (2, tv.Lines);
  447. Assert.Equal (new (11, 1), tv.CursorPosition);
  448. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  449. Assert.Equal ($"First line.{Environment.NewLine}Second ", tv.Text);
  450. Assert.Equal (2, tv.Lines);
  451. Assert.Equal (new (7, 1), tv.CursorPosition);
  452. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  453. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  454. Assert.Equal (2, tv.Lines);
  455. Assert.Equal (new (0, 1), tv.CursorPosition);
  456. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  457. Assert.Equal ("First line.", tv.Text);
  458. Assert.Equal (1, tv.Lines);
  459. Assert.Equal (new (11, 0), tv.CursorPosition);
  460. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  461. Assert.Equal ("First line", tv.Text);
  462. Assert.Equal (1, tv.Lines);
  463. Assert.Equal (new (10, 0), tv.CursorPosition);
  464. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  465. Assert.Equal ("First ", tv.Text);
  466. Assert.Equal (1, tv.Lines);
  467. Assert.Equal (new (6, 0), tv.CursorPosition);
  468. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  469. Assert.Equal ("", tv.Text);
  470. Assert.Equal (1, tv.Lines);
  471. Assert.Equal (Point.Empty, tv.CursorPosition);
  472. }
  473. [Fact]
  474. public void HistoryText_Undo_Redo_KillWordForward ()
  475. {
  476. var text = "First line.\nSecond line.";
  477. var tv = new TextView { Text = text };
  478. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  479. Assert.Equal ($"line.{Environment.NewLine}Second line.", tv.Text);
  480. Assert.Equal ("", tv.SelectedText);
  481. Assert.Equal (2, tv.Lines);
  482. Assert.Equal (Point.Empty, tv.CursorPosition);
  483. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  484. Assert.Equal ($".{Environment.NewLine}Second line.", tv.Text);
  485. Assert.Equal (2, tv.Lines);
  486. Assert.Equal (Point.Empty, tv.CursorPosition);
  487. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  488. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  489. Assert.Equal (2, tv.Lines);
  490. Assert.Equal (Point.Empty, tv.CursorPosition);
  491. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  492. Assert.Equal ("Second line.", tv.Text);
  493. Assert.Equal (1, tv.Lines);
  494. Assert.Equal (Point.Empty, tv.CursorPosition);
  495. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  496. Assert.Equal ("line.", tv.Text);
  497. Assert.Equal (1, tv.Lines);
  498. Assert.Equal (Point.Empty, tv.CursorPosition);
  499. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  500. Assert.Equal (".", tv.Text);
  501. Assert.Equal (1, tv.Lines);
  502. Assert.Equal (Point.Empty, tv.CursorPosition);
  503. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  504. Assert.Equal ("", tv.Text);
  505. Assert.Equal (1, tv.Lines);
  506. Assert.Equal (Point.Empty, tv.CursorPosition);
  507. // Undo
  508. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  509. Assert.Equal (".", tv.Text);
  510. Assert.Equal (1, tv.Lines);
  511. Assert.Equal (Point.Empty, tv.CursorPosition);
  512. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  513. Assert.Equal ("line.", tv.Text);
  514. Assert.Equal (1, tv.Lines);
  515. Assert.Equal (Point.Empty, tv.CursorPosition);
  516. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  517. Assert.Equal ("Second line.", tv.Text);
  518. Assert.Equal (1, tv.Lines);
  519. Assert.Equal (Point.Empty, tv.CursorPosition);
  520. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  521. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  522. Assert.Equal (2, tv.Lines);
  523. Assert.Equal (Point.Empty, tv.CursorPosition);
  524. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  525. Assert.Equal ($".{Environment.NewLine}Second line.", tv.Text);
  526. Assert.Equal (2, tv.Lines);
  527. Assert.Equal (Point.Empty, tv.CursorPosition);
  528. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  529. Assert.Equal ($"line.{Environment.NewLine}Second line.", tv.Text);
  530. Assert.Equal (2, tv.Lines);
  531. Assert.Equal (Point.Empty, tv.CursorPosition);
  532. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  533. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  534. Assert.Equal (2, tv.Lines);
  535. Assert.Equal (Point.Empty, tv.CursorPosition);
  536. // Redo
  537. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  538. Assert.Equal ($"line.{Environment.NewLine}Second line.", tv.Text);
  539. Assert.Equal (2, tv.Lines);
  540. Assert.Equal (Point.Empty, tv.CursorPosition);
  541. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  542. Assert.Equal ($".{Environment.NewLine}Second line.", tv.Text);
  543. Assert.Equal (2, tv.Lines);
  544. Assert.Equal (Point.Empty, tv.CursorPosition);
  545. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  546. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  547. Assert.Equal (2, tv.Lines);
  548. Assert.Equal (Point.Empty, tv.CursorPosition);
  549. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  550. Assert.Equal ("Second line.", tv.Text);
  551. Assert.Equal (1, tv.Lines);
  552. Assert.Equal (Point.Empty, tv.CursorPosition);
  553. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  554. Assert.Equal ("line.", tv.Text);
  555. Assert.Equal (1, tv.Lines);
  556. Assert.Equal (Point.Empty, tv.CursorPosition);
  557. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  558. Assert.Equal (".", tv.Text);
  559. Assert.Equal (1, tv.Lines);
  560. Assert.Equal (Point.Empty, tv.CursorPosition);
  561. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  562. Assert.Equal ("", tv.Text);
  563. Assert.Equal (1, tv.Lines);
  564. Assert.Equal (Point.Empty, tv.CursorPosition);
  565. }
  566. [Fact]
  567. public void HistoryText_Undo_Redo_Multi_Line_Selected_All_Return_And_InsertText ()
  568. {
  569. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  570. var tv = new TextView { Text = text };
  571. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl.WithShift));
  572. Assert.Equal (
  573. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  574. tv.Text
  575. );
  576. Assert.Equal (3, tv.Lines);
  577. Assert.Equal (new (23, 2), tv.CursorPosition);
  578. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  579. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  580. Assert.Equal (2, tv.Lines);
  581. Assert.Equal (new (0, 1), tv.CursorPosition);
  582. Assert.True (tv.NewKeyDownEvent (Key.A));
  583. Assert.Equal ($"{Environment.NewLine}a", tv.Text);
  584. Assert.Equal (2, tv.Lines);
  585. Assert.Equal (new (1, 1), tv.CursorPosition);
  586. // Undo
  587. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  588. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  589. Assert.Equal (2, tv.Lines);
  590. Assert.Equal (new (0, 1), tv.CursorPosition);
  591. Assert.True (tv.IsDirty);
  592. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  593. Assert.Equal (
  594. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  595. tv.Text
  596. );
  597. Assert.Equal (3, tv.Lines);
  598. Assert.Equal (new (23, 2), tv.CursorPosition);
  599. Assert.False (tv.IsDirty);
  600. // Redo
  601. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  602. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  603. Assert.Equal (2, tv.Lines);
  604. Assert.Equal (new (0, 1), tv.CursorPosition);
  605. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  606. Assert.Equal ($"{Environment.NewLine}a", tv.Text);
  607. Assert.Equal (2, tv.Lines);
  608. Assert.Equal (new (1, 1), tv.CursorPosition);
  609. // Undo
  610. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  611. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  612. Assert.Equal (2, tv.Lines);
  613. Assert.Equal (new (0, 1), tv.CursorPosition);
  614. Assert.True (tv.IsDirty);
  615. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  616. Assert.Equal (
  617. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  618. tv.Text
  619. );
  620. Assert.Equal (3, tv.Lines);
  621. Assert.Equal (new (23, 2), tv.CursorPosition);
  622. Assert.False (tv.IsDirty);
  623. // Redo
  624. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  625. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  626. Assert.Equal (2, tv.Lines);
  627. Assert.Equal (new (0, 1), tv.CursorPosition);
  628. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  629. Assert.Equal ($"{Environment.NewLine}a", tv.Text);
  630. Assert.Equal (2, tv.Lines);
  631. Assert.Equal (new (1, 1), tv.CursorPosition);
  632. }
  633. [Fact]
  634. public void HistoryText_Undo_Redo_Multi_Line_Selected_DeleteCharLeft_All ()
  635. {
  636. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  637. var tv = new TextView { Text = text };
  638. Assert.Equal (
  639. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  640. tv.Text
  641. );
  642. Assert.Equal (3, tv.Lines);
  643. Assert.Equal (Point.Empty, tv.CursorPosition);
  644. Assert.False (tv.IsDirty);
  645. Assert.False (tv.HasHistoryChanges);
  646. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl.WithShift));
  647. Assert.Equal (
  648. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  649. tv.Text
  650. );
  651. Assert.Equal (
  652. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  653. tv.SelectedText
  654. );
  655. Assert.Equal (3, tv.Lines);
  656. Assert.Equal (new (23, 2), tv.CursorPosition);
  657. Assert.Equal (70 + Environment.NewLine.Length * 2, tv.SelectedLength);
  658. Assert.False (tv.IsDirty);
  659. Assert.False (tv.HasHistoryChanges);
  660. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  661. Assert.Equal ("", tv.Text);
  662. Assert.Equal ("", tv.SelectedText);
  663. Assert.Equal (1, tv.Lines);
  664. Assert.Equal (Point.Empty, tv.CursorPosition);
  665. Assert.Equal (0, tv.SelectedLength);
  666. Assert.True (tv.IsDirty);
  667. Assert.True (tv.HasHistoryChanges);
  668. // Undo
  669. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  670. Assert.Equal (
  671. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  672. tv.Text
  673. );
  674. Assert.Equal ("", tv.SelectedText);
  675. Assert.Equal (3, tv.Lines);
  676. Assert.Equal (new (23, 2), tv.CursorPosition);
  677. Assert.Equal (0, tv.SelectedLength);
  678. Assert.False (tv.IsDirty);
  679. Assert.True (tv.HasHistoryChanges);
  680. // Redo
  681. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  682. Assert.Equal ("", tv.Text);
  683. Assert.Equal ("", tv.SelectedText);
  684. Assert.Equal (1, tv.Lines);
  685. Assert.Equal (Point.Empty, tv.CursorPosition);
  686. Assert.Equal (0, tv.SelectedLength);
  687. Assert.True (tv.IsDirty);
  688. Assert.True (tv.HasHistoryChanges);
  689. }
  690. [Fact]
  691. public void HistoryText_Undo_Redo_Multi_Line_Selected_DeleteCharRight_All ()
  692. {
  693. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  694. var tv = new TextView { Text = text };
  695. Assert.Equal (
  696. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  697. tv.Text
  698. );
  699. Assert.Equal (3, tv.Lines);
  700. Assert.Equal (Point.Empty, tv.CursorPosition);
  701. Assert.False (tv.IsDirty);
  702. Assert.False (tv.HasHistoryChanges);
  703. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl.WithShift));
  704. Assert.Equal (
  705. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  706. tv.Text
  707. );
  708. Assert.Equal (
  709. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  710. tv.SelectedText
  711. );
  712. Assert.Equal (3, tv.Lines);
  713. Assert.Equal (new (23, 2), tv.CursorPosition);
  714. Assert.Equal (70 + Environment.NewLine.Length * 2, tv.SelectedLength);
  715. Assert.False (tv.IsDirty);
  716. Assert.False (tv.HasHistoryChanges);
  717. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  718. Assert.Equal ("", tv.Text);
  719. Assert.Equal ("", tv.SelectedText);
  720. Assert.Equal (1, tv.Lines);
  721. Assert.Equal (Point.Empty, tv.CursorPosition);
  722. Assert.Equal (0, tv.SelectedLength);
  723. Assert.True (tv.IsDirty);
  724. Assert.True (tv.HasHistoryChanges);
  725. // Undo
  726. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  727. Assert.Equal (
  728. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  729. tv.Text
  730. );
  731. Assert.Equal ("", tv.SelectedText);
  732. Assert.Equal (3, tv.Lines);
  733. Assert.Equal (new (23, 2), tv.CursorPosition);
  734. Assert.Equal (0, tv.SelectedLength);
  735. Assert.False (tv.IsDirty);
  736. Assert.True (tv.HasHistoryChanges);
  737. // Redo
  738. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  739. Assert.Equal ("", tv.Text);
  740. Assert.Equal ("", tv.SelectedText);
  741. Assert.Equal (1, tv.Lines);
  742. Assert.Equal (Point.Empty, tv.CursorPosition);
  743. Assert.Equal (0, tv.SelectedLength);
  744. Assert.True (tv.IsDirty);
  745. Assert.True (tv.HasHistoryChanges);
  746. }
  747. [Fact]
  748. public void HistoryText_Undo_Redo_Multiline_Selected_Tab_BackTab ()
  749. {
  750. var text = "First line.\nSecond line.\nThird line.";
  751. var tv = new TextView { Width = 80, Height = 5, Text = text };
  752. tv.SelectionStartColumn = 6;
  753. tv.CursorPosition = new (6, 2);
  754. Assert.True (tv.NewKeyDownEvent (Key.Tab));
  755. Assert.Equal ("First \tline.", tv.Text);
  756. Assert.Equal (1, tv.Lines);
  757. Assert.Equal (new (7, 0), tv.CursorPosition);
  758. Assert.True (tv.NewKeyDownEvent (Key.Tab.WithShift));
  759. Assert.Equal ("First line.", tv.Text);
  760. Assert.Equal (1, tv.Lines);
  761. Assert.Equal (new (6, 0), tv.CursorPosition);
  762. // Undo
  763. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  764. Assert.Equal ("First \tline.", tv.Text);
  765. Assert.Equal (1, tv.Lines);
  766. Assert.Equal (new (7, 0), tv.CursorPosition);
  767. Assert.True (tv.IsDirty);
  768. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  769. Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text);
  770. Assert.Equal (3, tv.Lines);
  771. Assert.Equal (new (6, 2), tv.CursorPosition);
  772. Assert.False (tv.IsDirty);
  773. // Redo
  774. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  775. Assert.Equal ("First \tline.", tv.Text);
  776. Assert.Equal (1, tv.Lines);
  777. Assert.Equal (new (7, 0), tv.CursorPosition);
  778. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  779. Assert.Equal ("First line.", tv.Text);
  780. Assert.Equal (1, tv.Lines);
  781. Assert.Equal (new (6, 0), tv.CursorPosition);
  782. }
  783. [Fact]
  784. public void HistoryText_Undo_Redo_Multiline_Simples_Tab_BackTab ()
  785. {
  786. var text = "First line.\nSecond line.\nThird line.";
  787. var tv = new TextView { Width = 80, Height = 5, Text = text };
  788. Assert.True (tv.NewKeyDownEvent (Key.Tab));
  789. Assert.Equal (
  790. $"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.",
  791. tv.Text
  792. );
  793. Assert.Equal (3, tv.Lines);
  794. Assert.Equal (new (1, 0), tv.CursorPosition);
  795. Assert.True (tv.NewKeyDownEvent (Key.Tab.WithShift));
  796. Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text);
  797. Assert.Equal (3, tv.Lines);
  798. Assert.Equal (Point.Empty, tv.CursorPosition);
  799. // Undo
  800. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  801. Assert.Equal (
  802. $"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.",
  803. tv.Text
  804. );
  805. Assert.Equal (3, tv.Lines);
  806. Assert.Equal (new (1, 0), tv.CursorPosition);
  807. Assert.True (tv.IsDirty);
  808. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  809. Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text);
  810. Assert.Equal (3, tv.Lines);
  811. Assert.Equal (Point.Empty, tv.CursorPosition);
  812. Assert.False (tv.IsDirty);
  813. // Redo
  814. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  815. Assert.Equal (
  816. $"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.",
  817. tv.Text
  818. );
  819. Assert.Equal (3, tv.Lines);
  820. Assert.Equal (new (1, 0), tv.CursorPosition);
  821. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  822. Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text);
  823. Assert.Equal (3, tv.Lines);
  824. Assert.Equal (Point.Empty, tv.CursorPosition);
  825. }
  826. [Fact]
  827. public void HistoryText_Undo_Redo_Single_Line_Selected_Return ()
  828. {
  829. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  830. var tv = new TextView { Text = text };
  831. tv.SelectionStartColumn = 12;
  832. tv.CursorPosition = new (17, 0);
  833. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  834. Assert.Equal (
  835. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  836. tv.Text
  837. );
  838. Assert.Equal (4, tv.Lines);
  839. Assert.Equal (new (0, 1), tv.CursorPosition);
  840. // Undo
  841. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  842. Assert.Equal (
  843. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  844. tv.Text
  845. );
  846. Assert.Equal (3, tv.Lines);
  847. Assert.Equal (new (17, 0), tv.CursorPosition);
  848. Assert.False (tv.IsDirty);
  849. // Redo
  850. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  851. Assert.Equal (
  852. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  853. tv.Text
  854. );
  855. Assert.Equal (4, tv.Lines);
  856. Assert.Equal (new (0, 1), tv.CursorPosition);
  857. // Undo
  858. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  859. Assert.Equal (
  860. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  861. tv.Text
  862. );
  863. Assert.Equal (3, tv.Lines);
  864. Assert.Equal (new (17, 0), tv.CursorPosition);
  865. Assert.False (tv.IsDirty);
  866. // Redo
  867. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  868. Assert.Equal (
  869. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  870. tv.Text
  871. );
  872. Assert.Equal (4, tv.Lines);
  873. Assert.Equal (new (0, 1), tv.CursorPosition);
  874. }
  875. [Fact]
  876. public void HistoryText_Undo_Redo_Single_Second_Line_Selected_Return ()
  877. {
  878. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  879. var tv = new TextView { Text = text };
  880. tv.SelectionStartColumn = 12;
  881. tv.SelectionStartRow = 1;
  882. tv.CursorPosition = new (18, 1);
  883. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  884. Assert.Equal (
  885. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  886. tv.Text
  887. );
  888. Assert.Equal (4, tv.Lines);
  889. Assert.Equal (new (0, 2), tv.CursorPosition);
  890. // Undo
  891. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  892. Assert.Equal (
  893. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  894. tv.Text
  895. );
  896. Assert.Equal (3, tv.Lines);
  897. Assert.Equal (new (18, 1), tv.CursorPosition);
  898. Assert.False (tv.IsDirty);
  899. // Redo
  900. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  901. Assert.Equal (
  902. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  903. tv.Text
  904. );
  905. Assert.Equal (4, tv.Lines);
  906. Assert.Equal (new (0, 2), tv.CursorPosition);
  907. // Undo
  908. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  909. Assert.Equal (
  910. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  911. tv.Text
  912. );
  913. Assert.Equal (3, tv.Lines);
  914. Assert.Equal (new (18, 1), tv.CursorPosition);
  915. Assert.False (tv.IsDirty);
  916. // Redo
  917. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  918. Assert.Equal (
  919. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  920. tv.Text
  921. );
  922. Assert.Equal (4, tv.Lines);
  923. Assert.Equal (new (0, 2), tv.CursorPosition);
  924. }
  925. [Fact]
  926. public void HistoryText_Undo_Redo_Single_Second_Line_Selected_Return_And_InsertText ()
  927. {
  928. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  929. var tv = new TextView { Text = text };
  930. tv.SelectionStartColumn = 12;
  931. tv.SelectionStartRow = 1;
  932. tv.CursorPosition = new (18, 1);
  933. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  934. Assert.Equal (
  935. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  936. tv.Text
  937. );
  938. Assert.Equal (4, tv.Lines);
  939. Assert.Equal (new (0, 2), tv.CursorPosition);
  940. Assert.True (tv.NewKeyDownEvent (Key.A));
  941. Assert.Equal (
  942. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine}a line.{Environment.NewLine}This is the third line.",
  943. tv.Text
  944. );
  945. Assert.Equal (4, tv.Lines);
  946. Assert.Equal (new (1, 2), tv.CursorPosition);
  947. // Undo
  948. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  949. Assert.Equal (
  950. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  951. tv.Text
  952. );
  953. Assert.Equal (4, tv.Lines);
  954. Assert.Equal (new (0, 2), tv.CursorPosition);
  955. Assert.True (tv.IsDirty);
  956. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  957. Assert.Equal (
  958. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  959. tv.Text
  960. );
  961. Assert.Equal (3, tv.Lines);
  962. Assert.Equal (new (18, 1), tv.CursorPosition);
  963. Assert.False (tv.IsDirty);
  964. // Redo
  965. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  966. Assert.Equal (
  967. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  968. tv.Text
  969. );
  970. Assert.Equal (4, tv.Lines);
  971. Assert.Equal (new (0, 2), tv.CursorPosition);
  972. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  973. Assert.Equal (
  974. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine}a line.{Environment.NewLine}This is the third line.",
  975. tv.Text
  976. );
  977. Assert.Equal (4, tv.Lines);
  978. Assert.Equal (new (1, 2), tv.CursorPosition);
  979. // Undo
  980. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  981. Assert.Equal (
  982. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  983. tv.Text
  984. );
  985. Assert.Equal (4, tv.Lines);
  986. Assert.Equal (new (0, 2), tv.CursorPosition);
  987. Assert.True (tv.IsDirty);
  988. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  989. Assert.Equal (
  990. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  991. tv.Text
  992. );
  993. Assert.Equal (3, tv.Lines);
  994. Assert.Equal (new (18, 1), tv.CursorPosition);
  995. Assert.False (tv.IsDirty);
  996. // Redo
  997. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  998. Assert.Equal (
  999. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  1000. tv.Text
  1001. );
  1002. Assert.Equal (4, tv.Lines);
  1003. Assert.Equal (new (0, 2), tv.CursorPosition);
  1004. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1005. Assert.Equal (
  1006. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine}a line.{Environment.NewLine}This is the third line.",
  1007. tv.Text
  1008. );
  1009. Assert.Equal (4, tv.Lines);
  1010. Assert.Equal (new (1, 2), tv.CursorPosition);
  1011. }
  1012. [Fact]
  1013. public void HistoryText_Undo_Redo_Three_Line_Selected_Return ()
  1014. {
  1015. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1016. var tv = new TextView { Text = text };
  1017. tv.SelectionStartColumn = 12;
  1018. tv.CursorPosition = new (17, 2);
  1019. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  1020. Assert.Equal ($"This is the {Environment.NewLine} line.", tv.Text);
  1021. Assert.Equal (2, tv.Lines);
  1022. Assert.Equal (new (0, 1), tv.CursorPosition);
  1023. // Undo
  1024. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1025. Assert.Equal (
  1026. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1027. tv.Text
  1028. );
  1029. Assert.Equal (3, tv.Lines);
  1030. Assert.Equal (new (17, 2), tv.CursorPosition);
  1031. Assert.False (tv.IsDirty);
  1032. // Redo
  1033. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1034. Assert.Equal ($"This is the {Environment.NewLine} line.", tv.Text);
  1035. Assert.Equal (2, tv.Lines);
  1036. Assert.Equal (new (0, 1), tv.CursorPosition);
  1037. // Undo
  1038. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1039. Assert.Equal (
  1040. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1041. tv.Text
  1042. );
  1043. Assert.Equal (3, tv.Lines);
  1044. Assert.Equal (new (17, 2), tv.CursorPosition);
  1045. Assert.False (tv.IsDirty);
  1046. // Redo
  1047. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1048. Assert.Equal ($"This is the {Environment.NewLine} line.", tv.Text);
  1049. Assert.Equal (2, tv.Lines);
  1050. Assert.Equal (new (0, 1), tv.CursorPosition);
  1051. }
  1052. [Fact]
  1053. public void HistoryText_Undo_Redo_Two_Line_Selected_Return ()
  1054. {
  1055. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1056. var tv = new TextView { Text = text };
  1057. tv.SelectionStartColumn = 12;
  1058. tv.CursorPosition = new (18, 1);
  1059. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  1060. Assert.Equal (
  1061. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  1062. tv.Text
  1063. );
  1064. Assert.Equal (3, tv.Lines);
  1065. Assert.Equal (new (0, 1), tv.CursorPosition);
  1066. // Undo
  1067. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1068. Assert.Equal (
  1069. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1070. tv.Text
  1071. );
  1072. Assert.Equal (3, tv.Lines);
  1073. Assert.Equal (new (18, 1), tv.CursorPosition);
  1074. Assert.False (tv.IsDirty);
  1075. // Redo
  1076. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1077. Assert.Equal (
  1078. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  1079. tv.Text
  1080. );
  1081. Assert.Equal (3, tv.Lines);
  1082. Assert.Equal (new (0, 1), tv.CursorPosition);
  1083. // Undo
  1084. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1085. Assert.Equal (
  1086. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1087. tv.Text
  1088. );
  1089. Assert.Equal (3, tv.Lines);
  1090. Assert.Equal (new (18, 1), tv.CursorPosition);
  1091. Assert.False (tv.IsDirty);
  1092. // Redo
  1093. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1094. Assert.Equal (
  1095. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  1096. tv.Text
  1097. );
  1098. Assert.Equal (3, tv.Lines);
  1099. Assert.Equal (new (0, 1), tv.CursorPosition);
  1100. }
  1101. [Fact]
  1102. public void HistoryText_Undo_Redo_ApplyCellsAttribute ()
  1103. {
  1104. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1105. var tv = new TextView { Text = text };
  1106. tv.SelectionStartColumn = 12;
  1107. tv.CursorPosition = new (18, 1);
  1108. if (Environment.NewLine.Length == 2)
  1109. {
  1110. Assert.Equal (31, tv.SelectedLength);
  1111. }
  1112. else
  1113. {
  1114. Assert.Equal (30, tv.SelectedLength);
  1115. }
  1116. Assert.Equal ($"first line.{Environment.NewLine}This is the second", tv.SelectedText);
  1117. Assert.Equal ($"first line.{Environment.NewLine}This is the second", Cell.ToString (tv.SelectedCellsList));
  1118. Assert.Equal (new (18, 1), tv.CursorPosition);
  1119. Assert.False (tv.IsDirty);
  1120. AssertNullAttribute ();
  1121. tv.ApplyCellsAttribute (new (Color.Red, Color.Green));
  1122. AssertRedGreenAttribute ();
  1123. Assert.Equal (0, tv.SelectedLength);
  1124. Assert.Equal ("", tv.SelectedText);
  1125. Assert.Equal ($"first line.{Environment.NewLine}This is the second", Cell.ToString (tv.SelectedCellsList));
  1126. Assert.Equal (new (18, 1), tv.CursorPosition);
  1127. Assert.True (tv.IsDirty);
  1128. // Undo
  1129. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1130. AssertNullAttribute ();
  1131. Assert.Equal (12, tv.SelectionStartColumn);
  1132. Assert.Equal (0, tv.SelectionStartRow);
  1133. Assert.Equal (0, tv.SelectedLength);
  1134. Assert.Equal ("", tv.SelectedText);
  1135. Assert.Empty (tv.SelectedCellsList);
  1136. Assert.Equal (new (12, 0), tv.CursorPosition);
  1137. Assert.False (tv.IsDirty);
  1138. // Redo
  1139. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1140. AssertRedGreenAttribute ();
  1141. Assert.Equal (12, tv.SelectionStartColumn);
  1142. Assert.Equal (0, tv.SelectionStartRow);
  1143. Assert.Equal (0, tv.SelectedLength);
  1144. Assert.Equal ("", tv.SelectedText);
  1145. Assert.Empty (tv.SelectedCellsList);
  1146. Assert.Equal (new (12, 0), tv.CursorPosition);
  1147. Assert.True (tv.IsDirty);
  1148. void AssertNullAttribute ()
  1149. {
  1150. tv.GetRegion (out List<List<Cell>> region, 0, 12, 1, 18);
  1151. foreach (List<Cell> cells in region)
  1152. {
  1153. foreach (Cell cell in cells)
  1154. {
  1155. Assert.Null (cell.Attribute);
  1156. }
  1157. }
  1158. }
  1159. void AssertRedGreenAttribute ()
  1160. {
  1161. tv.GetRegion (out List<List<Cell>> region, 0, 12, 1, 18);
  1162. foreach (List<Cell> cells in region)
  1163. {
  1164. foreach (Cell cell in cells)
  1165. {
  1166. Assert.Equal ("[Red,Green,None]", cell.Attribute.ToString ());
  1167. }
  1168. }
  1169. }
  1170. }
  1171. [Fact]
  1172. public void Internal_Tests ()
  1173. {
  1174. var txt = "This is a text.";
  1175. List<Cell> txtStrings = Cell.StringToCells (txt);
  1176. Assert.Equal (txt.Length, txtStrings.Count);
  1177. Assert.Equal ("T", txtStrings [0].Grapheme);
  1178. Assert.Equal ("h", txtStrings [1].Grapheme);
  1179. Assert.Equal ("i", txtStrings [2].Grapheme);
  1180. Assert.Equal ("s", txtStrings [3].Grapheme);
  1181. Assert.Equal (" ", txtStrings [4].Grapheme);
  1182. Assert.Equal ("i", txtStrings [5].Grapheme);
  1183. Assert.Equal ("s", txtStrings [6].Grapheme);
  1184. Assert.Equal (" ", txtStrings [7].Grapheme);
  1185. Assert.Equal ("a", txtStrings [8].Grapheme);
  1186. Assert.Equal (" ", txtStrings [9].Grapheme);
  1187. Assert.Equal ("t", txtStrings [10].Grapheme);
  1188. Assert.Equal ("e", txtStrings [11].Grapheme);
  1189. Assert.Equal ("x", txtStrings [12].Grapheme);
  1190. Assert.Equal ("t", txtStrings [13].Grapheme);
  1191. Assert.Equal (".", txtStrings [^1].Grapheme);
  1192. var col = 0;
  1193. Assert.True (TextModel.SetCol (ref col, 80, 79));
  1194. Assert.False (TextModel.SetCol (ref col, 80, 80));
  1195. Assert.Equal (79, col);
  1196. var start = 0;
  1197. var x = 8;
  1198. Assert.Equal (8, TextModel.GetColFromX (txtStrings, start, x));
  1199. Assert.Equal ("a", txtStrings [start + x].Grapheme);
  1200. start = 1;
  1201. x = 7;
  1202. Assert.Equal (7, TextModel.GetColFromX (txtStrings, start, x));
  1203. Assert.Equal ("a", txtStrings [start + x].Grapheme);
  1204. Assert.Equal ((15, 15), TextModel.DisplaySize (txtStrings));
  1205. Assert.Equal ((6, 6), TextModel.DisplaySize (txtStrings, 1, 7));
  1206. Assert.Equal (0, TextModel.CalculateLeftColumn (txtStrings, 0, 7, 8));
  1207. Assert.Equal (1, TextModel.CalculateLeftColumn (txtStrings, 0, 8, 8));
  1208. Assert.Equal (2, TextModel.CalculateLeftColumn (txtStrings, 0, 9, 8));
  1209. var tm = new TextModel ();
  1210. tm.AddLine (0, Cell.StringToCells ("This is first line."));
  1211. tm.AddLine (1, Cell.StringToCells ("This is last line."));
  1212. Assert.Equal ((new Point (2, 0), true), tm.FindNextText ("is", out bool gaveFullTurn));
  1213. Assert.False (gaveFullTurn);
  1214. Assert.Equal ((new Point (5, 0), true), tm.FindNextText ("is", out gaveFullTurn));
  1215. Assert.False (gaveFullTurn);
  1216. Assert.Equal ((new Point (2, 1), true), tm.FindNextText ("is", out gaveFullTurn));
  1217. Assert.False (gaveFullTurn);
  1218. Assert.Equal ((new Point (5, 1), true), tm.FindNextText ("is", out gaveFullTurn));
  1219. Assert.False (gaveFullTurn);
  1220. Assert.Equal ((new Point (2, 0), true), tm.FindNextText ("is", out gaveFullTurn));
  1221. Assert.True (gaveFullTurn);
  1222. tm.ResetContinuousFind (Point.Empty);
  1223. Assert.Equal ((new Point (5, 1), true), tm.FindPreviousText ("is", out gaveFullTurn));
  1224. Assert.False (gaveFullTurn);
  1225. Assert.Equal ((new Point (2, 1), true), tm.FindPreviousText ("is", out gaveFullTurn));
  1226. Assert.False (gaveFullTurn);
  1227. Assert.Equal ((new Point (5, 0), true), tm.FindPreviousText ("is", out gaveFullTurn));
  1228. Assert.False (gaveFullTurn);
  1229. Assert.Equal ((new Point (2, 0), true), tm.FindPreviousText ("is", out gaveFullTurn));
  1230. Assert.False (gaveFullTurn);
  1231. Assert.Equal ((new Point (5, 1), true), tm.FindPreviousText ("is", out gaveFullTurn));
  1232. Assert.True (gaveFullTurn);
  1233. Assert.Equal ((new Point (9, 1), true), tm.ReplaceAllText ("is", false, false, "really"));
  1234. Assert.Equal (Cell.StringToCells ("Threally really first line."), tm.GetLine (0));
  1235. Assert.Equal (Cell.StringToCells ("Threally really last line."), tm.GetLine (1));
  1236. tm = new ();
  1237. tm.AddLine (0, Cell.StringToCells ("This is first line."));
  1238. tm.AddLine (1, Cell.StringToCells ("This is last line."));
  1239. Assert.Equal ((new Point (5, 1), true), tm.ReplaceAllText ("is", false, true, "really"));
  1240. Assert.Equal (Cell.StringToCells ("This really first line."), tm.GetLine (0));
  1241. Assert.Equal (Cell.StringToCells ("This really last line."), tm.GetLine (1));
  1242. }
  1243. [Fact]
  1244. public void LeftColumn_Add_One_If_Text_Length_Is_Equal_To_Width ()
  1245. {
  1246. var tv = new TextView { Width = 10, Text = "1234567890" };
  1247. Assert.Equal (Point.Empty, tv.CursorPosition);
  1248. Assert.Equal (0, tv.LeftColumn);
  1249. tv.CursorPosition = new (9, 0);
  1250. Assert.Equal (new (9, 0), tv.CursorPosition);
  1251. Assert.Equal (0, tv.LeftColumn);
  1252. Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
  1253. tv.CursorPosition = new (10, 0);
  1254. Assert.Equal (new (10, 0), tv.CursorPosition);
  1255. Assert.Equal (1, tv.LeftColumn);
  1256. }
  1257. [Fact]
  1258. public void LoadFile_Throws_If_File_Is_Empty ()
  1259. {
  1260. var result = false;
  1261. var tv = new TextView ();
  1262. Assert.Throws<ArgumentException> (() => result = tv.Load (""));
  1263. Assert.False (result);
  1264. }
  1265. [Fact]
  1266. public void LoadFile_Throws_If_File_Is_Null ()
  1267. {
  1268. var result = false;
  1269. var tv = new TextView ();
  1270. Assert.Throws<ArgumentNullException> (() => result = tv.Load ((string)null));
  1271. Assert.False (result);
  1272. }
  1273. [Fact]
  1274. public void LoadFile_Throws_If_File_Not_Exist ()
  1275. {
  1276. var result = false;
  1277. var tv = new TextView ();
  1278. Assert.Throws<FileNotFoundException> (() => result = tv.Load ("blabla"));
  1279. Assert.False (result);
  1280. }
  1281. [Fact]
  1282. public void LoadStream_CRLF ()
  1283. {
  1284. var text = "This is the first line.\r\nThis is the second line.\r\n";
  1285. var tv = new TextView ();
  1286. tv.Load (new MemoryStream (Encoding.ASCII.GetBytes (text)));
  1287. Assert.Equal (
  1288. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  1289. tv.Text
  1290. );
  1291. }
  1292. [Fact]
  1293. public void LoadStream_IsDirty ()
  1294. {
  1295. var text = "Testing";
  1296. using (var stream = new MemoryStream ())
  1297. {
  1298. var writer = new StreamWriter (stream);
  1299. writer.Write (text);
  1300. writer.Flush ();
  1301. stream.Position = 0;
  1302. var tv = new TextView ();
  1303. tv.Load (stream);
  1304. Assert.Equal (7, text.Length);
  1305. Assert.Equal (text.Length, tv.Text.Length);
  1306. Assert.Equal (text, tv.Text);
  1307. Assert.False (tv.IsDirty);
  1308. }
  1309. }
  1310. [Fact]
  1311. public void LoadStream_IsDirty_With_Null_On_The_Text ()
  1312. {
  1313. var text = "Test\0ing";
  1314. using (var stream = new MemoryStream ())
  1315. {
  1316. var writer = new StreamWriter (stream);
  1317. writer.Write (text);
  1318. writer.Flush ();
  1319. stream.Position = 0;
  1320. var tv = new TextView ();
  1321. tv.Load (stream);
  1322. Assert.Equal (8, text.Length);
  1323. Assert.Equal (text.Length, tv.Text.Length);
  1324. Assert.Equal (8, text.Length);
  1325. Assert.Equal (8, tv.Text.Length);
  1326. Assert.Equal (text, tv.Text);
  1327. Assert.False (tv.IsDirty);
  1328. Assert.Equal ((Rune)'\u2400', ((Rune)tv.Text [4]).MakePrintable ());
  1329. }
  1330. }
  1331. [Fact]
  1332. public void LoadStream_LF ()
  1333. {
  1334. var text = "This is the first line.\nThis is the second line.\n";
  1335. var tv = new TextView ();
  1336. tv.Load (new MemoryStream (Encoding.ASCII.GetBytes (text)));
  1337. Assert.Equal (
  1338. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  1339. tv.Text
  1340. );
  1341. }
  1342. [Fact]
  1343. public void LoadStream_Stream_Is_Empty ()
  1344. {
  1345. var tv = new TextView ();
  1346. tv.Load (new MemoryStream ());
  1347. Assert.Equal ("", tv.Text);
  1348. }
  1349. [Fact]
  1350. public void LoadStream_Throws_If_Stream_Is_Null ()
  1351. {
  1352. var tv = new TextView ();
  1353. Assert.Throws<ArgumentNullException> (() => tv.Load ((Stream)null));
  1354. }
  1355. [Fact]
  1356. public void ReplaceAllText_Does_Not_Throw_Exception ()
  1357. {
  1358. var textToFind = "hello! hello!";
  1359. var textToReplace = "hello!";
  1360. var tv = new TextView { Width = 20, Height = 3, Text = textToFind };
  1361. Exception exception = Record.Exception (() => tv.ReplaceAllText (textToFind, false, false, textToReplace));
  1362. Assert.Null (exception);
  1363. Assert.Equal (textToReplace, tv.Text);
  1364. }
  1365. [Fact]
  1366. public void StringToRunes_Slipts_CRLF ()
  1367. {
  1368. var text = "This is the first line.\r\nThis is the second line.\r\n";
  1369. var tv = new TextView ();
  1370. tv.Text = text;
  1371. Assert.Equal (
  1372. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  1373. tv.Text
  1374. );
  1375. }
  1376. [Fact]
  1377. public void StringToRunes_Slipts_LF ()
  1378. {
  1379. var text = "This is the first line.\nThis is the second line.\n";
  1380. var tv = new TextView ();
  1381. tv.Text = text;
  1382. Assert.Equal (
  1383. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  1384. tv.Text
  1385. );
  1386. }
  1387. [Fact]
  1388. public void TextView_MultiLine_But_Without_Tabs ()
  1389. {
  1390. var view = new TextView ();
  1391. // the default for TextView
  1392. Assert.True (view.Multiline);
  1393. view.AllowsTab = false;
  1394. Assert.False (view.AllowsTab);
  1395. Assert.True (view.Multiline);
  1396. }
  1397. [Fact]
  1398. public void WordBackward_WordForward_Limits_Return_Null ()
  1399. {
  1400. var model = new TextModel ();
  1401. model.LoadString ("Test");
  1402. (int col, int row)? newPos = model.WordBackward (0, 0, false);
  1403. Assert.Null (newPos);
  1404. newPos = model.WordForward (4, 0, false);
  1405. Assert.Null (newPos);
  1406. }
  1407. [Fact]
  1408. public void WordWrap_Gets_Sets ()
  1409. {
  1410. var tv = new TextView { WordWrap = true };
  1411. Assert.True (tv.WordWrap);
  1412. tv.WordWrap = false;
  1413. Assert.False (tv.WordWrap);
  1414. }
  1415. [Fact]
  1416. public void WordWrap_True_Text_Always_Returns_Unwrapped ()
  1417. {
  1418. var text = "This is the first line.\nThis is the second line.\n";
  1419. var tv = new TextView { Width = 10 };
  1420. tv.Text = text;
  1421. Assert.Equal (
  1422. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  1423. tv.Text
  1424. );
  1425. tv.WordWrap = true;
  1426. Assert.Equal (
  1427. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  1428. tv.Text
  1429. );
  1430. }
  1431. [Fact]
  1432. public void HotKey_Command_SetsFocus ()
  1433. {
  1434. var view = new TextView ();
  1435. view.CanFocus = true;
  1436. Assert.False (view.HasFocus);
  1437. view.InvokeCommand (Command.HotKey);
  1438. Assert.True (view.HasFocus);
  1439. }
  1440. [Fact]
  1441. public void HotKey_Command_Does_Not_Accept ()
  1442. {
  1443. var view = new TextView ();
  1444. var accepted = false;
  1445. view.Accepting += OnAccept;
  1446. view.InvokeCommand (Command.HotKey);
  1447. Assert.False (accepted);
  1448. return;
  1449. void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
  1450. }
  1451. [Theory]
  1452. [InlineData (false, 1)]
  1453. [InlineData (true, 1)]
  1454. public void Accepted_Command_Raises_Accepted_Regardles_Of_AllowsReturn (bool allowsReturn, int expectedAcceptEvents)
  1455. {
  1456. var view = new TextView
  1457. {
  1458. AllowsReturn = allowsReturn
  1459. };
  1460. var acceptedEvents = 0;
  1461. view.Accepting += Accept;
  1462. view.InvokeCommand (Command.Accept);
  1463. Assert.Equal (expectedAcceptEvents, acceptedEvents);
  1464. return;
  1465. void Accept (object sender, CommandEventArgs e) { acceptedEvents++; }
  1466. }
  1467. [Theory]
  1468. [InlineData (false, 1)]
  1469. [InlineData (true, 0)]
  1470. public void Enter_Key_Fires_Accepted_BasedOn_AllowsReturn (bool allowsReturn, int expectedAccepts)
  1471. {
  1472. var view = new TextView
  1473. {
  1474. Multiline = allowsReturn
  1475. };
  1476. var accepted = 0;
  1477. view.Accepting += Accept;
  1478. view.NewKeyDownEvent (Key.Enter);
  1479. Assert.Equal (expectedAccepts, accepted);
  1480. return;
  1481. void Accept (object sender, CommandEventArgs e) { accepted++; }
  1482. }
  1483. [Theory]
  1484. [InlineData (false, 1)]
  1485. [InlineData (true, 0)]
  1486. public void Enter_Key_Fires_Accepted_BasedOn_Multiline (bool multiline, int expectedAccepts)
  1487. {
  1488. var view = new TextView
  1489. {
  1490. Multiline = multiline
  1491. };
  1492. var accepted = 0;
  1493. view.Accepting += Accept;
  1494. view.NewKeyDownEvent (Key.Enter);
  1495. Assert.Equal (expectedAccepts, accepted);
  1496. return;
  1497. void Accept (object sender, CommandEventArgs e) { accepted++; }
  1498. }
  1499. [Fact]
  1500. public void Space_Key_Types_Space ()
  1501. {
  1502. var view = new TextView ();
  1503. view.NewKeyDownEvent (Key.Space);
  1504. Assert.Equal (" ", view.Text);
  1505. }
  1506. [Theory]
  1507. [InlineData (false, false, 1, 1)]
  1508. [InlineData (false, true, 1, 0)]
  1509. [InlineData (true, false, 0, 0)]
  1510. [InlineData (true, true, 0, 0)]
  1511. public void Accepted_Event_Handled_Prevents_Default_Button_Accept (bool multiline, bool handleAccept, int expectedAccepts, int expectedButtonAccepts)
  1512. {
  1513. var superView = new Window ();
  1514. var tv = new TextView
  1515. {
  1516. Multiline = multiline
  1517. };
  1518. var button = new Button
  1519. {
  1520. IsDefault = true
  1521. };
  1522. superView.Add (tv, button);
  1523. var buttonAccept = 0;
  1524. button.Accepting += ButtonAccept;
  1525. var textViewAccept = 0;
  1526. tv.Accepting += TextViewAccept;
  1527. tv.SetFocus ();
  1528. Assert.True (tv.HasFocus);
  1529. superView.NewKeyDownEvent (Key.Enter);
  1530. Assert.Equal (expectedAccepts, textViewAccept);
  1531. Assert.Equal (expectedButtonAccepts, buttonAccept);
  1532. button.SetFocus ();
  1533. superView.NewKeyDownEvent (Key.Enter);
  1534. Assert.Equal (expectedAccepts, textViewAccept);
  1535. Assert.Equal (expectedButtonAccepts + 1, buttonAccept);
  1536. return;
  1537. void TextViewAccept (object sender, CommandEventArgs e)
  1538. {
  1539. textViewAccept++;
  1540. e.Handled = handleAccept;
  1541. }
  1542. void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
  1543. }
  1544. [Theory]
  1545. [InlineData (true, 0)]
  1546. [InlineData (false, 1)]
  1547. public void Accepted_No_Handler_Enables_Default_Button_Accept (bool multiline, int expectedButtonAccept)
  1548. {
  1549. var superView = new Window ();
  1550. var tv = new TextView
  1551. {
  1552. Multiline = multiline
  1553. };
  1554. var button = new Button
  1555. {
  1556. IsDefault = true
  1557. };
  1558. superView.Add (tv, button);
  1559. var buttonAccept = 0;
  1560. button.Accepting += ButtonAccept;
  1561. tv.SetFocus ();
  1562. Assert.True (tv.HasFocus);
  1563. superView.NewKeyDownEvent (Key.Enter);
  1564. Assert.Equal (expectedButtonAccept, buttonAccept);
  1565. button.SetFocus ();
  1566. superView.NewKeyDownEvent (Key.Enter);
  1567. Assert.Equal (expectedButtonAccept + 1, buttonAccept);
  1568. return;
  1569. void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
  1570. }
  1571. [Fact]
  1572. public void Autocomplete_Popup_Added_To_SuperView_On_Init ()
  1573. {
  1574. View superView = new ()
  1575. {
  1576. CanFocus = true
  1577. };
  1578. TextView t = new ();
  1579. superView.Add (t);
  1580. Assert.Single (superView.SubViews);
  1581. superView.BeginInit ();
  1582. superView.EndInit ();
  1583. Assert.Equal (2, superView.SubViews.Count);
  1584. }
  1585. [Fact]
  1586. public void Autocomplete__Added_To_SuperView_On_Add ()
  1587. {
  1588. View superView = new ()
  1589. {
  1590. CanFocus = true,
  1591. Id = "superView"
  1592. };
  1593. superView.BeginInit ();
  1594. superView.EndInit ();
  1595. Assert.Empty (superView.SubViews);
  1596. TextView t = new ()
  1597. {
  1598. Id = "t"
  1599. };
  1600. superView.Add (t);
  1601. Assert.Equal (2, superView.SubViews.Count);
  1602. }
  1603. [Fact]
  1604. public void Autocomplete_Visible_False_By_Default ()
  1605. {
  1606. View superView = new ()
  1607. {
  1608. CanFocus = true
  1609. };
  1610. TextView t = new ();
  1611. superView.Add (t);
  1612. superView.BeginInit ();
  1613. superView.EndInit ();
  1614. Assert.Equal (2, superView.SubViews.Count);
  1615. Assert.True (t.Visible);
  1616. Assert.False (t.Autocomplete.Visible);
  1617. }
  1618. [Fact]
  1619. public void Right_CursorAtEnd_WithSelection_ShouldClearSelection ()
  1620. {
  1621. var tv = new TextView
  1622. {
  1623. Text = "Hello"
  1624. };
  1625. tv.SetFocus ();
  1626. tv.NewKeyDownEvent (Key.End.WithShift);
  1627. Assert.Equal (5, tv.CursorPosition.X);
  1628. // When there is selected text and the cursor is at the end of the text field
  1629. Assert.Equal ("Hello", tv.SelectedText);
  1630. // Pressing right should not move focus, instead it should clear selection
  1631. Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
  1632. Assert.Empty (tv.SelectedText);
  1633. // Now that the selection is cleared another right keypress should move focus
  1634. Assert.False (tv.NewKeyDownEvent (Key.CursorRight));
  1635. }
  1636. [Fact]
  1637. public void Left_CursorAtStart_WithSelection_ShouldClearSelection ()
  1638. {
  1639. var tv = new TextView
  1640. {
  1641. Text = "Hello"
  1642. };
  1643. tv.SetFocus ();
  1644. tv.NewKeyDownEvent (Key.CursorRight);
  1645. tv.NewKeyDownEvent (Key.CursorRight);
  1646. Assert.Equal (2, tv.CursorPosition.X);
  1647. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift));
  1648. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift));
  1649. // When there is selected text and the cursor is at the start of the text field
  1650. Assert.Equal ("He", tv.SelectedText);
  1651. // Pressing left should not move focus, instead it should clear selection
  1652. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft));
  1653. Assert.Empty (tv.SelectedText);
  1654. // When clearing selected text with left the cursor should be at the start of the selection
  1655. Assert.Equal (0, tv.CursorPosition.X);
  1656. // Now that the selection is cleared another left keypress should move focus
  1657. Assert.False (tv.NewKeyDownEvent (Key.CursorLeft));
  1658. }
  1659. [Fact]
  1660. public void Equals_True ()
  1661. {
  1662. var c1 = new Cell ();
  1663. var c2 = new Cell ();
  1664. Assert.True (c1.Equals (c2));
  1665. Assert.True (c2.Equals (c1));
  1666. c1.Grapheme = new ("a");
  1667. c1.Attribute = new ();
  1668. c2.Grapheme = new ("a");
  1669. c2.Attribute = new ();
  1670. Assert.True (c1.Equals (c2));
  1671. Assert.True (c2.Equals (c1));
  1672. }
  1673. [Fact]
  1674. public void Cell_LoadCells_Without_Scheme_Is_Never_Null ()
  1675. {
  1676. List<Cell> cells = new ()
  1677. {
  1678. new () { Grapheme = new ("T") },
  1679. new () { Grapheme = new ("e") },
  1680. new () { Grapheme = new ("s") },
  1681. new () { Grapheme = new ("t") }
  1682. };
  1683. TextView tv = CreateTextView ();
  1684. var top = new Toplevel ();
  1685. top.Add (tv);
  1686. tv.Load (cells);
  1687. for (var i = 0; i < tv.Lines; i++)
  1688. {
  1689. List<Cell> line = tv.GetLine (i);
  1690. foreach (Cell c in line)
  1691. {
  1692. Assert.NotNull (c.Attribute);
  1693. }
  1694. }
  1695. }
  1696. [Theory]
  1697. [InlineData ("", false, "")]
  1698. [InlineData ("", true, "")]
  1699. [InlineData (" ", false, "")]
  1700. [InlineData (" ", true, "")]
  1701. [InlineData (" ", false, "")]
  1702. [InlineData (" ", true, "")]
  1703. [InlineData ("a", false, "")]
  1704. [InlineData ("a", true, "")]
  1705. [InlineData ("a ", false, "")]
  1706. [InlineData ("a ", true, "")]
  1707. [InlineData (" a ", false, "a ", "")]
  1708. [InlineData (" a ", true, "a ", "")]
  1709. [InlineData (" H1 ", false, "H1 ", "")]
  1710. [InlineData (" H1 ", true, "H1 ", "")]
  1711. [InlineData ("a$", false, "$", "")]
  1712. [InlineData ("a$", true, "$", "")]
  1713. [InlineData ("a$#", false, "$#", "")]
  1714. [InlineData ("a$#", true, "$#", "#", "")]
  1715. [InlineData (" a$# ", false, "a$# ", "$# ", "")]
  1716. [InlineData (" a$# ", true, "a$# ", "$# ", "# ", "")]
  1717. [InlineData ("\"$schema\"", false, "schema\"", "\"", "")]
  1718. [InlineData ("\"$schema\"", true, "$schema\"", "schema\"", "\"", "")]
  1719. [InlineData ("\": \"", false, "\"", "")]
  1720. [InlineData ("\": \"", true, "\"", "")]
  1721. [InlineData ("\"$schema\": \"", false, "schema\": \"", "\": \"", "\"", "")]
  1722. [InlineData ("\"$schema\": \"", true, "$schema\": \"", "schema\": \"", "\": \"", "\"", "")]
  1723. [InlineData ("1ºªA", false, "")]
  1724. [InlineData ("1ºªA", true, "")]
  1725. [InlineData (
  1726. "ºª\\!\"#%&/()?'«»*;,:._-@{[]}]|$=+´`~^<>£€¨",
  1727. false,
  1728. "\\!\"#%&/()?'«»*;,:._-@{[]}]|$=+´`~^<>£€¨",
  1729. "")]
  1730. [InlineData (
  1731. "ºª\\!\"#%&/()?'«»*;,:._-@{[]}]|$=+´`~^<>£€¨",
  1732. true,
  1733. "\\!\"#%&/()?'«»*;,:._-@{[]}]|$=+´`~^<>£€¨",
  1734. "|$=+´`~^<>£€¨",
  1735. "")]
  1736. [InlineData (
  1737. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1738. false,
  1739. "\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1740. " \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1741. "\"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1742. "schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1743. "\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1744. "\"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1745. "https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1746. "://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1747. "gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1748. "-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1749. "cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1750. ".github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1751. "github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1752. ".io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1753. "io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1754. "/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1755. "Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1756. ".Gui/schemas/tui-config-schema.json\"\r\n}",
  1757. "Gui/schemas/tui-config-schema.json\"\r\n}",
  1758. "/schemas/tui-config-schema.json\"\r\n}",
  1759. "schemas/tui-config-schema.json\"\r\n}",
  1760. "/tui-config-schema.json\"\r\n}",
  1761. "tui-config-schema.json\"\r\n}",
  1762. "-config-schema.json\"\r\n}",
  1763. "config-schema.json\"\r\n}",
  1764. "-schema.json\"\r\n}",
  1765. "schema.json\"\r\n}",
  1766. ".json\"\r\n}",
  1767. "json\"\r\n}",
  1768. "\"\r\n}",
  1769. "\r\n}",
  1770. "}",
  1771. "")]
  1772. [InlineData (
  1773. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1774. true,
  1775. "\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1776. " \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1777. "\"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1778. "$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1779. "schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1780. "\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1781. "\"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1782. "https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1783. "://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1784. "gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1785. "-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1786. "cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1787. ".github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1788. "github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1789. ".io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1790. "io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1791. "/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1792. "Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1793. ".Gui/schemas/tui-config-schema.json\"\r\n}",
  1794. "Gui/schemas/tui-config-schema.json\"\r\n}",
  1795. "/schemas/tui-config-schema.json\"\r\n}",
  1796. "schemas/tui-config-schema.json\"\r\n}",
  1797. "/tui-config-schema.json\"\r\n}",
  1798. "tui-config-schema.json\"\r\n}",
  1799. "-config-schema.json\"\r\n}",
  1800. "config-schema.json\"\r\n}",
  1801. "-schema.json\"\r\n}",
  1802. "schema.json\"\r\n}",
  1803. ".json\"\r\n}",
  1804. "json\"\r\n}",
  1805. "\"\r\n}",
  1806. "\r\n}",
  1807. "}",
  1808. "")]
  1809. public void WordForward_WordWrap_False_True (string text, bool useSameRuneType, params string [] expectedText)
  1810. {
  1811. TextView tv = CreateTextView ();
  1812. tv.UseSameRuneTypeForWords = useSameRuneType;
  1813. ProcessDeleteWithCtrl ();
  1814. tv.WordWrap = true;
  1815. ProcessDeleteWithCtrl ();
  1816. void ProcessDeleteWithCtrl ()
  1817. {
  1818. tv.Text = text;
  1819. var idx = 0;
  1820. while (!string.IsNullOrEmpty (tv.Text))
  1821. {
  1822. tv.NewKeyDownEvent (Key.Delete.WithCtrl);
  1823. Assert.Equal (expectedText [idx].Replace ("\r\n", Environment.NewLine), tv.Text);
  1824. idx++;
  1825. }
  1826. }
  1827. }
  1828. [Theory]
  1829. [InlineData ("", false, "")]
  1830. [InlineData ("", true, "")]
  1831. [InlineData (" ", false, "")]
  1832. [InlineData (" ", true, "")]
  1833. [InlineData (" ", false, "")]
  1834. [InlineData (" ", true, "")]
  1835. [InlineData ("a", false, "")]
  1836. [InlineData ("a", true, "")]
  1837. [InlineData ("a ", false, "")]
  1838. [InlineData ("a ", true, "")]
  1839. [InlineData (" a ", false, " ", "")]
  1840. [InlineData (" a ", true, " ", "")]
  1841. [InlineData (" H1 ", false, " ", "")]
  1842. [InlineData (" H1 ", true, " ", "")]
  1843. [InlineData ("a$", false, "a", "")]
  1844. [InlineData ("a$", true, "a", "")]
  1845. [InlineData ("a$#", false, "a", "")]
  1846. [InlineData ("a$#", true, "a$", "a", "")]
  1847. [InlineData (" a$# ", false, " a", " ", "")]
  1848. [InlineData (" a$# ", true, " a$", " a", " ", "")]
  1849. [InlineData ("\"$schema\"", false, "\"$schema", "\"$", "")]
  1850. [InlineData ("\"$schema\"", true, "\"$schema", "\"$", "\"", "")]
  1851. [InlineData ("\"$schema\": \"", false, "\"$schema\": ", "\"$schema", "\"$", "")]
  1852. [InlineData ("\"$schema\": \"", true, "\"$schema\": ", "\"$schema", "\"$", "\"", "")]
  1853. [InlineData ("1ºªA", false, "")]
  1854. [InlineData ("1ºªA", true, "")]
  1855. [InlineData (
  1856. "ºª\\!\"#%&/()?'«»*;,:._-@{[]}]|$=+´`~^<>£€¨",
  1857. false,
  1858. "ºª",
  1859. "")]
  1860. [InlineData (
  1861. "ºª\\!\"#%&/()?'«»*;,:._-@{[]}]|$=+´`~^<>£€¨",
  1862. true,
  1863. "ºª\\!\"#%&/()?'«»*;,:._-@{[]}]",
  1864. "ºª",
  1865. "")]
  1866. [InlineData (
  1867. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1868. false,
  1869. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n",
  1870. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"",
  1871. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json",
  1872. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.",
  1873. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema",
  1874. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-",
  1875. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config",
  1876. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-",
  1877. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui",
  1878. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/",
  1879. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas",
  1880. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/",
  1881. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui",
  1882. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.",
  1883. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal",
  1884. "{\r\n \"$schema\": \"https://gui-cs.github.io/",
  1885. "{\r\n \"$schema\": \"https://gui-cs.github.io",
  1886. "{\r\n \"$schema\": \"https://gui-cs.github.",
  1887. "{\r\n \"$schema\": \"https://gui-cs.github",
  1888. "{\r\n \"$schema\": \"https://gui-cs.",
  1889. "{\r\n \"$schema\": \"https://gui-cs",
  1890. "{\r\n \"$schema\": \"https://gui-",
  1891. "{\r\n \"$schema\": \"https://gui",
  1892. "{\r\n \"$schema\": \"https://",
  1893. "{\r\n \"$schema\": \"https",
  1894. "{\r\n \"$schema\": \"",
  1895. "{\r\n \"$schema\": ",
  1896. "{\r\n \"$schema",
  1897. "{\r\n \"$",
  1898. "{\r\n ",
  1899. "{\r\n",
  1900. "{",
  1901. "")]
  1902. [InlineData (
  1903. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n}",
  1904. true,
  1905. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"\r\n",
  1906. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json\"",
  1907. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json",
  1908. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.",
  1909. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema",
  1910. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-",
  1911. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-config",
  1912. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui-",
  1913. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/tui",
  1914. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas/",
  1915. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/schemas",
  1916. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui/",
  1917. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.Gui",
  1918. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal.",
  1919. "{\r\n \"$schema\": \"https://gui-cs.github.io/Terminal",
  1920. "{\r\n \"$schema\": \"https://gui-cs.github.io/",
  1921. "{\r\n \"$schema\": \"https://gui-cs.github.io",
  1922. "{\r\n \"$schema\": \"https://gui-cs.github.",
  1923. "{\r\n \"$schema\": \"https://gui-cs.github",
  1924. "{\r\n \"$schema\": \"https://gui-cs.",
  1925. "{\r\n \"$schema\": \"https://gui-cs",
  1926. "{\r\n \"$schema\": \"https://gui-",
  1927. "{\r\n \"$schema\": \"https://gui",
  1928. "{\r\n \"$schema\": \"https://",
  1929. "{\r\n \"$schema\": \"https",
  1930. "{\r\n \"$schema\": \"",
  1931. "{\r\n \"$schema\": ",
  1932. "{\r\n \"$schema",
  1933. "{\r\n \"$",
  1934. "{\r\n \"",
  1935. "{\r\n ",
  1936. "{\r\n",
  1937. "{",
  1938. "")]
  1939. public void WordBackward_WordWrap_False_True (string text, bool useSameRuneType, params string [] expectedText)
  1940. {
  1941. TextView tv = CreateTextView ();
  1942. tv.UseSameRuneTypeForWords = useSameRuneType;
  1943. ProcessBackspaceWithCtrl ();
  1944. tv.WordWrap = true;
  1945. ProcessBackspaceWithCtrl ();
  1946. void ProcessBackspaceWithCtrl ()
  1947. {
  1948. tv.Text = text;
  1949. tv.MoveEnd ();
  1950. var idx = 0;
  1951. while (!string.IsNullOrEmpty (tv.Text))
  1952. {
  1953. tv.NewKeyDownEvent (Key.Backspace.WithCtrl);
  1954. Assert.Equal (expectedText [idx].Replace ("\r\n", Environment.NewLine), tv.Text);
  1955. idx++;
  1956. }
  1957. }
  1958. }
  1959. [Theory]
  1960. [InlineData ("", 0, false, "")]
  1961. [InlineData ("", 0, true, "")]
  1962. [InlineData ("a", 0, false, "a")]
  1963. [InlineData ("a", 0, true, "a")]
  1964. [InlineData ("a:", 0, false, "a")]
  1965. [InlineData ("a:", 0, true, "a")]
  1966. [InlineData ("a:", 1, false, ":")]
  1967. [InlineData ("a:", 1, true, ":")]
  1968. [InlineData ("a ", 0, false, "a ")]
  1969. [InlineData ("a ", 0, true, "a")]
  1970. [InlineData ("a ", 1, false, "a ")]
  1971. [InlineData ("a ", 1, true, "a")]
  1972. [InlineData ("a b", 0, false, "a ")]
  1973. [InlineData ("a b", 0, true, "a")]
  1974. [InlineData ("a b", 1, false, "a ")]
  1975. [InlineData ("a b", 1, true, "a")]
  1976. [InlineData ("a b ", 2, false, "b ")]
  1977. [InlineData ("a b ", 2, true, "b")]
  1978. [InlineData ("a b ", 3, false, "b ")]
  1979. [InlineData ("a b ", 3, true, "b")]
  1980. [InlineData (" a b ", 0, false, " ")]
  1981. [InlineData (" a b ", 0, true, " ")]
  1982. [InlineData (" a b ", 2, false, " ")]
  1983. [InlineData (" a b ", 2, true, " ")]
  1984. [InlineData (" a b ", 3, false, " ")]
  1985. [InlineData (" a b ", 3, true, " ")]
  1986. [InlineData (" H1$&#2you ", 2, false, "H1")]
  1987. [InlineData (" H1$&#2you ", 2, true, "H1")]
  1988. [InlineData (" H1$&#2you ", 3, false, "$&#")]
  1989. [InlineData (" H1$&#2you ", 3, true, "$&#")]
  1990. [InlineData (" H1$&#2you ", 4, false, "$&#")]
  1991. [InlineData (" H1$&#2you ", 4, true, "$&#")]
  1992. [InlineData (" H1$&#2you ", 5, false, "$&#")]
  1993. [InlineData (" H1$&#2you ", 5, true, "$&#")]
  1994. [InlineData (" H1$&#2you ", 6, false, "2you ")]
  1995. [InlineData (" H1$&#2you ", 6, true, "2you")]
  1996. public void ProcessDoubleClickSelection_False_True (string text, int col, bool selectWordOnly, string expectedText)
  1997. {
  1998. TextView tv = CreateTextView ();
  1999. tv.Text = text;
  2000. tv.SelectWordOnlyOnDoubleClick = selectWordOnly;
  2001. Assert.True (tv.NewMouseEvent (new () { Position = new (col, 0), Flags = MouseFlags.Button1DoubleClicked }));
  2002. Assert.Equal (expectedText, tv.SelectedText);
  2003. }
  2004. private TextView CreateTextView () { return new () { Width = 30, Height = 10 }; }
  2005. }