TextFormatterTests.cs 157 KB

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