TextFormatterTests.cs 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Terminal.Gui.Views;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.Core {
  11. public class TextFormatterTests {
  12. readonly ITestOutputHelper output;
  13. public TextFormatterTests (ITestOutputHelper output)
  14. {
  15. this.output = output;
  16. }
  17. [Fact]
  18. public void Basic_Usage ()
  19. {
  20. var testText = ustring.Make ("test");
  21. var expectedSize = new Size ();
  22. var testBounds = new Rect (0, 0, 100, 1);
  23. var tf = new TextFormatter ();
  24. tf.Text = testText;
  25. expectedSize = new Size (testText.Length, 1);
  26. Assert.Equal (testText, tf.Text);
  27. Assert.Equal (TextAlignment.Left, tf.Alignment);
  28. Assert.Equal (expectedSize, tf.Size);
  29. tf.Draw (testBounds, new Attribute (), new Attribute ());
  30. Assert.Equal (expectedSize, tf.Size);
  31. Assert.NotEmpty (tf.Lines);
  32. tf.Alignment = TextAlignment.Right;
  33. expectedSize = new Size (testText.Length, 1);
  34. Assert.Equal (testText, tf.Text);
  35. Assert.Equal (TextAlignment.Right, tf.Alignment);
  36. Assert.Equal (expectedSize, tf.Size);
  37. tf.Draw (testBounds, new Attribute (), new Attribute ());
  38. Assert.Equal (expectedSize, tf.Size);
  39. Assert.NotEmpty (tf.Lines);
  40. tf.Alignment = TextAlignment.Right;
  41. expectedSize = new Size (testText.Length * 2, 1);
  42. tf.Size = expectedSize;
  43. Assert.Equal (testText, tf.Text);
  44. Assert.Equal (TextAlignment.Right, tf.Alignment);
  45. Assert.Equal (expectedSize, tf.Size);
  46. tf.Draw (testBounds, new Attribute (), new Attribute ());
  47. Assert.Equal (expectedSize, tf.Size);
  48. Assert.NotEmpty (tf.Lines);
  49. tf.Alignment = TextAlignment.Centered;
  50. expectedSize = new Size (testText.Length * 2, 1);
  51. tf.Size = expectedSize;
  52. Assert.Equal (testText, tf.Text);
  53. Assert.Equal (TextAlignment.Centered, tf.Alignment);
  54. Assert.Equal (expectedSize, tf.Size);
  55. tf.Draw (testBounds, new Attribute (), new Attribute ());
  56. Assert.Equal (expectedSize, tf.Size);
  57. Assert.NotEmpty (tf.Lines);
  58. }
  59. [Fact]
  60. public void TestSize_TextChange ()
  61. {
  62. var tf = new TextFormatter () { Text = "你" };
  63. Assert.Equal (2, tf.Size.Width);
  64. tf.Text = "你你";
  65. Assert.Equal (4, tf.Size.Width);
  66. }
  67. [Fact]
  68. public void NeedsFormat_Sets ()
  69. {
  70. var testText = ustring.Make ("test");
  71. var testBounds = new Rect (0, 0, 100, 1);
  72. var tf = new TextFormatter ();
  73. tf.Text = "test";
  74. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  75. Assert.NotEmpty (tf.Lines);
  76. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  77. Assert.Equal (testText, tf.Text);
  78. tf.Draw (testBounds, new Attribute (), new Attribute ());
  79. Assert.False (tf.NeedsFormat);
  80. tf.Size = new Size (1, 1);
  81. Assert.True (tf.NeedsFormat);
  82. Assert.NotEmpty (tf.Lines);
  83. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  84. tf.Alignment = TextAlignment.Centered;
  85. Assert.True (tf.NeedsFormat);
  86. Assert.NotEmpty (tf.Lines);
  87. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  88. }
  89. [Fact]
  90. public void FindHotKey_Invalid_ReturnsFalse ()
  91. {
  92. var text = ustring.Empty;
  93. Rune hotKeySpecifier = '_';
  94. bool supportFirstUpperCase = false;
  95. int hotPos = 0;
  96. Key hotKey = Key.Unknown;
  97. bool result = false;
  98. text = null;
  99. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  100. Assert.False (result);
  101. Assert.Equal (-1, hotPos);
  102. Assert.Equal (Key.Unknown, hotKey);
  103. text = "";
  104. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  105. Assert.False (result);
  106. Assert.Equal (-1, hotPos);
  107. Assert.Equal (Key.Unknown, hotKey);
  108. text = "no hotkey";
  109. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  110. Assert.False (result);
  111. Assert.Equal (-1, hotPos);
  112. Assert.Equal (Key.Unknown, hotKey);
  113. text = "No hotkey, Upper Case";
  114. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  115. Assert.False (result);
  116. Assert.Equal (-1, hotPos);
  117. Assert.Equal (Key.Unknown, hotKey);
  118. text = "Non-english: Сохранить";
  119. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  120. Assert.False (result);
  121. Assert.Equal (-1, hotPos);
  122. Assert.Equal (Key.Unknown, hotKey);
  123. }
  124. [Fact]
  125. public void FindHotKey_AlphaUpperCase_Succeeds ()
  126. {
  127. var text = ustring.Empty;
  128. Rune hotKeySpecifier = '_';
  129. bool supportFirstUpperCase = false;
  130. int hotPos = 0;
  131. Key hotKey = Key.Unknown;
  132. bool result = false;
  133. text = "_K Before";
  134. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  135. Assert.True (result);
  136. Assert.Equal (0, hotPos);
  137. Assert.Equal ((Key)'K', hotKey);
  138. text = "a_K Second";
  139. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  140. Assert.True (result);
  141. Assert.Equal (1, hotPos);
  142. Assert.Equal ((Key)'K', hotKey);
  143. text = "Last _K";
  144. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  145. Assert.True (result);
  146. Assert.Equal (5, hotPos);
  147. Assert.Equal ((Key)'K', hotKey);
  148. text = "After K_";
  149. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  150. Assert.False (result);
  151. Assert.Equal (-1, hotPos);
  152. Assert.Equal (Key.Unknown, hotKey);
  153. text = "Multiple _K and _R";
  154. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  155. Assert.True (result);
  156. Assert.Equal (9, hotPos);
  157. Assert.Equal ((Key)'K', hotKey);
  158. // Cryllic K (К)
  159. text = "Non-english: _Кдать";
  160. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  161. Assert.True (result);
  162. Assert.Equal (13, hotPos);
  163. Assert.Equal ((Key)'К', hotKey);
  164. // Turn on FirstUpperCase and verify same results
  165. supportFirstUpperCase = true;
  166. text = "_K Before";
  167. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  168. Assert.True (result);
  169. Assert.Equal (0, hotPos);
  170. Assert.Equal ((Key)'K', hotKey);
  171. text = "a_K Second";
  172. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  173. Assert.True (result);
  174. Assert.Equal (1, hotPos);
  175. Assert.Equal ((Key)'K', hotKey);
  176. text = "Last _K";
  177. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  178. Assert.True (result);
  179. Assert.Equal (5, hotPos);
  180. Assert.Equal ((Key)'K', hotKey);
  181. text = "After K_";
  182. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  183. Assert.False (result);
  184. Assert.Equal (-1, hotPos);
  185. Assert.Equal (Key.Unknown, hotKey);
  186. text = "Multiple _K and _R";
  187. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  188. Assert.True (result);
  189. Assert.Equal (9, hotPos);
  190. Assert.Equal ((Key)'K', hotKey);
  191. // Cryllic K (К)
  192. text = "Non-english: _Кдать";
  193. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  194. Assert.True (result);
  195. Assert.Equal (13, hotPos);
  196. Assert.Equal ((Key)'К', hotKey);
  197. }
  198. [Fact]
  199. public void FindHotKey_AlphaLowerCase_Succeeds ()
  200. {
  201. var text = ustring.Empty;
  202. Rune hotKeySpecifier = '_';
  203. bool supportFirstUpperCase = false;
  204. int hotPos = 0;
  205. Key hotKey = Key.Unknown;
  206. bool result = false;
  207. // lower case should return uppercase Hotkey
  208. text = "_k Before";
  209. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  210. Assert.True (result);
  211. Assert.Equal (0, hotPos);
  212. Assert.Equal ((Key)'K', hotKey);
  213. text = "a_k Second";
  214. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  215. Assert.True (result);
  216. Assert.Equal (1, hotPos);
  217. Assert.Equal ((Key)'K', hotKey);
  218. text = "Last _k";
  219. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  220. Assert.True (result);
  221. Assert.Equal (5, hotPos);
  222. Assert.Equal ((Key)'K', hotKey);
  223. text = "After k_";
  224. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  225. Assert.False (result);
  226. Assert.Equal (-1, hotPos);
  227. Assert.Equal (Key.Unknown, hotKey);
  228. text = "Multiple _k and _R";
  229. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  230. Assert.True (result);
  231. Assert.Equal (9, hotPos);
  232. Assert.Equal ((Key)'K', hotKey);
  233. // Lower case Cryllic K (к)
  234. text = "Non-english: _кдать";
  235. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  236. Assert.True (result);
  237. Assert.Equal (13, hotPos);
  238. Assert.Equal ((Key)'К', hotKey);
  239. // Turn on FirstUpperCase and verify same results
  240. supportFirstUpperCase = true;
  241. text = "_k Before";
  242. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  243. Assert.True (result);
  244. Assert.Equal (0, hotPos);
  245. Assert.Equal ((Key)'K', hotKey);
  246. text = "a_k Second";
  247. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  248. Assert.True (result);
  249. Assert.Equal (1, hotPos);
  250. Assert.Equal ((Key)'K', hotKey);
  251. text = "Last _k";
  252. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  253. Assert.True (result);
  254. Assert.Equal (5, hotPos);
  255. Assert.Equal ((Key)'K', hotKey);
  256. text = "After k_";
  257. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  258. Assert.False (result);
  259. Assert.Equal (-1, hotPos);
  260. Assert.Equal (Key.Unknown, hotKey);
  261. text = "Multiple _k and _R";
  262. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  263. Assert.True (result);
  264. Assert.Equal (9, hotPos);
  265. Assert.Equal ((Key)'K', hotKey);
  266. // Lower case Cryllic K (к)
  267. text = "Non-english: _кдать";
  268. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  269. Assert.True (result);
  270. Assert.Equal (13, hotPos);
  271. Assert.Equal ((Key)'К', hotKey);
  272. }
  273. [Fact]
  274. public void FindHotKey_Numeric_Succeeds ()
  275. {
  276. var text = ustring.Empty;
  277. Rune hotKeySpecifier = '_';
  278. bool supportFirstUpperCase = false;
  279. int hotPos = 0;
  280. Key hotKey = Key.Unknown;
  281. bool result = false;
  282. // Digits
  283. text = "_1 Before";
  284. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  285. Assert.True (result);
  286. Assert.Equal (0, hotPos);
  287. Assert.Equal ((Key)'1', hotKey);
  288. text = "a_1 Second";
  289. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  290. Assert.True (result);
  291. Assert.Equal (1, hotPos);
  292. Assert.Equal ((Key)'1', hotKey);
  293. text = "Last _1";
  294. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  295. Assert.True (result);
  296. Assert.Equal (5, hotPos);
  297. Assert.Equal ((Key)'1', hotKey);
  298. text = "After 1_";
  299. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  300. Assert.False (result);
  301. Assert.Equal (-1, hotPos);
  302. Assert.Equal (Key.Unknown, hotKey);
  303. text = "Multiple _1 and _2";
  304. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  305. Assert.True (result);
  306. Assert.Equal (9, hotPos);
  307. Assert.Equal ((Key)'1', hotKey);
  308. // Turn on FirstUpperCase and verify same results
  309. supportFirstUpperCase = true;
  310. text = "_1 Before";
  311. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  312. Assert.True (result);
  313. Assert.Equal (0, hotPos);
  314. Assert.Equal ((Key)'1', hotKey);
  315. text = "a_1 Second";
  316. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  317. Assert.True (result);
  318. Assert.Equal (1, hotPos);
  319. Assert.Equal ((Key)'1', hotKey);
  320. text = "Last _1";
  321. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  322. Assert.True (result);
  323. Assert.Equal (5, hotPos);
  324. Assert.Equal ((Key)'1', hotKey);
  325. text = "After 1_";
  326. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  327. Assert.False (result);
  328. Assert.Equal (-1, hotPos);
  329. Assert.Equal (Key.Unknown, hotKey);
  330. text = "Multiple _1 and _2";
  331. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  332. Assert.True (result);
  333. Assert.Equal (9, hotPos);
  334. Assert.Equal ((Key)'1', hotKey);
  335. }
  336. [Fact]
  337. public void FindHotKey_Legacy_FirstUpperCase_Succeeds ()
  338. {
  339. bool supportFirstUpperCase = true;
  340. var text = ustring.Empty;
  341. Rune hotKeySpecifier = (Rune)0;
  342. int hotPos = 0;
  343. Key hotKey = Key.Unknown;
  344. bool result = false;
  345. text = "K Before";
  346. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  347. Assert.True (result);
  348. Assert.Equal (0, hotPos);
  349. Assert.Equal ((Key)'K', hotKey);
  350. text = "aK Second";
  351. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  352. Assert.True (result);
  353. Assert.Equal (1, hotPos);
  354. Assert.Equal ((Key)'K', hotKey);
  355. text = "last K";
  356. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  357. Assert.True (result);
  358. Assert.Equal (5, hotPos);
  359. Assert.Equal ((Key)'K', hotKey);
  360. text = "multiple K and R";
  361. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  362. Assert.True (result);
  363. Assert.Equal (9, hotPos);
  364. Assert.Equal ((Key)'K', hotKey);
  365. // Cryllic K (К)
  366. text = "non-english: Кдать";
  367. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  368. Assert.True (result);
  369. Assert.Equal (13, hotPos);
  370. Assert.Equal ((Key)'К', hotKey);
  371. }
  372. [Fact]
  373. public void FindHotKey_Legacy_FirstUpperCase_NotFound_Returns_False ()
  374. {
  375. bool supportFirstUpperCase = true;
  376. var text = ustring.Empty;
  377. Rune hotKeySpecifier = (Rune)0;
  378. int hotPos = 0;
  379. Key hotKey = Key.Unknown;
  380. bool result = false;
  381. text = "k before";
  382. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  383. Assert.False (result);
  384. Assert.Equal (-1, hotPos);
  385. Assert.Equal (Key.Unknown, hotKey);
  386. text = "ak second";
  387. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  388. Assert.False (result);
  389. Assert.Equal (-1, hotPos);
  390. Assert.Equal (Key.Unknown, hotKey);
  391. text = "last k";
  392. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  393. Assert.False (result);
  394. Assert.Equal (-1, hotPos);
  395. Assert.Equal (Key.Unknown, hotKey);
  396. text = "multiple k and r";
  397. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  398. Assert.False (result);
  399. Assert.Equal (-1, hotPos);
  400. Assert.Equal (Key.Unknown, hotKey);
  401. text = "12345";
  402. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  403. Assert.False (result);
  404. Assert.Equal (-1, hotPos);
  405. Assert.Equal (Key.Unknown, hotKey);
  406. // punctuation
  407. text = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
  408. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  409. Assert.False (result);
  410. Assert.Equal (-1, hotPos);
  411. Assert.Equal (Key.Unknown, hotKey);
  412. // ~IsLetterOrDigit + Unicode
  413. text = " ~  s  gui.cs   master ↑10";
  414. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  415. Assert.False (result);
  416. Assert.Equal (-1, hotPos);
  417. Assert.Equal (Key.Unknown, hotKey);
  418. // Lower case Cryllic K (к)
  419. text = "non-english: кдать";
  420. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  421. Assert.False (result);
  422. Assert.Equal (-1, hotPos);
  423. Assert.Equal (Key.Unknown, hotKey);
  424. }
  425. static ustring testHotKeyAtStart = "_K Before";
  426. static ustring testHotKeyAtSecondPos = "a_K Second";
  427. static ustring testHotKeyAtLastPos = "Last _K";
  428. static ustring testHotKeyAfterLastChar = "After K_";
  429. static ustring testMultiHotKeys = "Multiple _K and _R";
  430. static ustring testNonEnglish = "Non-english: _Кдать";
  431. [Fact]
  432. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal ()
  433. {
  434. Rune hotKeySpecifier = '_';
  435. Assert.Null (TextFormatter.RemoveHotKeySpecifier (null, 0, hotKeySpecifier));
  436. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 0, hotKeySpecifier));
  437. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", -1, hotKeySpecifier));
  438. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 100, hotKeySpecifier));
  439. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", -1, hotKeySpecifier));
  440. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", 100, hotKeySpecifier));
  441. }
  442. [Fact]
  443. public void RemoveHotKeySpecifier_Valid_ReturnsStripped ()
  444. {
  445. Rune hotKeySpecifier = '_';
  446. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("_K Before", 0, hotKeySpecifier));
  447. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("a_K Second", 1, hotKeySpecifier));
  448. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last _K", 5, hotKeySpecifier));
  449. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K_", 7, hotKeySpecifier));
  450. Assert.Equal ("Multiple K and _R", TextFormatter.RemoveHotKeySpecifier ("Multiple _K and _R", 9, hotKeySpecifier));
  451. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: _Кдать", 13, hotKeySpecifier));
  452. }
  453. [Fact]
  454. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal ()
  455. {
  456. Rune hotKeySpecifier = '_';
  457. Assert.Equal ("all lower case", TextFormatter.RemoveHotKeySpecifier ("all lower case", 0, hotKeySpecifier));
  458. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("K Before", 0, hotKeySpecifier));
  459. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("aK Second", 1, hotKeySpecifier));
  460. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last K", 5, hotKeySpecifier));
  461. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K", 7, hotKeySpecifier));
  462. Assert.Equal ("Multiple K and R", TextFormatter.RemoveHotKeySpecifier ("Multiple K and R", 9, hotKeySpecifier));
  463. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: Кдать", 13, hotKeySpecifier));
  464. }
  465. [Fact]
  466. public void CalcRect_Invalid_Returns_Empty ()
  467. {
  468. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, null));
  469. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, ""));
  470. Assert.Equal (new Rect (new Point (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, ""));
  471. Assert.Equal (new Rect (new Point (-1, -2), Size.Empty), TextFormatter.CalcRect (-1, -2, ""));
  472. }
  473. [Fact]
  474. public void CalcRect_SingleLine_Returns_1High ()
  475. {
  476. var text = ustring.Empty;
  477. text = "test";
  478. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  479. Assert.Equal (new Rect (0, 0, text.ConsoleWidth, 1), TextFormatter.CalcRect (0, 0, text));
  480. text = " ~  s  gui.cs   master ↑10";
  481. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  482. Assert.Equal (new Rect (0, 0, text.ConsoleWidth, 1), TextFormatter.CalcRect (0, 0, text));
  483. }
  484. [Fact]
  485. public void CalcRect_MultiLine_Returns_nHigh ()
  486. {
  487. var text = ustring.Empty;
  488. var lines = 0;
  489. text = "line1\nline2";
  490. lines = 2;
  491. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  492. text = "\nline2";
  493. lines = 2;
  494. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  495. text = "\n\n";
  496. lines = 3;
  497. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  498. text = "\n\n\n";
  499. lines = 4;
  500. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  501. text = "line1\nline2\nline3long!";
  502. lines = 3;
  503. Assert.Equal (new Rect (0, 0, 10, lines), TextFormatter.CalcRect (0, 0, text));
  504. text = "line1\nline2\n\n";
  505. lines = 4;
  506. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  507. text = "line1\r\nline2";
  508. lines = 2;
  509. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  510. text = " ~  s  gui.cs   master ↑10\n";
  511. lines = 2;
  512. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  513. Assert.Equal (new Rect (0, 0, text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 0)), lines), TextFormatter.CalcRect (0, 0, text));
  514. text = "\n ~  s  gui.cs   master ↑10";
  515. lines = 2;
  516. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  517. Assert.Equal (new Rect (0, 0, text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 0)), lines), TextFormatter.CalcRect (0, 0, text));
  518. text = " ~  s  gui.cs   master\n↑10";
  519. lines = 2;
  520. Assert.Equal (new Rect (0, 0, ustring.Make (" ~  s  gui.cs   master\n").RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  521. Assert.Equal (new Rect (0, 0, ustring.Make (" ~  s  gui.cs   master\n").ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 0)), lines), TextFormatter.CalcRect (0, 0, text));
  522. }
  523. [Fact]
  524. public void ClipAndJustify_Invalid_Returns_Original ()
  525. {
  526. var text = ustring.Empty;
  527. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  528. text = null;
  529. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  530. text = "test";
  531. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.ClipAndJustify (text, -1, TextAlignment.Left));
  532. }
  533. [Fact]
  534. public void ClipAndJustify_Valid_Left ()
  535. {
  536. var align = TextAlignment.Left;
  537. var text = ustring.Empty;
  538. var justifiedText = ustring.Empty;
  539. int maxWidth = 0;
  540. int expectedClippedWidth = 0;
  541. text = "test";
  542. maxWidth = 0;
  543. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  544. text = "test";
  545. maxWidth = 2;
  546. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  547. text = "test";
  548. maxWidth = int.MaxValue;
  549. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  550. Assert.True (justifiedText.RuneCount <= maxWidth);
  551. Assert.True (justifiedText.ConsoleWidth <= maxWidth);
  552. text = "A sentence has words.";
  553. // should fit
  554. maxWidth = text.RuneCount + 1;
  555. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  556. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  557. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  558. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  559. Assert.True (expectedClippedWidth <= maxWidth);
  560. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  561. // Should fit.
  562. maxWidth = text.RuneCount + 0;
  563. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  564. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  565. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  566. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  567. Assert.True (expectedClippedWidth <= maxWidth);
  568. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  569. // Should fit.
  570. maxWidth = int.MaxValue;
  571. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  572. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  573. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  574. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  575. Assert.True (expectedClippedWidth <= maxWidth);
  576. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  577. // Should not fit
  578. maxWidth = text.RuneCount - 1;
  579. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  580. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  581. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  582. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  583. Assert.True (expectedClippedWidth <= maxWidth);
  584. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  585. // Should not fit
  586. maxWidth = 10;
  587. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  588. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  589. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  590. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  591. Assert.True (expectedClippedWidth <= maxWidth);
  592. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  593. text = "A\tsentence\thas\twords.";
  594. maxWidth = int.MaxValue;
  595. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  596. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  597. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  598. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  599. Assert.True (expectedClippedWidth <= maxWidth);
  600. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  601. text = "A\tsentence\thas\twords.";
  602. maxWidth = 10;
  603. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  604. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  605. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  606. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  607. Assert.True (expectedClippedWidth <= maxWidth);
  608. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  609. text = "line1\nline2\nline3long!";
  610. maxWidth = int.MaxValue;
  611. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  612. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  613. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  614. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  615. Assert.True (expectedClippedWidth <= maxWidth);
  616. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  617. text = "line1\nline2\nline3long!";
  618. maxWidth = 10;
  619. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  620. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  621. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  622. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  623. Assert.True (expectedClippedWidth <= maxWidth);
  624. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  625. // Unicode
  626. text = " ~  s  gui.cs   master ↑10";
  627. maxWidth = 10;
  628. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  629. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  630. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  631. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  632. Assert.True (expectedClippedWidth <= maxWidth);
  633. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  634. // should fit
  635. text = "Ð ÑÐ";
  636. maxWidth = text.RuneCount + 1;
  637. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  638. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  639. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  640. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  641. Assert.True (expectedClippedWidth <= maxWidth);
  642. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  643. // Should fit.
  644. maxWidth = text.RuneCount + 0;
  645. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  646. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  647. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  648. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  649. Assert.True (expectedClippedWidth <= maxWidth);
  650. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  651. // Should not fit
  652. maxWidth = text.RuneCount - 1;
  653. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  654. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  655. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  656. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  657. Assert.True (expectedClippedWidth <= maxWidth);
  658. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  659. }
  660. [Fact]
  661. public void ClipAndJustify_Valid_Right ()
  662. {
  663. var align = TextAlignment.Right;
  664. var text = ustring.Empty;
  665. var justifiedText = ustring.Empty;
  666. int maxWidth = 0;
  667. int expectedClippedWidth = 0;
  668. text = "test";
  669. maxWidth = 0;
  670. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  671. text = "test";
  672. maxWidth = 2;
  673. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  674. text = "test";
  675. maxWidth = int.MaxValue;
  676. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  677. Assert.True (justifiedText.RuneCount <= maxWidth);
  678. Assert.True (justifiedText.ConsoleWidth <= maxWidth);
  679. text = "A sentence has words.";
  680. // should fit
  681. maxWidth = text.RuneCount + 1;
  682. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  683. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  684. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  685. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  686. Assert.True (expectedClippedWidth <= maxWidth);
  687. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  688. // Should fit.
  689. maxWidth = text.RuneCount + 0;
  690. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  691. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  692. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  693. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  694. Assert.True (expectedClippedWidth <= maxWidth);
  695. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  696. // Should fit.
  697. maxWidth = int.MaxValue;
  698. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  699. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  700. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  701. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  702. Assert.True (expectedClippedWidth <= maxWidth);
  703. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  704. // Should not fit
  705. maxWidth = text.RuneCount - 1;
  706. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  707. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  708. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  709. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  710. Assert.True (expectedClippedWidth <= maxWidth);
  711. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  712. // Should not fit
  713. maxWidth = 10;
  714. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  715. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  716. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  717. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  718. Assert.True (expectedClippedWidth <= maxWidth);
  719. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  720. text = "A\tsentence\thas\twords.";
  721. maxWidth = int.MaxValue;
  722. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  723. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  724. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  725. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  726. Assert.True (expectedClippedWidth <= maxWidth);
  727. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  728. text = "A\tsentence\thas\twords.";
  729. maxWidth = 10;
  730. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  731. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  732. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  733. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  734. Assert.True (expectedClippedWidth <= maxWidth);
  735. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  736. text = "line1\nline2\nline3long!";
  737. maxWidth = int.MaxValue;
  738. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  739. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  740. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  741. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  742. Assert.True (expectedClippedWidth <= maxWidth);
  743. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  744. text = "line1\nline2\nline3long!";
  745. maxWidth = 10;
  746. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  747. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  748. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  749. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  750. Assert.True (expectedClippedWidth <= maxWidth);
  751. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  752. // Unicode
  753. text = " ~  s  gui.cs   master ↑10";
  754. maxWidth = 10;
  755. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  756. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  757. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  758. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  759. Assert.True (expectedClippedWidth <= maxWidth);
  760. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  761. // should fit
  762. text = "Ð ÑÐ";
  763. maxWidth = text.RuneCount + 1;
  764. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  765. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  766. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  767. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  768. Assert.True (expectedClippedWidth <= maxWidth);
  769. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  770. // Should fit.
  771. maxWidth = text.RuneCount + 0;
  772. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  773. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  774. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  775. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  776. Assert.True (expectedClippedWidth <= maxWidth);
  777. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  778. // Should not fit
  779. maxWidth = text.RuneCount - 1;
  780. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  781. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  782. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  783. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  784. Assert.True (expectedClippedWidth <= maxWidth);
  785. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  786. }
  787. [Fact]
  788. public void ClipAndJustify_Valid_Centered ()
  789. {
  790. var align = TextAlignment.Centered;
  791. var text = ustring.Empty;
  792. var justifiedText = ustring.Empty;
  793. int maxWidth = 0;
  794. int expectedClippedWidth = 0;
  795. text = "test";
  796. maxWidth = 0;
  797. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  798. text = "test";
  799. maxWidth = 2;
  800. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  801. text = "test";
  802. maxWidth = int.MaxValue;
  803. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  804. Assert.True (justifiedText.RuneCount <= maxWidth);
  805. Assert.True (justifiedText.ConsoleWidth <= maxWidth);
  806. text = "A sentence has words.";
  807. // should fit
  808. maxWidth = text.RuneCount + 1;
  809. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  810. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  811. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  812. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  813. Assert.True (expectedClippedWidth <= maxWidth);
  814. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  815. // Should fit.
  816. maxWidth = text.RuneCount + 0;
  817. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  818. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  819. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  820. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  821. Assert.True (expectedClippedWidth <= maxWidth);
  822. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  823. // Should fit.
  824. maxWidth = int.MaxValue;
  825. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  826. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  827. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  828. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  829. Assert.True (expectedClippedWidth <= maxWidth);
  830. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  831. // Should not fit
  832. maxWidth = text.RuneCount - 1;
  833. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  834. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  835. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  836. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  837. Assert.True (expectedClippedWidth <= maxWidth);
  838. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  839. // Should not fit
  840. maxWidth = 10;
  841. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  842. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  843. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  844. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  845. Assert.True (expectedClippedWidth <= maxWidth);
  846. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  847. text = "A\tsentence\thas\twords.";
  848. maxWidth = int.MaxValue;
  849. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  850. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  851. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  852. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  853. Assert.True (expectedClippedWidth <= maxWidth);
  854. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  855. text = "A\tsentence\thas\twords.";
  856. maxWidth = 10;
  857. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  858. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  859. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  860. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  861. Assert.True (expectedClippedWidth <= maxWidth);
  862. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  863. text = "line1\nline2\nline3long!";
  864. maxWidth = int.MaxValue;
  865. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  866. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  867. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  868. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  869. Assert.True (expectedClippedWidth <= maxWidth);
  870. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  871. text = "line1\nline2\nline3long!";
  872. maxWidth = 10;
  873. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  874. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  875. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  876. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  877. Assert.True (expectedClippedWidth <= maxWidth);
  878. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  879. // Unicode
  880. text = " ~  s  gui.cs   master ↑10";
  881. maxWidth = 10;
  882. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  883. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  884. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  885. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  886. Assert.True (expectedClippedWidth <= maxWidth);
  887. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  888. // should fit
  889. text = "Ð ÑÐ";
  890. maxWidth = text.RuneCount + 1;
  891. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  892. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  893. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  894. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  895. Assert.True (expectedClippedWidth <= maxWidth);
  896. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  897. // Should fit.
  898. maxWidth = text.RuneCount + 0;
  899. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  900. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  901. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  902. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  903. Assert.True (expectedClippedWidth <= maxWidth);
  904. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  905. // Should not fit
  906. maxWidth = text.RuneCount - 1;
  907. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  908. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  909. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  910. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  911. Assert.True (expectedClippedWidth <= maxWidth);
  912. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  913. }
  914. [Fact]
  915. public void ClipAndJustify_Valid_Justified ()
  916. {
  917. var align = TextAlignment.Justified;
  918. var text = ustring.Empty;
  919. var justifiedText = ustring.Empty;
  920. int maxWidth = 0;
  921. int expectedClippedWidth = 0;
  922. text = "test";
  923. maxWidth = 0;
  924. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  925. text = "test";
  926. maxWidth = 2;
  927. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  928. text = "test";
  929. maxWidth = int.MaxValue;
  930. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  931. Assert.True (justifiedText.RuneCount <= maxWidth);
  932. text = "A sentence has words.";
  933. // should fit
  934. maxWidth = text.RuneCount + 1;
  935. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  936. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  937. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  938. Assert.True (expectedClippedWidth <= maxWidth);
  939. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  940. // Should fit.
  941. maxWidth = text.RuneCount + 0;
  942. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  943. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  944. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  945. Assert.True (expectedClippedWidth <= maxWidth);
  946. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  947. // Should fit.
  948. maxWidth = 500;
  949. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  950. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  951. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  952. Assert.True (expectedClippedWidth <= maxWidth);
  953. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  954. // Should not fit
  955. maxWidth = text.RuneCount - 1;
  956. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  957. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  958. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  959. Assert.True (expectedClippedWidth <= maxWidth);
  960. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  961. // Should not fit
  962. maxWidth = 10;
  963. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  964. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  965. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  966. Assert.True (expectedClippedWidth <= maxWidth);
  967. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  968. text = "A\tsentence\thas\twords.";
  969. maxWidth = int.MaxValue;
  970. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  971. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  972. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  973. Assert.True (expectedClippedWidth <= maxWidth);
  974. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  975. text = "A\tsentence\thas\twords.";
  976. maxWidth = 10;
  977. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  978. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  979. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  980. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  981. Assert.True (expectedClippedWidth <= maxWidth);
  982. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  983. text = "line1\nline2\nline3long!";
  984. maxWidth = int.MaxValue;
  985. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  986. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  987. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  988. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  989. Assert.True (expectedClippedWidth <= maxWidth);
  990. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  991. text = "line1\nline2\nline3long!";
  992. maxWidth = 10;
  993. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  994. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  995. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  996. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)));
  997. Assert.True (expectedClippedWidth <= maxWidth);
  998. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  999. // Unicode
  1000. text = " ~  s  gui.cs   master ↑10";
  1001. maxWidth = 10;
  1002. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1003. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  1004. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  1005. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  1006. Assert.True (expectedClippedWidth <= maxWidth);
  1007. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  1008. // should fit
  1009. text = "Ð ÑÐ";
  1010. maxWidth = text.RuneCount + 1;
  1011. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1012. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  1013. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  1014. Assert.True (expectedClippedWidth <= maxWidth);
  1015. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  1016. // Should fit.
  1017. maxWidth = text.RuneCount + 0;
  1018. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1019. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  1020. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  1021. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  1022. Assert.True (expectedClippedWidth <= maxWidth);
  1023. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  1024. // Should not fit
  1025. maxWidth = text.RuneCount - 1;
  1026. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1027. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  1028. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  1029. Assert.Equal (expectedClippedWidth, justifiedText.ConsoleWidth);
  1030. Assert.True (expectedClippedWidth <= maxWidth);
  1031. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  1032. // see Justify_ tests below
  1033. }
  1034. [Fact]
  1035. public void Justify_Invalid ()
  1036. {
  1037. var text = ustring.Empty;
  1038. Assert.Equal (text, TextFormatter.Justify (text, 0));
  1039. text = null;
  1040. Assert.Equal (text, TextFormatter.Justify (text, 0));
  1041. text = "test";
  1042. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  1043. }
  1044. [Fact]
  1045. public void Justify_SingleWord ()
  1046. {
  1047. var text = ustring.Empty;
  1048. var justifiedText = ustring.Empty;
  1049. int width = 0;
  1050. char fillChar = '+';
  1051. // Even # of chars
  1052. text = "word";
  1053. justifiedText = text;
  1054. width = text.RuneCount;
  1055. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1056. width = text.RuneCount + 1;
  1057. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1058. width = text.RuneCount + 2;
  1059. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1060. width = text.RuneCount + 10;
  1061. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1062. width = text.RuneCount + 11;
  1063. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1064. // Odd # of chars
  1065. text = "word.";
  1066. justifiedText = text;
  1067. width = text.RuneCount;
  1068. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1069. width = text.RuneCount + 1;
  1070. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1071. width = text.RuneCount + 2;
  1072. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1073. width = text.RuneCount + 10;
  1074. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1075. width = text.RuneCount + 11;
  1076. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1077. // Unicode (even #)
  1078. text = "пÑивеÑ";
  1079. justifiedText = text;
  1080. width = text.RuneCount;
  1081. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1082. width = text.RuneCount + 1;
  1083. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1084. width = text.RuneCount + 2;
  1085. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1086. width = text.RuneCount + 10;
  1087. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1088. width = text.RuneCount + 11;
  1089. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1090. // Unicode (odd # of chars)
  1091. text = "пÑивеÑ.";
  1092. justifiedText = text;
  1093. width = text.RuneCount;
  1094. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1095. width = text.RuneCount + 1;
  1096. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1097. width = text.RuneCount + 2;
  1098. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1099. width = text.RuneCount + 10;
  1100. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1101. width = text.RuneCount + 11;
  1102. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1103. }
  1104. [Fact]
  1105. public void Justify_Sentence ()
  1106. {
  1107. var text = ustring.Empty;
  1108. var justifiedText = ustring.Empty;
  1109. int forceToWidth = 0;
  1110. char fillChar = '+';
  1111. // Even # of spaces
  1112. // 0123456789
  1113. text = "012 456 89";
  1114. forceToWidth = text.RuneCount;
  1115. justifiedText = text.Replace (" ", "+");
  1116. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1117. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1118. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1119. justifiedText = text.Replace (" ", "+");
  1120. forceToWidth = text.RuneCount + 1;
  1121. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1122. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1123. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1124. justifiedText = text.Replace (" ", "++");
  1125. forceToWidth = text.RuneCount + 2;
  1126. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1127. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1128. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1129. justifiedText = text.Replace (" ", "++");
  1130. forceToWidth = text.RuneCount + 3;
  1131. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1132. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1133. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1134. justifiedText = text.Replace (" ", "+++");
  1135. forceToWidth = text.RuneCount + 4;
  1136. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1137. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1138. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1139. justifiedText = text.Replace (" ", "+++");
  1140. forceToWidth = text.RuneCount + 5;
  1141. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1142. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1143. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1144. justifiedText = text.Replace (" ", "++++");
  1145. forceToWidth = text.RuneCount + 6;
  1146. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1147. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1148. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1149. justifiedText = text.Replace (" ", "+++++++++++");
  1150. forceToWidth = text.RuneCount + 20;
  1151. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1152. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1153. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1154. justifiedText = text.Replace (" ", "++++++++++++");
  1155. forceToWidth = text.RuneCount + 23;
  1156. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1157. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1158. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1159. // Odd # of spaces
  1160. // 0123456789
  1161. text = "012 456 89 end";
  1162. forceToWidth = text.RuneCount;
  1163. justifiedText = text.Replace (" ", "+");
  1164. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1165. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1166. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1167. justifiedText = text.Replace (" ", "+");
  1168. forceToWidth = text.RuneCount + 1;
  1169. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1170. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1171. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1172. justifiedText = text.Replace (" ", "+");
  1173. forceToWidth = text.RuneCount + 2;
  1174. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1175. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1176. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1177. justifiedText = text.Replace (" ", "++");
  1178. forceToWidth = text.RuneCount + 3;
  1179. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1180. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1181. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1182. justifiedText = text.Replace (" ", "++");
  1183. forceToWidth = text.RuneCount + 4;
  1184. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1185. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1186. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1187. justifiedText = text.Replace (" ", "++");
  1188. forceToWidth = text.RuneCount + 5;
  1189. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1190. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1191. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1192. justifiedText = text.Replace (" ", "+++");
  1193. forceToWidth = text.RuneCount + 6;
  1194. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1195. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1196. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1197. justifiedText = text.Replace (" ", "+++++++");
  1198. forceToWidth = text.RuneCount + 20;
  1199. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1200. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1201. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1202. justifiedText = text.Replace (" ", "++++++++");
  1203. forceToWidth = text.RuneCount + 23;
  1204. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1205. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1206. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1207. // Unicode
  1208. // Even # of chars
  1209. // 0123456789
  1210. text = "пÑРвРÑ";
  1211. forceToWidth = text.RuneCount;
  1212. justifiedText = text.Replace (" ", "+");
  1213. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1214. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1215. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1216. justifiedText = text.Replace (" ", "+");
  1217. forceToWidth = text.RuneCount + 1;
  1218. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1219. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1220. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1221. justifiedText = text.Replace (" ", "++");
  1222. forceToWidth = text.RuneCount + 2;
  1223. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1224. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1225. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1226. justifiedText = text.Replace (" ", "++");
  1227. forceToWidth = text.RuneCount + 3;
  1228. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1229. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1230. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1231. justifiedText = text.Replace (" ", "+++");
  1232. forceToWidth = text.RuneCount + 4;
  1233. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1234. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1235. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1236. justifiedText = text.Replace (" ", "+++");
  1237. forceToWidth = text.RuneCount + 5;
  1238. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1239. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1240. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1241. justifiedText = text.Replace (" ", "++++");
  1242. forceToWidth = text.RuneCount + 6;
  1243. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1244. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1245. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1246. justifiedText = text.Replace (" ", "+++++++++++");
  1247. forceToWidth = text.RuneCount + 20;
  1248. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1249. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1250. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1251. justifiedText = text.Replace (" ", "++++++++++++");
  1252. forceToWidth = text.RuneCount + 23;
  1253. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1254. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1255. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1256. // Unicode
  1257. // Odd # of chars
  1258. // 0123456789
  1259. text = "Ð ÑРвРÑ";
  1260. forceToWidth = text.RuneCount;
  1261. justifiedText = text.Replace (" ", "+");
  1262. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1263. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1264. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1265. justifiedText = text.Replace (" ", "+");
  1266. forceToWidth = text.RuneCount + 1;
  1267. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1268. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1269. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1270. justifiedText = text.Replace (" ", "+");
  1271. forceToWidth = text.RuneCount + 2;
  1272. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1273. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1274. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1275. justifiedText = text.Replace (" ", "++");
  1276. forceToWidth = text.RuneCount + 3;
  1277. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1278. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1279. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1280. justifiedText = text.Replace (" ", "++");
  1281. forceToWidth = text.RuneCount + 4;
  1282. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1283. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1284. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1285. justifiedText = text.Replace (" ", "++");
  1286. forceToWidth = text.RuneCount + 5;
  1287. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1288. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1289. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1290. justifiedText = text.Replace (" ", "+++");
  1291. forceToWidth = text.RuneCount + 6;
  1292. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1293. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1294. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1295. justifiedText = text.Replace (" ", "+++++++");
  1296. forceToWidth = text.RuneCount + 20;
  1297. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1298. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1299. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1300. justifiedText = text.Replace (" ", "++++++++");
  1301. forceToWidth = text.RuneCount + 23;
  1302. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1303. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1304. Assert.True (Math.Abs (forceToWidth - justifiedText.ConsoleWidth) < text.Count (" "));
  1305. }
  1306. [Fact]
  1307. public void WordWrap_Invalid ()
  1308. {
  1309. var text = ustring.Empty;
  1310. int width = 0;
  1311. Assert.Empty (TextFormatter.WordWrap (null, width));
  1312. Assert.Empty (TextFormatter.WordWrap (text, width));
  1313. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrap (text, -1));
  1314. }
  1315. [Fact]
  1316. public void WordWrap_SingleWordLine ()
  1317. {
  1318. var text = ustring.Empty;
  1319. int width = 0;
  1320. List<ustring> wrappedLines;
  1321. text = "Constantinople";
  1322. width = text.RuneCount;
  1323. wrappedLines = TextFormatter.WordWrap (text, width);
  1324. Assert.True (wrappedLines.Count == 1);
  1325. width = text.RuneCount - 1;
  1326. wrappedLines = TextFormatter.WordWrap (text, width);
  1327. Assert.Equal (2, wrappedLines.Count);
  1328. Assert.Equal (text [0, text.RuneCount - 1].ToString (), wrappedLines [0].ToString ());
  1329. Assert.Equal ("e", wrappedLines [1].ToString ());
  1330. width = text.RuneCount - 2;
  1331. wrappedLines = TextFormatter.WordWrap (text, width);
  1332. Assert.Equal (2, wrappedLines.Count);
  1333. Assert.Equal (text [0, text.RuneCount - 2].ToString (), wrappedLines [0].ToString ());
  1334. width = text.RuneCount - 5;
  1335. wrappedLines = TextFormatter.WordWrap (text, width);
  1336. Assert.Equal (2, wrappedLines.Count);
  1337. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1338. wrappedLines = TextFormatter.WordWrap (text, width);
  1339. Assert.Equal (2, wrappedLines.Count);
  1340. Assert.Equal ("Constan", wrappedLines [0].ToString ());
  1341. Assert.Equal ("tinople", wrappedLines [1].ToString ());
  1342. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1343. wrappedLines = TextFormatter.WordWrap (text, width);
  1344. Assert.Equal (3, wrappedLines.Count);
  1345. Assert.Equal ("Const", wrappedLines [0].ToString ());
  1346. Assert.Equal ("antin", wrappedLines [1].ToString ());
  1347. Assert.Equal ("ople", wrappedLines [2].ToString ());
  1348. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1349. wrappedLines = TextFormatter.WordWrap (text, width);
  1350. Assert.Equal (4, wrappedLines.Count);
  1351. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1352. wrappedLines = TextFormatter.WordWrap (text, width);
  1353. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1354. Assert.Equal (text.ConsoleWidth, wrappedLines.Count);
  1355. Assert.Equal ("C", wrappedLines [0].ToString ());
  1356. Assert.Equal ("o", wrappedLines [1].ToString ());
  1357. Assert.Equal ("n", wrappedLines [2].ToString ());
  1358. Assert.Equal ("s", wrappedLines [3].ToString ());
  1359. Assert.Equal ("t", wrappedLines [4].ToString ());
  1360. Assert.Equal ("a", wrappedLines [5].ToString ());
  1361. Assert.Equal ("n", wrappedLines [6].ToString ());
  1362. Assert.Equal ("t", wrappedLines [7].ToString ());
  1363. Assert.Equal ("i", wrappedLines [8].ToString ());
  1364. Assert.Equal ("n", wrappedLines [9].ToString ());
  1365. Assert.Equal ("o", wrappedLines [10].ToString ());
  1366. Assert.Equal ("p", wrappedLines [11].ToString ());
  1367. Assert.Equal ("l", wrappedLines [12].ToString ());
  1368. Assert.Equal ("e", wrappedLines [13].ToString ());
  1369. }
  1370. [Fact]
  1371. public void WordWrap_Unicode_SingleWordLine ()
  1372. {
  1373. var text = ustring.Empty;
  1374. int width = 0;
  1375. List<ustring> wrappedLines;
  1376. text = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ";
  1377. width = text.RuneCount;
  1378. wrappedLines = TextFormatter.WordWrap (text, width);
  1379. Assert.True (wrappedLines.Count == 1);
  1380. width = text.RuneCount - 1;
  1381. wrappedLines = TextFormatter.WordWrap (text, width);
  1382. Assert.Equal (2, wrappedLines.Count);
  1383. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1384. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) - 1)]).ToString (), wrappedLines [0].ToString ());
  1385. Assert.Equal ("ำ", wrappedLines [1].ToString ());
  1386. width = text.RuneCount - 2;
  1387. wrappedLines = TextFormatter.WordWrap (text, width);
  1388. Assert.Equal (2, wrappedLines.Count);
  1389. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1390. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) - 2)]).ToString (), wrappedLines [0].ToString ());
  1391. width = text.RuneCount - 5;
  1392. wrappedLines = TextFormatter.WordWrap (text, width);
  1393. Assert.Equal (2, wrappedLines.Count);
  1394. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1395. wrappedLines = TextFormatter.WordWrap (text, width);
  1396. Assert.Equal (2, wrappedLines.Count);
  1397. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", wrappedLines [0].ToString ());
  1398. Assert.Equal ("ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [1].ToString ());
  1399. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1400. wrappedLines = TextFormatter.WordWrap (text, width);
  1401. Assert.Equal (3, wrappedLines.Count);
  1402. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑ", wrappedLines [0].ToString ());
  1403. Assert.Equal ("ฒณดตถทธนบปผฝพฟภมย", wrappedLines [1].ToString ());
  1404. Assert.Equal ("รฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [2].ToString ());
  1405. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1406. wrappedLines = TextFormatter.WordWrap (text, width);
  1407. Assert.Equal (4, wrappedLines.Count);
  1408. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1409. wrappedLines = TextFormatter.WordWrap (text, width);
  1410. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1411. Assert.Equal (text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)), wrappedLines.Count);
  1412. Assert.Equal ("ก", wrappedLines [0].ToString ());
  1413. Assert.Equal ("ข", wrappedLines [1].ToString ());
  1414. Assert.Equal ("ฃ", wrappedLines [2].ToString ());
  1415. Assert.Equal ("ำ", wrappedLines [^1].ToString ());
  1416. }
  1417. [Fact]
  1418. public void WordWrap_Unicode_LineWithNonBreakingSpace ()
  1419. {
  1420. var text = ustring.Empty;
  1421. int width = 0;
  1422. List<ustring> wrappedLines;
  1423. text = "This\u00A0is\u00A0a\u00A0sentence.";
  1424. width = text.RuneCount;
  1425. wrappedLines = TextFormatter.WordWrap (text, width);
  1426. Assert.True (wrappedLines.Count == 1);
  1427. width = text.RuneCount - 1;
  1428. wrappedLines = TextFormatter.WordWrap (text, width);
  1429. Assert.Equal (2, wrappedLines.Count);
  1430. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1431. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) - 1)]).ToString (), wrappedLines [0].ToString ());
  1432. Assert.Equal (".", wrappedLines [1].ToString ());
  1433. width = text.RuneCount - 2;
  1434. wrappedLines = TextFormatter.WordWrap (text, width);
  1435. Assert.Equal (2, wrappedLines.Count);
  1436. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1437. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) - 2)]).ToString (), wrappedLines [0].ToString ());
  1438. width = text.RuneCount - 5;
  1439. wrappedLines = TextFormatter.WordWrap (text, width);
  1440. Assert.Equal (2, wrappedLines.Count);
  1441. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1442. wrappedLines = TextFormatter.WordWrap (text, width);
  1443. Assert.Equal (2, wrappedLines.Count);
  1444. Assert.Equal ("This\u00A0is\u00A0a\u00A0", wrappedLines [0].ToString ());
  1445. Assert.Equal ("sentence.", wrappedLines [1].ToString ());
  1446. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1447. wrappedLines = TextFormatter.WordWrap (text, width);
  1448. Assert.Equal (3, wrappedLines.Count);
  1449. Assert.Equal ("This\u00A0is", wrappedLines [0].ToString ());
  1450. Assert.Equal ("\u00a0a\u00a0sent", wrappedLines [1].ToString ());
  1451. Assert.Equal ("ence.", wrappedLines [2].ToString ());
  1452. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1453. wrappedLines = TextFormatter.WordWrap (text, width);
  1454. Assert.Equal (4, wrappedLines.Count);
  1455. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1456. wrappedLines = TextFormatter.WordWrap (text, width);
  1457. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1458. Assert.Equal (text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1)), wrappedLines.Count);
  1459. Assert.Equal ("T", wrappedLines [0].ToString ());
  1460. Assert.Equal ("h", wrappedLines [1].ToString ());
  1461. Assert.Equal ("i", wrappedLines [2].ToString ());
  1462. Assert.Equal (".", wrappedLines [^1].ToString ());
  1463. }
  1464. [Fact]
  1465. public void WordWrap_Unicode_2LinesWithNonBreakingSpace ()
  1466. {
  1467. var text = ustring.Empty;
  1468. int width = 0;
  1469. List<ustring> wrappedLines;
  1470. text = "This\u00A0is\n\u00A0a\u00A0sentence.";
  1471. width = text.RuneCount;
  1472. wrappedLines = TextFormatter.WordWrap (text, width);
  1473. Assert.True (wrappedLines.Count == 1);
  1474. width = text.RuneCount - 1;
  1475. wrappedLines = TextFormatter.WordWrap (text, width);
  1476. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1477. Assert.Equal (1, wrappedLines.Count);
  1478. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1479. Assert.Equal ("This\u00A0is\u00A0a\u00A0sentence.", wrappedLines [0].ToString ());
  1480. text = "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.";
  1481. width = text.RuneCount;
  1482. wrappedLines = TextFormatter.WordWrap (text, width);
  1483. Assert.True (wrappedLines.Count == 1);
  1484. }
  1485. [Fact]
  1486. public void WordWrap_NoNewLines ()
  1487. {
  1488. var text = ustring.Empty;
  1489. int maxWidth = 0;
  1490. int expectedClippedWidth = 0;
  1491. List<ustring> wrappedLines;
  1492. text = "A sentence has words.";
  1493. maxWidth = text.RuneCount;
  1494. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1495. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1496. Assert.True (wrappedLines.Count == 1);
  1497. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1498. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1499. Assert.Equal ("A sentence has words.", wrappedLines [0].ToString ());
  1500. maxWidth = text.RuneCount - 1;
  1501. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1502. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1503. Assert.Equal (2, wrappedLines.Count);
  1504. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1505. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1506. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1507. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1508. maxWidth = text.RuneCount - "words.".Length;
  1509. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1510. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1511. Assert.Equal (2, wrappedLines.Count);
  1512. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1513. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1514. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1515. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1516. maxWidth = text.RuneCount - " words.".Length;
  1517. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1518. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1519. Assert.Equal (2, wrappedLines.Count);
  1520. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1521. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1522. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1523. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1524. maxWidth = text.RuneCount - "s words.".Length;
  1525. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1526. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1527. Assert.Equal (2, wrappedLines.Count);
  1528. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1529. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1530. Assert.Equal ("A sentence", wrappedLines [0].ToString ());
  1531. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1532. // Unicode
  1533. text = "A Unicode sentence (пÑивеÑ) has words.";
  1534. maxWidth = text.RuneCount;
  1535. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1536. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1537. Assert.True (wrappedLines.Count == 1);
  1538. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1539. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1540. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.", wrappedLines [0].ToString ());
  1541. maxWidth = text.RuneCount - 1;
  1542. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1543. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1544. Assert.Equal (2, wrappedLines.Count);
  1545. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1546. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1547. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1548. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1549. maxWidth = text.RuneCount - "words.".Length;
  1550. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1551. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1552. Assert.Equal (2, wrappedLines.Count);
  1553. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1554. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1555. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1556. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1557. maxWidth = text.RuneCount - " words.".Length;
  1558. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1559. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1560. Assert.Equal (2, wrappedLines.Count);
  1561. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1562. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1563. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1564. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1565. maxWidth = text.RuneCount - "s words.".Length;
  1566. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1567. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1568. Assert.Equal (2, wrappedLines.Count);
  1569. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1570. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1571. Assert.Equal ("A Unicode sentence (пÑивеÑ)", wrappedLines [0].ToString ());
  1572. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1573. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1574. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1575. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1576. Assert.Equal (2, wrappedLines.Count);
  1577. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1578. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1579. Assert.Equal ("A Unicode sentence", wrappedLines [0].ToString ());
  1580. Assert.Equal ("(пÑивеÑ) has words.", wrappedLines [1].ToString ());
  1581. }
  1582. /// <summary>
  1583. /// WordWrap strips CRLF
  1584. /// </summary>
  1585. [Fact]
  1586. public void WordWrap_WithNewLines ()
  1587. {
  1588. var text = ustring.Empty;
  1589. int maxWidth = 0;
  1590. int expectedClippedWidth = 0;
  1591. List<ustring> wrappedLines;
  1592. text = "A sentence has words.\nA paragraph has lines.";
  1593. maxWidth = text.RuneCount;
  1594. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1595. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1596. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1597. Assert.Equal (1, wrappedLines.Count);
  1598. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1599. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1600. Assert.Equal ("A sentence has words.A paragraph has lines.", wrappedLines [0].ToString ());
  1601. maxWidth = text.RuneCount - 1;
  1602. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1603. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1604. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1605. Assert.Equal (1, wrappedLines.Count);
  1606. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1607. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1608. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1609. Assert.Equal ("A sentence has words.A paragraph has lines.", wrappedLines [0].ToString ());
  1610. maxWidth = text.RuneCount - "words.".Length;
  1611. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1612. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1613. Assert.Equal (2, wrappedLines.Count);
  1614. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1615. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1616. Assert.Equal ("A sentence has words.A paragraph has", wrappedLines [0].ToString ());
  1617. Assert.Equal ("lines.", wrappedLines [1].ToString ());
  1618. // Unicode
  1619. text = "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии.";
  1620. maxWidth = text.RuneCount;
  1621. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1622. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1623. Assert.True (wrappedLines.Count == 1);
  1624. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1625. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1626. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии.", wrappedLines [0].ToString ());
  1627. maxWidth = text.RuneCount - 1;
  1628. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1629. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1630. Assert.Equal (2, wrappedLines.Count);
  1631. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1632. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1633. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", wrappedLines [0].ToString ());
  1634. Assert.Equal ("Линии.", wrappedLines [1].ToString ());
  1635. maxWidth = text.RuneCount - "words.".Length;
  1636. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1637. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1638. Assert.Equal (2, wrappedLines.Count);
  1639. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1640. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1641. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", wrappedLines [0].ToString ());
  1642. Assert.Equal ("Линии.", wrappedLines [1].ToString ());
  1643. maxWidth = text.RuneCount - "s words.".Length;
  1644. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1645. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1646. Assert.Equal (2, wrappedLines.Count);
  1647. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1648. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1649. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", wrappedLines [0].ToString ());
  1650. Assert.Equal ("has Линии.", wrappedLines [1].ToString ());
  1651. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1652. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1653. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1654. Assert.Equal (2, wrappedLines.Count);
  1655. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1656. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1657. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode", wrappedLines [0].ToString ());
  1658. Assert.Equal ("Пункт has Линии.", wrappedLines [1].ToString ());
  1659. }
  1660. [Fact]
  1661. public void WordWrap_Narrow ()
  1662. {
  1663. var text = ustring.Empty;
  1664. int maxWidth = 1;
  1665. int expectedClippedWidth = 1;
  1666. List<ustring> wrappedLines;
  1667. text = "A sentence has words.";
  1668. maxWidth = 3;
  1669. expectedClippedWidth = 3;
  1670. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1671. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1672. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1673. Assert.Equal ("A", wrappedLines [0].ToString ());
  1674. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1675. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1676. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1677. Assert.Equal ("has", wrappedLines [4].ToString ());
  1678. Assert.Equal ("wor", wrappedLines [5].ToString ());
  1679. Assert.Equal ("ds.", wrappedLines [6].ToString ());
  1680. maxWidth = 2;
  1681. expectedClippedWidth = 2;
  1682. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1683. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1684. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1685. Assert.Equal ("A", wrappedLines [0].ToString ());
  1686. Assert.Equal ("se", wrappedLines [1].ToString ());
  1687. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1688. Assert.Equal ("en", wrappedLines [3].ToString ());
  1689. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1690. maxWidth = 1;
  1691. expectedClippedWidth = 1;
  1692. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1693. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1694. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1695. Assert.Equal ("A", wrappedLines [0].ToString ());
  1696. Assert.Equal ("s", wrappedLines [1].ToString ());
  1697. Assert.Equal ("e", wrappedLines [2].ToString ());
  1698. Assert.Equal ("n", wrappedLines [3].ToString ());
  1699. Assert.Equal (".", wrappedLines [^1].ToString ());
  1700. }
  1701. [Fact]
  1702. public void WordWrap_preserveTrailingSpaces ()
  1703. {
  1704. var text = ustring.Empty;
  1705. int maxWidth = 1;
  1706. int expectedClippedWidth = 1;
  1707. List<ustring> wrappedLines;
  1708. text = "A sentence has words.";
  1709. maxWidth = 14;
  1710. expectedClippedWidth = 14;
  1711. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1712. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1713. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.ConsoleWidth));
  1714. Assert.Equal ("A sentence ", 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]
  2524. public void Draw_Horizontal_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2525. {
  2526. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2527. var top = Application.Top;
  2528. var view = new View ("view") { X = -2 };
  2529. top.Add (view);
  2530. Application.Iteration += () => {
  2531. Assert.Equal (-2, view.X);
  2532. Application.RequestStop ();
  2533. };
  2534. try {
  2535. Application.Run ();
  2536. } catch (IndexOutOfRangeException ex) {
  2537. // After the fix this exception will not be caught.
  2538. Assert.IsType<IndexOutOfRangeException> (ex);
  2539. }
  2540. // Shutdown must be called to safely clean up Application if Init has been called
  2541. Application.Shutdown ();
  2542. }
  2543. [Fact]
  2544. public void TestClipOrPad_ShortWord ()
  2545. {
  2546. // word is short but we want it to fill 6 so it should be padded
  2547. Assert.Equal ("fff ", TextFormatter.ClipOrPad ("fff", 6));
  2548. }
  2549. [Fact]
  2550. public void TestClipOrPad_LongWord ()
  2551. {
  2552. // word is long but we want it to fill 3 space only
  2553. Assert.Equal ("123", TextFormatter.ClipOrPad ("123456789", 3));
  2554. }
  2555. [Fact]
  2556. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2557. {
  2558. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2559. var top = Application.Top;
  2560. var view = new View ("view") {
  2561. Y = -2,
  2562. Height = 10,
  2563. TextDirection = TextDirection.TopBottom_LeftRight
  2564. };
  2565. top.Add (view);
  2566. Application.Iteration += () => {
  2567. Assert.Equal (-2, view.Y);
  2568. Application.RequestStop ();
  2569. };
  2570. try {
  2571. Application.Run ();
  2572. } catch (IndexOutOfRangeException ex) {
  2573. // After the fix this exception will not be caught.
  2574. Assert.IsType<IndexOutOfRangeException> (ex);
  2575. }
  2576. // Shutdown must be called to safely clean up Application if Init has been called
  2577. Application.Shutdown ();
  2578. }
  2579. [Fact]
  2580. public void Internal_Tests ()
  2581. {
  2582. var tf = new TextFormatter ();
  2583. Assert.Equal (Key.Null, tf.HotKey);
  2584. tf.HotKey = Key.CtrlMask | Key.Q;
  2585. Assert.Equal (Key.CtrlMask | Key.Q, tf.HotKey);
  2586. }
  2587. [Fact, AutoInitShutdown]
  2588. public void Draw_Horizontal_Simple_Runes ()
  2589. {
  2590. var label = new Label ("Demo Simple Rune");
  2591. Application.Top.Add (label);
  2592. Application.Begin (Application.Top);
  2593. Assert.True (label.AutoSize);
  2594. Assert.Equal (new Rect (0, 0, 16, 1), label.Frame);
  2595. var expected = @"
  2596. Demo Simple Rune
  2597. ";
  2598. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2599. Assert.Equal (new Rect (0, 0, 16, 1), pos);
  2600. }
  2601. [Fact, AutoInitShutdown]
  2602. public void Draw_Vertical_Simple_Runes ()
  2603. {
  2604. var label = new Label ("Demo Simple Rune") {
  2605. TextDirection = TextDirection.TopBottom_LeftRight
  2606. };
  2607. Application.Top.Add (label);
  2608. Application.Begin (Application.Top);
  2609. Assert.True (label.AutoSize);
  2610. Assert.Equal (new Rect (0, 0, 1, 16), label.Frame);
  2611. Assert.NotNull (label.Width);
  2612. Assert.NotNull (label.Height);
  2613. var expected = @"
  2614. D
  2615. e
  2616. m
  2617. o
  2618. S
  2619. i
  2620. m
  2621. p
  2622. l
  2623. e
  2624. R
  2625. u
  2626. n
  2627. e
  2628. ";
  2629. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2630. Assert.Equal (new Rect (0, 0, 1, 16), pos);
  2631. }
  2632. [Fact, AutoInitShutdown]
  2633. public void Draw_Horizontal_Wide_Runes ()
  2634. {
  2635. var label = new Label ("デモエムポンズ");
  2636. Application.Top.Add (label);
  2637. Application.Begin (Application.Top);
  2638. Assert.True (label.AutoSize);
  2639. Assert.Equal (new Rect (0, 0, 14, 1), label.Frame);
  2640. var expected = @"
  2641. デモエムポンズ
  2642. ";
  2643. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2644. Assert.Equal (new Rect (0, 0, 14, 1), pos);
  2645. }
  2646. [Fact, AutoInitShutdown]
  2647. public void Draw_Vertical_Wide_Runes ()
  2648. {
  2649. var label = new Label ("デモエムポンズ") {
  2650. TextDirection = TextDirection.TopBottom_LeftRight
  2651. };
  2652. Application.Top.Add (label);
  2653. Application.Begin (Application.Top);
  2654. Assert.True (label.AutoSize);
  2655. Assert.Equal (new Rect (0, 0, 2, 7), label.Frame);
  2656. var expected = @"
  2657. ";
  2658. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2659. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  2660. }
  2661. [Fact, AutoInitShutdown]
  2662. public void Draw_Vertical_Wide_Runes_With_ForceValidatePosDim ()
  2663. {
  2664. var label = new Label ("デモエムポンズ") {
  2665. Width = Dim.Fill (),
  2666. Height = Dim.Percent (50f),
  2667. TextDirection = TextDirection.TopBottom_LeftRight,
  2668. ForceValidatePosDim = true
  2669. };
  2670. Application.Top.Add (label);
  2671. Application.Begin (Application.Top);
  2672. Assert.True (label.AutoSize);
  2673. Assert.Equal (new Rect (0, 0, 80, 12), label.Frame);
  2674. var expected = @"
  2675. ";
  2676. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2677. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  2678. }
  2679. [Fact, AutoInitShutdown]
  2680. public void Draw_Horizontal_Simple_TextAlignments ()
  2681. {
  2682. var text = "Hello World";
  2683. var width = 20;
  2684. var lblLeft = new Label (text) { Width = width };
  2685. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2686. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2687. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2688. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2689. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2690. Application.Top.Add (frame);
  2691. Application.Begin (Application.Top);
  2692. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2693. Assert.True (lblLeft.AutoSize);
  2694. Assert.True (lblCenter.AutoSize);
  2695. Assert.True (lblRight.AutoSize);
  2696. Assert.True (lblJust.AutoSize);
  2697. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2698. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2699. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2700. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2701. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2702. var expected = @"
  2703. ┌────────────────────┐
  2704. │Hello World │
  2705. │ Hello World │
  2706. │ Hello World│
  2707. │Hello World│
  2708. └────────────────────┘
  2709. ";
  2710. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2711. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2712. }
  2713. [Fact, AutoInitShutdown]
  2714. public void Draw_Vertical_Simple_TextAlignments ()
  2715. {
  2716. var text = "Hello World";
  2717. var height = 20;
  2718. var lblLeft = new Label (text, direction: TextDirection.TopBottom_LeftRight) { Height = height };
  2719. var lblCenter = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 2, Height = height, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2720. var lblRight = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 4, Height = height, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2721. var lblJust = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 6, Height = height, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2722. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2723. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2724. Application.Top.Add (frame);
  2725. Application.Begin (Application.Top);
  2726. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  2727. Assert.True (lblLeft.AutoSize);
  2728. Assert.True (lblCenter.AutoSize);
  2729. Assert.True (lblRight.AutoSize);
  2730. Assert.True (lblJust.AutoSize);
  2731. Assert.Equal (new Rect (0, 0, 1, height), lblLeft.Frame);
  2732. Assert.Equal (new Rect (2, 0, 1, height), lblCenter.Frame);
  2733. Assert.Equal (new Rect (4, 0, 1, height), lblRight.Frame);
  2734. Assert.Equal (new Rect (6, 0, 1, height), lblJust.Frame);
  2735. Assert.Equal (new Rect (0, 0, 9, height + 2), frame.Frame);
  2736. var expected = @"
  2737. ┌───────┐
  2738. │H H│
  2739. │e e│
  2740. │l l│
  2741. │l l│
  2742. │o H o│
  2743. │ e │
  2744. │W l │
  2745. │o l │
  2746. │r o │
  2747. │l H │
  2748. │d W e │
  2749. │ o l │
  2750. │ r l │
  2751. │ l o │
  2752. │ d │
  2753. │ W W│
  2754. │ o o│
  2755. │ r r│
  2756. │ l l│
  2757. │ d d│
  2758. └───────┘
  2759. ";
  2760. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2761. Assert.Equal (new Rect (0, 0, 9, height + 2), pos);
  2762. }
  2763. [Fact, AutoInitShutdown]
  2764. public void Draw_Horizontal_Wide_TextAlignments ()
  2765. {
  2766. var text = "こんにちは 世界";
  2767. var width = 25;
  2768. var lblLeft = new Label (text) { Width = width };
  2769. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2770. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2771. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2772. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2773. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2774. Application.Top.Add (frame);
  2775. Application.Begin (Application.Top);
  2776. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2777. Assert.True (lblLeft.AutoSize);
  2778. Assert.True (lblCenter.AutoSize);
  2779. Assert.True (lblRight.AutoSize);
  2780. Assert.True (lblJust.AutoSize);
  2781. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2782. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2783. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2784. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2785. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2786. var expected = @"
  2787. ┌─────────────────────────┐
  2788. │こんにちは 世界 │
  2789. │ こんにちは 世界 │
  2790. │ こんにちは 世界│
  2791. │こんにちは 世界│
  2792. └─────────────────────────┘
  2793. ";
  2794. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2795. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2796. }
  2797. [Fact, AutoInitShutdown]
  2798. public void Draw_Vertical_Wide_TextAlignments ()
  2799. {
  2800. var text = "こんにちは 世界";
  2801. var height = 23;
  2802. var lblLeft = new Label (text) { Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight };
  2803. var lblCenter = new Label (text) { X = 3, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2804. var lblRight = new Label (text) { X = 6, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2805. var lblJust = new Label (text) { X = 9, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2806. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2807. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2808. Application.Top.Add (frame);
  2809. Application.Begin (Application.Top);
  2810. ((FakeDriver)Application.Driver).SetBufferSize (13, height + 2);
  2811. // All AutoSize are false because the Frame.Height != TextFormatter.Size.Height
  2812. Assert.True (lblLeft.AutoSize);
  2813. Assert.True (lblCenter.AutoSize);
  2814. Assert.True (lblRight.AutoSize);
  2815. Assert.True (lblJust.AutoSize);
  2816. Assert.Equal (new Rect (0, 0, 2, height), lblLeft.Frame);
  2817. Assert.Equal (new Rect (3, 0, 2, height), lblCenter.Frame);
  2818. Assert.Equal (new Rect (6, 0, 2, height), lblRight.Frame);
  2819. Assert.Equal (new Rect (9, 0, 2, height), lblJust.Frame);
  2820. Assert.Equal (new Rect (0, 0, 13, height + 2), frame.Frame);
  2821. var expected = @"
  2822. ┌───────────┐
  2823. │こ こ│
  2824. │ん ん│
  2825. │に に│
  2826. │ち ち│
  2827. │は は│
  2828. │ │
  2829. │世 │
  2830. │界 こ │
  2831. │ ん │
  2832. │ に │
  2833. │ ち │
  2834. │ は │
  2835. │ │
  2836. │ 世 │
  2837. │ 界 │
  2838. │ こ │
  2839. │ ん │
  2840. │ に │
  2841. │ ち │
  2842. │ は │
  2843. │ │
  2844. │ 世 世│
  2845. │ 界 界│
  2846. └───────────┘
  2847. ";
  2848. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2849. Assert.Equal (new Rect (0, 0, 13, height + 2), pos);
  2850. }
  2851. [Fact]
  2852. public void GetTextWidth_Simple_And_Wide_Runes ()
  2853. {
  2854. ustring text = "Hello World";
  2855. Assert.Equal (11, TextFormatter.GetTextWidth (text));
  2856. text = "こんにちは世界";
  2857. Assert.Equal (14, TextFormatter.GetTextWidth (text));
  2858. }
  2859. [Fact]
  2860. public void GetSumMaxCharWidth_Simple_And_Wide_Runes ()
  2861. {
  2862. ustring text = "Hello World";
  2863. Assert.Equal (11, TextFormatter.GetSumMaxCharWidth (text));
  2864. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2865. text = "こんにちは 世界";
  2866. Assert.Equal (15, TextFormatter.GetSumMaxCharWidth (text));
  2867. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2868. }
  2869. [Fact]
  2870. public void GetSumMaxCharWidth_List_Simple_And_Wide_Runes ()
  2871. {
  2872. List<ustring> text = new List<ustring> () { "Hello", "World" };
  2873. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text));
  2874. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2875. text = new List<ustring> () { "こんにちは", "世界" };
  2876. Assert.Equal (4, TextFormatter.GetSumMaxCharWidth (text));
  2877. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2878. }
  2879. [Fact]
  2880. public void GetMaxLengthForWidth_Simple_And_Wide_Runes ()
  2881. {
  2882. ustring text = "Hello World";
  2883. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (text, 6));
  2884. text = "こんにちは 世界";
  2885. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (text, 6));
  2886. }
  2887. [Fact]
  2888. public void GetMaxLengthForWidth_List_Simple_And_Wide_Runes ()
  2889. {
  2890. var runes = ustring.Make ("Hello World").ToRuneList ();
  2891. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2892. runes = ustring.Make ("こんにちは 世界").ToRuneList ();
  2893. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2894. runes = ustring.Make ("[ Say Hello 你 ]").ToRuneList ();
  2895. Assert.Equal (15, TextFormatter.GetMaxLengthForWidth (runes, 16));
  2896. }
  2897. [Fact]
  2898. public void Format_Truncate_Simple_And_Wide_Runes ()
  2899. {
  2900. var text = "Truncate";
  2901. var list = TextFormatter.Format (text, 3, false, false);
  2902. Assert.Equal ("Tru", list [^1].ToString ());
  2903. text = "デモエムポンズ";
  2904. list = TextFormatter.Format (text, 3, false, false);
  2905. Assert.Equal ("デ", list [^1].ToString ());
  2906. }
  2907. [Fact, AutoInitShutdown]
  2908. public void AutoSize_False_View_IsEmpty_False_Return_Null_Lines ()
  2909. {
  2910. var text = "Views";
  2911. var view = new View () {
  2912. Width = Dim.Fill () - text.Length,
  2913. Height = 1,
  2914. Text = text
  2915. };
  2916. var win = new Window ("Window") {
  2917. Width = Dim.Fill (),
  2918. Height = Dim.Fill ()
  2919. };
  2920. win.Add (view);
  2921. Application.Top.Add (win);
  2922. Application.Begin (Application.Top);
  2923. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  2924. Assert.Equal (5, text.Length);
  2925. Assert.False (view.AutoSize);
  2926. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  2927. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  2928. Assert.Equal (new List<ustring> () { "Vie" }, view.TextFormatter.Lines);
  2929. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  2930. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  2931. var expected = @"
  2932. ┌ Wind ──┐
  2933. │Vie │
  2934. │ │
  2935. └────────┘
  2936. ";
  2937. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2938. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2939. text = "0123456789";
  2940. Assert.Equal (10, text.Length);
  2941. view.Width = Dim.Fill () - text.Length;
  2942. Application.Refresh ();
  2943. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  2944. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  2945. Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines);
  2946. expected = @"
  2947. ┌ Wind ──┐
  2948. │ │
  2949. │ │
  2950. └────────┘
  2951. ";
  2952. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2953. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2954. }
  2955. [Fact, AutoInitShutdown]
  2956. public void AutoSize_False_View_IsEmpty_True_Minimum_Height ()
  2957. {
  2958. var text = "Views";
  2959. var view = new View () {
  2960. Width = Dim.Fill () - text.Length,
  2961. Text = text
  2962. };
  2963. var win = new Window ("Window") {
  2964. Width = Dim.Fill (),
  2965. Height = Dim.Fill ()
  2966. };
  2967. win.Add (view);
  2968. Application.Top.Add (win);
  2969. Application.Begin (Application.Top);
  2970. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  2971. Assert.Equal (5, text.Length);
  2972. Assert.False (view.AutoSize);
  2973. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  2974. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  2975. Assert.Single (view.TextFormatter.Lines);
  2976. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  2977. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  2978. var expected = @"
  2979. ┌ Wind ──┐
  2980. │Vie │
  2981. │ │
  2982. └────────┘
  2983. ";
  2984. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2985. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  2986. text = "0123456789";
  2987. Assert.Equal (10, text.Length);
  2988. view.Width = Dim.Fill () - text.Length;
  2989. Application.Refresh ();
  2990. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  2991. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  2992. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  2993. Assert.Null (exception);
  2994. expected = @"
  2995. ┌ Wind ──┐
  2996. │ │
  2997. │ │
  2998. └────────┘
  2999. ";
  3000. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3001. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3002. }
  3003. [Fact, AutoInitShutdown]
  3004. public void AutoSize_True_Label_IsEmpty_False_Never_Return_Null_Lines ()
  3005. {
  3006. var text = "Label";
  3007. var label = new Label () {
  3008. Width = Dim.Fill () - text.Length,
  3009. Height = 1,
  3010. Text = text
  3011. };
  3012. var win = new Window ("Window") {
  3013. Width = Dim.Fill (),
  3014. Height = Dim.Fill ()
  3015. };
  3016. win.Add (label);
  3017. Application.Top.Add (win);
  3018. Application.Begin (Application.Top);
  3019. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3020. Assert.Equal (5, text.Length);
  3021. Assert.True (label.AutoSize);
  3022. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3023. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3024. Assert.Equal (new List<ustring> () { "Label" }, label.TextFormatter.Lines);
  3025. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3026. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3027. var expected = @"
  3028. ┌ Wind ──┐
  3029. │Label │
  3030. │ │
  3031. └────────┘
  3032. ";
  3033. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3034. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3035. text = "0123456789";
  3036. Assert.Equal (10, text.Length);
  3037. label.Width = Dim.Fill () - text.Length;
  3038. Application.Refresh ();
  3039. Assert.True (label.AutoSize);
  3040. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3041. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3042. Assert.Single (label.TextFormatter.Lines);
  3043. expected = @"
  3044. ┌ Wind ──┐
  3045. │Label │
  3046. │ │
  3047. └────────┘
  3048. ";
  3049. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3050. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3051. }
  3052. [Fact, AutoInitShutdown]
  3053. public void AutoSize_False_Label_IsEmpty_True_Return_Null_Lines ()
  3054. {
  3055. var text = "Label";
  3056. var label = new Label () {
  3057. Width = Dim.Fill () - text.Length,
  3058. Height = 1,
  3059. Text = text,
  3060. AutoSize = false
  3061. };
  3062. var win = new Window ("Window") {
  3063. Width = Dim.Fill (),
  3064. Height = Dim.Fill ()
  3065. };
  3066. win.Add (label);
  3067. Application.Top.Add (win);
  3068. Application.Begin (Application.Top);
  3069. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3070. Assert.Equal (5, text.Length);
  3071. Assert.False (label.AutoSize);
  3072. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  3073. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  3074. Assert.Equal (new List<ustring> () { "Lab" }, label.TextFormatter.Lines);
  3075. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3076. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3077. var expected = @"
  3078. ┌ Wind ──┐
  3079. │Lab │
  3080. │ │
  3081. └────────┘
  3082. ";
  3083. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3084. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3085. text = "0123456789";
  3086. Assert.Equal (10, text.Length);
  3087. label.Width = Dim.Fill () - text.Length;
  3088. Application.Refresh ();
  3089. Assert.False (label.AutoSize);
  3090. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  3091. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  3092. Assert.Equal (new List<ustring> { ustring.Empty }, label.TextFormatter.Lines);
  3093. expected = @"
  3094. ┌ Wind ──┐
  3095. │ │
  3096. │ │
  3097. └────────┘
  3098. ";
  3099. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3100. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3101. }
  3102. [Fact, AutoInitShutdown]
  3103. public void AutoSize_True_Label_IsEmpty_False_Minimum_Height ()
  3104. {
  3105. var text = "Label";
  3106. var label = new Label () {
  3107. Width = Dim.Fill () - text.Length,
  3108. Text = text
  3109. };
  3110. var win = new Window ("Window") {
  3111. Width = Dim.Fill (),
  3112. Height = Dim.Fill ()
  3113. };
  3114. win.Add (label);
  3115. Application.Top.Add (win);
  3116. Application.Begin (Application.Top);
  3117. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3118. Assert.Equal (5, text.Length);
  3119. Assert.True (label.AutoSize);
  3120. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3121. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3122. Assert.Equal (new List<ustring> () { "Label" }, label.TextFormatter.Lines);
  3123. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3124. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3125. var expected = @"
  3126. ┌ Wind ──┐
  3127. │Label │
  3128. │ │
  3129. └────────┘
  3130. ";
  3131. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3132. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3133. text = "0123456789";
  3134. Assert.Equal (10, text.Length);
  3135. label.Width = Dim.Fill () - text.Length;
  3136. Application.Refresh ();
  3137. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  3138. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  3139. var exception = Record.Exception (() => Assert.Single (label.TextFormatter.Lines));
  3140. Assert.Null (exception);
  3141. expected = @"
  3142. ┌ Wind ──┐
  3143. │Label │
  3144. │ │
  3145. └────────┘
  3146. ";
  3147. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3148. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3149. }
  3150. [Fact, AutoInitShutdown]
  3151. public void AutoSize_False_Label_Height_Zero_Returns_Minimum_Height ()
  3152. {
  3153. var text = "Label";
  3154. var label = new Label () {
  3155. Width = Dim.Fill () - text.Length,
  3156. Text = text,
  3157. AutoSize = false
  3158. };
  3159. var win = new Window ("Window") {
  3160. Width = Dim.Fill (),
  3161. Height = Dim.Fill ()
  3162. };
  3163. win.Add (label);
  3164. Application.Top.Add (win);
  3165. Application.Begin (Application.Top);
  3166. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  3167. Assert.Equal (5, text.Length);
  3168. Assert.False (label.AutoSize);
  3169. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  3170. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  3171. Assert.Single (label.TextFormatter.Lines);
  3172. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  3173. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  3174. var expected = @"
  3175. ┌ Wind ──┐
  3176. │Lab │
  3177. │ │
  3178. └────────┘
  3179. ";
  3180. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3181. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3182. text = "0123456789";
  3183. Assert.Equal (10, text.Length);
  3184. label.Width = Dim.Fill () - text.Length;
  3185. Application.Refresh ();
  3186. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  3187. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  3188. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, label.TextFormatter.Lines));
  3189. Assert.Null (exception);
  3190. expected = @"
  3191. ┌ Wind ──┐
  3192. │ │
  3193. │ │
  3194. └────────┘
  3195. ";
  3196. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3197. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  3198. }
  3199. [Fact, AutoInitShutdown]
  3200. public void AutoSize_True_View_IsEmpty_False_Minimum_Width ()
  3201. {
  3202. var text = "Views";
  3203. var view = new View () {
  3204. TextDirection = TextDirection.TopBottom_LeftRight,
  3205. Height = Dim.Fill () - text.Length,
  3206. Text = text,
  3207. AutoSize = true
  3208. };
  3209. var win = new Window ("Window") {
  3210. Width = Dim.Fill (),
  3211. Height = Dim.Fill ()
  3212. };
  3213. win.Add (view);
  3214. Application.Top.Add (win);
  3215. Application.Begin (Application.Top);
  3216. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3217. Assert.Equal (5, text.Length);
  3218. Assert.True (view.AutoSize);
  3219. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  3220. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  3221. Assert.Equal (new List<ustring> () { "Views" }, view.TextFormatter.Lines);
  3222. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3223. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3224. var expected = @"
  3225. ┌──┐
  3226. │V │
  3227. │i │
  3228. │e │
  3229. │w │
  3230. │s │
  3231. │ │
  3232. │ │
  3233. │ │
  3234. └──┘
  3235. ";
  3236. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3237. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3238. text = "0123456789";
  3239. Assert.Equal (10, text.Length);
  3240. view.Height = Dim.Fill () - text.Length;
  3241. Application.Refresh ();
  3242. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  3243. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  3244. var exception = Record.Exception (() => Assert.Single (view.TextFormatter.Lines));
  3245. Assert.Null (exception);
  3246. expected = @"
  3247. ┌──┐
  3248. │V │
  3249. │i │
  3250. │e │
  3251. │w │
  3252. │s │
  3253. │ │
  3254. │ │
  3255. │ │
  3256. └──┘
  3257. ";
  3258. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3259. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3260. }
  3261. [Fact, AutoInitShutdown]
  3262. public void AutoSize_False_View_Width_Null_Returns_Host_Frame_Width ()
  3263. {
  3264. var text = "Views";
  3265. var view = new View () {
  3266. TextDirection = TextDirection.TopBottom_LeftRight,
  3267. Height = Dim.Fill () - text.Length,
  3268. Text = text
  3269. };
  3270. var win = new Window ("Window") {
  3271. Width = Dim.Fill (),
  3272. Height = Dim.Fill ()
  3273. };
  3274. win.Add (view);
  3275. Application.Top.Add (win);
  3276. Application.Begin (Application.Top);
  3277. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3278. Assert.Equal (5, text.Length);
  3279. Assert.False (view.AutoSize);
  3280. Assert.Equal (new Rect (0, 0, 1, 3), view.Frame);
  3281. Assert.Equal (new Size (1, 3), view.TextFormatter.Size);
  3282. Assert.Single (view.TextFormatter.Lines);
  3283. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3284. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3285. var expected = @"
  3286. ┌──┐
  3287. │V │
  3288. │i │
  3289. │e │
  3290. │ │
  3291. │ │
  3292. │ │
  3293. │ │
  3294. │ │
  3295. └──┘
  3296. ";
  3297. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3298. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3299. text = "0123456789";
  3300. Assert.Equal (10, text.Length);
  3301. view.Height = Dim.Fill () - text.Length;
  3302. Application.Refresh ();
  3303. Assert.Equal (new Rect (0, 0, 1, 0), view.Frame);
  3304. Assert.Equal (new Size (1, 0), view.TextFormatter.Size);
  3305. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  3306. Assert.Null (exception);
  3307. expected = @"
  3308. ┌──┐
  3309. │ │
  3310. │ │
  3311. │ │
  3312. │ │
  3313. │ │
  3314. │ │
  3315. │ │
  3316. │ │
  3317. └──┘
  3318. ";
  3319. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3320. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3321. }
  3322. [Fact, AutoInitShutdown]
  3323. public void AutoSize_True_View_IsEmpty_False_Minimum_Width_Wide_Rune ()
  3324. {
  3325. var text = "界View";
  3326. var view = new View () {
  3327. TextDirection = TextDirection.TopBottom_LeftRight,
  3328. Height = Dim.Fill () - text.Length,
  3329. Text = text,
  3330. AutoSize = true
  3331. };
  3332. var win = new Window ("Window") {
  3333. Width = Dim.Fill (),
  3334. Height = Dim.Fill ()
  3335. };
  3336. win.Add (view);
  3337. Application.Top.Add (win);
  3338. Application.Begin (Application.Top);
  3339. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3340. Assert.Equal (5, text.Length);
  3341. Assert.True (view.AutoSize);
  3342. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  3343. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  3344. Assert.Equal (new List<ustring> () { "界View" }, view.TextFormatter.Lines);
  3345. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3346. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3347. var expected = @"
  3348. ┌──┐
  3349. │界│
  3350. │V │
  3351. │i │
  3352. │e │
  3353. │w │
  3354. │ │
  3355. │ │
  3356. │ │
  3357. └──┘
  3358. ";
  3359. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3360. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3361. text = "0123456789";
  3362. Assert.Equal (10, text.Length);
  3363. view.Height = Dim.Fill () - text.Length;
  3364. Application.Refresh ();
  3365. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  3366. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  3367. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { "界View" }, view.TextFormatter.Lines));
  3368. Assert.Null (exception);
  3369. expected = @"
  3370. ┌──┐
  3371. │界│
  3372. │V │
  3373. │i │
  3374. │e │
  3375. │w │
  3376. │ │
  3377. │ │
  3378. │ │
  3379. └──┘
  3380. ";
  3381. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3382. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3383. }
  3384. [Fact, AutoInitShutdown]
  3385. public void AutoSize_False_View_Width_Zero_Returns_Minimum_Width_With_Wide_Rune ()
  3386. {
  3387. var text = "界View";
  3388. var view = new View () {
  3389. TextDirection = TextDirection.TopBottom_LeftRight,
  3390. Height = Dim.Fill () - text.Length,
  3391. Text = text
  3392. };
  3393. var win = new Window ("Window") {
  3394. Width = Dim.Fill (),
  3395. Height = Dim.Fill ()
  3396. };
  3397. win.Add (view);
  3398. Application.Top.Add (win);
  3399. Application.Begin (Application.Top);
  3400. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  3401. Assert.Equal (5, text.Length);
  3402. Assert.False (view.AutoSize);
  3403. Assert.Equal (new Rect (0, 0, 2, 3), view.Frame);
  3404. Assert.Equal (new Size (2, 3), view.TextFormatter.Size);
  3405. Assert.Single (view.TextFormatter.Lines);
  3406. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  3407. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  3408. var expected = @"
  3409. ┌──┐
  3410. │界│
  3411. │V │
  3412. │i │
  3413. │ │
  3414. │ │
  3415. │ │
  3416. │ │
  3417. │ │
  3418. └──┘
  3419. ";
  3420. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3421. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3422. text = "0123456789";
  3423. Assert.Equal (10, text.Length);
  3424. view.Height = Dim.Fill () - text.Length;
  3425. Application.Refresh ();
  3426. Assert.Equal (new Rect (0, 0, 2, 0), view.Frame);
  3427. Assert.Equal (new Size (2, 0), view.TextFormatter.Size);
  3428. var exception = Record.Exception (() => Assert.Equal (new List<ustring> () { ustring.Empty }, view.TextFormatter.Lines));
  3429. Assert.Null (exception);
  3430. expected = @"
  3431. ┌──┐
  3432. │ │
  3433. │ │
  3434. │ │
  3435. │ │
  3436. │ │
  3437. │ │
  3438. │ │
  3439. │ │
  3440. └──┘
  3441. ";
  3442. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  3443. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  3444. }
  3445. [Fact]
  3446. public void Format_With_PreserveTrailingSpaces_And_Without_PreserveTrailingSpaces ()
  3447. {
  3448. var text = $"Line1{Environment.NewLine}Line2{Environment.NewLine}Line3{Environment.NewLine}";
  3449. var width = 60;
  3450. var preserveTrailingSpaces = false;
  3451. var formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
  3452. Assert.Equal ("Line1", formated [0]);
  3453. Assert.Equal ("Line2", formated [1]);
  3454. Assert.Equal ("Line3", formated [^1]);
  3455. preserveTrailingSpaces = true;
  3456. formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
  3457. Assert.Equal ("Line1", formated [0]);
  3458. Assert.Equal ("Line2", formated [1]);
  3459. Assert.Equal ("Line3", formated [^1]);
  3460. }
  3461. }
  3462. }