TextFormatterTests.cs 132 KB

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