TextFormatterTests.cs 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. using UnitTests;
  4. // Alias Console to MockConsole so we don't accidentally use Console
  5. namespace Terminal.Gui.TextTests;
  6. public class TextFormatterTests
  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. List<Rune> runes = text.ToRuneList ();
  716. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (runes, columns));
  717. }
  718. [Theory]
  719. [InlineData ("test", 3, 3)]
  720. [InlineData ("test", 4, 4)]
  721. [InlineData ("test", 10, 4)]
  722. public void GetLengthThatFits_Runelist (string text, int columns, int expectedLength)
  723. {
  724. List<Rune> runes = text.ToRuneList ();
  725. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (runes, columns));
  726. }
  727. [Theory]
  728. [InlineData ("Hello World", 6, 6)]
  729. [InlineData ("こんにちは 世界", 6, 3)]
  730. public void GetLengthThatFits_Simple_And_Wide_Runes (string text, int columns, int expectedLength)
  731. {
  732. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  733. }
  734. [Theory]
  735. [InlineData ("test", 3, 3)]
  736. [InlineData ("test", 4, 4)]
  737. [InlineData ("test", 10, 4)]
  738. [InlineData ("test", 1, 1)]
  739. [InlineData ("test", 0, 0)]
  740. [InlineData ("test", -1, 0)]
  741. [InlineData (null, -1, 0)]
  742. [InlineData ("", -1, 0)]
  743. public void GetLengthThatFits_String (string text, int columns, int expectedLength)
  744. {
  745. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  746. }
  747. [Fact]
  748. public void GetLengthThatFits_With_Combining_Runes ()
  749. {
  750. var text = "Les Mise\u0328\u0301rables";
  751. Assert.Equal (16, TextFormatter.GetLengthThatFits (text, 14));
  752. }
  753. [Fact]
  754. public void GetMaxColsForWidth_With_Combining_Runes ()
  755. {
  756. List<string> text = new () { "Les Mis", "e\u0328\u0301", "rables" };
  757. Assert.Equal (1, TextFormatter.GetMaxColsForWidth (text, 1));
  758. }
  759. //[Fact]
  760. //public void GetWidestLineLength_With_Combining_Runes ()
  761. //{
  762. // var text = "Les Mise\u0328\u0301rables";
  763. // Assert.Equal (1, TextFormatter.GetWidestLineLength (text, 1, 1));
  764. //}
  765. [Fact]
  766. public void Internal_Tests ()
  767. {
  768. var tf = new TextFormatter ();
  769. Assert.Equal (KeyCode.Null, tf.HotKey);
  770. tf.HotKey = KeyCode.CtrlMask | KeyCode.Q;
  771. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, tf.HotKey);
  772. }
  773. [Theory]
  774. [InlineData ("")]
  775. [InlineData (null)]
  776. [InlineData ("test")]
  777. public void Justify_Invalid (string text)
  778. {
  779. Assert.Equal (text, TextFormatter.Justify (text, 0));
  780. Assert.Equal (text, TextFormatter.Justify (text, 0));
  781. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  782. }
  783. [Theory]
  784. // Even # of spaces
  785. // 0123456789
  786. [InlineData ("012 456 89", "012 456 89", 10, 0, "+", true)]
  787. [InlineData ("012 456 89", "012++456+89", 11, 1)]
  788. [InlineData ("012 456 89", "012 456 89", 12, 2, "++", true)]
  789. [InlineData ("012 456 89", "012+++456++89", 13, 3)]
  790. [InlineData ("012 456 89", "012 456 89", 14, 4, "+++", true)]
  791. [InlineData ("012 456 89", "012++++456+++89", 15, 5)]
  792. [InlineData ("012 456 89", "012 456 89", 16, 6, "++++", true)]
  793. [InlineData ("012 456 89", "012 456 89", 30, 20, "+++++++++++", true)]
  794. [InlineData ("012 456 89", "012+++++++++++++456++++++++++++89", 33, 23)]
  795. // Odd # of spaces
  796. // 01234567890123
  797. [InlineData ("012 456 89 end", "012 456 89 end", 14, 0, "+", true)]
  798. [InlineData ("012 456 89 end", "012++456+89+end", 15, 1)]
  799. [InlineData ("012 456 89 end", "012++456++89+end", 16, 2)]
  800. [InlineData ("012 456 89 end", "012 456 89 end", 17, 3, "++", true)]
  801. [InlineData ("012 456 89 end", "012+++456++89++end", 18, 4)]
  802. [InlineData ("012 456 89 end", "012+++456+++89++end", 19, 5)]
  803. [InlineData ("012 456 89 end", "012 456 89 end", 20, 6, "+++", true)]
  804. [InlineData ("012 456 89 end", "012++++++++456++++++++89+++++++end", 34, 20)]
  805. [InlineData ("012 456 89 end", "012+++++++++456+++++++++89++++++++end", 37, 23)]
  806. // Unicode
  807. // Even # of chars
  808. // 0123456789
  809. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 10, 0, "+", true)]
  810. [InlineData ("пÑРвРÑ", "пÑÐ++вÐ+Ñ", 11, 1)]
  811. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 12, 2, "++", true)]
  812. [InlineData ("пÑРвРÑ", "пÑÐ+++вÐ++Ñ", 13, 3)]
  813. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 14, 4, "+++", true)]
  814. [InlineData ("пÑРвРÑ", "пÑÐ++++вÐ+++Ñ", 15, 5)]
  815. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 16, 6, "++++", true)]
  816. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 30, 20, "+++++++++++", true)]
  817. [InlineData ("пÑРвРÑ", "пÑÐ+++++++++++++вÐ++++++++++++Ñ", 33, 23)]
  818. // Unicode
  819. // Odd # of chars
  820. // 0123456789
  821. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 10, 0, "+", true)]
  822. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ+вÐ+Ñ", 11, 1)]
  823. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ++вÐ+Ñ", 12, 2)]
  824. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 13, 3, "++", true)]
  825. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ++вÐ++Ñ", 14, 4)]
  826. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ+++вÐ++Ñ", 15, 5)]
  827. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 16, 6, "+++", true)]
  828. [InlineData ("Ð ÑРвРÑ", "Ð++++++++ÑÐ++++++++вÐ+++++++Ñ", 30, 20)]
  829. [InlineData ("Ð ÑРвРÑ", "Ð+++++++++ÑÐ+++++++++вÐ++++++++Ñ", 33, 23)]
  830. public void Justify_Sentence (
  831. string text,
  832. string justifiedText,
  833. int forceToWidth,
  834. int widthOffset,
  835. string replaceWith = null,
  836. bool replace = false
  837. )
  838. {
  839. var fillChar = '+';
  840. Assert.Equal (forceToWidth, text.GetRuneCount () + widthOffset);
  841. if (replace)
  842. {
  843. justifiedText = text.Replace (" ", replaceWith);
  844. }
  845. Assert.Equal (justifiedText, TextFormatter.Justify (text, forceToWidth, fillChar));
  846. Assert.True (Math.Abs (forceToWidth - justifiedText.GetRuneCount ()) < text.Count (s => s == ' '));
  847. Assert.True (Math.Abs (forceToWidth - justifiedText.GetColumns ()) < text.Count (s => s == ' '));
  848. }
  849. [Theory]
  850. [InlineData ("word")] // Even # of chars
  851. [InlineData ("word.")] // Odd # of chars
  852. [InlineData ("пÑивеÑ")] // Unicode (even #)
  853. [InlineData ("пÑивеÑ.")] // Unicode (odd # of chars)
  854. public void Justify_SingleWord (string text)
  855. {
  856. string justifiedText = text;
  857. var fillChar = '+';
  858. int width = text.GetRuneCount ();
  859. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  860. width = text.GetRuneCount () + 1;
  861. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  862. width = text.GetRuneCount () + 2;
  863. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  864. width = text.GetRuneCount () + 10;
  865. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  866. width = text.GetRuneCount () + 11;
  867. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  868. }
  869. [Theory]
  870. [InlineData ("Single Line 界", 14)]
  871. [InlineData ("First Line 界\nSecond Line 界\nThird Line 界\n", 14)]
  872. public void MaxWidthLine_With_And_Without_Newlines (string text, int expected) { Assert.Equal (expected, TextFormatter.GetWidestLineLength (text)); }
  873. [Theory]
  874. [InlineData (
  875. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  876. 0,
  877. 0,
  878. false,
  879. new [] { "" }
  880. )]
  881. [InlineData (
  882. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  883. 0,
  884. 1,
  885. false,
  886. new [] { "" }
  887. )]
  888. [InlineData (
  889. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  890. 1,
  891. 0,
  892. false,
  893. new [] { "" }
  894. )]
  895. [InlineData (
  896. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  897. 0,
  898. 0,
  899. true,
  900. new [] { "" }
  901. )]
  902. [InlineData (
  903. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  904. 0,
  905. 1,
  906. true,
  907. new [] { "" }
  908. )]
  909. [InlineData (
  910. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  911. 1,
  912. 0,
  913. true,
  914. new [] { "" }
  915. )]
  916. [InlineData (
  917. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  918. 6,
  919. 5,
  920. false,
  921. new [] { "First " }
  922. )]
  923. [InlineData ("1\n2\n3\n4\n5\n6", 6, 5, false, new [] { "1 2 3 " })]
  924. [InlineData (
  925. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  926. 6,
  927. 5,
  928. true,
  929. new [] { "First ", "Second", "Third ", "Forty ", "Fiftee" }
  930. )]
  931. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, false, new [] { "第一" })]
  932. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, true, new [] { "第一", "第二", "第三", "四十", "第十" })]
  933. public void MultiLine_WordWrap_False_Horizontal_Direction (
  934. string text,
  935. int maxWidth,
  936. int maxHeight,
  937. bool multiLine,
  938. IEnumerable<string> resultLines
  939. )
  940. {
  941. var tf = new TextFormatter
  942. {
  943. Text = text, ConstrainToSize = new (maxWidth, maxHeight), WordWrap = false, MultiLine = multiLine
  944. };
  945. Assert.False (tf.WordWrap);
  946. Assert.True (tf.MultiLine == multiLine);
  947. Assert.Equal (TextDirection.LeftRight_TopBottom, tf.Direction);
  948. List<string> splitLines = tf.GetLines ();
  949. Assert.Equal (splitLines.Count, resultLines.Count ());
  950. Assert.Equal (splitLines, resultLines);
  951. }
  952. [Theory]
  953. [InlineData (
  954. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  955. 0,
  956. 0,
  957. false,
  958. new [] { "" }
  959. )]
  960. [InlineData (
  961. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  962. 0,
  963. 1,
  964. false,
  965. new [] { "" }
  966. )]
  967. [InlineData (
  968. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  969. 1,
  970. 0,
  971. false,
  972. new [] { "" }
  973. )]
  974. [InlineData (
  975. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  976. 0,
  977. 0,
  978. true,
  979. new [] { "" }
  980. )]
  981. [InlineData (
  982. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  983. 0,
  984. 1,
  985. true,
  986. new [] { "" }
  987. )]
  988. [InlineData (
  989. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  990. 1,
  991. 0,
  992. true,
  993. new [] { "" }
  994. )]
  995. [InlineData (
  996. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  997. 6,
  998. 5,
  999. false,
  1000. new [] { "First" }
  1001. )]
  1002. [InlineData ("1\n2\n3\n4\n5\n6", 6, 5, false, new [] { "1 2 3" })]
  1003. [InlineData (
  1004. "First Line\nSecond Line\nThird Line\nForty Line\nFifteenth Line\nSeventy Line",
  1005. 6,
  1006. 5,
  1007. true,
  1008. new [] { "First", "Secon", "Third", "Forty", "Fifte", "Seven" }
  1009. )]
  1010. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, false, new [] { "第一行 第" })]
  1011. [InlineData ("第一行\n第二行\n第三行\n四十行\n第十五行\n七十行", 5, 5, true, new [] { "第一行", "第二行" })]
  1012. public void MultiLine_WordWrap_False_Vertical_Direction (
  1013. string text,
  1014. int maxWidth,
  1015. int maxHeight,
  1016. bool multiLine,
  1017. IEnumerable<string> resultLines
  1018. )
  1019. {
  1020. var tf = new TextFormatter
  1021. {
  1022. Text = text,
  1023. ConstrainToSize = new (maxWidth, maxHeight),
  1024. WordWrap = false,
  1025. MultiLine = multiLine,
  1026. Direction = TextDirection.TopBottom_LeftRight
  1027. };
  1028. Assert.False (tf.WordWrap);
  1029. Assert.True (tf.MultiLine == multiLine);
  1030. Assert.Equal (TextDirection.TopBottom_LeftRight, tf.Direction);
  1031. List<string> splitLines = tf.GetLines ();
  1032. Assert.Equal (splitLines.Count, resultLines.Count ());
  1033. Assert.Equal (splitLines, resultLines);
  1034. }
  1035. [Fact]
  1036. public void NeedsFormat_Sets ()
  1037. {
  1038. var testText = "test";
  1039. var testBounds = new Rectangle (0, 0, 100, 1);
  1040. var tf = new TextFormatter ();
  1041. tf.Text = "test";
  1042. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  1043. Assert.NotEmpty (tf.GetLines ());
  1044. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1045. Assert.Equal (testText, tf.Text);
  1046. tf.Draw (testBounds, new (), new ());
  1047. Assert.False (tf.NeedsFormat);
  1048. tf.ConstrainToSize = new (1, 1);
  1049. Assert.True (tf.NeedsFormat);
  1050. Assert.NotEmpty (tf.GetLines ());
  1051. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1052. tf.Alignment = Alignment.Center;
  1053. Assert.True (tf.NeedsFormat);
  1054. Assert.NotEmpty (tf.GetLines ());
  1055. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  1056. }
  1057. // Test that changing TextFormatter does not impact View dimensions if Dim.Auto is not in play
  1058. [Fact]
  1059. public void Not_Used_TextFormatter_Does_Not_Change_View_Size ()
  1060. {
  1061. View view = new ()
  1062. {
  1063. Text = "_1234"
  1064. };
  1065. Assert.Equal (Size.Empty, view.Frame.Size);
  1066. view.TextFormatter.Text = "ABC";
  1067. Assert.Equal (Size.Empty, view.Frame.Size);
  1068. view.TextFormatter.Alignment = Alignment.Fill;
  1069. Assert.Equal (Size.Empty, view.Frame.Size);
  1070. view.TextFormatter.VerticalAlignment = Alignment.Center;
  1071. Assert.Equal (Size.Empty, view.Frame.Size);
  1072. view.TextFormatter.HotKeySpecifier = (Rune)'*';
  1073. Assert.Equal (Size.Empty, view.Frame.Size);
  1074. view.TextFormatter.Text = "*ABC";
  1075. Assert.Equal (Size.Empty, view.Frame.Size);
  1076. }
  1077. [Fact]
  1078. public void Not_Used_TextSettings_Do_Not_Change_View_Size ()
  1079. {
  1080. View view = new ()
  1081. {
  1082. Text = "_1234"
  1083. };
  1084. Assert.Equal (Size.Empty, view.Frame.Size);
  1085. view.TextAlignment = Alignment.Fill;
  1086. Assert.Equal (Size.Empty, view.Frame.Size);
  1087. view.VerticalTextAlignment = Alignment.Center;
  1088. Assert.Equal (Size.Empty, view.Frame.Size);
  1089. view.HotKeySpecifier = (Rune)'*';
  1090. Assert.Equal (Size.Empty, view.Frame.Size);
  1091. view.Text = "*ABC";
  1092. Assert.Equal (Size.Empty, view.Frame.Size);
  1093. }
  1094. [Theory]
  1095. [InlineData ("", -1, Alignment.Start, false, 0)]
  1096. [InlineData (null, 0, Alignment.Start, false, 1)]
  1097. [InlineData (null, 0, Alignment.Start, true, 1)]
  1098. [InlineData ("", 0, Alignment.Start, false, 1)]
  1099. [InlineData ("", 0, Alignment.Start, true, 1)]
  1100. public void Reformat_Invalid (string text, int maxWidth, Alignment alignment, bool wrap, int linesCount)
  1101. {
  1102. if (maxWidth < 0)
  1103. {
  1104. Assert.Throws<ArgumentOutOfRangeException> (
  1105. () =>
  1106. TextFormatter.Format (text, maxWidth, alignment, wrap)
  1107. );
  1108. }
  1109. else
  1110. {
  1111. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap);
  1112. Assert.NotEmpty (list);
  1113. Assert.True (list.Count == linesCount);
  1114. Assert.Equal (string.Empty, list [0]);
  1115. }
  1116. }
  1117. [Theory]
  1118. [InlineData ("A sentence has words.\nLine 2.", 0, -29, Alignment.Start, false, 1, true)]
  1119. [InlineData ("A sentence has words.\nLine 2.", 1, -28, Alignment.Start, false, 1, false)]
  1120. [InlineData ("A sentence has words.\nLine 2.", 5, -24, Alignment.Start, false, 1, false)]
  1121. [InlineData ("A sentence has words.\nLine 2.", 28, -1, Alignment.Start, false, 1, false)]
  1122. // no clip
  1123. [InlineData ("A sentence has words.\nLine 2.", 29, 0, Alignment.Start, false, 1, false)]
  1124. [InlineData ("A sentence has words.\nLine 2.", 30, 1, Alignment.Start, false, 1, false)]
  1125. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, Alignment.Start, false, 1, true)]
  1126. [InlineData ("A sentence has words.\r\nLine 2.", 1, -29, Alignment.Start, false, 1, false)]
  1127. [InlineData ("A sentence has words.\r\nLine 2.", 5, -25, Alignment.Start, false, 1, false)]
  1128. [InlineData ("A sentence has words.\r\nLine 2.", 29, -1, Alignment.Start, false, 1, false, 1)]
  1129. [InlineData ("A sentence has words.\r\nLine 2.", 30, 0, Alignment.Start, false, 1, false)]
  1130. [InlineData ("A sentence has words.\r\nLine 2.", 31, 1, Alignment.Start, false, 1, false)]
  1131. public void Reformat_NoWordrap_NewLines_MultiLine_False (
  1132. string text,
  1133. int maxWidth,
  1134. int widthOffset,
  1135. Alignment alignment,
  1136. bool wrap,
  1137. int linesCount,
  1138. bool stringEmpty,
  1139. int clipWidthOffset = 0
  1140. )
  1141. {
  1142. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1143. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth) + clipWidthOffset;
  1144. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap);
  1145. Assert.NotEmpty (list);
  1146. Assert.True (list.Count == linesCount);
  1147. if (stringEmpty)
  1148. {
  1149. Assert.Equal (string.Empty, list [0]);
  1150. }
  1151. else
  1152. {
  1153. Assert.NotEqual (string.Empty, list [0]);
  1154. }
  1155. if (text.Contains ("\r\n") && maxWidth > 0)
  1156. {
  1157. Assert.Equal (
  1158. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth])
  1159. .Replace ("\r\n", " "),
  1160. list [0]
  1161. );
  1162. }
  1163. else if (text.Contains ('\n') && maxWidth > 0)
  1164. {
  1165. Assert.Equal (
  1166. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth])
  1167. .Replace ("\n", " "),
  1168. list [0]
  1169. );
  1170. }
  1171. else
  1172. {
  1173. Assert.Equal (StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]), list [0]);
  1174. }
  1175. }
  1176. [Theory]
  1177. [InlineData ("A sentence has words.\nLine 2.", 0, -29, Alignment.Start, false, 1, true, new [] { "" })]
  1178. [InlineData (
  1179. "A sentence has words.\nLine 2.",
  1180. 1,
  1181. -28,
  1182. Alignment.Start,
  1183. false,
  1184. 2,
  1185. false,
  1186. new [] { "A", "L" }
  1187. )]
  1188. [InlineData (
  1189. "A sentence has words.\nLine 2.",
  1190. 5,
  1191. -24,
  1192. Alignment.Start,
  1193. false,
  1194. 2,
  1195. false,
  1196. new [] { "A sen", "Line " }
  1197. )]
  1198. [InlineData (
  1199. "A sentence has words.\nLine 2.",
  1200. 28,
  1201. -1,
  1202. Alignment.Start,
  1203. false,
  1204. 2,
  1205. false,
  1206. new [] { "A sentence has words.", "Line 2." }
  1207. )]
  1208. //// no clip
  1209. [InlineData (
  1210. "A sentence has words.\nLine 2.",
  1211. 29,
  1212. 0,
  1213. Alignment.Start,
  1214. false,
  1215. 2,
  1216. false,
  1217. new [] { "A sentence has words.", "Line 2." }
  1218. )]
  1219. [InlineData (
  1220. "A sentence has words.\nLine 2.",
  1221. 30,
  1222. 1,
  1223. Alignment.Start,
  1224. false,
  1225. 2,
  1226. false,
  1227. new [] { "A sentence has words.", "Line 2." }
  1228. )]
  1229. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, Alignment.Start, false, 1, true, new [] { "" })]
  1230. [InlineData (
  1231. "A sentence has words.\r\nLine 2.",
  1232. 1,
  1233. -29,
  1234. Alignment.Start,
  1235. false,
  1236. 2,
  1237. false,
  1238. new [] { "A", "L" }
  1239. )]
  1240. [InlineData (
  1241. "A sentence has words.\r\nLine 2.",
  1242. 5,
  1243. -25,
  1244. Alignment.Start,
  1245. false,
  1246. 2,
  1247. false,
  1248. new [] { "A sen", "Line " }
  1249. )]
  1250. [InlineData (
  1251. "A sentence has words.\r\nLine 2.",
  1252. 29,
  1253. -1,
  1254. Alignment.Start,
  1255. false,
  1256. 2,
  1257. false,
  1258. new [] { "A sentence has words.", "Line 2." }
  1259. )]
  1260. [InlineData (
  1261. "A sentence has words.\r\nLine 2.",
  1262. 30,
  1263. 0,
  1264. Alignment.Start,
  1265. false,
  1266. 2,
  1267. false,
  1268. new [] { "A sentence has words.", "Line 2." }
  1269. )]
  1270. [InlineData (
  1271. "A sentence has words.\r\nLine 2.",
  1272. 31,
  1273. 1,
  1274. Alignment.Start,
  1275. false,
  1276. 2,
  1277. false,
  1278. new [] { "A sentence has words.", "Line 2." }
  1279. )]
  1280. public void Reformat_NoWordrap_NewLines_MultiLine_True (
  1281. string text,
  1282. int maxWidth,
  1283. int widthOffset,
  1284. Alignment alignment,
  1285. bool wrap,
  1286. int linesCount,
  1287. bool stringEmpty,
  1288. IEnumerable<string> resultLines
  1289. )
  1290. {
  1291. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1292. List<string> list = TextFormatter.Format (
  1293. text,
  1294. maxWidth,
  1295. alignment,
  1296. wrap,
  1297. false,
  1298. 0,
  1299. TextDirection.LeftRight_TopBottom,
  1300. true
  1301. );
  1302. Assert.NotEmpty (list);
  1303. Assert.True (list.Count == linesCount);
  1304. if (stringEmpty)
  1305. {
  1306. Assert.Equal (string.Empty, list [0]);
  1307. }
  1308. else
  1309. {
  1310. Assert.NotEqual (string.Empty, list [0]);
  1311. }
  1312. Assert.Equal (list, resultLines);
  1313. }
  1314. [Theory]
  1315. [InlineData ("A sentence has words.\nLine 2.", 0, -29, Alignment.Start, false, 1, true, new [] { "" })]
  1316. [InlineData (
  1317. "A sentence has words.\nLine 2.",
  1318. 1,
  1319. -28,
  1320. Alignment.Start,
  1321. false,
  1322. 2,
  1323. false,
  1324. new [] { "A", "L" }
  1325. )]
  1326. [InlineData (
  1327. "A sentence has words.\nLine 2.",
  1328. 5,
  1329. -24,
  1330. Alignment.Start,
  1331. false,
  1332. 2,
  1333. false,
  1334. new [] { "A sen", "Line " }
  1335. )]
  1336. [InlineData (
  1337. "A sentence has words.\nLine 2.",
  1338. 28,
  1339. -1,
  1340. Alignment.Start,
  1341. false,
  1342. 2,
  1343. false,
  1344. new [] { "A sentence has words.", "Line 2." }
  1345. )]
  1346. //// no clip
  1347. [InlineData (
  1348. "A sentence has words.\nLine 2.",
  1349. 29,
  1350. 0,
  1351. Alignment.Start,
  1352. false,
  1353. 2,
  1354. false,
  1355. new [] { "A sentence has words.", "Line 2." }
  1356. )]
  1357. [InlineData (
  1358. "A sentence has words.\nLine 2.",
  1359. 30,
  1360. 1,
  1361. Alignment.Start,
  1362. false,
  1363. 2,
  1364. false,
  1365. new [] { "A sentence has words.", "Line 2." }
  1366. )]
  1367. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, Alignment.Start, false, 1, true, new [] { "" })]
  1368. [InlineData (
  1369. "A sentence has words.\r\nLine 2.",
  1370. 1,
  1371. -29,
  1372. Alignment.Start,
  1373. false,
  1374. 2,
  1375. false,
  1376. new [] { "A", "L" }
  1377. )]
  1378. [InlineData (
  1379. "A sentence has words.\r\nLine 2.",
  1380. 5,
  1381. -25,
  1382. Alignment.Start,
  1383. false,
  1384. 2,
  1385. false,
  1386. new [] { "A sen", "Line " }
  1387. )]
  1388. [InlineData (
  1389. "A sentence has words.\r\nLine 2.",
  1390. 29,
  1391. -1,
  1392. Alignment.Start,
  1393. false,
  1394. 2,
  1395. false,
  1396. new [] { "A sentence has words.", "Line 2." }
  1397. )]
  1398. [InlineData (
  1399. "A sentence has words.\r\nLine 2.",
  1400. 30,
  1401. 0,
  1402. Alignment.Start,
  1403. false,
  1404. 2,
  1405. false,
  1406. new [] { "A sentence has words.", "Line 2." }
  1407. )]
  1408. [InlineData (
  1409. "A sentence has words.\r\nLine 2.",
  1410. 31,
  1411. 1,
  1412. Alignment.Start,
  1413. false,
  1414. 2,
  1415. false,
  1416. new [] { "A sentence has words.", "Line 2." }
  1417. )]
  1418. public void Reformat_NoWordrap_NewLines_MultiLine_True_Vertical (
  1419. string text,
  1420. int maxWidth,
  1421. int widthOffset,
  1422. Alignment alignment,
  1423. bool wrap,
  1424. int linesCount,
  1425. bool stringEmpty,
  1426. IEnumerable<string> resultLines
  1427. )
  1428. {
  1429. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1430. List<string> list = TextFormatter.Format (
  1431. text,
  1432. maxWidth,
  1433. alignment,
  1434. wrap,
  1435. false,
  1436. 0,
  1437. TextDirection.TopBottom_LeftRight,
  1438. true
  1439. );
  1440. Assert.NotEmpty (list);
  1441. Assert.True (list.Count == linesCount);
  1442. if (stringEmpty)
  1443. {
  1444. Assert.Equal (string.Empty, list [0]);
  1445. }
  1446. else
  1447. {
  1448. Assert.NotEqual (string.Empty, list [0]);
  1449. }
  1450. Assert.Equal (list, resultLines);
  1451. }
  1452. [Theory]
  1453. [InlineData ("", 0, 0, Alignment.Start, false, 1, true)]
  1454. [InlineData ("", 1, 1, Alignment.Start, false, 1, true)]
  1455. [InlineData ("A sentence has words.", 0, -21, Alignment.Start, false, 1, true)]
  1456. [InlineData ("A sentence has words.", 1, -20, Alignment.Start, false, 1, false)]
  1457. [InlineData ("A sentence has words.", 5, -16, Alignment.Start, false, 1, false)]
  1458. [InlineData ("A sentence has words.", 20, -1, Alignment.Start, false, 1, false)]
  1459. // no clip
  1460. [InlineData ("A sentence has words.", 21, 0, Alignment.Start, false, 1, false)]
  1461. [InlineData ("A sentence has words.", 22, 1, Alignment.Start, false, 1, false)]
  1462. public void Reformat_NoWordrap_SingleLine (
  1463. string text,
  1464. int maxWidth,
  1465. int widthOffset,
  1466. Alignment alignment,
  1467. bool wrap,
  1468. int linesCount,
  1469. bool stringEmpty
  1470. )
  1471. {
  1472. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1473. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1474. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap);
  1475. Assert.NotEmpty (list);
  1476. Assert.True (list.Count == linesCount);
  1477. if (stringEmpty)
  1478. {
  1479. Assert.Equal (string.Empty, list [0]);
  1480. }
  1481. else
  1482. {
  1483. Assert.NotEqual (string.Empty, list [0]);
  1484. }
  1485. Assert.Equal (StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]), list [0]);
  1486. }
  1487. [Theory]
  1488. // Unicode
  1489. [InlineData (
  1490. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1491. 8,
  1492. -1,
  1493. Alignment.Start,
  1494. true,
  1495. false,
  1496. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1497. )]
  1498. // no clip
  1499. [InlineData (
  1500. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1501. 9,
  1502. 0,
  1503. Alignment.Start,
  1504. true,
  1505. false,
  1506. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1507. )]
  1508. [InlineData (
  1509. "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464",
  1510. 10,
  1511. 1,
  1512. Alignment.Start,
  1513. true,
  1514. false,
  1515. new [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" }
  1516. )]
  1517. public void Reformat_Unicode_Wrap_Spaces_NewLines (
  1518. string text,
  1519. int maxWidth,
  1520. int widthOffset,
  1521. Alignment alignment,
  1522. bool wrap,
  1523. bool preserveTrailingSpaces,
  1524. IEnumerable<string> resultLines
  1525. )
  1526. {
  1527. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1528. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap, preserveTrailingSpaces);
  1529. Assert.Equal (list.Count, resultLines.Count ());
  1530. Assert.Equal (resultLines, list);
  1531. }
  1532. [Theory]
  1533. // Unicode
  1534. // Even # of chars
  1535. // 0123456789
  1536. [InlineData ("\u2660пÑРвРÑ", 10, -1, Alignment.Start, true, false, new [] { "\u2660пÑРвÐ", "Ñ" })]
  1537. // no clip
  1538. [InlineData ("\u2660пÑРвРÑ", 11, 0, Alignment.Start, true, false, new [] { "\u2660пÑРвРÑ" })]
  1539. [InlineData ("\u2660пÑРвРÑ", 12, 1, Alignment.Start, true, false, new [] { "\u2660пÑРвРÑ" })]
  1540. // Unicode
  1541. // Odd # of chars
  1542. // 0123456789
  1543. [InlineData ("\u2660 ÑРвРÑ", 9, -1, Alignment.Start, true, false, new [] { "\u2660 ÑРвÐ", "Ñ" })]
  1544. // no clip
  1545. [InlineData ("\u2660 ÑРвРÑ", 10, 0, Alignment.Start, true, false, new [] { "\u2660 ÑРвРÑ" })]
  1546. [InlineData ("\u2660 ÑРвРÑ", 11, 1, Alignment.Start, true, false, new [] { "\u2660 ÑРвРÑ" })]
  1547. public void Reformat_Unicode_Wrap_Spaces_No_NewLines (
  1548. string text,
  1549. int maxWidth,
  1550. int widthOffset,
  1551. Alignment alignment,
  1552. bool wrap,
  1553. bool preserveTrailingSpaces,
  1554. IEnumerable<string> resultLines
  1555. )
  1556. {
  1557. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1558. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap, preserveTrailingSpaces);
  1559. Assert.Equal (list.Count, resultLines.Count ());
  1560. Assert.Equal (resultLines, list);
  1561. }
  1562. [Theory]
  1563. // Even # of spaces
  1564. // 0123456789
  1565. [InlineData ("012 456 89", 0, -10, Alignment.Start, true, true, true, new [] { "" })]
  1566. [InlineData (
  1567. "012 456 89",
  1568. 1,
  1569. -9,
  1570. Alignment.Start,
  1571. true,
  1572. true,
  1573. false,
  1574. new [] { "0", "1", "2", " ", "4", "5", "6", " ", "8", "9" },
  1575. "01245689"
  1576. )]
  1577. [InlineData ("012 456 89", 5, -5, Alignment.Start, true, true, false, new [] { "012 ", "456 ", "89" })]
  1578. [InlineData ("012 456 89", 9, -1, Alignment.Start, true, true, false, new [] { "012 456 ", "89" })]
  1579. // no clip
  1580. [InlineData ("012 456 89", 10, 0, Alignment.Start, true, true, false, new [] { "012 456 89" })]
  1581. [InlineData ("012 456 89", 11, 1, Alignment.Start, true, true, false, new [] { "012 456 89" })]
  1582. // Odd # of spaces
  1583. // 01234567890123
  1584. [InlineData ("012 456 89 end", 13, -1, Alignment.Start, true, true, false, new [] { "012 456 89 ", "end" })]
  1585. // no clip
  1586. [InlineData ("012 456 89 end", 14, 0, Alignment.Start, true, true, false, new [] { "012 456 89 end" })]
  1587. [InlineData ("012 456 89 end", 15, 1, Alignment.Start, true, true, false, new [] { "012 456 89 end" })]
  1588. public void Reformat_Wrap_Spaces_No_NewLines (
  1589. string text,
  1590. int maxWidth,
  1591. int widthOffset,
  1592. Alignment alignment,
  1593. bool wrap,
  1594. bool preserveTrailingSpaces,
  1595. bool stringEmpty,
  1596. IEnumerable<string> resultLines,
  1597. string noSpaceText = ""
  1598. )
  1599. {
  1600. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1601. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1602. List<string> list = TextFormatter.Format (text, maxWidth, alignment, wrap, preserveTrailingSpaces);
  1603. Assert.NotEmpty (list);
  1604. Assert.True (list.Count == resultLines.Count ());
  1605. if (stringEmpty)
  1606. {
  1607. Assert.Equal (string.Empty, list [0]);
  1608. }
  1609. else
  1610. {
  1611. Assert.NotEqual (string.Empty, list [0]);
  1612. }
  1613. Assert.Equal (resultLines, list);
  1614. if (maxWidth > 0)
  1615. {
  1616. // remove whitespace chars
  1617. if (maxWidth < 5)
  1618. {
  1619. expectedClippedWidth = text.GetRuneCount () - text.Sum (r => r == ' ' ? 1 : 0);
  1620. }
  1621. else
  1622. {
  1623. expectedClippedWidth = Math.Min (
  1624. text.GetRuneCount (),
  1625. maxWidth - text.Sum (r => r == ' ' ? 1 : 0)
  1626. );
  1627. }
  1628. list = TextFormatter.Format (text, maxWidth, Alignment.Start, wrap);
  1629. if (maxWidth == 1)
  1630. {
  1631. Assert.Equal (expectedClippedWidth, list.Count);
  1632. Assert.Equal (noSpaceText, string.Concat (list.ToArray ()));
  1633. }
  1634. if (maxWidth > 1 && maxWidth < 10)
  1635. {
  1636. Assert.Equal (
  1637. StringExtensions.ToString (text.ToRunes () [..expectedClippedWidth]),
  1638. list [0]
  1639. );
  1640. }
  1641. }
  1642. }
  1643. [Theory]
  1644. [InlineData (null)]
  1645. [InlineData ("")]
  1646. [InlineData ("a")]
  1647. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal (string text)
  1648. {
  1649. var hotKeySpecifier = (Rune)'_';
  1650. if (text == null)
  1651. {
  1652. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  1653. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  1654. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  1655. }
  1656. else
  1657. {
  1658. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  1659. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  1660. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  1661. }
  1662. }
  1663. [Theory]
  1664. [InlineData ("all lower case", 0)]
  1665. [InlineData ("K Before", 0)]
  1666. [InlineData ("aK Second", 1)]
  1667. [InlineData ("Last K", 5)]
  1668. [InlineData ("fter K", 7)]
  1669. [InlineData ("Multiple K and R", 9)]
  1670. [InlineData ("Non-english: Кдать", 13)]
  1671. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal (string text, int hotPos)
  1672. {
  1673. var hotKeySpecifier = (Rune)'_';
  1674. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  1675. }
  1676. [Theory]
  1677. [InlineData ("_K Before", 0, "K Before")]
  1678. [InlineData ("a_K Second", 1, "aK Second")]
  1679. [InlineData ("Last _K", 5, "Last K")]
  1680. [InlineData ("After K_", 7, "After K")]
  1681. [InlineData ("Multiple _K and _R", 9, "Multiple K and _R")]
  1682. [InlineData ("Non-english: _Кдать", 13, "Non-english: Кдать")]
  1683. public void RemoveHotKeySpecifier_Valid_ReturnsStripped (string text, int hotPos, string expectedText)
  1684. {
  1685. var hotKeySpecifier = (Rune)'_';
  1686. Assert.Equal (expectedText, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  1687. }
  1688. [Theory]
  1689. [InlineData ("test", 0, 't', "test")]
  1690. [InlineData ("test", 1, 'e', "test")]
  1691. [InlineData ("Ok", 0, 'O', "Ok")]
  1692. [InlineData ("[◦ Ok ◦]", 3, 'O', "[◦ Ok ◦]")]
  1693. [InlineData ("^k", 0, '^', "^k")]
  1694. public void ReplaceHotKeyWithTag (string text, int hotPos, uint tag, string expected)
  1695. {
  1696. var tf = new TextFormatter ();
  1697. List<Rune> runes = text.ToRuneList ();
  1698. Rune rune;
  1699. if (Rune.TryGetRuneAt (text, hotPos, out rune))
  1700. {
  1701. Assert.Equal (rune, (Rune)tag);
  1702. }
  1703. string result = TextFormatter.ReplaceHotKeyWithTag (text, hotPos);
  1704. Assert.Equal (result, expected);
  1705. Assert.Equal ((Rune)tag, result.ToRunes () [hotPos]);
  1706. Assert.Equal (text.GetRuneCount (), runes.Count);
  1707. Assert.Equal (text, StringExtensions.ToString (runes));
  1708. }
  1709. public static IEnumerable<object []> SplitEnvironmentNewLine =>
  1710. new List<object []>
  1711. {
  1712. new object []
  1713. {
  1714. $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界",
  1715. new [] { "First Line 界", "Second Line 界", "Third Line 界" }
  1716. },
  1717. new object []
  1718. {
  1719. $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界{Environment.NewLine}",
  1720. new [] { "First Line 界", "Second Line 界", "Third Line 界", "" }
  1721. }
  1722. };
  1723. [Theory]
  1724. [MemberData (nameof (SplitEnvironmentNewLine))]
  1725. public void SplitNewLine_Ending__With_Or_Without_NewLine_Probably_CRLF (
  1726. string text,
  1727. IEnumerable<string> expected
  1728. )
  1729. {
  1730. List<string> splited = TextFormatter.SplitNewLine (text);
  1731. Assert.Equal (expected, splited);
  1732. }
  1733. [Theory]
  1734. [InlineData (
  1735. "First Line 界\nSecond Line 界\nThird Line 界\n",
  1736. new [] { "First Line 界", "Second Line 界", "Third Line 界", "" }
  1737. )]
  1738. public void SplitNewLine_Ending_With_NewLine_Only_LF (string text, IEnumerable<string> expected)
  1739. {
  1740. List<string> splited = TextFormatter.SplitNewLine (text);
  1741. Assert.Equal (expected, splited);
  1742. }
  1743. [Theory]
  1744. [InlineData (
  1745. "First Line 界\nSecond Line 界\nThird Line 界",
  1746. new [] { "First Line 界", "Second Line 界", "Third Line 界" }
  1747. )]
  1748. public void SplitNewLine_Ending_Without_NewLine_Only_LF (string text, IEnumerable<string> expected)
  1749. {
  1750. List<string> splited = TextFormatter.SplitNewLine (text);
  1751. Assert.Equal (expected, splited);
  1752. }
  1753. [Theory]
  1754. [InlineData ("New Test 你", 10, 10, 20320, 20320, 9, "你")]
  1755. [InlineData ("New Test \U0001d539", 10, 11, 120121, 55349, 9, "𝔹")]
  1756. public void String_Array_Is_Not_Always_Equal_ToRunes_Array (
  1757. string text,
  1758. int runesLength,
  1759. int stringLength,
  1760. int runeValue,
  1761. int stringValue,
  1762. int index,
  1763. string expected
  1764. )
  1765. {
  1766. Rune [] usToRunes = text.ToRunes ();
  1767. Assert.Equal (runesLength, usToRunes.Length);
  1768. Assert.Equal (stringLength, text.Length);
  1769. Assert.Equal (runeValue, usToRunes [index].Value);
  1770. Assert.Equal (stringValue, text [index]);
  1771. Assert.Equal (expected, usToRunes [index].ToString ());
  1772. if (char.IsHighSurrogate (text [index]))
  1773. {
  1774. // Rune array length isn't equal to string array
  1775. Assert.Equal (expected, new (new [] { text [index], text [index + 1] }));
  1776. }
  1777. else
  1778. {
  1779. // Rune array length is equal to string array
  1780. Assert.Equal (expected, text [index].ToString ());
  1781. }
  1782. }
  1783. [Theory]
  1784. [InlineData ("123456789", 3, "123")]
  1785. [InlineData ("Hello World", 8, "Hello Wo")]
  1786. public void TestClipOrPad_LongWord (string text, int fillPad, string expectedText)
  1787. {
  1788. // word is long but we want it to fill # space only
  1789. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1790. }
  1791. [Theory]
  1792. [InlineData ("fff", 6, "fff ")]
  1793. [InlineData ("Hello World", 16, "Hello World ")]
  1794. public void TestClipOrPad_ShortWord (string text, int fillPad, string expectedText)
  1795. {
  1796. // word is short but we want it to fill # so it should be padded
  1797. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1798. }
  1799. [Theory]
  1800. [InlineData ("你", TextDirection.LeftRight_TopBottom, 2, 1)]
  1801. [InlineData ("你", TextDirection.TopBottom_LeftRight, 2, 1)]
  1802. [InlineData ("你你", TextDirection.LeftRight_TopBottom, 4, 1)]
  1803. [InlineData ("你你", TextDirection.TopBottom_LeftRight, 2, 2)]
  1804. public void Text_Set_SizeIsCorrect (string text, TextDirection textDirection, int expectedWidth, int expectedHeight)
  1805. {
  1806. var tf = new TextFormatter { Direction = textDirection, Text = text };
  1807. tf.ConstrainToWidth = 10;
  1808. tf.ConstrainToHeight = 10;
  1809. Assert.Equal (new (expectedWidth, expectedHeight), tf.FormatAndGetSize ());
  1810. }
  1811. [Fact]
  1812. public void WordWrap_BigWidth ()
  1813. {
  1814. List<string> wrappedLines;
  1815. var text = "Constantinople";
  1816. wrappedLines = TextFormatter.WordWrapText (text, 100);
  1817. Assert.True (wrappedLines.Count == 1);
  1818. Assert.Equal ("Constantinople", wrappedLines [0]);
  1819. }
  1820. [Fact]
  1821. public void WordWrap_Invalid ()
  1822. {
  1823. var text = string.Empty;
  1824. var width = 0;
  1825. Assert.Empty (TextFormatter.WordWrapText (null, width));
  1826. Assert.Empty (TextFormatter.WordWrapText (text, width));
  1827. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrapText (text, -1));
  1828. }
  1829. [Theory]
  1830. [InlineData ("A sentence has words.", 3, -18, new [] { "A", "sen", "ten", "ce", "has", "wor", "ds." })]
  1831. [InlineData (
  1832. "A sentence has words.",
  1833. 2,
  1834. -19,
  1835. new [] { "A", "se", "nt", "en", "ce", "ha", "s", "wo", "rd", "s." }
  1836. )]
  1837. [InlineData (
  1838. "A sentence has words.",
  1839. 1,
  1840. -20,
  1841. new [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." }
  1842. )]
  1843. public void WordWrap_Narrow_Default (
  1844. string text,
  1845. int maxWidth,
  1846. int widthOffset,
  1847. IEnumerable<string> resultLines
  1848. )
  1849. {
  1850. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  1851. List<string> wrappedLines;
  1852. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1853. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1854. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  1855. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1856. Assert.True (
  1857. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  1858. );
  1859. Assert.True (
  1860. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  1861. );
  1862. Assert.Equal (resultLines, wrappedLines);
  1863. }
  1864. [Theory]
  1865. [InlineData ("A sentence has words.", 21, 0, new [] { "A sentence has words." })]
  1866. [InlineData ("A sentence has words.", 20, -1, new [] { "A sentence has", "words." })]
  1867. [InlineData ("A sentence has words.", 15, -6, new [] { "A sentence has", "words." })]
  1868. [InlineData ("A sentence has words.", 14, -7, new [] { "A sentence has", "words." })]
  1869. [InlineData ("A sentence has words.", 13, -8, new [] { "A sentence", "has words." })]
  1870. // Unicode
  1871. [InlineData (
  1872. "A Unicode sentence (пÑивеÑ) has words.",
  1873. 42,
  1874. 0,
  1875. new [] { "A Unicode sentence (пÑивеÑ) has words." }
  1876. )]
  1877. [InlineData (
  1878. "A Unicode sentence (пÑивеÑ) has words.",
  1879. 41,
  1880. -1,
  1881. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  1882. )]
  1883. [InlineData (
  1884. "A Unicode sentence (пÑивеÑ) has words.",
  1885. 36,
  1886. -6,
  1887. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  1888. )]
  1889. [InlineData (
  1890. "A Unicode sentence (пÑивеÑ) has words.",
  1891. 35,
  1892. -7,
  1893. new [] { "A Unicode sentence (пÑивеÑ) has", "words." }
  1894. )]
  1895. [InlineData (
  1896. "A Unicode sentence (пÑивеÑ) has words.",
  1897. 34,
  1898. -8,
  1899. new [] { "A Unicode sentence (пÑивеÑ)", "has words." }
  1900. )]
  1901. [InlineData (
  1902. "A Unicode sentence (пÑивеÑ) has words.",
  1903. 25,
  1904. -17,
  1905. new [] { "A Unicode sentence", "(пÑивеÑ) has words." }
  1906. )]
  1907. public void WordWrap_NoNewLines_Default (
  1908. string text,
  1909. int maxWidth,
  1910. int widthOffset,
  1911. IEnumerable<string> resultLines
  1912. )
  1913. {
  1914. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  1915. List<string> wrappedLines;
  1916. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1917. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1918. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  1919. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1920. Assert.True (
  1921. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  1922. );
  1923. Assert.True (
  1924. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  1925. );
  1926. Assert.Equal (resultLines, wrappedLines);
  1927. }
  1928. [Theory]
  1929. [InlineData ("これが最初の行です。 こんにちは世界。 これが2行目です。", 29, 0, new [] { "これが最初の行です。", "こんにちは世界。", "これが2行目です。" })]
  1930. public void WordWrap_PreserveTrailingSpaces_False_Unicode_Wide_Runes (
  1931. string text,
  1932. int maxWidth,
  1933. int widthOffset,
  1934. IEnumerable<string> resultLines
  1935. )
  1936. {
  1937. List<string> wrappedLines;
  1938. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1939. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1940. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  1941. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1942. Assert.True (
  1943. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  1944. );
  1945. Assert.True (
  1946. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  1947. );
  1948. Assert.Equal (resultLines, wrappedLines);
  1949. }
  1950. [Theory]
  1951. [InlineData ("文に は言葉 があり ます。", 14, 0, new [] { "文に は言葉", "があり ます。" })]
  1952. [InlineData ("文に は言葉 があり ます。", 3, -11, new [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  1953. [InlineData ("文に は言葉 があり ます。", 2, -12, new [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  1954. [InlineData (
  1955. "文に は言葉 があり ます。",
  1956. 1,
  1957. -13,
  1958. new [] { " ", " ", " " }
  1959. )] // Just Spaces; should result in a single space for each line
  1960. public void WordWrap_PreserveTrailingSpaces_False_Wide_Runes (
  1961. string text,
  1962. int maxWidth,
  1963. int widthOffset,
  1964. IEnumerable<string> resultLines
  1965. )
  1966. {
  1967. List<string> wrappedLines;
  1968. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1969. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1970. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  1971. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1972. Assert.True (
  1973. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  1974. );
  1975. Assert.True (
  1976. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  1977. );
  1978. Assert.Equal (resultLines, wrappedLines);
  1979. }
  1980. [Theory]
  1981. [InlineData (null, 1, new string [] { })] // null input
  1982. [InlineData ("", 1, new string [] { })] // Empty input
  1983. [InlineData ("1 34", 1, new [] { "1", "3", "4" })] // Single Spaces
  1984. [InlineData ("1", 1, new [] { "1" })] // Short input
  1985. [InlineData ("12", 1, new [] { "1", "2" })]
  1986. [InlineData ("123", 1, new [] { "1", "2", "3" })]
  1987. [InlineData ("123456", 1, new [] { "1", "2", "3", "4", "5", "6" })] // No spaces
  1988. [InlineData (" ", 1, new [] { " " })] // Just Spaces; should result in a single space
  1989. [InlineData (" ", 1, new [] { " " })]
  1990. [InlineData (" ", 1, new [] { " ", " " })]
  1991. [InlineData (" ", 1, new [] { " ", " " })]
  1992. [InlineData ("12 456", 1, new [] { "1", "2", "4", "5", "6" })] // Single Spaces
  1993. [InlineData (" 2 456", 1, new [] { " ", "2", "4", "5", "6" })] // Leading spaces should be preserved.
  1994. [InlineData (" 2 456 8", 1, new [] { " ", "2", "4", "5", "6", "8" })]
  1995. [InlineData (
  1996. "A sentence has words. ",
  1997. 1,
  1998. new [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." }
  1999. )] // Complex example
  2000. [InlineData ("12 567", 1, new [] { "1", "2", " ", "5", "6", "7" })] // Double Spaces
  2001. [InlineData (" 3 567", 1, new [] { " ", "3", "5", "6", "7" })] // Double Leading spaces should be preserved.
  2002. [InlineData (" 3 678 1", 1, new [] { " ", "3", " ", "6", "7", "8", " ", "1" })]
  2003. [InlineData ("1 456", 1, new [] { "1", " ", "4", "5", "6" })]
  2004. [InlineData (
  2005. "A sentence has words. ",
  2006. 1,
  2007. new []
  2008. {
  2009. "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", "w", "o", "r", "d", "s", ".", " "
  2010. }
  2011. )] // Double space Complex example
  2012. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_1 (
  2013. string text,
  2014. int width,
  2015. IEnumerable<string> resultLines
  2016. )
  2017. {
  2018. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2019. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2020. Assert.Equal (resultLines, wrappedLines);
  2021. var breakLines = "";
  2022. foreach (string line in wrappedLines)
  2023. {
  2024. breakLines += $"{line}{Environment.NewLine}";
  2025. }
  2026. var expected = string.Empty;
  2027. foreach (string line in resultLines)
  2028. {
  2029. expected += $"{line}{Environment.NewLine}";
  2030. }
  2031. Assert.Equal (expected, breakLines);
  2032. }
  2033. [Theory]
  2034. [InlineData (null, 3, new string [] { })] // null input
  2035. [InlineData ("", 3, new string [] { })] // Empty input
  2036. [InlineData ("1", 3, new [] { "1" })] // Short input
  2037. [InlineData ("12", 3, new [] { "12" })]
  2038. [InlineData ("123", 3, new [] { "123" })]
  2039. [InlineData ("123456", 3, new [] { "123", "456" })] // No spaces
  2040. [InlineData ("1234567", 3, new [] { "123", "456", "7" })] // No spaces
  2041. [InlineData (" ", 3, new [] { " " })] // Just Spaces; should result in a single space
  2042. [InlineData (" ", 3, new [] { " " })]
  2043. [InlineData (" ", 3, new [] { " " })]
  2044. [InlineData (" ", 3, new [] { " " })]
  2045. [InlineData ("12 456", 3, new [] { "12", "456" })] // Single Spaces
  2046. [InlineData (" 2 456", 3, new [] { " 2", "456" })] // Leading spaces should be preserved.
  2047. [InlineData (" 2 456 8", 3, new [] { " 2", "456", "8" })]
  2048. [InlineData (
  2049. "A sentence has words. ",
  2050. 3,
  2051. new [] { "A", "sen", "ten", "ce", "has", "wor", "ds." }
  2052. )] // Complex example
  2053. [InlineData ("12 567", 3, new [] { "12 ", "567" })] // Double Spaces
  2054. [InlineData (" 3 567", 3, new [] { " 3", "567" })] // Double Leading spaces should be preserved.
  2055. [InlineData (" 3 678 1", 3, new [] { " 3", " 67", "8 ", "1" })]
  2056. [InlineData ("1 456", 3, new [] { "1 ", "456" })]
  2057. [InlineData (
  2058. "A sentence has words. ",
  2059. 3,
  2060. new [] { "A ", "sen", "ten", "ce ", " ", "has", "wor", "ds.", " " }
  2061. )] // Double space Complex example
  2062. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_3 (
  2063. string text,
  2064. int width,
  2065. IEnumerable<string> resultLines
  2066. )
  2067. {
  2068. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2069. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2070. Assert.Equal (resultLines, wrappedLines);
  2071. var breakLines = "";
  2072. foreach (string line in wrappedLines)
  2073. {
  2074. breakLines += $"{line}{Environment.NewLine}";
  2075. }
  2076. var expected = string.Empty;
  2077. foreach (string line in resultLines)
  2078. {
  2079. expected += $"{line}{Environment.NewLine}";
  2080. }
  2081. Assert.Equal (expected, breakLines);
  2082. }
  2083. [Theory]
  2084. [InlineData (null, 50, new string [] { })] // null input
  2085. [InlineData ("", 50, new string [] { })] // Empty input
  2086. [InlineData ("1", 50, new [] { "1" })] // Short input
  2087. [InlineData ("12", 50, new [] { "12" })]
  2088. [InlineData ("123", 50, new [] { "123" })]
  2089. [InlineData ("123456", 50, new [] { "123456" })] // No spaces
  2090. [InlineData ("1234567", 50, new [] { "1234567" })] // No spaces
  2091. [InlineData (" ", 50, new [] { " " })] // Just Spaces; should result in a single space
  2092. [InlineData (" ", 50, new [] { " " })]
  2093. [InlineData (" ", 50, new [] { " " })]
  2094. [InlineData ("12 456", 50, new [] { "12 456" })] // Single Spaces
  2095. [InlineData (" 2 456", 50, new [] { " 2 456" })] // Leading spaces should be preserved.
  2096. [InlineData (" 2 456 8", 50, new [] { " 2 456 8" })]
  2097. [InlineData ("A sentence has words. ", 50, new [] { "A sentence has words. " })] // Complex example
  2098. [InlineData ("12 567", 50, new [] { "12 567" })] // Double Spaces
  2099. [InlineData (" 3 567", 50, new [] { " 3 567" })] // Double Leading spaces should be preserved.
  2100. [InlineData (" 3 678 1", 50, new [] { " 3 678 1" })]
  2101. [InlineData ("1 456", 50, new [] { "1 456" })]
  2102. [InlineData (
  2103. "A sentence has words. ",
  2104. 50,
  2105. new [] { "A sentence has words. " }
  2106. )] // Double space Complex example
  2107. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_50 (
  2108. string text,
  2109. int width,
  2110. IEnumerable<string> resultLines
  2111. )
  2112. {
  2113. List<string> wrappedLines = TextFormatter.WordWrapText (text, width);
  2114. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2115. Assert.Equal (resultLines, wrappedLines);
  2116. var breakLines = "";
  2117. foreach (string line in wrappedLines)
  2118. {
  2119. breakLines += $"{line}{Environment.NewLine}";
  2120. }
  2121. var expected = string.Empty;
  2122. foreach (string line in resultLines)
  2123. {
  2124. expected += $"{line}{Environment.NewLine}";
  2125. }
  2126. Assert.Equal (expected, breakLines);
  2127. }
  2128. [Theory]
  2129. [InlineData ("A sentence has words.", 14, -7, new [] { "A sentence ", "has words." })]
  2130. [InlineData ("A sentence has words.", 8, -13, new [] { "A ", "sentence", " has ", "words." })]
  2131. [InlineData ("A sentence has words.", 6, -15, new [] { "A ", "senten", "ce ", "has ", "words." })]
  2132. [InlineData ("A sentence has words.", 3, -18, new [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds." })]
  2133. [InlineData (
  2134. "A sentence has words.",
  2135. 2,
  2136. -19,
  2137. new [] { "A ", "se", "nt", "en", "ce", " ", "ha", "s ", "wo", "rd", "s." }
  2138. )]
  2139. [InlineData (
  2140. "A sentence has words.",
  2141. 1,
  2142. -20,
  2143. new []
  2144. {
  2145. "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", " ", "w", "o", "r", "d", "s", "."
  2146. }
  2147. )]
  2148. public void WordWrap_PreserveTrailingSpaces_True (
  2149. string text,
  2150. int maxWidth,
  2151. int widthOffset,
  2152. IEnumerable<string> resultLines
  2153. )
  2154. {
  2155. List<string> wrappedLines;
  2156. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2157. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2158. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true);
  2159. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2160. Assert.True (
  2161. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2162. );
  2163. Assert.True (
  2164. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2165. );
  2166. Assert.Equal (resultLines, wrappedLines);
  2167. }
  2168. [Theory]
  2169. [InlineData ("文に は言葉 があり ます。", 14, 0, new [] { "文に は言葉 ", "があり ます。" })]
  2170. [InlineData ("文に は言葉 があり ます。", 3, -11, new [] { "文", "に ", "は", "言", "葉 ", "が", "あ", "り ", "ま", "す", "。" })]
  2171. [InlineData (
  2172. "文に は言葉 があり ます。",
  2173. 2,
  2174. -12,
  2175. new [] { "文", "に", " ", "は", "言", "葉", " ", "が", "あ", "り", " ", "ま", "す", "。" }
  2176. )]
  2177. [InlineData ("文に は言葉 があり ます。", 1, -13, new string [] { })]
  2178. public void WordWrap_PreserveTrailingSpaces_True_Wide_Runes (
  2179. string text,
  2180. int maxWidth,
  2181. int widthOffset,
  2182. IEnumerable<string> resultLines
  2183. )
  2184. {
  2185. List<string> wrappedLines;
  2186. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2187. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2188. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true);
  2189. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2190. Assert.True (
  2191. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2192. );
  2193. Assert.True (
  2194. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2195. );
  2196. Assert.Equal (resultLines, wrappedLines);
  2197. }
  2198. [Theory]
  2199. [InlineData ("A sentence has words. ", 3, new [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds.", " " })]
  2200. [InlineData (
  2201. "A sentence has words. ",
  2202. 3,
  2203. new [] { "A ", " ", "sen", "ten", "ce ", " ", " ", " ", "has", " ", "wor", "ds.", " " }
  2204. )]
  2205. public void WordWrap_PreserveTrailingSpaces_True_With_Simple_Runes_Width_3 (
  2206. string text,
  2207. int width,
  2208. IEnumerable<string> resultLines
  2209. )
  2210. {
  2211. List<string> wrappedLines = TextFormatter.WordWrapText (text, width, true);
  2212. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2213. Assert.Equal (resultLines, wrappedLines);
  2214. var breakLines = "";
  2215. foreach (string line in wrappedLines)
  2216. {
  2217. breakLines += $"{line}{Environment.NewLine}";
  2218. }
  2219. var expected = string.Empty;
  2220. foreach (string line in resultLines)
  2221. {
  2222. expected += $"{line}{Environment.NewLine}";
  2223. }
  2224. Assert.Equal (expected, breakLines);
  2225. // Double space Complex example - this is how VS 2022 does it
  2226. //text = "A sentence has words. ";
  2227. //breakLines = "";
  2228. //wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: true);
  2229. //foreach (var line in wrappedLines) {
  2230. // breakLines += $"{line}{Environment.NewLine}";
  2231. //}
  2232. //expected = "A " + Environment.NewLine +
  2233. // " se" + Environment.NewLine +
  2234. // " nt" + Environment.NewLine +
  2235. // " en" + Environment.NewLine +
  2236. // " ce" + Environment.NewLine +
  2237. // " " + Environment.NewLine +
  2238. // " " + Environment.NewLine +
  2239. // " " + Environment.NewLine +
  2240. // " ha" + Environment.NewLine +
  2241. // " s " + Environment.NewLine +
  2242. // " wo" + Environment.NewLine +
  2243. // " rd" + Environment.NewLine +
  2244. // " s." + Environment.NewLine;
  2245. //Assert.Equal (expected, breakLines);
  2246. }
  2247. [Theory]
  2248. [InlineData ("A sentence\t\t\t has words.", 14, -10, new [] { "A sentence\t", "\t\t has ", "words." })]
  2249. [InlineData (
  2250. "A sentence\t\t\t has words.",
  2251. 8,
  2252. -16,
  2253. new [] { "A ", "sentence", "\t\t", "\t ", "has ", "words." }
  2254. )]
  2255. [InlineData (
  2256. "A sentence\t\t\t has words.",
  2257. 3,
  2258. -21,
  2259. new [] { "A ", "sen", "ten", "ce", "\t", "\t", "\t", " ", "has", " ", "wor", "ds." }
  2260. )]
  2261. [InlineData (
  2262. "A sentence\t\t\t has words.",
  2263. 2,
  2264. -22,
  2265. new [] { "A ", "se", "nt", "en", "ce", "\t", "\t", "\t", " ", "ha", "s ", "wo", "rd", "s." }
  2266. )]
  2267. [InlineData (
  2268. "A sentence\t\t\t has words.",
  2269. 1,
  2270. -23,
  2271. new []
  2272. {
  2273. "A",
  2274. " ",
  2275. "s",
  2276. "e",
  2277. "n",
  2278. "t",
  2279. "e",
  2280. "n",
  2281. "c",
  2282. "e",
  2283. "\t",
  2284. "\t",
  2285. "\t",
  2286. " ",
  2287. "h",
  2288. "a",
  2289. "s",
  2290. " ",
  2291. "w",
  2292. "o",
  2293. "r",
  2294. "d",
  2295. "s",
  2296. "."
  2297. }
  2298. )]
  2299. public void WordWrap_PreserveTrailingSpaces_True_With_Tab (
  2300. string text,
  2301. int maxWidth,
  2302. int widthOffset,
  2303. IEnumerable<string> resultLines,
  2304. int tabWidth = 4
  2305. )
  2306. {
  2307. List<string> wrappedLines;
  2308. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2309. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2310. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, true, tabWidth);
  2311. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2312. Assert.True (
  2313. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2314. );
  2315. Assert.True (
  2316. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2317. );
  2318. Assert.Equal (resultLines, wrappedLines);
  2319. }
  2320. [Theory]
  2321. [InlineData ("Constantinople", 14, 0, new [] { "Constantinople" })]
  2322. [InlineData ("Constantinople", 12, -2, new [] { "Constantinop", "le" })]
  2323. [InlineData ("Constantinople", 9, -5, new [] { "Constanti", "nople" })]
  2324. [InlineData ("Constantinople", 7, -7, new [] { "Constan", "tinople" })]
  2325. [InlineData ("Constantinople", 5, -9, new [] { "Const", "antin", "ople" })]
  2326. [InlineData ("Constantinople", 4, -10, new [] { "Cons", "tant", "inop", "le" })]
  2327. [InlineData (
  2328. "Constantinople",
  2329. 1,
  2330. -13,
  2331. new [] { "C", "o", "n", "s", "t", "a", "n", "t", "i", "n", "o", "p", "l", "e" }
  2332. )]
  2333. public void WordWrap_SingleWordLine (
  2334. string text,
  2335. int maxWidth,
  2336. int widthOffset,
  2337. IEnumerable<string> resultLines
  2338. )
  2339. {
  2340. List<string> wrappedLines;
  2341. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2342. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2343. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2344. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2345. Assert.True (
  2346. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2347. );
  2348. Assert.True (
  2349. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2350. );
  2351. Assert.Equal (resultLines, wrappedLines);
  2352. }
  2353. [Theory]
  2354. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 20, 0, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2355. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 19, -1, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2356. [InlineData (
  2357. "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.",
  2358. 19,
  2359. 0,
  2360. new [] { "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence." }
  2361. )]
  2362. public void WordWrap_Unicode_2LinesWithNonBreakingSpace (
  2363. string text,
  2364. int maxWidth,
  2365. int widthOffset,
  2366. IEnumerable<string> resultLines
  2367. )
  2368. {
  2369. List<string> wrappedLines;
  2370. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2371. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2372. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2373. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2374. Assert.True (
  2375. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2376. );
  2377. Assert.True (
  2378. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2379. );
  2380. Assert.Equal (resultLines, wrappedLines);
  2381. }
  2382. [Theory]
  2383. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 19, 0, new [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  2384. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 18, -1, new [] { "This\u00A0is\u00A0a\u00A0sentence", "." })]
  2385. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 17, -2, new [] { "This\u00A0is\u00A0a\u00A0sentenc", "e." })]
  2386. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 14, -5, new [] { "This\u00A0is\u00A0a\u00A0sent", "ence." })]
  2387. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 10, -9, new [] { "This\u00A0is\u00A0a\u00A0", "sentence." })]
  2388. [InlineData (
  2389. "This\u00A0is\u00A0a\u00A0sentence.",
  2390. 7,
  2391. -12,
  2392. new [] { "This\u00A0is", "\u00A0a\u00A0sent", "ence." }
  2393. )]
  2394. [InlineData (
  2395. "This\u00A0is\u00A0a\u00A0sentence.",
  2396. 5,
  2397. -14,
  2398. new [] { "This\u00A0", "is\u00A0a\u00A0", "sente", "nce." }
  2399. )]
  2400. [InlineData (
  2401. "This\u00A0is\u00A0a\u00A0sentence.",
  2402. 1,
  2403. -18,
  2404. new []
  2405. {
  2406. "T", "h", "i", "s", "\u00A0", "i", "s", "\u00A0", "a", "\u00A0", "s", "e", "n", "t", "e", "n", "c", "e", "."
  2407. }
  2408. )]
  2409. public void WordWrap_Unicode_LineWithNonBreakingSpace (
  2410. string text,
  2411. int maxWidth,
  2412. int widthOffset,
  2413. IEnumerable<string> resultLines
  2414. )
  2415. {
  2416. List<string> wrappedLines;
  2417. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2418. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2419. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2420. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2421. Assert.True (
  2422. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2423. );
  2424. Assert.True (
  2425. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2426. );
  2427. Assert.Equal (resultLines, wrappedLines);
  2428. }
  2429. [Theory]
  2430. [InlineData (
  2431. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2432. 51,
  2433. 0,
  2434. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2435. )]
  2436. [InlineData (
  2437. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2438. 50,
  2439. -1,
  2440. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2441. )]
  2442. [InlineData (
  2443. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2444. 46,
  2445. -5,
  2446. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮ", "ฯะัาำ" }
  2447. )]
  2448. [InlineData (
  2449. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2450. 26,
  2451. -25,
  2452. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" }
  2453. )]
  2454. [InlineData (
  2455. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2456. 17,
  2457. -34,
  2458. new [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑ", "ฒณดตถทธนบปผฝพฟภมย", "รฤลฦวศษสหฬอฮฯะัาำ" }
  2459. )]
  2460. [InlineData (
  2461. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2462. 13,
  2463. -38,
  2464. new [] { "กขฃคฅฆงจฉชซฌญ", "ฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦว", "ศษสหฬอฮฯะัาำ" }
  2465. )]
  2466. [InlineData (
  2467. "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ",
  2468. 1,
  2469. -50,
  2470. new []
  2471. {
  2472. "ก",
  2473. "ข",
  2474. "ฃ",
  2475. "ค",
  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. public void WordWrap_Unicode_SingleWordLine (
  2525. string text,
  2526. int maxWidth,
  2527. int widthOffset,
  2528. IEnumerable<string> resultLines
  2529. )
  2530. {
  2531. List<string> wrappedLines;
  2532. IEnumerable<Rune> zeroWidth = text.EnumerateRunes ().Where (r => r.GetColumns () == 0);
  2533. Assert.Single (zeroWidth);
  2534. Assert.Equal ('ั', zeroWidth.ElementAt (0).Value);
  2535. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2536. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2537. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2538. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2539. Assert.True (
  2540. expectedClippedWidth
  2541. >= (wrappedLines.Count > 0
  2542. ? wrappedLines.Max (
  2543. l => l.GetRuneCount ()
  2544. + zeroWidth.Count ()
  2545. - 1
  2546. + widthOffset
  2547. )
  2548. : 0)
  2549. );
  2550. Assert.True (
  2551. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2552. );
  2553. Assert.Equal (resultLines, wrappedLines);
  2554. }
  2555. /// <summary>WordWrap strips CRLF</summary>
  2556. [Theory]
  2557. [InlineData (
  2558. "A sentence has words.\nA paragraph has lines.",
  2559. 44,
  2560. 0,
  2561. new [] { "A sentence has words.A paragraph has lines." }
  2562. )]
  2563. [InlineData (
  2564. "A sentence has words.\nA paragraph has lines.",
  2565. 43,
  2566. -1,
  2567. new [] { "A sentence has words.A paragraph has lines." }
  2568. )]
  2569. [InlineData (
  2570. "A sentence has words.\nA paragraph has lines.",
  2571. 38,
  2572. -6,
  2573. new [] { "A sentence has words.A paragraph has", "lines." }
  2574. )]
  2575. [InlineData (
  2576. "A sentence has words.\nA paragraph has lines.",
  2577. 34,
  2578. -10,
  2579. new [] { "A sentence has words.A paragraph", "has lines." }
  2580. )]
  2581. [InlineData (
  2582. "A sentence has words.\nA paragraph has lines.",
  2583. 27,
  2584. -17,
  2585. new [] { "A sentence has words.A", "paragraph has lines." }
  2586. )]
  2587. // Unicode
  2588. [InlineData (
  2589. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2590. 69,
  2591. 0,
  2592. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." }
  2593. )]
  2594. [InlineData (
  2595. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2596. 68,
  2597. -1,
  2598. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." }
  2599. )]
  2600. [InlineData (
  2601. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2602. 63,
  2603. -6,
  2604. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", "Линии." }
  2605. )]
  2606. [InlineData (
  2607. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2608. 59,
  2609. -10,
  2610. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", "has Линии." }
  2611. )]
  2612. [InlineData (
  2613. "A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.",
  2614. 52,
  2615. -17,
  2616. new [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode", "Пункт has Линии." }
  2617. )]
  2618. public void WordWrap_WithNewLines (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  2619. {
  2620. List<string> wrappedLines;
  2621. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  2622. int expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  2623. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  2624. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  2625. Assert.True (
  2626. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0)
  2627. );
  2628. Assert.True (
  2629. expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0)
  2630. );
  2631. Assert.Equal (resultLines, wrappedLines);
  2632. }
  2633. [Theory]
  2634. [InlineData ("No crlf", "No crlf")]
  2635. // CRLF
  2636. [InlineData ("\r\nThis has crlf in the beginning", "This has crlf in the beginning")]
  2637. [InlineData ("This has crlf\r\nin the middle", "This has crlfin the middle")]
  2638. [InlineData ("This has crlf in the end\r\n", "This has crlf in the end")]
  2639. // LFCR
  2640. [InlineData ("\n\rThis has lfcr in the beginning", "This has lfcr in the beginning")]
  2641. [InlineData ("This has lfcr\n\rin the middle", "This has lfcrin the middle")]
  2642. [InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end")]
  2643. // CR
  2644. [InlineData ("\rThis has cr in the beginning", "This has cr in the beginning")]
  2645. [InlineData ("This has cr\rin the middle", "This has crin the middle")]
  2646. [InlineData ("This has cr in the end\r", "This has cr in the end")]
  2647. // LF
  2648. [InlineData ("\nThis has lf in the beginning", "This has lf in the beginning")]
  2649. [InlineData ("This has lf\nin the middle", "This has lfin the middle")]
  2650. [InlineData ("This has lf in the end\n", "This has lf in the end")]
  2651. public void StripCRLF_RemovesCrLf (string input, string expected)
  2652. {
  2653. string actual = TextFormatter.StripCRLF(input, keepNewLine: false);
  2654. Assert.Equal (expected, actual);
  2655. }
  2656. [Theory]
  2657. [InlineData ("No crlf", "No crlf")]
  2658. // CRLF
  2659. [InlineData ("\r\nThis has crlf in the beginning", "\nThis has crlf in the beginning")]
  2660. [InlineData ("This has crlf\r\nin the middle", "This has crlf\nin the middle")]
  2661. [InlineData ("This has crlf in the end\r\n", "This has crlf in the end\n")]
  2662. // LFCR
  2663. [InlineData ("\n\rThis has lfcr in the beginning", "\n\rThis has lfcr in the beginning")]
  2664. [InlineData ("This has lfcr\n\rin the middle", "This has lfcr\n\rin the middle")]
  2665. [InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end\n\r")]
  2666. // CR
  2667. [InlineData ("\rThis has cr in the beginning", "\rThis has cr in the beginning")]
  2668. [InlineData ("This has cr\rin the middle", "This has cr\rin the middle")]
  2669. [InlineData ("This has cr in the end\r", "This has cr in the end\r")]
  2670. // LF
  2671. [InlineData ("\nThis has lf in the beginning", "\nThis has lf in the beginning")]
  2672. [InlineData ("This has lf\nin the middle", "This has lf\nin the middle")]
  2673. [InlineData ("This has lf in the end\n", "This has lf in the end\n")]
  2674. public void StripCRLF_KeepNewLine_RemovesCarriageReturnFromCrLf (string input, string expected)
  2675. {
  2676. string actual = TextFormatter.StripCRLF(input, keepNewLine: true);
  2677. Assert.Equal (expected, actual);
  2678. }
  2679. [Theory]
  2680. [InlineData ("No crlf", "No crlf")]
  2681. // CRLF
  2682. [InlineData ("\r\nThis has crlf in the beginning", " This has crlf in the beginning")]
  2683. [InlineData ("This has crlf\r\nin the middle", "This has crlf in the middle")]
  2684. [InlineData ("This has crlf in the end\r\n", "This has crlf in the end ")]
  2685. // LFCR
  2686. [InlineData ("\n\rThis has lfcr in the beginning", " This has lfcr in the beginning")]
  2687. [InlineData ("This has lfcr\n\rin the middle", "This has lfcr in the middle")]
  2688. [InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end ")]
  2689. // CR
  2690. [InlineData ("\rThis has cr in the beginning", " This has cr in the beginning")]
  2691. [InlineData ("This has cr\rin the middle", "This has cr in the middle")]
  2692. [InlineData ("This has cr in the end\r", "This has cr in the end ")]
  2693. // LF
  2694. [InlineData ("\nThis has lf in the beginning", " This has lf in the beginning")]
  2695. [InlineData ("This has lf\nin the middle", "This has lf in the middle")]
  2696. [InlineData ("This has lf in the end\n", "This has lf in the end ")]
  2697. public void ReplaceCRLFWithSpace_ReplacesCrLfWithSpace (string input, string expected)
  2698. {
  2699. string actual = TextFormatter.ReplaceCRLFWithSpace(input);
  2700. Assert.Equal (expected, actual);
  2701. }
  2702. }