TextFormatterTests.cs 126 KB

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