TextFormatterTests.cs 134 KB

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