TextFormatterTests.cs 159 KB

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