TextFormatterTests.cs 128 KB

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