TextFormatterTests.cs 126 KB

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