TextFormatterTests.cs 133 KB

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