TextFormatterTests.cs 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.SpinnerStyle;
  4. // Alias Console to MockConsole so we don't accidentally use Console
  5. namespace Terminal.Gui.TextTests;
  6. public class TextFormatterTests
  7. {
  8. private readonly ITestOutputHelper _output;
  9. public TextFormatterTests (ITestOutputHelper output) { _output = output; }
  10. public static IEnumerable<object []> CMGlyphs =>
  11. new List<object []> { new object [] { $"{CM.Glyphs.LeftBracket} Say Hello 你 {CM.Glyphs.RightBracket}", 16, 15 } };
  12. public static IEnumerable<object []> FormatEnvironmentNewLine =>
  13. new List<object []>
  14. {
  15. new object []
  16. {
  17. $"Line1{Environment.NewLine}Line2{Environment.NewLine}Line3{Environment.NewLine}",
  18. 60,
  19. new [] { "Line1", "Line2", "Line3" }
  20. }
  21. };
  22. public static IEnumerable<object []> SplitEnvironmentNewLine =>
  23. new List<object []>
  24. {
  25. new object []
  26. {
  27. $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界",
  28. new [] { "First Line 界", "Second Line 界", "Third Line 界" }
  29. },
  30. new object []
  31. {
  32. $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界{Environment.NewLine}",
  33. new [] { "First Line 界", "Second Line 界", "Third Line 界", "" }
  34. }
  35. };
  36. [Fact]
  37. public void Basic_Usage ()
  38. {
  39. var testText = "test";
  40. var testBounds = new Rectangle (0, 0, 100, 1);
  41. var tf = new TextFormatter ();
  42. tf.Text = testText;
  43. Size expectedSize = new (testText.Length, 1);
  44. Assert.Equal (testText, tf.Text);
  45. Assert.Equal (TextAlignment.Left, tf.Alignment);
  46. Assert.Equal (expectedSize, tf.Size);
  47. tf.Draw (testBounds, new Attribute (), new Attribute ());
  48. Assert.Equal (expectedSize, tf.Size);
  49. Assert.NotEmpty (tf.GetLines ());
  50. tf.Alignment = TextAlignment.Right;
  51. expectedSize = new (testText.Length, 1);
  52. Assert.Equal (testText, tf.Text);
  53. Assert.Equal (TextAlignment.Right, tf.Alignment);
  54. Assert.Equal (expectedSize, tf.Size);
  55. tf.Draw (testBounds, new Attribute (), new Attribute ());
  56. Assert.Equal (expectedSize, tf.Size);
  57. Assert.NotEmpty (tf.GetLines ());
  58. tf.Alignment = TextAlignment.Right;
  59. expectedSize = new (testText.Length * 2, 1);
  60. tf.Size = expectedSize;
  61. Assert.Equal (testText, tf.Text);
  62. Assert.Equal (TextAlignment.Right, tf.Alignment);
  63. Assert.Equal (expectedSize, tf.Size);
  64. tf.Draw (testBounds, new Attribute (), new Attribute ());
  65. Assert.Equal (expectedSize, tf.Size);
  66. Assert.NotEmpty (tf.GetLines ());
  67. tf.Alignment = TextAlignment.Centered;
  68. expectedSize = new (testText.Length * 2, 1);
  69. tf.Size = expectedSize;
  70. Assert.Equal (testText, tf.Text);
  71. Assert.Equal (TextAlignment.Centered, tf.Alignment);
  72. Assert.Equal (expectedSize, tf.Size);
  73. tf.Draw (testBounds, new Attribute (), new Attribute ());
  74. Assert.Equal (expectedSize, tf.Size);
  75. Assert.NotEmpty (tf.GetLines ());
  76. }
  77. [Theory]
  78. [InlineData (null)]
  79. [InlineData ("")]
  80. public void CalcRect_Invalid_Returns_Empty (string text)
  81. {
  82. Assert.Equal (Rectangle.Empty, TextFormatter.CalcRect (0, 0, text));
  83. Assert.Equal (new (new (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, text));
  84. Assert.Equal (new (new (-1, -2), Size.Empty), TextFormatter.CalcRect (-1, -2, text));
  85. }
  86. [Theory]
  87. [InlineData ("line1\nline2", 5, 2)]
  88. [InlineData ("\nline2", 5, 2)]
  89. [InlineData ("\n\n", 0, 3)]
  90. [InlineData ("\n\n\n", 0, 4)]
  91. [InlineData ("line1\nline2\nline3long!", 10, 3)]
  92. [InlineData ("line1\nline2\n\n", 5, 4)]
  93. [InlineData ("line1\r\nline2", 5, 2)]
  94. [InlineData (" ~  s  gui.cs   master ↑10\n", 31, 2)]
  95. [InlineData ("\n ~  s  gui.cs   master ↑10", 31, 2)]
  96. [InlineData (" ~  s  gui.cs   master\n↑10", 27, 2)]
  97. public void CalcRect_MultiLine_Returns_nHigh (string text, int expectedWidth, int expectedLines)
  98. {
  99. Assert.Equal (new (0, 0, expectedWidth, expectedLines), TextFormatter.CalcRect (0, 0, text));
  100. string [] lines = text.Split (text.Contains (Environment.NewLine) ? Environment.NewLine : "\n");
  101. int maxWidth = lines.Max (s => s.GetColumns ());
  102. var lineWider = 0;
  103. for (var i = 0; i < lines.Length; i++)
  104. {
  105. int w = lines [i].GetColumns ();
  106. if (w == maxWidth)
  107. {
  108. lineWider = i;
  109. }
  110. }
  111. Assert.Equal (new (0, 0, maxWidth, expectedLines), TextFormatter.CalcRect (0, 0, text));
  112. Assert.Equal (
  113. new (
  114. 0,
  115. 0,
  116. lines [lineWider].ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 0)),
  117. expectedLines
  118. ),
  119. TextFormatter.CalcRect (0, 0, text)
  120. );
  121. }
  122. [Theory]
  123. [InlineData ("test")]
  124. [InlineData (" ~  s  gui.cs   master ↑10")]
  125. public void CalcRect_SingleLine_Returns_1High (string text)
  126. {
  127. Assert.Equal (new (0, 0, text.GetRuneCount (), 1), TextFormatter.CalcRect (0, 0, text));
  128. Assert.Equal (new (0, 0, text.GetColumns (), 1), TextFormatter.CalcRect (0, 0, text));
  129. }
  130. [Theory]
  131. [InlineData (14, 1, TextDirection.LeftRight_TopBottom)]
  132. [InlineData (1, 14, TextDirection.TopBottom_LeftRight)]
  133. public void CalcRect_With_Combining_Runes (int width, int height, TextDirection textDirection)
  134. {
  135. var text = "Les Mise\u0328\u0301rables";
  136. Assert.Equal (new (0, 0, width, height), TextFormatter.CalcRect (0, 0, text, textDirection));
  137. }
  138. [Theory]
  139. [InlineData ("")]
  140. [InlineData (null)]
  141. [InlineData ("test")]
  142. public void ClipAndJustify_Invalid_Returns_Original (string text)
  143. {
  144. string expected = string.IsNullOrEmpty (text) ? text : "";
  145. Assert.Equal (expected, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  146. Assert.Equal (expected, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  147. Assert.Throws<ArgumentOutOfRangeException> (
  148. () =>
  149. TextFormatter.ClipAndJustify (text, -1, TextAlignment.Left)
  150. );
  151. }
  152. [Theory]
  153. [InlineData ("test", "", 0)]
  154. [InlineData ("test", "te", 2)]
  155. [InlineData ("test", "test", int.MaxValue)]
  156. [InlineData ("A sentence has words.", "A sentence has words.", 22)] // should fit
  157. [InlineData ("A sentence has words.", "A sentence has words.", 21)] // should fit
  158. [InlineData ("A sentence has words.", "A sentence has words.", int.MaxValue)] // should fit
  159. [InlineData ("A sentence has words.", "A sentence has words", 20)] // Should not fit
  160. [InlineData ("A sentence has words.", "A sentence", 10)] // Should not fit
  161. [InlineData ("A\tsentence\thas\twords.", "A sentence has words.", int.MaxValue)]
  162. [InlineData ("A\tsentence\thas\twords.", "A sentence", 10)]
  163. [InlineData ("line1\nline2\nline3long!", "line1\nline2\nline3long!", int.MaxValue)]
  164. [InlineData ("line1\nline2\nline3long!", "line1\nline", 10)]
  165. [InlineData (" ~  s  gui.cs   master ↑10", " ~  s  ", 10)] // Unicode
  166. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 5)] // should fit
  167. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 4)] // should fit
  168. [InlineData ("Ð ÑÐ", "Ð Ñ", 3)] // Should not fit
  169. public void ClipAndJustify_Valid_Centered (string text, string justifiedText, int maxWidth)
  170. {
  171. var align = TextAlignment.Centered;
  172. var textDirection = TextDirection.LeftRight_TopBottom;
  173. var tabWidth = 1;
  174. Assert.Equal (
  175. justifiedText,
  176. TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth)
  177. );
  178. int expectedClippedWidth = Math.Min (justifiedText.GetRuneCount (), maxWidth);
  179. Assert.Equal (
  180. justifiedText,
  181. TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth)
  182. );
  183. Assert.True (justifiedText.GetRuneCount () <= maxWidth);
  184. Assert.True (justifiedText.GetColumns () <= maxWidth);
  185. Assert.Equal (expectedClippedWidth, justifiedText.GetRuneCount ());
  186. Assert.Equal (
  187. expectedClippedWidth,
  188. justifiedText.ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 1))
  189. );
  190. Assert.True (expectedClippedWidth <= maxWidth);
  191. Assert.Equal (
  192. StringExtensions.ToString (justifiedText.ToRunes () [..expectedClippedWidth]),
  193. justifiedText
  194. );
  195. }
  196. [Theory]
  197. [InlineData ("test", "", 0)]
  198. [InlineData ("test", "te", 2)]
  199. [InlineData ("test", "test", int.MaxValue)] // This doesn't throw because it only create a word with length 1
  200. [InlineData ("A sentence has words.", "A sentence has words.", 22)] // should fit
  201. [InlineData ("A sentence has words.", "A sentence has words.", 21)] // should fit
  202. [InlineData (
  203. "A sentence has words.",
  204. "A sentence has words.",
  205. 500
  206. )] // should fit
  207. [InlineData ("A sentence has words.", "A sentence has words", 20)] // Should not fit
  208. [InlineData ("A sentence has words.", "A sentence", 10)] // Should not fit
  209. // Now throw System.OutOfMemoryException. See https://stackoverflow.com/questions/20672920/maxcapacity-of-stringbuilder
  210. //[InlineData ("A\tsentence\thas\twords.", "A sentence has words.", int.MaxValue)]
  211. [InlineData ("A\tsentence\thas\twords.", "A sentence", 10)]
  212. [InlineData (
  213. "line1\nline2\nline3long!",
  214. "line1\nline2\nline3long!",
  215. int.MaxValue
  216. )] // This doesn't throw because it only create a line with length 1
  217. [InlineData ("line1\nline2\nline3long!", "line1\nline", 10)]
  218. [InlineData (" ~  s  gui.cs   master ↑10", " ~  s  ", 10)] // Unicode
  219. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 5)] // should fit
  220. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 4)] // should fit
  221. [InlineData ("Ð ÑÐ", "Ð Ñ", 3)] // Should not fit
  222. public void ClipAndJustify_Valid_Justified (string text, string justifiedText, int maxWidth)
  223. {
  224. var align = TextAlignment.Justified;
  225. var textDirection = TextDirection.LeftRight_TopBottom;
  226. var tabWidth = 1;
  227. Assert.Equal (
  228. justifiedText,
  229. TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth)
  230. );
  231. int expectedClippedWidth = Math.Min (justifiedText.GetRuneCount (), maxWidth);
  232. Assert.Equal (
  233. justifiedText,
  234. TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth)
  235. );
  236. Assert.True (justifiedText.GetRuneCount () <= maxWidth);
  237. Assert.True (justifiedText.GetColumns () <= maxWidth);
  238. Assert.Equal (expectedClippedWidth, justifiedText.GetRuneCount ());
  239. Assert.Equal (
  240. expectedClippedWidth,
  241. justifiedText.ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 1))
  242. );
  243. Assert.True (expectedClippedWidth <= maxWidth);
  244. Assert.Equal (
  245. StringExtensions.ToString (justifiedText.ToRunes () [..expectedClippedWidth]),
  246. justifiedText
  247. );
  248. // see Justify_ tests below
  249. }
  250. [Theory]
  251. [InlineData ("test", "", 0)]
  252. [InlineData ("test", "te", 2)]
  253. [InlineData ("test", "test", int.MaxValue)]
  254. [InlineData ("A sentence has words.", "A sentence has words.", 22)] // should fit
  255. [InlineData ("A sentence has words.", "A sentence has words.", 21)] // should fit
  256. [InlineData ("A sentence has words.", "A sentence has words.", int.MaxValue)] // should fit
  257. [InlineData ("A sentence has words.", "A sentence has words", 20)] // Should not fit
  258. [InlineData ("A sentence has words.", "A sentence", 10)] // Should not fit
  259. [InlineData ("A\tsentence\thas\twords.", "A sentence has words.", int.MaxValue)]
  260. [InlineData ("A\tsentence\thas\twords.", "A sentence", 10)]
  261. [InlineData ("line1\nline2\nline3long!", "line1\nline2\nline3long!", int.MaxValue)]
  262. [InlineData ("line1\nline2\nline3long!", "line1\nline", 10)]
  263. [InlineData (" ~  s  gui.cs   master ↑10", " ~  s  ", 10)] // Unicode
  264. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 5)] // should fit
  265. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 4)] // should fit
  266. [InlineData ("Ð ÑÐ", "Ð Ñ", 3)] // Should not fit
  267. public void ClipAndJustify_Valid_Left (string text, string justifiedText, int maxWidth)
  268. {
  269. var align = TextAlignment.Left;
  270. var textDirection = TextDirection.LeftRight_BottomTop;
  271. var tabWidth = 1;
  272. Assert.Equal (
  273. justifiedText,
  274. TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth)
  275. );
  276. int expectedClippedWidth = Math.Min (justifiedText.GetRuneCount (), maxWidth);
  277. Assert.Equal (
  278. justifiedText,
  279. TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth)
  280. );
  281. Assert.True (justifiedText.GetRuneCount () <= maxWidth);
  282. Assert.True (justifiedText.GetColumns () <= maxWidth);
  283. Assert.Equal (expectedClippedWidth, justifiedText.GetRuneCount ());
  284. Assert.Equal (
  285. expectedClippedWidth,
  286. justifiedText.ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 1))
  287. );
  288. Assert.True (expectedClippedWidth <= maxWidth);
  289. Assert.Equal (
  290. StringExtensions.ToString (justifiedText.ToRunes () [..expectedClippedWidth]),
  291. justifiedText
  292. );
  293. }
  294. [Theory]
  295. [InlineData ("test", "", 0)]
  296. [InlineData ("test", "te", 2)]
  297. [InlineData ("test", "test", int.MaxValue)]
  298. [InlineData ("A sentence has words.", "A sentence has words.", 22)] // should fit
  299. [InlineData ("A sentence has words.", "A sentence has words.", 21)] // should fit
  300. [InlineData ("A sentence has words.", "A sentence has words.", int.MaxValue)] // should fit
  301. [InlineData ("A sentence has words.", "A sentence has words", 20)] // Should not fit
  302. [InlineData ("A sentence has words.", "A sentence", 10)] // Should not fit
  303. [InlineData ("A\tsentence\thas\twords.", "A sentence has words.", int.MaxValue)]
  304. [InlineData ("A\tsentence\thas\twords.", "A sentence", 10)]
  305. [InlineData ("line1\nline2\nline3long!", "line1\nline2\nline3long!", int.MaxValue)]
  306. [InlineData ("line1\nline2\nline3long!", "line1\nline", 10)]
  307. [InlineData (" ~  s  gui.cs   master ↑10", " ~  s  ", 10)] // Unicode
  308. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 5)] // should fit
  309. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 4)] // should fit
  310. [InlineData ("Ð ÑÐ", "Ð Ñ", 3)] // Should not fit
  311. public void ClipAndJustify_Valid_Right (string text, string justifiedText, int maxWidth)
  312. {
  313. var align = TextAlignment.Right;
  314. var textDirection = TextDirection.LeftRight_BottomTop;
  315. var tabWidth = 1;
  316. Assert.Equal (
  317. justifiedText,
  318. TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth)
  319. );
  320. int expectedClippedWidth = Math.Min (justifiedText.GetRuneCount (), maxWidth);
  321. Assert.Equal (
  322. justifiedText,
  323. TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth)
  324. );
  325. Assert.True (justifiedText.GetRuneCount () <= maxWidth);
  326. Assert.True (justifiedText.GetColumns () <= maxWidth);
  327. Assert.Equal (expectedClippedWidth, justifiedText.GetRuneCount ());
  328. Assert.Equal (
  329. expectedClippedWidth,
  330. justifiedText.ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 1))
  331. );
  332. Assert.True (expectedClippedWidth <= maxWidth);
  333. Assert.Equal (
  334. StringExtensions.ToString (justifiedText.ToRunes () [..expectedClippedWidth]),
  335. justifiedText
  336. );
  337. }
  338. [Theory]
  339. [InlineData (14, 1, TextDirection.LeftRight_TopBottom, "Les Misęrables")]
  340. [InlineData (1, 14, TextDirection.TopBottom_LeftRight, "L\ne\ns\n \nM\ni\ns\nę\nr\na\nb\nl\ne\ns")]
  341. [InlineData (
  342. 4,
  343. 4,
  344. TextDirection.TopBottom_LeftRight,
  345. @"
  346. LMre
  347. eias
  348. ssb
  349. ęl "
  350. )]
  351. public void Draw_With_Combining_Runes (int width, int height, TextDirection textDirection, string expected)
  352. {
  353. var driver = new FakeDriver ();
  354. driver.Init ();
  355. var text = "Les Mise\u0328\u0301rables";
  356. var tf = new TextFormatter ();
  357. tf.Direction = textDirection;
  358. tf.Text = text;
  359. Assert.True (tf.WordWrap);
  360. if (textDirection == TextDirection.LeftRight_TopBottom)
  361. {
  362. Assert.Equal (new (width, height), tf.Size);
  363. }
  364. else
  365. {
  366. Assert.Equal (new (1, text.GetColumns ()), tf.Size);
  367. tf.Size = new (width, height);
  368. }
  369. tf.Draw (
  370. new (0, 0, width, height),
  371. new Attribute (ColorName.White, ColorName.Black),
  372. new Attribute (ColorName.Blue, ColorName.Black),
  373. default (Rectangle),
  374. driver
  375. );
  376. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
  377. driver.End ();
  378. }
  379. [Fact]
  380. [SetupFakeDriver]
  381. public void FillRemaining_True_False ()
  382. {
  383. ((FakeDriver)Application.Driver).SetBufferSize (22, 5);
  384. Attribute [] attrs =
  385. {
  386. Attribute.Default, new Attribute (ColorName.Green, ColorName.BrightMagenta),
  387. new Attribute (ColorName.Blue, ColorName.Cyan)
  388. };
  389. var tf = new TextFormatter { Size = new (14, 3), Text = "Test\nTest long\nTest long long\n", MultiLine = true };
  390. tf.Draw (
  391. new (1, 1, 19, 3),
  392. attrs [1],
  393. attrs [2]);
  394. Assert.False (tf.FillRemaining);
  395. TestHelpers.AssertDriverContentsWithFrameAre (
  396. @"
  397. Test
  398. Test long
  399. Test long long",
  400. _output);
  401. TestHelpers.AssertDriverAttributesAre (
  402. @"
  403. 000000000000000000000
  404. 011110000000000000000
  405. 011111111100000000000
  406. 011111111111111000000
  407. 000000000000000000000",
  408. null,
  409. attrs);
  410. tf.FillRemaining = true;
  411. tf.Draw (
  412. new (1, 1, 19, 3),
  413. attrs [1],
  414. attrs [2]);
  415. TestHelpers.AssertDriverAttributesAre (
  416. @"
  417. 000000000000000000000
  418. 011111111111111111110
  419. 011111111111111111110
  420. 011111111111111111110
  421. 000000000000000000000",
  422. null,
  423. attrs);
  424. }
  425. [Theory]
  426. [InlineData ("_k Before", true, 0, (KeyCode)'K')] // lower case should return uppercase Hotkey
  427. [InlineData ("a_k Second", true, 1, (KeyCode)'K')]
  428. [InlineData ("Last _k", true, 5, (KeyCode)'K')]
  429. [InlineData ("After k_", false, -1, KeyCode.Null)]
  430. [InlineData ("Multiple _k and _R", true, 9, (KeyCode)'K')]
  431. [InlineData ("Non-english: _кдать", true, 13, (KeyCode)'к')] // Lower case Cryllic K (к)
  432. [InlineData ("_k Before", true, 0, (KeyCode)'K', true)] // Turn on FirstUpperCase and verify same results
  433. [InlineData ("a_k Second", true, 1, (KeyCode)'K', true)]
  434. [InlineData ("Last _k", true, 5, (KeyCode)'K', true)]
  435. [InlineData ("After k_", false, -1, KeyCode.Null, true)]
  436. [InlineData ("Multiple _k and _r", true, 9, (KeyCode)'K', true)]
  437. [InlineData ("Non-english: _кдать", true, 13, (KeyCode)'к', true)] // Cryllic K (К)
  438. public void FindHotKey_AlphaLowerCase_Succeeds (
  439. string text,
  440. bool expectedResult,
  441. int expectedHotPos,
  442. KeyCode expectedKey,
  443. bool supportFirstUpperCase = false
  444. )
  445. {
  446. var hotKeySpecifier = (Rune)'_';
  447. bool result = TextFormatter.FindHotKey (
  448. text,
  449. hotKeySpecifier,
  450. out int hotPos,
  451. out Key hotKey,
  452. supportFirstUpperCase
  453. );
  454. if (expectedResult)
  455. {
  456. Assert.True (result);
  457. }
  458. else
  459. {
  460. Assert.False (result);
  461. }
  462. Assert.Equal (expectedResult, result);
  463. Assert.Equal (expectedHotPos, hotPos);
  464. Assert.Equal (expectedKey, hotKey);
  465. }
  466. [Theory]
  467. [InlineData ("_K Before", true, 0, (KeyCode)'K')]
  468. [InlineData ("a_K Second", true, 1, (KeyCode)'K')]
  469. [InlineData ("Last _K", true, 5, (KeyCode)'K')]
  470. [InlineData ("After K_", false, -1, KeyCode.Null)]
  471. [InlineData ("Multiple _K and _R", true, 9, (KeyCode)'K')]
  472. [InlineData ("Non-english: _Кдать", true, 13, (KeyCode)'К')] // Cryllic K (К)
  473. [InlineData ("_K Before", true, 0, (KeyCode)'K', true)] // Turn on FirstUpperCase and verify same results
  474. [InlineData ("a_K Second", true, 1, (KeyCode)'K', true)]
  475. [InlineData ("Last _K", true, 5, (KeyCode)'K', true)]
  476. [InlineData ("After K_", false, -1, KeyCode.Null, true)]
  477. [InlineData ("Multiple _K and _R", true, 9, (KeyCode)'K', true)]
  478. [InlineData ("Non-english: _Кдать", true, 13, (KeyCode)'К', true)] // Cryllic K (К)
  479. public void FindHotKey_AlphaUpperCase_Succeeds (
  480. string text,
  481. bool expectedResult,
  482. int expectedHotPos,
  483. KeyCode expectedKey,
  484. bool supportFirstUpperCase = false
  485. )
  486. {
  487. var hotKeySpecifier = (Rune)'_';
  488. bool result = TextFormatter.FindHotKey (
  489. text,
  490. hotKeySpecifier,
  491. out int hotPos,
  492. out Key hotKey,
  493. supportFirstUpperCase
  494. );
  495. if (expectedResult)
  496. {
  497. Assert.True (result);
  498. }
  499. else
  500. {
  501. Assert.False (result);
  502. }
  503. Assert.Equal (expectedResult, result);
  504. Assert.Equal (expectedHotPos, hotPos);
  505. Assert.Equal (expectedKey, hotKey);
  506. }
  507. [Theory]
  508. [InlineData (null)]
  509. [InlineData ("")]
  510. [InlineData ("no hotkey")]
  511. [InlineData ("No hotkey, Upper Case")]
  512. [InlineData ("Non-english: Сохранить")]
  513. public void FindHotKey_Invalid_ReturnsFalse (string text)
  514. {
  515. var hotKeySpecifier = (Rune)'_';
  516. var supportFirstUpperCase = false;
  517. var hotPos = 0;
  518. Key hotKey = KeyCode.Null;
  519. var result = false;
  520. result = TextFormatter.FindHotKey (
  521. text,
  522. hotKeySpecifier,
  523. out hotPos,
  524. out hotKey,
  525. supportFirstUpperCase
  526. );
  527. Assert.False (result);
  528. Assert.Equal (-1, hotPos);
  529. Assert.Equal (KeyCode.Null, hotKey);
  530. }
  531. [Theory]
  532. [InlineData ("\"k before")]
  533. [InlineData ("ak second")]
  534. [InlineData ("last k")]
  535. [InlineData ("multiple k and r")]
  536. [InlineData ("12345")]
  537. [InlineData ("`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?")] // punctuation
  538. [InlineData (" ~  s  gui.cs   master ↑10")] // ~IsLetterOrDigit + Unicode
  539. [InlineData ("non-english: кдать")] // Lower case Cryllic K (к)
  540. public void FindHotKey_Legacy_FirstUpperCase_NotFound_Returns_False (string text)
  541. {
  542. var supportFirstUpperCase = true;
  543. var hotKeySpecifier = (Rune)0;
  544. bool result = TextFormatter.FindHotKey (
  545. text,
  546. hotKeySpecifier,
  547. out int hotPos,
  548. out Key hotKey,
  549. supportFirstUpperCase
  550. );
  551. Assert.False (result);
  552. Assert.Equal (-1, hotPos);
  553. Assert.Equal (KeyCode.Null, hotKey);
  554. }
  555. [Theory]
  556. [InlineData ("K Before", true, 0, (KeyCode)'K')]
  557. [InlineData ("aK Second", true, 1, (KeyCode)'K')]
  558. [InlineData ("last K", true, 5, (KeyCode)'K')]
  559. [InlineData ("multiple K and R", true, 9, (KeyCode)'K')]
  560. [InlineData ("non-english: Кдать", true, 13, (KeyCode)'К')] // Cryllic K (К)
  561. public void FindHotKey_Legacy_FirstUpperCase_Succeeds (
  562. string text,
  563. bool expectedResult,
  564. int expectedHotPos,
  565. KeyCode expectedKey
  566. )
  567. {
  568. var supportFirstUpperCase = true;
  569. var hotKeySpecifier = (Rune)0;
  570. bool result = TextFormatter.FindHotKey (
  571. text,
  572. hotKeySpecifier,
  573. out int hotPos,
  574. out Key hotKey,
  575. supportFirstUpperCase
  576. );
  577. if (expectedResult)
  578. {
  579. Assert.True (result);
  580. }
  581. else
  582. {
  583. Assert.False (result);
  584. }
  585. Assert.Equal (expectedResult, result);
  586. Assert.Equal (expectedHotPos, hotPos);
  587. Assert.Equal (expectedKey, hotKey);
  588. }
  589. [Theory]
  590. [InlineData ("_1 Before", true, 0, (KeyCode)'1')] // Digits
  591. [InlineData ("a_1 Second", true, 1, (KeyCode)'1')]
  592. [InlineData ("Last _1", true, 5, (KeyCode)'1')]
  593. [InlineData ("After 1_", false, -1, KeyCode.Null)]
  594. [InlineData ("Multiple _1 and _2", true, 9, (KeyCode)'1')]
  595. [InlineData ("_1 Before", true, 0, (KeyCode)'1', true)] // Turn on FirstUpperCase and verify same results
  596. [InlineData ("a_1 Second", true, 1, (KeyCode)'1', true)]
  597. [InlineData ("Last _1", true, 5, (KeyCode)'1', true)]
  598. [InlineData ("After 1_", false, -1, KeyCode.Null, true)]
  599. [InlineData ("Multiple _1 and _2", true, 9, (KeyCode)'1', true)]
  600. public void FindHotKey_Numeric_Succeeds (
  601. string text,
  602. bool expectedResult,
  603. int expectedHotPos,
  604. KeyCode expectedKey,
  605. bool supportFirstUpperCase = false
  606. )
  607. {
  608. var hotKeySpecifier = (Rune)'_';
  609. bool result = TextFormatter.FindHotKey (
  610. text,
  611. hotKeySpecifier,
  612. out int hotPos,
  613. out Key hotKey,
  614. supportFirstUpperCase
  615. );
  616. if (expectedResult)
  617. {
  618. Assert.True (result);
  619. }
  620. else
  621. {
  622. Assert.False (result);
  623. }
  624. Assert.Equal (expectedResult, result);
  625. Assert.Equal (expectedHotPos, hotPos);
  626. Assert.Equal (expectedKey, hotKey);
  627. }
  628. [Theory]
  629. [InlineData ("_\"k before", true, (KeyCode)'"')] // BUGBUG: Not sure why this fails. " is a normal char
  630. [InlineData ("\"_k before", true, KeyCode.K)]
  631. [InlineData ("_`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?", true, (KeyCode)'`')]
  632. [InlineData ("`_~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?", true, (KeyCode)'~')]
  633. [InlineData (
  634. "`~!@#$%^&*()-__=+[{]}\\|;:'\",<.>/?",
  635. true,
  636. (KeyCode)'='
  637. )] // BUGBUG: Not sure why this fails. Ignore the first and consider the second
  638. [InlineData ("_ ~  s  gui.cs   master ↑10", true, (KeyCode)'')] // ~IsLetterOrDigit + Unicode
  639. [InlineData (" ~  s  gui.cs  _ master ↑10", true, (KeyCode)'')] // ~IsLetterOrDigit + Unicode
  640. [InlineData ("non-english: _кдать", true, (KeyCode)'к')] // Lower case Cryllic K (к)
  641. public void FindHotKey_Symbols_Returns_Symbol (string text, bool found, KeyCode expected)
  642. {
  643. var hotKeySpecifier = (Rune)'_';
  644. bool result = TextFormatter.FindHotKey (text, hotKeySpecifier, out int _, out Key hotKey);
  645. Assert.Equal (found, result);
  646. Assert.Equal (expected, hotKey);
  647. }
  648. [Fact]
  649. public void Format_Dont_Throw_ArgumentException_With_WordWrap_As_False_And_Keep_End_Spaces_As_True ()
  650. {
  651. Exception exception = Record.Exception (
  652. () =>
  653. TextFormatter.Format (
  654. "Some text",
  655. 4,
  656. TextAlignment.Left,
  657. false,
  658. true
  659. )
  660. );
  661. Assert.Null (exception);
  662. }
  663. [Theory]
  664. [InlineData (
  665. "Hello world, how are you today? Pretty neat!",
  666. 44,
  667. 80,
  668. "Hello world, how are you today? Pretty neat!"
  669. )]
  670. public void Format_Justified_Always_Returns_Text_Width_Equal_To_Passed_Width_Horizontal (
  671. string text,
  672. int runeCount,
  673. int maxWidth,
  674. string justifiedText
  675. )
  676. {
  677. Assert.Equal (runeCount, text.GetRuneCount ());
  678. var fmtText = string.Empty;
  679. for (int i = text.GetRuneCount (); i < maxWidth; i++)
  680. {
  681. fmtText = TextFormatter.Format (text, i, TextAlignment.Justified, false, true) [0];
  682. Assert.Equal (i, fmtText.GetRuneCount ());
  683. char c = fmtText [^1];
  684. Assert.True (text.EndsWith (c));
  685. }
  686. Assert.Equal (justifiedText, fmtText);
  687. }
  688. [Theory]
  689. [InlineData (
  690. "Hello world, how are you today? Pretty neat!",
  691. 44,
  692. 80,
  693. "Hello world, how are you today? Pretty neat!"
  694. )]
  695. public void Format_Justified_Always_Returns_Text_Width_Equal_To_Passed_Width_Vertical (
  696. string text,
  697. int runeCount,
  698. int maxWidth,
  699. string justifiedText
  700. )
  701. {
  702. Assert.Equal (runeCount, text.GetRuneCount ());
  703. var fmtText = string.Empty;
  704. for (int i = text.GetRuneCount (); i < maxWidth; i++)
  705. {
  706. fmtText = TextFormatter.Format (
  707. text,
  708. i,
  709. TextAlignment.Justified,
  710. false,
  711. true,
  712. 0,
  713. TextDirection.TopBottom_LeftRight
  714. ) [0];
  715. Assert.Equal (i, fmtText.GetRuneCount ());
  716. char c = fmtText [^1];
  717. Assert.True (text.EndsWith (c));
  718. }
  719. Assert.Equal (justifiedText, fmtText);
  720. }
  721. [Theory]
  722. [InlineData ("Truncate", 3, "Tru")]
  723. [InlineData ("デモエムポンズ", 3, "デ")]
  724. public void Format_Truncate_Simple_And_Wide_Runes (string text, int width, string expected)
  725. {
  726. List<string> list = TextFormatter.Format (text, width, false, false);
  727. Assert.Equal (expected, list [^1]);
  728. }
  729. [Theory]
  730. [MemberData (nameof (FormatEnvironmentNewLine))]
  731. public void Format_With_PreserveTrailingSpaces_And_Without_PreserveTrailingSpaces (
  732. string text,
  733. int width,
  734. IEnumerable<string> expected
  735. )
  736. {
  737. var preserveTrailingSpaces = false;
  738. List<string> formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
  739. Assert.Equal (expected, formated);
  740. preserveTrailingSpaces = true;
  741. formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
  742. Assert.Equal (expected, formated);
  743. }
  744. [Theory]
  745. [InlineData (
  746. " A sentence has words. \n This is the second Line - 2. ",
  747. 4,
  748. -50,
  749. TextAlignment.Left,
  750. true,
  751. false,
  752. new [] { " A", "sent", "ence", "has", "word", "s. ", " Thi", "s is", "the", "seco", "nd", "Line", "- 2." },
  753. " Asentencehaswords. This isthesecondLine- 2."
  754. )]
  755. [InlineData (
  756. " A sentence has words. \n This is the second Line - 2. ",
  757. 4,
  758. -50,
  759. TextAlignment.Left,
  760. true,
  761. true,
  762. new []
  763. {
  764. " A ",
  765. "sent",
  766. "ence",
  767. " ",
  768. "has ",
  769. "word",
  770. "s. ",
  771. " ",
  772. "This",
  773. " is ",
  774. "the ",
  775. "seco",
  776. "nd ",
  777. "Line",
  778. " - ",
  779. "2. "
  780. },
  781. " A sentence has words. This is the second Line - 2. "
  782. )]
  783. public void Format_WordWrap_PreserveTrailingSpaces (
  784. string text,
  785. int maxWidth,
  786. int widthOffset,
  787. TextAlignment textAlignment,
  788. bool wrap,
  789. bool preserveTrailingSpaces,
  790. IEnumerable<string> resultLines,
  791. string expectedWrappedText
  792. )
  793. {
  794. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  795. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  796. Assert.Equal (list.Count, resultLines.Count ());
  797. Assert.Equal (resultLines, list);
  798. var wrappedText = string.Empty;
  799. foreach (string txt in list)
  800. {
  801. wrappedText += txt;
  802. }
  803. Assert.Equal (expectedWrappedText, wrappedText);
  804. }
  805. [Theory]
  806. [InlineData ("Hello World", 11)]
  807. [InlineData ("こんにちは世界", 14)]
  808. public void GetColumns_Simple_And_Wide_Runes (string text, int width) { Assert.Equal (width, text.GetColumns ()); }
  809. [Theory]
  810. [InlineData ("Hello World", 6, 6)]
  811. [InlineData ("こんにちは 世界", 6, 3)]
  812. [MemberData (nameof (CMGlyphs))]
  813. public void GetLengthThatFits_List_Simple_And_Wide_Runes (string text, int columns, int expectedLength)
  814. {
  815. List<Rune> runes = text.ToRuneList ();
  816. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (runes, columns));
  817. }
  818. [Theory]
  819. [InlineData ("test", 3, 3)]
  820. [InlineData ("test", 4, 4)]
  821. [InlineData ("test", 10, 4)]
  822. public void GetLengthThatFits_Runelist (string text, int columns, int expectedLength)
  823. {
  824. List<Rune> runes = text.ToRuneList ();
  825. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (runes, columns));
  826. }
  827. [Theory]
  828. [InlineData ("Hello World", 6, 6)]
  829. [InlineData ("こんにちは 世界", 6, 3)]
  830. public void GetLengthThatFits_Simple_And_Wide_Runes (string text, int columns, int expectedLength)
  831. {
  832. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  833. }
  834. [Theory]
  835. [InlineData ("test", 3, 3)]
  836. [InlineData ("test", 4, 4)]
  837. [InlineData ("test", 10, 4)]
  838. [InlineData ("test", 1, 1)]
  839. [InlineData ("test", 0, 0)]
  840. [InlineData ("test", -1, 0)]
  841. [InlineData (null, -1, 0)]
  842. [InlineData ("", -1, 0)]
  843. public void GetLengthThatFits_String (string text, int columns, int expectedLength)
  844. {
  845. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  846. }
  847. [Fact]
  848. public void GetLengthThatFits_With_Combining_Runes ()
  849. {
  850. var text = "Les Mise\u0328\u0301rables";
  851. Assert.Equal (16, TextFormatter.GetLengthThatFits (text, 14));
  852. }
  853. [Fact]
  854. public void GetMaxColsForWidth_With_Combining_Runes ()
  855. {
  856. List<string> text = new () { "Les Mis", "e\u0328\u0301", "rables" };
  857. Assert.Equal (1, TextFormatter.GetMaxColsForWidth (text, 1));
  858. }
  859. [Theory]
  860. [InlineData (new [] { "0123456789" }, 1)]
  861. [InlineData (new [] { "Hello World" }, 1)]
  862. [InlineData (new [] { "Hello", "World" }, 2)]
  863. [InlineData (new [] { "こんにちは", "世界" }, 4)]
  864. public void GetColumnsRequiredForVerticalText_List_GetsWidth (IEnumerable<string> text, int expectedWidth)
  865. {
  866. Assert.Equal (expectedWidth, TextFormatter.GetColumnsRequiredForVerticalText (text.ToList ()));
  867. }
  868. [Theory]
  869. [InlineData (new [] { "Hello World" }, 1, 0, 1, 1)]
  870. [InlineData (new [] { "Hello", "World" }, 2, 1, 1, 1)]
  871. [InlineData (new [] { "こんにちは", "世界" }, 4, 1, 1, 2)]
  872. public void GetColumnsRequiredForVerticalText_List_Simple_And_Wide_Runes (
  873. IEnumerable<string> text,
  874. int expectedWidth,
  875. int index,
  876. int length,
  877. int expectedIndexWidth
  878. )
  879. {
  880. Assert.Equal (expectedWidth, TextFormatter.GetColumnsRequiredForVerticalText (text.ToList ()));
  881. Assert.Equal (expectedIndexWidth, TextFormatter.GetColumnsRequiredForVerticalText (text.ToList (), index, length));
  882. }
  883. [Fact]
  884. public void GetColumnsRequiredForVerticalText_List_With_Combining_Runes ()
  885. {
  886. List<string> text = new () { "Les Mis", "e\u0328\u0301", "rables" };
  887. Assert.Equal (1, TextFormatter.GetColumnsRequiredForVerticalText (text, 1, 1));
  888. }
  889. [Fact]
  890. public void GetColumnsRequiredForVerticalText_With_Combining_Runes ()
  891. {
  892. var text = "Les Mise\u0328\u0301rables";
  893. Assert.Equal (1, TextFormatter.GetWidestLineLength (text, 1, 1));
  894. }
  895. [Fact]
  896. public void Internal_Tests ()
  897. {
  898. var tf = new TextFormatter ();
  899. Assert.Equal (KeyCode.Null, tf.HotKey);
  900. tf.HotKey = KeyCode.CtrlMask | KeyCode.Q;
  901. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, tf.HotKey);
  902. }
  903. [Theory]
  904. [InlineData ("")]
  905. [InlineData (null)]
  906. [InlineData ("test")]
  907. public void Justify_Invalid (string text)
  908. {
  909. Assert.Equal (text, TextFormatter.Justify (text, 0));
  910. Assert.Equal (text, TextFormatter.Justify (text, 0));
  911. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  912. }
  913. [Theory]
  914. // Even # of spaces
  915. // 0123456789
  916. [InlineData ("012 456 89", "012 456 89", 10, 0, "+", true)]
  917. [InlineData ("012 456 89", "012++456+89", 11, 1)]
  918. [InlineData ("012 456 89", "012 456 89", 12, 2, "++", true)]
  919. [InlineData ("012 456 89", "012+++456++89", 13, 3)]
  920. [InlineData ("012 456 89", "012 456 89", 14, 4, "+++", true)]
  921. [InlineData ("012 456 89", "012++++456+++89", 15, 5)]
  922. [InlineData ("012 456 89", "012 456 89", 16, 6, "++++", true)]
  923. [InlineData ("012 456 89", "012 456 89", 30, 20, "+++++++++++", true)]
  924. [InlineData ("012 456 89", "012+++++++++++++456++++++++++++89", 33, 23)]
  925. // Odd # of spaces
  926. // 01234567890123
  927. [InlineData ("012 456 89 end", "012 456 89 end", 14, 0, "+", true)]
  928. [InlineData ("012 456 89 end", "012++456+89+end", 15, 1)]
  929. [InlineData ("012 456 89 end", "012++456++89+end", 16, 2)]
  930. [InlineData ("012 456 89 end", "012 456 89 end", 17, 3, "++", true)]
  931. [InlineData ("012 456 89 end", "012+++456++89++end", 18, 4)]
  932. [InlineData ("012 456 89 end", "012+++456+++89++end", 19, 5)]
  933. [InlineData ("012 456 89 end", "012 456 89 end", 20, 6, "+++", true)]
  934. [InlineData ("012 456 89 end", "012++++++++456++++++++89+++++++end", 34, 20)]
  935. [InlineData ("012 456 89 end", "012+++++++++456+++++++++89++++++++end", 37, 23)]
  936. // Unicode
  937. // Even # of chars
  938. // 0123456789
  939. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 10, 0, "+", true)]
  940. [InlineData ("пÑРвРÑ", "пÑÐ++вÐ+Ñ", 11, 1)]
  941. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 12, 2, "++", true)]
  942. [InlineData ("пÑРвРÑ", "пÑÐ+++вÐ++Ñ", 13, 3)]
  943. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 14, 4, "+++", true)]
  944. [InlineData ("пÑРвРÑ", "пÑÐ++++вÐ+++Ñ", 15, 5)]
  945. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 16, 6, "++++", true)]
  946. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 30, 20, "+++++++++++", true)]
  947. [InlineData ("пÑРвРÑ", "пÑÐ+++++++++++++вÐ++++++++++++Ñ", 33, 23)]
  948. // Unicode
  949. // Odd # of chars
  950. // 0123456789
  951. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 10, 0, "+", true)]
  952. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ+вÐ+Ñ", 11, 1)]
  953. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ++вÐ+Ñ", 12, 2)]
  954. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 13, 3, "++", true)]
  955. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ++вÐ++Ñ", 14, 4)]
  956. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ+++вÐ++Ñ", 15, 5)]
  957. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 16, 6, "+++", true)]
  958. [InlineData ("Ð ÑРвРÑ", "Ð++++++++ÑÐ++++++++вÐ+++++++Ñ", 30, 20)]
  959. [InlineData ("Ð ÑРвРÑ", "Ð+++++++++ÑÐ+++++++++вÐ++++++++Ñ", 33, 23)]
  960. public void Justify_Sentence (
  961. string text,
  962. string justifiedText,
  963. int forceToWidth,
  964. int widthOffset,
  965. string replaceWith = null,
  966. bool replace = false
  967. )
  968. {
  969. var fillChar = '+';
  970. Assert.Equal (forceToWidth, text.GetRuneCount () + widthOffset);
  971. if (replace)
  972. {
  973. justifiedText = text.Replace (" ", replaceWith);
  974. }
  975. Assert.Equal (justifiedText, TextFormatter.Justify (text, forceToWidth, fillChar));
  976. Assert.True (Math.Abs (forceToWidth - justifiedText.GetRuneCount ()) < text.Count (s => s == ' '));
  977. Assert.True (Math.Abs (forceToWidth - justifiedText.GetColumns ()) < text.Count (s => s == ' '));
  978. }
  979. [Theory]
  980. [InlineData ("word")] // Even # of chars
  981. [InlineData ("word.")] // Odd # of chars
  982. [InlineData ("пÑивеÑ")] // Unicode (even #)
  983. [InlineData ("пÑивеÑ.")] // Unicode (odd # of chars)
  984. public void Justify_SingleWord (string text)
  985. {
  986. string justifiedText = text;
  987. var fillChar = '+';
  988. int width = text.GetRuneCount ();
  989. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  990. width = text.GetRuneCount () + 1;
  991. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  992. width = text.GetRuneCount () + 2;
  993. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  994. width = text.GetRuneCount () + 10;
  995. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  996. width = text.GetRuneCount () + 11;
  997. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  998. }
  999. [Theory]
  1000. [InlineData ("Single Line 界", 14)]
  1001. [InlineData ("First Line 界\nSecond Line 界\nThird Line 界\n", 14)]
  1002. public void MaxWidthLine_With_And_Without_Newlines (string text, int expected) { Assert.Equal (expected, TextFormatter.GetWidestLineLength (text)); }
  1003. [Theory]
  1004. [InlineData (
  1005. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1006. 0,
  1007. 0,
  1008. false,
  1009. new [] { "" }
  1010. )]
  1011. [InlineData (
  1012. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1013. 0,
  1014. 1,
  1015. false,
  1016. new [] { "" }
  1017. )]
  1018. [InlineData (
  1019. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1020. 1,
  1021. 0,
  1022. false,
  1023. new [] { "" }
  1024. )]
  1025. [InlineData (
  1026. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1027. 0,
  1028. 0,
  1029. true,
  1030. new [] { "" }
  1031. )]
  1032. [InlineData (
  1033. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1034. 0,
  1035. 1,
  1036. true,
  1037. new [] { "" }
  1038. )]
  1039. [InlineData (
  1040. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1041. 1,
  1042. 0,
  1043. true,
  1044. new [] { "" }
  1045. )]
  1046. [InlineData (
  1047. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1048. 6,
  1049. 5,
  1050. false,
  1051. new [] { "First " }
  1052. )]
  1053. [InlineData ("1\n2\n3\n4\n5\n6", 6, 5, false, new [] { "1 2 3 " })]
  1054. [InlineData (
  1055. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1056. 6,
  1057. 5,
  1058. true,
  1059. new [] { "First ", "Second", "Third ", "Forty ", "Fiftee" }
  1060. )]
  1061. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, false, new [] { "第一" })]
  1062. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, true, new [] { "第一", "第二", "第三", "四十", "第十" })]
  1063. public void MultiLine_WordWrap_False_Horizontal_Direction (
  1064. string text,
  1065. int maxWidth,
  1066. int maxHeight,
  1067. bool multiLine,
  1068. IEnumerable<string> resultLines
  1069. )
  1070. {
  1071. var tf = new TextFormatter
  1072. {
  1073. Text = text, Size = new (maxWidth, maxHeight), WordWrap = false, MultiLine = multiLine
  1074. };
  1075. Assert.False (tf.AutoSize);
  1076. Assert.False (tf.WordWrap);
  1077. Assert.True (tf.MultiLine == multiLine);
  1078. Assert.Equal (TextDirection.LeftRight_TopBottom, tf.Direction);
  1079. List<string> splitLines = tf.GetLines ();
  1080. Assert.Equal (splitLines.Count, resultLines.Count ());
  1081. Assert.Equal (splitLines, resultLines);
  1082. }
  1083. [Theory]
  1084. [InlineData (
  1085. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1086. 0,
  1087. 0,
  1088. false,
  1089. new [] { "" }
  1090. )]
  1091. [InlineData (
  1092. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1093. 0,
  1094. 1,
  1095. false,
  1096. new [] { "" }
  1097. )]
  1098. [InlineData (
  1099. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1100. 1,
  1101. 0,
  1102. false,
  1103. new [] { "" }
  1104. )]
  1105. [InlineData (
  1106. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1107. 0,
  1108. 0,
  1109. true,
  1110. new [] { "" }
  1111. )]
  1112. [InlineData (
  1113. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1114. 0,
  1115. 1,
  1116. true,
  1117. new [] { "" }
  1118. )]
  1119. [InlineData (
  1120. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1121. 1,
  1122. 0,
  1123. true,
  1124. new [] { "" }
  1125. )]
  1126. [InlineData (
  1127. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1128. 6,
  1129. 5,
  1130. false,
  1131. new [] { "First" }
  1132. )]
  1133. [InlineData ("1\n2\n3\n4\n5\n6", 6, 5, false, new [] { "1 2 3" })]
  1134. [InlineData (
  1135. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1136. 6,
  1137. 5,
  1138. true,
  1139. new [] { "First", "Secon", "Third", "Forty", "Fifte", "Seven" }
  1140. )]
  1141. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, false, new [] { "第一行 第" })]
  1142. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, true, new [] { "第一行", "第二行" })]
  1143. public void MultiLine_WordWrap_False_Vertical_Direction (
  1144. string text,
  1145. int maxWidth,
  1146. int maxHeight,
  1147. bool multiLine,
  1148. IEnumerable<string> resultLines
  1149. )
  1150. {
  1151. var tf = new TextFormatter
  1152. {
  1153. Text = text,
  1154. Size = new (maxWidth, maxHeight),
  1155. WordWrap = false,
  1156. MultiLine = multiLine,
  1157. Direction = TextDirection.TopBottom_LeftRight
  1158. };
  1159. Assert.False (tf.AutoSize);
  1160. Assert.False (tf.WordWrap);
  1161. Assert.True (tf.MultiLine == multiLine);
  1162. Assert.Equal (TextDirection.TopBottom_LeftRight, tf.Direction);
  1163. List<string> splitLines = tf.GetLines ();
  1164. Assert.Equal (splitLines.Count, resultLines.Count ());
  1165. Assert.Equal (splitLines, resultLines);
  1166. }
  1167. [Fact]
  1168. public void NeedsFormat_Sets ()
  1169. {
  1170. var testText = "test";
  1171. var testBounds = new Rectangle (0, 0, 100, 1);
  1172. var tf = new TextFormatter ();
  1173. tf.Text = "test";
  1174. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  1175. Assert.NotEmpty (tf.GetLines ());
  1176. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1177. Assert.Equal (testText, tf.Text);
  1178. tf.Draw (testBounds, new Attribute (), new Attribute ());
  1179. Assert.False (tf.NeedsFormat);
  1180. tf.Size = new (1, 1);
  1181. Assert.True (tf.NeedsFormat);
  1182. Assert.NotEmpty (tf.GetLines ());
  1183. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1184. tf.Alignment = TextAlignment.Centered;
  1185. Assert.True (tf.NeedsFormat);
  1186. Assert.NotEmpty (tf.GetLines ());
  1187. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1188. }
  1189. [Theory]
  1190. [InlineData ("", -1, TextAlignment.Left, false, 0)]
  1191. [InlineData (null, 0, TextAlignment.Left, false, 1)]
  1192. [InlineData (null, 0, TextAlignment.Left, true, 1)]
  1193. [InlineData ("", 0, TextAlignment.Left, false, 1)]
  1194. [InlineData ("", 0, TextAlignment.Left, true, 1)]
  1195. public void Reformat_Invalid (string text, int maxWidth, TextAlignment textAlignment, bool wrap, int linesCount)
  1196. {
  1197. if (maxWidth < 0)
  1198. {
  1199. Assert.Throws<ArgumentOutOfRangeException> (
  1200. () =>
  1201. TextFormatter.Format (text, maxWidth, textAlignment, wrap)
  1202. );
  1203. }
  1204. else
  1205. {
  1206. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1207. Assert.NotEmpty (list);
  1208. Assert.True (list.Count == linesCount);
  1209. Assert.Equal (string.Empty, list [0]);
  1210. }
  1211. }
  1212. [Theory]
  1213. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true)]
  1214. [InlineData ("A sentence has words.\nLine 2.", 1, -28, TextAlignment.Left, false, 1, false)]
  1215. [InlineData ("A sentence has words.\nLine 2.", 5, -24, TextAlignment.Left, false, 1, false)]
  1216. [InlineData ("A sentence has words.\nLine 2.", 28, -1, TextAlignment.Left, false, 1, false)]
  1217. // no clip
  1218. [InlineData ("A sentence has words.\nLine 2.", 29, 0, TextAlignment.Left, false, 1, false)]
  1219. [InlineData ("A sentence has words.\nLine 2.", 30, 1, TextAlignment.Left, false, 1, false)]
  1220. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true)]
  1221. [InlineData ("A sentence has words.\r\nLine 2.", 1, -29, TextAlignment.Left, false, 1, false)]
  1222. [InlineData ("A sentence has words.\r\nLine 2.", 5, -25, TextAlignment.Left, false, 1, false)]
  1223. [InlineData ("A sentence has words.\r\nLine 2.", 29, -1, TextAlignment.Left, false, 1, false, 1)]
  1224. [InlineData ("A sentence has words.\r\nLine 2.", 30, 0, TextAlignment.Left, false, 1, false)]
  1225. [InlineData ("A sentence has words.\r\nLine 2.", 31, 1, TextAlignment.Left, false, 1, false)]
  1226. public void Reformat_NoWordrap_NewLines_MultiLine_False (
  1227. string text,
  1228. int maxWidth,
  1229. int widthOffset,
  1230. TextAlignment textAlignment,
  1231. bool wrap,
  1232. int linesCount,
  1233. bool stringEmpty,
  1234. int clipWidthOffset = 0
  1235. )
  1236. {
  1237. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1238. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth) + clipWidthOffset;
  1239. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1240. Assert.NotEmpty (list);
  1241. Assert.True (list.Count == linesCount);
  1242. if (stringEmpty)
  1243. {
  1244. Assert.Equal (string.Empty, list [0]);
  1245. }
  1246. else
  1247. {
  1248. Assert.NotEqual (string.Empty, list [0]);
  1249. }
  1250. if (text.Contains ("\r\n") && maxWidth > 0)
  1251. {
  1252. Assert.Equal (
  1253. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth])
  1254. .Replace ("\r\n", " "),
  1255. list [0]
  1256. );
  1257. }
  1258. else if (text.Contains ('\n') && maxWidth > 0)
  1259. {
  1260. Assert.Equal (
  1261. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth])
  1262. .Replace ("\n", " "),
  1263. list [0]
  1264. );
  1265. }
  1266. else
  1267. {
  1268. Assert.Equal (StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]), list [0]);
  1269. }
  1270. }
  1271. [Theory]
  1272. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true, new [] { "" })]
  1273. [InlineData (
  1274. "A sentence has words.\nLine 2.",
  1275. 1,
  1276. -28,
  1277. TextAlignment.Left,
  1278. false,
  1279. 2,
  1280. false,
  1281. new [] { "A", "L" }
  1282. )]
  1283. [InlineData (
  1284. "A sentence has words.\nLine 2.",
  1285. 5,
  1286. -24,
  1287. TextAlignment.Left,
  1288. false,
  1289. 2,
  1290. false,
  1291. new [] { "A sen", "Line " }
  1292. )]
  1293. [InlineData (
  1294. "A sentence has words.\nLine 2.",
  1295. 28,
  1296. -1,
  1297. TextAlignment.Left,
  1298. false,
  1299. 2,
  1300. false,
  1301. new [] { "A sentence has words.", "Line 2." }
  1302. )]
  1303. //// no clip
  1304. [InlineData (
  1305. "A sentence has words.\nLine 2.",
  1306. 29,
  1307. 0,
  1308. TextAlignment.Left,
  1309. false,
  1310. 2,
  1311. false,
  1312. new [] { "A sentence has words.", "Line 2." }
  1313. )]
  1314. [InlineData (
  1315. "A sentence has words.\nLine 2.",
  1316. 30,
  1317. 1,
  1318. TextAlignment.Left,
  1319. false,
  1320. 2,
  1321. false,
  1322. new [] { "A sentence has words.", "Line 2." }
  1323. )]
  1324. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true, new [] { "" })]
  1325. [InlineData (
  1326. "A sentence has words.\r\nLine 2.",
  1327. 1,
  1328. -29,
  1329. TextAlignment.Left,
  1330. false,
  1331. 2,
  1332. false,
  1333. new [] { "A", "L" }
  1334. )]
  1335. [InlineData (
  1336. "A sentence has words.\r\nLine 2.",
  1337. 5,
  1338. -25,
  1339. TextAlignment.Left,
  1340. false,
  1341. 2,
  1342. false,
  1343. new [] { "A sen", "Line " }
  1344. )]
  1345. [InlineData (
  1346. "A sentence has words.\r\nLine 2.",
  1347. 29,
  1348. -1,
  1349. TextAlignment.Left,
  1350. false,
  1351. 2,
  1352. false,
  1353. new [] { "A sentence has words.", "Line 2." }
  1354. )]
  1355. [InlineData (
  1356. "A sentence has words.\r\nLine 2.",
  1357. 30,
  1358. 0,
  1359. TextAlignment.Left,
  1360. false,
  1361. 2,
  1362. false,
  1363. new [] { "A sentence has words.", "Line 2." }
  1364. )]
  1365. [InlineData (
  1366. "A sentence has words.\r\nLine 2.",
  1367. 31,
  1368. 1,
  1369. TextAlignment.Left,
  1370. false,
  1371. 2,
  1372. false,
  1373. new [] { "A sentence has words.", "Line 2." }
  1374. )]
  1375. public void Reformat_NoWordrap_NewLines_MultiLine_True (
  1376. string text,
  1377. int maxWidth,
  1378. int widthOffset,
  1379. TextAlignment textAlignment,
  1380. bool wrap,
  1381. int linesCount,
  1382. bool stringEmpty,
  1383. IEnumerable<string> resultLines
  1384. )
  1385. {
  1386. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1387. List<string> list = TextFormatter.Format (
  1388. text,
  1389. maxWidth,
  1390. textAlignment,
  1391. wrap,
  1392. false,
  1393. 0,
  1394. TextDirection.LeftRight_TopBottom,
  1395. true
  1396. );
  1397. Assert.NotEmpty (list);
  1398. Assert.True (list.Count == linesCount);
  1399. if (stringEmpty)
  1400. {
  1401. Assert.Equal (string.Empty, list [0]);
  1402. }
  1403. else
  1404. {
  1405. Assert.NotEqual (string.Empty, list [0]);
  1406. }
  1407. Assert.Equal (list, resultLines);
  1408. }
  1409. [Theory]
  1410. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true, new [] { "" })]
  1411. [InlineData (
  1412. "A sentence has words.\nLine 2.",
  1413. 1,
  1414. -28,
  1415. TextAlignment.Left,
  1416. false,
  1417. 2,
  1418. false,
  1419. new [] { "A", "L" }
  1420. )]
  1421. [InlineData (
  1422. "A sentence has words.\nLine 2.",
  1423. 5,
  1424. -24,
  1425. TextAlignment.Left,
  1426. false,
  1427. 2,
  1428. false,
  1429. new [] { "A sen", "Line " }
  1430. )]
  1431. [InlineData (
  1432. "A sentence has words.\nLine 2.",
  1433. 28,
  1434. -1,
  1435. TextAlignment.Left,
  1436. false,
  1437. 2,
  1438. false,
  1439. new [] { "A sentence has words.", "Line 2." }
  1440. )]
  1441. //// no clip
  1442. [InlineData (
  1443. "A sentence has words.\nLine 2.",
  1444. 29,
  1445. 0,
  1446. TextAlignment.Left,
  1447. false,
  1448. 2,
  1449. false,
  1450. new [] { "A sentence has words.", "Line 2." }
  1451. )]
  1452. [InlineData (
  1453. "A sentence has words.\nLine 2.",
  1454. 30,
  1455. 1,
  1456. TextAlignment.Left,
  1457. false,
  1458. 2,
  1459. false,
  1460. new [] { "A sentence has words.", "Line 2." }
  1461. )]
  1462. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true, new [] { "" })]
  1463. [InlineData (
  1464. "A sentence has words.\r\nLine 2.",
  1465. 1,
  1466. -29,
  1467. TextAlignment.Left,
  1468. false,
  1469. 2,
  1470. false,
  1471. new [] { "A", "L" }
  1472. )]
  1473. [InlineData (
  1474. "A sentence has words.\r\nLine 2.",
  1475. 5,
  1476. -25,
  1477. TextAlignment.Left,
  1478. false,
  1479. 2,
  1480. false,
  1481. new [] { "A sen", "Line " }
  1482. )]
  1483. [InlineData (
  1484. "A sentence has words.\r\nLine 2.",
  1485. 29,
  1486. -1,
  1487. TextAlignment.Left,
  1488. false,
  1489. 2,
  1490. false,
  1491. new [] { "A sentence has words.", "Line 2." }
  1492. )]
  1493. [InlineData (
  1494. "A sentence has words.\r\nLine 2.",
  1495. 30,
  1496. 0,
  1497. TextAlignment.Left,
  1498. false,
  1499. 2,
  1500. false,
  1501. new [] { "A sentence has words.", "Line 2." }
  1502. )]
  1503. [InlineData (
  1504. "A sentence has words.\r\nLine 2.",
  1505. 31,
  1506. 1,
  1507. TextAlignment.Left,
  1508. false,
  1509. 2,
  1510. false,
  1511. new [] { "A sentence has words.", "Line 2." }
  1512. )]
  1513. public void Reformat_NoWordrap_NewLines_MultiLine_True_Vertical (
  1514. string text,
  1515. int maxWidth,
  1516. int widthOffset,
  1517. TextAlignment textAlignment,
  1518. bool wrap,
  1519. int linesCount,
  1520. bool stringEmpty,
  1521. IEnumerable<string> resultLines
  1522. )
  1523. {
  1524. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1525. List<string> list = TextFormatter.Format (
  1526. text,
  1527. maxWidth,
  1528. textAlignment,
  1529. wrap,
  1530. false,
  1531. 0,
  1532. TextDirection.TopBottom_LeftRight,
  1533. true
  1534. );
  1535. Assert.NotEmpty (list);
  1536. Assert.True (list.Count == linesCount);
  1537. if (stringEmpty)
  1538. {
  1539. Assert.Equal (string.Empty, list [0]);
  1540. }
  1541. else
  1542. {
  1543. Assert.NotEqual (string.Empty, list [0]);
  1544. }
  1545. Assert.Equal (list, resultLines);
  1546. }
  1547. [Theory]
  1548. [InlineData ("", 0, 0, TextAlignment.Left, false, 1, true)]
  1549. [InlineData ("", 1, 1, TextAlignment.Left, false, 1, true)]
  1550. [InlineData ("A sentence has words.", 0, -21, TextAlignment.Left, false, 1, true)]
  1551. [InlineData ("A sentence has words.", 1, -20, TextAlignment.Left, false, 1, false)]
  1552. [InlineData ("A sentence has words.", 5, -16, TextAlignment.Left, false, 1, false)]
  1553. [InlineData ("A sentence has words.", 20, -1, TextAlignment.Left, false, 1, false)]
  1554. // no clip
  1555. [InlineData ("A sentence has words.", 21, 0, TextAlignment.Left, false, 1, false)]
  1556. [InlineData ("A sentence has words.", 22, 1, TextAlignment.Left, false, 1, false)]
  1557. public void Reformat_NoWordrap_SingleLine (
  1558. string text,
  1559. int maxWidth,
  1560. int widthOffset,
  1561. TextAlignment textAlignment,
  1562. bool wrap,
  1563. int linesCount,
  1564. bool stringEmpty
  1565. )
  1566. {
  1567. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1568. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1569. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1570. Assert.NotEmpty (list);
  1571. Assert.True (list.Count == linesCount);
  1572. if (stringEmpty)
  1573. {
  1574. Assert.Equal (string.Empty, list [0]);
  1575. }
  1576. else
  1577. {
  1578. Assert.NotEqual (string.Empty, list [0]);
  1579. }
  1580. Assert.Equal (StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]), list [0]);
  1581. }
  1582. [Theory]
  1583. // Unicode
  1584. [InlineData (
  1585. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1586. 8,
  1587. -1,
  1588. TextAlignment.Left,
  1589. true,
  1590. false,
  1591. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1592. )]
  1593. // no clip
  1594. [InlineData (
  1595. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1596. 9,
  1597. 0,
  1598. TextAlignment.Left,
  1599. true,
  1600. false,
  1601. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1602. )]
  1603. [InlineData (
  1604. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1605. 10,
  1606. 1,
  1607. TextAlignment.Left,
  1608. true,
  1609. false,
  1610. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1611. )]
  1612. public void Reformat_Unicode_Wrap_Spaces_NewLines (
  1613. string text,
  1614. int maxWidth,
  1615. int widthOffset,
  1616. TextAlignment textAlignment,
  1617. bool wrap,
  1618. bool preserveTrailingSpaces,
  1619. IEnumerable<string> resultLines
  1620. )
  1621. {
  1622. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1623. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1624. Assert.Equal (list.Count, resultLines.Count ());
  1625. Assert.Equal (resultLines, list);
  1626. }
  1627. [Theory]
  1628. // Unicode
  1629. // Even # of chars
  1630. // 0123456789
  1631. [InlineData ("\u2660пÑРвРÑ", 10, -1, TextAlignment.Left, true, false, new [] { "\u2660пÑРвÐ", "Ñ" })]
  1632. // no clip
  1633. [InlineData ("\u2660пÑРвРÑ", 11, 0, TextAlignment.Left, true, false, new [] { "\u2660пÑРвРÑ" })]
  1634. [InlineData ("\u2660пÑРвРÑ", 12, 1, TextAlignment.Left, true, false, new [] { "\u2660пÑРвРÑ" })]
  1635. // Unicode
  1636. // Odd # of chars
  1637. // 0123456789
  1638. [InlineData ("\u2660 ÑРвРÑ", 9, -1, TextAlignment.Left, true, false, new [] { "\u2660 ÑРвÐ", "Ñ" })]
  1639. // no clip
  1640. [InlineData ("\u2660 ÑРвРÑ", 10, 0, TextAlignment.Left, true, false, new [] { "\u2660 ÑРвРÑ" })]
  1641. [InlineData ("\u2660 ÑРвРÑ", 11, 1, TextAlignment.Left, true, false, new [] { "\u2660 ÑРвРÑ" })]
  1642. public void Reformat_Unicode_Wrap_Spaces_No_NewLines (
  1643. string text,
  1644. int maxWidth,
  1645. int widthOffset,
  1646. TextAlignment textAlignment,
  1647. bool wrap,
  1648. bool preserveTrailingSpaces,
  1649. IEnumerable<string> resultLines
  1650. )
  1651. {
  1652. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1653. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1654. Assert.Equal (list.Count, resultLines.Count ());
  1655. Assert.Equal (resultLines, list);
  1656. }
  1657. [Theory]
  1658. // Even # of spaces
  1659. // 0123456789
  1660. [InlineData ("012 456 89", 0, -10, TextAlignment.Left, true, true, true, new [] { "" })]
  1661. [InlineData (
  1662. "012 456 89",
  1663. 1,
  1664. -9,
  1665. TextAlignment.Left,
  1666. true,
  1667. true,
  1668. false,
  1669. new [] { "0", "1", "2", " ", "4", "5", "6", " ", "8", "9" },
  1670. "01245689"
  1671. )]
  1672. [InlineData ("012 456 89", 5, -5, TextAlignment.Left, true, true, false, new [] { "012 ", "456 ", "89" })]
  1673. [InlineData ("012 456 89", 9, -1, TextAlignment.Left, true, true, false, new [] { "012 456 ", "89" })]
  1674. // no clip
  1675. [InlineData ("012 456 89", 10, 0, TextAlignment.Left, true, true, false, new [] { "012 456 89" })]
  1676. [InlineData ("012 456 89", 11, 1, TextAlignment.Left, true, true, false, new [] { "012 456 89" })]
  1677. // Odd # of spaces
  1678. // 01234567890123
  1679. [InlineData ("012 456 89 end", 13, -1, TextAlignment.Left, true, true, false, new [] { "012 456 89 ", "end" })]
  1680. // no clip
  1681. [InlineData ("012 456 89 end", 14, 0, TextAlignment.Left, true, true, false, new [] { "012 456 89 end" })]
  1682. [InlineData ("012 456 89 end", 15, 1, TextAlignment.Left, true, true, false, new [] { "012 456 89 end" })]
  1683. public void Reformat_Wrap_Spaces_No_NewLines (
  1684. string text,
  1685. int maxWidth,
  1686. int widthOffset,
  1687. TextAlignment textAlignment,
  1688. bool wrap,
  1689. bool preserveTrailingSpaces,
  1690. bool stringEmpty,
  1691. IEnumerable<string> resultLines,
  1692. string noSpaceText = ""
  1693. )
  1694. {
  1695. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1696. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1697. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1698. Assert.NotEmpty (list);
  1699. Assert.True (list.Count == resultLines.Count ());
  1700. if (stringEmpty)
  1701. {
  1702. Assert.Equal (string.Empty, list [0]);
  1703. }
  1704. else
  1705. {
  1706. Assert.NotEqual (string.Empty, list [0]);
  1707. }
  1708. Assert.Equal (resultLines, list);
  1709. if (maxWidth > 0)
  1710. {
  1711. // remove whitespace chars
  1712. if (maxWidth < 5)
  1713. {
  1714. expectedClippedWidth = text.GetRuneCount () - text.Sum (r => r == ' ' ? 1 : 0);
  1715. }
  1716. else
  1717. {
  1718. expectedClippedWidth = Math.Min (
  1719. text.GetRuneCount (),
  1720. maxWidth - text.Sum (r => r == ' ' ? 1 : 0)
  1721. );
  1722. }
  1723. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1724. if (maxWidth == 1)
  1725. {
  1726. Assert.Equal (expectedClippedWidth, list.Count);
  1727. Assert.Equal (noSpaceText, string.Concat (list.ToArray ()));
  1728. }
  1729. if (maxWidth > 1 && maxWidth < 10)
  1730. {
  1731. Assert.Equal (
  1732. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]),
  1733. list [0]
  1734. );
  1735. }
  1736. }
  1737. }
  1738. [Theory]
  1739. [InlineData (null)]
  1740. [InlineData ("")]
  1741. [InlineData ("a")]
  1742. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal (string text)
  1743. {
  1744. var hotKeySpecifier = (Rune)'_';
  1745. if (text == null)
  1746. {
  1747. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  1748. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  1749. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  1750. }
  1751. else
  1752. {
  1753. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  1754. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  1755. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  1756. }
  1757. }
  1758. [Theory]
  1759. [InlineData ("all lower case", 0)]
  1760. [InlineData ("K Before", 0)]
  1761. [InlineData ("aK Second", 1)]
  1762. [InlineData ("Last K", 5)]
  1763. [InlineData ("fter K", 7)]
  1764. [InlineData ("Multiple K and R", 9)]
  1765. [InlineData ("Non-english: Кдать", 13)]
  1766. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal (string text, int hotPos)
  1767. {
  1768. var hotKeySpecifier = (Rune)'_';
  1769. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  1770. }
  1771. [Theory]
  1772. [InlineData ("_K Before", 0, "K Before")]
  1773. [InlineData ("a_K Second", 1, "aK Second")]
  1774. [InlineData ("Last _K", 5, "Last K")]
  1775. [InlineData ("After K_", 7, "After K")]
  1776. [InlineData ("Multiple _K and _R", 9, "Multiple K and _R")]
  1777. [InlineData ("Non-english: _Кдать", 13, "Non-english: Кдать")]
  1778. public void RemoveHotKeySpecifier_Valid_ReturnsStripped (string text, int hotPos, string expectedText)
  1779. {
  1780. var hotKeySpecifier = (Rune)'_';
  1781. Assert.Equal (expectedText, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  1782. }
  1783. [Theory]
  1784. [InlineData ("test", 0, 't', "test")]
  1785. [InlineData ("test", 1, 'e', "test")]
  1786. [InlineData ("Ok", 0, 'O', "Ok")]
  1787. [InlineData ("[◦ Ok ◦]", 3, 'O', "[◦ Ok ◦]")]
  1788. [InlineData ("^k", 0, '^', "^k")]
  1789. public void ReplaceHotKeyWithTag (string text, int hotPos, uint tag, string expected)
  1790. {
  1791. var tf = new TextFormatter ();
  1792. List<Rune> runes = text.ToRuneList ();
  1793. Rune rune;
  1794. if (Rune.TryGetRuneAt (text, hotPos, out rune))
  1795. {
  1796. Assert.Equal (rune, (Rune)tag);
  1797. }
  1798. string result = TextFormatter.ReplaceHotKeyWithTag (text, hotPos);
  1799. Assert.Equal (result, expected);
  1800. Assert.Equal ((Rune)tag, result.ToRunes () [hotPos]);
  1801. Assert.Equal (text.GetRuneCount (), runes.Count);
  1802. Assert.Equal (text, StringExtensions.ToString (runes));
  1803. }
  1804. [Theory]
  1805. [MemberData (nameof (SplitEnvironmentNewLine))]
  1806. public void SplitNewLine_Ending__With_Or_Without_NewLine_Probably_CRLF (
  1807. string text,
  1808. IEnumerable<string> expected
  1809. )
  1810. {
  1811. List<string> splited = TextFormatter.SplitNewLine (text);
  1812. Assert.Equal (expected, splited);
  1813. }
  1814. [Theory]
  1815. [InlineData (
  1816. "First Line 界\nSecond Line 界\nThird Line 界\n",
  1817. new [] { "First Line 界", "Second Line 界", "Third Line 界", "" }
  1818. )]
  1819. public void SplitNewLine_Ending_With_NewLine_Only_LF (string text, IEnumerable<string> expected)
  1820. {
  1821. List<string> splited = TextFormatter.SplitNewLine (text);
  1822. Assert.Equal (expected, splited);
  1823. }
  1824. [Theory]
  1825. [InlineData (
  1826. "First Line 界\nSecond Line 界\nThird Line 界",
  1827. new [] { "First Line 界", "Second Line 界", "Third Line 界" }
  1828. )]
  1829. public void SplitNewLine_Ending_Without_NewLine_Only_LF (string text, IEnumerable<string> expected)
  1830. {
  1831. List<string> splited = TextFormatter.SplitNewLine (text);
  1832. Assert.Equal (expected, splited);
  1833. }
  1834. [Theory]
  1835. [InlineData ("New Test 你", 10, 10, 20320, 20320, 9, "你")]
  1836. [InlineData ("New Test \U0001d539", 10, 11, 120121, 55349, 9, "𝔹")]
  1837. public void String_Array_Is_Not_Always_Equal_ToRunes_Array (
  1838. string text,
  1839. int runesLength,
  1840. int stringLength,
  1841. int runeValue,
  1842. int stringValue,
  1843. int index,
  1844. string expected
  1845. )
  1846. {
  1847. Rune [] usToRunes = text.ToRunes ();
  1848. Assert.Equal (runesLength, usToRunes.Length);
  1849. Assert.Equal (stringLength, text.Length);
  1850. Assert.Equal (runeValue, usToRunes [index].Value);
  1851. Assert.Equal (stringValue, text [index]);
  1852. Assert.Equal (expected, usToRunes [index].ToString ());
  1853. if (char.IsHighSurrogate (text [index]))
  1854. {
  1855. // Rune array length isn't equal to string array
  1856. Assert.Equal (expected, new string (new [] { text [index], text [index + 1] }));
  1857. }
  1858. else
  1859. {
  1860. // Rune array length is equal to string array
  1861. Assert.Equal (expected, text [index].ToString ());
  1862. }
  1863. }
  1864. [Theory]
  1865. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1866. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1867. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1868. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1869. public void TabWith_PreserveTrailingSpaces_False (
  1870. int width,
  1871. int height,
  1872. TextDirection textDirection,
  1873. int tabWidth,
  1874. string expected
  1875. )
  1876. {
  1877. var driver = new FakeDriver ();
  1878. driver.Init ();
  1879. var text = "This is a \tTab";
  1880. var tf = new TextFormatter ();
  1881. tf.Direction = textDirection;
  1882. tf.TabWidth = tabWidth;
  1883. tf.Text = text;
  1884. Assert.True (tf.WordWrap);
  1885. Assert.False (tf.PreserveTrailingSpaces);
  1886. Assert.Equal (new (width, height), tf.Size);
  1887. tf.Draw (
  1888. new (0, 0, width, height),
  1889. new Attribute (ColorName.White, ColorName.Black),
  1890. new Attribute (ColorName.Blue, ColorName.Black),
  1891. default (Rectangle),
  1892. driver
  1893. );
  1894. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
  1895. driver.End ();
  1896. }
  1897. [Theory]
  1898. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1899. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1900. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1901. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1902. public void TabWith_PreserveTrailingSpaces_True (
  1903. int width,
  1904. int height,
  1905. TextDirection textDirection,
  1906. int tabWidth,
  1907. string expected
  1908. )
  1909. {
  1910. var driver = new FakeDriver ();
  1911. driver.Init ();
  1912. var text = "This is a \tTab";
  1913. var tf = new TextFormatter ();
  1914. tf.Direction = textDirection;
  1915. tf.TabWidth = tabWidth;
  1916. tf.PreserveTrailingSpaces = true;
  1917. tf.Text = text;
  1918. Assert.True (tf.WordWrap);
  1919. Assert.Equal (new (width, height), tf.Size);
  1920. tf.Draw (
  1921. new (0, 0, width, height),
  1922. new Attribute (ColorName.White, ColorName.Black),
  1923. new Attribute (ColorName.Blue, ColorName.Black),
  1924. default (Rectangle),
  1925. driver
  1926. );
  1927. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
  1928. driver.End ();
  1929. }
  1930. [Theory]
  1931. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1932. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1933. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1934. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1935. public void TabWith_WordWrap_True (
  1936. int width,
  1937. int height,
  1938. TextDirection textDirection,
  1939. int tabWidth,
  1940. string expected
  1941. )
  1942. {
  1943. var driver = new FakeDriver ();
  1944. driver.Init ();
  1945. var text = "This is a \tTab";
  1946. var tf = new TextFormatter ();
  1947. tf.Direction = textDirection;
  1948. tf.TabWidth = tabWidth;
  1949. tf.WordWrap = true;
  1950. tf.Text = text;
  1951. Assert.False (tf.PreserveTrailingSpaces);
  1952. Assert.Equal (new (width, height), tf.Size);
  1953. tf.Draw (
  1954. new (0, 0, width, height),
  1955. new Attribute (ColorName.White, ColorName.Black),
  1956. new Attribute (ColorName.Blue, ColorName.Black),
  1957. default (Rectangle),
  1958. driver
  1959. );
  1960. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
  1961. driver.End ();
  1962. }
  1963. [Theory]
  1964. [InlineData ("123456789", 3, "123")]
  1965. [InlineData ("Hello World", 8, "Hello Wo")]
  1966. public void TestClipOrPad_LongWord (string text, int fillPad, string expectedText)
  1967. {
  1968. // word is long but we want it to fill # space only
  1969. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1970. }
  1971. [Theory]
  1972. [InlineData ("fff", 6, "fff ")]
  1973. [InlineData ("Hello World", 16, "Hello World ")]
  1974. public void TestClipOrPad_ShortWord (string text, int fillPad, string expectedText)
  1975. {
  1976. // word is short but we want it to fill # so it should be padded
  1977. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1978. }
  1979. [Theory]
  1980. [InlineData (TextDirection.LeftRight_TopBottom)]
  1981. [InlineData (TextDirection.TopBottom_LeftRight)]
  1982. public void TestSize_AutoSizeChange (TextDirection textDirection)
  1983. {
  1984. var tf = new TextFormatter { Direction = textDirection, Text = "你你" };
  1985. if (textDirection == TextDirection.LeftRight_TopBottom)
  1986. {
  1987. Assert.Equal (4, tf.Size.Width);
  1988. Assert.Equal (1, tf.Size.Height);
  1989. }
  1990. else
  1991. {
  1992. Assert.Equal (2, tf.Size.Width);
  1993. Assert.Equal (2, tf.Size.Height);
  1994. }
  1995. Assert.False (tf.AutoSize);
  1996. tf.Size = new (1, 1);
  1997. Assert.Equal (1, tf.Size.Width);
  1998. Assert.Equal (1, tf.Size.Height);
  1999. tf.AutoSize = true;
  2000. if (textDirection == TextDirection.LeftRight_TopBottom)
  2001. {
  2002. Assert.Equal (4, tf.Size.Width);
  2003. Assert.Equal (1, tf.Size.Height);
  2004. }
  2005. else
  2006. {
  2007. Assert.Equal (2, tf.Size.Width);
  2008. Assert.Equal (2, tf.Size.Height);
  2009. }
  2010. }
  2011. [Theory]
  2012. [InlineData (TextAlignment.Left, false)]
  2013. [InlineData (TextAlignment.Centered, true)]
  2014. [InlineData (TextAlignment.Right, false)]
  2015. [InlineData (TextAlignment.Justified, true)]
  2016. public void TestSize_DirectionChange_AutoSize_True_Or_False_Horizontal (
  2017. TextAlignment textAlignment,
  2018. bool autoSize
  2019. )
  2020. {
  2021. var tf = new TextFormatter
  2022. {
  2023. Direction = TextDirection.LeftRight_TopBottom, Text = "你你", Alignment = textAlignment, AutoSize = autoSize
  2024. };
  2025. Assert.Equal (4, tf.Size.Width);
  2026. Assert.Equal (1, tf.Size.Height);
  2027. tf.Direction = TextDirection.TopBottom_LeftRight;
  2028. if (autoSize && textAlignment != TextAlignment.Justified)
  2029. {
  2030. Assert.Equal (2, tf.Size.Width);
  2031. Assert.Equal (2, tf.Size.Height);
  2032. }
  2033. else
  2034. {
  2035. Assert.Equal (4, tf.Size.Width);
  2036. Assert.Equal (1, tf.Size.Height);
  2037. }
  2038. }
  2039. [Theory]
  2040. [InlineData (VerticalTextAlignment.Top, false)]
  2041. [InlineData (VerticalTextAlignment.Middle, true)]
  2042. [InlineData (VerticalTextAlignment.Bottom, false)]
  2043. [InlineData (VerticalTextAlignment.Justified, true)]
  2044. public void TestSize_DirectionChange_AutoSize_True_Or_False_Vertical (
  2045. VerticalTextAlignment textAlignment,
  2046. bool autoSize
  2047. )
  2048. {
  2049. var tf = new TextFormatter
  2050. {
  2051. Direction = TextDirection.TopBottom_LeftRight,
  2052. Text = "你你",
  2053. VerticalAlignment = textAlignment,
  2054. AutoSize = autoSize
  2055. };
  2056. Assert.Equal (2, tf.Size.Width);
  2057. Assert.Equal (2, tf.Size.Height);
  2058. tf.Direction = TextDirection.LeftRight_TopBottom;
  2059. if (autoSize && textAlignment != VerticalTextAlignment.Justified)
  2060. {
  2061. Assert.Equal (4, tf.Size.Width);
  2062. Assert.Equal (1, tf.Size.Height);
  2063. }
  2064. else
  2065. {
  2066. Assert.Equal (2, tf.Size.Width);
  2067. Assert.Equal (2, tf.Size.Height);
  2068. }
  2069. }
  2070. [Theory]
  2071. [InlineData (TextDirection.LeftRight_TopBottom, false)]
  2072. [InlineData (TextDirection.LeftRight_TopBottom, true)]
  2073. [InlineData (TextDirection.TopBottom_LeftRight, false)]
  2074. [InlineData (TextDirection.TopBottom_LeftRight, true)]
  2075. public void TestSize_SizeChange_AutoSize_True_Or_False (TextDirection textDirection, bool autoSize)
  2076. {
  2077. var tf = new TextFormatter { Direction = textDirection, Text = "你你", AutoSize = autoSize };
  2078. if (textDirection == TextDirection.LeftRight_TopBottom)
  2079. {
  2080. Assert.Equal (4, tf.Size.Width);
  2081. Assert.Equal (1, tf.Size.Height);
  2082. }
  2083. else
  2084. {
  2085. Assert.Equal (2, tf.Size.Width);
  2086. Assert.Equal (2, tf.Size.Height);
  2087. }
  2088. tf.Size = new (1, 1);
  2089. if (autoSize)
  2090. {
  2091. if (textDirection == TextDirection.LeftRight_TopBottom)
  2092. {
  2093. Assert.Equal (4, tf.Size.Width);
  2094. Assert.Equal (1, tf.Size.Height);
  2095. }
  2096. else
  2097. {
  2098. Assert.Equal (2, tf.Size.Width);
  2099. Assert.Equal (2, tf.Size.Height);
  2100. }
  2101. }
  2102. else
  2103. {
  2104. Assert.Equal (1, tf.Size.Width);
  2105. Assert.Equal (1, tf.Size.Height);
  2106. }
  2107. }
  2108. [Theory]
  2109. [InlineData (TextAlignment.Left, false)]
  2110. [InlineData (TextAlignment.Centered, true)]
  2111. [InlineData (TextAlignment.Right, false)]
  2112. [InlineData (TextAlignment.Justified, true)]
  2113. public void TestSize_SizeChange_AutoSize_True_Or_False_Horizontal (TextAlignment textAlignment, bool autoSize)
  2114. {
  2115. var tf = new TextFormatter
  2116. {
  2117. Direction = TextDirection.LeftRight_TopBottom, Text = "你你", Alignment = textAlignment, AutoSize = autoSize
  2118. };
  2119. Assert.Equal (4, tf.Size.Width);
  2120. Assert.Equal (1, tf.Size.Height);
  2121. tf.Size = new (1, 1);
  2122. if (autoSize && textAlignment != TextAlignment.Justified)
  2123. {
  2124. Assert.Equal (4, tf.Size.Width);
  2125. Assert.Equal (1, tf.Size.Height);
  2126. }
  2127. else
  2128. {
  2129. Assert.Equal (1, tf.Size.Width);
  2130. Assert.Equal (1, tf.Size.Height);
  2131. }
  2132. }
  2133. [Theory]
  2134. [InlineData (VerticalTextAlignment.Top, false)]
  2135. [InlineData (VerticalTextAlignment.Middle, true)]
  2136. [InlineData (VerticalTextAlignment.Bottom, false)]
  2137. [InlineData (VerticalTextAlignment.Justified, true)]
  2138. public void TestSize_SizeChange_AutoSize_True_Or_False_Vertical (
  2139. VerticalTextAlignment textAlignment,
  2140. bool autoSize
  2141. )
  2142. {
  2143. var tf = new TextFormatter
  2144. {
  2145. Direction = TextDirection.TopBottom_LeftRight,
  2146. Text = "你你",
  2147. VerticalAlignment = textAlignment,
  2148. AutoSize = autoSize
  2149. };
  2150. Assert.Equal (2, tf.Size.Width);
  2151. Assert.Equal (2, tf.Size.Height);
  2152. tf.Size = new (1, 1);
  2153. if (autoSize && textAlignment != VerticalTextAlignment.Justified)
  2154. {
  2155. Assert.Equal (2, tf.Size.Width);
  2156. Assert.Equal (2, tf.Size.Height);
  2157. }
  2158. else
  2159. {
  2160. Assert.Equal (1, tf.Size.Width);
  2161. Assert.Equal (1, tf.Size.Height);
  2162. }
  2163. }
  2164. [Theory]
  2165. [InlineData (TextDirection.LeftRight_TopBottom, false)]
  2166. [InlineData (TextDirection.LeftRight_TopBottom, true)]
  2167. [InlineData (TextDirection.TopBottom_LeftRight, false)]
  2168. [InlineData (TextDirection.TopBottom_LeftRight, true)]
  2169. public void TestSize_TextChange (TextDirection textDirection, bool autoSize)
  2170. {
  2171. var tf = new TextFormatter { Direction = textDirection, Text = "你", AutoSize = autoSize };
  2172. Assert.Equal (2, tf.Size.Width);
  2173. Assert.Equal (1, tf.Size.Height);
  2174. tf.Text = "你你";
  2175. if (autoSize)
  2176. {
  2177. if (textDirection == TextDirection.LeftRight_TopBottom)
  2178. {
  2179. Assert.Equal (4, tf.Size.Width);
  2180. Assert.Equal (1, tf.Size.Height);
  2181. }
  2182. else
  2183. {
  2184. Assert.Equal (2, tf.Size.Width);
  2185. Assert.Equal (2, tf.Size.Height);
  2186. }
  2187. }
  2188. else
  2189. {
  2190. Assert.Equal (2, tf.Size.Width);
  2191. Assert.Equal (1, tf.Size.Height);
  2192. }
  2193. }
  2194. [Fact]
  2195. public void WordWrap_BigWidth ()
  2196. {
  2197. List<string> wrappedLines;
  2198. var text = "Constantinople";
  2199. wrappedLines = TextFormatter.WordWrapText (text, 100);
  2200. Assert.True (wrappedLines.Count == 1);
  2201. Assert.Equal ("Constantinople", wrappedLines [0]);
  2202. }
  2203. [Fact]
  2204. public void WordWrap_Invalid ()
  2205. {
  2206. var text = string.Empty;
  2207. var width = 0;
  2208. Assert.Empty (TextFormatter.WordWrapText (null, width));
  2209. Assert.Empty (TextFormatter.WordWrapText (text, width));
  2210. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrapText (text, -1));
  2211. }
  2212. [Theory]
  2213. [InlineData ("A sentence has words.", 3, -18, new [] { "A", "sen", "ten", "ce", "has", "wor", "ds." })]
  2214. [InlineData (
  2215. "A sentence has words.",
  2216. 2,
  2217. -19,
  2218. new [] { "A", "se", "nt", "en", "ce", "ha", "s", "wo", "rd", "s." }
  2219. )]
  2220. [InlineData (
  2221. "A sentence has words.",
  2222. 1,
  2223. -20,
  2224. new [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." }
  2225. )]
  2226. public void WordWrap_Narrow_Default (
  2227. string text,
  2228. int maxWidth,
  2229. int widthOffset,
  2230. IEnumerable<string> resultLines
  2231. )
  2232. {
  2233. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  2234. List<string> wrappedLines;
  2235. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2236. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2237. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2238. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2239. Assert.True (
  2240. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2241. );
  2242. Assert.True (
  2243. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2244. );
  2245. Assert.Equal (resultLines, wrappedLines);
  2246. }
  2247. [Theory]
  2248. [InlineData ("A sentence has words.", 21, 0, new [] { "A sentence has words." })]
  2249. [InlineData ("A sentence has words.", 20, -1, new [] { "A sentence has", "words." })]
  2250. [InlineData ("A sentence has words.", 15, -6, new [] { "A sentence has", "words." })]
  2251. [InlineData ("A sentence has words.", 14, -7, new [] { "A sentence has", "words." })]
  2252. [InlineData ("A sentence has words.", 13, -8, new [] { "A sentence", "has words." })]
  2253. // Unicode
  2254. [InlineData (
  2255. "A Unicode sentence (пÑивеÑ) has words.",
  2256. 42,
  2257. 0,
  2258. new [] { "A Unicode sentence (пÑивеÑ) has words." }
  2259. )]
  2260. [InlineData (
  2261. "A Unicode sentence (пÑивеÑ) has words.",
  2262. 41,
  2263. -1,
  2264. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  2265. )]
  2266. [InlineData (
  2267. "A Unicode sentence (пÑивеÑ) has words.",
  2268. 36,
  2269. -6,
  2270. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  2271. )]
  2272. [InlineData (
  2273. "A Unicode sentence (пÑивеÑ) has words.",
  2274. 35,
  2275. -7,
  2276. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  2277. )]
  2278. [InlineData (
  2279. "A Unicode sentence (пÑивеÑ) has words.",
  2280. 34,
  2281. -8,
  2282. new [] { "A Unicode sentence (пÑивеÑ)", "has words." }
  2283. )]
  2284. [InlineData (
  2285. "A Unicode sentence (пÑивеÑ) has words.",
  2286. 25,
  2287. -17,
  2288. new [] { "A Unicode sentence", "(пÑивеÑ) has words." }
  2289. )]
  2290. public void WordWrap_NoNewLines_Default (
  2291. string text,
  2292. int maxWidth,
  2293. int widthOffset,
  2294. IEnumerable<string> resultLines
  2295. )
  2296. {
  2297. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  2298. List<string> wrappedLines;
  2299. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2300. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2301. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2302. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2303. Assert.True (
  2304. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2305. );
  2306. Assert.True (
  2307. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2308. );
  2309. Assert.Equal (resultLines, wrappedLines);
  2310. }
  2311. [Theory]
  2312. [InlineData ("これが最初の行です。 こんにちは世界。 これが2行目です。", 29, 0, new [] { "これが最初の行です。", "こんにちは世界。", "これが2行目です。" })]
  2313. public void WordWrap_PreserveTrailingSpaces_False_Unicode_Wide_Runes (
  2314. string text,
  2315. int maxWidth,
  2316. int widthOffset,
  2317. IEnumerable<string> resultLines
  2318. )
  2319. {
  2320. List<string> wrappedLines;
  2321. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2322. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2323. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2324. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2325. Assert.True (
  2326. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2327. );
  2328. Assert.True (
  2329. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2330. );
  2331. Assert.Equal (resultLines, wrappedLines);
  2332. }
  2333. [Theory]
  2334. [InlineData ("文に は言葉 があり ます。", 14, 0, new [] { "文に は言葉", "があり ます。" })]
  2335. [InlineData ("文に は言葉 があり ます。", 3, -11, new [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  2336. [InlineData ("文に は言葉 があり ます。", 2, -12, new [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  2337. [InlineData (
  2338. "文に は言葉 があり ます。",
  2339. 1,
  2340. -13,
  2341. new [] { " ", " ", " " }
  2342. )] // Just Spaces; should result in a single space for each line
  2343. public void WordWrap_PreserveTrailingSpaces_False_Wide_Runes (
  2344. string text,
  2345. int maxWidth,
  2346. int widthOffset,
  2347. IEnumerable<string> resultLines
  2348. )
  2349. {
  2350. List<string> wrappedLines;
  2351. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2352. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2353. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2354. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2355. Assert.True (
  2356. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2357. );
  2358. Assert.True (
  2359. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2360. );
  2361. Assert.Equal (resultLines, wrappedLines);
  2362. }
  2363. [Theory]
  2364. [InlineData (null, 1, new string [] { })] // null input
  2365. [InlineData ("", 1, new string [] { })] // Empty input
  2366. [InlineData ("1 34", 1, new [] { "1", "3", "4" })] // Single Spaces
  2367. [InlineData ("1", 1, new [] { "1" })] // Short input
  2368. [InlineData ("12", 1, new [] { "1", "2" })]
  2369. [InlineData ("123", 1, new [] { "1", "2", "3" })]
  2370. [InlineData ("123456", 1, new [] { "1", "2", "3", "4", "5", "6" })] // No spaces
  2371. [InlineData (" ", 1, new [] { " " })] // Just Spaces; should result in a single space
  2372. [InlineData (" ", 1, new [] { " " })]
  2373. [InlineData (" ", 1, new [] { " ", " " })]
  2374. [InlineData (" ", 1, new [] { " ", " " })]
  2375. [InlineData ("12 456", 1, new [] { "1", "2", "4", "5", "6" })] // Single Spaces
  2376. [InlineData (" 2 456", 1, new [] { " ", "2", "4", "5", "6" })] // Leading spaces should be preserved.
  2377. [InlineData (" 2 456 8", 1, new [] { " ", "2", "4", "5", "6", "8" })]
  2378. [InlineData (
  2379. "A sentence has words. ",
  2380. 1,
  2381. new [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." }
  2382. )] // Complex example
  2383. [InlineData ("12 567", 1, new [] { "1", "2", " ", "5", "6", "7" })] // Double Spaces
  2384. [InlineData (" 3 567", 1, new [] { " ", "3", "5", "6", "7" })] // Double Leading spaces should be preserved.
  2385. [InlineData (" 3 678 1", 1, new [] { " ", "3", " ", "6", "7", "8", " ", "1" })]
  2386. [InlineData ("1 456", 1, new [] { "1", " ", "4", "5", "6" })]
  2387. [InlineData (
  2388. "A sentence has words. ",
  2389. 1,
  2390. new []
  2391. {
  2392. "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", "w", "o", "r", "d", "s", ".", " "
  2393. }
  2394. )] // Double space Complex example
  2395. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_1 (
  2396. string text,
  2397. int width,
  2398. IEnumerable<string> resultLines
  2399. )
  2400. {
  2401. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2402. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2403. Assert.Equal (resultLines, wrappedLines);
  2404. var breakLines = "";
  2405. foreach (string line in wrappedLines)
  2406. {
  2407. breakLines += $"{line}{Environment.NewLine}";
  2408. }
  2409. var expected = string.Empty;
  2410. foreach (string line in resultLines)
  2411. {
  2412. expected += $"{line}{Environment.NewLine}";
  2413. }
  2414. Assert.Equal (expected, breakLines);
  2415. }
  2416. [Theory]
  2417. [InlineData (null, 3, new string [] { })] // null input
  2418. [InlineData ("", 3, new string [] { })] // Empty input
  2419. [InlineData ("1", 3, new [] { "1" })] // Short input
  2420. [InlineData ("12", 3, new [] { "12" })]
  2421. [InlineData ("123", 3, new [] { "123" })]
  2422. [InlineData ("123456", 3, new [] { "123", "456" })] // No spaces
  2423. [InlineData ("1234567", 3, new [] { "123", "456", "7" })] // No spaces
  2424. [InlineData (" ", 3, new [] { " " })] // Just Spaces; should result in a single space
  2425. [InlineData (" ", 3, new [] { " " })]
  2426. [InlineData (" ", 3, new [] { " " })]
  2427. [InlineData (" ", 3, new [] { " " })]
  2428. [InlineData ("12 456", 3, new [] { "12", "456" })] // Single Spaces
  2429. [InlineData (" 2 456", 3, new [] { " 2", "456" })] // Leading spaces should be preserved.
  2430. [InlineData (" 2 456 8", 3, new [] { " 2", "456", "8" })]
  2431. [InlineData (
  2432. "A sentence has words. ",
  2433. 3,
  2434. new [] { "A", "sen", "ten", "ce", "has", "wor", "ds." }
  2435. )] // Complex example
  2436. [InlineData ("12 567", 3, new [] { "12 ", "567" })] // Double Spaces
  2437. [InlineData (" 3 567", 3, new [] { " 3", "567" })] // Double Leading spaces should be preserved.
  2438. [InlineData (" 3 678 1", 3, new [] { " 3", " 67", "8 ", "1" })]
  2439. [InlineData ("1 456", 3, new [] { "1 ", "456" })]
  2440. [InlineData (
  2441. "A sentence has words. ",
  2442. 3,
  2443. new [] { "A ", "sen", "ten", "ce ", " ", "has", "wor", "ds.", " " }
  2444. )] // Double space Complex example
  2445. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_3 (
  2446. string text,
  2447. int width,
  2448. IEnumerable<string> resultLines
  2449. )
  2450. {
  2451. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2452. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2453. Assert.Equal (resultLines, wrappedLines);
  2454. var breakLines = "";
  2455. foreach (string line in wrappedLines)
  2456. {
  2457. breakLines += $"{line}{Environment.NewLine}";
  2458. }
  2459. var expected = string.Empty;
  2460. foreach (string line in resultLines)
  2461. {
  2462. expected += $"{line}{Environment.NewLine}";
  2463. }
  2464. Assert.Equal (expected, breakLines);
  2465. }
  2466. [Theory]
  2467. [InlineData (null, 50, new string [] { })] // null input
  2468. [InlineData ("", 50, new string [] { })] // Empty input
  2469. [InlineData ("1", 50, new [] { "1" })] // Short input
  2470. [InlineData ("12", 50, new [] { "12" })]
  2471. [InlineData ("123", 50, new [] { "123" })]
  2472. [InlineData ("123456", 50, new [] { "123456" })] // No spaces
  2473. [InlineData ("1234567", 50, new [] { "1234567" })] // No spaces
  2474. [InlineData (" ", 50, new [] { " " })] // Just Spaces; should result in a single space
  2475. [InlineData (" ", 50, new [] { " " })]
  2476. [InlineData (" ", 50, new [] { " " })]
  2477. [InlineData ("12 456", 50, new [] { "12 456" })] // Single Spaces
  2478. [InlineData (" 2 456", 50, new [] { " 2 456" })] // Leading spaces should be preserved.
  2479. [InlineData (" 2 456 8", 50, new [] { " 2 456 8" })]
  2480. [InlineData ("A sentence has words. ", 50, new [] { "A sentence has words. " })] // Complex example
  2481. [InlineData ("12 567", 50, new [] { "12 567" })] // Double Spaces
  2482. [InlineData (" 3 567", 50, new [] { " 3 567" })] // Double Leading spaces should be preserved.
  2483. [InlineData (" 3 678 1", 50, new [] { " 3 678 1" })]
  2484. [InlineData ("1 456", 50, new [] { "1 456" })]
  2485. [InlineData (
  2486. "A sentence has words. ",
  2487. 50,
  2488. new [] { "A sentence has words. " }
  2489. )] // Double space Complex example
  2490. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_50 (
  2491. string text,
  2492. int width,
  2493. IEnumerable<string> resultLines
  2494. )
  2495. {
  2496. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2497. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2498. Assert.Equal (resultLines, wrappedLines);
  2499. var breakLines = "";
  2500. foreach (string line in wrappedLines)
  2501. {
  2502. breakLines += $"{line}{Environment.NewLine}";
  2503. }
  2504. var expected = string.Empty;
  2505. foreach (string line in resultLines)
  2506. {
  2507. expected += $"{line}{Environment.NewLine}";
  2508. }
  2509. Assert.Equal (expected, breakLines);
  2510. }
  2511. [Theory]
  2512. [InlineData ("A sentence has words.", 14, -7, new [] { "A sentence ", "has words." })]
  2513. [InlineData ("A sentence has words.", 8, -13, new [] { "A ", "sentence", " has ", "words." })]
  2514. [InlineData ("A sentence has words.", 6, -15, new [] { "A ", "senten", "ce ", "has ", "words." })]
  2515. [InlineData ("A sentence has words.", 3, -18, new [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds." })]
  2516. [InlineData (
  2517. "A sentence has words.",
  2518. 2,
  2519. -19,
  2520. new [] { "A ", "se", "nt", "en", "ce", " ", "ha", "s ", "wo", "rd", "s." }
  2521. )]
  2522. [InlineData (
  2523. "A sentence has words.",
  2524. 1,
  2525. -20,
  2526. new []
  2527. {
  2528. "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", " ", "w", "o", "r", "d", "s", "."
  2529. }
  2530. )]
  2531. public void WordWrap_PreserveTrailingSpaces_True (
  2532. string text,
  2533. int maxWidth,
  2534. int widthOffset,
  2535. IEnumerable<string> resultLines
  2536. )
  2537. {
  2538. List<string> wrappedLines;
  2539. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2540. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2541. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true);
  2542. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2543. Assert.True (
  2544. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2545. );
  2546. Assert.True (
  2547. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2548. );
  2549. Assert.Equal (resultLines, wrappedLines);
  2550. }
  2551. [Theory]
  2552. [InlineData ("文に は言葉 があり ます。", 14, 0, new [] { "文に は言葉 ", "があり ます。" })]
  2553. [InlineData ("文に は言葉 があり ます。", 3, -11, new [] { "文", "に ", "は", "言", "葉 ", "が", "あ", "り ", "ま", "す", "。" })]
  2554. [InlineData (
  2555. "文に は言葉 があり ます。",
  2556. 2,
  2557. -12,
  2558. new [] { "文", "に", " ", "は", "言", "葉", " ", "が", "あ", "り", " ", "ま", "す", "。" }
  2559. )]
  2560. [InlineData ("文に は言葉 があり ます。", 1, -13, new string [] { })]
  2561. public void WordWrap_PreserveTrailingSpaces_True_Wide_Runes (
  2562. string text,
  2563. int maxWidth,
  2564. int widthOffset,
  2565. IEnumerable<string> resultLines
  2566. )
  2567. {
  2568. List<string> wrappedLines;
  2569. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2570. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2571. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true);
  2572. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2573. Assert.True (
  2574. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2575. );
  2576. Assert.True (
  2577. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2578. );
  2579. Assert.Equal (resultLines, wrappedLines);
  2580. }
  2581. [Theory]
  2582. [InlineData ("A sentence has words. ", 3, new [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds.", " " })]
  2583. [InlineData (
  2584. "A sentence has words. ",
  2585. 3,
  2586. new [] { "A ", " ", "sen", "ten", "ce ", " ", " ", " ", "has", " ", "wor", "ds.", " " }
  2587. )]
  2588. public void WordWrap_PreserveTrailingSpaces_True_With_Simple_Runes_Width_3 (
  2589. string text,
  2590. int width,
  2591. IEnumerable<string> resultLines
  2592. )
  2593. {
  2594. List<string> wrappedLines = TextFormatter.WordWrapText (text, width, true);
  2595. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2596. Assert.Equal (resultLines, wrappedLines);
  2597. var breakLines = "";
  2598. foreach (string line in wrappedLines)
  2599. {
  2600. breakLines += $"{line}{Environment.NewLine}";
  2601. }
  2602. var expected = string.Empty;
  2603. foreach (string line in resultLines)
  2604. {
  2605. expected += $"{line}{Environment.NewLine}";
  2606. }
  2607. Assert.Equal (expected, breakLines);
  2608. // Double space Complex example - this is how VS 2022 does it
  2609. //text = "A sentence has words. ";
  2610. //breakLines = "";
  2611. //wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: true);
  2612. //foreach (var line in wrappedLines) {
  2613. // breakLines += $"{line}{Environment.NewLine}";
  2614. //}
  2615. //expected = "A " + Environment.NewLine +
  2616. // " se" + Environment.NewLine +
  2617. // " nt" + Environment.NewLine +
  2618. // " en" + Environment.NewLine +
  2619. // " ce" + Environment.NewLine +
  2620. // " " + Environment.NewLine +
  2621. // " " + Environment.NewLine +
  2622. // " " + Environment.NewLine +
  2623. // " ha" + Environment.NewLine +
  2624. // " s " + Environment.NewLine +
  2625. // " wo" + Environment.NewLine +
  2626. // " rd" + Environment.NewLine +
  2627. // " s." + Environment.NewLine;
  2628. //Assert.Equal (expected, breakLines);
  2629. }
  2630. [Theory]
  2631. [InlineData ("A sentence\t\t\t has words.", 14, -10, new [] { "A sentence\t", "\t\t has ", "words." })]
  2632. [InlineData (
  2633. "A sentence\t\t\t has words.",
  2634. 8,
  2635. -16,
  2636. new [] { "A ", "sentence", "\t\t", "\t ", "has ", "words." }
  2637. )]
  2638. [InlineData (
  2639. "A sentence\t\t\t has words.",
  2640. 3,
  2641. -21,
  2642. new [] { "A ", "sen", "ten", "ce", "\t", "\t", "\t", " ", "has", " ", "wor", "ds." }
  2643. )]
  2644. [InlineData (
  2645. "A sentence\t\t\t has words.",
  2646. 2,
  2647. -22,
  2648. new [] { "A ", "se", "nt", "en", "ce", "\t", "\t", "\t", " ", "ha", "s ", "wo", "rd", "s." }
  2649. )]
  2650. [InlineData (
  2651. "A sentence\t\t\t has words.",
  2652. 1,
  2653. -23,
  2654. new []
  2655. {
  2656. "A",
  2657. " ",
  2658. "s",
  2659. "e",
  2660. "n",
  2661. "t",
  2662. "e",
  2663. "n",
  2664. "c",
  2665. "e",
  2666. "\t",
  2667. "\t",
  2668. "\t",
  2669. " ",
  2670. "h",
  2671. "a",
  2672. "s",
  2673. " ",
  2674. "w",
  2675. "o",
  2676. "r",
  2677. "d",
  2678. "s",
  2679. "."
  2680. }
  2681. )]
  2682. public void WordWrap_PreserveTrailingSpaces_True_With_Tab (
  2683. string text,
  2684. int maxWidth,
  2685. int widthOffset,
  2686. IEnumerable<string> resultLines,
  2687. int tabWidth = 4
  2688. )
  2689. {
  2690. List<string> wrappedLines;
  2691. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2692. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2693. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true, tabWidth);
  2694. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2695. Assert.True (
  2696. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2697. );
  2698. Assert.True (
  2699. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2700. );
  2701. Assert.Equal (resultLines, wrappedLines);
  2702. }
  2703. [Theory]
  2704. [InlineData ("Constantinople", 14, 0, new [] { "Constantinople" })]
  2705. [InlineData ("Constantinople", 12, -2, new [] { "Constantinop", "le" })]
  2706. [InlineData ("Constantinople", 9, -5, new [] { "Constanti", "nople" })]
  2707. [InlineData ("Constantinople", 7, -7, new [] { "Constan", "tinople" })]
  2708. [InlineData ("Constantinople", 5, -9, new [] { "Const", "antin", "ople" })]
  2709. [InlineData ("Constantinople", 4, -10, new [] { "Cons", "tant", "inop", "le" })]
  2710. [InlineData (
  2711. "Constantinople",
  2712. 1,
  2713. -13,
  2714. new [] { "C", "o", "n", "s", "t", "a", "n", "t", "i", "n", "o", "p", "l", "e" }
  2715. )]
  2716. public void WordWrap_SingleWordLine (
  2717. string text,
  2718. int maxWidth,
  2719. int widthOffset,
  2720. IEnumerable<string> resultLines
  2721. )
  2722. {
  2723. List<string> wrappedLines;
  2724. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2725. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2726. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2727. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2728. Assert.True (
  2729. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2730. );
  2731. Assert.True (
  2732. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2733. );
  2734. Assert.Equal (resultLines, wrappedLines);
  2735. }
  2736. [Theory]
  2737. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 20, 0, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2738. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 19, -1, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2739. [InlineData (
  2740. "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.",
  2741. 19,
  2742. 0,
  2743. new [] { "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence." }
  2744. )]
  2745. public void WordWrap_Unicode_2LinesWithNonBreakingSpace (
  2746. string text,
  2747. int maxWidth,
  2748. int widthOffset,
  2749. IEnumerable<string> resultLines
  2750. )
  2751. {
  2752. List<string> wrappedLines;
  2753. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2754. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2755. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2756. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2757. Assert.True (
  2758. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2759. );
  2760. Assert.True (
  2761. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2762. );
  2763. Assert.Equal (resultLines, wrappedLines);
  2764. }
  2765. [Theory]
  2766. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 19, 0, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2767. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 18, -1, new [] { "This\u00A0is\u00A0a\u00A0sentence", "." })]
  2768. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 17, -2, new [] { "This\u00A0is\u00A0a\u00A0sentenc", "e." })]
  2769. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 14, -5, new [] { "This\u00A0is\u00A0a\u00A0sent", "ence." })]
  2770. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 10, -9, new [] { "This\u00A0is\u00A0a\u00A0", "sentence." })]
  2771. [InlineData (
  2772. "This\u00A0is\u00A0a\u00A0sentence.",
  2773. 7,
  2774. -12,
  2775. new [] { "This\u00A0is", "\u00A0a\u00A0sent", "ence." }
  2776. )]
  2777. [InlineData (
  2778. "This\u00A0is\u00A0a\u00A0sentence.",
  2779. 5,
  2780. -14,
  2781. new [] { "This\u00A0", "is\u00A0a\u00A0", "sente", "nce." }
  2782. )]
  2783. [InlineData (
  2784. "This\u00A0is\u00A0a\u00A0sentence.",
  2785. 1,
  2786. -18,
  2787. new []
  2788. {
  2789. "T", "h", "i", "s", "\u00A0", "i", "s", "\u00A0", "a", "\u00A0", "s", "e", "n", "t", "e", "n", "c", "e", "."
  2790. }
  2791. )]
  2792. public void WordWrap_Unicode_LineWithNonBreakingSpace (
  2793. string text,
  2794. int maxWidth,
  2795. int widthOffset,
  2796. IEnumerable<string> resultLines
  2797. )
  2798. {
  2799. List<string> wrappedLines;
  2800. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2801. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2802. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2803. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2804. Assert.True (
  2805. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2806. );
  2807. Assert.True (
  2808. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2809. );
  2810. Assert.Equal (resultLines, wrappedLines);
  2811. }
  2812. [Theory]
  2813. [InlineData (
  2814. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2815. 51,
  2816. 0,
  2817. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2818. )]
  2819. [InlineData (
  2820. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2821. 50,
  2822. -1,
  2823. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2824. )]
  2825. [InlineData (
  2826. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2827. 46,
  2828. -5,
  2829. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮ", "ฯะัาำ" }
  2830. )]
  2831. [InlineData (
  2832. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2833. 26,
  2834. -25,
  2835. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2836. )]
  2837. [InlineData (
  2838. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2839. 17,
  2840. -34,
  2841. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑ", "ฒณดตถทธนบปผฝพฟภมย", "รฤลฦวศษสหฬอฮฯะัาำ" }
  2842. )]
  2843. [InlineData (
  2844. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2845. 13,
  2846. -38,
  2847. new [] { "กขฃคฅฆงจฉชซฌญ", "ฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦว", "ศษสหฬอฮฯะัาำ" }
  2848. )]
  2849. [InlineData (
  2850. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2851. 1,
  2852. -50,
  2853. new []
  2854. {
  2855. "ก",
  2856. "ข",
  2857. "ฃ",
  2858. "ค",
  2859. "ฅ",
  2860. "ฆ",
  2861. "ง",
  2862. "จ",
  2863. "ฉ",
  2864. "ช",
  2865. "ซ",
  2866. "ฌ",
  2867. "ญ",
  2868. "ฎ",
  2869. "ฏ",
  2870. "ฐ",
  2871. "ฑ",
  2872. "ฒ",
  2873. "ณ",
  2874. "ด",
  2875. "ต",
  2876. "ถ",
  2877. "ท",
  2878. "ธ",
  2879. "น",
  2880. "บ",
  2881. "ป",
  2882. "ผ",
  2883. "ฝ",
  2884. "พ",
  2885. "ฟ",
  2886. "ภ",
  2887. "ม",
  2888. "ย",
  2889. "ร",
  2890. "ฤ",
  2891. "ล",
  2892. "ฦ",
  2893. "ว",
  2894. "ศ",
  2895. "ษ",
  2896. "ส",
  2897. "ห",
  2898. "ฬ",
  2899. "อ",
  2900. "ฮ",
  2901. "ฯ",
  2902. "ะั",
  2903. "า",
  2904. "ำ"
  2905. }
  2906. )]
  2907. public void WordWrap_Unicode_SingleWordLine (
  2908. string text,
  2909. int maxWidth,
  2910. int widthOffset,
  2911. IEnumerable<string> resultLines
  2912. )
  2913. {
  2914. List<string> wrappedLines;
  2915. IEnumerable<Rune> zeroWidth = text.EnumerateRunes ().Where (r => r.GetColumns () == 0);
  2916. Assert.Single (zeroWidth);
  2917. Assert.Equal ('ั', zeroWidth.ElementAt (0).Value);
  2918. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2919. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2920. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2921. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2922. Assert.True (
  2923. expectedClippedWidth
  2924. >= (wrappedLines.Count > 0
  2925. ? wrappedLines.Max (
  2926. l => l.GetRuneCount ()
  2927. + zeroWidth.Count ()
  2928. - 1
  2929. + widthOffset
  2930. )
  2931. : 0)
  2932. );
  2933. Assert.True (
  2934. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2935. );
  2936. Assert.Equal (resultLines, wrappedLines);
  2937. }
  2938. /// <summary>WordWrap strips CRLF</summary>
  2939. [Theory]
  2940. [InlineData (
  2941. "A sentence has words.\nA paragraph has lines.",
  2942. 44,
  2943. 0,
  2944. new [] { "A sentence has words.A paragraph has lines." }
  2945. )]
  2946. [InlineData (
  2947. "A sentence has words.\nA paragraph has lines.",
  2948. 43,
  2949. -1,
  2950. new [] { "A sentence has words.A paragraph has lines." }
  2951. )]
  2952. [InlineData (
  2953. "A sentence has words.\nA paragraph has lines.",
  2954. 38,
  2955. -6,
  2956. new [] { "A sentence has words.A paragraph has", "lines." }
  2957. )]
  2958. [InlineData (
  2959. "A sentence has words.\nA paragraph has lines.",
  2960. 34,
  2961. -10,
  2962. new [] { "A sentence has words.A paragraph", "has lines." }
  2963. )]
  2964. [InlineData (
  2965. "A sentence has words.\nA paragraph has lines.",
  2966. 27,
  2967. -17,
  2968. new [] { "A sentence has words.A", "paragraph has lines." }
  2969. )]
  2970. // Unicode
  2971. [InlineData (
  2972. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2973. 69,
  2974. 0,
  2975. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." }
  2976. )]
  2977. [InlineData (
  2978. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2979. 68,
  2980. -1,
  2981. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." }
  2982. )]
  2983. [InlineData (
  2984. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2985. 63,
  2986. -6,
  2987. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", "Линии." }
  2988. )]
  2989. [InlineData (
  2990. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2991. 59,
  2992. -10,
  2993. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", "has Линии." }
  2994. )]
  2995. [InlineData (
  2996. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2997. 52,
  2998. -17,
  2999. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode", "Пункт has Линии." }
  3000. )]
  3001. public void WordWrap_WithNewLines (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  3002. {
  3003. List<string> wrappedLines;
  3004. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  3005. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  3006. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  3007. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  3008. Assert.True (
  3009. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  3010. );
  3011. Assert.True (
  3012. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  3013. );
  3014. Assert.Equal (resultLines, wrappedLines);
  3015. }
  3016. [SetupFakeDriver]
  3017. [Theory]
  3018. [InlineData ("A", 0, false, "")]
  3019. [InlineData ("A", 1, false, "A")]
  3020. [InlineData ("A", 2, false, "A")]
  3021. [InlineData ("AB", 1, false, "A")]
  3022. [InlineData ("AB", 2, false, "AB")]
  3023. [InlineData ("ABC", 3, false, "ABC")]
  3024. [InlineData ("ABC", 4, false, "ABC")]
  3025. [InlineData ("ABC", 6, false, "ABC")]
  3026. [InlineData ("A", 0, true, "")]
  3027. [InlineData ("A", 1, true, "A")]
  3028. [InlineData ("A", 2, true, "A")]
  3029. [InlineData ("AB", 1, true, "A")]
  3030. [InlineData ("AB", 2, true, "AB")]
  3031. [InlineData ("ABC", 3, true, "ABC")]
  3032. [InlineData ("ABC", 4, true, "ABC")]
  3033. [InlineData ("ABC", 6, true, "ABC")]
  3034. public void Draw_Horizontal_Left (string text, int width, bool autoSize, string expectedText)
  3035. {
  3036. TextFormatter tf = new ()
  3037. {
  3038. Text = text,
  3039. Alignment = TextAlignment.Left,
  3040. AutoSize = autoSize,
  3041. };
  3042. if (!autoSize)
  3043. {
  3044. tf.Size = new Size (width, 1);
  3045. }
  3046. tf.Draw (new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default);
  3047. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  3048. }
  3049. [SetupFakeDriver]
  3050. [Theory]
  3051. [InlineData ("A", 0, false, "")]
  3052. [InlineData ("A", 1, false, "A")]
  3053. [InlineData ("A", 2, false, " A")]
  3054. [InlineData ("AB", 1, false, "A")]
  3055. [InlineData ("AB", 2, false, "AB")]
  3056. [InlineData ("ABC", 3, false, "ABC")]
  3057. [InlineData ("ABC", 4, false, " ABC")]
  3058. [InlineData ("ABC", 6, false, " ABC")]
  3059. [InlineData ("A", 0, true, "")]
  3060. [InlineData ("A", 1, true, "A")]
  3061. [InlineData ("A", 2, true, " A")]
  3062. [InlineData ("AB", 1, true, "")] // BUGBUG: Should be "B". See https://github.com/gui-cs/Terminal.Gui/issues/3418#issuecomment-2067771418 for a partial fix
  3063. [InlineData ("AB", 2, true, "AB")]
  3064. [InlineData ("ABC", 3, true, "ABC")]
  3065. [InlineData ("ABC", 4, true, " ABC")]
  3066. [InlineData ("ABC", 6, true, " ABC")]
  3067. public void Draw_Horizontal_Right (string text, int width, bool autoSize, string expectedText)
  3068. {
  3069. TextFormatter tf = new ()
  3070. {
  3071. Text = text,
  3072. Alignment = TextAlignment.Right,
  3073. AutoSize = autoSize,
  3074. };
  3075. if (!autoSize)
  3076. {
  3077. tf.Size = new Size (width, 1);
  3078. }
  3079. tf.Draw (new Rectangle (Point.Empty, new (width, 1)), Attribute.Default, Attribute.Default);
  3080. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  3081. }
  3082. [SetupFakeDriver]
  3083. [Theory]
  3084. [InlineData ("A", 0, false, "")]
  3085. [InlineData ("A", 1, false, "A")]
  3086. [InlineData ("A", 2, false, "A")]
  3087. [InlineData ("A", 3, false, " A")]
  3088. [InlineData ("AB", 1, false, "A")]
  3089. [InlineData ("AB", 2, false, "AB")]
  3090. [InlineData ("ABC", 3, false, "ABC")]
  3091. [InlineData ("ABC", 4, false, "ABC")]
  3092. [InlineData ("ABC", 5, false, " ABC")]
  3093. [InlineData ("ABC", 6, false, " ABC")]
  3094. [InlineData ("ABC", 9, false, " ABC")]
  3095. [InlineData ("A", 0, true, "")]
  3096. [InlineData ("A", 1, true, "A")]
  3097. [InlineData ("A", 2, true, "A")]
  3098. [InlineData ("A", 3, true, " A")]
  3099. [InlineData ("AB", 1, true, "A")]
  3100. [InlineData ("AB", 2, true, "AB")]
  3101. [InlineData ("ABC", 3, true, "ABC")]
  3102. [InlineData ("ABC", 4, true, "ABC")]
  3103. [InlineData ("ABC", 5, true, " ABC")]
  3104. [InlineData ("ABC", 6, true, " ABC")]
  3105. [InlineData ("ABC", 9, true, " ABC")]
  3106. public void Draw_Horizontal_Centered (string text, int width, bool autoSize, string expectedText)
  3107. {
  3108. TextFormatter tf = new ()
  3109. {
  3110. Text = text,
  3111. Alignment = TextAlignment.Centered,
  3112. AutoSize = autoSize,
  3113. };
  3114. if (!autoSize)
  3115. {
  3116. tf.Size = new Size (width, 1);
  3117. }
  3118. tf.Draw (new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default);
  3119. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  3120. }
  3121. [SetupFakeDriver]
  3122. [Theory]
  3123. [InlineData ("A", 0, false, "")]
  3124. [InlineData ("A", 1, false, "A")]
  3125. [InlineData ("A", 2, false,"A")]
  3126. [InlineData ("A B", 3, false,"A B")]
  3127. [InlineData ("A B", 1, false,"A")]
  3128. [InlineData ("A B", 2, false,"A")]
  3129. [InlineData ("A B", 3, false,"A B")]
  3130. [InlineData ("A B", 4, false,"A B")]
  3131. [InlineData ("A B", 5, false,"A B")]
  3132. [InlineData ("A B", 6, false,"A B")]
  3133. [InlineData ("A B", 10,false,"A B")]
  3134. [InlineData ("ABC ABC", 10, false, "ABC ABC")]
  3135. [InlineData ("A", 0, true, "")]
  3136. [InlineData ("A", 1, true, "A")]
  3137. [InlineData ("A", 2, true, "A")]
  3138. [InlineData ("A B", 3, true, "A B")]
  3139. [InlineData ("A B", 1, true, "A")]
  3140. [InlineData ("A B", 2, true, "A")]
  3141. [InlineData ("A B", 3, true, "A B")]
  3142. [InlineData ("A B", 4, true, "A B")]
  3143. [InlineData ("A B", 5, true, "A B")]
  3144. [InlineData ("A B", 6, true, "A B")]
  3145. [InlineData ("A B", 10, true, "A B")]
  3146. [InlineData ("ABC ABC", 10, true, "ABC ABC")]
  3147. public void Draw_Horizontal_Justified (string text, int width, bool autoSize, string expectedText)
  3148. {
  3149. TextFormatter tf = new ()
  3150. {
  3151. Text = text,
  3152. Alignment = TextAlignment.Justified,
  3153. AutoSize = autoSize,
  3154. };
  3155. if (!autoSize)
  3156. {
  3157. tf.Size = new Size (width, 1);
  3158. }
  3159. tf.Draw (new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default);
  3160. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  3161. }
  3162. [SetupFakeDriver]
  3163. [Theory]
  3164. [InlineData ("A", 5, false, "A")]
  3165. [InlineData ("AB12", 5, false, @"
  3166. A
  3167. B
  3168. 1
  3169. 2")]
  3170. [InlineData ("AB\n12", 5, false, @"
  3171. A1
  3172. B2")]
  3173. [InlineData ("", 1, false, "")]
  3174. public void Draw_Vertical_TopBottom_LeftRight (string text, int height, bool autoSize, string expectedText)
  3175. {
  3176. TextFormatter tf = new ()
  3177. {
  3178. Text = text,
  3179. AutoSize = autoSize,
  3180. Direction = TextDirection.TopBottom_LeftRight,
  3181. };
  3182. if (!autoSize)
  3183. {
  3184. tf.Size = new Size (5, height);
  3185. }
  3186. tf.Draw (new Rectangle (0, 0, 5, height), Attribute.Default, Attribute.Default);
  3187. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  3188. }
  3189. }