TextFormatterTests.cs 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Terminal.Gui.Views;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.Core {
  11. public class TextFormatterTests {
  12. readonly ITestOutputHelper output;
  13. public TextFormatterTests (ITestOutputHelper output)
  14. {
  15. this.output = output;
  16. }
  17. [Fact]
  18. public void Basic_Usage ()
  19. {
  20. var testText = ustring.Make ("test");
  21. var expectedSize = new Size ();
  22. var testBounds = new Rect (0, 0, 100, 1);
  23. var tf = new TextFormatter ();
  24. tf.Text = testText;
  25. expectedSize = new Size (testText.Length, 1);
  26. Assert.Equal (testText, tf.Text);
  27. Assert.Equal (TextAlignment.Left, tf.Alignment);
  28. Assert.Equal (expectedSize, tf.Size);
  29. tf.Draw (testBounds, new Attribute (), new Attribute ());
  30. Assert.Equal (expectedSize, tf.Size);
  31. Assert.NotEmpty (tf.Lines);
  32. tf.Alignment = TextAlignment.Right;
  33. expectedSize = new Size (testText.Length, 1);
  34. Assert.Equal (testText, tf.Text);
  35. Assert.Equal (TextAlignment.Right, tf.Alignment);
  36. Assert.Equal (expectedSize, tf.Size);
  37. tf.Draw (testBounds, new Attribute (), new Attribute ());
  38. Assert.Equal (expectedSize, tf.Size);
  39. Assert.NotEmpty (tf.Lines);
  40. tf.Alignment = TextAlignment.Right;
  41. expectedSize = new Size (testText.Length * 2, 1);
  42. tf.Size = expectedSize;
  43. Assert.Equal (testText, tf.Text);
  44. Assert.Equal (TextAlignment.Right, tf.Alignment);
  45. Assert.Equal (expectedSize, tf.Size);
  46. tf.Draw (testBounds, new Attribute (), new Attribute ());
  47. Assert.Equal (expectedSize, tf.Size);
  48. Assert.NotEmpty (tf.Lines);
  49. tf.Alignment = TextAlignment.Centered;
  50. expectedSize = new Size (testText.Length * 2, 1);
  51. tf.Size = expectedSize;
  52. Assert.Equal (testText, tf.Text);
  53. Assert.Equal (TextAlignment.Centered, tf.Alignment);
  54. Assert.Equal (expectedSize, tf.Size);
  55. tf.Draw (testBounds, new Attribute (), new Attribute ());
  56. Assert.Equal (expectedSize, tf.Size);
  57. Assert.NotEmpty (tf.Lines);
  58. }
  59. [Fact]
  60. public void TestSize_TextChange ()
  61. {
  62. var tf = new TextFormatter () { Text = "你" };
  63. Assert.Equal (2, tf.Size.Width);
  64. tf.Text = "你你";
  65. Assert.Equal (4, tf.Size.Width);
  66. }
  67. [Fact]
  68. public void NeedsFormat_Sets ()
  69. {
  70. var testText = ustring.Make ("test");
  71. var testBounds = new Rect (0, 0, 100, 1);
  72. var tf = new TextFormatter ();
  73. tf.Text = "test";
  74. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  75. Assert.NotEmpty (tf.Lines);
  76. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  77. Assert.Equal (testText, tf.Text);
  78. tf.Draw (testBounds, new Attribute (), new Attribute ());
  79. Assert.False (tf.NeedsFormat);
  80. tf.Size = new Size (1, 1);
  81. Assert.True (tf.NeedsFormat);
  82. Assert.NotEmpty (tf.Lines);
  83. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  84. tf.Alignment = TextAlignment.Centered;
  85. Assert.True (tf.NeedsFormat);
  86. Assert.NotEmpty (tf.Lines);
  87. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  88. }
  89. [Fact]
  90. public void FindHotKey_Invalid_ReturnsFalse ()
  91. {
  92. var text = ustring.Empty;
  93. Rune hotKeySpecifier = '_';
  94. bool supportFirstUpperCase = false;
  95. int hotPos = 0;
  96. Key hotKey = Key.Unknown;
  97. bool result = false;
  98. text = null;
  99. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  100. Assert.False (result);
  101. Assert.Equal (-1, hotPos);
  102. Assert.Equal (Key.Unknown, hotKey);
  103. text = "";
  104. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  105. Assert.False (result);
  106. Assert.Equal (-1, hotPos);
  107. Assert.Equal (Key.Unknown, hotKey);
  108. text = "no hotkey";
  109. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  110. Assert.False (result);
  111. Assert.Equal (-1, hotPos);
  112. Assert.Equal (Key.Unknown, hotKey);
  113. text = "No hotkey, Upper Case";
  114. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  115. Assert.False (result);
  116. Assert.Equal (-1, hotPos);
  117. Assert.Equal (Key.Unknown, hotKey);
  118. text = "Non-english: Сохранить";
  119. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  120. Assert.False (result);
  121. Assert.Equal (-1, hotPos);
  122. Assert.Equal (Key.Unknown, hotKey);
  123. }
  124. [Fact]
  125. public void FindHotKey_AlphaUpperCase_Succeeds ()
  126. {
  127. var text = ustring.Empty;
  128. Rune hotKeySpecifier = '_';
  129. bool supportFirstUpperCase = false;
  130. int hotPos = 0;
  131. Key hotKey = Key.Unknown;
  132. bool result = false;
  133. text = "_K Before";
  134. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  135. Assert.True (result);
  136. Assert.Equal (0, hotPos);
  137. Assert.Equal ((Key)'K', hotKey);
  138. text = "a_K Second";
  139. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  140. Assert.True (result);
  141. Assert.Equal (1, hotPos);
  142. Assert.Equal ((Key)'K', hotKey);
  143. text = "Last _K";
  144. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  145. Assert.True (result);
  146. Assert.Equal (5, hotPos);
  147. Assert.Equal ((Key)'K', hotKey);
  148. text = "After K_";
  149. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  150. Assert.False (result);
  151. Assert.Equal (-1, hotPos);
  152. Assert.Equal (Key.Unknown, hotKey);
  153. text = "Multiple _K and _R";
  154. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  155. Assert.True (result);
  156. Assert.Equal (9, hotPos);
  157. Assert.Equal ((Key)'K', hotKey);
  158. // Cryllic K (К)
  159. text = "Non-english: _Кдать";
  160. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  161. Assert.True (result);
  162. Assert.Equal (13, hotPos);
  163. Assert.Equal ((Key)'К', hotKey);
  164. // Turn on FirstUpperCase and verify same results
  165. supportFirstUpperCase = true;
  166. text = "_K Before";
  167. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  168. Assert.True (result);
  169. Assert.Equal (0, hotPos);
  170. Assert.Equal ((Key)'K', hotKey);
  171. text = "a_K Second";
  172. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  173. Assert.True (result);
  174. Assert.Equal (1, hotPos);
  175. Assert.Equal ((Key)'K', hotKey);
  176. text = "Last _K";
  177. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  178. Assert.True (result);
  179. Assert.Equal (5, hotPos);
  180. Assert.Equal ((Key)'K', hotKey);
  181. text = "After K_";
  182. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  183. Assert.False (result);
  184. Assert.Equal (-1, hotPos);
  185. Assert.Equal (Key.Unknown, hotKey);
  186. text = "Multiple _K and _R";
  187. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  188. Assert.True (result);
  189. Assert.Equal (9, hotPos);
  190. Assert.Equal ((Key)'K', hotKey);
  191. // Cryllic K (К)
  192. text = "Non-english: _Кдать";
  193. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  194. Assert.True (result);
  195. Assert.Equal (13, hotPos);
  196. Assert.Equal ((Key)'К', hotKey);
  197. }
  198. [Fact]
  199. public void FindHotKey_AlphaLowerCase_Succeeds ()
  200. {
  201. var text = ustring.Empty;
  202. Rune hotKeySpecifier = '_';
  203. bool supportFirstUpperCase = false;
  204. int hotPos = 0;
  205. Key hotKey = Key.Unknown;
  206. bool result = false;
  207. // lower case should return uppercase Hotkey
  208. text = "_k Before";
  209. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  210. Assert.True (result);
  211. Assert.Equal (0, hotPos);
  212. Assert.Equal ((Key)'K', hotKey);
  213. text = "a_k Second";
  214. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  215. Assert.True (result);
  216. Assert.Equal (1, hotPos);
  217. Assert.Equal ((Key)'K', hotKey);
  218. text = "Last _k";
  219. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  220. Assert.True (result);
  221. Assert.Equal (5, hotPos);
  222. Assert.Equal ((Key)'K', hotKey);
  223. text = "After k_";
  224. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  225. Assert.False (result);
  226. Assert.Equal (-1, hotPos);
  227. Assert.Equal (Key.Unknown, hotKey);
  228. text = "Multiple _k and _R";
  229. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  230. Assert.True (result);
  231. Assert.Equal (9, hotPos);
  232. Assert.Equal ((Key)'K', hotKey);
  233. // Lower case Cryllic K (к)
  234. text = "Non-english: _кдать";
  235. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  236. Assert.True (result);
  237. Assert.Equal (13, hotPos);
  238. Assert.Equal ((Key)'К', hotKey);
  239. // Turn on FirstUpperCase and verify same results
  240. supportFirstUpperCase = true;
  241. text = "_k Before";
  242. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  243. Assert.True (result);
  244. Assert.Equal (0, hotPos);
  245. Assert.Equal ((Key)'K', hotKey);
  246. text = "a_k Second";
  247. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  248. Assert.True (result);
  249. Assert.Equal (1, hotPos);
  250. Assert.Equal ((Key)'K', hotKey);
  251. text = "Last _k";
  252. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  253. Assert.True (result);
  254. Assert.Equal (5, hotPos);
  255. Assert.Equal ((Key)'K', hotKey);
  256. text = "After k_";
  257. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  258. Assert.False (result);
  259. Assert.Equal (-1, hotPos);
  260. Assert.Equal (Key.Unknown, hotKey);
  261. text = "Multiple _k and _R";
  262. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  263. Assert.True (result);
  264. Assert.Equal (9, hotPos);
  265. Assert.Equal ((Key)'K', hotKey);
  266. // Lower case Cryllic K (к)
  267. text = "Non-english: _кдать";
  268. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  269. Assert.True (result);
  270. Assert.Equal (13, hotPos);
  271. Assert.Equal ((Key)'К', hotKey);
  272. }
  273. [Fact]
  274. public void FindHotKey_Numeric_Succeeds ()
  275. {
  276. var text = ustring.Empty;
  277. Rune hotKeySpecifier = '_';
  278. bool supportFirstUpperCase = false;
  279. int hotPos = 0;
  280. Key hotKey = Key.Unknown;
  281. bool result = false;
  282. // Digits
  283. text = "_1 Before";
  284. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  285. Assert.True (result);
  286. Assert.Equal (0, hotPos);
  287. Assert.Equal ((Key)'1', hotKey);
  288. text = "a_1 Second";
  289. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  290. Assert.True (result);
  291. Assert.Equal (1, hotPos);
  292. Assert.Equal ((Key)'1', hotKey);
  293. text = "Last _1";
  294. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  295. Assert.True (result);
  296. Assert.Equal (5, hotPos);
  297. Assert.Equal ((Key)'1', hotKey);
  298. text = "After 1_";
  299. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  300. Assert.False (result);
  301. Assert.Equal (-1, hotPos);
  302. Assert.Equal (Key.Unknown, hotKey);
  303. text = "Multiple _1 and _2";
  304. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  305. Assert.True (result);
  306. Assert.Equal (9, hotPos);
  307. Assert.Equal ((Key)'1', hotKey);
  308. // Turn on FirstUpperCase and verify same results
  309. supportFirstUpperCase = true;
  310. text = "_1 Before";
  311. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  312. Assert.True (result);
  313. Assert.Equal (0, hotPos);
  314. Assert.Equal ((Key)'1', hotKey);
  315. text = "a_1 Second";
  316. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  317. Assert.True (result);
  318. Assert.Equal (1, hotPos);
  319. Assert.Equal ((Key)'1', hotKey);
  320. text = "Last _1";
  321. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  322. Assert.True (result);
  323. Assert.Equal (5, hotPos);
  324. Assert.Equal ((Key)'1', hotKey);
  325. text = "After 1_";
  326. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  327. Assert.False (result);
  328. Assert.Equal (-1, hotPos);
  329. Assert.Equal (Key.Unknown, hotKey);
  330. text = "Multiple _1 and _2";
  331. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  332. Assert.True (result);
  333. Assert.Equal (9, hotPos);
  334. Assert.Equal ((Key)'1', hotKey);
  335. }
  336. [Fact]
  337. public void FindHotKey_Legacy_FirstUpperCase_Succeeds ()
  338. {
  339. bool supportFirstUpperCase = true;
  340. var text = ustring.Empty;
  341. Rune hotKeySpecifier = (Rune)0;
  342. int hotPos = 0;
  343. Key hotKey = Key.Unknown;
  344. bool result = false;
  345. text = "K Before";
  346. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  347. Assert.True (result);
  348. Assert.Equal (0, hotPos);
  349. Assert.Equal ((Key)'K', hotKey);
  350. text = "aK Second";
  351. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  352. Assert.True (result);
  353. Assert.Equal (1, hotPos);
  354. Assert.Equal ((Key)'K', hotKey);
  355. text = "last K";
  356. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  357. Assert.True (result);
  358. Assert.Equal (5, hotPos);
  359. Assert.Equal ((Key)'K', hotKey);
  360. text = "multiple K and R";
  361. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  362. Assert.True (result);
  363. Assert.Equal (9, hotPos);
  364. Assert.Equal ((Key)'K', hotKey);
  365. // Cryllic K (К)
  366. text = "non-english: Кдать";
  367. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  368. Assert.True (result);
  369. Assert.Equal (13, hotPos);
  370. Assert.Equal ((Key)'К', hotKey);
  371. }
  372. [Fact]
  373. public void FindHotKey_Legacy_FirstUpperCase_NotFound_Returns_False ()
  374. {
  375. bool supportFirstUpperCase = true;
  376. var text = ustring.Empty;
  377. Rune hotKeySpecifier = (Rune)0;
  378. int hotPos = 0;
  379. Key hotKey = Key.Unknown;
  380. bool result = false;
  381. text = "k before";
  382. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  383. Assert.False (result);
  384. Assert.Equal (-1, hotPos);
  385. Assert.Equal (Key.Unknown, hotKey);
  386. text = "ak second";
  387. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  388. Assert.False (result);
  389. Assert.Equal (-1, hotPos);
  390. Assert.Equal (Key.Unknown, hotKey);
  391. text = "last k";
  392. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  393. Assert.False (result);
  394. Assert.Equal (-1, hotPos);
  395. Assert.Equal (Key.Unknown, hotKey);
  396. text = "multiple k and r";
  397. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  398. Assert.False (result);
  399. Assert.Equal (-1, hotPos);
  400. Assert.Equal (Key.Unknown, hotKey);
  401. text = "12345";
  402. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  403. Assert.False (result);
  404. Assert.Equal (-1, hotPos);
  405. Assert.Equal (Key.Unknown, hotKey);
  406. // punctuation
  407. text = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
  408. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  409. Assert.False (result);
  410. Assert.Equal (-1, hotPos);
  411. Assert.Equal (Key.Unknown, hotKey);
  412. // ~IsLetterOrDigit + Unicode
  413. text = " ~  s  gui.cs   master ↑10";
  414. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  415. Assert.False (result);
  416. Assert.Equal (-1, hotPos);
  417. Assert.Equal (Key.Unknown, hotKey);
  418. // Lower case Cryllic K (к)
  419. text = "non-english: кдать";
  420. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  421. Assert.False (result);
  422. Assert.Equal (-1, hotPos);
  423. Assert.Equal (Key.Unknown, hotKey);
  424. }
  425. static ustring testHotKeyAtStart = "_K Before";
  426. static ustring testHotKeyAtSecondPos = "a_K Second";
  427. static ustring testHotKeyAtLastPos = "Last _K";
  428. static ustring testHotKeyAfterLastChar = "After K_";
  429. static ustring testMultiHotKeys = "Multiple _K and _R";
  430. static ustring testNonEnglish = "Non-english: _Кдать";
  431. [Fact]
  432. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal ()
  433. {
  434. Rune hotKeySpecifier = '_';
  435. Assert.Null (TextFormatter.RemoveHotKeySpecifier (null, 0, hotKeySpecifier));
  436. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 0, hotKeySpecifier));
  437. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", -1, hotKeySpecifier));
  438. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 100, hotKeySpecifier));
  439. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", -1, hotKeySpecifier));
  440. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", 100, hotKeySpecifier));
  441. }
  442. [Fact]
  443. public void RemoveHotKeySpecifier_Valid_ReturnsStripped ()
  444. {
  445. Rune hotKeySpecifier = '_';
  446. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("_K Before", 0, hotKeySpecifier));
  447. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("a_K Second", 1, hotKeySpecifier));
  448. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last _K", 5, hotKeySpecifier));
  449. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K_", 7, hotKeySpecifier));
  450. Assert.Equal ("Multiple K and _R", TextFormatter.RemoveHotKeySpecifier ("Multiple _K and _R", 9, hotKeySpecifier));
  451. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: _Кдать", 13, hotKeySpecifier));
  452. }
  453. [Fact]
  454. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal ()
  455. {
  456. Rune hotKeySpecifier = '_';
  457. Assert.Equal ("all lower case", TextFormatter.RemoveHotKeySpecifier ("all lower case", 0, hotKeySpecifier));
  458. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("K Before", 0, hotKeySpecifier));
  459. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("aK Second", 1, hotKeySpecifier));
  460. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last K", 5, hotKeySpecifier));
  461. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K", 7, hotKeySpecifier));
  462. Assert.Equal ("Multiple K and R", TextFormatter.RemoveHotKeySpecifier ("Multiple K and R", 9, hotKeySpecifier));
  463. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: Кдать", 13, hotKeySpecifier));
  464. }
  465. [Fact]
  466. public void CalcRect_Invalid_Returns_Empty ()
  467. {
  468. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, null));
  469. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, ""));
  470. Assert.Equal (new Rect (new Point (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, ""));
  471. Assert.Equal (new Rect (new Point (-1, -2), Size.Empty), TextFormatter.CalcRect (-1, -2, ""));
  472. }
  473. [Fact]
  474. public void CalcRect_SingleLine_Returns_1High ()
  475. {
  476. var text = ustring.Empty;
  477. text = "test";
  478. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  479. Assert.Equal (new Rect (0, 0, text.ConsoleWidth, 1), TextFormatter.CalcRect (0, 0, text));
  480. text = " ~  s  gui.cs   master ↑10";
  481. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  482. Assert.Equal (new Rect (0, 0, text.ConsoleWidth, 1), TextFormatter.CalcRect (0, 0, text));
  483. }
  484. [Fact]
  485. public void CalcRect_MultiLine_Returns_nHigh ()
  486. {
  487. var text = ustring.Empty;
  488. var lines = 0;
  489. text = "line1\nline2";
  490. lines = 2;
  491. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  492. text = "\nline2";
  493. lines = 2;
  494. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  495. text = "\n\n";
  496. lines = 3;
  497. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  498. text = "\n\n\n";
  499. lines = 4;
  500. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  501. text = "line1\nline2\nline3long!";
  502. lines = 3;
  503. Assert.Equal (new Rect (0, 0, 10, lines), TextFormatter.CalcRect (0, 0, text));
  504. text = "line1\nline2\n\n";
  505. lines = 4;
  506. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  507. text = "line1\r\nline2";
  508. lines = 2;
  509. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  510. text = " ~  s  gui.cs   master ↑10\n";
  511. lines = 2;
  512. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  513. Assert.Equal (new Rect (0, 0, text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 0)), lines), TextFormatter.CalcRect (0, 0, text));
  514. text = "\n ~  s  gui.cs   master ↑10";
  515. lines = 2;
  516. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  517. Assert.Equal (new Rect (0, 0, text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 0)), lines), TextFormatter.CalcRect (0, 0, text));
  518. text = " ~  s  gui.cs   master\n↑10";
  519. lines = 2;
  520. Assert.Equal (new Rect (0, 0, ustring.Make (" ~  s  gui.cs   master\n").RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  521. Assert.Equal (new Rect (0, 0, ustring.Make (" ~  s  gui.cs   master\n").ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 0)), lines), TextFormatter.CalcRect (0, 0, text));
  522. }
  523. [Fact]
  524. public void ClipAndJustify_Invalid_Returns_Original ()
  525. {
  526. var text = ustring.Empty;
  527. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  528. text = null;
  529. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  530. text = "test";
  531. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.ClipAndJustify (text, -1, TextAlignment.Left));
  532. }
  533. [Fact]
  534. public void ClipAndJustify_Valid_Left ()
  535. {
  536. var align = TextAlignment.Left;
  537. var text = ustring.Empty;
  538. var justifiedText = ustring.Empty;
  539. int maxWidth = 0;
  540. int expectedClippedWidth = 0;
  541. text = "test";
  542. maxWidth = 0;
  543. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  544. text = "test";
  545. maxWidth = 2;
  546. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  547. text = "test";
  548. maxWidth = int.MaxValue;
  549. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  550. Assert.True (justifiedText.RuneCount <= maxWidth);
  551. Assert.True (justifiedText.ConsoleWidth <= maxWidth);
  552. text = "A sentence has words.";
  553. // should fit
  554. maxWidth = text.RuneCount + 1;
  555. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  556. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  557. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  558. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  559. Assert.True (expectedClippedWidth <= maxWidth);
  560. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  561. // Should fit.
  562. maxWidth = text.RuneCount + 0;
  563. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  564. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  565. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  566. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  567. Assert.True (expectedClippedWidth <= maxWidth);
  568. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  569. // Should fit.
  570. maxWidth = int.MaxValue;
  571. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  572. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  573. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  574. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  575. Assert.True (expectedClippedWidth <= maxWidth);
  576. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  577. // Should not fit
  578. maxWidth = text.RuneCount - 1;
  579. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  580. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  581. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  582. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  583. Assert.True (expectedClippedWidth <= maxWidth);
  584. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  585. // Should not fit
  586. maxWidth = 10;
  587. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  588. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  589. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  590. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  591. Assert.True (expectedClippedWidth <= maxWidth);
  592. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  593. text = "A\tsentence\thas\twords.";
  594. maxWidth = int.MaxValue;
  595. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  596. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  597. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  598. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  599. Assert.True (expectedClippedWidth <= maxWidth);
  600. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  601. text = "A\tsentence\thas\twords.";
  602. maxWidth = 10;
  603. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  604. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  605. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  606. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  607. Assert.True (expectedClippedWidth <= maxWidth);
  608. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  609. text = "line1\nline2\nline3long!";
  610. maxWidth = int.MaxValue;
  611. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  612. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  613. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  614. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  615. Assert.True (expectedClippedWidth <= maxWidth);
  616. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  617. text = "line1\nline2\nline3long!";
  618. maxWidth = 10;
  619. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  620. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  621. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  622. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  623. Assert.True (expectedClippedWidth <= maxWidth);
  624. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  625. // Unicode
  626. text = " ~  s  gui.cs   master ↑10";
  627. maxWidth = 10;
  628. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  629. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  630. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  631. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  632. Assert.True (expectedClippedWidth <= maxWidth);
  633. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  634. // should fit
  635. text = "Ð ÑÐ";
  636. maxWidth = text.RuneCount + 1;
  637. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  638. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  639. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  640. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  641. Assert.True (expectedClippedWidth <= maxWidth);
  642. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  643. // Should fit.
  644. maxWidth = text.RuneCount + 0;
  645. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  646. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  647. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  648. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  649. Assert.True (expectedClippedWidth <= maxWidth);
  650. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  651. // Should not fit
  652. maxWidth = text.RuneCount - 1;
  653. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  654. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  655. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  656. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  657. Assert.True (expectedClippedWidth <= maxWidth);
  658. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  659. }
  660. [Fact]
  661. public void ClipAndJustify_Valid_Right ()
  662. {
  663. var align = TextAlignment.Right;
  664. var text = ustring.Empty;
  665. var justifiedText = ustring.Empty;
  666. int maxWidth = 0;
  667. int expectedClippedWidth = 0;
  668. text = "test";
  669. maxWidth = 0;
  670. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  671. text = "test";
  672. maxWidth = 2;
  673. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  674. text = "test";
  675. maxWidth = int.MaxValue;
  676. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  677. Assert.True (justifiedText.RuneCount <= maxWidth);
  678. Assert.True (justifiedText.ConsoleWidth <= maxWidth);
  679. text = "A sentence has words.";
  680. // should fit
  681. maxWidth = text.RuneCount + 1;
  682. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  683. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  684. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  685. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  686. Assert.True (expectedClippedWidth <= maxWidth);
  687. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  688. // Should fit.
  689. maxWidth = text.RuneCount + 0;
  690. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  691. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  692. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  693. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  694. Assert.True (expectedClippedWidth <= maxWidth);
  695. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  696. // Should fit.
  697. maxWidth = int.MaxValue;
  698. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  699. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  700. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  701. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  702. Assert.True (expectedClippedWidth <= maxWidth);
  703. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  704. // Should not fit
  705. maxWidth = text.RuneCount - 1;
  706. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  707. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  708. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  709. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  710. Assert.True (expectedClippedWidth <= maxWidth);
  711. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  712. // Should not fit
  713. maxWidth = 10;
  714. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  715. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  716. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  717. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  718. Assert.True (expectedClippedWidth <= maxWidth);
  719. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  720. text = "A\tsentence\thas\twords.";
  721. maxWidth = int.MaxValue;
  722. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  723. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  724. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  725. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  726. Assert.True (expectedClippedWidth <= maxWidth);
  727. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  728. text = "A\tsentence\thas\twords.";
  729. maxWidth = 10;
  730. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  731. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  732. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  733. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  734. Assert.True (expectedClippedWidth <= maxWidth);
  735. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  736. text = "line1\nline2\nline3long!";
  737. maxWidth = int.MaxValue;
  738. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  739. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  740. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  741. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  742. Assert.True (expectedClippedWidth <= maxWidth);
  743. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  744. text = "line1\nline2\nline3long!";
  745. maxWidth = 10;
  746. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  747. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  748. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  749. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  750. Assert.True (expectedClippedWidth <= maxWidth);
  751. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  752. // Unicode
  753. text = " ~  s  gui.cs   master ↑10";
  754. maxWidth = 10;
  755. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  756. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  757. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  758. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  759. Assert.True (expectedClippedWidth <= maxWidth);
  760. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  761. // should fit
  762. text = "Ð ÑÐ";
  763. maxWidth = text.RuneCount + 1;
  764. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  765. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  766. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  767. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  768. Assert.True (expectedClippedWidth <= maxWidth);
  769. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  770. // Should fit.
  771. maxWidth = text.RuneCount + 0;
  772. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  773. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  774. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  775. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  776. Assert.True (expectedClippedWidth <= maxWidth);
  777. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  778. // Should not fit
  779. maxWidth = text.RuneCount - 1;
  780. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  781. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  782. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  783. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  784. Assert.True (expectedClippedWidth <= maxWidth);
  785. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  786. }
  787. [Fact]
  788. public void ClipAndJustify_Valid_Centered ()
  789. {
  790. var align = TextAlignment.Centered;
  791. var text = ustring.Empty;
  792. var justifiedText = ustring.Empty;
  793. int maxWidth = 0;
  794. int expectedClippedWidth = 0;
  795. text = "test";
  796. maxWidth = 0;
  797. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  798. text = "test";
  799. maxWidth = 2;
  800. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  801. text = "test";
  802. maxWidth = int.MaxValue;
  803. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  804. Assert.True (justifiedText.RuneCount <= maxWidth);
  805. Assert.True (justifiedText.ConsoleWidth <= maxWidth);
  806. text = "A sentence has words.";
  807. // should fit
  808. maxWidth = text.RuneCount + 1;
  809. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  810. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  811. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  812. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  813. Assert.True (expectedClippedWidth <= maxWidth);
  814. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  815. // Should fit.
  816. maxWidth = text.RuneCount + 0;
  817. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  818. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  819. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  820. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  821. Assert.True (expectedClippedWidth <= maxWidth);
  822. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  823. // Should fit.
  824. maxWidth = int.MaxValue;
  825. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  826. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  827. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  828. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  829. Assert.True (expectedClippedWidth <= maxWidth);
  830. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  831. // Should not fit
  832. maxWidth = text.RuneCount - 1;
  833. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  834. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  835. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  836. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  837. Assert.True (expectedClippedWidth <= maxWidth);
  838. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  839. // Should not fit
  840. maxWidth = 10;
  841. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  842. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  843. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  844. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  845. Assert.True (expectedClippedWidth <= maxWidth);
  846. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  847. text = "A\tsentence\thas\twords.";
  848. maxWidth = int.MaxValue;
  849. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  850. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  851. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  852. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  853. Assert.True (expectedClippedWidth <= maxWidth);
  854. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  855. text = "A\tsentence\thas\twords.";
  856. maxWidth = 10;
  857. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  858. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  859. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  860. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  861. Assert.True (expectedClippedWidth <= maxWidth);
  862. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  863. text = "line1\nline2\nline3long!";
  864. maxWidth = int.MaxValue;
  865. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  866. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  867. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  868. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  869. Assert.True (expectedClippedWidth <= maxWidth);
  870. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  871. text = "line1\nline2\nline3long!";
  872. maxWidth = 10;
  873. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  874. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  875. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  876. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  877. Assert.True (expectedClippedWidth <= maxWidth);
  878. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  879. // Unicode
  880. text = " ~  s  gui.cs   master ↑10";
  881. maxWidth = 10;
  882. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  883. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  884. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  885. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  886. Assert.True (expectedClippedWidth <= maxWidth);
  887. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  888. // should fit
  889. text = "Ð ÑÐ";
  890. maxWidth = text.RuneCount + 1;
  891. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  892. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  893. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  894. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  895. Assert.True (expectedClippedWidth <= maxWidth);
  896. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  897. // Should fit.
  898. maxWidth = text.RuneCount + 0;
  899. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  900. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  901. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  902. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  903. Assert.True (expectedClippedWidth <= maxWidth);
  904. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  905. // Should not fit
  906. maxWidth = text.RuneCount - 1;
  907. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  908. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  909. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  910. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  911. Assert.True (expectedClippedWidth <= maxWidth);
  912. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  913. }
  914. [Fact]
  915. public void ClipAndJustify_Valid_Justified ()
  916. {
  917. var align = TextAlignment.Justified;
  918. var text = ustring.Empty;
  919. var justifiedText = ustring.Empty;
  920. int maxWidth = 0;
  921. int expectedClippedWidth = 0;
  922. text = "test";
  923. maxWidth = 0;
  924. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  925. text = "test";
  926. maxWidth = 2;
  927. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  928. text = "test";
  929. maxWidth = int.MaxValue;
  930. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  931. Assert.True (justifiedText.RuneCount <= maxWidth);
  932. text = "A sentence has words.";
  933. // should fit
  934. maxWidth = text.RuneCount + 1;
  935. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  936. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  937. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  938. Assert.True (expectedClippedWidth <= maxWidth);
  939. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  940. // Should fit.
  941. maxWidth = text.RuneCount + 0;
  942. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  943. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  944. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  945. Assert.True (expectedClippedWidth <= maxWidth);
  946. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  947. // Should fit.
  948. maxWidth = 500;
  949. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  950. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  951. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  952. Assert.True (expectedClippedWidth <= maxWidth);
  953. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  954. // Should not fit
  955. maxWidth = text.RuneCount - 1;
  956. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  957. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  958. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  959. Assert.True (expectedClippedWidth <= maxWidth);
  960. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  961. // Should not fit
  962. maxWidth = 10;
  963. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  964. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  965. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  966. Assert.True (expectedClippedWidth <= maxWidth);
  967. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  968. text = "A\tsentence\thas\twords.";
  969. maxWidth = int.MaxValue;
  970. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  971. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  972. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  973. Assert.True (expectedClippedWidth <= maxWidth);
  974. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  975. text = "A\tsentence\thas\twords.";
  976. maxWidth = 10;
  977. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  978. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  979. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  980. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  981. Assert.True (expectedClippedWidth <= maxWidth);
  982. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  983. text = "line1\nline2\nline3long!";
  984. maxWidth = int.MaxValue;
  985. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  986. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  987. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  988. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  989. Assert.True (expectedClippedWidth <= maxWidth);
  990. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  991. text = "line1\nline2\nline3long!";
  992. maxWidth = 10;
  993. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  994. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  995. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  996. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  997. Assert.True (expectedClippedWidth <= maxWidth);
  998. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  999. // Unicode
  1000. text = " ~  s  gui.cs   master ↑10";
  1001. maxWidth = 10;
  1002. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1003. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  1004. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  1005. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  1006. Assert.True (expectedClippedWidth <= maxWidth);
  1007. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  1008. // should fit
  1009. text = "Ð ÑÐ";
  1010. maxWidth = text.RuneCount + 1;
  1011. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1012. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  1013. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  1014. Assert.True (expectedClippedWidth <= maxWidth);
  1015. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  1016. // Should fit.
  1017. maxWidth = text.RuneCount + 0;
  1018. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1019. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  1020. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  1021. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  1022. Assert.True (expectedClippedWidth <= maxWidth);
  1023. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  1024. // Should not fit
  1025. maxWidth = text.RuneCount - 1;
  1026. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1027. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  1028. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  1029. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  1030. Assert.True (expectedClippedWidth <= maxWidth);
  1031. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  1032. // see Justify_ tests below
  1033. }
  1034. [Fact]
  1035. public void Justify_Invalid ()
  1036. {
  1037. var text = ustring.Empty;
  1038. Assert.Equal (text, TextFormatter.Justify (text, 0));
  1039. text = null;
  1040. Assert.Equal (text, TextFormatter.Justify (text, 0));
  1041. text = "test";
  1042. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  1043. }
  1044. [Fact]
  1045. public void Justify_SingleWord ()
  1046. {
  1047. var text = ustring.Empty;
  1048. var justifiedText = ustring.Empty;
  1049. int width = 0;
  1050. char fillChar = '+';
  1051. // Even # of chars
  1052. text = "word";
  1053. justifiedText = text;
  1054. width = text.RuneCount;
  1055. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1056. width = text.RuneCount + 1;
  1057. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1058. width = text.RuneCount + 2;
  1059. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1060. width = text.RuneCount + 10;
  1061. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1062. width = text.RuneCount + 11;
  1063. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1064. // Odd # of chars
  1065. text = "word.";
  1066. justifiedText = text;
  1067. width = text.RuneCount;
  1068. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1069. width = text.RuneCount + 1;
  1070. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1071. width = text.RuneCount + 2;
  1072. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1073. width = text.RuneCount + 10;
  1074. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1075. width = text.RuneCount + 11;
  1076. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1077. // Unicode (even #)
  1078. text = "пÑивеÑ";
  1079. justifiedText = text;
  1080. width = text.RuneCount;
  1081. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1082. width = text.RuneCount + 1;
  1083. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1084. width = text.RuneCount + 2;
  1085. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1086. width = text.RuneCount + 10;
  1087. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1088. width = text.RuneCount + 11;
  1089. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1090. // Unicode (odd # of chars)
  1091. text = "пÑивеÑ.";
  1092. justifiedText = text;
  1093. width = text.RuneCount;
  1094. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1095. width = text.RuneCount + 1;
  1096. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1097. width = text.RuneCount + 2;
  1098. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1099. width = text.RuneCount + 10;
  1100. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1101. width = text.RuneCount + 11;
  1102. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1103. }
  1104. [Fact]
  1105. public void Justify_Sentence ()
  1106. {
  1107. var text = ustring.Empty;
  1108. var justifiedText = ustring.Empty;
  1109. int forceToWidth = 0;
  1110. char fillChar = '+';
  1111. // Even # of spaces
  1112. // 0123456789
  1113. text = "012 456 89";
  1114. forceToWidth = text.RuneCount;
  1115. justifiedText = text.Replace (" ", "+");
  1116. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1117. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1118. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1119. justifiedText = text.Replace (" ", "+");
  1120. forceToWidth = text.RuneCount + 1;
  1121. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1122. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1123. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1124. justifiedText = text.Replace (" ", "++");
  1125. forceToWidth = text.RuneCount + 2;
  1126. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1127. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1128. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1129. justifiedText = text.Replace (" ", "++");
  1130. forceToWidth = text.RuneCount + 3;
  1131. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1132. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1133. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1134. justifiedText = text.Replace (" ", "+++");
  1135. forceToWidth = text.RuneCount + 4;
  1136. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1137. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1138. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1139. justifiedText = text.Replace (" ", "+++");
  1140. forceToWidth = text.RuneCount + 5;
  1141. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1142. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1143. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1144. justifiedText = text.Replace (" ", "++++");
  1145. forceToWidth = text.RuneCount + 6;
  1146. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1147. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1148. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1149. justifiedText = text.Replace (" ", "+++++++++++");
  1150. forceToWidth = text.RuneCount + 20;
  1151. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1152. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1153. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1154. justifiedText = text.Replace (" ", "++++++++++++");
  1155. forceToWidth = text.RuneCount + 23;
  1156. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1157. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1158. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1159. // Odd # of spaces
  1160. // 0123456789
  1161. text = "012 456 89 end";
  1162. forceToWidth = text.RuneCount;
  1163. justifiedText = text.Replace (" ", "+");
  1164. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1165. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1166. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1167. justifiedText = text.Replace (" ", "+");
  1168. forceToWidth = text.RuneCount + 1;
  1169. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1170. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1171. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1172. justifiedText = text.Replace (" ", "+");
  1173. forceToWidth = text.RuneCount + 2;
  1174. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1175. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1176. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1177. justifiedText = text.Replace (" ", "++");
  1178. forceToWidth = text.RuneCount + 3;
  1179. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1180. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1181. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1182. justifiedText = text.Replace (" ", "++");
  1183. forceToWidth = text.RuneCount + 4;
  1184. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1185. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1186. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1187. justifiedText = text.Replace (" ", "++");
  1188. forceToWidth = text.RuneCount + 5;
  1189. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1190. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1191. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1192. justifiedText = text.Replace (" ", "+++");
  1193. forceToWidth = text.RuneCount + 6;
  1194. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1195. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1196. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1197. justifiedText = text.Replace (" ", "+++++++");
  1198. forceToWidth = text.RuneCount + 20;
  1199. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1200. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1201. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1202. justifiedText = text.Replace (" ", "++++++++");
  1203. forceToWidth = text.RuneCount + 23;
  1204. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1205. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1206. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1207. // Unicode
  1208. // Even # of chars
  1209. // 0123456789
  1210. text = "пÑРвРÑ";
  1211. forceToWidth = text.RuneCount;
  1212. justifiedText = text.Replace (" ", "+");
  1213. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1214. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1215. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1216. justifiedText = text.Replace (" ", "+");
  1217. forceToWidth = text.RuneCount + 1;
  1218. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1219. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1220. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1221. justifiedText = text.Replace (" ", "++");
  1222. forceToWidth = text.RuneCount + 2;
  1223. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1224. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1225. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1226. justifiedText = text.Replace (" ", "++");
  1227. forceToWidth = text.RuneCount + 3;
  1228. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1229. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1230. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1231. justifiedText = text.Replace (" ", "+++");
  1232. forceToWidth = text.RuneCount + 4;
  1233. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1234. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1235. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1236. justifiedText = text.Replace (" ", "+++");
  1237. forceToWidth = text.RuneCount + 5;
  1238. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1239. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1240. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1241. justifiedText = text.Replace (" ", "++++");
  1242. forceToWidth = text.RuneCount + 6;
  1243. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1244. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1245. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1246. justifiedText = text.Replace (" ", "+++++++++++");
  1247. forceToWidth = text.RuneCount + 20;
  1248. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1249. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1250. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1251. justifiedText = text.Replace (" ", "++++++++++++");
  1252. forceToWidth = text.RuneCount + 23;
  1253. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1254. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1255. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1256. // Unicode
  1257. // Odd # of chars
  1258. // 0123456789
  1259. text = "Ð ÑРвРÑ";
  1260. forceToWidth = text.RuneCount;
  1261. justifiedText = text.Replace (" ", "+");
  1262. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1263. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1264. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1265. justifiedText = text.Replace (" ", "+");
  1266. forceToWidth = text.RuneCount + 1;
  1267. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1268. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1269. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1270. justifiedText = text.Replace (" ", "+");
  1271. forceToWidth = text.RuneCount + 2;
  1272. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1273. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1274. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1275. justifiedText = text.Replace (" ", "++");
  1276. forceToWidth = text.RuneCount + 3;
  1277. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1278. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1279. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1280. justifiedText = text.Replace (" ", "++");
  1281. forceToWidth = text.RuneCount + 4;
  1282. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1283. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1284. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1285. justifiedText = text.Replace (" ", "++");
  1286. forceToWidth = text.RuneCount + 5;
  1287. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1288. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1289. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1290. justifiedText = text.Replace (" ", "+++");
  1291. forceToWidth = text.RuneCount + 6;
  1292. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1293. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1294. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1295. justifiedText = text.Replace (" ", "+++++++");
  1296. forceToWidth = text.RuneCount + 20;
  1297. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1298. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1299. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1300. justifiedText = text.Replace (" ", "++++++++");
  1301. forceToWidth = text.RuneCount + 23;
  1302. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1303. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1304. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1305. }
  1306. [Fact]
  1307. public void WordWrap_Invalid ()
  1308. {
  1309. var text = ustring.Empty;
  1310. int width = 0;
  1311. Assert.Empty (TextFormatter.WordWrap (null, width));
  1312. Assert.Empty (TextFormatter.WordWrap (text, width));
  1313. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrap (text, -1));
  1314. }
  1315. [Fact]
  1316. public void WordWrap_SingleWordLine ()
  1317. {
  1318. var text = ustring.Empty;
  1319. int width = 0;
  1320. List<ustring> wrappedLines;
  1321. text = "Constantinople";
  1322. width = text.RuneCount;
  1323. wrappedLines = TextFormatter.WordWrap (text, width);
  1324. Assert.True (wrappedLines.Count == 1);
  1325. width = text.RuneCount - 1;
  1326. wrappedLines = TextFormatter.WordWrap (text, width);
  1327. Assert.Equal (2, wrappedLines.Count);
  1328. Assert.Equal (text [0, text.RuneCount - 1].ToString (), wrappedLines [0].ToString ());
  1329. Assert.Equal ("e", wrappedLines [1].ToString ());
  1330. width = text.RuneCount - 2;
  1331. wrappedLines = TextFormatter.WordWrap (text, width);
  1332. Assert.Equal (2, wrappedLines.Count);
  1333. Assert.Equal (text [0, text.RuneCount - 2].ToString (), wrappedLines [0].ToString ());
  1334. width = text.RuneCount - 5;
  1335. wrappedLines = TextFormatter.WordWrap (text, width);
  1336. Assert.Equal (2, wrappedLines.Count);
  1337. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1338. wrappedLines = TextFormatter.WordWrap (text, width);
  1339. Assert.Equal (2, wrappedLines.Count);
  1340. Assert.Equal ("Constan", wrappedLines [0].ToString ());
  1341. Assert.Equal ("tinople", wrappedLines [1].ToString ());
  1342. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1343. wrappedLines = TextFormatter.WordWrap (text, width);
  1344. Assert.Equal (3, wrappedLines.Count);
  1345. Assert.Equal ("Const", wrappedLines [0].ToString ());
  1346. Assert.Equal ("antin", wrappedLines [1].ToString ());
  1347. Assert.Equal ("ople", wrappedLines [2].ToString ());
  1348. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1349. wrappedLines = TextFormatter.WordWrap (text, width);
  1350. Assert.Equal (4, wrappedLines.Count);
  1351. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1352. wrappedLines = TextFormatter.WordWrap (text, width);
  1353. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1354. Assert.Equal (text.ConsoleWidth, wrappedLines.Count);
  1355. Assert.Equal ("C", wrappedLines [0].ToString ());
  1356. Assert.Equal ("o", wrappedLines [1].ToString ());
  1357. Assert.Equal ("n", wrappedLines [2].ToString ());
  1358. Assert.Equal ("s", wrappedLines [3].ToString ());
  1359. Assert.Equal ("t", wrappedLines [4].ToString ());
  1360. Assert.Equal ("a", wrappedLines [5].ToString ());
  1361. Assert.Equal ("n", wrappedLines [6].ToString ());
  1362. Assert.Equal ("t", wrappedLines [7].ToString ());
  1363. Assert.Equal ("i", wrappedLines [8].ToString ());
  1364. Assert.Equal ("n", wrappedLines [9].ToString ());
  1365. Assert.Equal ("o", wrappedLines [10].ToString ());
  1366. Assert.Equal ("p", wrappedLines [11].ToString ());
  1367. Assert.Equal ("l", wrappedLines [12].ToString ());
  1368. Assert.Equal ("e", wrappedLines [13].ToString ());
  1369. }
  1370. [Fact]
  1371. public void WordWrap_Unicode_SingleWordLine ()
  1372. {
  1373. var text = ustring.Empty;
  1374. int width = 0;
  1375. List<ustring> wrappedLines;
  1376. text = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ";
  1377. width = text.RuneCount;
  1378. wrappedLines = TextFormatter.WordWrap (text, width);
  1379. Assert.True (wrappedLines.Count == 1);
  1380. width = text.RuneCount - 1;
  1381. wrappedLines = TextFormatter.WordWrap (text, width);
  1382. Assert.Equal (2, wrappedLines.Count);
  1383. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1384. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) - 1)]).ToString (), wrappedLines [0].ToString ());
  1385. Assert.Equal ("ำ", wrappedLines [1].ToString ());
  1386. width = text.RuneCount - 2;
  1387. wrappedLines = TextFormatter.WordWrap (text, width);
  1388. Assert.Equal (2, wrappedLines.Count);
  1389. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1390. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) - 2)]).ToString (), wrappedLines [0].ToString ());
  1391. width = text.RuneCount - 5;
  1392. wrappedLines = TextFormatter.WordWrap (text, width);
  1393. Assert.Equal (2, wrappedLines.Count);
  1394. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1395. wrappedLines = TextFormatter.WordWrap (text, width);
  1396. Assert.Equal (2, wrappedLines.Count);
  1397. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", wrappedLines [0].ToString ());
  1398. Assert.Equal ("ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [1].ToString ());
  1399. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1400. wrappedLines = TextFormatter.WordWrap (text, width);
  1401. Assert.Equal (3, wrappedLines.Count);
  1402. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑ", wrappedLines [0].ToString ());
  1403. Assert.Equal ("ฒณดตถทธนบปผฝพฟภมย", wrappedLines [1].ToString ());
  1404. Assert.Equal ("รฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [2].ToString ());
  1405. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1406. wrappedLines = TextFormatter.WordWrap (text, width);
  1407. Assert.Equal (4, wrappedLines.Count);
  1408. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1409. wrappedLines = TextFormatter.WordWrap (text, width);
  1410. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1411. Assert.Equal (text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)), wrappedLines.Count);
  1412. Assert.Equal ("ก", wrappedLines [0].ToString ());
  1413. Assert.Equal ("ข", wrappedLines [1].ToString ());
  1414. Assert.Equal ("ฃ", wrappedLines [2].ToString ());
  1415. Assert.Equal ("ำ", wrappedLines [^1].ToString ());
  1416. }
  1417. [Fact]
  1418. public void WordWrap_Unicode_LineWithNonBreakingSpace ()
  1419. {
  1420. var text = ustring.Empty;
  1421. int width = 0;
  1422. List<ustring> wrappedLines;
  1423. text = "This\u00A0is\u00A0a\u00A0sentence.";
  1424. width = text.RuneCount;
  1425. wrappedLines = TextFormatter.WordWrap (text, width);
  1426. Assert.True (wrappedLines.Count == 1);
  1427. width = text.RuneCount - 1;
  1428. wrappedLines = TextFormatter.WordWrap (text, width);
  1429. Assert.Equal (2, wrappedLines.Count);
  1430. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1431. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) - 1)]).ToString (), wrappedLines [0].ToString ());
  1432. Assert.Equal (".", wrappedLines [1].ToString ());
  1433. width = text.RuneCount - 2;
  1434. wrappedLines = TextFormatter.WordWrap (text, width);
  1435. Assert.Equal (2, wrappedLines.Count);
  1436. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1437. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) - 2)]).ToString (), wrappedLines [0].ToString ());
  1438. width = text.RuneCount - 5;
  1439. wrappedLines = TextFormatter.WordWrap (text, width);
  1440. Assert.Equal (2, wrappedLines.Count);
  1441. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1442. wrappedLines = TextFormatter.WordWrap (text, width);
  1443. Assert.Equal (2, wrappedLines.Count);
  1444. Assert.Equal ("This\u00A0is\u00A0a\u00A0", wrappedLines [0].ToString ());
  1445. Assert.Equal ("sentence.", wrappedLines [1].ToString ());
  1446. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1447. wrappedLines = TextFormatter.WordWrap (text, width);
  1448. Assert.Equal (3, wrappedLines.Count);
  1449. Assert.Equal ("This\u00A0is", wrappedLines [0].ToString ());
  1450. Assert.Equal ("\u00a0a\u00a0sent", wrappedLines [1].ToString ());
  1451. Assert.Equal ("ence.", wrappedLines [2].ToString ());
  1452. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1453. wrappedLines = TextFormatter.WordWrap (text, width);
  1454. Assert.Equal (4, wrappedLines.Count);
  1455. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1456. wrappedLines = TextFormatter.WordWrap (text, width);
  1457. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1458. Assert.Equal (text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)), wrappedLines.Count);
  1459. Assert.Equal ("T", wrappedLines [0].ToString ());
  1460. Assert.Equal ("h", wrappedLines [1].ToString ());
  1461. Assert.Equal ("i", wrappedLines [2].ToString ());
  1462. Assert.Equal (".", wrappedLines [^1].ToString ());
  1463. }
  1464. [Fact]
  1465. public void WordWrap_Unicode_2LinesWithNonBreakingSpace ()
  1466. {
  1467. var text = ustring.Empty;
  1468. int width = 0;
  1469. List<ustring> wrappedLines;
  1470. text = "This\u00A0is\n\u00A0a\u00A0sentence.";
  1471. width = text.RuneCount;
  1472. wrappedLines = TextFormatter.WordWrap (text, width);
  1473. Assert.True (wrappedLines.Count == 1);
  1474. width = text.RuneCount - 1;
  1475. wrappedLines = TextFormatter.WordWrap (text, width);
  1476. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1477. Assert.Equal (1, wrappedLines.Count);
  1478. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1479. Assert.Equal ("This\u00A0is\u00A0a\u00A0sentence.", wrappedLines [0].ToString ());
  1480. text = "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.";
  1481. width = text.RuneCount;
  1482. wrappedLines = TextFormatter.WordWrap (text, width);
  1483. Assert.True (wrappedLines.Count == 1);
  1484. }
  1485. [Fact]
  1486. public void WordWrap_NoNewLines ()
  1487. {
  1488. var text = ustring.Empty;
  1489. int maxWidth = 0;
  1490. int expectedClippedWidth = 0;
  1491. List<ustring> wrappedLines;
  1492. text = "A sentence has words.";
  1493. maxWidth = text.RuneCount;
  1494. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1495. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1496. Assert.True (wrappedLines.Count == 1);
  1497. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1498. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1499. Assert.Equal ("A sentence has words.", wrappedLines [0].ToString ());
  1500. maxWidth = text.RuneCount - 1;
  1501. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1502. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1503. Assert.Equal (2, wrappedLines.Count);
  1504. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1505. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1506. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1507. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1508. maxWidth = text.RuneCount - "words.".Length;
  1509. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1510. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1511. Assert.Equal (2, wrappedLines.Count);
  1512. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1513. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1514. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1515. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1516. maxWidth = text.RuneCount - " words.".Length;
  1517. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1518. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1519. Assert.Equal (2, wrappedLines.Count);
  1520. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1521. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1522. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1523. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1524. maxWidth = text.RuneCount - "s words.".Length;
  1525. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1526. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1527. Assert.Equal (2, wrappedLines.Count);
  1528. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1529. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1530. Assert.Equal ("A sentence", wrappedLines [0].ToString ());
  1531. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1532. // Unicode
  1533. text = "A Unicode sentence (пÑивеÑ) has words.";
  1534. maxWidth = text.RuneCount;
  1535. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1536. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1537. Assert.True (wrappedLines.Count == 1);
  1538. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1539. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1540. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.", wrappedLines [0].ToString ());
  1541. maxWidth = text.RuneCount - 1;
  1542. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1543. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1544. Assert.Equal (2, wrappedLines.Count);
  1545. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1546. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1547. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1548. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1549. maxWidth = text.RuneCount - "words.".Length;
  1550. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1551. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1552. Assert.Equal (2, wrappedLines.Count);
  1553. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1554. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1555. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1556. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1557. maxWidth = text.RuneCount - " words.".Length;
  1558. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1559. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1560. Assert.Equal (2, wrappedLines.Count);
  1561. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1562. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1563. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1564. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1565. maxWidth = text.RuneCount - "s words.".Length;
  1566. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1567. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1568. Assert.Equal (2, wrappedLines.Count);
  1569. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1570. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1571. Assert.Equal ("A Unicode sentence (пÑивеÑ)", wrappedLines [0].ToString ());
  1572. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1573. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1574. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1575. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1576. Assert.Equal (2, wrappedLines.Count);
  1577. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1578. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1579. Assert.Equal ("A Unicode sentence", wrappedLines [0].ToString ());
  1580. Assert.Equal ("(пÑивеÑ) has words.", wrappedLines [1].ToString ());
  1581. }
  1582. /// <summary>
  1583. /// WordWrap strips CRLF
  1584. /// </summary>
  1585. [Fact]
  1586. public void WordWrap_WithNewLines ()
  1587. {
  1588. var text = ustring.Empty;
  1589. int maxWidth = 0;
  1590. int expectedClippedWidth = 0;
  1591. List<ustring> wrappedLines;
  1592. text = "A sentence has words.\nA paragraph has lines.";
  1593. maxWidth = text.RuneCount;
  1594. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1595. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1596. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1597. Assert.Equal (1, wrappedLines.Count);
  1598. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1599. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1600. Assert.Equal ("A sentence has words.A paragraph has lines.", wrappedLines [0].ToString ());
  1601. maxWidth = text.RuneCount - 1;
  1602. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1603. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1604. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1605. Assert.Equal (1, wrappedLines.Count);
  1606. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1607. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1608. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1609. Assert.Equal ("A sentence has words.A paragraph has lines.", wrappedLines [0].ToString ());
  1610. maxWidth = text.RuneCount - "words.".Length;
  1611. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1612. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1613. Assert.Equal (2, wrappedLines.Count);
  1614. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1615. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1616. Assert.Equal ("A sentence has words.A paragraph has", wrappedLines [0].ToString ());
  1617. Assert.Equal ("lines.", wrappedLines [1].ToString ());
  1618. // Unicode
  1619. text = "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии.";
  1620. maxWidth = text.RuneCount;
  1621. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1622. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1623. Assert.True (wrappedLines.Count == 1);
  1624. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1625. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1626. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии.", wrappedLines [0].ToString ());
  1627. maxWidth = text.RuneCount - 1;
  1628. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1629. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1630. Assert.Equal (2, wrappedLines.Count);
  1631. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1632. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1633. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", wrappedLines [0].ToString ());
  1634. Assert.Equal ("Линии.", wrappedLines [1].ToString ());
  1635. maxWidth = text.RuneCount - "words.".Length;
  1636. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1637. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1638. Assert.Equal (2, wrappedLines.Count);
  1639. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1640. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1641. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", wrappedLines [0].ToString ());
  1642. Assert.Equal ("Линии.", wrappedLines [1].ToString ());
  1643. maxWidth = text.RuneCount - "s words.".Length;
  1644. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1645. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1646. Assert.Equal (2, wrappedLines.Count);
  1647. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1648. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1649. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", wrappedLines [0].ToString ());
  1650. Assert.Equal ("has Линии.", wrappedLines [1].ToString ());
  1651. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1652. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1653. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1654. Assert.Equal (2, wrappedLines.Count);
  1655. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1656. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1657. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode", wrappedLines [0].ToString ());
  1658. Assert.Equal ("Пункт has Линии.", wrappedLines [1].ToString ());
  1659. }
  1660. [Fact]
  1661. public void WordWrap_Narrow ()
  1662. {
  1663. var text = ustring.Empty;
  1664. int maxWidth = 1;
  1665. int expectedClippedWidth = 1;
  1666. List<ustring> wrappedLines;
  1667. text = "A sentence has words.";
  1668. maxWidth = 3;
  1669. expectedClippedWidth = 3;
  1670. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1671. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1672. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1673. Assert.Equal ("A", wrappedLines [0].ToString ());
  1674. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1675. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1676. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1677. Assert.Equal ("has", wrappedLines [4].ToString ());
  1678. Assert.Equal ("wor", wrappedLines [5].ToString ());
  1679. Assert.Equal ("ds.", wrappedLines [6].ToString ());
  1680. maxWidth = 2;
  1681. expectedClippedWidth = 2;
  1682. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1683. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1684. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1685. Assert.Equal ("A", wrappedLines [0].ToString ());
  1686. Assert.Equal ("se", wrappedLines [1].ToString ());
  1687. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1688. Assert.Equal ("en", wrappedLines [3].ToString ());
  1689. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1690. maxWidth = 1;
  1691. expectedClippedWidth = 1;
  1692. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1693. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1694. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1695. Assert.Equal ("A", wrappedLines [0].ToString ());
  1696. Assert.Equal ("s", wrappedLines [1].ToString ());
  1697. Assert.Equal ("e", wrappedLines [2].ToString ());
  1698. Assert.Equal ("n", wrappedLines [3].ToString ());
  1699. Assert.Equal (".", wrappedLines [^1].ToString ());
  1700. }
  1701. [Fact]
  1702. public void WordWrap_preserveTrailingSpaces ()
  1703. {
  1704. var text = ustring.Empty;
  1705. int maxWidth = 1;
  1706. int expectedClippedWidth = 1;
  1707. List<ustring> wrappedLines;
  1708. text = "A sentence has words.";
  1709. maxWidth = 14;
  1710. expectedClippedWidth = 14;
  1711. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1712. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1713. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1714. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1715. Assert.Equal (" words.", wrappedLines [1].ToString ());
  1716. Assert.True (wrappedLines.Count == 2);
  1717. maxWidth = 3;
  1718. expectedClippedWidth = 3;
  1719. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1720. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1721. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1722. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1723. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1724. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1725. Assert.Equal ("ce ", wrappedLines [3].ToString ());
  1726. Assert.Equal ("has", wrappedLines [4].ToString ());
  1727. Assert.Equal (" ", wrappedLines [5].ToString ());
  1728. Assert.Equal ("wor", wrappedLines [6].ToString ());
  1729. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  1730. Assert.True (wrappedLines.Count == 8);
  1731. maxWidth = 2;
  1732. expectedClippedWidth = 2;
  1733. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1734. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1735. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1736. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1737. Assert.Equal ("se", wrappedLines [1].ToString ());
  1738. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1739. Assert.Equal ("en", wrappedLines [3].ToString ());
  1740. Assert.Equal ("ce", wrappedLines [4].ToString ());
  1741. Assert.Equal (" ", wrappedLines [5].ToString ());
  1742. Assert.Equal ("ha", wrappedLines [6].ToString ());
  1743. Assert.Equal ("s ", wrappedLines [7].ToString ());
  1744. Assert.Equal ("wo", wrappedLines [8].ToString ());
  1745. Assert.Equal ("rd", wrappedLines [9].ToString ());
  1746. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1747. Assert.True (wrappedLines.Count == 11);
  1748. maxWidth = 1;
  1749. expectedClippedWidth = 1;
  1750. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1751. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1752. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1753. Assert.Equal ("A", wrappedLines [0].ToString ());
  1754. Assert.Equal (" ", wrappedLines [1].ToString ());
  1755. Assert.Equal ("s", wrappedLines [2].ToString ());
  1756. Assert.Equal ("e", wrappedLines [3].ToString ());
  1757. Assert.Equal ("n", wrappedLines [4].ToString ());
  1758. Assert.Equal ("t", wrappedLines [5].ToString ());
  1759. Assert.Equal ("e", wrappedLines [6].ToString ());
  1760. Assert.Equal ("n", wrappedLines [7].ToString ());
  1761. Assert.Equal ("c", wrappedLines [8].ToString ());
  1762. Assert.Equal ("e", wrappedLines [9].ToString ());
  1763. Assert.Equal (" ", wrappedLines [10].ToString ());
  1764. Assert.Equal (".", wrappedLines [^1].ToString ());
  1765. Assert.True (wrappedLines.Count == text.Length);
  1766. }
  1767. [Fact]
  1768. public void WordWrap_preserveTrailingSpaces_Wide_Runes ()
  1769. {
  1770. var text = ustring.Empty;
  1771. int maxWidth = 1;
  1772. int expectedClippedWidth = 1;
  1773. List<ustring> wrappedLines;
  1774. text = "文に は言葉 があり ます。";
  1775. maxWidth = 14;
  1776. expectedClippedWidth = 14;
  1777. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1778. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1779. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1780. Assert.Equal ("文に は言葉 ", wrappedLines [0].ToString ());
  1781. Assert.Equal ("があり ます。", wrappedLines [1].ToString ());
  1782. Assert.True (wrappedLines.Count == 2);
  1783. maxWidth = 3;
  1784. expectedClippedWidth = 3;
  1785. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1786. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1787. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1788. Assert.Equal ("文", wrappedLines [0].ToString ());
  1789. Assert.Equal ("に ", wrappedLines [1].ToString ());
  1790. Assert.Equal ("は", wrappedLines [2].ToString ());
  1791. Assert.Equal ("言", wrappedLines [3].ToString ());
  1792. Assert.Equal ("葉 ", wrappedLines [4].ToString ());
  1793. Assert.Equal ("が", wrappedLines [5].ToString ());
  1794. Assert.Equal ("あ", wrappedLines [6].ToString ());
  1795. Assert.Equal ("り ", wrappedLines [7].ToString ());
  1796. Assert.Equal ("ま", wrappedLines [8].ToString ());
  1797. Assert.Equal ("す", wrappedLines [9].ToString ());
  1798. Assert.Equal ("。", wrappedLines [^1].ToString ());
  1799. Assert.True (wrappedLines.Count == 11);
  1800. maxWidth = 2;
  1801. expectedClippedWidth = 2;
  1802. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1803. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1804. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1805. Assert.Equal ("文", wrappedLines [0].ToString ());
  1806. Assert.Equal ("に", wrappedLines [1].ToString ());
  1807. Assert.Equal (" ", wrappedLines [2].ToString ());
  1808. Assert.Equal ("は", wrappedLines [3].ToString ());
  1809. Assert.Equal ("言", wrappedLines [4].ToString ());
  1810. Assert.Equal ("葉", wrappedLines [5].ToString ());
  1811. Assert.Equal (" ", wrappedLines [6].ToString ());
  1812. Assert.Equal ("が", wrappedLines [7].ToString ());
  1813. Assert.Equal ("あ", wrappedLines [8].ToString ());
  1814. Assert.Equal ("り", wrappedLines [9].ToString ());
  1815. Assert.Equal (" ", wrappedLines [10].ToString ());
  1816. Assert.Equal ("ま", wrappedLines [11].ToString ());
  1817. Assert.Equal ("す", wrappedLines [12].ToString ());
  1818. Assert.Equal ("。", wrappedLines [^1].ToString ());
  1819. Assert.True (wrappedLines.Count == 14);
  1820. maxWidth = 1;
  1821. expectedClippedWidth = 0;
  1822. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1823. Assert.Empty (wrappedLines);
  1824. Assert.False (wrappedLines.Count == text.Length);
  1825. Assert.False (wrappedLines.Count == text.RuneCount);
  1826. Assert.False (wrappedLines.Count == text.ConsoleWidth);
  1827. Assert.Equal (25, text.ConsoleWidth);
  1828. Assert.Equal (25, TextFormatter.GetTextWidth (text));
  1829. }
  1830. [Fact, AutoInitShutdown]
  1831. public void WordWrap_preserveTrailingSpaces_Horizontal_With_Simple_Runes ()
  1832. {
  1833. var text = "A sentence has words.";
  1834. var width = 3;
  1835. var height = 8;
  1836. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1837. var breakLines = "";
  1838. foreach (var line in wrappedLines) {
  1839. breakLines += $"{line}{Environment.NewLine}";
  1840. }
  1841. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  1842. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1843. frame.Add (label);
  1844. Application.Top.Add (frame);
  1845. Application.Begin (Application.Top);
  1846. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1847. Assert.True (label.AutoSize);
  1848. Assert.Equal (new Rect (0, 0, width, height + 1), label.Frame);
  1849. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1850. var expected = @"
  1851. ┌───┐
  1852. │A │
  1853. │sen│
  1854. │ten│
  1855. │ce │
  1856. │has│
  1857. │ │
  1858. │wor│
  1859. │ds.│
  1860. └───┘
  1861. ";
  1862. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1863. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1864. }
  1865. [Fact, AutoInitShutdown]
  1866. public void WordWrap_preserveTrailingSpaces_Vertical_With_Simple_Runes ()
  1867. {
  1868. var text = "A sentence has words.";
  1869. var width = 8;
  1870. var height = 3;
  1871. var wrappedLines = TextFormatter.WordWrap (text, height, true);
  1872. var breakLines = "";
  1873. for (int i = 0; i < wrappedLines.Count; i++) {
  1874. breakLines += $"{wrappedLines [i]}{(i < wrappedLines.Count - 1 ? Environment.NewLine : string.Empty)}";
  1875. }
  1876. var label = new Label (breakLines) {
  1877. TextDirection = TextDirection.TopBottom_LeftRight,
  1878. Width = Dim.Fill (),
  1879. Height = Dim.Fill ()
  1880. };
  1881. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1882. frame.Add (label);
  1883. Application.Top.Add (frame);
  1884. Application.Begin (Application.Top);
  1885. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1886. Assert.True (label.AutoSize);
  1887. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1888. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1889. var expected = @"
  1890. ┌────────┐
  1891. │Astch wd│
  1892. │ eeea os│
  1893. │ nn s r.│
  1894. └────────┘
  1895. ";
  1896. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1897. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1898. }
  1899. [Fact, AutoInitShutdown]
  1900. public void WordWrap_preserveTrailingSpaces_Horizontal_With_Wide_Runes ()
  1901. {
  1902. var text = "文に は言葉 があり ます。";
  1903. var width = 6;
  1904. var height = 8;
  1905. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1906. var breakLines = "";
  1907. foreach (var line in wrappedLines) {
  1908. breakLines += $"{line}{Environment.NewLine}";
  1909. }
  1910. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  1911. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1912. frame.Add (label);
  1913. Application.Top.Add (frame);
  1914. Application.Begin (Application.Top);
  1915. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1916. Assert.True (label.AutoSize);
  1917. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1918. Assert.Equal (new Size (width, height), label.TextFormatter.Size);
  1919. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1920. var expected = @"
  1921. ┌──────┐
  1922. │文に │
  1923. │は言葉│
  1924. │ があ │
  1925. │り ま │
  1926. │す。 │
  1927. │ │
  1928. │ │
  1929. │ │
  1930. └──────┘
  1931. ";
  1932. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1933. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1934. }
  1935. [Fact, AutoInitShutdown]
  1936. public void WordWrap_preserveTrailingSpaces_Vertical_With_Wide_Runes ()
  1937. {
  1938. var text = "文に は言葉 があり ます。";
  1939. var width = 8;
  1940. var height = 4;
  1941. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1942. var breakLines = "";
  1943. for (int i = 0; i < wrappedLines.Count; i++) {
  1944. breakLines += $"{wrappedLines [i]}{(i < wrappedLines.Count - 1 ? Environment.NewLine : string.Empty)}";
  1945. }
  1946. var label = new Label (breakLines) {
  1947. TextDirection = TextDirection.TopBottom_LeftRight,
  1948. Width = Dim.Fill (),
  1949. Height = Dim.Fill ()
  1950. };
  1951. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1952. frame.Add (label);
  1953. Application.Top.Add (frame);
  1954. Application.Begin (Application.Top);
  1955. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1956. Assert.True (label.AutoSize);
  1957. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1958. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1959. var expected = @"
  1960. ┌────────┐
  1961. │文言あす│
  1962. │に葉り。│
  1963. │ │
  1964. │はがま │
  1965. └────────┘
  1966. ";
  1967. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1968. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1969. }
  1970. [Fact]
  1971. public void WordWrap_preserveTrailingSpaces_With_Tab ()
  1972. {
  1973. var text = ustring.Empty;
  1974. int maxWidth = 1;
  1975. int expectedClippedWidth = 1;
  1976. List<ustring> wrappedLines;
  1977. text = "A sentence\t\t\t has words.";
  1978. var tabWidth = 4;
  1979. maxWidth = 14;
  1980. expectedClippedWidth = 11;
  1981. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1982. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1983. Assert.Equal ("A sentence\t", wrappedLines [0].ToString ());
  1984. Assert.Equal ("\t\t has ", wrappedLines [1].ToString ());
  1985. Assert.Equal ("words.", wrappedLines [2].ToString ());
  1986. Assert.True (wrappedLines.Count == 3);
  1987. maxWidth = 3;
  1988. expectedClippedWidth = 3;
  1989. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1990. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1991. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1992. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1993. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1994. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1995. Assert.Equal ("\t", wrappedLines [4].ToString ());
  1996. Assert.Equal ("\t", wrappedLines [5].ToString ());
  1997. Assert.Equal ("\t", wrappedLines [6].ToString ());
  1998. Assert.Equal (" ", wrappedLines [7].ToString ());
  1999. Assert.Equal ("has", wrappedLines [8].ToString ());
  2000. Assert.Equal (" ", wrappedLines [9].ToString ());
  2001. Assert.Equal ("wor", wrappedLines [10].ToString ());
  2002. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  2003. Assert.True (wrappedLines.Count == 12);
  2004. maxWidth = 2;
  2005. expectedClippedWidth = 2;
  2006. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  2007. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  2008. Assert.Equal ("A ", wrappedLines [0].ToString ());
  2009. Assert.Equal ("se", wrappedLines [1].ToString ());
  2010. Assert.Equal ("nt", wrappedLines [2].ToString ());
  2011. Assert.Equal ("en", wrappedLines [3].ToString ());
  2012. Assert.Equal ("ce", wrappedLines [4].ToString ());
  2013. Assert.Equal ("\t", wrappedLines [5].ToString ());
  2014. Assert.Equal ("\t", wrappedLines [6].ToString ());
  2015. Assert.Equal ("\t", wrappedLines [7].ToString ());
  2016. Assert.Equal (" ", wrappedLines [8].ToString ());
  2017. Assert.Equal ("ha", wrappedLines [9].ToString ());
  2018. Assert.Equal ("s ", wrappedLines [10].ToString ());
  2019. Assert.Equal ("wo", wrappedLines [11].ToString ());
  2020. Assert.Equal ("rd", wrappedLines [12].ToString ());
  2021. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  2022. Assert.True (wrappedLines.Count == 14);
  2023. maxWidth = 1;
  2024. expectedClippedWidth = 1;
  2025. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  2026. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  2027. Assert.Equal ("A", wrappedLines [0].ToString ());
  2028. Assert.Equal (" ", wrappedLines [1].ToString ());
  2029. Assert.Equal ("s", wrappedLines [2].ToString ());
  2030. Assert.Equal ("e", wrappedLines [3].ToString ());
  2031. Assert.Equal ("n", wrappedLines [4].ToString ());
  2032. Assert.Equal ("t", wrappedLines [5].ToString ());
  2033. Assert.Equal ("e", wrappedLines [6].ToString ());
  2034. Assert.Equal ("n", wrappedLines [7].ToString ());
  2035. Assert.Equal ("c", wrappedLines [8].ToString ());
  2036. Assert.Equal ("e", wrappedLines [9].ToString ());
  2037. Assert.Equal ("\t", wrappedLines [10].ToString ());
  2038. Assert.Equal ("\t", wrappedLines [11].ToString ());
  2039. Assert.Equal ("\t", wrappedLines [12].ToString ());
  2040. Assert.Equal (" ", wrappedLines [13].ToString ());
  2041. Assert.Equal (".", wrappedLines [^1].ToString ());
  2042. Assert.True (wrappedLines.Count == text.Length);
  2043. }
  2044. [Fact]
  2045. public void WordWrap_Unicode_Wide_Runes ()
  2046. {
  2047. ustring text = "これが最初の行です。 こんにちは世界。 これが2行目です。";
  2048. var width = text.RuneCount;
  2049. var wrappedLines = TextFormatter.WordWrap (text, width);
  2050. Assert.Equal (3, wrappedLines.Count);
  2051. Assert.Equal ("これが最初の行です。", wrappedLines [0].ToString ());
  2052. Assert.Equal ("こんにちは世界。", wrappedLines [1].ToString ());
  2053. Assert.Equal ("これが2行目です。", wrappedLines [^1].ToString ());
  2054. }
  2055. [Fact]
  2056. public void ReplaceHotKeyWithTag ()
  2057. {
  2058. var tf = new TextFormatter ();
  2059. ustring text = "test";
  2060. int hotPos = 0;
  2061. uint tag = tf.HotKeyTagMask | 't';
  2062. Assert.Equal (ustring.Make (new Rune [] { tag, 'e', 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  2063. tag = tf.HotKeyTagMask | 'e';
  2064. hotPos = 1;
  2065. Assert.Equal (ustring.Make (new Rune [] { 't', tag, 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  2066. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  2067. Assert.Equal ('e', (uint)(result.ToRunes () [1] & ~tf.HotKeyTagMask));
  2068. text = "Ok";
  2069. tag = 0x100000 | 'O';
  2070. hotPos = 0;
  2071. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  2072. Assert.Equal ('O', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  2073. text = "[◦ Ok ◦]";
  2074. text = ustring.Make (new Rune [] { '[', '◦', ' ', 'O', 'k', ' ', '◦', ']' });
  2075. var runes = text.ToRuneList ();
  2076. Assert.Equal (text.RuneCount, runes.Count);
  2077. Assert.Equal (text, ustring.Make (runes));
  2078. tag = tf.HotKeyTagMask | 'O';
  2079. hotPos = 3;
  2080. Assert.Equal (ustring.Make (new Rune [] { '[', '◦', ' ', tag, 'k', ' ', '◦', ']' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  2081. Assert.Equal ('O', (uint)(result.ToRunes () [3] & ~tf.HotKeyTagMask));
  2082. text = "^k";
  2083. tag = '^';
  2084. hotPos = 0;
  2085. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  2086. Assert.Equal ('^', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  2087. }
  2088. [Fact]
  2089. public void Reformat_Invalid ()
  2090. {
  2091. var text = ustring.Empty;
  2092. var list = new List<ustring> ();
  2093. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, -1, TextAlignment.Left, false));
  2094. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  2095. Assert.NotEmpty (list);
  2096. Assert.True (list.Count == 1);
  2097. Assert.Equal (ustring.Empty, list [0]);
  2098. text = null;
  2099. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  2100. Assert.NotEmpty (list);
  2101. Assert.True (list.Count == 1);
  2102. Assert.Equal (ustring.Empty, list [0]);
  2103. list = TextFormatter.Format (text, 0, TextAlignment.Left, true);
  2104. Assert.NotEmpty (list);
  2105. Assert.True (list.Count == 1);
  2106. Assert.Equal (ustring.Empty, list [0]);
  2107. }
  2108. [Fact]
  2109. public void Reformat_NoWordrap_SingleLine ()
  2110. {
  2111. var text = ustring.Empty;
  2112. var list = new List<ustring> ();
  2113. var maxWidth = 0;
  2114. var expectedClippedWidth = 0;
  2115. var wrap = false;
  2116. maxWidth = 0;
  2117. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2118. Assert.True (list.Count == 1);
  2119. maxWidth = 1;
  2120. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2121. Assert.True (list.Count == 1);
  2122. text = "A sentence has words.";
  2123. maxWidth = 0;
  2124. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2125. Assert.True (list.Count == 1);
  2126. Assert.Equal (ustring.Empty, list [0]);
  2127. maxWidth = 1;
  2128. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2129. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2130. Assert.True (list.Count == 1);
  2131. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2132. maxWidth = 5;
  2133. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2134. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2135. Assert.True (list.Count == 1);
  2136. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2137. maxWidth = text.RuneCount - 1;
  2138. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2139. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2140. Assert.True (list.Count == 1);
  2141. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2142. // no clip
  2143. maxWidth = text.RuneCount + 0;
  2144. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2145. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2146. Assert.True (list.Count == 1);
  2147. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2148. maxWidth = text.RuneCount + 1;
  2149. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2150. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2151. Assert.True (list.Count == 1);
  2152. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2153. }
  2154. [Fact]
  2155. public void Reformat_NoWordrap_NewLines ()
  2156. {
  2157. var text = ustring.Empty;
  2158. var list = new List<ustring> ();
  2159. var maxWidth = 0;
  2160. var expectedClippedWidth = 0;
  2161. var wrap = false;
  2162. text = "A sentence has words.\nLine 2.";
  2163. maxWidth = 0;
  2164. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2165. Assert.True (list.Count == 1);
  2166. Assert.Equal (ustring.Empty, list [0]);
  2167. maxWidth = 1;
  2168. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2169. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2170. Assert.True (list.Count == 1);
  2171. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2172. maxWidth = 5;
  2173. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2174. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2175. Assert.True (list.Count == 1);
  2176. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2177. maxWidth = text.RuneCount - 1;
  2178. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2179. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2180. Assert.True (list.Count == 1);
  2181. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2182. // no clip
  2183. maxWidth = text.RuneCount + 0;
  2184. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2185. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2186. Assert.True (list.Count == 1);
  2187. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2188. maxWidth = text.RuneCount + 1;
  2189. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2190. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2191. Assert.True (list.Count == 1);
  2192. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2193. text = "A sentence has words.\r\nLine 2.";
  2194. maxWidth = 0;
  2195. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2196. Assert.True (list.Count == 1);
  2197. Assert.Equal (ustring.Empty, list [0]);
  2198. maxWidth = 1;
  2199. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2200. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2201. Assert.True (list.Count == 1);
  2202. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2203. maxWidth = 5;
  2204. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2205. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2206. Assert.True (list.Count == 1);
  2207. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2208. maxWidth = text.RuneCount - 1;
  2209. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth) + 1;
  2210. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2211. Assert.True (list.Count == 1);
  2212. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " ").ToString (), list [0].ToString ());
  2213. // no clip
  2214. maxWidth = text.RuneCount + 0;
  2215. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2216. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2217. Assert.True (list.Count == 1);
  2218. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  2219. maxWidth = text.RuneCount + 1;
  2220. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2221. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2222. Assert.True (list.Count == 1);
  2223. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  2224. }
  2225. [Fact]
  2226. public void Reformat_Wrap_Spaces_No_NewLines ()
  2227. {
  2228. var text = ustring.Empty;
  2229. var list = new List<ustring> ();
  2230. var maxWidth = 0;
  2231. var expectedClippedWidth = 0;
  2232. var wrap = true;
  2233. var preserveTrailingSpaces = true;
  2234. // Even # of spaces
  2235. // 0123456789
  2236. text = "012 456 89";
  2237. // See WordWrap BUGBUGs above.
  2238. maxWidth = 0;
  2239. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2240. Assert.True (list.Count == 1);
  2241. Assert.Equal (ustring.Empty, list [0]);
  2242. maxWidth = 1;
  2243. // remove 3 whitespace chars
  2244. expectedClippedWidth = text.RuneCount - text.Sum (r => r == ' ' ? 1 : 0);
  2245. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2246. Assert.Equal (expectedClippedWidth, list.Count);
  2247. Assert.Equal ("01245689", ustring.Join ("", list.ToArray ()));
  2248. maxWidth = 1;
  2249. // keep 3 whitespace chars
  2250. expectedClippedWidth = text.RuneCount;
  2251. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap, preserveTrailingSpaces);
  2252. Assert.Equal (expectedClippedWidth, list.Count);
  2253. Assert.Equal (text, ustring.Join ("", list.ToArray ()));
  2254. maxWidth = 5;
  2255. // remove 3 whitespace chars
  2256. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth - text.Sum (r => r == ' ' ? 1 : 0));
  2257. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2258. Assert.Equal (expectedClippedWidth, list.Count);
  2259. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2260. Assert.Equal ("01245689", ustring.Join ("", list.ToArray ()));
  2261. maxWidth = 5;
  2262. // keep 3 whitespace chars
  2263. expectedClippedWidth = Math.Min (text.RuneCount, (int)Math.Ceiling ((double)(text.RuneCount / 3F)));
  2264. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap, preserveTrailingSpaces);
  2265. Assert.Equal (expectedClippedWidth - (maxWidth - expectedClippedWidth), list.Count);
  2266. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2267. Assert.Equal ("012 456 89", ustring.Join ("", list.ToArray ()));
  2268. maxWidth = text.RuneCount - 1;
  2269. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2270. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2271. Assert.True (list.Count == 2);
  2272. Assert.Equal ("012 456", list [0]);
  2273. Assert.Equal ("89", list [1]);
  2274. // no clip
  2275. maxWidth = text.RuneCount + 0;
  2276. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2277. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2278. Assert.True (list.Count == 1);
  2279. Assert.Equal ("012 456 89", list [0]);
  2280. maxWidth = text.RuneCount + 1;
  2281. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2282. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2283. Assert.True (list.Count == 1);
  2284. Assert.Equal ("012 456 89", list [0]);
  2285. // Odd # of spaces
  2286. // 0123456789
  2287. text = "012 456 89 end";
  2288. maxWidth = text.RuneCount - 1;
  2289. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2290. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2291. Assert.True (list.Count == 2);
  2292. Assert.Equal ("012 456 89", list [0]);
  2293. Assert.Equal ("end", list [1]);
  2294. // no clip
  2295. maxWidth = text.RuneCount + 0;
  2296. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2297. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2298. Assert.True (list.Count == 1);
  2299. Assert.Equal ("012 456 89 end", list [0]);
  2300. maxWidth = text.RuneCount + 1;
  2301. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2302. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2303. Assert.True (list.Count == 1);
  2304. Assert.Equal ("012 456 89 end", list [0]);
  2305. }
  2306. [Fact]
  2307. public void Reformat_Unicode_Wrap_Spaces_No_NewLines ()
  2308. {
  2309. var text = ustring.Empty;
  2310. var list = new List<ustring> ();
  2311. var maxWidth = 0;
  2312. var expectedClippedWidth = 0;
  2313. var wrap = true;
  2314. // Unicode
  2315. // Even # of chars
  2316. // 0123456789
  2317. text = "\u2660пÑРвРÑ";
  2318. maxWidth = text.RuneCount - 1;
  2319. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2320. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2321. Assert.True (list.Count == 2);
  2322. Assert.Equal ("\u2660пÑРвÐ", list [0]);
  2323. Assert.Equal ("Ñ", list [1]);
  2324. // no clip
  2325. maxWidth = text.RuneCount + 0;
  2326. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2327. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2328. Assert.True (list.Count == 1);
  2329. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  2330. maxWidth = text.RuneCount + 1;
  2331. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2332. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2333. Assert.True (list.Count == 1);
  2334. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  2335. // Unicode
  2336. // Odd # of chars
  2337. // 0123456789
  2338. text = "\u2660 ÑРвРÑ";
  2339. maxWidth = text.RuneCount - 1;
  2340. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2341. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2342. Assert.True (list.Count == 2);
  2343. Assert.Equal ("\u2660 ÑРвÐ", list [0]);
  2344. Assert.Equal ("Ñ", list [1]);
  2345. // no clip
  2346. maxWidth = text.RuneCount + 0;
  2347. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2348. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2349. Assert.True (list.Count == 1);
  2350. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  2351. maxWidth = text.RuneCount + 1;
  2352. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2353. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2354. Assert.True (list.Count == 1);
  2355. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  2356. }
  2357. [Fact]
  2358. public void Reformat_Unicode_Wrap_Spaces_NewLines ()
  2359. {
  2360. var text = ustring.Empty;
  2361. var list = new List<ustring> ();
  2362. var maxWidth = 0;
  2363. var expectedClippedWidth = 0;
  2364. var wrap = true;
  2365. // Unicode
  2366. text = "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464";
  2367. maxWidth = text.RuneCount - 1;
  2368. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2369. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2370. Assert.Equal (2, list.Count);
  2371. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2372. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2373. // no clip
  2374. maxWidth = text.RuneCount + 0;
  2375. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2376. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2377. Assert.Equal (2, list.Count);
  2378. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2379. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2380. maxWidth = text.RuneCount + 1;
  2381. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2382. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2383. Assert.Equal (2, list.Count);
  2384. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2385. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2386. }
  2387. [Fact]
  2388. public void System_Rune_ColumnWidth ()
  2389. {
  2390. var c = new System.Rune ('a');
  2391. Assert.Equal (1, Rune.ColumnWidth (c));
  2392. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2393. Assert.Equal (1, ustring.Make (c).Length);
  2394. c = new System.Rune ('b');
  2395. Assert.Equal (1, Rune.ColumnWidth (c));
  2396. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2397. Assert.Equal (1, ustring.Make (c).Length);
  2398. c = new System.Rune (123);
  2399. Assert.Equal (1, Rune.ColumnWidth (c));
  2400. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2401. Assert.Equal (1, ustring.Make (c).Length);
  2402. c = new System.Rune ('\u1150');
  2403. Assert.Equal (2, Rune.ColumnWidth (c)); // 0x1150 ᅐ Unicode Technical Report #11
  2404. Assert.Equal (2, ustring.Make (c).ConsoleWidth);
  2405. Assert.Equal (3, ustring.Make (c).Length);
  2406. c = new System.Rune ('\u1161');
  2407. Assert.Equal (0, Rune.ColumnWidth (c)); // 0x1161 ᅡ column width of 0
  2408. Assert.Equal (0, ustring.Make (c).ConsoleWidth);
  2409. Assert.Equal (3, ustring.Make (c).Length);
  2410. c = new System.Rune (31);
  2411. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2412. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2413. Assert.Equal (1, ustring.Make (c).Length);
  2414. c = new System.Rune (127);
  2415. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2416. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2417. Assert.Equal (1, ustring.Make (c).Length);
  2418. }
  2419. [Fact]
  2420. public void System_Text_Rune ()
  2421. {
  2422. var c = new System.Text.Rune ('a');
  2423. Assert.Equal (1, c.Utf8SequenceLength);
  2424. c = new System.Text.Rune ('b');
  2425. Assert.Equal (1, c.Utf8SequenceLength);
  2426. c = new System.Text.Rune (123);
  2427. Assert.Equal (1, c.Utf8SequenceLength);
  2428. c = new System.Text.Rune ('\u1150');
  2429. Assert.Equal (3, c.Utf8SequenceLength); // 0x1150 ᅐ Unicode Technical Report #11
  2430. c = new System.Text.Rune ('\u1161');
  2431. Assert.Equal (3, c.Utf8SequenceLength); // 0x1161 ᅡ column width of 0
  2432. c = new System.Text.Rune (31);
  2433. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2434. c = new System.Text.Rune (127);
  2435. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2436. }
  2437. [Fact]
  2438. public void Format_WordWrap_preserveTrailingSpaces ()
  2439. {
  2440. ustring text = " A sentence has words. \n This is the second Line - 2. ";
  2441. // With preserveTrailingSpaces = false by default.
  2442. var list1 = TextFormatter.Format (text, 4, TextAlignment.Left, true);
  2443. ustring wrappedText1 = ustring.Empty;
  2444. Assert.Equal (" A", list1 [0].ToString ());
  2445. Assert.Equal ("sent", list1 [1].ToString ());
  2446. Assert.Equal ("ence", list1 [2].ToString ());
  2447. Assert.Equal ("has", list1 [3].ToString ());
  2448. Assert.Equal ("word", list1 [4].ToString ());
  2449. Assert.Equal ("s. ", list1 [5].ToString ());
  2450. Assert.Equal (" Thi", list1 [6].ToString ());
  2451. Assert.Equal ("s is", list1 [7].ToString ());
  2452. Assert.Equal ("the", list1 [8].ToString ());
  2453. Assert.Equal ("seco", list1 [9].ToString ());
  2454. Assert.Equal ("nd", list1 [10].ToString ());
  2455. Assert.Equal ("Line", list1 [11].ToString ());
  2456. Assert.Equal ("- 2.", list1 [^1].ToString ());
  2457. foreach (var txt in list1) {
  2458. wrappedText1 += txt;
  2459. }
  2460. Assert.Equal (" Asentencehaswords. This isthesecondLine- 2.", wrappedText1);
  2461. // With preserveTrailingSpaces = true.
  2462. var list2 = TextFormatter.Format (text, 4, TextAlignment.Left, true, true);
  2463. ustring wrappedText2 = ustring.Empty;
  2464. Assert.Equal (" A ", list2 [0].ToString ());
  2465. Assert.Equal ("sent", list2 [1].ToString ());
  2466. Assert.Equal ("ence", list2 [2].ToString ());
  2467. Assert.Equal (" has", list2 [3].ToString ());
  2468. Assert.Equal (" ", list2 [4].ToString ());
  2469. Assert.Equal ("word", list2 [5].ToString ());
  2470. Assert.Equal ("s. ", list2 [6].ToString ());
  2471. Assert.Equal (" ", list2 [7].ToString ());
  2472. Assert.Equal ("This", list2 [8].ToString ());
  2473. Assert.Equal (" is ", list2 [9].ToString ());
  2474. Assert.Equal ("the ", list2 [10].ToString ());
  2475. Assert.Equal ("seco", list2 [11].ToString ());
  2476. Assert.Equal ("nd ", list2 [12].ToString ());
  2477. Assert.Equal ("Line", list2 [13].ToString ());
  2478. Assert.Equal (" - ", list2 [14].ToString ());
  2479. Assert.Equal ("2. ", list2 [^1].ToString ());
  2480. foreach (var txt in list2) {
  2481. wrappedText2 += txt;
  2482. }
  2483. Assert.Equal (" A sentence has words. This is the second Line - 2. ", wrappedText2);
  2484. }
  2485. [Fact]
  2486. public void Format_Dont_Throw_ArgumentException_With_WordWrap_As_False_And_Keep_End_Spaces_As_True ()
  2487. {
  2488. var exception = Record.Exception (() => TextFormatter.Format ("Some text", 4, TextAlignment.Left, false, true));
  2489. Assert.Null (exception);
  2490. }
  2491. [Fact]
  2492. public void Draw_Horizontal_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2493. {
  2494. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2495. var top = Application.Top;
  2496. var view = new View ("view") { X = -2 };
  2497. top.Add (view);
  2498. Application.Iteration += () => {
  2499. Assert.Equal (-2, view.X);
  2500. Application.RequestStop ();
  2501. };
  2502. try {
  2503. Application.Run ();
  2504. } catch (IndexOutOfRangeException ex) {
  2505. // After the fix this exception will not be caught.
  2506. Assert.IsType<IndexOutOfRangeException> (ex);
  2507. }
  2508. // Shutdown must be called to safely clean up Application if Init has been called
  2509. Application.Shutdown ();
  2510. }
  2511. [Fact]
  2512. public void TestClipOrPad_ShortWord ()
  2513. {
  2514. // word is short but we want it to fill 6 so it should be padded
  2515. Assert.Equal ("fff ", TextFormatter.ClipOrPad ("fff", 6));
  2516. }
  2517. [Fact]
  2518. public void TestClipOrPad_LongWord ()
  2519. {
  2520. // word is long but we want it to fill 3 space only
  2521. Assert.Equal ("123", TextFormatter.ClipOrPad ("123456789", 3));
  2522. }
  2523. [Fact]
  2524. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2525. {
  2526. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2527. var top = Application.Top;
  2528. var view = new View ("view") {
  2529. Y = -2,
  2530. Height = 10,
  2531. TextDirection = TextDirection.TopBottom_LeftRight
  2532. };
  2533. top.Add (view);
  2534. Application.Iteration += () => {
  2535. Assert.Equal (-2, view.Y);
  2536. Application.RequestStop ();
  2537. };
  2538. try {
  2539. Application.Run ();
  2540. } catch (IndexOutOfRangeException ex) {
  2541. // After the fix this exception will not be caught.
  2542. Assert.IsType<IndexOutOfRangeException> (ex);
  2543. }
  2544. // Shutdown must be called to safely clean up Application if Init has been called
  2545. Application.Shutdown ();
  2546. }
  2547. [Fact]
  2548. public void Internal_Tests ()
  2549. {
  2550. var tf = new TextFormatter ();
  2551. Assert.Equal (Key.Null, tf.HotKey);
  2552. tf.HotKey = Key.CtrlMask | Key.Q;
  2553. Assert.Equal (Key.CtrlMask | Key.Q, tf.HotKey);
  2554. }
  2555. [Fact, AutoInitShutdown]
  2556. public void Draw_Horizontal_Simple_Runes ()
  2557. {
  2558. var label = new Label ("Demo Simple Rune");
  2559. Application.Top.Add (label);
  2560. Application.Begin (Application.Top);
  2561. Assert.True (label.AutoSize);
  2562. Assert.Equal (new Rect (0, 0, 16, 1), label.Frame);
  2563. var expected = @"
  2564. Demo Simple Rune
  2565. ";
  2566. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2567. Assert.Equal (new Rect (0, 0, 16, 1), pos);
  2568. }
  2569. [Fact, AutoInitShutdown]
  2570. public void Draw_Vertical_Simple_Runes ()
  2571. {
  2572. var label = new Label ("Demo Simple Rune") {
  2573. TextDirection = TextDirection.TopBottom_LeftRight
  2574. };
  2575. Application.Top.Add (label);
  2576. Application.Begin (Application.Top);
  2577. Assert.True (label.AutoSize);
  2578. Assert.Equal (new Rect (0, 0, 1, 16), label.Frame);
  2579. Assert.NotNull (label.Width);
  2580. Assert.NotNull (label.Height);
  2581. var expected = @"
  2582. D
  2583. e
  2584. m
  2585. o
  2586. S
  2587. i
  2588. m
  2589. p
  2590. l
  2591. e
  2592. R
  2593. u
  2594. n
  2595. e
  2596. ";
  2597. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2598. Assert.Equal (new Rect (0, 0, 1, 16), pos);
  2599. }
  2600. [Fact, AutoInitShutdown]
  2601. public void Draw_Horizontal_Wide_Runes ()
  2602. {
  2603. var label = new Label ("デモエムポンズ");
  2604. Application.Top.Add (label);
  2605. Application.Begin (Application.Top);
  2606. Assert.True (label.AutoSize);
  2607. Assert.Equal (new Rect (0, 0, 14, 1), label.Frame);
  2608. var expected = @"
  2609. デモエムポンズ
  2610. ";
  2611. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2612. Assert.Equal (new Rect (0, 0, 14, 1), pos);
  2613. }
  2614. [Fact, AutoInitShutdown]
  2615. public void Draw_Vertical_Wide_Runes ()
  2616. {
  2617. var label = new Label ("デモエムポンズ") {
  2618. TextDirection = TextDirection.TopBottom_LeftRight
  2619. };
  2620. Application.Top.Add (label);
  2621. Application.Begin (Application.Top);
  2622. Assert.True (label.AutoSize);
  2623. Assert.Equal (new Rect (0, 0, 2, 7), label.Frame);
  2624. var expected = @"
  2625. ";
  2626. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2627. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  2628. }
  2629. [Fact, AutoInitShutdown]
  2630. public void Draw_Vertical_Wide_Runes_With_ForceValidatePosDim ()
  2631. {
  2632. var label = new Label ("デモエムポンズ") {
  2633. Width = Dim.Fill (),
  2634. Height = Dim.Percent (50f),
  2635. TextDirection = TextDirection.TopBottom_LeftRight,
  2636. ForceValidatePosDim = true
  2637. };
  2638. Application.Top.Add (label);
  2639. Application.Begin (Application.Top);
  2640. Assert.True (label.AutoSize);
  2641. Assert.Equal (new Rect (0, 0, 80, 12), label.Frame);
  2642. var expected = @"
  2643. ";
  2644. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2645. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  2646. }
  2647. [Fact, AutoInitShutdown]
  2648. public void Draw_Horizontal_Simple_TextAlignments ()
  2649. {
  2650. var text = "Hello World";
  2651. var width = 20;
  2652. var lblLeft = new Label (text) { Width = width };
  2653. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2654. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2655. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2656. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2657. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2658. Application.Top.Add (frame);
  2659. Application.Begin (Application.Top);
  2660. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2661. Assert.True (lblLeft.AutoSize);
  2662. Assert.True (lblCenter.AutoSize);
  2663. Assert.True (lblRight.AutoSize);
  2664. Assert.True (lblJust.AutoSize);
  2665. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2666. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2667. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2668. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2669. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2670. var expected = @"
  2671. ┌────────────────────┐
  2672. │Hello World │
  2673. │ Hello World │
  2674. │ Hello World│
  2675. │Hello World│
  2676. └────────────────────┘
  2677. ";
  2678. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2679. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2680. }
  2681. [Fact, AutoInitShutdown]
  2682. public void Draw_Vertical_Simple_TextAlignments ()
  2683. {
  2684. var text = "Hello World";
  2685. var height = 20;
  2686. var lblLeft = new Label (text, direction: TextDirection.TopBottom_LeftRight) { Height = height };
  2687. var lblCenter = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 2, Height = height, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2688. var lblRight = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 4, Height = height, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2689. var lblJust = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 6, Height = height, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2690. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2691. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2692. Application.Top.Add (frame);
  2693. Application.Begin (Application.Top);
  2694. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  2695. Assert.True (lblLeft.AutoSize);
  2696. Assert.True (lblCenter.AutoSize);
  2697. Assert.True (lblRight.AutoSize);
  2698. Assert.True (lblJust.AutoSize);
  2699. Assert.Equal (new Rect (0, 0, 1, height), lblLeft.Frame);
  2700. Assert.Equal (new Rect (2, 0, 1, height), lblCenter.Frame);
  2701. Assert.Equal (new Rect (4, 0, 1, height), lblRight.Frame);
  2702. Assert.Equal (new Rect (6, 0, 1, height), lblJust.Frame);
  2703. Assert.Equal (new Rect (0, 0, 9, height + 2), frame.Frame);
  2704. var expected = @"
  2705. ┌───────┐
  2706. │H H│
  2707. │e e│
  2708. │l l│
  2709. │l l│
  2710. │o H o│
  2711. │ e │
  2712. │W l │
  2713. │o l │
  2714. │r o │
  2715. │l H │
  2716. │d W e │
  2717. │ o l │
  2718. │ r l │
  2719. │ l o │
  2720. │ d │
  2721. │ W W│
  2722. │ o o│
  2723. │ r r│
  2724. │ l l│
  2725. │ d d│
  2726. └───────┘
  2727. ";
  2728. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2729. Assert.Equal (new Rect (0, 0, 9, height + 2), pos);
  2730. }
  2731. [Fact, AutoInitShutdown]
  2732. public void Draw_Horizontal_Wide_TextAlignments ()
  2733. {
  2734. var text = "こんにちは 世界";
  2735. var width = 25;
  2736. var lblLeft = new Label (text) { Width = width };
  2737. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2738. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2739. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2740. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2741. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2742. Application.Top.Add (frame);
  2743. Application.Begin (Application.Top);
  2744. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2745. Assert.True (lblLeft.AutoSize);
  2746. Assert.True (lblCenter.AutoSize);
  2747. Assert.True (lblRight.AutoSize);
  2748. Assert.True (lblJust.AutoSize);
  2749. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2750. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2751. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2752. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2753. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2754. var expected = @"
  2755. ┌─────────────────────────┐
  2756. │こんにちは 世界 │
  2757. │ こんにちは 世界 │
  2758. │ こんにちは 世界│
  2759. │こんにちは 世界│
  2760. └─────────────────────────┘
  2761. ";
  2762. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2763. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2764. }
  2765. [Fact, AutoInitShutdown]
  2766. public void Draw_Vertical_Wide_TextAlignments ()
  2767. {
  2768. var text = "こんにちは 世界";
  2769. var height = 23;
  2770. var lblLeft = new Label (text) { Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight };
  2771. var lblCenter = new Label (text) { X = 3, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2772. var lblRight = new Label (text) { X = 6, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2773. var lblJust = new Label (text) { X = 9, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2774. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2775. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2776. Application.Top.Add (frame);
  2777. Application.Begin (Application.Top);
  2778. ((FakeDriver)Application.Driver).SetBufferSize (13, height + 2);
  2779. // All AutoSize are false because the Frame.Height != TextFormatter.Size.Height
  2780. Assert.True (lblLeft.AutoSize);
  2781. Assert.True (lblCenter.AutoSize);
  2782. Assert.True (lblRight.AutoSize);
  2783. Assert.True (lblJust.AutoSize);
  2784. Assert.Equal (new Rect (0, 0, 2, height), lblLeft.Frame);
  2785. Assert.Equal (new Rect (3, 0, 2, height), lblCenter.Frame);
  2786. Assert.Equal (new Rect (6, 0, 2, height), lblRight.Frame);
  2787. Assert.Equal (new Rect (9, 0, 2, height), lblJust.Frame);
  2788. Assert.Equal (new Rect (0, 0, 13, height + 2), frame.Frame);
  2789. var expected = @"
  2790. ┌───────────┐
  2791. │こ こ│
  2792. │ん ん│
  2793. │に に│
  2794. │ち ち│
  2795. │は は│
  2796. │ │
  2797. │世 │
  2798. │界 こ │
  2799. │ ん │
  2800. │ に │
  2801. │ ち │
  2802. │ は │
  2803. │ │
  2804. │ 世 │
  2805. │ 界 │
  2806. │ こ │
  2807. │ ん │
  2808. │ に │
  2809. │ ち │
  2810. │ は │
  2811. │ │
  2812. │ 世 世│
  2813. │ 界 界│
  2814. └───────────┘
  2815. ";
  2816. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2817. Assert.Equal (new Rect (0, 0, 13, height + 2), pos);
  2818. }
  2819. [Fact]
  2820. public void GetTextWidth_Simple_And_Wide_Runes ()
  2821. {
  2822. ustring text = "Hello World";
  2823. Assert.Equal (11, TextFormatter.GetTextWidth (text));
  2824. text = "こんにちは世界";
  2825. Assert.Equal (14, TextFormatter.GetTextWidth (text));
  2826. }
  2827. [Fact]
  2828. public void GetSumMaxCharWidth_Simple_And_Wide_Runes ()
  2829. {
  2830. ustring text = "Hello World";
  2831. Assert.Equal (11, TextFormatter.GetSumMaxCharWidth (text));
  2832. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2833. text = "こんにちは 世界";
  2834. Assert.Equal (15, TextFormatter.GetSumMaxCharWidth (text));
  2835. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2836. }
  2837. [Fact]
  2838. public void GetSumMaxCharWidth_List_Simple_And_Wide_Runes ()
  2839. {
  2840. List<ustring> text = new List<ustring> () { "Hello", "World" };
  2841. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text));
  2842. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2843. text = new List<ustring> () { "こんにちは", "世界" };
  2844. Assert.Equal (4, TextFormatter.GetSumMaxCharWidth (text));
  2845. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2846. }
  2847. [Fact]
  2848. public void GetMaxLengthForWidth_Simple_And_Wide_Runes ()
  2849. {
  2850. ustring text = "Hello World";
  2851. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (text, 6));
  2852. text = "こんにちは 世界";
  2853. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (text, 6));
  2854. }
  2855. [Fact]
  2856. public void GetMaxLengthForWidth_List_Simple_And_Wide_Runes ()
  2857. {
  2858. var runes = ustring.Make ("Hello World").ToRuneList ();
  2859. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2860. runes = ustring.Make ("こんにちは 世界").ToRuneList ();
  2861. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2862. runes = ustring.Make ("[ Say Hello 你 ]").ToRuneList ();
  2863. Assert.Equal (15, TextFormatter.GetMaxLengthForWidth (runes, 16));
  2864. }
  2865. [Fact]
  2866. public void Format_Truncate_Simple_And_Wide_Runes ()
  2867. {
  2868. var text = "Truncate";
  2869. var list = TextFormatter.Format (text, 3, false, false);
  2870. Assert.Equal ("Tru", list [^1].ToString ());
  2871. text = "デモエムポンズ";
  2872. list = TextFormatter.Format (text, 3, false, false);
  2873. Assert.Equal ("デ", list [^1].ToString ());
  2874. }
  2875. [Fact, AutoInitShutdown]
  2876. public void AutoSize_False_View_IsEmpty_False_Return_Null_Lines ()
  2877. {
  2878. var text = "Views";
  2879. var view = new View () {
  2880. Width = Dim.Fill () - text.Length,
  2881. Height = 1,
  2882. Text = text
  2883. };
  2884. var win = new Window ("Window") {
  2885. Width = Dim.Fill (),
  2886. Height = Dim.Fill ()
  2887. };
  2888. win.Add (view);
  2889. Application.Top.Add (win);
  2890. Application.Begin (Application.Top);
  2891. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  2892. Assert.Equal (5, text.Length);
  2893. Assert.False (view.AutoSize);
  2894. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  2895. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  2896. Assert.Equal (new List<ustring> () { "Vie" }, view.TextFormatter.Lines);
  2897. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  2898. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  2899. var expected = @"
  2900. ┌ Wind ──┐
  2901. │Vie │
  2902. │ │
  2903. └────────┘
  2904. ";
  2905. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2906. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2907. text = "0123456789";
  2908. Assert.Equal (10, text.Length);
  2909. view.Width = Dim.Fill () - text.Length;
  2910. Application.Refresh ();
  2911. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  2912. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  2913. Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines);
  2914. expected = @"
  2915. ┌ Wind ──┐
  2916. │ │
  2917. │ │
  2918. └────────┘
  2919. ";
  2920. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2921. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2922. }
  2923. [Fact, AutoInitShutdown]
  2924. public void AutoSize_False_View_IsEmpty_True_Minimum_Height ()
  2925. {
  2926. var text = "Views";
  2927. var view = new View () {
  2928. Width = Dim.Fill () - text.Length,
  2929. Text = text
  2930. };
  2931. var win = new Window ("Window") {
  2932. Width = Dim.Fill (),
  2933. Height = Dim.Fill ()
  2934. };
  2935. win.Add (view);
  2936. Application.Top.Add (win);
  2937. Application.Begin (Application.Top);
  2938. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  2939. Assert.Equal (5, text.Length);
  2940. Assert.False (view.AutoSize);
  2941. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  2942. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  2943. Assert.Single (view.TextFormatter.Lines);
  2944. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  2945. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  2946. var expected = @"
  2947. ┌ Wind ──┐
  2948. │Vie │
  2949. │ │
  2950. └────────┘
  2951. ";
  2952. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2953. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2954. text = "0123456789";
  2955. Assert.Equal (10, text.Length);
  2956. view.Width = Dim.Fill () - text.Length;
  2957. Application.Refresh ();
  2958. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  2959. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  2960. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  2961. Assert.Null (exception);
  2962. expected = @"
  2963. ┌ Wind ──┐
  2964. │ │
  2965. │ │
  2966. └────────┘
  2967. ";
  2968. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2969. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2970. }
  2971. [Fact, AutoInitShutdown]
  2972. public void AutoSize_True_Label_IsEmpty_False_Never_Return_Null_Lines ()
  2973. {
  2974. var text = "Label";
  2975. var label = new Label () {
  2976. Width = Dim.Fill () - text.Length,
  2977. Height = 1,
  2978. Text = text
  2979. };
  2980. var win = new Window ("Window") {
  2981. Width = Dim.Fill (),
  2982. Height = Dim.Fill ()
  2983. };
  2984. win.Add (label);
  2985. Application.Top.Add (win);
  2986. Application.Begin (Application.Top);
  2987. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  2988. Assert.Equal (5, text.Length);
  2989. Assert.True (label.AutoSize);
  2990. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  2991. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  2992. Assert.Equal (new List<ustring> () { "Label" }, label.TextFormatter.Lines);
  2993. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  2994. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  2995. var expected = @"
  2996. ┌ Wind ──┐
  2997. │Label │
  2998. │ │
  2999. └────────┘
  3000. ";
  3001. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3002. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3003. text = "0123456789";
  3004. Assert.Equal (10, text.Length);
  3005. label.Width = Dim.Fill () - text.Length;
  3006. Application.Refresh ();
  3007. Assert.True (label.AutoSize);
  3008. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3009. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3010. Assert.Single (label.TextFormatter.Lines);
  3011. expected = @"
  3012. ┌ Wind ──┐
  3013. │Label │
  3014. │ │
  3015. └────────┘
  3016. ";
  3017. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3018. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3019. }
  3020. [Fact, AutoInitShutdown]
  3021. public void AutoSize_False_Label_IsEmpty_True_Return_Null_Lines ()
  3022. {
  3023. var text = "Label";
  3024. var label = new Label () {
  3025. Width = Dim.Fill () - text.Length,
  3026. Height = 1,
  3027. Text = text,
  3028. AutoSize = false
  3029. };
  3030. var win = new Window ("Window") {
  3031. Width = Dim.Fill (),
  3032. Height = Dim.Fill ()
  3033. };
  3034. win.Add (label);
  3035. Application.Top.Add (win);
  3036. Application.Begin (Application.Top);
  3037. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3038. Assert.Equal (5, text.Length);
  3039. Assert.False (label.AutoSize);
  3040. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  3041. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  3042. Assert.Equal (new List<ustring> () { "Lab" }, label.TextFormatter.Lines);
  3043. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3044. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3045. var expected = @"
  3046. ┌ Wind ──┐
  3047. │Lab │
  3048. │ │
  3049. └────────┘
  3050. ";
  3051. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3052. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3053. text = "0123456789";
  3054. Assert.Equal (10, text.Length);
  3055. label.Width = Dim.Fill () - text.Length;
  3056. Application.Refresh ();
  3057. Assert.False (label.AutoSize);
  3058. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  3059. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  3060. Assert.Equal (new List<ustring> { ustring.Empty }, label.TextFormatter.Lines);
  3061. expected = @"
  3062. ┌ Wind ──┐
  3063. │ │
  3064. │ │
  3065. └────────┘
  3066. ";
  3067. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3068. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3069. }
  3070. [Fact, AutoInitShutdown]
  3071. public void AutoSize_True_Label_IsEmpty_False_Minimum_Height ()
  3072. {
  3073. var text = "Label";
  3074. var label = new Label () {
  3075. Width = Dim.Fill () - text.Length,
  3076. Text = text
  3077. };
  3078. var win = new Window ("Window") {
  3079. Width = Dim.Fill (),
  3080. Height = Dim.Fill ()
  3081. };
  3082. win.Add (label);
  3083. Application.Top.Add (win);
  3084. Application.Begin (Application.Top);
  3085. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3086. Assert.Equal (5, text.Length);
  3087. Assert.True (label.AutoSize);
  3088. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3089. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3090. Assert.Equal (new List<ustring> () { "Label" }, label.TextFormatter.Lines);
  3091. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3092. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3093. var expected = @"
  3094. ┌ Wind ──┐
  3095. │Label │
  3096. │ │
  3097. └────────┘
  3098. ";
  3099. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3100. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3101. text = "0123456789";
  3102. Assert.Equal (10, text.Length);
  3103. label.Width = Dim.Fill () - text.Length;
  3104. Application.Refresh ();
  3105. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3106. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3107. var exception = Record.Exception (() => Assert.Single (label.TextFormatter.Lines));
  3108. Assert.Null (exception);
  3109. expected = @"
  3110. ┌ Wind ──┐
  3111. │Label │
  3112. │ │
  3113. └────────┘
  3114. ";
  3115. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3116. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3117. }
  3118. [Fact, AutoInitShutdown]
  3119. public void AutoSize_False_Label_Height_Zero_Returns_Minimum_Height ()
  3120. {
  3121. var text = "Label";
  3122. var label = new Label () {
  3123. Width = Dim.Fill () - text.Length,
  3124. Text = text,
  3125. AutoSize = false
  3126. };
  3127. var win = new Window ("Window") {
  3128. Width = Dim.Fill (),
  3129. Height = Dim.Fill ()
  3130. };
  3131. win.Add (label);
  3132. Application.Top.Add (win);
  3133. Application.Begin (Application.Top);
  3134. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3135. Assert.Equal (5, text.Length);
  3136. Assert.False (label.AutoSize);
  3137. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  3138. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  3139. Assert.Single (label.TextFormatter.Lines);
  3140. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3141. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3142. var expected = @"
  3143. ┌ Wind ──┐
  3144. │Lab │
  3145. │ │
  3146. └────────┘
  3147. ";
  3148. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3149. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3150. text = "0123456789";
  3151. Assert.Equal (10, text.Length);
  3152. label.Width = Dim.Fill () - text.Length;
  3153. Application.Refresh ();
  3154. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  3155. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  3156. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, label.TextFormatter.Lines));
  3157. Assert.Null (exception);
  3158. expected = @"
  3159. ┌ Wind ──┐
  3160. │ │
  3161. │ │
  3162. └────────┘
  3163. ";
  3164. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3165. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3166. }
  3167. [Fact, AutoInitShutdown]
  3168. public void AutoSize_True_View_IsEmpty_False_Minimum_Width ()
  3169. {
  3170. var text = "Views";
  3171. var view = new View () {
  3172. TextDirection = TextDirection.TopBottom_LeftRight,
  3173. Height = Dim.Fill () - text.Length,
  3174. Text = text,
  3175. AutoSize = true
  3176. };
  3177. var win = new Window ("Window") {
  3178. Width = Dim.Fill (),
  3179. Height = Dim.Fill ()
  3180. };
  3181. win.Add (view);
  3182. Application.Top.Add (win);
  3183. Application.Begin (Application.Top);
  3184. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3185. Assert.Equal (5, text.Length);
  3186. Assert.True (view.AutoSize);
  3187. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  3188. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  3189. Assert.Equal (new List<ustring> () { "Views" }, view.TextFormatter.Lines);
  3190. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3191. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3192. var expected = @"
  3193. ┌──┐
  3194. │V │
  3195. │i │
  3196. │e │
  3197. │w │
  3198. │s │
  3199. │ │
  3200. │ │
  3201. │ │
  3202. └──┘
  3203. ";
  3204. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3205. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3206. text = "0123456789";
  3207. Assert.Equal (10, text.Length);
  3208. view.Height = Dim.Fill () - text.Length;
  3209. Application.Refresh ();
  3210. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  3211. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  3212. var exception = Record.Exception (() => Assert.Single (view.TextFormatter.Lines));
  3213. Assert.Null (exception);
  3214. expected = @"
  3215. ┌──┐
  3216. │V │
  3217. │i │
  3218. │e │
  3219. │w │
  3220. │s │
  3221. │ │
  3222. │ │
  3223. │ │
  3224. └──┘
  3225. ";
  3226. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3227. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3228. }
  3229. [Fact, AutoInitShutdown]
  3230. public void AutoSize_False_View_Width_Null_Returns_Host_Frame_Width ()
  3231. {
  3232. var text = "Views";
  3233. var view = new View () {
  3234. TextDirection = TextDirection.TopBottom_LeftRight,
  3235. Height = Dim.Fill () - text.Length,
  3236. Text = text
  3237. };
  3238. var win = new Window ("Window") {
  3239. Width = Dim.Fill (),
  3240. Height = Dim.Fill ()
  3241. };
  3242. win.Add (view);
  3243. Application.Top.Add (win);
  3244. Application.Begin (Application.Top);
  3245. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3246. Assert.Equal (5, text.Length);
  3247. Assert.False (view.AutoSize);
  3248. Assert.Equal (new Rect (0, 0, 1, 3), view.Frame);
  3249. Assert.Equal (new Size (1, 3), view.TextFormatter.Size);
  3250. Assert.Single (view.TextFormatter.Lines);
  3251. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3252. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3253. var expected = @"
  3254. ┌──┐
  3255. │V │
  3256. │i │
  3257. │e │
  3258. │ │
  3259. │ │
  3260. │ │
  3261. │ │
  3262. │ │
  3263. └──┘
  3264. ";
  3265. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3266. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3267. text = "0123456789";
  3268. Assert.Equal (10, text.Length);
  3269. view.Height = Dim.Fill () - text.Length;
  3270. Application.Refresh ();
  3271. Assert.Equal (new Rect (0, 0, 1, 0), view.Frame);
  3272. Assert.Equal (new Size (1, 0), view.TextFormatter.Size);
  3273. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  3274. Assert.Null (exception);
  3275. expected = @"
  3276. ┌──┐
  3277. │ │
  3278. │ │
  3279. │ │
  3280. │ │
  3281. │ │
  3282. │ │
  3283. │ │
  3284. │ │
  3285. └──┘
  3286. ";
  3287. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3288. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3289. }
  3290. [Fact, AutoInitShutdown]
  3291. public void AutoSize_True_View_IsEmpty_False_Minimum_Width_Wide_Rune ()
  3292. {
  3293. var text = "界View";
  3294. var view = new View () {
  3295. TextDirection = TextDirection.TopBottom_LeftRight,
  3296. Height = Dim.Fill () - text.Length,
  3297. Text = text,
  3298. AutoSize = true
  3299. };
  3300. var win = new Window ("Window") {
  3301. Width = Dim.Fill (),
  3302. Height = Dim.Fill ()
  3303. };
  3304. win.Add (view);
  3305. Application.Top.Add (win);
  3306. Application.Begin (Application.Top);
  3307. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3308. Assert.Equal (5, text.Length);
  3309. Assert.True (view.AutoSize);
  3310. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  3311. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  3312. Assert.Equal (new List<ustring> () { "界View" }, view.TextFormatter.Lines);
  3313. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3314. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3315. var expected = @"
  3316. ┌──┐
  3317. │界│
  3318. │V │
  3319. │i │
  3320. │e │
  3321. │w │
  3322. │ │
  3323. │ │
  3324. │ │
  3325. └──┘
  3326. ";
  3327. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3328. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3329. text = "0123456789";
  3330. Assert.Equal (10, text.Length);
  3331. view.Height = Dim.Fill () - text.Length;
  3332. Application.Refresh ();
  3333. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  3334. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  3335. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { "界View" }, view.TextFormatter.Lines));
  3336. Assert.Null (exception);
  3337. expected = @"
  3338. ┌──┐
  3339. │界│
  3340. │V │
  3341. │i │
  3342. │e │
  3343. │w │
  3344. │ │
  3345. │ │
  3346. │ │
  3347. └──┘
  3348. ";
  3349. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3350. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3351. }
  3352. [Fact, AutoInitShutdown]
  3353. public void AutoSize_False_View_Width_Zero_Returns_Minimum_Width_With_Wide_Rune ()
  3354. {
  3355. var text = "界View";
  3356. var view = new View () {
  3357. TextDirection = TextDirection.TopBottom_LeftRight,
  3358. Height = Dim.Fill () - text.Length,
  3359. Text = text
  3360. };
  3361. var win = new Window ("Window") {
  3362. Width = Dim.Fill (),
  3363. Height = Dim.Fill ()
  3364. };
  3365. win.Add (view);
  3366. Application.Top.Add (win);
  3367. Application.Begin (Application.Top);
  3368. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3369. Assert.Equal (5, text.Length);
  3370. Assert.False (view.AutoSize);
  3371. Assert.Equal (new Rect (0, 0, 2, 3), view.Frame);
  3372. Assert.Equal (new Size (2, 3), view.TextFormatter.Size);
  3373. Assert.Single (view.TextFormatter.Lines);
  3374. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3375. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3376. var expected = @"
  3377. ┌──┐
  3378. │界│
  3379. │V │
  3380. │i │
  3381. │ │
  3382. │ │
  3383. │ │
  3384. │ │
  3385. │ │
  3386. └──┘
  3387. ";
  3388. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3389. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3390. text = "0123456789";
  3391. Assert.Equal (10, text.Length);
  3392. view.Height = Dim.Fill () - text.Length;
  3393. Application.Refresh ();
  3394. Assert.Equal (new Rect (0, 0, 2, 0), view.Frame);
  3395. Assert.Equal (new Size (2, 0), view.TextFormatter.Size);
  3396. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  3397. Assert.Null (exception);
  3398. expected = @"
  3399. ┌──┐
  3400. │ │
  3401. │ │
  3402. │ │
  3403. │ │
  3404. │ │
  3405. │ │
  3406. │ │
  3407. │ │
  3408. └──┘
  3409. ";
  3410. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3411. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3412. }
  3413. }
  3414. }