TextFormatterTests.cs 143 KB

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