TextFormatterTests.cs 136 KB

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