TextFormatterTests.cs 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. // Alias Console to MockConsole so we don't accidentally use Console
  4. namespace Terminal.Gui.TextTests;
  5. public class TextFormatterTests
  6. {
  7. private readonly ITestOutputHelper _output;
  8. public TextFormatterTests (ITestOutputHelper output) { _output = output; }
  9. public static IEnumerable<object []> CMGlyphs =>
  10. new List<object []> { new object [] { $"{CM.Glyphs.LeftBracket} Say Hello 你 {CM.Glyphs.RightBracket}", 16, 15 } };
  11. public static IEnumerable<object []> FormatEnvironmentNewLine =>
  12. new List<object []>
  13. {
  14. new object []
  15. {
  16. $"Line1{Environment.NewLine}Line2{Environment.NewLine}Line3{Environment.NewLine}",
  17. 60,
  18. new [] { "Line1", "Line2", "Line3" }
  19. }
  20. };
  21. public static IEnumerable<object []> SplitEnvironmentNewLine =>
  22. new List<object []>
  23. {
  24. new object []
  25. {
  26. $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界",
  27. new [] { "First Line 界", "Second Line 界", "Third Line 界" }
  28. },
  29. new object []
  30. {
  31. $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界{Environment.NewLine}",
  32. new [] { "First Line 界", "Second Line 界", "Third Line 界", "" }
  33. }
  34. };
  35. [Fact]
  36. public void Basic_Usage ()
  37. {
  38. var testText = "test";
  39. var expectedSize = new Size ();
  40. var testBounds = new Rectangle (0, 0, 100, 1);
  41. var tf = new TextFormatter ();
  42. tf.Text = testText;
  43. expectedSize = new Size (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 Size (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 Size (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 Size (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 Rectangle (new Point (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, text));
  84. Assert.Equal (new Rectangle (new Point (-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 Rectangle (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 Rectangle (0, 0, maxWidth, expectedLines), TextFormatter.CalcRect (0, 0, text));
  112. Assert.Equal (
  113. new Rectangle (
  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 Rectangle (0, 0, text.GetRuneCount (), 1), TextFormatter.CalcRect (0, 0, text));
  128. Assert.Equal (new Rectangle (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 Rectangle (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 Size (width, height), tf.Size);
  363. }
  364. else
  365. {
  366. Assert.Equal (new Size (1, text.GetColumns ()), tf.Size);
  367. tf.Size = new Size (width, height);
  368. }
  369. tf.Draw (
  370. new Rectangle (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 Size (14, 3), Text = "Test\nTest long\nTest long long\n", MultiLine = true };
  390. tf.Draw (
  391. new Rectangle (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 Rectangle (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 [] { "Hello", "World" }, 2, 1, 1, 1)]
  861. [InlineData (new [] { "こんにちは", "世界" }, 4, 1, 1, 2)]
  862. public void GetWidestLineLength_List_Simple_And_Wide_Runes (
  863. IEnumerable<string> text,
  864. int width,
  865. int index,
  866. int length,
  867. int indexWidth
  868. )
  869. {
  870. Assert.Equal (width, TextFormatter.GetWidestLineLength (text.ToList ()));
  871. Assert.Equal (indexWidth, TextFormatter.GetWidestLineLength (text.ToList (), index, length));
  872. }
  873. [Fact]
  874. public void GetWidestLineLength_List_With_Combining_Runes ()
  875. {
  876. List<string> text = new () { "Les Mis", "e\u0328\u0301", "rables" };
  877. Assert.Equal (1, TextFormatter.GetWidestLineLength (text, 1, 1));
  878. }
  879. [Fact]
  880. public void GetWidestLineLength_With_Combining_Runes ()
  881. {
  882. var text = "Les Mise\u0328\u0301rables";
  883. Assert.Equal (1, TextFormatter.GetWidestLineLength (text, 1, 1));
  884. }
  885. [Fact]
  886. public void Internal_Tests ()
  887. {
  888. var tf = new TextFormatter ();
  889. Assert.Equal (KeyCode.Null, tf.HotKey);
  890. tf.HotKey = KeyCode.CtrlMask | KeyCode.Q;
  891. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, tf.HotKey);
  892. }
  893. [Theory]
  894. [InlineData ("")]
  895. [InlineData (null)]
  896. [InlineData ("test")]
  897. public void Justify_Invalid (string text)
  898. {
  899. Assert.Equal (text, TextFormatter.Justify (text, 0));
  900. Assert.Equal (text, TextFormatter.Justify (text, 0));
  901. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  902. }
  903. [Theory]
  904. // Even # of spaces
  905. // 0123456789
  906. [InlineData ("012 456 89", "012 456 89", 10, 0, "+", true)]
  907. [InlineData ("012 456 89", "012++456+89", 11, 1)]
  908. [InlineData ("012 456 89", "012 456 89", 12, 2, "++", true)]
  909. [InlineData ("012 456 89", "012+++456++89", 13, 3)]
  910. [InlineData ("012 456 89", "012 456 89", 14, 4, "+++", true)]
  911. [InlineData ("012 456 89", "012++++456+++89", 15, 5)]
  912. [InlineData ("012 456 89", "012 456 89", 16, 6, "++++", true)]
  913. [InlineData ("012 456 89", "012 456 89", 30, 20, "+++++++++++", true)]
  914. [InlineData ("012 456 89", "012+++++++++++++456++++++++++++89", 33, 23)]
  915. // Odd # of spaces
  916. // 01234567890123
  917. [InlineData ("012 456 89 end", "012 456 89 end", 14, 0, "+", true)]
  918. [InlineData ("012 456 89 end", "012++456+89+end", 15, 1)]
  919. [InlineData ("012 456 89 end", "012++456++89+end", 16, 2)]
  920. [InlineData ("012 456 89 end", "012 456 89 end", 17, 3, "++", true)]
  921. [InlineData ("012 456 89 end", "012+++456++89++end", 18, 4)]
  922. [InlineData ("012 456 89 end", "012+++456+++89++end", 19, 5)]
  923. [InlineData ("012 456 89 end", "012 456 89 end", 20, 6, "+++", true)]
  924. [InlineData ("012 456 89 end", "012++++++++456++++++++89+++++++end", 34, 20)]
  925. [InlineData ("012 456 89 end", "012+++++++++456+++++++++89++++++++end", 37, 23)]
  926. // Unicode
  927. // Even # of chars
  928. // 0123456789
  929. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 10, 0, "+", true)]
  930. [InlineData ("пÑРвРÑ", "пÑÐ++вÐ+Ñ", 11, 1)]
  931. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 12, 2, "++", true)]
  932. [InlineData ("пÑРвРÑ", "пÑÐ+++вÐ++Ñ", 13, 3)]
  933. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 14, 4, "+++", true)]
  934. [InlineData ("пÑРвРÑ", "пÑÐ++++вÐ+++Ñ", 15, 5)]
  935. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 16, 6, "++++", true)]
  936. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 30, 20, "+++++++++++", true)]
  937. [InlineData ("пÑРвРÑ", "пÑÐ+++++++++++++вÐ++++++++++++Ñ", 33, 23)]
  938. // Unicode
  939. // Odd # of chars
  940. // 0123456789
  941. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 10, 0, "+", true)]
  942. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ+вÐ+Ñ", 11, 1)]
  943. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ++вÐ+Ñ", 12, 2)]
  944. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 13, 3, "++", true)]
  945. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ++вÐ++Ñ", 14, 4)]
  946. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ+++вÐ++Ñ", 15, 5)]
  947. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 16, 6, "+++", true)]
  948. [InlineData ("Ð ÑРвРÑ", "Ð++++++++ÑÐ++++++++вÐ+++++++Ñ", 30, 20)]
  949. [InlineData ("Ð ÑРвРÑ", "Ð+++++++++ÑÐ+++++++++вÐ++++++++Ñ", 33, 23)]
  950. public void Justify_Sentence (
  951. string text,
  952. string justifiedText,
  953. int forceToWidth,
  954. int widthOffset,
  955. string replaceWith = null,
  956. bool replace = false
  957. )
  958. {
  959. var fillChar = '+';
  960. Assert.Equal (forceToWidth, text.GetRuneCount () + widthOffset);
  961. if (replace)
  962. {
  963. justifiedText = text.Replace (" ", replaceWith);
  964. }
  965. Assert.Equal (justifiedText, TextFormatter.Justify (text, forceToWidth, fillChar));
  966. Assert.True (Math.Abs (forceToWidth - justifiedText.GetRuneCount ()) < text.Count (s => s == ' '));
  967. Assert.True (Math.Abs (forceToWidth - justifiedText.GetColumns ()) < text.Count (s => s == ' '));
  968. }
  969. [Theory]
  970. [InlineData ("word")] // Even # of chars
  971. [InlineData ("word.")] // Odd # of chars
  972. [InlineData ("пÑивеÑ")] // Unicode (even #)
  973. [InlineData ("пÑивеÑ.")] // Unicode (odd # of chars)
  974. public void Justify_SingleWord (string text)
  975. {
  976. string justifiedText = text;
  977. var fillChar = '+';
  978. int width = text.GetRuneCount ();
  979. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  980. width = text.GetRuneCount () + 1;
  981. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  982. width = text.GetRuneCount () + 2;
  983. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  984. width = text.GetRuneCount () + 10;
  985. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  986. width = text.GetRuneCount () + 11;
  987. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  988. }
  989. [Theory]
  990. [InlineData ("Single Line 界", 14)]
  991. [InlineData ("First Line 界\nSecond Line 界\nThird Line 界\n", 14)]
  992. public void MaxWidthLine_With_And_Without_Newlines (string text, int expected) { Assert.Equal (expected, TextFormatter.GetWidestLineLength (text)); }
  993. [Theory]
  994. [InlineData (
  995. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  996. 0,
  997. 0,
  998. false,
  999. new [] { "" }
  1000. )]
  1001. [InlineData (
  1002. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1003. 0,
  1004. 1,
  1005. false,
  1006. new [] { "" }
  1007. )]
  1008. [InlineData (
  1009. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1010. 1,
  1011. 0,
  1012. false,
  1013. new [] { "" }
  1014. )]
  1015. [InlineData (
  1016. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1017. 0,
  1018. 0,
  1019. true,
  1020. new [] { "" }
  1021. )]
  1022. [InlineData (
  1023. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1024. 0,
  1025. 1,
  1026. true,
  1027. new [] { "" }
  1028. )]
  1029. [InlineData (
  1030. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1031. 1,
  1032. 0,
  1033. true,
  1034. new [] { "" }
  1035. )]
  1036. [InlineData (
  1037. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1038. 6,
  1039. 5,
  1040. false,
  1041. new [] { "First " }
  1042. )]
  1043. [InlineData ("1\n2\n3\n4\n5\n6", 6, 5, false, new [] { "1 2 3 " })]
  1044. [InlineData (
  1045. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1046. 6,
  1047. 5,
  1048. true,
  1049. new [] { "First ", "Second", "Third ", "Forty ", "Fiftee" }
  1050. )]
  1051. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, false, new [] { "第一" })]
  1052. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, true, new [] { "第一", "第二", "第三", "四十", "第十" })]
  1053. public void MultiLine_WordWrap_False_Horizontal_Direction (
  1054. string text,
  1055. int maxWidth,
  1056. int maxHeight,
  1057. bool multiLine,
  1058. IEnumerable<string> resultLines
  1059. )
  1060. {
  1061. var tf = new TextFormatter
  1062. {
  1063. Text = text, Size = new Size (maxWidth, maxHeight), WordWrap = false, MultiLine = multiLine
  1064. };
  1065. Assert.False (tf.AutoSize);
  1066. Assert.False (tf.WordWrap);
  1067. Assert.True (tf.MultiLine == multiLine);
  1068. Assert.Equal (TextDirection.LeftRight_TopBottom, tf.Direction);
  1069. List<string> splitLines = tf.GetLines ();
  1070. Assert.Equal (splitLines.Count, resultLines.Count ());
  1071. Assert.Equal (splitLines, resultLines);
  1072. }
  1073. [Theory]
  1074. [InlineData (
  1075. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1076. 0,
  1077. 0,
  1078. false,
  1079. new [] { "" }
  1080. )]
  1081. [InlineData (
  1082. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1083. 0,
  1084. 1,
  1085. false,
  1086. new [] { "" }
  1087. )]
  1088. [InlineData (
  1089. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1090. 1,
  1091. 0,
  1092. false,
  1093. new [] { "" }
  1094. )]
  1095. [InlineData (
  1096. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1097. 0,
  1098. 0,
  1099. true,
  1100. new [] { "" }
  1101. )]
  1102. [InlineData (
  1103. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1104. 0,
  1105. 1,
  1106. true,
  1107. new [] { "" }
  1108. )]
  1109. [InlineData (
  1110. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1111. 1,
  1112. 0,
  1113. true,
  1114. new [] { "" }
  1115. )]
  1116. [InlineData (
  1117. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1118. 6,
  1119. 5,
  1120. false,
  1121. new [] { "First" }
  1122. )]
  1123. [InlineData ("1\n2\n3\n4\n5\n6", 6, 5, false, new [] { "1 2 3" })]
  1124. [InlineData (
  1125. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1126. 6,
  1127. 5,
  1128. true,
  1129. new [] { "First", "Secon", "Third", "Forty", "Fifte", "Seven" }
  1130. )]
  1131. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, false, new [] { "第一行 第" })]
  1132. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, true, new [] { "第一行", "第二行" })]
  1133. public void MultiLine_WordWrap_False_Vertical_Direction (
  1134. string text,
  1135. int maxWidth,
  1136. int maxHeight,
  1137. bool multiLine,
  1138. IEnumerable<string> resultLines
  1139. )
  1140. {
  1141. var tf = new TextFormatter
  1142. {
  1143. Text = text,
  1144. Size = new Size (maxWidth, maxHeight),
  1145. WordWrap = false,
  1146. MultiLine = multiLine,
  1147. Direction = TextDirection.TopBottom_LeftRight
  1148. };
  1149. Assert.False (tf.AutoSize);
  1150. Assert.False (tf.WordWrap);
  1151. Assert.True (tf.MultiLine == multiLine);
  1152. Assert.Equal (TextDirection.TopBottom_LeftRight, tf.Direction);
  1153. List<string> splitLines = tf.GetLines ();
  1154. Assert.Equal (splitLines.Count, resultLines.Count ());
  1155. Assert.Equal (splitLines, resultLines);
  1156. }
  1157. [Fact]
  1158. public void NeedsFormat_Sets ()
  1159. {
  1160. var testText = "test";
  1161. var testBounds = new Rectangle (0, 0, 100, 1);
  1162. var tf = new TextFormatter ();
  1163. tf.Text = "test";
  1164. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  1165. Assert.NotEmpty (tf.GetLines ());
  1166. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1167. Assert.Equal (testText, tf.Text);
  1168. tf.Draw (testBounds, new Attribute (), new Attribute ());
  1169. Assert.False (tf.NeedsFormat);
  1170. tf.Size = new Size (1, 1);
  1171. Assert.True (tf.NeedsFormat);
  1172. Assert.NotEmpty (tf.GetLines ());
  1173. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1174. tf.Alignment = TextAlignment.Centered;
  1175. Assert.True (tf.NeedsFormat);
  1176. Assert.NotEmpty (tf.GetLines ());
  1177. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1178. }
  1179. [Theory]
  1180. [InlineData ("", -1, TextAlignment.Left, false, 0)]
  1181. [InlineData (null, 0, TextAlignment.Left, false, 1)]
  1182. [InlineData (null, 0, TextAlignment.Left, true, 1)]
  1183. [InlineData ("", 0, TextAlignment.Left, false, 1)]
  1184. [InlineData ("", 0, TextAlignment.Left, true, 1)]
  1185. public void Reformat_Invalid (string text, int maxWidth, TextAlignment textAlignment, bool wrap, int linesCount)
  1186. {
  1187. if (maxWidth < 0)
  1188. {
  1189. Assert.Throws<ArgumentOutOfRangeException> (
  1190. () =>
  1191. TextFormatter.Format (text, maxWidth, textAlignment, wrap)
  1192. );
  1193. }
  1194. else
  1195. {
  1196. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1197. Assert.NotEmpty (list);
  1198. Assert.True (list.Count == linesCount);
  1199. Assert.Equal (string.Empty, list [0]);
  1200. }
  1201. }
  1202. [Theory]
  1203. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true)]
  1204. [InlineData ("A sentence has words.\nLine 2.", 1, -28, TextAlignment.Left, false, 1, false)]
  1205. [InlineData ("A sentence has words.\nLine 2.", 5, -24, TextAlignment.Left, false, 1, false)]
  1206. [InlineData ("A sentence has words.\nLine 2.", 28, -1, TextAlignment.Left, false, 1, false)]
  1207. // no clip
  1208. [InlineData ("A sentence has words.\nLine 2.", 29, 0, TextAlignment.Left, false, 1, false)]
  1209. [InlineData ("A sentence has words.\nLine 2.", 30, 1, TextAlignment.Left, false, 1, false)]
  1210. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true)]
  1211. [InlineData ("A sentence has words.\r\nLine 2.", 1, -29, TextAlignment.Left, false, 1, false)]
  1212. [InlineData ("A sentence has words.\r\nLine 2.", 5, -25, TextAlignment.Left, false, 1, false)]
  1213. [InlineData ("A sentence has words.\r\nLine 2.", 29, -1, TextAlignment.Left, false, 1, false, 1)]
  1214. [InlineData ("A sentence has words.\r\nLine 2.", 30, 0, TextAlignment.Left, false, 1, false)]
  1215. [InlineData ("A sentence has words.\r\nLine 2.", 31, 1, TextAlignment.Left, false, 1, false)]
  1216. public void Reformat_NoWordrap_NewLines_MultiLine_False (
  1217. string text,
  1218. int maxWidth,
  1219. int widthOffset,
  1220. TextAlignment textAlignment,
  1221. bool wrap,
  1222. int linesCount,
  1223. bool stringEmpty,
  1224. int clipWidthOffset = 0
  1225. )
  1226. {
  1227. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1228. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth) + clipWidthOffset;
  1229. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1230. Assert.NotEmpty (list);
  1231. Assert.True (list.Count == linesCount);
  1232. if (stringEmpty)
  1233. {
  1234. Assert.Equal (string.Empty, list [0]);
  1235. }
  1236. else
  1237. {
  1238. Assert.NotEqual (string.Empty, list [0]);
  1239. }
  1240. if (text.Contains ("\r\n") && maxWidth > 0)
  1241. {
  1242. Assert.Equal (
  1243. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth])
  1244. .Replace ("\r\n", " "),
  1245. list [0]
  1246. );
  1247. }
  1248. else if (text.Contains ('\n') && maxWidth > 0)
  1249. {
  1250. Assert.Equal (
  1251. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth])
  1252. .Replace ("\n", " "),
  1253. list [0]
  1254. );
  1255. }
  1256. else
  1257. {
  1258. Assert.Equal (StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]), list [0]);
  1259. }
  1260. }
  1261. [Theory]
  1262. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true, new [] { "" })]
  1263. [InlineData (
  1264. "A sentence has words.\nLine 2.",
  1265. 1,
  1266. -28,
  1267. TextAlignment.Left,
  1268. false,
  1269. 2,
  1270. false,
  1271. new [] { "A", "L" }
  1272. )]
  1273. [InlineData (
  1274. "A sentence has words.\nLine 2.",
  1275. 5,
  1276. -24,
  1277. TextAlignment.Left,
  1278. false,
  1279. 2,
  1280. false,
  1281. new [] { "A sen", "Line " }
  1282. )]
  1283. [InlineData (
  1284. "A sentence has words.\nLine 2.",
  1285. 28,
  1286. -1,
  1287. TextAlignment.Left,
  1288. false,
  1289. 2,
  1290. false,
  1291. new [] { "A sentence has words.", "Line 2." }
  1292. )]
  1293. //// no clip
  1294. [InlineData (
  1295. "A sentence has words.\nLine 2.",
  1296. 29,
  1297. 0,
  1298. TextAlignment.Left,
  1299. false,
  1300. 2,
  1301. false,
  1302. new [] { "A sentence has words.", "Line 2." }
  1303. )]
  1304. [InlineData (
  1305. "A sentence has words.\nLine 2.",
  1306. 30,
  1307. 1,
  1308. TextAlignment.Left,
  1309. false,
  1310. 2,
  1311. false,
  1312. new [] { "A sentence has words.", "Line 2." }
  1313. )]
  1314. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true, new [] { "" })]
  1315. [InlineData (
  1316. "A sentence has words.\r\nLine 2.",
  1317. 1,
  1318. -29,
  1319. TextAlignment.Left,
  1320. false,
  1321. 2,
  1322. false,
  1323. new [] { "A", "L" }
  1324. )]
  1325. [InlineData (
  1326. "A sentence has words.\r\nLine 2.",
  1327. 5,
  1328. -25,
  1329. TextAlignment.Left,
  1330. false,
  1331. 2,
  1332. false,
  1333. new [] { "A sen", "Line " }
  1334. )]
  1335. [InlineData (
  1336. "A sentence has words.\r\nLine 2.",
  1337. 29,
  1338. -1,
  1339. TextAlignment.Left,
  1340. false,
  1341. 2,
  1342. false,
  1343. new [] { "A sentence has words.", "Line 2." }
  1344. )]
  1345. [InlineData (
  1346. "A sentence has words.\r\nLine 2.",
  1347. 30,
  1348. 0,
  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. 31,
  1358. 1,
  1359. TextAlignment.Left,
  1360. false,
  1361. 2,
  1362. false,
  1363. new [] { "A sentence has words.", "Line 2." }
  1364. )]
  1365. public void Reformat_NoWordrap_NewLines_MultiLine_True (
  1366. string text,
  1367. int maxWidth,
  1368. int widthOffset,
  1369. TextAlignment textAlignment,
  1370. bool wrap,
  1371. int linesCount,
  1372. bool stringEmpty,
  1373. IEnumerable<string> resultLines
  1374. )
  1375. {
  1376. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1377. List<string> list = TextFormatter.Format (
  1378. text,
  1379. maxWidth,
  1380. textAlignment,
  1381. wrap,
  1382. false,
  1383. 0,
  1384. TextDirection.LeftRight_TopBottom,
  1385. true
  1386. );
  1387. Assert.NotEmpty (list);
  1388. Assert.True (list.Count == linesCount);
  1389. if (stringEmpty)
  1390. {
  1391. Assert.Equal (string.Empty, list [0]);
  1392. }
  1393. else
  1394. {
  1395. Assert.NotEqual (string.Empty, list [0]);
  1396. }
  1397. Assert.Equal (list, resultLines);
  1398. }
  1399. [Theory]
  1400. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true, new [] { "" })]
  1401. [InlineData (
  1402. "A sentence has words.\nLine 2.",
  1403. 1,
  1404. -28,
  1405. TextAlignment.Left,
  1406. false,
  1407. 2,
  1408. false,
  1409. new [] { "A", "L" }
  1410. )]
  1411. [InlineData (
  1412. "A sentence has words.\nLine 2.",
  1413. 5,
  1414. -24,
  1415. TextAlignment.Left,
  1416. false,
  1417. 2,
  1418. false,
  1419. new [] { "A sen", "Line " }
  1420. )]
  1421. [InlineData (
  1422. "A sentence has words.\nLine 2.",
  1423. 28,
  1424. -1,
  1425. TextAlignment.Left,
  1426. false,
  1427. 2,
  1428. false,
  1429. new [] { "A sentence has words.", "Line 2." }
  1430. )]
  1431. //// no clip
  1432. [InlineData (
  1433. "A sentence has words.\nLine 2.",
  1434. 29,
  1435. 0,
  1436. TextAlignment.Left,
  1437. false,
  1438. 2,
  1439. false,
  1440. new [] { "A sentence has words.", "Line 2." }
  1441. )]
  1442. [InlineData (
  1443. "A sentence has words.\nLine 2.",
  1444. 30,
  1445. 1,
  1446. TextAlignment.Left,
  1447. false,
  1448. 2,
  1449. false,
  1450. new [] { "A sentence has words.", "Line 2." }
  1451. )]
  1452. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true, new [] { "" })]
  1453. [InlineData (
  1454. "A sentence has words.\r\nLine 2.",
  1455. 1,
  1456. -29,
  1457. TextAlignment.Left,
  1458. false,
  1459. 2,
  1460. false,
  1461. new [] { "A", "L" }
  1462. )]
  1463. [InlineData (
  1464. "A sentence has words.\r\nLine 2.",
  1465. 5,
  1466. -25,
  1467. TextAlignment.Left,
  1468. false,
  1469. 2,
  1470. false,
  1471. new [] { "A sen", "Line " }
  1472. )]
  1473. [InlineData (
  1474. "A sentence has words.\r\nLine 2.",
  1475. 29,
  1476. -1,
  1477. TextAlignment.Left,
  1478. false,
  1479. 2,
  1480. false,
  1481. new [] { "A sentence has words.", "Line 2." }
  1482. )]
  1483. [InlineData (
  1484. "A sentence has words.\r\nLine 2.",
  1485. 30,
  1486. 0,
  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. 31,
  1496. 1,
  1497. TextAlignment.Left,
  1498. false,
  1499. 2,
  1500. false,
  1501. new [] { "A sentence has words.", "Line 2." }
  1502. )]
  1503. public void Reformat_NoWordrap_NewLines_MultiLine_True_Vertical (
  1504. string text,
  1505. int maxWidth,
  1506. int widthOffset,
  1507. TextAlignment textAlignment,
  1508. bool wrap,
  1509. int linesCount,
  1510. bool stringEmpty,
  1511. IEnumerable<string> resultLines
  1512. )
  1513. {
  1514. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1515. List<string> list = TextFormatter.Format (
  1516. text,
  1517. maxWidth,
  1518. textAlignment,
  1519. wrap,
  1520. false,
  1521. 0,
  1522. TextDirection.TopBottom_LeftRight,
  1523. true
  1524. );
  1525. Assert.NotEmpty (list);
  1526. Assert.True (list.Count == linesCount);
  1527. if (stringEmpty)
  1528. {
  1529. Assert.Equal (string.Empty, list [0]);
  1530. }
  1531. else
  1532. {
  1533. Assert.NotEqual (string.Empty, list [0]);
  1534. }
  1535. Assert.Equal (list, resultLines);
  1536. }
  1537. [Theory]
  1538. [InlineData ("", 0, 0, TextAlignment.Left, false, 1, true)]
  1539. [InlineData ("", 1, 1, TextAlignment.Left, false, 1, true)]
  1540. [InlineData ("A sentence has words.", 0, -21, TextAlignment.Left, false, 1, true)]
  1541. [InlineData ("A sentence has words.", 1, -20, TextAlignment.Left, false, 1, false)]
  1542. [InlineData ("A sentence has words.", 5, -16, TextAlignment.Left, false, 1, false)]
  1543. [InlineData ("A sentence has words.", 20, -1, TextAlignment.Left, false, 1, false)]
  1544. // no clip
  1545. [InlineData ("A sentence has words.", 21, 0, TextAlignment.Left, false, 1, false)]
  1546. [InlineData ("A sentence has words.", 22, 1, TextAlignment.Left, false, 1, false)]
  1547. public void Reformat_NoWordrap_SingleLine (
  1548. string text,
  1549. int maxWidth,
  1550. int widthOffset,
  1551. TextAlignment textAlignment,
  1552. bool wrap,
  1553. int linesCount,
  1554. bool stringEmpty
  1555. )
  1556. {
  1557. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1558. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1559. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1560. Assert.NotEmpty (list);
  1561. Assert.True (list.Count == linesCount);
  1562. if (stringEmpty)
  1563. {
  1564. Assert.Equal (string.Empty, list [0]);
  1565. }
  1566. else
  1567. {
  1568. Assert.NotEqual (string.Empty, list [0]);
  1569. }
  1570. Assert.Equal (StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]), list [0]);
  1571. }
  1572. [Theory]
  1573. // Unicode
  1574. [InlineData (
  1575. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1576. 8,
  1577. -1,
  1578. TextAlignment.Left,
  1579. true,
  1580. false,
  1581. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1582. )]
  1583. // no clip
  1584. [InlineData (
  1585. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1586. 9,
  1587. 0,
  1588. TextAlignment.Left,
  1589. true,
  1590. false,
  1591. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1592. )]
  1593. [InlineData (
  1594. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1595. 10,
  1596. 1,
  1597. TextAlignment.Left,
  1598. true,
  1599. false,
  1600. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1601. )]
  1602. public void Reformat_Unicode_Wrap_Spaces_NewLines (
  1603. string text,
  1604. int maxWidth,
  1605. int widthOffset,
  1606. TextAlignment textAlignment,
  1607. bool wrap,
  1608. bool preserveTrailingSpaces,
  1609. IEnumerable<string> resultLines
  1610. )
  1611. {
  1612. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1613. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1614. Assert.Equal (list.Count, resultLines.Count ());
  1615. Assert.Equal (resultLines, list);
  1616. }
  1617. [Theory]
  1618. // Unicode
  1619. // Even # of chars
  1620. // 0123456789
  1621. [InlineData ("\u2660пÑРвРÑ", 10, -1, TextAlignment.Left, true, false, new [] { "\u2660пÑРвÐ", "Ñ" })]
  1622. // no clip
  1623. [InlineData ("\u2660пÑРвРÑ", 11, 0, TextAlignment.Left, true, false, new [] { "\u2660пÑРвРÑ" })]
  1624. [InlineData ("\u2660пÑРвРÑ", 12, 1, TextAlignment.Left, true, false, new [] { "\u2660пÑРвРÑ" })]
  1625. // Unicode
  1626. // Odd # of chars
  1627. // 0123456789
  1628. [InlineData ("\u2660 ÑРвРÑ", 9, -1, TextAlignment.Left, true, false, new [] { "\u2660 ÑРвÐ", "Ñ" })]
  1629. // no clip
  1630. [InlineData ("\u2660 ÑРвРÑ", 10, 0, TextAlignment.Left, true, false, new [] { "\u2660 ÑРвРÑ" })]
  1631. [InlineData ("\u2660 ÑРвРÑ", 11, 1, TextAlignment.Left, true, false, new [] { "\u2660 ÑРвРÑ" })]
  1632. public void Reformat_Unicode_Wrap_Spaces_No_NewLines (
  1633. string text,
  1634. int maxWidth,
  1635. int widthOffset,
  1636. TextAlignment textAlignment,
  1637. bool wrap,
  1638. bool preserveTrailingSpaces,
  1639. IEnumerable<string> resultLines
  1640. )
  1641. {
  1642. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1643. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1644. Assert.Equal (list.Count, resultLines.Count ());
  1645. Assert.Equal (resultLines, list);
  1646. }
  1647. [Theory]
  1648. // Even # of spaces
  1649. // 0123456789
  1650. [InlineData ("012 456 89", 0, -10, TextAlignment.Left, true, true, true, new [] { "" })]
  1651. [InlineData (
  1652. "012 456 89",
  1653. 1,
  1654. -9,
  1655. TextAlignment.Left,
  1656. true,
  1657. true,
  1658. false,
  1659. new [] { "0", "1", "2", " ", "4", "5", "6", " ", "8", "9" },
  1660. "01245689"
  1661. )]
  1662. [InlineData ("012 456 89", 5, -5, TextAlignment.Left, true, true, false, new [] { "012 ", "456 ", "89" })]
  1663. [InlineData ("012 456 89", 9, -1, TextAlignment.Left, true, true, false, new [] { "012 456 ", "89" })]
  1664. // no clip
  1665. [InlineData ("012 456 89", 10, 0, TextAlignment.Left, true, true, false, new [] { "012 456 89" })]
  1666. [InlineData ("012 456 89", 11, 1, TextAlignment.Left, true, true, false, new [] { "012 456 89" })]
  1667. // Odd # of spaces
  1668. // 01234567890123
  1669. [InlineData ("012 456 89 end", 13, -1, TextAlignment.Left, true, true, false, new [] { "012 456 89 ", "end" })]
  1670. // no clip
  1671. [InlineData ("012 456 89 end", 14, 0, TextAlignment.Left, true, true, false, new [] { "012 456 89 end" })]
  1672. [InlineData ("012 456 89 end", 15, 1, TextAlignment.Left, true, true, false, new [] { "012 456 89 end" })]
  1673. public void Reformat_Wrap_Spaces_No_NewLines (
  1674. string text,
  1675. int maxWidth,
  1676. int widthOffset,
  1677. TextAlignment textAlignment,
  1678. bool wrap,
  1679. bool preserveTrailingSpaces,
  1680. bool stringEmpty,
  1681. IEnumerable<string> resultLines,
  1682. string noSpaceText = ""
  1683. )
  1684. {
  1685. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1686. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1687. List<string> list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1688. Assert.NotEmpty (list);
  1689. Assert.True (list.Count == resultLines.Count ());
  1690. if (stringEmpty)
  1691. {
  1692. Assert.Equal (string.Empty, list [0]);
  1693. }
  1694. else
  1695. {
  1696. Assert.NotEqual (string.Empty, list [0]);
  1697. }
  1698. Assert.Equal (resultLines, list);
  1699. if (maxWidth > 0)
  1700. {
  1701. // remove whitespace chars
  1702. if (maxWidth < 5)
  1703. {
  1704. expectedClippedWidth = text.GetRuneCount () - text.Sum (r => r == ' ' ? 1 : 0);
  1705. }
  1706. else
  1707. {
  1708. expectedClippedWidth = Math.Min (
  1709. text.GetRuneCount (),
  1710. maxWidth - text.Sum (r => r == ' ' ? 1 : 0)
  1711. );
  1712. }
  1713. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1714. if (maxWidth == 1)
  1715. {
  1716. Assert.Equal (expectedClippedWidth, list.Count);
  1717. Assert.Equal (noSpaceText, string.Concat (list.ToArray ()));
  1718. }
  1719. if (maxWidth > 1 && maxWidth < 10)
  1720. {
  1721. Assert.Equal (
  1722. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]),
  1723. list [0]
  1724. );
  1725. }
  1726. }
  1727. }
  1728. [Theory]
  1729. [InlineData (null)]
  1730. [InlineData ("")]
  1731. [InlineData ("a")]
  1732. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal (string text)
  1733. {
  1734. var hotKeySpecifier = (Rune)'_';
  1735. if (text == null)
  1736. {
  1737. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  1738. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  1739. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  1740. }
  1741. else
  1742. {
  1743. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  1744. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  1745. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  1746. }
  1747. }
  1748. [Theory]
  1749. [InlineData ("all lower case", 0)]
  1750. [InlineData ("K Before", 0)]
  1751. [InlineData ("aK Second", 1)]
  1752. [InlineData ("Last K", 5)]
  1753. [InlineData ("fter K", 7)]
  1754. [InlineData ("Multiple K and R", 9)]
  1755. [InlineData ("Non-english: Кдать", 13)]
  1756. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal (string text, int hotPos)
  1757. {
  1758. var hotKeySpecifier = (Rune)'_';
  1759. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  1760. }
  1761. [Theory]
  1762. [InlineData ("_K Before", 0, "K Before")]
  1763. [InlineData ("a_K Second", 1, "aK Second")]
  1764. [InlineData ("Last _K", 5, "Last K")]
  1765. [InlineData ("After K_", 7, "After K")]
  1766. [InlineData ("Multiple _K and _R", 9, "Multiple K and _R")]
  1767. [InlineData ("Non-english: _Кдать", 13, "Non-english: Кдать")]
  1768. public void RemoveHotKeySpecifier_Valid_ReturnsStripped (string text, int hotPos, string expectedText)
  1769. {
  1770. var hotKeySpecifier = (Rune)'_';
  1771. Assert.Equal (expectedText, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  1772. }
  1773. [Theory]
  1774. [InlineData ("test", 0, 't', "test")]
  1775. [InlineData ("test", 1, 'e', "test")]
  1776. [InlineData ("Ok", 0, 'O', "Ok")]
  1777. [InlineData ("[◦ Ok ◦]", 3, 'O', "[◦ Ok ◦]")]
  1778. [InlineData ("^k", 0, '^', "^k")]
  1779. public void ReplaceHotKeyWithTag (string text, int hotPos, uint tag, string expected)
  1780. {
  1781. var tf = new TextFormatter ();
  1782. List<Rune> runes = text.ToRuneList ();
  1783. Rune rune;
  1784. if (Rune.TryGetRuneAt (text, hotPos, out rune))
  1785. {
  1786. Assert.Equal (rune, (Rune)tag);
  1787. }
  1788. string result = TextFormatter.ReplaceHotKeyWithTag (text, hotPos);
  1789. Assert.Equal (result, expected);
  1790. Assert.Equal ((Rune)tag, result.ToRunes () [hotPos]);
  1791. Assert.Equal (text.GetRuneCount (), runes.Count);
  1792. Assert.Equal (text, StringExtensions.ToString (runes));
  1793. }
  1794. [Theory]
  1795. [MemberData (nameof (SplitEnvironmentNewLine))]
  1796. public void SplitNewLine_Ending__With_Or_Without_NewLine_Probably_CRLF (
  1797. string text,
  1798. IEnumerable<string> expected
  1799. )
  1800. {
  1801. List<string> splited = TextFormatter.SplitNewLine (text);
  1802. Assert.Equal (expected, splited);
  1803. }
  1804. [Theory]
  1805. [InlineData (
  1806. "First Line 界\nSecond Line 界\nThird Line 界\n",
  1807. new [] { "First Line 界", "Second Line 界", "Third Line 界", "" }
  1808. )]
  1809. public void SplitNewLine_Ending_With_NewLine_Only_LF (string text, IEnumerable<string> expected)
  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 界",
  1817. new [] { "First Line 界", "Second Line 界", "Third Line 界" }
  1818. )]
  1819. public void SplitNewLine_Ending_Without_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 ("New Test 你", 10, 10, 20320, 20320, 9, "你")]
  1826. [InlineData ("New Test \U0001d539", 10, 11, 120121, 55349, 9, "𝔹")]
  1827. public void String_Array_Is_Not_Always_Equal_ToRunes_Array (
  1828. string text,
  1829. int runesLength,
  1830. int stringLength,
  1831. int runeValue,
  1832. int stringValue,
  1833. int index,
  1834. string expected
  1835. )
  1836. {
  1837. Rune [] usToRunes = text.ToRunes ();
  1838. Assert.Equal (runesLength, usToRunes.Length);
  1839. Assert.Equal (stringLength, text.Length);
  1840. Assert.Equal (runeValue, usToRunes [index].Value);
  1841. Assert.Equal (stringValue, text [index]);
  1842. Assert.Equal (expected, usToRunes [index].ToString ());
  1843. if (char.IsHighSurrogate (text [index]))
  1844. {
  1845. // Rune array length isn't equal to string array
  1846. Assert.Equal (expected, new string (new [] { text [index], text [index + 1] }));
  1847. }
  1848. else
  1849. {
  1850. // Rune array length is equal to string array
  1851. Assert.Equal (expected, text [index].ToString ());
  1852. }
  1853. }
  1854. [Theory]
  1855. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1856. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1857. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1858. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1859. public void TabWith_PreserveTrailingSpaces_False (
  1860. int width,
  1861. int height,
  1862. TextDirection textDirection,
  1863. int tabWidth,
  1864. string expected
  1865. )
  1866. {
  1867. var driver = new FakeDriver ();
  1868. driver.Init ();
  1869. var text = "This is a \tTab";
  1870. var tf = new TextFormatter ();
  1871. tf.Direction = textDirection;
  1872. tf.TabWidth = tabWidth;
  1873. tf.Text = text;
  1874. Assert.True (tf.WordWrap);
  1875. Assert.False (tf.PreserveTrailingSpaces);
  1876. Assert.Equal (new Size (width, height), tf.Size);
  1877. tf.Draw (
  1878. new Rectangle (0, 0, width, height),
  1879. new Attribute (ColorName.White, ColorName.Black),
  1880. new Attribute (ColorName.Blue, ColorName.Black),
  1881. default (Rectangle),
  1882. driver
  1883. );
  1884. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
  1885. driver.End ();
  1886. }
  1887. [Theory]
  1888. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1889. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1890. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1891. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1892. public void TabWith_PreserveTrailingSpaces_True (
  1893. int width,
  1894. int height,
  1895. TextDirection textDirection,
  1896. int tabWidth,
  1897. string expected
  1898. )
  1899. {
  1900. var driver = new FakeDriver ();
  1901. driver.Init ();
  1902. var text = "This is a \tTab";
  1903. var tf = new TextFormatter ();
  1904. tf.Direction = textDirection;
  1905. tf.TabWidth = tabWidth;
  1906. tf.PreserveTrailingSpaces = true;
  1907. tf.Text = text;
  1908. Assert.True (tf.WordWrap);
  1909. Assert.Equal (new Size (width, height), tf.Size);
  1910. tf.Draw (
  1911. new Rectangle (0, 0, width, height),
  1912. new Attribute (ColorName.White, ColorName.Black),
  1913. new Attribute (ColorName.Blue, ColorName.Black),
  1914. default (Rectangle),
  1915. driver
  1916. );
  1917. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
  1918. driver.End ();
  1919. }
  1920. [Theory]
  1921. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1922. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1923. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1924. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1925. public void TabWith_WordWrap_True (
  1926. int width,
  1927. int height,
  1928. TextDirection textDirection,
  1929. int tabWidth,
  1930. string expected
  1931. )
  1932. {
  1933. var driver = new FakeDriver ();
  1934. driver.Init ();
  1935. var text = "This is a \tTab";
  1936. var tf = new TextFormatter ();
  1937. tf.Direction = textDirection;
  1938. tf.TabWidth = tabWidth;
  1939. tf.WordWrap = true;
  1940. tf.Text = text;
  1941. Assert.False (tf.PreserveTrailingSpaces);
  1942. Assert.Equal (new Size (width, height), tf.Size);
  1943. tf.Draw (
  1944. new Rectangle (0, 0, width, height),
  1945. new Attribute (ColorName.White, ColorName.Black),
  1946. new Attribute (ColorName.Blue, ColorName.Black),
  1947. default (Rectangle),
  1948. driver
  1949. );
  1950. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
  1951. driver.End ();
  1952. }
  1953. [Theory]
  1954. [InlineData ("123456789", 3, "123")]
  1955. [InlineData ("Hello World", 8, "Hello Wo")]
  1956. public void TestClipOrPad_LongWord (string text, int fillPad, string expectedText)
  1957. {
  1958. // word is long but we want it to fill # space only
  1959. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1960. }
  1961. [Theory]
  1962. [InlineData ("fff", 6, "fff ")]
  1963. [InlineData ("Hello World", 16, "Hello World ")]
  1964. public void TestClipOrPad_ShortWord (string text, int fillPad, string expectedText)
  1965. {
  1966. // word is short but we want it to fill # so it should be padded
  1967. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1968. }
  1969. [Theory]
  1970. [InlineData (TextDirection.LeftRight_TopBottom)]
  1971. [InlineData (TextDirection.TopBottom_LeftRight)]
  1972. public void TestSize_AutoSizeChange (TextDirection textDirection)
  1973. {
  1974. var tf = new TextFormatter { Direction = textDirection, Text = "你你" };
  1975. if (textDirection == TextDirection.LeftRight_TopBottom)
  1976. {
  1977. Assert.Equal (4, tf.Size.Width);
  1978. Assert.Equal (1, tf.Size.Height);
  1979. }
  1980. else
  1981. {
  1982. Assert.Equal (2, tf.Size.Width);
  1983. Assert.Equal (2, tf.Size.Height);
  1984. }
  1985. Assert.False (tf.AutoSize);
  1986. tf.Size = new Size (1, 1);
  1987. Assert.Equal (1, tf.Size.Width);
  1988. Assert.Equal (1, tf.Size.Height);
  1989. tf.AutoSize = true;
  1990. if (textDirection == TextDirection.LeftRight_TopBottom)
  1991. {
  1992. Assert.Equal (4, tf.Size.Width);
  1993. Assert.Equal (1, tf.Size.Height);
  1994. }
  1995. else
  1996. {
  1997. Assert.Equal (2, tf.Size.Width);
  1998. Assert.Equal (2, tf.Size.Height);
  1999. }
  2000. }
  2001. [Theory]
  2002. [InlineData (TextAlignment.Left, false)]
  2003. [InlineData (TextAlignment.Centered, true)]
  2004. [InlineData (TextAlignment.Right, false)]
  2005. [InlineData (TextAlignment.Justified, true)]
  2006. public void TestSize_DirectionChange_AutoSize_True_Or_False_Horizontal (
  2007. TextAlignment textAlignment,
  2008. bool autoSize
  2009. )
  2010. {
  2011. var tf = new TextFormatter
  2012. {
  2013. Direction = TextDirection.LeftRight_TopBottom, Text = "你你", Alignment = textAlignment, AutoSize = autoSize
  2014. };
  2015. Assert.Equal (4, tf.Size.Width);
  2016. Assert.Equal (1, tf.Size.Height);
  2017. tf.Direction = TextDirection.TopBottom_LeftRight;
  2018. if (autoSize && textAlignment != TextAlignment.Justified)
  2019. {
  2020. Assert.Equal (2, tf.Size.Width);
  2021. Assert.Equal (2, tf.Size.Height);
  2022. }
  2023. else
  2024. {
  2025. Assert.Equal (4, tf.Size.Width);
  2026. Assert.Equal (1, tf.Size.Height);
  2027. }
  2028. }
  2029. [Theory]
  2030. [InlineData (VerticalTextAlignment.Top, false)]
  2031. [InlineData (VerticalTextAlignment.Middle, true)]
  2032. [InlineData (VerticalTextAlignment.Bottom, false)]
  2033. [InlineData (VerticalTextAlignment.Justified, true)]
  2034. public void TestSize_DirectionChange_AutoSize_True_Or_False_Vertical (
  2035. VerticalTextAlignment textAlignment,
  2036. bool autoSize
  2037. )
  2038. {
  2039. var tf = new TextFormatter
  2040. {
  2041. Direction = TextDirection.TopBottom_LeftRight,
  2042. Text = "你你",
  2043. VerticalAlignment = textAlignment,
  2044. AutoSize = autoSize
  2045. };
  2046. Assert.Equal (2, tf.Size.Width);
  2047. Assert.Equal (2, tf.Size.Height);
  2048. tf.Direction = TextDirection.LeftRight_TopBottom;
  2049. if (autoSize && textAlignment != VerticalTextAlignment.Justified)
  2050. {
  2051. Assert.Equal (4, tf.Size.Width);
  2052. Assert.Equal (1, tf.Size.Height);
  2053. }
  2054. else
  2055. {
  2056. Assert.Equal (2, tf.Size.Width);
  2057. Assert.Equal (2, tf.Size.Height);
  2058. }
  2059. }
  2060. [Theory]
  2061. [InlineData (TextDirection.LeftRight_TopBottom, false)]
  2062. [InlineData (TextDirection.LeftRight_TopBottom, true)]
  2063. [InlineData (TextDirection.TopBottom_LeftRight, false)]
  2064. [InlineData (TextDirection.TopBottom_LeftRight, true)]
  2065. public void TestSize_SizeChange_AutoSize_True_Or_False (TextDirection textDirection, bool autoSize)
  2066. {
  2067. var tf = new TextFormatter { Direction = textDirection, Text = "你你", AutoSize = autoSize };
  2068. if (textDirection == TextDirection.LeftRight_TopBottom)
  2069. {
  2070. Assert.Equal (4, tf.Size.Width);
  2071. Assert.Equal (1, tf.Size.Height);
  2072. }
  2073. else
  2074. {
  2075. Assert.Equal (2, tf.Size.Width);
  2076. Assert.Equal (2, tf.Size.Height);
  2077. }
  2078. tf.Size = new Size (1, 1);
  2079. if (autoSize)
  2080. {
  2081. if (textDirection == TextDirection.LeftRight_TopBottom)
  2082. {
  2083. Assert.Equal (4, tf.Size.Width);
  2084. Assert.Equal (1, tf.Size.Height);
  2085. }
  2086. else
  2087. {
  2088. Assert.Equal (2, tf.Size.Width);
  2089. Assert.Equal (2, tf.Size.Height);
  2090. }
  2091. }
  2092. else
  2093. {
  2094. Assert.Equal (1, tf.Size.Width);
  2095. Assert.Equal (1, tf.Size.Height);
  2096. }
  2097. }
  2098. [Theory]
  2099. [InlineData (TextAlignment.Left, false)]
  2100. [InlineData (TextAlignment.Centered, true)]
  2101. [InlineData (TextAlignment.Right, false)]
  2102. [InlineData (TextAlignment.Justified, true)]
  2103. public void TestSize_SizeChange_AutoSize_True_Or_False_Horizontal (TextAlignment textAlignment, bool autoSize)
  2104. {
  2105. var tf = new TextFormatter
  2106. {
  2107. Direction = TextDirection.LeftRight_TopBottom, Text = "你你", Alignment = textAlignment, AutoSize = autoSize
  2108. };
  2109. Assert.Equal (4, tf.Size.Width);
  2110. Assert.Equal (1, tf.Size.Height);
  2111. tf.Size = new Size (1, 1);
  2112. if (autoSize && textAlignment != TextAlignment.Justified)
  2113. {
  2114. Assert.Equal (4, tf.Size.Width);
  2115. Assert.Equal (1, tf.Size.Height);
  2116. }
  2117. else
  2118. {
  2119. Assert.Equal (1, tf.Size.Width);
  2120. Assert.Equal (1, tf.Size.Height);
  2121. }
  2122. }
  2123. [Theory]
  2124. [InlineData (VerticalTextAlignment.Top, false)]
  2125. [InlineData (VerticalTextAlignment.Middle, true)]
  2126. [InlineData (VerticalTextAlignment.Bottom, false)]
  2127. [InlineData (VerticalTextAlignment.Justified, true)]
  2128. public void TestSize_SizeChange_AutoSize_True_Or_False_Vertical (
  2129. VerticalTextAlignment textAlignment,
  2130. bool autoSize
  2131. )
  2132. {
  2133. var tf = new TextFormatter
  2134. {
  2135. Direction = TextDirection.TopBottom_LeftRight,
  2136. Text = "你你",
  2137. VerticalAlignment = textAlignment,
  2138. AutoSize = autoSize
  2139. };
  2140. Assert.Equal (2, tf.Size.Width);
  2141. Assert.Equal (2, tf.Size.Height);
  2142. tf.Size = new Size (1, 1);
  2143. if (autoSize && textAlignment != VerticalTextAlignment.Justified)
  2144. {
  2145. Assert.Equal (2, tf.Size.Width);
  2146. Assert.Equal (2, tf.Size.Height);
  2147. }
  2148. else
  2149. {
  2150. Assert.Equal (1, tf.Size.Width);
  2151. Assert.Equal (1, tf.Size.Height);
  2152. }
  2153. }
  2154. [Theory]
  2155. [InlineData (TextDirection.LeftRight_TopBottom, false)]
  2156. [InlineData (TextDirection.LeftRight_TopBottom, true)]
  2157. [InlineData (TextDirection.TopBottom_LeftRight, false)]
  2158. [InlineData (TextDirection.TopBottom_LeftRight, true)]
  2159. public void TestSize_TextChange (TextDirection textDirection, bool autoSize)
  2160. {
  2161. var tf = new TextFormatter { Direction = textDirection, Text = "你", AutoSize = autoSize };
  2162. Assert.Equal (2, tf.Size.Width);
  2163. Assert.Equal (1, tf.Size.Height);
  2164. tf.Text = "你你";
  2165. if (autoSize)
  2166. {
  2167. if (textDirection == TextDirection.LeftRight_TopBottom)
  2168. {
  2169. Assert.Equal (4, tf.Size.Width);
  2170. Assert.Equal (1, tf.Size.Height);
  2171. }
  2172. else
  2173. {
  2174. Assert.Equal (2, tf.Size.Width);
  2175. Assert.Equal (2, tf.Size.Height);
  2176. }
  2177. }
  2178. else
  2179. {
  2180. Assert.Equal (2, tf.Size.Width);
  2181. Assert.Equal (1, tf.Size.Height);
  2182. }
  2183. }
  2184. [Fact]
  2185. public void WordWrap_BigWidth ()
  2186. {
  2187. List<string> wrappedLines;
  2188. var text = "Constantinople";
  2189. wrappedLines = TextFormatter.WordWrapText (text, 100);
  2190. Assert.True (wrappedLines.Count == 1);
  2191. Assert.Equal ("Constantinople", wrappedLines [0]);
  2192. }
  2193. [Fact]
  2194. public void WordWrap_Invalid ()
  2195. {
  2196. var text = string.Empty;
  2197. var width = 0;
  2198. Assert.Empty (TextFormatter.WordWrapText (null, width));
  2199. Assert.Empty (TextFormatter.WordWrapText (text, width));
  2200. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrapText (text, -1));
  2201. }
  2202. [Theory]
  2203. [InlineData ("A sentence has words.", 3, -18, new [] { "A", "sen", "ten", "ce", "has", "wor", "ds." })]
  2204. [InlineData (
  2205. "A sentence has words.",
  2206. 2,
  2207. -19,
  2208. new [] { "A", "se", "nt", "en", "ce", "ha", "s", "wo", "rd", "s." }
  2209. )]
  2210. [InlineData (
  2211. "A sentence has words.",
  2212. 1,
  2213. -20,
  2214. new [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." }
  2215. )]
  2216. public void WordWrap_Narrow_Default (
  2217. string text,
  2218. int maxWidth,
  2219. int widthOffset,
  2220. IEnumerable<string> resultLines
  2221. )
  2222. {
  2223. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  2224. List<string> wrappedLines;
  2225. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2226. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2227. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2228. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2229. Assert.True (
  2230. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2231. );
  2232. Assert.True (
  2233. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2234. );
  2235. Assert.Equal (resultLines, wrappedLines);
  2236. }
  2237. [Theory]
  2238. [InlineData ("A sentence has words.", 21, 0, new [] { "A sentence has words." })]
  2239. [InlineData ("A sentence has words.", 20, -1, new [] { "A sentence has", "words." })]
  2240. [InlineData ("A sentence has words.", 15, -6, new [] { "A sentence has", "words." })]
  2241. [InlineData ("A sentence has words.", 14, -7, new [] { "A sentence has", "words." })]
  2242. [InlineData ("A sentence has words.", 13, -8, new [] { "A sentence", "has words." })]
  2243. // Unicode
  2244. [InlineData (
  2245. "A Unicode sentence (пÑивеÑ) has words.",
  2246. 42,
  2247. 0,
  2248. new [] { "A Unicode sentence (пÑивеÑ) has words." }
  2249. )]
  2250. [InlineData (
  2251. "A Unicode sentence (пÑивеÑ) has words.",
  2252. 41,
  2253. -1,
  2254. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  2255. )]
  2256. [InlineData (
  2257. "A Unicode sentence (пÑивеÑ) has words.",
  2258. 36,
  2259. -6,
  2260. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  2261. )]
  2262. [InlineData (
  2263. "A Unicode sentence (пÑивеÑ) has words.",
  2264. 35,
  2265. -7,
  2266. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  2267. )]
  2268. [InlineData (
  2269. "A Unicode sentence (пÑивеÑ) has words.",
  2270. 34,
  2271. -8,
  2272. new [] { "A Unicode sentence (пÑивеÑ)", "has words." }
  2273. )]
  2274. [InlineData (
  2275. "A Unicode sentence (пÑивеÑ) has words.",
  2276. 25,
  2277. -17,
  2278. new [] { "A Unicode sentence", "(пÑивеÑ) has words." }
  2279. )]
  2280. public void WordWrap_NoNewLines_Default (
  2281. string text,
  2282. int maxWidth,
  2283. int widthOffset,
  2284. IEnumerable<string> resultLines
  2285. )
  2286. {
  2287. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  2288. List<string> wrappedLines;
  2289. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2290. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2291. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2292. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2293. Assert.True (
  2294. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2295. );
  2296. Assert.True (
  2297. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2298. );
  2299. Assert.Equal (resultLines, wrappedLines);
  2300. }
  2301. [Theory]
  2302. [InlineData ("これが最初の行です。 こんにちは世界。 これが2行目です。", 29, 0, new [] { "これが最初の行です。", "こんにちは世界。", "これが2行目です。" })]
  2303. public void WordWrap_PreserveTrailingSpaces_False_Unicode_Wide_Runes (
  2304. string text,
  2305. int maxWidth,
  2306. int widthOffset,
  2307. IEnumerable<string> resultLines
  2308. )
  2309. {
  2310. List<string> wrappedLines;
  2311. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2312. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2313. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2314. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2315. Assert.True (
  2316. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2317. );
  2318. Assert.True (
  2319. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2320. );
  2321. Assert.Equal (resultLines, wrappedLines);
  2322. }
  2323. [Theory]
  2324. [InlineData ("文に は言葉 があり ます。", 14, 0, new [] { "文に は言葉", "があり ます。" })]
  2325. [InlineData ("文に は言葉 があり ます。", 3, -11, new [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  2326. [InlineData ("文に は言葉 があり ます。", 2, -12, new [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  2327. [InlineData (
  2328. "文に は言葉 があり ます。",
  2329. 1,
  2330. -13,
  2331. new [] { " ", " ", " " }
  2332. )] // Just Spaces; should result in a single space for each line
  2333. public void WordWrap_PreserveTrailingSpaces_False_Wide_Runes (
  2334. string text,
  2335. int maxWidth,
  2336. int widthOffset,
  2337. IEnumerable<string> resultLines
  2338. )
  2339. {
  2340. List<string> wrappedLines;
  2341. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2342. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2343. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2344. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2345. Assert.True (
  2346. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2347. );
  2348. Assert.True (
  2349. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2350. );
  2351. Assert.Equal (resultLines, wrappedLines);
  2352. }
  2353. [Theory]
  2354. [InlineData (null, 1, new string [] { })] // null input
  2355. [InlineData ("", 1, new string [] { })] // Empty input
  2356. [InlineData ("1 34", 1, new [] { "1", "3", "4" })] // Single Spaces
  2357. [InlineData ("1", 1, new [] { "1" })] // Short input
  2358. [InlineData ("12", 1, new [] { "1", "2" })]
  2359. [InlineData ("123", 1, new [] { "1", "2", "3" })]
  2360. [InlineData ("123456", 1, new [] { "1", "2", "3", "4", "5", "6" })] // No spaces
  2361. [InlineData (" ", 1, new [] { " " })] // Just Spaces; should result in a single space
  2362. [InlineData (" ", 1, new [] { " " })]
  2363. [InlineData (" ", 1, new [] { " ", " " })]
  2364. [InlineData (" ", 1, new [] { " ", " " })]
  2365. [InlineData ("12 456", 1, new [] { "1", "2", "4", "5", "6" })] // Single Spaces
  2366. [InlineData (" 2 456", 1, new [] { " ", "2", "4", "5", "6" })] // Leading spaces should be preserved.
  2367. [InlineData (" 2 456 8", 1, new [] { " ", "2", "4", "5", "6", "8" })]
  2368. [InlineData (
  2369. "A sentence has words. ",
  2370. 1,
  2371. new [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." }
  2372. )] // Complex example
  2373. [InlineData ("12 567", 1, new [] { "1", "2", " ", "5", "6", "7" })] // Double Spaces
  2374. [InlineData (" 3 567", 1, new [] { " ", "3", "5", "6", "7" })] // Double Leading spaces should be preserved.
  2375. [InlineData (" 3 678 1", 1, new [] { " ", "3", " ", "6", "7", "8", " ", "1" })]
  2376. [InlineData ("1 456", 1, new [] { "1", " ", "4", "5", "6" })]
  2377. [InlineData (
  2378. "A sentence has words. ",
  2379. 1,
  2380. new []
  2381. {
  2382. "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", "w", "o", "r", "d", "s", ".", " "
  2383. }
  2384. )] // Double space Complex example
  2385. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_1 (
  2386. string text,
  2387. int width,
  2388. IEnumerable<string> resultLines
  2389. )
  2390. {
  2391. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2392. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2393. Assert.Equal (resultLines, wrappedLines);
  2394. var breakLines = "";
  2395. foreach (string line in wrappedLines)
  2396. {
  2397. breakLines += $"{line}{Environment.NewLine}";
  2398. }
  2399. var expected = string.Empty;
  2400. foreach (string line in resultLines)
  2401. {
  2402. expected += $"{line}{Environment.NewLine}";
  2403. }
  2404. Assert.Equal (expected, breakLines);
  2405. }
  2406. [Theory]
  2407. [InlineData (null, 3, new string [] { })] // null input
  2408. [InlineData ("", 3, new string [] { })] // Empty input
  2409. [InlineData ("1", 3, new [] { "1" })] // Short input
  2410. [InlineData ("12", 3, new [] { "12" })]
  2411. [InlineData ("123", 3, new [] { "123" })]
  2412. [InlineData ("123456", 3, new [] { "123", "456" })] // No spaces
  2413. [InlineData ("1234567", 3, new [] { "123", "456", "7" })] // No spaces
  2414. [InlineData (" ", 3, new [] { " " })] // Just Spaces; should result in a single space
  2415. [InlineData (" ", 3, new [] { " " })]
  2416. [InlineData (" ", 3, new [] { " " })]
  2417. [InlineData (" ", 3, new [] { " " })]
  2418. [InlineData ("12 456", 3, new [] { "12", "456" })] // Single Spaces
  2419. [InlineData (" 2 456", 3, new [] { " 2", "456" })] // Leading spaces should be preserved.
  2420. [InlineData (" 2 456 8", 3, new [] { " 2", "456", "8" })]
  2421. [InlineData (
  2422. "A sentence has words. ",
  2423. 3,
  2424. new [] { "A", "sen", "ten", "ce", "has", "wor", "ds." }
  2425. )] // Complex example
  2426. [InlineData ("12 567", 3, new [] { "12 ", "567" })] // Double Spaces
  2427. [InlineData (" 3 567", 3, new [] { " 3", "567" })] // Double Leading spaces should be preserved.
  2428. [InlineData (" 3 678 1", 3, new [] { " 3", " 67", "8 ", "1" })]
  2429. [InlineData ("1 456", 3, new [] { "1 ", "456" })]
  2430. [InlineData (
  2431. "A sentence has words. ",
  2432. 3,
  2433. new [] { "A ", "sen", "ten", "ce ", " ", "has", "wor", "ds.", " " }
  2434. )] // Double space Complex example
  2435. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_3 (
  2436. string text,
  2437. int width,
  2438. IEnumerable<string> resultLines
  2439. )
  2440. {
  2441. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2442. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2443. Assert.Equal (resultLines, wrappedLines);
  2444. var breakLines = "";
  2445. foreach (string line in wrappedLines)
  2446. {
  2447. breakLines += $"{line}{Environment.NewLine}";
  2448. }
  2449. var expected = string.Empty;
  2450. foreach (string line in resultLines)
  2451. {
  2452. expected += $"{line}{Environment.NewLine}";
  2453. }
  2454. Assert.Equal (expected, breakLines);
  2455. }
  2456. [Theory]
  2457. [InlineData (null, 50, new string [] { })] // null input
  2458. [InlineData ("", 50, new string [] { })] // Empty input
  2459. [InlineData ("1", 50, new [] { "1" })] // Short input
  2460. [InlineData ("12", 50, new [] { "12" })]
  2461. [InlineData ("123", 50, new [] { "123" })]
  2462. [InlineData ("123456", 50, new [] { "123456" })] // No spaces
  2463. [InlineData ("1234567", 50, new [] { "1234567" })] // No spaces
  2464. [InlineData (" ", 50, new [] { " " })] // Just Spaces; should result in a single space
  2465. [InlineData (" ", 50, new [] { " " })]
  2466. [InlineData (" ", 50, new [] { " " })]
  2467. [InlineData ("12 456", 50, new [] { "12 456" })] // Single Spaces
  2468. [InlineData (" 2 456", 50, new [] { " 2 456" })] // Leading spaces should be preserved.
  2469. [InlineData (" 2 456 8", 50, new [] { " 2 456 8" })]
  2470. [InlineData ("A sentence has words. ", 50, new [] { "A sentence has words. " })] // Complex example
  2471. [InlineData ("12 567", 50, new [] { "12 567" })] // Double Spaces
  2472. [InlineData (" 3 567", 50, new [] { " 3 567" })] // Double Leading spaces should be preserved.
  2473. [InlineData (" 3 678 1", 50, new [] { " 3 678 1" })]
  2474. [InlineData ("1 456", 50, new [] { "1 456" })]
  2475. [InlineData (
  2476. "A sentence has words. ",
  2477. 50,
  2478. new [] { "A sentence has words. " }
  2479. )] // Double space Complex example
  2480. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_50 (
  2481. string text,
  2482. int width,
  2483. IEnumerable<string> resultLines
  2484. )
  2485. {
  2486. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2487. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2488. Assert.Equal (resultLines, wrappedLines);
  2489. var breakLines = "";
  2490. foreach (string line in wrappedLines)
  2491. {
  2492. breakLines += $"{line}{Environment.NewLine}";
  2493. }
  2494. var expected = string.Empty;
  2495. foreach (string line in resultLines)
  2496. {
  2497. expected += $"{line}{Environment.NewLine}";
  2498. }
  2499. Assert.Equal (expected, breakLines);
  2500. }
  2501. [Theory]
  2502. [InlineData ("A sentence has words.", 14, -7, new [] { "A sentence ", "has words." })]
  2503. [InlineData ("A sentence has words.", 8, -13, new [] { "A ", "sentence", " has ", "words." })]
  2504. [InlineData ("A sentence has words.", 6, -15, new [] { "A ", "senten", "ce ", "has ", "words." })]
  2505. [InlineData ("A sentence has words.", 3, -18, new [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds." })]
  2506. [InlineData (
  2507. "A sentence has words.",
  2508. 2,
  2509. -19,
  2510. new [] { "A ", "se", "nt", "en", "ce", " ", "ha", "s ", "wo", "rd", "s." }
  2511. )]
  2512. [InlineData (
  2513. "A sentence has words.",
  2514. 1,
  2515. -20,
  2516. new []
  2517. {
  2518. "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", " ", "w", "o", "r", "d", "s", "."
  2519. }
  2520. )]
  2521. public void WordWrap_PreserveTrailingSpaces_True (
  2522. string text,
  2523. int maxWidth,
  2524. int widthOffset,
  2525. IEnumerable<string> resultLines
  2526. )
  2527. {
  2528. List<string> wrappedLines;
  2529. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2530. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2531. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true);
  2532. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2533. Assert.True (
  2534. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2535. );
  2536. Assert.True (
  2537. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2538. );
  2539. Assert.Equal (resultLines, wrappedLines);
  2540. }
  2541. [Theory]
  2542. [InlineData ("文に は言葉 があり ます。", 14, 0, new [] { "文に は言葉 ", "があり ます。" })]
  2543. [InlineData ("文に は言葉 があり ます。", 3, -11, new [] { "文", "に ", "は", "言", "葉 ", "が", "あ", "り ", "ま", "す", "。" })]
  2544. [InlineData (
  2545. "文に は言葉 があり ます。",
  2546. 2,
  2547. -12,
  2548. new [] { "文", "に", " ", "は", "言", "葉", " ", "が", "あ", "り", " ", "ま", "す", "。" }
  2549. )]
  2550. [InlineData ("文に は言葉 があり ます。", 1, -13, new string [] { })]
  2551. public void WordWrap_PreserveTrailingSpaces_True_Wide_Runes (
  2552. string text,
  2553. int maxWidth,
  2554. int widthOffset,
  2555. IEnumerable<string> resultLines
  2556. )
  2557. {
  2558. List<string> wrappedLines;
  2559. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2560. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2561. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true);
  2562. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2563. Assert.True (
  2564. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2565. );
  2566. Assert.True (
  2567. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2568. );
  2569. Assert.Equal (resultLines, wrappedLines);
  2570. }
  2571. [Theory]
  2572. [InlineData ("A sentence has words. ", 3, new [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds.", " " })]
  2573. [InlineData (
  2574. "A sentence has words. ",
  2575. 3,
  2576. new [] { "A ", " ", "sen", "ten", "ce ", " ", " ", " ", "has", " ", "wor", "ds.", " " }
  2577. )]
  2578. public void WordWrap_PreserveTrailingSpaces_True_With_Simple_Runes_Width_3 (
  2579. string text,
  2580. int width,
  2581. IEnumerable<string> resultLines
  2582. )
  2583. {
  2584. List<string> wrappedLines = TextFormatter.WordWrapText (text, width, true);
  2585. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2586. Assert.Equal (resultLines, wrappedLines);
  2587. var breakLines = "";
  2588. foreach (string line in wrappedLines)
  2589. {
  2590. breakLines += $"{line}{Environment.NewLine}";
  2591. }
  2592. var expected = string.Empty;
  2593. foreach (string line in resultLines)
  2594. {
  2595. expected += $"{line}{Environment.NewLine}";
  2596. }
  2597. Assert.Equal (expected, breakLines);
  2598. // Double space Complex example - this is how VS 2022 does it
  2599. //text = "A sentence has words. ";
  2600. //breakLines = "";
  2601. //wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: true);
  2602. //foreach (var line in wrappedLines) {
  2603. // breakLines += $"{line}{Environment.NewLine}";
  2604. //}
  2605. //expected = "A " + Environment.NewLine +
  2606. // " se" + Environment.NewLine +
  2607. // " nt" + Environment.NewLine +
  2608. // " en" + Environment.NewLine +
  2609. // " ce" + Environment.NewLine +
  2610. // " " + Environment.NewLine +
  2611. // " " + Environment.NewLine +
  2612. // " " + Environment.NewLine +
  2613. // " ha" + Environment.NewLine +
  2614. // " s " + Environment.NewLine +
  2615. // " wo" + Environment.NewLine +
  2616. // " rd" + Environment.NewLine +
  2617. // " s." + Environment.NewLine;
  2618. //Assert.Equal (expected, breakLines);
  2619. }
  2620. [Theory]
  2621. [InlineData ("A sentence\t\t\t has words.", 14, -10, new [] { "A sentence\t", "\t\t has ", "words." })]
  2622. [InlineData (
  2623. "A sentence\t\t\t has words.",
  2624. 8,
  2625. -16,
  2626. new [] { "A ", "sentence", "\t\t", "\t ", "has ", "words." }
  2627. )]
  2628. [InlineData (
  2629. "A sentence\t\t\t has words.",
  2630. 3,
  2631. -21,
  2632. new [] { "A ", "sen", "ten", "ce", "\t", "\t", "\t", " ", "has", " ", "wor", "ds." }
  2633. )]
  2634. [InlineData (
  2635. "A sentence\t\t\t has words.",
  2636. 2,
  2637. -22,
  2638. new [] { "A ", "se", "nt", "en", "ce", "\t", "\t", "\t", " ", "ha", "s ", "wo", "rd", "s." }
  2639. )]
  2640. [InlineData (
  2641. "A sentence\t\t\t has words.",
  2642. 1,
  2643. -23,
  2644. new []
  2645. {
  2646. "A",
  2647. " ",
  2648. "s",
  2649. "e",
  2650. "n",
  2651. "t",
  2652. "e",
  2653. "n",
  2654. "c",
  2655. "e",
  2656. "\t",
  2657. "\t",
  2658. "\t",
  2659. " ",
  2660. "h",
  2661. "a",
  2662. "s",
  2663. " ",
  2664. "w",
  2665. "o",
  2666. "r",
  2667. "d",
  2668. "s",
  2669. "."
  2670. }
  2671. )]
  2672. public void WordWrap_PreserveTrailingSpaces_True_With_Tab (
  2673. string text,
  2674. int maxWidth,
  2675. int widthOffset,
  2676. IEnumerable<string> resultLines,
  2677. int tabWidth = 4
  2678. )
  2679. {
  2680. List<string> wrappedLines;
  2681. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2682. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2683. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true, tabWidth);
  2684. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2685. Assert.True (
  2686. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2687. );
  2688. Assert.True (
  2689. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2690. );
  2691. Assert.Equal (resultLines, wrappedLines);
  2692. }
  2693. [Theory]
  2694. [InlineData ("Constantinople", 14, 0, new [] { "Constantinople" })]
  2695. [InlineData ("Constantinople", 12, -2, new [] { "Constantinop", "le" })]
  2696. [InlineData ("Constantinople", 9, -5, new [] { "Constanti", "nople" })]
  2697. [InlineData ("Constantinople", 7, -7, new [] { "Constan", "tinople" })]
  2698. [InlineData ("Constantinople", 5, -9, new [] { "Const", "antin", "ople" })]
  2699. [InlineData ("Constantinople", 4, -10, new [] { "Cons", "tant", "inop", "le" })]
  2700. [InlineData (
  2701. "Constantinople",
  2702. 1,
  2703. -13,
  2704. new [] { "C", "o", "n", "s", "t", "a", "n", "t", "i", "n", "o", "p", "l", "e" }
  2705. )]
  2706. public void WordWrap_SingleWordLine (
  2707. string text,
  2708. int maxWidth,
  2709. int widthOffset,
  2710. IEnumerable<string> resultLines
  2711. )
  2712. {
  2713. List<string> wrappedLines;
  2714. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2715. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2716. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2717. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2718. Assert.True (
  2719. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2720. );
  2721. Assert.True (
  2722. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2723. );
  2724. Assert.Equal (resultLines, wrappedLines);
  2725. }
  2726. [Theory]
  2727. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 20, 0, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2728. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 19, -1, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2729. [InlineData (
  2730. "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.",
  2731. 19,
  2732. 0,
  2733. new [] { "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence." }
  2734. )]
  2735. public void WordWrap_Unicode_2LinesWithNonBreakingSpace (
  2736. string text,
  2737. int maxWidth,
  2738. int widthOffset,
  2739. IEnumerable<string> resultLines
  2740. )
  2741. {
  2742. List<string> wrappedLines;
  2743. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2744. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2745. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2746. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2747. Assert.True (
  2748. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2749. );
  2750. Assert.True (
  2751. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2752. );
  2753. Assert.Equal (resultLines, wrappedLines);
  2754. }
  2755. [Theory]
  2756. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 19, 0, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2757. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 18, -1, new [] { "This\u00A0is\u00A0a\u00A0sentence", "." })]
  2758. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 17, -2, new [] { "This\u00A0is\u00A0a\u00A0sentenc", "e." })]
  2759. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 14, -5, new [] { "This\u00A0is\u00A0a\u00A0sent", "ence." })]
  2760. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 10, -9, new [] { "This\u00A0is\u00A0a\u00A0", "sentence." })]
  2761. [InlineData (
  2762. "This\u00A0is\u00A0a\u00A0sentence.",
  2763. 7,
  2764. -12,
  2765. new [] { "This\u00A0is", "\u00A0a\u00A0sent", "ence." }
  2766. )]
  2767. [InlineData (
  2768. "This\u00A0is\u00A0a\u00A0sentence.",
  2769. 5,
  2770. -14,
  2771. new [] { "This\u00A0", "is\u00A0a\u00A0", "sente", "nce." }
  2772. )]
  2773. [InlineData (
  2774. "This\u00A0is\u00A0a\u00A0sentence.",
  2775. 1,
  2776. -18,
  2777. new []
  2778. {
  2779. "T", "h", "i", "s", "\u00A0", "i", "s", "\u00A0", "a", "\u00A0", "s", "e", "n", "t", "e", "n", "c", "e", "."
  2780. }
  2781. )]
  2782. public void WordWrap_Unicode_LineWithNonBreakingSpace (
  2783. string text,
  2784. int maxWidth,
  2785. int widthOffset,
  2786. IEnumerable<string> resultLines
  2787. )
  2788. {
  2789. List<string> wrappedLines;
  2790. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2791. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2792. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2793. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2794. Assert.True (
  2795. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2796. );
  2797. Assert.True (
  2798. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2799. );
  2800. Assert.Equal (resultLines, wrappedLines);
  2801. }
  2802. [Theory]
  2803. [InlineData (
  2804. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2805. 51,
  2806. 0,
  2807. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2808. )]
  2809. [InlineData (
  2810. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2811. 50,
  2812. -1,
  2813. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2814. )]
  2815. [InlineData (
  2816. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2817. 46,
  2818. -5,
  2819. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮ", "ฯะัาำ" }
  2820. )]
  2821. [InlineData (
  2822. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2823. 26,
  2824. -25,
  2825. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2826. )]
  2827. [InlineData (
  2828. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2829. 17,
  2830. -34,
  2831. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑ", "ฒณดตถทธนบปผฝพฟภมย", "รฤลฦวศษสหฬอฮฯะัาำ" }
  2832. )]
  2833. [InlineData (
  2834. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2835. 13,
  2836. -38,
  2837. new [] { "กขฃคฅฆงจฉชซฌญ", "ฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦว", "ศษสหฬอฮฯะัาำ" }
  2838. )]
  2839. [InlineData (
  2840. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2841. 1,
  2842. -50,
  2843. new []
  2844. {
  2845. "ก",
  2846. "ข",
  2847. "ฃ",
  2848. "ค",
  2849. "ฅ",
  2850. "ฆ",
  2851. "ง",
  2852. "จ",
  2853. "ฉ",
  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. public void WordWrap_Unicode_SingleWordLine (
  2898. string text,
  2899. int maxWidth,
  2900. int widthOffset,
  2901. IEnumerable<string> resultLines
  2902. )
  2903. {
  2904. List<string> wrappedLines;
  2905. IEnumerable<Rune> zeroWidth = text.EnumerateRunes ().Where (r => r.GetColumns () == 0);
  2906. Assert.Single (zeroWidth);
  2907. Assert.Equal ('ั', zeroWidth.ElementAt (0).Value);
  2908. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2909. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2910. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2911. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2912. Assert.True (
  2913. expectedClippedWidth
  2914. >= (wrappedLines.Count > 0
  2915. ? wrappedLines.Max (
  2916. l => l.GetRuneCount ()
  2917. + zeroWidth.Count ()
  2918. - 1
  2919. + widthOffset
  2920. )
  2921. : 0)
  2922. );
  2923. Assert.True (
  2924. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2925. );
  2926. Assert.Equal (resultLines, wrappedLines);
  2927. }
  2928. /// <summary>WordWrap strips CRLF</summary>
  2929. [Theory]
  2930. [InlineData (
  2931. "A sentence has words.\nA paragraph has lines.",
  2932. 44,
  2933. 0,
  2934. new [] { "A sentence has words.A paragraph has lines." }
  2935. )]
  2936. [InlineData (
  2937. "A sentence has words.\nA paragraph has lines.",
  2938. 43,
  2939. -1,
  2940. new [] { "A sentence has words.A paragraph has lines." }
  2941. )]
  2942. [InlineData (
  2943. "A sentence has words.\nA paragraph has lines.",
  2944. 38,
  2945. -6,
  2946. new [] { "A sentence has words.A paragraph has", "lines." }
  2947. )]
  2948. [InlineData (
  2949. "A sentence has words.\nA paragraph has lines.",
  2950. 34,
  2951. -10,
  2952. new [] { "A sentence has words.A paragraph", "has lines." }
  2953. )]
  2954. [InlineData (
  2955. "A sentence has words.\nA paragraph has lines.",
  2956. 27,
  2957. -17,
  2958. new [] { "A sentence has words.A", "paragraph has lines." }
  2959. )]
  2960. // Unicode
  2961. [InlineData (
  2962. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2963. 69,
  2964. 0,
  2965. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." }
  2966. )]
  2967. [InlineData (
  2968. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2969. 68,
  2970. -1,
  2971. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." }
  2972. )]
  2973. [InlineData (
  2974. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2975. 63,
  2976. -6,
  2977. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", "Линии." }
  2978. )]
  2979. [InlineData (
  2980. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2981. 59,
  2982. -10,
  2983. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", "has Линии." }
  2984. )]
  2985. [InlineData (
  2986. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2987. 52,
  2988. -17,
  2989. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode", "Пункт has Линии." }
  2990. )]
  2991. public void WordWrap_WithNewLines (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  2992. {
  2993. List<string> wrappedLines;
  2994. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2995. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2996. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2997. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2998. Assert.True (
  2999. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  3000. );
  3001. Assert.True (
  3002. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  3003. );
  3004. Assert.Equal (resultLines, wrappedLines);
  3005. }
  3006. }