TextFormatterTests.cs 127 KB

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