TextFormatterTests.cs 125 KB

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