TextFormatterTests.cs 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237
  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. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  715. }
  716. [Theory]
  717. [InlineData ("test", 3, 3)]
  718. [InlineData ("test", 4, 4)]
  719. [InlineData ("test", 10, 4)]
  720. public void GetLengthThatFits_For_String (string text, int columns, int expectedLength)
  721. {
  722. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  723. }
  724. [Theory]
  725. [InlineData ("Hello World", 6, 6)]
  726. [InlineData ("こんにちは 世界", 6, 3)]
  727. public void GetLengthThatFits_Simple_And_Wide_Runes (string text, int columns, int expectedLength)
  728. {
  729. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  730. }
  731. [Theory]
  732. [InlineData ("test", 3, 3)]
  733. [InlineData ("test", 4, 4)]
  734. [InlineData ("test", 10, 4)]
  735. [InlineData ("test", 1, 1)]
  736. [InlineData ("test", 0, 0)]
  737. [InlineData ("test", -1, 0)]
  738. [InlineData (null, -1, 0)]
  739. [InlineData ("", -1, 0)]
  740. public void GetLengthThatFits_String (string text, int columns, int expectedLength)
  741. {
  742. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  743. }
  744. [Fact]
  745. public void GetLengthThatFits_With_Combining_Runes ()
  746. {
  747. var text = "Les Mise\u0328\u0301rables";
  748. Assert.Equal (14, TextFormatter.GetLengthThatFits (text, 14));
  749. Assert.Equal ("Les Misę́rables", text);
  750. }
  751. [Fact]
  752. public void GetMaxColsForWidth_With_Combining_Runes ()
  753. {
  754. List<string> text = new () { "Les Mis", "e\u0328\u0301", "rables" };
  755. Assert.Equal (1, TextFormatter.GetMaxColsForWidth (text, 1));
  756. Assert.Equal ("Les Mis", text [0]);
  757. Assert.Equal ("ę́", text [1]);
  758. Assert.Equal ("rables", text [^1]);
  759. }
  760. [Fact]
  761. public void GetWidestLineLength_With_Combining_Runes ()
  762. {
  763. var text = "Les Mise\u0328\u0301rables";
  764. Assert.Equal (14, TextFormatter.GetWidestLineLength (text, 1));
  765. Assert.Equal ("Les Misę́rables", text);
  766. }
  767. [Fact]
  768. public void Internal_Tests ()
  769. {
  770. var tf = new TextFormatter ();
  771. Assert.Equal (KeyCode.Null, tf.HotKey);
  772. tf.HotKey = KeyCode.CtrlMask | KeyCode.Q;
  773. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, tf.HotKey);
  774. }
  775. [Theory]
  776. [InlineData ("")]
  777. [InlineData (null)]
  778. [InlineData ("test")]
  779. public void Justify_Invalid (string text)
  780. {
  781. Assert.Equal (text, TextFormatter.Justify (text, 0));
  782. Assert.Equal (text, TextFormatter.Justify (text, 0));
  783. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  784. }
  785. [Theory]
  786. // Even # of spaces
  787. // 0123456789
  788. [InlineData ("012 456 89", "012 456 89", 10, 0, "+", true)]
  789. [InlineData ("012 456 89", "012++456+89", 11, 1)]
  790. [InlineData ("012 456 89", "012 456 89", 12, 2, "++", true)]
  791. [InlineData ("012 456 89", "012+++456++89", 13, 3)]
  792. [InlineData ("012 456 89", "012 456 89", 14, 4, "+++", true)]
  793. [InlineData ("012 456 89", "012++++456+++89", 15, 5)]
  794. [InlineData ("012 456 89", "012 456 89", 16, 6, "++++", true)]
  795. [InlineData ("012 456 89", "012 456 89", 30, 20, "+++++++++++", true)]
  796. [InlineData ("012 456 89", "012+++++++++++++456++++++++++++89", 33, 23)]
  797. // Odd # of spaces
  798. // 01234567890123
  799. [InlineData ("012 456 89 end", "012 456 89 end", 14, 0, "+", true)]
  800. [InlineData ("012 456 89 end", "012++456+89+end", 15, 1)]
  801. [InlineData ("012 456 89 end", "012++456++89+end", 16, 2)]
  802. [InlineData ("012 456 89 end", "012 456 89 end", 17, 3, "++", true)]
  803. [InlineData ("012 456 89 end", "012+++456++89++end", 18, 4)]
  804. [InlineData ("012 456 89 end", "012+++456+++89++end", 19, 5)]
  805. [InlineData ("012 456 89 end", "012 456 89 end", 20, 6, "+++", true)]
  806. [InlineData ("012 456 89 end", "012++++++++456++++++++89+++++++end", 34, 20)]
  807. [InlineData ("012 456 89 end", "012+++++++++456+++++++++89++++++++end", 37, 23)]
  808. // Unicode
  809. // Even # of chars
  810. // 0123456789
  811. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 10, 0, "+", true)]
  812. [InlineData ("пÑРвРÑ", "пÑÐ++вÐ+Ñ", 11, 1)]
  813. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 12, 2, "++", true)]
  814. [InlineData ("пÑРвРÑ", "пÑÐ+++вÐ++Ñ", 13, 3)]
  815. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 14, 4, "+++", true)]
  816. [InlineData ("пÑРвРÑ", "пÑÐ++++вÐ+++Ñ", 15, 5)]
  817. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 16, 6, "++++", true)]
  818. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 30, 20, "+++++++++++", true)]
  819. [InlineData ("пÑРвРÑ", "пÑÐ+++++++++++++вÐ++++++++++++Ñ", 33, 23)]
  820. // Unicode
  821. // Odd # of chars
  822. // 0123456789
  823. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 10, 0, "+", true)]
  824. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ+вÐ+Ñ", 11, 1)]
  825. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ++вÐ+Ñ", 12, 2)]
  826. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 13, 3, "++", true)]
  827. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ++вÐ++Ñ", 14, 4)]
  828. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ+++вÐ++Ñ", 15, 5)]
  829. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 16, 6, "+++", true)]
  830. [InlineData ("Ð ÑРвРÑ", "Ð++++++++ÑÐ++++++++вÐ+++++++Ñ", 30, 20)]
  831. [InlineData ("Ð ÑРвРÑ", "Ð+++++++++ÑÐ+++++++++вÐ++++++++Ñ", 33, 23)]
  832. public void Justify_Sentence (
  833. string text,
  834. string justifiedText,
  835. int forceToWidth,
  836. int widthOffset,
  837. string replaceWith = null,
  838. bool replace = false
  839. )
  840. {
  841. var fillChar = '+';
  842. Assert.Equal (forceToWidth, text.GetRuneCount () + widthOffset);
  843. if (replace)
  844. {
  845. justifiedText = text.Replace (" ", replaceWith);
  846. }
  847. Assert.Equal (justifiedText, TextFormatter.Justify (text, forceToWidth, fillChar));
  848. Assert.True (Math.Abs (forceToWidth - justifiedText.GetRuneCount ()) < text.Count (s => s == ' '));
  849. Assert.True (Math.Abs (forceToWidth - justifiedText.GetColumns ()) < text.Count (s => s == ' '));
  850. }
  851. [Theory]
  852. [InlineData ("word")] // Even # of chars
  853. [InlineData ("word.")] // Odd # of chars
  854. [InlineData ("пÑивеÑ")] // Unicode (even #)
  855. [InlineData ("пÑивеÑ.")] // Unicode (odd # of chars)
  856. public void Justify_SingleWord (string text)
  857. {
  858. string justifiedText = text;
  859. var fillChar = '+';
  860. int width = text.GetRuneCount ();
  861. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  862. width = text.GetRuneCount () + 1;
  863. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  864. width = text.GetRuneCount () + 2;
  865. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  866. width = text.GetRuneCount () + 10;
  867. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  868. width = text.GetRuneCount () + 11;
  869. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  870. }
  871. [Theory]
  872. [InlineData ("Single Line 界", 14)]
  873. [InlineData ("First Line 界\nSecond Line 界\nThird Line 界\n", 14)]
  874. public void MaxWidthLine_With_And_Without_Newlines (string text, int expected) { Assert.Equal (expected, TextFormatter.GetWidestLineLength (text)); }
  875. [Theory]
  876. [InlineData (
  877. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  878. 0,
  879. 0,
  880. false,
  881. new [] { "" }
  882. )]
  883. [InlineData (
  884. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  885. 0,
  886. 1,
  887. false,
  888. new [] { "" }
  889. )]
  890. [InlineData (
  891. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  892. 1,
  893. 0,
  894. false,
  895. new [] { "" }
  896. )]
  897. [InlineData (
  898. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  899. 0,
  900. 0,
  901. true,
  902. new [] { "" }
  903. )]
  904. [InlineData (
  905. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  906. 0,
  907. 1,
  908. true,
  909. new [] { "" }
  910. )]
  911. [InlineData (
  912. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  913. 1,
  914. 0,
  915. true,
  916. new [] { "" }
  917. )]
  918. [InlineData (
  919. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  920. 6,
  921. 5,
  922. false,
  923. new [] { "First " }
  924. )]
  925. [InlineData ("1\n2\n3\n4\n5\n6", 6, 5, false, new [] { "1 2 3 " })]
  926. [InlineData (
  927. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  928. 6,
  929. 5,
  930. true,
  931. new [] { "First ", "Second", "Third ", "Forty ", "Fiftee" }
  932. )]
  933. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, false, new [] { "第一" })]
  934. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, true, new [] { "第一", "第二", "第三", "四十", "第十" })]
  935. public void MultiLine_WordWrap_False_Horizontal_Direction (
  936. string text,
  937. int maxWidth,
  938. int maxHeight,
  939. bool multiLine,
  940. IEnumerable<string> resultLines
  941. )
  942. {
  943. var tf = new TextFormatter
  944. {
  945. Text = text, ConstrainToSize = new (maxWidth, maxHeight), WordWrap = false, MultiLine = multiLine
  946. };
  947. Assert.False (tf.WordWrap);
  948. Assert.True (tf.MultiLine == multiLine);
  949. Assert.Equal (TextDirection.LeftRight_TopBottom, tf.Direction);
  950. List<string> splitLines = tf.GetLines ();
  951. Assert.Equal (splitLines.Count, resultLines.Count ());
  952. Assert.Equal (splitLines, resultLines);
  953. }
  954. [Theory]
  955. [InlineData (
  956. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  957. 0,
  958. 0,
  959. false,
  960. new [] { "" }
  961. )]
  962. [InlineData (
  963. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  964. 0,
  965. 1,
  966. false,
  967. new [] { "" }
  968. )]
  969. [InlineData (
  970. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  971. 1,
  972. 0,
  973. false,
  974. new [] { "" }
  975. )]
  976. [InlineData (
  977. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  978. 0,
  979. 0,
  980. true,
  981. new [] { "" }
  982. )]
  983. [InlineData (
  984. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  985. 0,
  986. 1,
  987. true,
  988. new [] { "" }
  989. )]
  990. [InlineData (
  991. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  992. 1,
  993. 0,
  994. true,
  995. new [] { "" }
  996. )]
  997. [InlineData (
  998. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  999. 6,
  1000. 5,
  1001. false,
  1002. new [] { "First" }
  1003. )]
  1004. [InlineData ("1\n2\n3\n4\n5\n6", 6, 5, false, new [] { "1 2 3" })]
  1005. [InlineData (
  1006. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1007. 6,
  1008. 5,
  1009. true,
  1010. new [] { "First", "Secon", "Third", "Forty", "Fifte", "Seven" }
  1011. )]
  1012. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, false, new [] { "第一行 第" })]
  1013. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, true, new [] { "第一行", "第二行" })]
  1014. public void MultiLine_WordWrap_False_Vertical_Direction (
  1015. string text,
  1016. int maxWidth,
  1017. int maxHeight,
  1018. bool multiLine,
  1019. IEnumerable<string> resultLines
  1020. )
  1021. {
  1022. var tf = new TextFormatter
  1023. {
  1024. Text = text,
  1025. ConstrainToSize = new (maxWidth, maxHeight),
  1026. WordWrap = false,
  1027. MultiLine = multiLine,
  1028. Direction = TextDirection.TopBottom_LeftRight
  1029. };
  1030. Assert.False (tf.WordWrap);
  1031. Assert.True (tf.MultiLine == multiLine);
  1032. Assert.Equal (TextDirection.TopBottom_LeftRight, tf.Direction);
  1033. List<string> splitLines = tf.GetLines ();
  1034. Assert.Equal (splitLines.Count, resultLines.Count ());
  1035. Assert.Equal (splitLines, resultLines);
  1036. }
  1037. [Fact]
  1038. public void NeedsFormat_Sets ()
  1039. {
  1040. IDriver driver = CreateFakeDriver ();
  1041. var testText = "test";
  1042. var testBounds = new Rectangle (0, 0, 100, 1);
  1043. var tf = new TextFormatter ();
  1044. tf.Text = "test";
  1045. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  1046. Assert.NotEmpty (tf.GetLines ());
  1047. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1048. Assert.Equal (testText, tf.Text);
  1049. tf.Draw (driver: driver, screen: testBounds, normalColor: new (), hotColor: new ());
  1050. Assert.False (tf.NeedsFormat);
  1051. tf.ConstrainToSize = new (1, 1);
  1052. Assert.True (tf.NeedsFormat);
  1053. Assert.NotEmpty (tf.GetLines ());
  1054. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1055. tf.Alignment = Alignment.Center;
  1056. Assert.True (tf.NeedsFormat);
  1057. Assert.NotEmpty (tf.GetLines ());
  1058. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1059. }
  1060. // Test that changing TextFormatter does not impact View dimensions if Dim.Auto is not in play
  1061. [Fact]
  1062. public void Not_Used_TextFormatter_Does_Not_Change_View_Size ()
  1063. {
  1064. View view = new ()
  1065. {
  1066. Text = "_1234"
  1067. };
  1068. Assert.Equal (Size.Empty, view.Frame.Size);
  1069. view.TextFormatter.Text = "ABC";
  1070. Assert.Equal (Size.Empty, view.Frame.Size);
  1071. view.TextFormatter.Alignment = Alignment.Fill;
  1072. Assert.Equal (Size.Empty, view.Frame.Size);
  1073. view.TextFormatter.VerticalAlignment = Alignment.Center;
  1074. Assert.Equal (Size.Empty, view.Frame.Size);
  1075. view.TextFormatter.HotKeySpecifier = (Rune)'*';
  1076. Assert.Equal (Size.Empty, view.Frame.Size);
  1077. view.TextFormatter.Text = "*ABC";
  1078. Assert.Equal (Size.Empty, view.Frame.Size);
  1079. }
  1080. [Fact]
  1081. public void Not_Used_TextSettings_Do_Not_Change_View_Size ()
  1082. {
  1083. View view = new ()
  1084. {
  1085. Text = "_1234"
  1086. };
  1087. Assert.Equal (Size.Empty, view.Frame.Size);
  1088. view.TextAlignment = Alignment.Fill;
  1089. Assert.Equal (Size.Empty, view.Frame.Size);
  1090. view.VerticalTextAlignment = Alignment.Center;
  1091. Assert.Equal (Size.Empty, view.Frame.Size);
  1092. view.HotKeySpecifier = (Rune)'*';
  1093. Assert.Equal (Size.Empty, view.Frame.Size);
  1094. view.Text = "*ABC";
  1095. Assert.Equal (Size.Empty, view.Frame.Size);
  1096. }
  1097. [Theory]
  1098. [InlineData ("", -1, Alignment.Start, false, 0)]
  1099. [InlineData (null, 0, Alignment.Start, false, 1)]
  1100. [InlineData (null, 0, Alignment.Start, true, 1)]
  1101. [InlineData ("", 0, Alignment.Start, false, 1)]
  1102. [InlineData ("", 0, Alignment.Start, true, 1)]
  1103. public void Reformat_Invalid (string text, int maxWidth, Alignment alignment, bool wrap, int linesCount)
  1104. {
  1105. if (maxWidth < 0)
  1106. {
  1107. Assert.Throws<ArgumentOutOfRangeException> (
  1108. () =>
  1109. TextFormatter.Format (text, maxWidth, alignment, wrap)
  1110. );
  1111. }
  1112. else
  1113. {
  1114. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap);
  1115. Assert.NotEmpty (list);
  1116. Assert.True (list.Count == linesCount);
  1117. Assert.Equal (string.Empty, list [0]);
  1118. }
  1119. }
  1120. [Theory]
  1121. [InlineData ("A sentence has words.\nLine 2.", 0, -29, Alignment.Start, false, 1, true)]
  1122. [InlineData ("A sentence has words.\nLine 2.", 1, -28, Alignment.Start, false, 1, false)]
  1123. [InlineData ("A sentence has words.\nLine 2.", 5, -24, Alignment.Start, false, 1, false)]
  1124. [InlineData ("A sentence has words.\nLine 2.", 28, -1, Alignment.Start, false, 1, false)]
  1125. // no clip
  1126. [InlineData ("A sentence has words.\nLine 2.", 29, 0, Alignment.Start, false, 1, false)]
  1127. [InlineData ("A sentence has words.\nLine 2.", 30, 1, Alignment.Start, false, 1, false)]
  1128. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, Alignment.Start, false, 1, true)]
  1129. [InlineData ("A sentence has words.\r\nLine 2.", 1, -29, Alignment.Start, false, 1, false)]
  1130. [InlineData ("A sentence has words.\r\nLine 2.", 5, -25, Alignment.Start, false, 1, false)]
  1131. [InlineData ("A sentence has words.\r\nLine 2.", 29, -1, Alignment.Start, false, 1, false, 1)]
  1132. [InlineData ("A sentence has words.\r\nLine 2.", 30, 0, Alignment.Start, false, 1, false)]
  1133. [InlineData ("A sentence has words.\r\nLine 2.", 31, 1, Alignment.Start, false, 1, false)]
  1134. public void Reformat_NoWordrap_NewLines_MultiLine_False (
  1135. string text,
  1136. int maxWidth,
  1137. int widthOffset,
  1138. Alignment alignment,
  1139. bool wrap,
  1140. int linesCount,
  1141. bool stringEmpty,
  1142. int clipWidthOffset = 0
  1143. )
  1144. {
  1145. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1146. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth) + clipWidthOffset;
  1147. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap);
  1148. Assert.NotEmpty (list);
  1149. Assert.True (list.Count == linesCount);
  1150. if (stringEmpty)
  1151. {
  1152. Assert.Equal (string.Empty, list [0]);
  1153. }
  1154. else
  1155. {
  1156. Assert.NotEqual (string.Empty, list [0]);
  1157. }
  1158. if (text.Contains ("\r\n") && maxWidth > 0)
  1159. {
  1160. Assert.Equal (
  1161. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth])
  1162. .Replace ("\r\n", " "),
  1163. list [0]
  1164. );
  1165. }
  1166. else if (text.Contains ('\n') && maxWidth > 0)
  1167. {
  1168. Assert.Equal (
  1169. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth])
  1170. .Replace ("\n", " "),
  1171. list [0]
  1172. );
  1173. }
  1174. else
  1175. {
  1176. Assert.Equal (StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]), list [0]);
  1177. }
  1178. }
  1179. [Theory]
  1180. [InlineData ("A sentence has words.\nLine 2.", 0, -29, Alignment.Start, false, 1, true, new [] { "" })]
  1181. [InlineData (
  1182. "A sentence has words.\nLine 2.",
  1183. 1,
  1184. -28,
  1185. Alignment.Start,
  1186. false,
  1187. 2,
  1188. false,
  1189. new [] { "A", "L" }
  1190. )]
  1191. [InlineData (
  1192. "A sentence has words.\nLine 2.",
  1193. 5,
  1194. -24,
  1195. Alignment.Start,
  1196. false,
  1197. 2,
  1198. false,
  1199. new [] { "A sen", "Line " }
  1200. )]
  1201. [InlineData (
  1202. "A sentence has words.\nLine 2.",
  1203. 28,
  1204. -1,
  1205. Alignment.Start,
  1206. false,
  1207. 2,
  1208. false,
  1209. new [] { "A sentence has words.", "Line 2." }
  1210. )]
  1211. //// no clip
  1212. [InlineData (
  1213. "A sentence has words.\nLine 2.",
  1214. 29,
  1215. 0,
  1216. Alignment.Start,
  1217. false,
  1218. 2,
  1219. false,
  1220. new [] { "A sentence has words.", "Line 2." }
  1221. )]
  1222. [InlineData (
  1223. "A sentence has words.\nLine 2.",
  1224. 30,
  1225. 1,
  1226. Alignment.Start,
  1227. false,
  1228. 2,
  1229. false,
  1230. new [] { "A sentence has words.", "Line 2." }
  1231. )]
  1232. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, Alignment.Start, false, 1, true, new [] { "" })]
  1233. [InlineData (
  1234. "A sentence has words.\r\nLine 2.",
  1235. 1,
  1236. -29,
  1237. Alignment.Start,
  1238. false,
  1239. 2,
  1240. false,
  1241. new [] { "A", "L" }
  1242. )]
  1243. [InlineData (
  1244. "A sentence has words.\r\nLine 2.",
  1245. 5,
  1246. -25,
  1247. Alignment.Start,
  1248. false,
  1249. 2,
  1250. false,
  1251. new [] { "A sen", "Line " }
  1252. )]
  1253. [InlineData (
  1254. "A sentence has words.\r\nLine 2.",
  1255. 29,
  1256. -1,
  1257. Alignment.Start,
  1258. false,
  1259. 2,
  1260. false,
  1261. new [] { "A sentence has words.", "Line 2." }
  1262. )]
  1263. [InlineData (
  1264. "A sentence has words.\r\nLine 2.",
  1265. 30,
  1266. 0,
  1267. Alignment.Start,
  1268. false,
  1269. 2,
  1270. false,
  1271. new [] { "A sentence has words.", "Line 2." }
  1272. )]
  1273. [InlineData (
  1274. "A sentence has words.\r\nLine 2.",
  1275. 31,
  1276. 1,
  1277. Alignment.Start,
  1278. false,
  1279. 2,
  1280. false,
  1281. new [] { "A sentence has words.", "Line 2." }
  1282. )]
  1283. public void Reformat_NoWordrap_NewLines_MultiLine_True (
  1284. string text,
  1285. int maxWidth,
  1286. int widthOffset,
  1287. Alignment alignment,
  1288. bool wrap,
  1289. int linesCount,
  1290. bool stringEmpty,
  1291. IEnumerable<string> resultLines
  1292. )
  1293. {
  1294. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1295. List<string> list = TextFormatter.Format (
  1296. text,
  1297. maxWidth,
  1298. alignment,
  1299. wrap,
  1300. false,
  1301. 0,
  1302. TextDirection.LeftRight_TopBottom,
  1303. true
  1304. );
  1305. Assert.NotEmpty (list);
  1306. Assert.True (list.Count == linesCount);
  1307. if (stringEmpty)
  1308. {
  1309. Assert.Equal (string.Empty, list [0]);
  1310. }
  1311. else
  1312. {
  1313. Assert.NotEqual (string.Empty, list [0]);
  1314. }
  1315. Assert.Equal (list, resultLines);
  1316. }
  1317. [Theory]
  1318. [InlineData ("A sentence has words.\nLine 2.", 0, -29, Alignment.Start, false, 1, true, new [] { "" })]
  1319. [InlineData (
  1320. "A sentence has words.\nLine 2.",
  1321. 1,
  1322. -28,
  1323. Alignment.Start,
  1324. false,
  1325. 2,
  1326. false,
  1327. new [] { "A", "L" }
  1328. )]
  1329. [InlineData (
  1330. "A sentence has words.\nLine 2.",
  1331. 5,
  1332. -24,
  1333. Alignment.Start,
  1334. false,
  1335. 2,
  1336. false,
  1337. new [] { "A sen", "Line " }
  1338. )]
  1339. [InlineData (
  1340. "A sentence has words.\nLine 2.",
  1341. 28,
  1342. -1,
  1343. Alignment.Start,
  1344. false,
  1345. 2,
  1346. false,
  1347. new [] { "A sentence has words.", "Line 2." }
  1348. )]
  1349. //// no clip
  1350. [InlineData (
  1351. "A sentence has words.\nLine 2.",
  1352. 29,
  1353. 0,
  1354. Alignment.Start,
  1355. false,
  1356. 2,
  1357. false,
  1358. new [] { "A sentence has words.", "Line 2." }
  1359. )]
  1360. [InlineData (
  1361. "A sentence has words.\nLine 2.",
  1362. 30,
  1363. 1,
  1364. Alignment.Start,
  1365. false,
  1366. 2,
  1367. false,
  1368. new [] { "A sentence has words.", "Line 2." }
  1369. )]
  1370. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, Alignment.Start, false, 1, true, new [] { "" })]
  1371. [InlineData (
  1372. "A sentence has words.\r\nLine 2.",
  1373. 1,
  1374. -29,
  1375. Alignment.Start,
  1376. false,
  1377. 2,
  1378. false,
  1379. new [] { "A", "L" }
  1380. )]
  1381. [InlineData (
  1382. "A sentence has words.\r\nLine 2.",
  1383. 5,
  1384. -25,
  1385. Alignment.Start,
  1386. false,
  1387. 2,
  1388. false,
  1389. new [] { "A sen", "Line " }
  1390. )]
  1391. [InlineData (
  1392. "A sentence has words.\r\nLine 2.",
  1393. 29,
  1394. -1,
  1395. Alignment.Start,
  1396. false,
  1397. 2,
  1398. false,
  1399. new [] { "A sentence has words.", "Line 2." }
  1400. )]
  1401. [InlineData (
  1402. "A sentence has words.\r\nLine 2.",
  1403. 30,
  1404. 0,
  1405. Alignment.Start,
  1406. false,
  1407. 2,
  1408. false,
  1409. new [] { "A sentence has words.", "Line 2." }
  1410. )]
  1411. [InlineData (
  1412. "A sentence has words.\r\nLine 2.",
  1413. 31,
  1414. 1,
  1415. Alignment.Start,
  1416. false,
  1417. 2,
  1418. false,
  1419. new [] { "A sentence has words.", "Line 2." }
  1420. )]
  1421. public void Reformat_NoWordrap_NewLines_MultiLine_True_Vertical (
  1422. string text,
  1423. int maxWidth,
  1424. int widthOffset,
  1425. Alignment alignment,
  1426. bool wrap,
  1427. int linesCount,
  1428. bool stringEmpty,
  1429. IEnumerable<string> resultLines
  1430. )
  1431. {
  1432. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1433. List<string> list = TextFormatter.Format (
  1434. text,
  1435. maxWidth,
  1436. alignment,
  1437. wrap,
  1438. false,
  1439. 0,
  1440. TextDirection.TopBottom_LeftRight,
  1441. true
  1442. );
  1443. Assert.NotEmpty (list);
  1444. Assert.True (list.Count == linesCount);
  1445. if (stringEmpty)
  1446. {
  1447. Assert.Equal (string.Empty, list [0]);
  1448. }
  1449. else
  1450. {
  1451. Assert.NotEqual (string.Empty, list [0]);
  1452. }
  1453. Assert.Equal (list, resultLines);
  1454. }
  1455. [Theory]
  1456. [InlineData ("", 0, 0, Alignment.Start, false, 1, true)]
  1457. [InlineData ("", 1, 1, Alignment.Start, false, 1, true)]
  1458. [InlineData ("A sentence has words.", 0, -21, Alignment.Start, false, 1, true)]
  1459. [InlineData ("A sentence has words.", 1, -20, Alignment.Start, false, 1, false)]
  1460. [InlineData ("A sentence has words.", 5, -16, Alignment.Start, false, 1, false)]
  1461. [InlineData ("A sentence has words.", 20, -1, Alignment.Start, false, 1, false)]
  1462. // no clip
  1463. [InlineData ("A sentence has words.", 21, 0, Alignment.Start, false, 1, false)]
  1464. [InlineData ("A sentence has words.", 22, 1, Alignment.Start, false, 1, false)]
  1465. public void Reformat_NoWordrap_SingleLine (
  1466. string text,
  1467. int maxWidth,
  1468. int widthOffset,
  1469. Alignment alignment,
  1470. bool wrap,
  1471. int linesCount,
  1472. bool stringEmpty
  1473. )
  1474. {
  1475. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1476. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1477. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap);
  1478. Assert.NotEmpty (list);
  1479. Assert.True (list.Count == linesCount);
  1480. if (stringEmpty)
  1481. {
  1482. Assert.Equal (string.Empty, list [0]);
  1483. }
  1484. else
  1485. {
  1486. Assert.NotEqual (string.Empty, list [0]);
  1487. }
  1488. Assert.Equal (StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]), list [0]);
  1489. }
  1490. [Theory]
  1491. // Unicode
  1492. [InlineData (
  1493. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1494. 8,
  1495. -1,
  1496. Alignment.Start,
  1497. true,
  1498. false,
  1499. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1500. )]
  1501. // no clip
  1502. [InlineData (
  1503. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1504. 9,
  1505. 0,
  1506. Alignment.Start,
  1507. true,
  1508. false,
  1509. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1510. )]
  1511. [InlineData (
  1512. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1513. 10,
  1514. 1,
  1515. Alignment.Start,
  1516. true,
  1517. false,
  1518. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1519. )]
  1520. public void Reformat_Unicode_Wrap_Spaces_NewLines (
  1521. string text,
  1522. int maxWidth,
  1523. int widthOffset,
  1524. Alignment alignment,
  1525. bool wrap,
  1526. bool preserveTrailingSpaces,
  1527. IEnumerable<string> resultLines
  1528. )
  1529. {
  1530. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1531. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap, preserveTrailingSpaces);
  1532. Assert.Equal (list.Count, resultLines.Count ());
  1533. Assert.Equal (resultLines, list);
  1534. }
  1535. [Theory]
  1536. // Unicode
  1537. // Even # of chars
  1538. // 0123456789
  1539. [InlineData ("\u2660пÑРвРÑ", 10, -1, Alignment.Start, true, false, new [] { "\u2660пÑРвÐ", "Ñ" })]
  1540. // no clip
  1541. [InlineData ("\u2660пÑРвРÑ", 11, 0, Alignment.Start, true, false, new [] { "\u2660пÑРвРÑ" })]
  1542. [InlineData ("\u2660пÑРвРÑ", 12, 1, Alignment.Start, true, false, new [] { "\u2660пÑРвРÑ" })]
  1543. // Unicode
  1544. // Odd # of chars
  1545. // 0123456789
  1546. [InlineData ("\u2660 ÑРвРÑ", 9, -1, Alignment.Start, true, false, new [] { "\u2660 ÑРвÐ", "Ñ" })]
  1547. // no clip
  1548. [InlineData ("\u2660 ÑРвРÑ", 10, 0, Alignment.Start, true, false, new [] { "\u2660 ÑРвРÑ" })]
  1549. [InlineData ("\u2660 ÑРвРÑ", 11, 1, Alignment.Start, true, false, new [] { "\u2660 ÑРвРÑ" })]
  1550. public void Reformat_Unicode_Wrap_Spaces_No_NewLines (
  1551. string text,
  1552. int maxWidth,
  1553. int widthOffset,
  1554. Alignment alignment,
  1555. bool wrap,
  1556. bool preserveTrailingSpaces,
  1557. IEnumerable<string> resultLines
  1558. )
  1559. {
  1560. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1561. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap, preserveTrailingSpaces);
  1562. Assert.Equal (list.Count, resultLines.Count ());
  1563. Assert.Equal (resultLines, list);
  1564. }
  1565. [Theory]
  1566. // Even # of spaces
  1567. // 0123456789
  1568. [InlineData ("012 456 89", 0, -10, Alignment.Start, true, true, true, new [] { "" })]
  1569. [InlineData (
  1570. "012 456 89",
  1571. 1,
  1572. -9,
  1573. Alignment.Start,
  1574. true,
  1575. true,
  1576. false,
  1577. new [] { "0", "1", "2", " ", "4", "5", "6", " ", "8", "9" },
  1578. "01245689"
  1579. )]
  1580. [InlineData ("012 456 89", 5, -5, Alignment.Start, true, true, false, new [] { "012 ", "456 ", "89" })]
  1581. [InlineData ("012 456 89", 9, -1, Alignment.Start, true, true, false, new [] { "012 456 ", "89" })]
  1582. // no clip
  1583. [InlineData ("012 456 89", 10, 0, Alignment.Start, true, true, false, new [] { "012 456 89" })]
  1584. [InlineData ("012 456 89", 11, 1, Alignment.Start, true, true, false, new [] { "012 456 89" })]
  1585. // Odd # of spaces
  1586. // 01234567890123
  1587. [InlineData ("012 456 89 end", 13, -1, Alignment.Start, true, true, false, new [] { "012 456 89 ", "end" })]
  1588. // no clip
  1589. [InlineData ("012 456 89 end", 14, 0, Alignment.Start, true, true, false, new [] { "012 456 89 end" })]
  1590. [InlineData ("012 456 89 end", 15, 1, Alignment.Start, true, true, false, new [] { "012 456 89 end" })]
  1591. public void Reformat_Wrap_Spaces_No_NewLines (
  1592. string text,
  1593. int maxWidth,
  1594. int widthOffset,
  1595. Alignment alignment,
  1596. bool wrap,
  1597. bool preserveTrailingSpaces,
  1598. bool stringEmpty,
  1599. IEnumerable<string> resultLines,
  1600. string noSpaceText = ""
  1601. )
  1602. {
  1603. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1604. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1605. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap, preserveTrailingSpaces);
  1606. Assert.NotEmpty (list);
  1607. Assert.True (list.Count == resultLines.Count ());
  1608. if (stringEmpty)
  1609. {
  1610. Assert.Equal (string.Empty, list [0]);
  1611. }
  1612. else
  1613. {
  1614. Assert.NotEqual (string.Empty, list [0]);
  1615. }
  1616. Assert.Equal (resultLines, list);
  1617. if (maxWidth > 0)
  1618. {
  1619. // remove whitespace chars
  1620. if (maxWidth < 5)
  1621. {
  1622. expectedClippedWidth = text.GetRuneCount () - text.Sum (r => r == ' ' ? 1 : 0);
  1623. }
  1624. else
  1625. {
  1626. expectedClippedWidth = Math.Min (
  1627. text.GetRuneCount (),
  1628. maxWidth - text.Sum (r => r == ' ' ? 1 : 0)
  1629. );
  1630. }
  1631. list = TextFormatter.Format (text, maxWidth, Alignment.Start, wrap);
  1632. if (maxWidth == 1)
  1633. {
  1634. Assert.Equal (expectedClippedWidth, list.Count);
  1635. Assert.Equal (noSpaceText, string.Concat (list.ToArray ()));
  1636. }
  1637. if (maxWidth > 1 && maxWidth < 10)
  1638. {
  1639. Assert.Equal (
  1640. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]),
  1641. list [0]
  1642. );
  1643. }
  1644. }
  1645. }
  1646. [Theory]
  1647. [InlineData (null)]
  1648. [InlineData ("")]
  1649. [InlineData ("a")]
  1650. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal (string text)
  1651. {
  1652. var hotKeySpecifier = (Rune)'_';
  1653. if (text == null)
  1654. {
  1655. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  1656. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  1657. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  1658. }
  1659. else
  1660. {
  1661. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  1662. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  1663. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  1664. }
  1665. }
  1666. [Theory]
  1667. [InlineData ("all lower case", 0)]
  1668. [InlineData ("K Before", 0)]
  1669. [InlineData ("aK Second", 1)]
  1670. [InlineData ("Last K", 5)]
  1671. [InlineData ("fter K", 7)]
  1672. [InlineData ("Multiple K and R", 9)]
  1673. [InlineData ("Non-english: Кдать", 13)]
  1674. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal (string text, int hotPos)
  1675. {
  1676. var hotKeySpecifier = (Rune)'_';
  1677. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  1678. }
  1679. [Theory]
  1680. [InlineData ("_K Before", 0, "K Before")]
  1681. [InlineData ("a_K Second", 1, "aK Second")]
  1682. [InlineData ("Last _K", 5, "Last K")]
  1683. [InlineData ("After K_", 7, "After K")]
  1684. [InlineData ("Multiple _K and _R", 9, "Multiple K and _R")]
  1685. [InlineData ("Non-english: _Кдать", 13, "Non-english: Кдать")]
  1686. public void RemoveHotKeySpecifier_Valid_ReturnsStripped (string text, int hotPos, string expectedText)
  1687. {
  1688. var hotKeySpecifier = (Rune)'_';
  1689. Assert.Equal (expectedText, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  1690. }
  1691. [Theory]
  1692. [InlineData ("test", 0, 't', "test")]
  1693. [InlineData ("test", 1, 'e', "test")]
  1694. [InlineData ("Ok", 0, 'O', "Ok")]
  1695. [InlineData ("[◦ Ok ◦]", 3, 'O', "[◦ Ok ◦]")]
  1696. [InlineData ("^k", 0, '^', "^k")]
  1697. public void ReplaceHotKeyWithTag (string text, int hotPos, uint tag, string expected)
  1698. {
  1699. var tf = new TextFormatter ();
  1700. List<Rune> runes = text.ToRuneList ();
  1701. Rune rune;
  1702. if (Rune.TryGetRuneAt (text, hotPos, out rune))
  1703. {
  1704. Assert.Equal (rune, (Rune)tag);
  1705. }
  1706. string result = TextFormatter.ReplaceHotKeyWithTag (text, hotPos);
  1707. Assert.Equal (result, expected);
  1708. Assert.Equal ((Rune)tag, result.ToRunes () [hotPos]);
  1709. Assert.Equal (text.GetRuneCount (), runes.Count);
  1710. Assert.Equal (text, StringExtensions.ToString (runes));
  1711. }
  1712. public static IEnumerable<object []> SplitEnvironmentNewLine =>
  1713. new List<object []>
  1714. {
  1715. new object []
  1716. {
  1717. $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界",
  1718. new [] { "First Line 界", "Second Line 界", "Third Line 界" }
  1719. },
  1720. new object []
  1721. {
  1722. $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界{Environment.NewLine}",
  1723. new [] { "First Line 界", "Second Line 界", "Third Line 界", "" }
  1724. }
  1725. };
  1726. [Theory]
  1727. [MemberData (nameof (SplitEnvironmentNewLine))]
  1728. public void SplitNewLine_Ending__With_Or_Without_NewLine_Probably_CRLF (
  1729. string text,
  1730. IEnumerable<string> expected
  1731. )
  1732. {
  1733. List<string> splited = TextFormatter.SplitNewLine (text);
  1734. Assert.Equal (expected, splited);
  1735. }
  1736. [Theory]
  1737. [InlineData (
  1738. "First Line 界\nSecond Line 界\nThird Line 界\n",
  1739. new [] { "First Line 界", "Second Line 界", "Third Line 界", "" }
  1740. )]
  1741. public void SplitNewLine_Ending_With_NewLine_Only_LF (string text, IEnumerable<string> expected)
  1742. {
  1743. List<string> splited = TextFormatter.SplitNewLine (text);
  1744. Assert.Equal (expected, splited);
  1745. }
  1746. [Theory]
  1747. [InlineData (
  1748. "First Line 界\nSecond Line 界\nThird Line 界",
  1749. new [] { "First Line 界", "Second Line 界", "Third Line 界" }
  1750. )]
  1751. public void SplitNewLine_Ending_Without_NewLine_Only_LF (string text, IEnumerable<string> expected)
  1752. {
  1753. List<string> splited = TextFormatter.SplitNewLine (text);
  1754. Assert.Equal (expected, splited);
  1755. }
  1756. [Theory]
  1757. [InlineData ("New Test 你", 10, 10, 20320, 20320, 9, "你")]
  1758. [InlineData ("New Test \U0001d539", 10, 11, 120121, 55349, 9, "𝔹")]
  1759. public void String_Array_Is_Not_Always_Equal_ToRunes_Array (
  1760. string text,
  1761. int runesLength,
  1762. int stringLength,
  1763. int runeValue,
  1764. int stringValue,
  1765. int index,
  1766. string expected
  1767. )
  1768. {
  1769. Rune [] usToRunes = text.ToRunes ();
  1770. Assert.Equal (runesLength, usToRunes.Length);
  1771. Assert.Equal (stringLength, text.Length);
  1772. Assert.Equal (runeValue, usToRunes [index].Value);
  1773. Assert.Equal (stringValue, text [index]);
  1774. Assert.Equal (expected, usToRunes [index].ToString ());
  1775. if (char.IsHighSurrogate (text [index]))
  1776. {
  1777. // Rune array length isn't equal to string array
  1778. Assert.Equal (expected, new (new [] { text [index], text [index + 1] }));
  1779. }
  1780. else
  1781. {
  1782. // Rune array length is equal to string array
  1783. Assert.Equal (expected, text [index].ToString ());
  1784. }
  1785. }
  1786. [Theory]
  1787. [InlineData ("123456789", 3, "123")]
  1788. [InlineData ("Hello World", 8, "Hello Wo")]
  1789. public void TestClipOrPad_LongWord (string text, int fillPad, string expectedText)
  1790. {
  1791. // word is long but we want it to fill # space only
  1792. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1793. }
  1794. [Theory]
  1795. [InlineData ("fff", 6, "fff ")]
  1796. [InlineData ("Hello World", 16, "Hello World ")]
  1797. public void TestClipOrPad_ShortWord (string text, int fillPad, string expectedText)
  1798. {
  1799. // word is short but we want it to fill # so it should be padded
  1800. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1801. }
  1802. [Theory]
  1803. [InlineData ("你", TextDirection.LeftRight_TopBottom, 2, 1)]
  1804. [InlineData ("你", TextDirection.TopBottom_LeftRight, 2, 1)]
  1805. [InlineData ("你你", TextDirection.LeftRight_TopBottom, 4, 1)]
  1806. [InlineData ("你你", TextDirection.TopBottom_LeftRight, 2, 2)]
  1807. public void Text_Set_SizeIsCorrect (string text, TextDirection textDirection, int expectedWidth, int expectedHeight)
  1808. {
  1809. var tf = new TextFormatter { Direction = textDirection, Text = text };
  1810. tf.ConstrainToWidth = 10;
  1811. tf.ConstrainToHeight = 10;
  1812. Assert.Equal (new (expectedWidth, expectedHeight), tf.FormatAndGetSize ());
  1813. }
  1814. [Fact]
  1815. public void WordWrap_BigWidth ()
  1816. {
  1817. List<string> wrappedLines;
  1818. var text = "Constantinople";
  1819. wrappedLines = TextFormatter.WordWrapText (text, 100);
  1820. Assert.True (wrappedLines.Count == 1);
  1821. Assert.Equal ("Constantinople", wrappedLines [0]);
  1822. }
  1823. [Fact]
  1824. public void WordWrap_Invalid ()
  1825. {
  1826. var text = string.Empty;
  1827. var width = 0;
  1828. Assert.Empty (TextFormatter.WordWrapText (null, width));
  1829. Assert.Empty (TextFormatter.WordWrapText (text, width));
  1830. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrapText (text, -1));
  1831. }
  1832. [Theory]
  1833. [InlineData ("A sentence has words.", 3, -18, new [] { "A", "sen", "ten", "ce", "has", "wor", "ds." })]
  1834. [InlineData (
  1835. "A sentence has words.",
  1836. 2,
  1837. -19,
  1838. new [] { "A", "se", "nt", "en", "ce", "ha", "s", "wo", "rd", "s." }
  1839. )]
  1840. [InlineData (
  1841. "A sentence has words.",
  1842. 1,
  1843. -20,
  1844. new [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." }
  1845. )]
  1846. public void WordWrap_Narrow_Default (
  1847. string text,
  1848. int maxWidth,
  1849. int widthOffset,
  1850. IEnumerable<string> resultLines
  1851. )
  1852. {
  1853. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  1854. List<string> wrappedLines;
  1855. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1856. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1857. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  1858. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1859. Assert.True (
  1860. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  1861. );
  1862. Assert.True (
  1863. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  1864. );
  1865. Assert.Equal (resultLines, wrappedLines);
  1866. }
  1867. [Theory]
  1868. [InlineData ("A sentence has words.", 21, 0, new [] { "A sentence has words." })]
  1869. [InlineData ("A sentence has words.", 20, -1, new [] { "A sentence has", "words." })]
  1870. [InlineData ("A sentence has words.", 15, -6, new [] { "A sentence has", "words." })]
  1871. [InlineData ("A sentence has words.", 14, -7, new [] { "A sentence has", "words." })]
  1872. [InlineData ("A sentence has words.", 13, -8, new [] { "A sentence", "has words." })]
  1873. // Unicode
  1874. [InlineData (
  1875. "A Unicode sentence (пÑивеÑ) has words.",
  1876. 42,
  1877. 0,
  1878. new [] { "A Unicode sentence (пÑивеÑ) has words." }
  1879. )]
  1880. [InlineData (
  1881. "A Unicode sentence (пÑивеÑ) has words.",
  1882. 41,
  1883. -1,
  1884. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  1885. )]
  1886. [InlineData (
  1887. "A Unicode sentence (пÑивеÑ) has words.",
  1888. 36,
  1889. -6,
  1890. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  1891. )]
  1892. [InlineData (
  1893. "A Unicode sentence (пÑивеÑ) has words.",
  1894. 35,
  1895. -7,
  1896. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  1897. )]
  1898. [InlineData (
  1899. "A Unicode sentence (пÑивеÑ) has words.",
  1900. 34,
  1901. -8,
  1902. new [] { "A Unicode sentence (пÑивеÑ)", "has words." }
  1903. )]
  1904. [InlineData (
  1905. "A Unicode sentence (пÑивеÑ) has words.",
  1906. 25,
  1907. -17,
  1908. new [] { "A Unicode sentence", "(пÑивеÑ) has words." }
  1909. )]
  1910. public void WordWrap_NoNewLines_Default (
  1911. string text,
  1912. int maxWidth,
  1913. int widthOffset,
  1914. IEnumerable<string> resultLines
  1915. )
  1916. {
  1917. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  1918. List<string> wrappedLines;
  1919. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1920. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1921. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  1922. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1923. Assert.True (
  1924. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  1925. );
  1926. Assert.True (
  1927. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  1928. );
  1929. Assert.Equal (resultLines, wrappedLines);
  1930. }
  1931. [Theory]
  1932. [InlineData ("これが最初の行です。 こんにちは世界。 これが2行目です。", 29, 0, new [] { "これが最初の行です。", "こんにちは世界。", "これが2行目です。" })]
  1933. public void WordWrap_PreserveTrailingSpaces_False_Unicode_Wide_Runes (
  1934. string text,
  1935. int maxWidth,
  1936. int widthOffset,
  1937. IEnumerable<string> resultLines
  1938. )
  1939. {
  1940. List<string> wrappedLines;
  1941. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1942. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1943. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  1944. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1945. Assert.True (
  1946. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  1947. );
  1948. Assert.True (
  1949. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  1950. );
  1951. Assert.Equal (resultLines, wrappedLines);
  1952. }
  1953. [Theory]
  1954. [InlineData ("文に は言葉 があり ます。", 14, 0, new [] { "文に は言葉", "があり ます。" })]
  1955. [InlineData ("文に は言葉 があり ます。", 3, -11, new [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  1956. [InlineData ("文に は言葉 があり ます。", 2, -12, new [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  1957. [InlineData (
  1958. "文に は言葉 があり ます。",
  1959. 1,
  1960. -13,
  1961. new [] { " ", " ", " " }
  1962. )] // Just Spaces; should result in a single space for each line
  1963. public void WordWrap_PreserveTrailingSpaces_False_Wide_Runes (
  1964. string text,
  1965. int maxWidth,
  1966. int widthOffset,
  1967. IEnumerable<string> resultLines
  1968. )
  1969. {
  1970. List<string> wrappedLines;
  1971. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1972. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1973. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  1974. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1975. Assert.True (
  1976. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  1977. );
  1978. Assert.True (
  1979. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  1980. );
  1981. Assert.Equal (resultLines, wrappedLines);
  1982. }
  1983. [Theory]
  1984. [InlineData (null, 1, new string [] { })] // null input
  1985. [InlineData ("", 1, new string [] { })] // Empty input
  1986. [InlineData ("1 34", 1, new [] { "1", "3", "4" })] // Single Spaces
  1987. [InlineData ("1", 1, new [] { "1" })] // Short input
  1988. [InlineData ("12", 1, new [] { "1", "2" })]
  1989. [InlineData ("123", 1, new [] { "1", "2", "3" })]
  1990. [InlineData ("123456", 1, new [] { "1", "2", "3", "4", "5", "6" })] // No spaces
  1991. [InlineData (" ", 1, new [] { " " })] // Just Spaces; should result in a single space
  1992. [InlineData (" ", 1, new [] { " " })]
  1993. [InlineData (" ", 1, new [] { " ", " " })]
  1994. [InlineData (" ", 1, new [] { " ", " " })]
  1995. [InlineData ("12 456", 1, new [] { "1", "2", "4", "5", "6" })] // Single Spaces
  1996. [InlineData (" 2 456", 1, new [] { " ", "2", "4", "5", "6" })] // Leading spaces should be preserved.
  1997. [InlineData (" 2 456 8", 1, new [] { " ", "2", "4", "5", "6", "8" })]
  1998. [InlineData (
  1999. "A sentence has words. ",
  2000. 1,
  2001. new [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." }
  2002. )] // Complex example
  2003. [InlineData ("12 567", 1, new [] { "1", "2", " ", "5", "6", "7" })] // Double Spaces
  2004. [InlineData (" 3 567", 1, new [] { " ", "3", "5", "6", "7" })] // Double Leading spaces should be preserved.
  2005. [InlineData (" 3 678 1", 1, new [] { " ", "3", " ", "6", "7", "8", " ", "1" })]
  2006. [InlineData ("1 456", 1, new [] { "1", " ", "4", "5", "6" })]
  2007. [InlineData (
  2008. "A sentence has words. ",
  2009. 1,
  2010. new []
  2011. {
  2012. "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", "w", "o", "r", "d", "s", ".", " "
  2013. }
  2014. )] // Double space Complex example
  2015. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_1 (
  2016. string text,
  2017. int width,
  2018. IEnumerable<string> resultLines
  2019. )
  2020. {
  2021. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2022. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2023. Assert.Equal (resultLines, wrappedLines);
  2024. var breakLines = "";
  2025. foreach (string line in wrappedLines)
  2026. {
  2027. breakLines += $"{line}{Environment.NewLine}";
  2028. }
  2029. var expected = string.Empty;
  2030. foreach (string line in resultLines)
  2031. {
  2032. expected += $"{line}{Environment.NewLine}";
  2033. }
  2034. Assert.Equal (expected, breakLines);
  2035. }
  2036. [Theory]
  2037. [InlineData (null, 3, new string [] { })] // null input
  2038. [InlineData ("", 3, new string [] { })] // Empty input
  2039. [InlineData ("1", 3, new [] { "1" })] // Short input
  2040. [InlineData ("12", 3, new [] { "12" })]
  2041. [InlineData ("123", 3, new [] { "123" })]
  2042. [InlineData ("123456", 3, new [] { "123", "456" })] // No spaces
  2043. [InlineData ("1234567", 3, new [] { "123", "456", "7" })] // No spaces
  2044. [InlineData (" ", 3, new [] { " " })] // Just Spaces; should result in a single space
  2045. [InlineData (" ", 3, new [] { " " })]
  2046. [InlineData (" ", 3, new [] { " " })]
  2047. [InlineData (" ", 3, new [] { " " })]
  2048. [InlineData ("12 456", 3, new [] { "12", "456" })] // Single Spaces
  2049. [InlineData (" 2 456", 3, new [] { " 2", "456" })] // Leading spaces should be preserved.
  2050. [InlineData (" 2 456 8", 3, new [] { " 2", "456", "8" })]
  2051. [InlineData (
  2052. "A sentence has words. ",
  2053. 3,
  2054. new [] { "A", "sen", "ten", "ce", "has", "wor", "ds." }
  2055. )] // Complex example
  2056. [InlineData ("12 567", 3, new [] { "12 ", "567" })] // Double Spaces
  2057. [InlineData (" 3 567", 3, new [] { " 3", "567" })] // Double Leading spaces should be preserved.
  2058. [InlineData (" 3 678 1", 3, new [] { " 3", " 67", "8 ", "1" })]
  2059. [InlineData ("1 456", 3, new [] { "1 ", "456" })]
  2060. [InlineData (
  2061. "A sentence has words. ",
  2062. 3,
  2063. new [] { "A ", "sen", "ten", "ce ", " ", "has", "wor", "ds.", " " }
  2064. )] // Double space Complex example
  2065. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_3 (
  2066. string text,
  2067. int width,
  2068. IEnumerable<string> resultLines
  2069. )
  2070. {
  2071. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2072. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2073. Assert.Equal (resultLines, wrappedLines);
  2074. var breakLines = "";
  2075. foreach (string line in wrappedLines)
  2076. {
  2077. breakLines += $"{line}{Environment.NewLine}";
  2078. }
  2079. var expected = string.Empty;
  2080. foreach (string line in resultLines)
  2081. {
  2082. expected += $"{line}{Environment.NewLine}";
  2083. }
  2084. Assert.Equal (expected, breakLines);
  2085. }
  2086. [Theory]
  2087. [InlineData (null, 50, new string [] { })] // null input
  2088. [InlineData ("", 50, new string [] { })] // Empty input
  2089. [InlineData ("1", 50, new [] { "1" })] // Short input
  2090. [InlineData ("12", 50, new [] { "12" })]
  2091. [InlineData ("123", 50, new [] { "123" })]
  2092. [InlineData ("123456", 50, new [] { "123456" })] // No spaces
  2093. [InlineData ("1234567", 50, new [] { "1234567" })] // No spaces
  2094. [InlineData (" ", 50, new [] { " " })] // Just Spaces; should result in a single space
  2095. [InlineData (" ", 50, new [] { " " })]
  2096. [InlineData (" ", 50, new [] { " " })]
  2097. [InlineData ("12 456", 50, new [] { "12 456" })] // Single Spaces
  2098. [InlineData (" 2 456", 50, new [] { " 2 456" })] // Leading spaces should be preserved.
  2099. [InlineData (" 2 456 8", 50, new [] { " 2 456 8" })]
  2100. [InlineData ("A sentence has words. ", 50, new [] { "A sentence has words. " })] // Complex example
  2101. [InlineData ("12 567", 50, new [] { "12 567" })] // Double Spaces
  2102. [InlineData (" 3 567", 50, new [] { " 3 567" })] // Double Leading spaces should be preserved.
  2103. [InlineData (" 3 678 1", 50, new [] { " 3 678 1" })]
  2104. [InlineData ("1 456", 50, new [] { "1 456" })]
  2105. [InlineData (
  2106. "A sentence has words. ",
  2107. 50,
  2108. new [] { "A sentence has words. " }
  2109. )] // Double space Complex example
  2110. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_50 (
  2111. string text,
  2112. int width,
  2113. IEnumerable<string> resultLines
  2114. )
  2115. {
  2116. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2117. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2118. Assert.Equal (resultLines, wrappedLines);
  2119. var breakLines = "";
  2120. foreach (string line in wrappedLines)
  2121. {
  2122. breakLines += $"{line}{Environment.NewLine}";
  2123. }
  2124. var expected = string.Empty;
  2125. foreach (string line in resultLines)
  2126. {
  2127. expected += $"{line}{Environment.NewLine}";
  2128. }
  2129. Assert.Equal (expected, breakLines);
  2130. }
  2131. [Theory]
  2132. [InlineData ("A sentence has words.", 14, -7, new [] { "A sentence ", "has words." })]
  2133. [InlineData ("A sentence has words.", 8, -13, new [] { "A ", "sentence", " has ", "words." })]
  2134. [InlineData ("A sentence has words.", 6, -15, new [] { "A ", "senten", "ce ", "has ", "words." })]
  2135. [InlineData ("A sentence has words.", 3, -18, new [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds." })]
  2136. [InlineData (
  2137. "A sentence has words.",
  2138. 2,
  2139. -19,
  2140. new [] { "A ", "se", "nt", "en", "ce", " ", "ha", "s ", "wo", "rd", "s." }
  2141. )]
  2142. [InlineData (
  2143. "A sentence has words.",
  2144. 1,
  2145. -20,
  2146. new []
  2147. {
  2148. "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", " ", "w", "o", "r", "d", "s", "."
  2149. }
  2150. )]
  2151. public void WordWrap_PreserveTrailingSpaces_True (
  2152. string text,
  2153. int maxWidth,
  2154. int widthOffset,
  2155. IEnumerable<string> resultLines
  2156. )
  2157. {
  2158. List<string> wrappedLines;
  2159. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2160. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2161. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true);
  2162. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2163. Assert.True (
  2164. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2165. );
  2166. Assert.True (
  2167. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2168. );
  2169. Assert.Equal (resultLines, wrappedLines);
  2170. }
  2171. [Theory]
  2172. [InlineData ("文に は言葉 があり ます。", 14, 0, new [] { "文に は言葉 ", "があり ます。" })]
  2173. [InlineData ("文に は言葉 があり ます。", 3, -11, new [] { "文", "に ", "は", "言", "葉 ", "が", "あ", "り ", "ま", "す", "。" })]
  2174. [InlineData (
  2175. "文に は言葉 があり ます。",
  2176. 2,
  2177. -12,
  2178. new [] { "文", "に", " ", "は", "言", "葉", " ", "が", "あ", "り", " ", "ま", "す", "。" }
  2179. )]
  2180. [InlineData ("文に は言葉 があり ます。", 1, -13, new string [] { })]
  2181. public void WordWrap_PreserveTrailingSpaces_True_Wide_Runes (
  2182. string text,
  2183. int maxWidth,
  2184. int widthOffset,
  2185. IEnumerable<string> resultLines
  2186. )
  2187. {
  2188. List<string> wrappedLines;
  2189. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2190. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2191. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true);
  2192. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2193. Assert.True (
  2194. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2195. );
  2196. Assert.True (
  2197. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2198. );
  2199. Assert.Equal (resultLines, wrappedLines);
  2200. }
  2201. [Theory]
  2202. [InlineData ("A sentence has words. ", 3, new [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds.", " " })]
  2203. [InlineData (
  2204. "A sentence has words. ",
  2205. 3,
  2206. new [] { "A ", " ", "sen", "ten", "ce ", " ", " ", " ", "has", " ", "wor", "ds.", " " }
  2207. )]
  2208. public void WordWrap_PreserveTrailingSpaces_True_With_Simple_Runes_Width_3 (
  2209. string text,
  2210. int width,
  2211. IEnumerable<string> resultLines
  2212. )
  2213. {
  2214. List<string> wrappedLines = TextFormatter.WordWrapText (text, width, true);
  2215. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2216. Assert.Equal (resultLines, wrappedLines);
  2217. var breakLines = "";
  2218. foreach (string line in wrappedLines)
  2219. {
  2220. breakLines += $"{line}{Environment.NewLine}";
  2221. }
  2222. var expected = string.Empty;
  2223. foreach (string line in resultLines)
  2224. {
  2225. expected += $"{line}{Environment.NewLine}";
  2226. }
  2227. Assert.Equal (expected, breakLines);
  2228. // Double space Complex example - this is how VS 2022 does it
  2229. // which I think is not correct.
  2230. //text = "A sentence has words. ";
  2231. //breakLines = "";
  2232. //wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: true);
  2233. //foreach (var line in wrappedLines) {
  2234. // breakLines += $"{line}{Environment.NewLine}";
  2235. //}
  2236. //expected = "A " + Environment.NewLine +
  2237. // " se" + Environment.NewLine +
  2238. // " nt" + Environment.NewLine +
  2239. // " en" + Environment.NewLine +
  2240. // " ce" + Environment.NewLine +
  2241. // " " + Environment.NewLine +
  2242. // " " + Environment.NewLine +
  2243. // " " + Environment.NewLine +
  2244. // " ha" + Environment.NewLine +
  2245. // " s " + Environment.NewLine +
  2246. // " wo" + Environment.NewLine +
  2247. // " rd" + Environment.NewLine +
  2248. // " s." + Environment.NewLine;
  2249. //Assert.Equal (expected, breakLines);
  2250. }
  2251. [Theory]
  2252. [InlineData ("A sentence\t\t\t has words.", 14, -10, new [] { "A sentence\t", "\t\t has ", "words." })]
  2253. [InlineData (
  2254. "A sentence\t\t\t has words.",
  2255. 8,
  2256. -16,
  2257. new [] { "A ", "sentence", "\t\t", "\t ", "has ", "words." }
  2258. )]
  2259. [InlineData (
  2260. "A sentence\t\t\t has words.",
  2261. 3,
  2262. -21,
  2263. new [] { "A ", "sen", "ten", "ce", "\t", "\t", "\t", " ", "has", " ", "wor", "ds." }
  2264. )]
  2265. [InlineData (
  2266. "A sentence\t\t\t has words.",
  2267. 2,
  2268. -22,
  2269. new [] { "A ", "se", "nt", "en", "ce", "\t", "\t", "\t", " ", "ha", "s ", "wo", "rd", "s." }
  2270. )]
  2271. [InlineData (
  2272. "A sentence\t\t\t has words.",
  2273. 1,
  2274. -23,
  2275. new []
  2276. {
  2277. "A",
  2278. " ",
  2279. "s",
  2280. "e",
  2281. "n",
  2282. "t",
  2283. "e",
  2284. "n",
  2285. "c",
  2286. "e",
  2287. "\t",
  2288. "\t",
  2289. "\t",
  2290. " ",
  2291. "h",
  2292. "a",
  2293. "s",
  2294. " ",
  2295. "w",
  2296. "o",
  2297. "r",
  2298. "d",
  2299. "s",
  2300. "."
  2301. }
  2302. )]
  2303. public void WordWrap_PreserveTrailingSpaces_True_With_Tab (
  2304. string text,
  2305. int maxWidth,
  2306. int widthOffset,
  2307. IEnumerable<string> resultLines,
  2308. int tabWidth = 4
  2309. )
  2310. {
  2311. List<string> wrappedLines;
  2312. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2313. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2314. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true, tabWidth);
  2315. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2316. Assert.True (
  2317. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2318. );
  2319. Assert.True (
  2320. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2321. );
  2322. Assert.Equal (resultLines, wrappedLines);
  2323. }
  2324. [Theory]
  2325. [InlineData ("Constantinople", 14, 0, new [] { "Constantinople" })]
  2326. [InlineData ("Constantinople", 12, -2, new [] { "Constantinop", "le" })]
  2327. [InlineData ("Constantinople", 9, -5, new [] { "Constanti", "nople" })]
  2328. [InlineData ("Constantinople", 7, -7, new [] { "Constan", "tinople" })]
  2329. [InlineData ("Constantinople", 5, -9, new [] { "Const", "antin", "ople" })]
  2330. [InlineData ("Constantinople", 4, -10, new [] { "Cons", "tant", "inop", "le" })]
  2331. [InlineData (
  2332. "Constantinople",
  2333. 1,
  2334. -13,
  2335. new [] { "C", "o", "n", "s", "t", "a", "n", "t", "i", "n", "o", "p", "l", "e" }
  2336. )]
  2337. public void WordWrap_SingleWordLine (
  2338. string text,
  2339. int maxWidth,
  2340. int widthOffset,
  2341. IEnumerable<string> resultLines
  2342. )
  2343. {
  2344. List<string> wrappedLines;
  2345. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2346. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2347. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2348. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2349. Assert.True (
  2350. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2351. );
  2352. Assert.True (
  2353. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2354. );
  2355. Assert.Equal (resultLines, wrappedLines);
  2356. }
  2357. [Theory]
  2358. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 20, 0, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2359. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 19, -1, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2360. [InlineData (
  2361. "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.",
  2362. 19,
  2363. 0,
  2364. new [] { "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence." }
  2365. )]
  2366. public void WordWrap_Unicode_2LinesWithNonBreakingSpace (
  2367. string text,
  2368. int maxWidth,
  2369. int widthOffset,
  2370. IEnumerable<string> resultLines
  2371. )
  2372. {
  2373. List<string> wrappedLines;
  2374. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2375. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2376. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2377. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2378. Assert.True (
  2379. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2380. );
  2381. Assert.True (
  2382. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2383. );
  2384. Assert.Equal (resultLines, wrappedLines);
  2385. }
  2386. [Theory]
  2387. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 19, 0, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2388. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 18, -1, new [] { "This\u00A0is\u00A0a\u00A0sentence", "." })]
  2389. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 17, -2, new [] { "This\u00A0is\u00A0a\u00A0sentenc", "e." })]
  2390. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 14, -5, new [] { "This\u00A0is\u00A0a\u00A0sent", "ence." })]
  2391. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 10, -9, new [] { "This\u00A0is\u00A0a\u00A0", "sentence." })]
  2392. [InlineData (
  2393. "This\u00A0is\u00A0a\u00A0sentence.",
  2394. 7,
  2395. -12,
  2396. new [] { "This\u00A0is", "\u00A0a\u00A0sent", "ence." }
  2397. )]
  2398. [InlineData (
  2399. "This\u00A0is\u00A0a\u00A0sentence.",
  2400. 5,
  2401. -14,
  2402. new [] { "This\u00A0", "is\u00A0a\u00A0", "sente", "nce." }
  2403. )]
  2404. [InlineData (
  2405. "This\u00A0is\u00A0a\u00A0sentence.",
  2406. 1,
  2407. -18,
  2408. new []
  2409. {
  2410. "T", "h", "i", "s", "\u00A0", "i", "s", "\u00A0", "a", "\u00A0", "s", "e", "n", "t", "e", "n", "c", "e", "."
  2411. }
  2412. )]
  2413. public void WordWrap_Unicode_LineWithNonBreakingSpace (
  2414. string text,
  2415. int maxWidth,
  2416. int widthOffset,
  2417. IEnumerable<string> resultLines
  2418. )
  2419. {
  2420. List<string> wrappedLines;
  2421. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2422. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2423. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2424. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2425. Assert.True (
  2426. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2427. );
  2428. Assert.True (
  2429. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2430. );
  2431. Assert.Equal (resultLines, wrappedLines);
  2432. }
  2433. [Theory]
  2434. [InlineData (
  2435. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2436. 51,
  2437. 0,
  2438. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2439. )]
  2440. [InlineData (
  2441. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2442. 50,
  2443. -1,
  2444. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2445. )]
  2446. [InlineData (
  2447. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2448. 46,
  2449. -5,
  2450. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮ", "ฯะัาำ" }
  2451. )]
  2452. [InlineData (
  2453. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2454. 26,
  2455. -25,
  2456. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2457. )]
  2458. [InlineData (
  2459. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2460. 17,
  2461. -34,
  2462. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑ", "ฒณดตถทธนบปผฝพฟภมย", "รฤลฦวศษสหฬอฮฯะัาำ" }
  2463. )]
  2464. [InlineData (
  2465. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2466. 13,
  2467. -38,
  2468. new [] { "กขฃคฅฆงจฉชซฌญ", "ฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦว", "ศษสหฬอฮฯะัาำ" }
  2469. )]
  2470. [InlineData (
  2471. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2472. 1,
  2473. -50,
  2474. new []
  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. "ะั",
  2524. "าำ"
  2525. }
  2526. )]
  2527. public void WordWrap_Unicode_SingleWordLine (
  2528. string text,
  2529. int maxWidth,
  2530. int widthOffset,
  2531. IEnumerable<string> resultLines
  2532. )
  2533. {
  2534. List<string> wrappedLines;
  2535. IEnumerable<Rune> zeroWidth = text.EnumerateRunes ().Where (r => r.GetColumns () == 0);
  2536. Assert.Single (zeroWidth);
  2537. Assert.Equal ('ั', zeroWidth.ElementAt (0).Value);
  2538. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2539. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2540. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2541. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2542. Assert.True (
  2543. expectedClippedWidth
  2544. >= (wrappedLines.Count > 0
  2545. ? wrappedLines.Max (
  2546. l => l.GetRuneCount ()
  2547. + zeroWidth.Count ()
  2548. - 1
  2549. + widthOffset
  2550. )
  2551. : 0)
  2552. );
  2553. Assert.True (
  2554. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2555. );
  2556. if (maxWidth == 1)
  2557. {
  2558. List<string> newResultLines = resultLines.ToList ();
  2559. newResultLines [^1] = "";
  2560. Assert.Equal (newResultLines, wrappedLines);
  2561. }
  2562. else
  2563. {
  2564. Assert.Equal (resultLines, wrappedLines);
  2565. }
  2566. }
  2567. /// <summary>WordWrap strips CRLF</summary>
  2568. [Theory]
  2569. [InlineData (
  2570. "A sentence has words.\nA paragraph has lines.",
  2571. 44,
  2572. 0,
  2573. new [] { "A sentence has words.A paragraph has lines." }
  2574. )]
  2575. [InlineData (
  2576. "A sentence has words.\nA paragraph has lines.",
  2577. 43,
  2578. -1,
  2579. new [] { "A sentence has words.A paragraph has lines." }
  2580. )]
  2581. [InlineData (
  2582. "A sentence has words.\nA paragraph has lines.",
  2583. 38,
  2584. -6,
  2585. new [] { "A sentence has words.A paragraph has", "lines." }
  2586. )]
  2587. [InlineData (
  2588. "A sentence has words.\nA paragraph has lines.",
  2589. 34,
  2590. -10,
  2591. new [] { "A sentence has words.A paragraph", "has lines." }
  2592. )]
  2593. [InlineData (
  2594. "A sentence has words.\nA paragraph has lines.",
  2595. 27,
  2596. -17,
  2597. new [] { "A sentence has words.A", "paragraph has lines." }
  2598. )]
  2599. // Unicode
  2600. [InlineData (
  2601. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2602. 69,
  2603. 0,
  2604. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." }
  2605. )]
  2606. [InlineData (
  2607. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2608. 68,
  2609. -1,
  2610. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." }
  2611. )]
  2612. [InlineData (
  2613. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2614. 63,
  2615. -6,
  2616. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", "Линии." }
  2617. )]
  2618. [InlineData (
  2619. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2620. 59,
  2621. -10,
  2622. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", "has Линии." }
  2623. )]
  2624. [InlineData (
  2625. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2626. 52,
  2627. -17,
  2628. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode", "Пункт has Линии." }
  2629. )]
  2630. public void WordWrap_WithNewLines (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  2631. {
  2632. List<string> wrappedLines;
  2633. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2634. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2635. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2636. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2637. Assert.True (
  2638. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2639. );
  2640. Assert.True (
  2641. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2642. );
  2643. Assert.Equal (resultLines, wrappedLines);
  2644. }
  2645. [Theory]
  2646. [InlineData ("No crlf", "No crlf")]
  2647. // CRLF
  2648. [InlineData ("\r\nThis has crlf in the beginning", "This has crlf in the beginning")]
  2649. [InlineData ("This has crlf\r\nin the middle", "This has crlfin the middle")]
  2650. [InlineData ("This has crlf in the end\r\n", "This has crlf in the end")]
  2651. // LFCR
  2652. [InlineData ("\n\rThis has lfcr in the beginning", "This has lfcr in the beginning")]
  2653. [InlineData ("This has lfcr\n\rin the middle", "This has lfcrin the middle")]
  2654. [InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end")]
  2655. // CR
  2656. [InlineData ("\rThis has cr in the beginning", "This has cr in the beginning")]
  2657. [InlineData ("This has cr\rin the middle", "This has crin the middle")]
  2658. [InlineData ("This has cr in the end\r", "This has cr in the end")]
  2659. // LF
  2660. [InlineData ("\nThis has lf in the beginning", "This has lf in the beginning")]
  2661. [InlineData ("This has lf\nin the middle", "This has lfin the middle")]
  2662. [InlineData ("This has lf in the end\n", "This has lf in the end")]
  2663. public void StripCRLF_RemovesCrLf (string input, string expected)
  2664. {
  2665. string actual = TextFormatter.StripCRLF (input, keepNewLine: false);
  2666. Assert.Equal (expected, actual);
  2667. }
  2668. [Theory]
  2669. [InlineData ("No crlf", "No crlf")]
  2670. // CRLF
  2671. [InlineData ("\r\nThis has crlf in the beginning", "\nThis has crlf in the beginning")]
  2672. [InlineData ("This has crlf\r\nin the middle", "This has crlf\nin the middle")]
  2673. [InlineData ("This has crlf in the end\r\n", "This has crlf in the end\n")]
  2674. // LFCR
  2675. [InlineData ("\n\rThis has lfcr in the beginning", "\n\rThis has lfcr in the beginning")]
  2676. [InlineData ("This has lfcr\n\rin the middle", "This has lfcr\n\rin the middle")]
  2677. [InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end\n\r")]
  2678. // CR
  2679. [InlineData ("\rThis has cr in the beginning", "\rThis has cr in the beginning")]
  2680. [InlineData ("This has cr\rin the middle", "This has cr\rin the middle")]
  2681. [InlineData ("This has cr in the end\r", "This has cr in the end\r")]
  2682. // LF
  2683. [InlineData ("\nThis has lf in the beginning", "\nThis has lf in the beginning")]
  2684. [InlineData ("This has lf\nin the middle", "This has lf\nin the middle")]
  2685. [InlineData ("This has lf in the end\n", "This has lf in the end\n")]
  2686. public void StripCRLF_KeepNewLine_RemovesCarriageReturnFromCrLf (string input, string expected)
  2687. {
  2688. string actual = TextFormatter.StripCRLF (input, keepNewLine: true);
  2689. Assert.Equal (expected, actual);
  2690. }
  2691. [Theory]
  2692. [InlineData ("No crlf", "No crlf")]
  2693. // CRLF
  2694. [InlineData ("\r\nThis has crlf in the beginning", " This has crlf in the beginning")]
  2695. [InlineData ("This has crlf\r\nin the middle", "This has crlf in the middle")]
  2696. [InlineData ("This has crlf in the end\r\n", "This has crlf in the end ")]
  2697. // LFCR
  2698. [InlineData ("\n\rThis has lfcr in the beginning", " This has lfcr in the beginning")]
  2699. [InlineData ("This has lfcr\n\rin the middle", "This has lfcr in the middle")]
  2700. [InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end ")]
  2701. // CR
  2702. [InlineData ("\rThis has cr in the beginning", " This has cr in the beginning")]
  2703. [InlineData ("This has cr\rin the middle", "This has cr in the middle")]
  2704. [InlineData ("This has cr in the end\r", "This has cr in the end ")]
  2705. // LF
  2706. [InlineData ("\nThis has lf in the beginning", " This has lf in the beginning")]
  2707. [InlineData ("This has lf\nin the middle", "This has lf in the middle")]
  2708. [InlineData ("This has lf in the end\n", "This has lf in the end ")]
  2709. public void ReplaceCRLFWithSpace_ReplacesCrLfWithSpace (string input, string expected)
  2710. {
  2711. string actual = TextFormatter.ReplaceCRLFWithSpace (input);
  2712. Assert.Equal (expected, actual);
  2713. }
  2714. // ============================================================
  2715. // MIGRATED TESTS FROM UnitTests/Text/TextFormatterTests.cs
  2716. // These tests now use CreateFakeDriver() from ParallelizableBase
  2717. // instead of relying on Application.Driver via [SetupFakeDriver]
  2718. // ============================================================
  2719. [Theory]
  2720. [InlineData ("A", 0, "")]
  2721. [InlineData ("A", 1, "A")]
  2722. [InlineData ("A", 2, "A")]
  2723. [InlineData ("A", 3, " A")]
  2724. [InlineData ("AB", 1, "A")]
  2725. [InlineData ("AB", 2, "AB")]
  2726. [InlineData ("ABC", 3, "ABC")]
  2727. [InlineData ("ABC", 4, "ABC")]
  2728. [InlineData ("ABC", 5, " ABC")]
  2729. [InlineData ("ABC", 6, " ABC")]
  2730. [InlineData ("ABC", 9, " ABC")]
  2731. public void Draw_Horizontal_Centered (string text, int width, string expectedText)
  2732. {
  2733. var driver = CreateFakeDriver (width > 0 ? width : 1, 1);
  2734. TextFormatter tf = new ()
  2735. {
  2736. Text = text,
  2737. Alignment = Alignment.Center,
  2738. ConstrainToWidth = width,
  2739. ConstrainToHeight = 1
  2740. };
  2741. tf.Draw (driver, new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default, default);
  2742. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, output, driver);
  2743. }
  2744. [Theory]
  2745. [InlineData ("A", 0, "")]
  2746. [InlineData ("A", 1, "A")]
  2747. [InlineData ("A", 2, "A")]
  2748. [InlineData ("A B", 3, "A B")]
  2749. [InlineData ("A B", 1, "A")]
  2750. [InlineData ("A B", 2, "A")]
  2751. [InlineData ("A B", 4, "A B")]
  2752. [InlineData ("A B", 5, "A B")]
  2753. [InlineData ("A B", 6, "A B")]
  2754. [InlineData ("A B", 10, "A B")]
  2755. [InlineData ("ABC ABC", 10, "ABC ABC")]
  2756. public void Draw_Horizontal_Justified (string text, int width, string expectedText)
  2757. {
  2758. var driver = CreateFakeDriver (width > 0 ? width : 1, 1);
  2759. TextFormatter tf = new ()
  2760. {
  2761. Text = text,
  2762. Alignment = Alignment.Fill,
  2763. ConstrainToWidth = width,
  2764. ConstrainToHeight = 1
  2765. };
  2766. tf.Draw (driver, new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default, default);
  2767. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, output, driver);
  2768. }
  2769. [Theory]
  2770. [InlineData ("A", 0, "")]
  2771. [InlineData ("A", 1, "A")]
  2772. [InlineData ("A", 2, "A")]
  2773. [InlineData ("AB", 1, "A")]
  2774. [InlineData ("AB", 2, "AB")]
  2775. [InlineData ("ABC", 3, "ABC")]
  2776. [InlineData ("ABC", 4, "ABC")]
  2777. [InlineData ("ABC", 6, "ABC")]
  2778. public void Draw_Horizontal_Left (string text, int width, string expectedText)
  2779. {
  2780. var driver = CreateFakeDriver (width > 0 ? width : 1, 1);
  2781. TextFormatter tf = new ()
  2782. {
  2783. Text = text,
  2784. Alignment = Alignment.Start,
  2785. ConstrainToWidth = width,
  2786. ConstrainToHeight = 1
  2787. };
  2788. tf.Draw (driver, new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default, default);
  2789. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, output, driver);
  2790. }
  2791. [Theory]
  2792. [InlineData ("A", 0, "")]
  2793. [InlineData ("A", 1, "A")]
  2794. [InlineData ("A", 2, " A")]
  2795. [InlineData ("AB", 1, "B")]
  2796. [InlineData ("AB", 2, "AB")]
  2797. [InlineData ("ABC", 3, "ABC")]
  2798. [InlineData ("ABC", 4, " ABC")]
  2799. [InlineData ("ABC", 6, " ABC")]
  2800. public void Draw_Horizontal_Right (string text, int width, string expectedText)
  2801. {
  2802. var driver = CreateFakeDriver (width > 0 ? width : 1, 1);
  2803. TextFormatter tf = new ()
  2804. {
  2805. Text = text,
  2806. Alignment = Alignment.End,
  2807. ConstrainToWidth = width,
  2808. ConstrainToHeight = 1
  2809. };
  2810. tf.Draw (driver, new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default, default);
  2811. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, output, driver);
  2812. }
  2813. [Theory]
  2814. [InlineData (14, 1, TextDirection.LeftRight_TopBottom, "Les Misę́rables")]
  2815. [InlineData (1, 14, TextDirection.TopBottom_LeftRight, "L\ne\ns\n \nM\ni\ns\nę́\nr\na\nb\nl\ne\ns")]
  2816. [InlineData (
  2817. 4,
  2818. 4,
  2819. TextDirection.TopBottom_LeftRight,
  2820. @"
  2821. LMre
  2822. eias
  2823. ssb
  2824. ę́l "
  2825. )]
  2826. public void Draw_With_Combining_Runes (int width, int height, TextDirection textDirection, string expected)
  2827. {
  2828. var driver = CreateFakeDriver ();
  2829. var text = "Les Mise\u0328\u0301rables";
  2830. var tf = new TextFormatter ();
  2831. tf.Direction = textDirection;
  2832. tf.Text = text;
  2833. Assert.True (tf.WordWrap);
  2834. tf.ConstrainToSize = new (width, height);
  2835. tf.Draw (
  2836. driver,
  2837. new (0, 0, width, height),
  2838. new (ColorName16.White, ColorName16.Black),
  2839. new (ColorName16.Blue, ColorName16.Black),
  2840. default (Rectangle));
  2841. DriverAssert.AssertDriverContentsWithFrameAre (expected, output, driver);
  2842. driver.End ();
  2843. }
  2844. [Theory]
  2845. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  2846. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  2847. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  2848. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  2849. public void TabWith_PreserveTrailingSpaces_True (
  2850. int width,
  2851. int height,
  2852. TextDirection textDirection,
  2853. int tabWidth,
  2854. string expected
  2855. )
  2856. {
  2857. var driver = CreateFakeDriver ();
  2858. var text = "This is a \tTab";
  2859. var tf = new TextFormatter ();
  2860. tf.Direction = textDirection;
  2861. tf.TabWidth = tabWidth;
  2862. tf.PreserveTrailingSpaces = true;
  2863. tf.Text = text;
  2864. tf.ConstrainToWidth = 20;
  2865. tf.ConstrainToHeight = 20;
  2866. Assert.True (tf.WordWrap);
  2867. tf.Draw (
  2868. driver,
  2869. new (0, 0, width, height),
  2870. new (ColorName16.White, ColorName16.Black),
  2871. new (ColorName16.Blue, ColorName16.Black),
  2872. default (Rectangle));
  2873. DriverAssert.AssertDriverContentsWithFrameAre (expected, output, driver);
  2874. driver.End ();
  2875. }
  2876. [Theory]
  2877. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  2878. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  2879. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  2880. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  2881. public void TabWith_WordWrap_True (
  2882. int width,
  2883. int height,
  2884. TextDirection textDirection,
  2885. int tabWidth,
  2886. string expected
  2887. )
  2888. {
  2889. var driver = CreateFakeDriver ();
  2890. var text = "This is a \tTab";
  2891. var tf = new TextFormatter ();
  2892. tf.Direction = textDirection;
  2893. tf.TabWidth = tabWidth;
  2894. tf.WordWrap = true;
  2895. tf.Text = text;
  2896. tf.ConstrainToWidth = 20;
  2897. tf.ConstrainToHeight = 20;
  2898. Assert.False (tf.PreserveTrailingSpaces);
  2899. tf.Draw (
  2900. driver,
  2901. new (0, 0, width, height),
  2902. new (ColorName16.White, ColorName16.Black),
  2903. new (ColorName16.Blue, ColorName16.Black),
  2904. default (Rectangle));
  2905. DriverAssert.AssertDriverContentsWithFrameAre (expected, output, driver);
  2906. driver.End ();
  2907. }
  2908. [Theory]
  2909. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  2910. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  2911. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  2912. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  2913. public void TabWith_PreserveTrailingSpaces_False (
  2914. int width,
  2915. int height,
  2916. TextDirection textDirection,
  2917. int tabWidth,
  2918. string expected
  2919. )
  2920. {
  2921. var driver = CreateFakeDriver ();
  2922. var text = "This is a \tTab";
  2923. var tf = new TextFormatter ();
  2924. tf.Direction = textDirection;
  2925. tf.TabWidth = tabWidth;
  2926. tf.Text = text;
  2927. tf.ConstrainToWidth = 20;
  2928. tf.ConstrainToHeight = 20;
  2929. Assert.True (tf.WordWrap);
  2930. Assert.False (tf.PreserveTrailingSpaces);
  2931. tf.Draw (
  2932. driver,
  2933. new (0, 0, width, height),
  2934. new (ColorName16.White, ColorName16.Black),
  2935. new (ColorName16.Blue, ColorName16.Black),
  2936. default (Rectangle));
  2937. DriverAssert.AssertDriverContentsWithFrameAre (expected, output, driver);
  2938. driver.End ();
  2939. }
  2940. }