TextFormatterTests.cs 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099
  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.Max (text.RuneCount, maxWidth);
  936. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  937. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  938. Assert.True (expectedClippedWidth <= maxWidth);
  939. Assert.Throws<ArgumentOutOfRangeException> (() => ustring.Make (text.ToRunes () [0..expectedClippedWidth]));
  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 = "012++456+89";
  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 = "012+++456++89";
  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 = "012++++456+++89";
  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 = "012+++++++++++++456++++++++++++89";
  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 = "012++456+89+end";
  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 = "012++456++89+end";
  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 = "012+++456++89++end";
  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 = "012+++456+++89++end";
  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 = "012++++++++456++++++++89+++++++end";
  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 = "012+++++++++456+++++++++89++++++++end";
  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 = "пÑÐ++вÐ+Ñ";
  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 = "пÑÐ+++вÐ++Ñ";
  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 = "пÑÐ++++вÐ+++Ñ";
  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 = "пÑÐ+++++++++++++вÐ++++++++++++Ñ";
  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 = "Ð++ÑÐ+вÐ+Ñ";
  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 = "Ð++ÑÐ++вÐ+Ñ";
  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 = "Ð+++ÑÐ++вÐ++Ñ";
  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 = "Ð+++ÑÐ+++вÐ++Ñ";
  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 = "Ð++++++++ÑÐ++++++++вÐ+++++++Ñ";
  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 = "Ð+++++++++ÑÐ+++++++++вÐ++++++++Ñ";
  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 ", wrappedLines [0].ToString ());
  1715. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1716. Assert.True (wrappedLines.Count == 2);
  1717. maxWidth = 8;
  1718. expectedClippedWidth = 8;
  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 ("sentence", wrappedLines [1].ToString ());
  1724. Assert.Equal (" has ", wrappedLines [2].ToString ());
  1725. Assert.Equal ("words.", wrappedLines [^1].ToString ());
  1726. Assert.True (wrappedLines.Count == 4);
  1727. maxWidth = 6;
  1728. expectedClippedWidth = 6;
  1729. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1730. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1731. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1732. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1733. Assert.Equal ("senten", wrappedLines [1].ToString ());
  1734. Assert.Equal ("ce ", wrappedLines [2].ToString ());
  1735. Assert.Equal ("has ", wrappedLines [3].ToString ());
  1736. Assert.Equal ("words.", wrappedLines [^1].ToString ());
  1737. Assert.True (wrappedLines.Count == 5);
  1738. maxWidth = 3;
  1739. expectedClippedWidth = 3;
  1740. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1741. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1742. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1743. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1744. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1745. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1746. Assert.Equal ("ce ", wrappedLines [3].ToString ());
  1747. Assert.Equal ("has", wrappedLines [4].ToString ());
  1748. Assert.Equal (" ", wrappedLines [5].ToString ());
  1749. Assert.Equal ("wor", wrappedLines [6].ToString ());
  1750. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  1751. Assert.True (wrappedLines.Count == 8);
  1752. maxWidth = 2;
  1753. expectedClippedWidth = 2;
  1754. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1755. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1756. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1757. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1758. Assert.Equal ("se", wrappedLines [1].ToString ());
  1759. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1760. Assert.Equal ("en", wrappedLines [3].ToString ());
  1761. Assert.Equal ("ce", wrappedLines [4].ToString ());
  1762. Assert.Equal (" ", wrappedLines [5].ToString ());
  1763. Assert.Equal ("ha", wrappedLines [6].ToString ());
  1764. Assert.Equal ("s ", wrappedLines [7].ToString ());
  1765. Assert.Equal ("wo", wrappedLines [8].ToString ());
  1766. Assert.Equal ("rd", wrappedLines [9].ToString ());
  1767. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1768. Assert.True (wrappedLines.Count == 11);
  1769. maxWidth = 1;
  1770. expectedClippedWidth = 1;
  1771. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1772. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1773. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1774. Assert.Equal ("A", wrappedLines [0].ToString ());
  1775. Assert.Equal (" ", wrappedLines [1].ToString ());
  1776. Assert.Equal ("s", wrappedLines [2].ToString ());
  1777. Assert.Equal ("e", wrappedLines [3].ToString ());
  1778. Assert.Equal ("n", wrappedLines [4].ToString ());
  1779. Assert.Equal ("t", wrappedLines [5].ToString ());
  1780. Assert.Equal ("e", wrappedLines [6].ToString ());
  1781. Assert.Equal ("n", wrappedLines [7].ToString ());
  1782. Assert.Equal ("c", wrappedLines [8].ToString ());
  1783. Assert.Equal ("e", wrappedLines [9].ToString ());
  1784. Assert.Equal (" ", wrappedLines [10].ToString ());
  1785. Assert.Equal (".", wrappedLines [^1].ToString ());
  1786. Assert.True (wrappedLines.Count == text.Length);
  1787. }
  1788. [Fact]
  1789. public void WordWrap_preserveTrailingSpaces_Wide_Runes ()
  1790. {
  1791. var text = ustring.Empty;
  1792. int maxWidth = 1;
  1793. int expectedClippedWidth = 1;
  1794. List<ustring> wrappedLines;
  1795. text = "文に は言葉 があり ます。";
  1796. maxWidth = 14;
  1797. expectedClippedWidth = 14;
  1798. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1799. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1800. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1801. Assert.Equal ("文に は言葉 ", wrappedLines [0].ToString ());
  1802. Assert.Equal ("があり ます。", wrappedLines [1].ToString ());
  1803. Assert.True (wrappedLines.Count == 2);
  1804. maxWidth = 3;
  1805. expectedClippedWidth = 3;
  1806. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1807. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1808. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1809. Assert.Equal ("文", wrappedLines [0].ToString ());
  1810. Assert.Equal ("に ", wrappedLines [1].ToString ());
  1811. Assert.Equal ("は", wrappedLines [2].ToString ());
  1812. Assert.Equal ("言", wrappedLines [3].ToString ());
  1813. Assert.Equal ("葉 ", wrappedLines [4].ToString ());
  1814. Assert.Equal ("が", wrappedLines [5].ToString ());
  1815. Assert.Equal ("あ", wrappedLines [6].ToString ());
  1816. Assert.Equal ("り ", wrappedLines [7].ToString ());
  1817. Assert.Equal ("ま", wrappedLines [8].ToString ());
  1818. Assert.Equal ("す", wrappedLines [9].ToString ());
  1819. Assert.Equal ("。", wrappedLines [^1].ToString ());
  1820. Assert.True (wrappedLines.Count == 11);
  1821. maxWidth = 2;
  1822. expectedClippedWidth = 2;
  1823. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1824. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1825. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1826. Assert.Equal ("文", wrappedLines [0].ToString ());
  1827. Assert.Equal ("に", wrappedLines [1].ToString ());
  1828. Assert.Equal (" ", wrappedLines [2].ToString ());
  1829. Assert.Equal ("は", wrappedLines [3].ToString ());
  1830. Assert.Equal ("言", wrappedLines [4].ToString ());
  1831. Assert.Equal ("葉", wrappedLines [5].ToString ());
  1832. Assert.Equal (" ", wrappedLines [6].ToString ());
  1833. Assert.Equal ("が", wrappedLines [7].ToString ());
  1834. Assert.Equal ("あ", wrappedLines [8].ToString ());
  1835. Assert.Equal ("り", wrappedLines [9].ToString ());
  1836. Assert.Equal (" ", wrappedLines [10].ToString ());
  1837. Assert.Equal ("ま", wrappedLines [11].ToString ());
  1838. Assert.Equal ("す", wrappedLines [12].ToString ());
  1839. Assert.Equal ("。", wrappedLines [^1].ToString ());
  1840. Assert.True (wrappedLines.Count == 14);
  1841. maxWidth = 1;
  1842. expectedClippedWidth = 0;
  1843. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1844. Assert.Empty (wrappedLines);
  1845. Assert.False (wrappedLines.Count == text.Length);
  1846. Assert.False (wrappedLines.Count == text.RuneCount);
  1847. Assert.False (wrappedLines.Count == text.ConsoleWidth);
  1848. Assert.Equal (25, text.ConsoleWidth);
  1849. Assert.Equal (25, TextFormatter.GetTextWidth (text));
  1850. }
  1851. [Fact, AutoInitShutdown]
  1852. public void WordWrap_preserveTrailingSpaces_Horizontal_With_Simple_Runes ()
  1853. {
  1854. var text = "A sentence has words.";
  1855. var width = 3;
  1856. var height = 8;
  1857. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1858. var breakLines = "";
  1859. foreach (var line in wrappedLines) {
  1860. breakLines += $"{line}{Environment.NewLine}";
  1861. }
  1862. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  1863. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1864. frame.Add (label);
  1865. Application.Top.Add (frame);
  1866. Application.Begin (Application.Top);
  1867. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1868. Assert.True (label.AutoSize);
  1869. Assert.Equal (new Rect (0, 0, width, height + 1), label.Frame);
  1870. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1871. var expected = @"
  1872. ┌───┐
  1873. │A │
  1874. │sen│
  1875. │ten│
  1876. │ce │
  1877. │has│
  1878. │ │
  1879. │wor│
  1880. │ds.│
  1881. └───┘
  1882. ";
  1883. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1884. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1885. }
  1886. [Fact, AutoInitShutdown]
  1887. public void WordWrap_preserveTrailingSpaces_Vertical_With_Simple_Runes ()
  1888. {
  1889. var text = "A sentence has words.";
  1890. var width = 8;
  1891. var height = 3;
  1892. var wrappedLines = TextFormatter.WordWrap (text, height, true);
  1893. var breakLines = "";
  1894. for (int i = 0; i < wrappedLines.Count; i++) {
  1895. breakLines += $"{wrappedLines [i]}{(i < wrappedLines.Count - 1 ? Environment.NewLine : string.Empty)}";
  1896. }
  1897. var label = new Label (breakLines) {
  1898. TextDirection = TextDirection.TopBottom_LeftRight,
  1899. Width = Dim.Fill (),
  1900. Height = Dim.Fill ()
  1901. };
  1902. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1903. frame.Add (label);
  1904. Application.Top.Add (frame);
  1905. Application.Begin (Application.Top);
  1906. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1907. Assert.True (label.AutoSize);
  1908. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1909. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1910. var expected = @"
  1911. ┌────────┐
  1912. │Astch wd│
  1913. │ eeea os│
  1914. │ nn s r.│
  1915. └────────┘
  1916. ";
  1917. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1918. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1919. }
  1920. [Fact, AutoInitShutdown]
  1921. public void WordWrap_preserveTrailingSpaces_Horizontal_With_Wide_Runes ()
  1922. {
  1923. var text = "文に は言葉 があり ます。";
  1924. var width = 6;
  1925. var height = 8;
  1926. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1927. var breakLines = "";
  1928. foreach (var line in wrappedLines) {
  1929. breakLines += $"{line}{Environment.NewLine}";
  1930. }
  1931. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  1932. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1933. frame.Add (label);
  1934. Application.Top.Add (frame);
  1935. Application.Begin (Application.Top);
  1936. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1937. Assert.True (label.AutoSize);
  1938. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1939. Assert.Equal (new Size (width, height), label.TextFormatter.Size);
  1940. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1941. var expected = @"
  1942. ┌──────┐
  1943. │文に │
  1944. │は言葉│
  1945. │ があ │
  1946. │り ま │
  1947. │す。 │
  1948. │ │
  1949. │ │
  1950. │ │
  1951. └──────┘
  1952. ";
  1953. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1954. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1955. }
  1956. [Fact, AutoInitShutdown]
  1957. public void WordWrap_preserveTrailingSpaces_Vertical_With_Wide_Runes ()
  1958. {
  1959. var text = "文に は言葉 があり ます。";
  1960. var width = 8;
  1961. var height = 4;
  1962. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1963. var breakLines = "";
  1964. for (int i = 0; i < wrappedLines.Count; i++) {
  1965. breakLines += $"{wrappedLines [i]}{(i < wrappedLines.Count - 1 ? Environment.NewLine : string.Empty)}";
  1966. }
  1967. var label = new Label (breakLines) {
  1968. TextDirection = TextDirection.TopBottom_LeftRight,
  1969. Width = Dim.Fill (),
  1970. Height = Dim.Fill ()
  1971. };
  1972. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1973. frame.Add (label);
  1974. Application.Top.Add (frame);
  1975. Application.Begin (Application.Top);
  1976. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1977. Assert.True (label.AutoSize);
  1978. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1979. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1980. var expected = @"
  1981. ┌────────┐
  1982. │文言あす│
  1983. │に葉り。│
  1984. │ │
  1985. │はがま │
  1986. └────────┘
  1987. ";
  1988. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1989. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1990. }
  1991. [Fact]
  1992. public void WordWrap_preserveTrailingSpaces_With_Tab ()
  1993. {
  1994. var text = ustring.Empty;
  1995. int maxWidth = 1;
  1996. int expectedClippedWidth = 1;
  1997. List<ustring> wrappedLines;
  1998. text = "A sentence\t\t\t has words.";
  1999. var tabWidth = 4;
  2000. maxWidth = 14;
  2001. expectedClippedWidth = 11;
  2002. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  2003. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  2004. Assert.Equal ("A sentence\t", wrappedLines [0].ToString ());
  2005. Assert.Equal ("\t\t has ", wrappedLines [1].ToString ());
  2006. Assert.Equal ("words.", wrappedLines [2].ToString ());
  2007. Assert.True (wrappedLines.Count == 3);
  2008. maxWidth = 8;
  2009. expectedClippedWidth = 8;
  2010. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  2011. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  2012. Assert.Equal ("A ", wrappedLines [0].ToString ());
  2013. Assert.Equal ("sentence", wrappedLines [1].ToString ());
  2014. Assert.Equal ("\t\t", wrappedLines [2].ToString ());
  2015. Assert.Equal ("\t ", wrappedLines [3].ToString ());
  2016. Assert.Equal ("has ", wrappedLines [4].ToString ());
  2017. Assert.Equal ("words.", wrappedLines [^1].ToString ());
  2018. Assert.True (wrappedLines.Count == 6);
  2019. maxWidth = 3;
  2020. expectedClippedWidth = 3;
  2021. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  2022. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  2023. Assert.Equal ("A ", wrappedLines [0].ToString ());
  2024. Assert.Equal ("sen", wrappedLines [1].ToString ());
  2025. Assert.Equal ("ten", wrappedLines [2].ToString ());
  2026. Assert.Equal ("ce", wrappedLines [3].ToString ());
  2027. Assert.Equal ("\t", wrappedLines [4].ToString ());
  2028. Assert.Equal ("\t", wrappedLines [5].ToString ());
  2029. Assert.Equal ("\t", wrappedLines [6].ToString ());
  2030. Assert.Equal (" ", wrappedLines [7].ToString ());
  2031. Assert.Equal ("has", wrappedLines [8].ToString ());
  2032. Assert.Equal (" ", wrappedLines [9].ToString ());
  2033. Assert.Equal ("wor", wrappedLines [10].ToString ());
  2034. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  2035. Assert.True (wrappedLines.Count == 12);
  2036. maxWidth = 2;
  2037. expectedClippedWidth = 2;
  2038. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  2039. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  2040. Assert.Equal ("A ", wrappedLines [0].ToString ());
  2041. Assert.Equal ("se", wrappedLines [1].ToString ());
  2042. Assert.Equal ("nt", wrappedLines [2].ToString ());
  2043. Assert.Equal ("en", wrappedLines [3].ToString ());
  2044. Assert.Equal ("ce", wrappedLines [4].ToString ());
  2045. Assert.Equal ("\t", wrappedLines [5].ToString ());
  2046. Assert.Equal ("\t", wrappedLines [6].ToString ());
  2047. Assert.Equal ("\t", wrappedLines [7].ToString ());
  2048. Assert.Equal (" ", wrappedLines [8].ToString ());
  2049. Assert.Equal ("ha", wrappedLines [9].ToString ());
  2050. Assert.Equal ("s ", wrappedLines [10].ToString ());
  2051. Assert.Equal ("wo", wrappedLines [11].ToString ());
  2052. Assert.Equal ("rd", wrappedLines [12].ToString ());
  2053. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  2054. Assert.True (wrappedLines.Count == 14);
  2055. maxWidth = 1;
  2056. expectedClippedWidth = 1;
  2057. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  2058. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  2059. Assert.Equal ("A", wrappedLines [0].ToString ());
  2060. Assert.Equal (" ", wrappedLines [1].ToString ());
  2061. Assert.Equal ("s", wrappedLines [2].ToString ());
  2062. Assert.Equal ("e", wrappedLines [3].ToString ());
  2063. Assert.Equal ("n", wrappedLines [4].ToString ());
  2064. Assert.Equal ("t", wrappedLines [5].ToString ());
  2065. Assert.Equal ("e", wrappedLines [6].ToString ());
  2066. Assert.Equal ("n", wrappedLines [7].ToString ());
  2067. Assert.Equal ("c", wrappedLines [8].ToString ());
  2068. Assert.Equal ("e", wrappedLines [9].ToString ());
  2069. Assert.Equal ("\t", wrappedLines [10].ToString ());
  2070. Assert.Equal ("\t", wrappedLines [11].ToString ());
  2071. Assert.Equal ("\t", wrappedLines [12].ToString ());
  2072. Assert.Equal (" ", wrappedLines [13].ToString ());
  2073. Assert.Equal (".", wrappedLines [^1].ToString ());
  2074. Assert.True (wrappedLines.Count == text.Length);
  2075. }
  2076. [Fact]
  2077. public void WordWrap_Unicode_Wide_Runes ()
  2078. {
  2079. ustring text = "これが最初の行です。 こんにちは世界。 これが2行目です。";
  2080. var width = text.RuneCount;
  2081. var wrappedLines = TextFormatter.WordWrap (text, width);
  2082. Assert.Equal (3, wrappedLines.Count);
  2083. Assert.Equal ("これが最初の行です。", wrappedLines [0].ToString ());
  2084. Assert.Equal ("こんにちは世界。", wrappedLines [1].ToString ());
  2085. Assert.Equal ("これが2行目です。", wrappedLines [^1].ToString ());
  2086. }
  2087. [Fact]
  2088. public void ReplaceHotKeyWithTag ()
  2089. {
  2090. var tf = new TextFormatter ();
  2091. ustring text = "test";
  2092. int hotPos = 0;
  2093. uint tag = tf.HotKeyTagMask | 't';
  2094. Assert.Equal (ustring.Make (new Rune [] { tag, 'e', 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  2095. tag = tf.HotKeyTagMask | 'e';
  2096. hotPos = 1;
  2097. Assert.Equal (ustring.Make (new Rune [] { 't', tag, 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  2098. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  2099. Assert.Equal ('e', (uint)(result.ToRunes () [1] & ~tf.HotKeyTagMask));
  2100. text = "Ok";
  2101. tag = 0x100000 | 'O';
  2102. hotPos = 0;
  2103. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  2104. Assert.Equal ('O', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  2105. text = "[◦ Ok ◦]";
  2106. text = ustring.Make (new Rune [] { '[', '◦', ' ', 'O', 'k', ' ', '◦', ']' });
  2107. var runes = text.ToRuneList ();
  2108. Assert.Equal (text.RuneCount, runes.Count);
  2109. Assert.Equal (text, ustring.Make (runes));
  2110. tag = tf.HotKeyTagMask | 'O';
  2111. hotPos = 3;
  2112. Assert.Equal (ustring.Make (new Rune [] { '[', '◦', ' ', tag, 'k', ' ', '◦', ']' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  2113. Assert.Equal ('O', (uint)(result.ToRunes () [3] & ~tf.HotKeyTagMask));
  2114. text = "^k";
  2115. tag = '^';
  2116. hotPos = 0;
  2117. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  2118. Assert.Equal ('^', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  2119. }
  2120. [Fact]
  2121. public void Reformat_Invalid ()
  2122. {
  2123. var text = ustring.Empty;
  2124. var list = new List<ustring> ();
  2125. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, -1, TextAlignment.Left, false));
  2126. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  2127. Assert.NotEmpty (list);
  2128. Assert.True (list.Count == 1);
  2129. Assert.Equal (ustring.Empty, list [0]);
  2130. text = null;
  2131. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  2132. Assert.NotEmpty (list);
  2133. Assert.True (list.Count == 1);
  2134. Assert.Equal (ustring.Empty, list [0]);
  2135. list = TextFormatter.Format (text, 0, TextAlignment.Left, true);
  2136. Assert.NotEmpty (list);
  2137. Assert.True (list.Count == 1);
  2138. Assert.Equal (ustring.Empty, list [0]);
  2139. }
  2140. [Fact]
  2141. public void Reformat_NoWordrap_SingleLine ()
  2142. {
  2143. var text = ustring.Empty;
  2144. var list = new List<ustring> ();
  2145. var maxWidth = 0;
  2146. var expectedClippedWidth = 0;
  2147. var wrap = false;
  2148. maxWidth = 0;
  2149. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2150. Assert.True (list.Count == 1);
  2151. maxWidth = 1;
  2152. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2153. Assert.True (list.Count == 1);
  2154. text = "A sentence has words.";
  2155. maxWidth = 0;
  2156. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2157. Assert.True (list.Count == 1);
  2158. Assert.Equal (ustring.Empty, list [0]);
  2159. maxWidth = 1;
  2160. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2161. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2162. Assert.True (list.Count == 1);
  2163. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2164. maxWidth = 5;
  2165. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2166. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2167. Assert.True (list.Count == 1);
  2168. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2169. maxWidth = text.RuneCount - 1;
  2170. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2171. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2172. Assert.True (list.Count == 1);
  2173. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2174. // no clip
  2175. maxWidth = text.RuneCount + 0;
  2176. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2177. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2178. Assert.True (list.Count == 1);
  2179. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2180. maxWidth = text.RuneCount + 1;
  2181. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2182. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2183. Assert.True (list.Count == 1);
  2184. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2185. }
  2186. [Fact]
  2187. public void Reformat_NoWordrap_NewLines ()
  2188. {
  2189. var text = ustring.Empty;
  2190. var list = new List<ustring> ();
  2191. var maxWidth = 0;
  2192. var expectedClippedWidth = 0;
  2193. var wrap = false;
  2194. text = "A sentence has words.\nLine 2.";
  2195. maxWidth = 0;
  2196. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2197. Assert.True (list.Count == 1);
  2198. Assert.Equal (ustring.Empty, list [0]);
  2199. maxWidth = 1;
  2200. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2201. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2202. Assert.True (list.Count == 1);
  2203. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2204. maxWidth = 5;
  2205. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2206. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2207. Assert.True (list.Count == 1);
  2208. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2209. maxWidth = text.RuneCount - 1;
  2210. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2211. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2212. Assert.True (list.Count == 1);
  2213. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2214. // no clip
  2215. maxWidth = text.RuneCount + 0;
  2216. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2217. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2218. Assert.True (list.Count == 1);
  2219. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2220. maxWidth = text.RuneCount + 1;
  2221. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2222. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2223. Assert.True (list.Count == 1);
  2224. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2225. text = "A sentence has words.\r\nLine 2.";
  2226. maxWidth = 0;
  2227. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2228. Assert.True (list.Count == 1);
  2229. Assert.Equal (ustring.Empty, list [0]);
  2230. maxWidth = 1;
  2231. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2232. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2233. Assert.True (list.Count == 1);
  2234. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2235. maxWidth = 5;
  2236. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2237. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2238. Assert.True (list.Count == 1);
  2239. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2240. maxWidth = text.RuneCount - 1;
  2241. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth) + 1;
  2242. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2243. Assert.True (list.Count == 1);
  2244. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " ").ToString (), list [0].ToString ());
  2245. // no clip
  2246. maxWidth = text.RuneCount + 0;
  2247. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2248. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2249. Assert.True (list.Count == 1);
  2250. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  2251. maxWidth = text.RuneCount + 1;
  2252. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2253. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2254. Assert.True (list.Count == 1);
  2255. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  2256. }
  2257. [Fact]
  2258. public void Reformat_Wrap_Spaces_No_NewLines ()
  2259. {
  2260. var text = ustring.Empty;
  2261. var list = new List<ustring> ();
  2262. var maxWidth = 0;
  2263. var expectedClippedWidth = 0;
  2264. var wrap = true;
  2265. var preserveTrailingSpaces = true;
  2266. // Even # of spaces
  2267. // 0123456789
  2268. text = "012 456 89";
  2269. // See WordWrap BUGBUGs above.
  2270. maxWidth = 0;
  2271. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2272. Assert.True (list.Count == 1);
  2273. Assert.Equal (ustring.Empty, list [0]);
  2274. maxWidth = 1;
  2275. // remove 3 whitespace chars
  2276. expectedClippedWidth = text.RuneCount - text.Sum (r => r == ' ' ? 1 : 0);
  2277. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2278. Assert.Equal (expectedClippedWidth, list.Count);
  2279. Assert.Equal ("01245689", ustring.Join ("", list.ToArray ()));
  2280. maxWidth = 1;
  2281. // keep 3 whitespace chars
  2282. expectedClippedWidth = text.RuneCount;
  2283. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap, preserveTrailingSpaces);
  2284. Assert.Equal (expectedClippedWidth, list.Count);
  2285. Assert.Equal (text, ustring.Join ("", list.ToArray ()));
  2286. maxWidth = 5;
  2287. // remove 3 whitespace chars
  2288. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth - text.Sum (r => r == ' ' ? 1 : 0));
  2289. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2290. Assert.Equal (expectedClippedWidth, list.Count);
  2291. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2292. Assert.Equal ("01245689", ustring.Join ("", list.ToArray ()));
  2293. maxWidth = 5;
  2294. // keep 3 whitespace chars
  2295. expectedClippedWidth = Math.Min (text.RuneCount, (int)Math.Ceiling ((double)(text.RuneCount / 3F)));
  2296. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap, preserveTrailingSpaces);
  2297. Assert.Equal (expectedClippedWidth - (maxWidth - expectedClippedWidth), list.Count);
  2298. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2299. Assert.Equal ("012 456 89", ustring.Join ("", list.ToArray ()));
  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 == 2);
  2304. Assert.Equal ("012 456", list [0]);
  2305. Assert.Equal ("89", list [1]);
  2306. // no clip
  2307. maxWidth = text.RuneCount + 0;
  2308. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2309. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2310. Assert.True (list.Count == 1);
  2311. Assert.Equal ("012 456 89", list [0]);
  2312. maxWidth = text.RuneCount + 1;
  2313. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2314. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2315. Assert.True (list.Count == 1);
  2316. Assert.Equal ("012 456 89", list [0]);
  2317. // Odd # of spaces
  2318. // 0123456789
  2319. text = "012 456 89 end";
  2320. maxWidth = text.RuneCount - 1;
  2321. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2322. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2323. Assert.True (list.Count == 2);
  2324. Assert.Equal ("012 456 89", list [0]);
  2325. Assert.Equal ("end", list [1]);
  2326. // no clip
  2327. maxWidth = text.RuneCount + 0;
  2328. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2329. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2330. Assert.True (list.Count == 1);
  2331. Assert.Equal ("012 456 89 end", list [0]);
  2332. maxWidth = text.RuneCount + 1;
  2333. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2334. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2335. Assert.True (list.Count == 1);
  2336. Assert.Equal ("012 456 89 end", list [0]);
  2337. }
  2338. [Fact]
  2339. public void Reformat_Unicode_Wrap_Spaces_No_NewLines ()
  2340. {
  2341. var text = ustring.Empty;
  2342. var list = new List<ustring> ();
  2343. var maxWidth = 0;
  2344. var expectedClippedWidth = 0;
  2345. var wrap = true;
  2346. // Unicode
  2347. // Even # of chars
  2348. // 0123456789
  2349. text = "\u2660пÑРвРÑ";
  2350. maxWidth = text.RuneCount - 1;
  2351. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2352. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2353. Assert.True (list.Count == 2);
  2354. Assert.Equal ("\u2660пÑРвÐ", list [0]);
  2355. Assert.Equal ("Ñ", list [1]);
  2356. // no clip
  2357. maxWidth = text.RuneCount + 0;
  2358. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2359. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2360. Assert.True (list.Count == 1);
  2361. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  2362. maxWidth = text.RuneCount + 1;
  2363. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2364. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2365. Assert.True (list.Count == 1);
  2366. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  2367. // Unicode
  2368. // Odd # of chars
  2369. // 0123456789
  2370. text = "\u2660 ÑРвРÑ";
  2371. maxWidth = text.RuneCount - 1;
  2372. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2373. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2374. Assert.True (list.Count == 2);
  2375. Assert.Equal ("\u2660 ÑРвÐ", list [0]);
  2376. Assert.Equal ("Ñ", list [1]);
  2377. // no clip
  2378. maxWidth = text.RuneCount + 0;
  2379. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2380. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2381. Assert.True (list.Count == 1);
  2382. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  2383. maxWidth = text.RuneCount + 1;
  2384. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2385. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2386. Assert.True (list.Count == 1);
  2387. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  2388. }
  2389. [Fact]
  2390. public void Reformat_Unicode_Wrap_Spaces_NewLines ()
  2391. {
  2392. var text = ustring.Empty;
  2393. var list = new List<ustring> ();
  2394. var maxWidth = 0;
  2395. var expectedClippedWidth = 0;
  2396. var wrap = true;
  2397. // Unicode
  2398. text = "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464";
  2399. maxWidth = text.RuneCount - 1;
  2400. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2401. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2402. Assert.Equal (2, list.Count);
  2403. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2404. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2405. // no clip
  2406. maxWidth = text.RuneCount + 0;
  2407. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2408. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2409. Assert.Equal (2, list.Count);
  2410. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2411. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2412. maxWidth = text.RuneCount + 1;
  2413. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2414. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2415. Assert.Equal (2, list.Count);
  2416. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2417. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2418. }
  2419. [Fact]
  2420. public void System_Rune_ColumnWidth ()
  2421. {
  2422. var c = new System.Rune ('a');
  2423. Assert.Equal (1, Rune.ColumnWidth (c));
  2424. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2425. Assert.Equal (1, ustring.Make (c).Length);
  2426. c = new System.Rune ('b');
  2427. Assert.Equal (1, Rune.ColumnWidth (c));
  2428. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2429. Assert.Equal (1, ustring.Make (c).Length);
  2430. c = new System.Rune (123);
  2431. Assert.Equal (1, Rune.ColumnWidth (c));
  2432. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2433. Assert.Equal (1, ustring.Make (c).Length);
  2434. c = new System.Rune ('\u1150');
  2435. Assert.Equal (2, Rune.ColumnWidth (c)); // 0x1150 ᅐ Unicode Technical Report #11
  2436. Assert.Equal (2, ustring.Make (c).ConsoleWidth);
  2437. Assert.Equal (3, ustring.Make (c).Length);
  2438. c = new System.Rune ('\u1161');
  2439. Assert.Equal (0, Rune.ColumnWidth (c)); // 0x1161 ᅡ column width of 0
  2440. Assert.Equal (0, ustring.Make (c).ConsoleWidth);
  2441. Assert.Equal (3, ustring.Make (c).Length);
  2442. c = new System.Rune (31);
  2443. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2444. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2445. Assert.Equal (1, ustring.Make (c).Length);
  2446. c = new System.Rune (127);
  2447. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2448. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2449. Assert.Equal (1, ustring.Make (c).Length);
  2450. }
  2451. [Fact]
  2452. public void System_Text_Rune ()
  2453. {
  2454. var c = new System.Text.Rune ('a');
  2455. Assert.Equal (1, c.Utf8SequenceLength);
  2456. c = new System.Text.Rune ('b');
  2457. Assert.Equal (1, c.Utf8SequenceLength);
  2458. c = new System.Text.Rune (123);
  2459. Assert.Equal (1, c.Utf8SequenceLength);
  2460. c = new System.Text.Rune ('\u1150');
  2461. Assert.Equal (3, c.Utf8SequenceLength); // 0x1150 ᅐ Unicode Technical Report #11
  2462. c = new System.Text.Rune ('\u1161');
  2463. Assert.Equal (3, c.Utf8SequenceLength); // 0x1161 ᅡ column width of 0
  2464. c = new System.Text.Rune (31);
  2465. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2466. c = new System.Text.Rune (127);
  2467. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2468. }
  2469. [Fact]
  2470. public void Format_WordWrap_preserveTrailingSpaces ()
  2471. {
  2472. ustring text = " A sentence has words. \n This is the second Line - 2. ";
  2473. // With preserveTrailingSpaces = false by default.
  2474. var list1 = TextFormatter.Format (text, 4, TextAlignment.Left, true);
  2475. ustring wrappedText1 = ustring.Empty;
  2476. Assert.Equal (" A", list1 [0].ToString ());
  2477. Assert.Equal ("sent", list1 [1].ToString ());
  2478. Assert.Equal ("ence", list1 [2].ToString ());
  2479. Assert.Equal ("has", list1 [3].ToString ());
  2480. Assert.Equal ("word", list1 [4].ToString ());
  2481. Assert.Equal ("s. ", list1 [5].ToString ());
  2482. Assert.Equal (" Thi", list1 [6].ToString ());
  2483. Assert.Equal ("s is", list1 [7].ToString ());
  2484. Assert.Equal ("the", list1 [8].ToString ());
  2485. Assert.Equal ("seco", list1 [9].ToString ());
  2486. Assert.Equal ("nd", list1 [10].ToString ());
  2487. Assert.Equal ("Line", list1 [11].ToString ());
  2488. Assert.Equal ("- 2.", list1 [^1].ToString ());
  2489. foreach (var txt in list1) {
  2490. wrappedText1 += txt;
  2491. }
  2492. Assert.Equal (" Asentencehaswords. This isthesecondLine- 2.", wrappedText1);
  2493. // With preserveTrailingSpaces = true.
  2494. var list2 = TextFormatter.Format (text, 4, TextAlignment.Left, true, true);
  2495. ustring wrappedText2 = ustring.Empty;
  2496. Assert.Equal (" A ", list2 [0].ToString ());
  2497. Assert.Equal ("sent", list2 [1].ToString ());
  2498. Assert.Equal ("ence", list2 [2].ToString ());
  2499. Assert.Equal (" ", list2 [3].ToString ());
  2500. Assert.Equal ("has ", list2 [4].ToString ());
  2501. Assert.Equal ("word", list2 [5].ToString ());
  2502. Assert.Equal ("s. ", list2 [6].ToString ());
  2503. Assert.Equal (" ", list2 [7].ToString ());
  2504. Assert.Equal ("This", list2 [8].ToString ());
  2505. Assert.Equal (" is ", list2 [9].ToString ());
  2506. Assert.Equal ("the ", list2 [10].ToString ());
  2507. Assert.Equal ("seco", list2 [11].ToString ());
  2508. Assert.Equal ("nd ", list2 [12].ToString ());
  2509. Assert.Equal ("Line", list2 [13].ToString ());
  2510. Assert.Equal (" - ", list2 [14].ToString ());
  2511. Assert.Equal ("2. ", list2 [^1].ToString ());
  2512. foreach (var txt in list2) {
  2513. wrappedText2 += txt;
  2514. }
  2515. Assert.Equal (" A sentence has words. This is the second Line - 2. ", wrappedText2);
  2516. }
  2517. [Fact]
  2518. public void Format_Dont_Throw_ArgumentException_With_WordWrap_As_False_And_Keep_End_Spaces_As_True ()
  2519. {
  2520. var exception = Record.Exception (() => TextFormatter.Format ("Some text", 4, TextAlignment.Left, false, true));
  2521. Assert.Null (exception);
  2522. }
  2523. [Fact, AutoInitShutdown]
  2524. public void Format_Justified_Always_Returns_Text_Width_Equal_To_Passed_Width_Horizontal ()
  2525. {
  2526. ustring text = "Hello world, how are you today? Pretty neat!";
  2527. Assert.Equal (44, text.RuneCount);
  2528. for (int i = 44; i < 80; i++) {
  2529. var fmtText = TextFormatter.Format (text, i, TextAlignment.Justified, false, true) [0];
  2530. Assert.Equal (i, fmtText.RuneCount);
  2531. var c = (char)fmtText [^1];
  2532. Assert.Equal ('!', c);
  2533. }
  2534. }
  2535. [Fact, AutoInitShutdown]
  2536. public void Format_Justified_Always_Returns_Text_Width_Equal_To_Passed_Width_Vertical ()
  2537. {
  2538. ustring text = "Hello world, how are you today? Pretty neat!";
  2539. Assert.Equal (44, text.RuneCount);
  2540. for (int i = 44; i < 80; i++) {
  2541. var fmtText = TextFormatter.Format (text, i, TextAlignment.Justified, false, true, 0, TextDirection.TopBottom_LeftRight) [0];
  2542. Assert.Equal (i, fmtText.RuneCount);
  2543. var c = (char)fmtText [^1];
  2544. Assert.Equal ('!', c);
  2545. }
  2546. }
  2547. [Fact]
  2548. public void Draw_Horizontal_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2549. {
  2550. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2551. var top = Application.Top;
  2552. var view = new View ("view") { X = -2 };
  2553. top.Add (view);
  2554. Application.Iteration += () => {
  2555. Assert.Equal (-2, view.X);
  2556. Application.RequestStop ();
  2557. };
  2558. try {
  2559. Application.Run ();
  2560. } catch (IndexOutOfRangeException ex) {
  2561. // After the fix this exception will not be caught.
  2562. Assert.IsType<IndexOutOfRangeException> (ex);
  2563. }
  2564. // Shutdown must be called to safely clean up Application if Init has been called
  2565. Application.Shutdown ();
  2566. }
  2567. [Fact]
  2568. public void TestClipOrPad_ShortWord ()
  2569. {
  2570. // word is short but we want it to fill 6 so it should be padded
  2571. Assert.Equal ("fff ", TextFormatter.ClipOrPad ("fff", 6));
  2572. }
  2573. [Fact]
  2574. public void TestClipOrPad_LongWord ()
  2575. {
  2576. // word is long but we want it to fill 3 space only
  2577. Assert.Equal ("123", TextFormatter.ClipOrPad ("123456789", 3));
  2578. }
  2579. [Fact]
  2580. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2581. {
  2582. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2583. var top = Application.Top;
  2584. var view = new View ("view") {
  2585. Y = -2,
  2586. Height = 10,
  2587. TextDirection = TextDirection.TopBottom_LeftRight
  2588. };
  2589. top.Add (view);
  2590. Application.Iteration += () => {
  2591. Assert.Equal (-2, view.Y);
  2592. Application.RequestStop ();
  2593. };
  2594. try {
  2595. Application.Run ();
  2596. } catch (IndexOutOfRangeException ex) {
  2597. // After the fix this exception will not be caught.
  2598. Assert.IsType<IndexOutOfRangeException> (ex);
  2599. }
  2600. // Shutdown must be called to safely clean up Application if Init has been called
  2601. Application.Shutdown ();
  2602. }
  2603. [Fact]
  2604. public void Internal_Tests ()
  2605. {
  2606. var tf = new TextFormatter ();
  2607. Assert.Equal (Key.Null, tf.HotKey);
  2608. tf.HotKey = Key.CtrlMask | Key.Q;
  2609. Assert.Equal (Key.CtrlMask | Key.Q, tf.HotKey);
  2610. }
  2611. [Fact, AutoInitShutdown]
  2612. public void Draw_Horizontal_Simple_Runes ()
  2613. {
  2614. var label = new Label ("Demo Simple Rune");
  2615. Application.Top.Add (label);
  2616. Application.Begin (Application.Top);
  2617. Assert.True (label.AutoSize);
  2618. Assert.Equal (new Rect (0, 0, 16, 1), label.Frame);
  2619. var expected = @"
  2620. Demo Simple Rune
  2621. ";
  2622. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2623. Assert.Equal (new Rect (0, 0, 16, 1), pos);
  2624. }
  2625. [Fact, AutoInitShutdown]
  2626. public void Draw_Vertical_Simple_Runes ()
  2627. {
  2628. var label = new Label ("Demo Simple Rune") {
  2629. TextDirection = TextDirection.TopBottom_LeftRight
  2630. };
  2631. Application.Top.Add (label);
  2632. Application.Begin (Application.Top);
  2633. Assert.True (label.AutoSize);
  2634. Assert.Equal (new Rect (0, 0, 1, 16), label.Frame);
  2635. Assert.NotNull (label.Width);
  2636. Assert.NotNull (label.Height);
  2637. var expected = @"
  2638. D
  2639. e
  2640. m
  2641. o
  2642. S
  2643. i
  2644. m
  2645. p
  2646. l
  2647. e
  2648. R
  2649. u
  2650. n
  2651. e
  2652. ";
  2653. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2654. Assert.Equal (new Rect (0, 0, 1, 16), pos);
  2655. }
  2656. [Fact, AutoInitShutdown]
  2657. public void Draw_Horizontal_Wide_Runes ()
  2658. {
  2659. var label = new Label ("デモエムポンズ");
  2660. Application.Top.Add (label);
  2661. Application.Begin (Application.Top);
  2662. Assert.True (label.AutoSize);
  2663. Assert.Equal (new Rect (0, 0, 14, 1), label.Frame);
  2664. var expected = @"
  2665. デモエムポンズ
  2666. ";
  2667. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2668. Assert.Equal (new Rect (0, 0, 14, 1), pos);
  2669. }
  2670. [Fact, AutoInitShutdown]
  2671. public void Draw_Vertical_Wide_Runes ()
  2672. {
  2673. var label = new Label ("デモエムポンズ") {
  2674. TextDirection = TextDirection.TopBottom_LeftRight
  2675. };
  2676. Application.Top.Add (label);
  2677. Application.Begin (Application.Top);
  2678. Assert.True (label.AutoSize);
  2679. Assert.Equal (new Rect (0, 0, 2, 7), label.Frame);
  2680. var expected = @"
  2681. ";
  2682. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2683. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  2684. }
  2685. [Fact, AutoInitShutdown]
  2686. public void Draw_Vertical_Wide_Runes_With_ForceValidatePosDim ()
  2687. {
  2688. var label = new Label ("デモエムポンズ") {
  2689. Width = Dim.Fill (),
  2690. Height = Dim.Percent (50f),
  2691. TextDirection = TextDirection.TopBottom_LeftRight,
  2692. ForceValidatePosDim = true
  2693. };
  2694. Application.Top.Add (label);
  2695. Application.Begin (Application.Top);
  2696. Assert.True (label.AutoSize);
  2697. Assert.Equal (new Rect (0, 0, 80, 12), label.Frame);
  2698. var expected = @"
  2699. ";
  2700. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2701. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  2702. }
  2703. [Fact, AutoInitShutdown]
  2704. public void Draw_Horizontal_Simple_TextAlignments ()
  2705. {
  2706. var text = "Hello World";
  2707. var width = 20;
  2708. var lblLeft = new Label (text) { Width = width };
  2709. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2710. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2711. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2712. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2713. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2714. Application.Top.Add (frame);
  2715. Application.Begin (Application.Top);
  2716. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2717. Assert.True (lblLeft.AutoSize);
  2718. Assert.True (lblCenter.AutoSize);
  2719. Assert.True (lblRight.AutoSize);
  2720. Assert.True (lblJust.AutoSize);
  2721. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2722. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2723. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2724. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2725. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2726. var expected = @"
  2727. ┌────────────────────┐
  2728. │Hello World │
  2729. │ Hello World │
  2730. │ Hello World│
  2731. │Hello World│
  2732. └────────────────────┘
  2733. ";
  2734. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2735. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2736. }
  2737. [Fact, AutoInitShutdown]
  2738. public void Draw_Vertical_Simple_TextAlignments ()
  2739. {
  2740. var text = "Hello World";
  2741. var height = 20;
  2742. var lblLeft = new Label (text, direction: TextDirection.TopBottom_LeftRight) { Height = height };
  2743. var lblCenter = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 2, Height = height, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2744. var lblRight = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 4, Height = height, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2745. var lblJust = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 6, Height = height, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2746. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2747. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2748. Application.Top.Add (frame);
  2749. Application.Begin (Application.Top);
  2750. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  2751. Assert.True (lblLeft.AutoSize);
  2752. Assert.True (lblCenter.AutoSize);
  2753. Assert.True (lblRight.AutoSize);
  2754. Assert.True (lblJust.AutoSize);
  2755. Assert.Equal (new Rect (0, 0, 1, height), lblLeft.Frame);
  2756. Assert.Equal (new Rect (2, 0, 1, height), lblCenter.Frame);
  2757. Assert.Equal (new Rect (4, 0, 1, height), lblRight.Frame);
  2758. Assert.Equal (new Rect (6, 0, 1, height), lblJust.Frame);
  2759. Assert.Equal (new Rect (0, 0, 9, height + 2), frame.Frame);
  2760. var expected = @"
  2761. ┌───────┐
  2762. │H H│
  2763. │e e│
  2764. │l l│
  2765. │l l│
  2766. │o H o│
  2767. │ e │
  2768. │W l │
  2769. │o l │
  2770. │r o │
  2771. │l H │
  2772. │d W e │
  2773. │ o l │
  2774. │ r l │
  2775. │ l o │
  2776. │ d │
  2777. │ W W│
  2778. │ o o│
  2779. │ r r│
  2780. │ l l│
  2781. │ d d│
  2782. └───────┘
  2783. ";
  2784. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2785. Assert.Equal (new Rect (0, 0, 9, height + 2), pos);
  2786. }
  2787. [Fact, AutoInitShutdown]
  2788. public void Draw_Horizontal_Wide_TextAlignments ()
  2789. {
  2790. var text = "こんにちは 世界";
  2791. var width = 25;
  2792. var lblLeft = new Label (text) { Width = width };
  2793. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2794. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2795. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2796. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2797. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2798. Application.Top.Add (frame);
  2799. Application.Begin (Application.Top);
  2800. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2801. Assert.True (lblLeft.AutoSize);
  2802. Assert.True (lblCenter.AutoSize);
  2803. Assert.True (lblRight.AutoSize);
  2804. Assert.True (lblJust.AutoSize);
  2805. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2806. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2807. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2808. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2809. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2810. var expected = @"
  2811. ┌─────────────────────────┐
  2812. │こんにちは 世界 │
  2813. │ こんにちは 世界 │
  2814. │ こんにちは 世界│
  2815. │こんにちは 世界│
  2816. └─────────────────────────┘
  2817. ";
  2818. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2819. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2820. }
  2821. [Fact, AutoInitShutdown]
  2822. public void Draw_Vertical_Wide_TextAlignments ()
  2823. {
  2824. var text = "こんにちは 世界";
  2825. var height = 23;
  2826. var lblLeft = new Label (text) { Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight };
  2827. var lblCenter = new Label (text) { X = 3, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2828. var lblRight = new Label (text) { X = 6, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2829. var lblJust = new Label (text) { X = 9, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2830. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2831. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2832. Application.Top.Add (frame);
  2833. Application.Begin (Application.Top);
  2834. ((FakeDriver)Application.Driver).SetBufferSize (13, height + 2);
  2835. // All AutoSize are false because the Frame.Height != TextFormatter.Size.Height
  2836. Assert.True (lblLeft.AutoSize);
  2837. Assert.True (lblCenter.AutoSize);
  2838. Assert.True (lblRight.AutoSize);
  2839. Assert.True (lblJust.AutoSize);
  2840. Assert.Equal (new Rect (0, 0, 2, height), lblLeft.Frame);
  2841. Assert.Equal (new Rect (3, 0, 2, height), lblCenter.Frame);
  2842. Assert.Equal (new Rect (6, 0, 2, height), lblRight.Frame);
  2843. Assert.Equal (new Rect (9, 0, 2, height), lblJust.Frame);
  2844. Assert.Equal (new Rect (0, 0, 13, height + 2), frame.Frame);
  2845. var expected = @"
  2846. ┌───────────┐
  2847. │こ こ│
  2848. │ん ん│
  2849. │に に│
  2850. │ち ち│
  2851. │は は│
  2852. │ │
  2853. │世 │
  2854. │界 こ │
  2855. │ ん │
  2856. │ に │
  2857. │ ち │
  2858. │ は │
  2859. │ │
  2860. │ 世 │
  2861. │ 界 │
  2862. │ こ │
  2863. │ ん │
  2864. │ に │
  2865. │ ち │
  2866. │ は │
  2867. │ │
  2868. │ 世 世│
  2869. │ 界 界│
  2870. └───────────┘
  2871. ";
  2872. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2873. Assert.Equal (new Rect (0, 0, 13, height + 2), pos);
  2874. }
  2875. [Fact]
  2876. public void GetTextWidth_Simple_And_Wide_Runes ()
  2877. {
  2878. ustring text = "Hello World";
  2879. Assert.Equal (11, TextFormatter.GetTextWidth (text));
  2880. text = "こんにちは世界";
  2881. Assert.Equal (14, TextFormatter.GetTextWidth (text));
  2882. }
  2883. [Fact]
  2884. public void GetSumMaxCharWidth_Simple_And_Wide_Runes ()
  2885. {
  2886. ustring text = "Hello World";
  2887. Assert.Equal (11, TextFormatter.GetSumMaxCharWidth (text));
  2888. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2889. text = "こんにちは 世界";
  2890. Assert.Equal (15, TextFormatter.GetSumMaxCharWidth (text));
  2891. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2892. }
  2893. [Fact]
  2894. public void GetSumMaxCharWidth_List_Simple_And_Wide_Runes ()
  2895. {
  2896. List<ustring> text = new List<ustring> () { "Hello", "World" };
  2897. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text));
  2898. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2899. text = new List<ustring> () { "こんにちは", "世界" };
  2900. Assert.Equal (4, TextFormatter.GetSumMaxCharWidth (text));
  2901. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2902. }
  2903. [Fact]
  2904. public void GetMaxLengthForWidth_Simple_And_Wide_Runes ()
  2905. {
  2906. ustring text = "Hello World";
  2907. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (text, 6));
  2908. text = "こんにちは 世界";
  2909. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (text, 6));
  2910. }
  2911. [Fact]
  2912. public void GetMaxLengthForWidth_List_Simple_And_Wide_Runes ()
  2913. {
  2914. var runes = ustring.Make ("Hello World").ToRuneList ();
  2915. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2916. runes = ustring.Make ("こんにちは 世界").ToRuneList ();
  2917. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2918. runes = ustring.Make ("[ Say Hello 你 ]").ToRuneList ();
  2919. Assert.Equal (15, TextFormatter.GetMaxLengthForWidth (runes, 16));
  2920. }
  2921. [Fact]
  2922. public void Format_Truncate_Simple_And_Wide_Runes ()
  2923. {
  2924. var text = "Truncate";
  2925. var list = TextFormatter.Format (text, 3, false, false);
  2926. Assert.Equal ("Tru", list [^1].ToString ());
  2927. text = "デモエムポンズ";
  2928. list = TextFormatter.Format (text, 3, false, false);
  2929. Assert.Equal ("デ", list [^1].ToString ());
  2930. }
  2931. [Fact, AutoInitShutdown]
  2932. public void AutoSize_False_View_IsEmpty_False_Return_Null_Lines ()
  2933. {
  2934. var text = "Views";
  2935. var view = new View () {
  2936. Width = Dim.Fill () - text.Length,
  2937. Height = 1,
  2938. Text = text
  2939. };
  2940. var win = new Window ("Window") {
  2941. Width = Dim.Fill (),
  2942. Height = Dim.Fill ()
  2943. };
  2944. win.Add (view);
  2945. Application.Top.Add (win);
  2946. Application.Begin (Application.Top);
  2947. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  2948. Assert.Equal (5, text.Length);
  2949. Assert.False (view.AutoSize);
  2950. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  2951. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  2952. Assert.Equal (new List<ustring> () { "Vie" }, view.TextFormatter.Lines);
  2953. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  2954. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  2955. var expected = @"
  2956. ┌ Wind ──┐
  2957. │Vie │
  2958. │ │
  2959. └────────┘
  2960. ";
  2961. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2962. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2963. text = "0123456789";
  2964. Assert.Equal (10, text.Length);
  2965. view.Width = Dim.Fill () - text.Length;
  2966. Application.Refresh ();
  2967. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  2968. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  2969. Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines);
  2970. expected = @"
  2971. ┌ Wind ──┐
  2972. │ │
  2973. │ │
  2974. └────────┘
  2975. ";
  2976. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2977. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2978. }
  2979. [Fact, AutoInitShutdown]
  2980. public void AutoSize_False_View_IsEmpty_True_Minimum_Height ()
  2981. {
  2982. var text = "Views";
  2983. var view = new View () {
  2984. Width = Dim.Fill () - text.Length,
  2985. Text = text
  2986. };
  2987. var win = new Window ("Window") {
  2988. Width = Dim.Fill (),
  2989. Height = Dim.Fill ()
  2990. };
  2991. win.Add (view);
  2992. Application.Top.Add (win);
  2993. Application.Begin (Application.Top);
  2994. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  2995. Assert.Equal (5, text.Length);
  2996. Assert.False (view.AutoSize);
  2997. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  2998. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  2999. Assert.Single (view.TextFormatter.Lines);
  3000. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3001. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3002. var expected = @"
  3003. ┌ Wind ──┐
  3004. │Vie │
  3005. │ │
  3006. └────────┘
  3007. ";
  3008. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3009. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3010. text = "0123456789";
  3011. Assert.Equal (10, text.Length);
  3012. view.Width = Dim.Fill () - text.Length;
  3013. Application.Refresh ();
  3014. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  3015. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  3016. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  3017. Assert.Null (exception);
  3018. expected = @"
  3019. ┌ Wind ──┐
  3020. │ │
  3021. │ │
  3022. └────────┘
  3023. ";
  3024. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3025. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3026. }
  3027. [Fact, AutoInitShutdown]
  3028. public void AutoSize_True_Label_IsEmpty_False_Never_Return_Null_Lines ()
  3029. {
  3030. var text = "Label";
  3031. var label = new Label () {
  3032. Width = Dim.Fill () - text.Length,
  3033. Height = 1,
  3034. Text = text
  3035. };
  3036. var win = new Window ("Window") {
  3037. Width = Dim.Fill (),
  3038. Height = Dim.Fill ()
  3039. };
  3040. win.Add (label);
  3041. Application.Top.Add (win);
  3042. Application.Begin (Application.Top);
  3043. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3044. Assert.Equal (5, text.Length);
  3045. Assert.True (label.AutoSize);
  3046. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3047. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3048. Assert.Equal (new List<ustring> () { "Label" }, label.TextFormatter.Lines);
  3049. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3050. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3051. var expected = @"
  3052. ┌ Wind ──┐
  3053. │Label │
  3054. │ │
  3055. └────────┘
  3056. ";
  3057. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3058. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3059. text = "0123456789";
  3060. Assert.Equal (10, text.Length);
  3061. label.Width = Dim.Fill () - text.Length;
  3062. Application.Refresh ();
  3063. Assert.True (label.AutoSize);
  3064. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3065. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3066. Assert.Single (label.TextFormatter.Lines);
  3067. expected = @"
  3068. ┌ Wind ──┐
  3069. │Label │
  3070. │ │
  3071. └────────┘
  3072. ";
  3073. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3074. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3075. }
  3076. [Fact, AutoInitShutdown]
  3077. public void AutoSize_False_Label_IsEmpty_True_Return_Null_Lines ()
  3078. {
  3079. var text = "Label";
  3080. var label = new Label () {
  3081. Width = Dim.Fill () - text.Length,
  3082. Height = 1,
  3083. Text = text,
  3084. AutoSize = false
  3085. };
  3086. var win = new Window ("Window") {
  3087. Width = Dim.Fill (),
  3088. Height = Dim.Fill ()
  3089. };
  3090. win.Add (label);
  3091. Application.Top.Add (win);
  3092. Application.Begin (Application.Top);
  3093. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3094. Assert.Equal (5, text.Length);
  3095. Assert.False (label.AutoSize);
  3096. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  3097. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  3098. Assert.Equal (new List<ustring> () { "Lab" }, label.TextFormatter.Lines);
  3099. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3100. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3101. var expected = @"
  3102. ┌ Wind ──┐
  3103. │Lab │
  3104. │ │
  3105. └────────┘
  3106. ";
  3107. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3108. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3109. text = "0123456789";
  3110. Assert.Equal (10, text.Length);
  3111. label.Width = Dim.Fill () - text.Length;
  3112. Application.Refresh ();
  3113. Assert.False (label.AutoSize);
  3114. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  3115. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  3116. Assert.Equal (new List<ustring> { ustring.Empty }, label.TextFormatter.Lines);
  3117. expected = @"
  3118. ┌ Wind ──┐
  3119. │ │
  3120. │ │
  3121. └────────┘
  3122. ";
  3123. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3124. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3125. }
  3126. [Fact, AutoInitShutdown]
  3127. public void AutoSize_True_Label_IsEmpty_False_Minimum_Height ()
  3128. {
  3129. var text = "Label";
  3130. var label = new Label () {
  3131. Width = Dim.Fill () - text.Length,
  3132. Text = text
  3133. };
  3134. var win = new Window ("Window") {
  3135. Width = Dim.Fill (),
  3136. Height = Dim.Fill ()
  3137. };
  3138. win.Add (label);
  3139. Application.Top.Add (win);
  3140. Application.Begin (Application.Top);
  3141. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3142. Assert.Equal (5, text.Length);
  3143. Assert.True (label.AutoSize);
  3144. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3145. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3146. Assert.Equal (new List<ustring> () { "Label" }, label.TextFormatter.Lines);
  3147. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3148. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3149. var expected = @"
  3150. ┌ Wind ──┐
  3151. │Label │
  3152. │ │
  3153. └────────┘
  3154. ";
  3155. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3156. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3157. text = "0123456789";
  3158. Assert.Equal (10, text.Length);
  3159. label.Width = Dim.Fill () - text.Length;
  3160. Application.Refresh ();
  3161. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3162. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3163. var exception = Record.Exception (() => Assert.Single (label.TextFormatter.Lines));
  3164. Assert.Null (exception);
  3165. expected = @"
  3166. ┌ Wind ──┐
  3167. │Label │
  3168. │ │
  3169. └────────┘
  3170. ";
  3171. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3172. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3173. }
  3174. [Fact, AutoInitShutdown]
  3175. public void AutoSize_False_Label_Height_Zero_Returns_Minimum_Height ()
  3176. {
  3177. var text = "Label";
  3178. var label = new Label () {
  3179. Width = Dim.Fill () - text.Length,
  3180. Text = text,
  3181. AutoSize = false
  3182. };
  3183. var win = new Window ("Window") {
  3184. Width = Dim.Fill (),
  3185. Height = Dim.Fill ()
  3186. };
  3187. win.Add (label);
  3188. Application.Top.Add (win);
  3189. Application.Begin (Application.Top);
  3190. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3191. Assert.Equal (5, text.Length);
  3192. Assert.False (label.AutoSize);
  3193. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  3194. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  3195. Assert.Single (label.TextFormatter.Lines);
  3196. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3197. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3198. var expected = @"
  3199. ┌ Wind ──┐
  3200. │Lab │
  3201. │ │
  3202. └────────┘
  3203. ";
  3204. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3205. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3206. text = "0123456789";
  3207. Assert.Equal (10, text.Length);
  3208. label.Width = Dim.Fill () - text.Length;
  3209. Application.Refresh ();
  3210. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  3211. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  3212. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, label.TextFormatter.Lines));
  3213. Assert.Null (exception);
  3214. expected = @"
  3215. ┌ Wind ──┐
  3216. │ │
  3217. │ │
  3218. └────────┘
  3219. ";
  3220. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3221. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3222. }
  3223. [Fact, AutoInitShutdown]
  3224. public void AutoSize_True_View_IsEmpty_False_Minimum_Width ()
  3225. {
  3226. var text = "Views";
  3227. var view = new View () {
  3228. TextDirection = TextDirection.TopBottom_LeftRight,
  3229. Height = Dim.Fill () - text.Length,
  3230. Text = text,
  3231. AutoSize = true
  3232. };
  3233. var win = new Window ("Window") {
  3234. Width = Dim.Fill (),
  3235. Height = Dim.Fill ()
  3236. };
  3237. win.Add (view);
  3238. Application.Top.Add (win);
  3239. Application.Begin (Application.Top);
  3240. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3241. Assert.Equal (5, text.Length);
  3242. Assert.True (view.AutoSize);
  3243. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  3244. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  3245. Assert.Equal (new List<ustring> () { "Views" }, view.TextFormatter.Lines);
  3246. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3247. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3248. var expected = @"
  3249. ┌──┐
  3250. │V │
  3251. │i │
  3252. │e │
  3253. │w │
  3254. │s │
  3255. │ │
  3256. │ │
  3257. │ │
  3258. └──┘
  3259. ";
  3260. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3261. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3262. text = "0123456789";
  3263. Assert.Equal (10, text.Length);
  3264. view.Height = Dim.Fill () - text.Length;
  3265. Application.Refresh ();
  3266. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  3267. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  3268. var exception = Record.Exception (() => Assert.Single (view.TextFormatter.Lines));
  3269. Assert.Null (exception);
  3270. expected = @"
  3271. ┌──┐
  3272. │V │
  3273. │i │
  3274. │e │
  3275. │w │
  3276. │s │
  3277. │ │
  3278. │ │
  3279. │ │
  3280. └──┘
  3281. ";
  3282. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3283. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3284. }
  3285. [Fact, AutoInitShutdown]
  3286. public void AutoSize_False_View_Width_Null_Returns_Host_Frame_Width ()
  3287. {
  3288. var text = "Views";
  3289. var view = new View () {
  3290. TextDirection = TextDirection.TopBottom_LeftRight,
  3291. Height = Dim.Fill () - text.Length,
  3292. Text = text
  3293. };
  3294. var win = new Window ("Window") {
  3295. Width = Dim.Fill (),
  3296. Height = Dim.Fill ()
  3297. };
  3298. win.Add (view);
  3299. Application.Top.Add (win);
  3300. Application.Begin (Application.Top);
  3301. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3302. Assert.Equal (5, text.Length);
  3303. Assert.False (view.AutoSize);
  3304. Assert.Equal (new Rect (0, 0, 1, 3), view.Frame);
  3305. Assert.Equal (new Size (1, 3), view.TextFormatter.Size);
  3306. Assert.Single (view.TextFormatter.Lines);
  3307. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3308. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3309. var expected = @"
  3310. ┌──┐
  3311. │V │
  3312. │i │
  3313. │e │
  3314. │ │
  3315. │ │
  3316. │ │
  3317. │ │
  3318. │ │
  3319. └──┘
  3320. ";
  3321. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3322. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3323. text = "0123456789";
  3324. Assert.Equal (10, text.Length);
  3325. view.Height = Dim.Fill () - text.Length;
  3326. Application.Refresh ();
  3327. Assert.Equal (new Rect (0, 0, 1, 0), view.Frame);
  3328. Assert.Equal (new Size (1, 0), view.TextFormatter.Size);
  3329. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  3330. Assert.Null (exception);
  3331. expected = @"
  3332. ┌──┐
  3333. │ │
  3334. │ │
  3335. │ │
  3336. │ │
  3337. │ │
  3338. │ │
  3339. │ │
  3340. │ │
  3341. └──┘
  3342. ";
  3343. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3344. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3345. }
  3346. [Fact, AutoInitShutdown]
  3347. public void AutoSize_True_View_IsEmpty_False_Minimum_Width_Wide_Rune ()
  3348. {
  3349. var text = "界View";
  3350. var view = new View () {
  3351. TextDirection = TextDirection.TopBottom_LeftRight,
  3352. Height = Dim.Fill () - text.Length,
  3353. Text = text,
  3354. AutoSize = true
  3355. };
  3356. var win = new Window ("Window") {
  3357. Width = Dim.Fill (),
  3358. Height = Dim.Fill ()
  3359. };
  3360. win.Add (view);
  3361. Application.Top.Add (win);
  3362. Application.Begin (Application.Top);
  3363. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3364. Assert.Equal (5, text.Length);
  3365. Assert.True (view.AutoSize);
  3366. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  3367. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  3368. Assert.Equal (new List<ustring> () { "界View" }, view.TextFormatter.Lines);
  3369. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3370. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3371. var expected = @"
  3372. ┌──┐
  3373. │界│
  3374. │V │
  3375. │i │
  3376. │e │
  3377. │w │
  3378. │ │
  3379. │ │
  3380. │ │
  3381. └──┘
  3382. ";
  3383. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3384. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3385. text = "0123456789";
  3386. Assert.Equal (10, text.Length);
  3387. view.Height = Dim.Fill () - text.Length;
  3388. Application.Refresh ();
  3389. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  3390. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  3391. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { "界View" }, view.TextFormatter.Lines));
  3392. Assert.Null (exception);
  3393. expected = @"
  3394. ┌──┐
  3395. │界│
  3396. │V │
  3397. │i │
  3398. │e │
  3399. │w │
  3400. │ │
  3401. │ │
  3402. │ │
  3403. └──┘
  3404. ";
  3405. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3406. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3407. }
  3408. [Fact, AutoInitShutdown]
  3409. public void AutoSize_False_View_Width_Zero_Returns_Minimum_Width_With_Wide_Rune ()
  3410. {
  3411. var text = "界View";
  3412. var view = new View () {
  3413. TextDirection = TextDirection.TopBottom_LeftRight,
  3414. Height = Dim.Fill () - text.Length,
  3415. Text = text
  3416. };
  3417. var win = new Window ("Window") {
  3418. Width = Dim.Fill (),
  3419. Height = Dim.Fill ()
  3420. };
  3421. win.Add (view);
  3422. Application.Top.Add (win);
  3423. Application.Begin (Application.Top);
  3424. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3425. Assert.Equal (5, text.Length);
  3426. Assert.False (view.AutoSize);
  3427. Assert.Equal (new Rect (0, 0, 2, 3), view.Frame);
  3428. Assert.Equal (new Size (2, 3), view.TextFormatter.Size);
  3429. Assert.Single (view.TextFormatter.Lines);
  3430. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3431. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3432. var expected = @"
  3433. ┌──┐
  3434. │界│
  3435. │V │
  3436. │i │
  3437. │ │
  3438. │ │
  3439. │ │
  3440. │ │
  3441. │ │
  3442. └──┘
  3443. ";
  3444. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3445. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3446. text = "0123456789";
  3447. Assert.Equal (10, text.Length);
  3448. view.Height = Dim.Fill () - text.Length;
  3449. Application.Refresh ();
  3450. Assert.Equal (new Rect (0, 0, 2, 0), view.Frame);
  3451. Assert.Equal (new Size (2, 0), view.TextFormatter.Size);
  3452. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  3453. Assert.Null (exception);
  3454. expected = @"
  3455. ┌──┐
  3456. │ │
  3457. │ │
  3458. │ │
  3459. │ │
  3460. │ │
  3461. │ │
  3462. │ │
  3463. │ │
  3464. └──┘
  3465. ";
  3466. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3467. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3468. }
  3469. [Fact]
  3470. public void Format_With_PreserveTrailingSpaces_And_Without_PreserveTrailingSpaces ()
  3471. {
  3472. var text = $"Line1{Environment.NewLine}Line2{Environment.NewLine}Line3{Environment.NewLine}";
  3473. var width = 60;
  3474. var preserveTrailingSpaces = false;
  3475. var formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
  3476. Assert.Equal ("Line1", formated [0]);
  3477. Assert.Equal ("Line2", formated [1]);
  3478. Assert.Equal ("Line3", formated [^1]);
  3479. preserveTrailingSpaces = true;
  3480. formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
  3481. Assert.Equal ("Line1", formated [0]);
  3482. Assert.Equal ("Line2", formated [1]);
  3483. Assert.Equal ("Line3", formated [^1]);
  3484. }
  3485. [Fact]
  3486. public void SplitNewLine_Ending_Without_NewLine_Probably_CRLF ()
  3487. {
  3488. var text = $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界";
  3489. var splited = TextFormatter.SplitNewLine (text);
  3490. Assert.Equal ("First Line 界", splited [0]);
  3491. Assert.Equal ("Second Line 界", splited [1]);
  3492. Assert.Equal ("Third Line 界", splited [^1]);
  3493. }
  3494. [Fact]
  3495. public void SplitNewLine_Ending_With_NewLine_Probably_CRLF ()
  3496. {
  3497. var text = $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界{Environment.NewLine}";
  3498. var splited = TextFormatter.SplitNewLine (text);
  3499. Assert.Equal ("First Line 界", splited [0]);
  3500. Assert.Equal ("Second Line 界", splited [1]);
  3501. Assert.Equal ("Third Line 界", splited [2]);
  3502. Assert.Equal ("", splited [^1]);
  3503. }
  3504. [Fact]
  3505. public void SplitNewLine_Ending_Without_NewLine_Only_LF ()
  3506. {
  3507. var text = $"First Line 界\nSecond Line 界\nThird Line 界";
  3508. var splited = TextFormatter.SplitNewLine (text);
  3509. Assert.Equal ("First Line 界", splited [0]);
  3510. Assert.Equal ("Second Line 界", splited [1]);
  3511. Assert.Equal ("Third Line 界", splited [^1]);
  3512. }
  3513. [Fact]
  3514. public void SplitNewLine_Ending_With_NewLine_Only_LF ()
  3515. {
  3516. var text = $"First Line 界\nSecond Line 界\nThird Line 界\n";
  3517. var splited = TextFormatter.SplitNewLine (text);
  3518. Assert.Equal ("First Line 界", splited [0]);
  3519. Assert.Equal ("Second Line 界", splited [1]);
  3520. Assert.Equal ("Third Line 界", splited [2]);
  3521. Assert.Equal ("", splited [^1]);
  3522. }
  3523. [Fact]
  3524. public void MaxWidthLine_With_And_Without_Newlines ()
  3525. {
  3526. var text = "Single Line 界";
  3527. Assert.Equal (14, TextFormatter.MaxWidthLine (text));
  3528. text = $"First Line 界\nSecond Line 界\nThird Line 界\n";
  3529. Assert.Equal (14, TextFormatter.MaxWidthLine (text));
  3530. }
  3531. }
  3532. }