TextFormatterTests.cs 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Linq;
  7. using Terminal.Gui;
  8. using Xunit;
  9. // Alias Console to MockConsole so we don't accidentally use Console
  10. using Console = Terminal.Gui.FakeConsole;
  11. namespace Terminal.Gui.Core {
  12. public class TextFormatterTests {
  13. [Fact]
  14. public void Basic_Usage ()
  15. {
  16. var testText = ustring.Make ("test");
  17. var expectedSize = new Size ();
  18. var testBounds = new Rect (0, 0, 100, 1);
  19. var tf = new TextFormatter ();
  20. tf.Text = testText;
  21. expectedSize = new Size (testText.Length, 1);
  22. Assert.Equal (testText, tf.Text);
  23. Assert.Equal (TextAlignment.Left, tf.Alignment);
  24. Assert.Equal (expectedSize, tf.Size);
  25. tf.Draw (testBounds, new Attribute (), new Attribute ());
  26. Assert.Equal (expectedSize, tf.Size);
  27. Assert.NotEmpty (tf.Lines);
  28. tf.Alignment = TextAlignment.Right;
  29. expectedSize = new Size (testText.Length, 1);
  30. Assert.Equal (testText, tf.Text);
  31. Assert.Equal (TextAlignment.Right, tf.Alignment);
  32. Assert.Equal (expectedSize, tf.Size);
  33. tf.Draw (testBounds, new Attribute (), new Attribute ());
  34. Assert.Equal (expectedSize, tf.Size);
  35. Assert.NotEmpty (tf.Lines);
  36. tf.Alignment = TextAlignment.Right;
  37. expectedSize = new Size (testText.Length * 2, 1);
  38. tf.Size = expectedSize;
  39. Assert.Equal (testText, tf.Text);
  40. Assert.Equal (TextAlignment.Right, tf.Alignment);
  41. Assert.Equal (expectedSize, tf.Size);
  42. tf.Draw (testBounds, new Attribute (), new Attribute ());
  43. Assert.Equal (expectedSize, tf.Size);
  44. Assert.NotEmpty (tf.Lines);
  45. tf.Alignment = TextAlignment.Centered;
  46. expectedSize = new Size (testText.Length * 2, 1);
  47. tf.Size = expectedSize;
  48. Assert.Equal (testText, tf.Text);
  49. Assert.Equal (TextAlignment.Centered, tf.Alignment);
  50. Assert.Equal (expectedSize, tf.Size);
  51. tf.Draw (testBounds, new Attribute (), new Attribute ());
  52. Assert.Equal (expectedSize, tf.Size);
  53. Assert.NotEmpty (tf.Lines);
  54. }
  55. [Fact]
  56. public void NeedsFormat_Sets ()
  57. {
  58. var testText = ustring.Make ("test");
  59. var testBounds = new Rect (0, 0, 100, 1);
  60. var tf = new TextFormatter ();
  61. tf.Text = "test";
  62. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  63. Assert.NotEmpty (tf.Lines);
  64. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  65. Assert.Equal (testText, tf.Text);
  66. tf.Draw (testBounds, new Attribute (), new Attribute ());
  67. Assert.False (tf.NeedsFormat);
  68. tf.Size = new Size (1, 1);
  69. Assert.True (tf.NeedsFormat);
  70. Assert.NotEmpty (tf.Lines);
  71. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  72. tf.Alignment = TextAlignment.Centered;
  73. Assert.True (tf.NeedsFormat);
  74. Assert.NotEmpty (tf.Lines);
  75. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  76. }
  77. [Fact]
  78. public void FindHotKey_Invalid_ReturnsFalse ()
  79. {
  80. var text = ustring.Empty;
  81. Rune hotKeySpecifier = '_';
  82. bool supportFirstUpperCase = false;
  83. int hotPos = 0;
  84. Key hotKey = Key.Unknown;
  85. bool result = false;
  86. text = null;
  87. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  88. Assert.False (result);
  89. Assert.Equal (-1, hotPos);
  90. Assert.Equal (Key.Unknown, hotKey);
  91. text = "";
  92. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  93. Assert.False (result);
  94. Assert.Equal (-1, hotPos);
  95. Assert.Equal (Key.Unknown, hotKey);
  96. text = "no hotkey";
  97. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  98. Assert.False (result);
  99. Assert.Equal (-1, hotPos);
  100. Assert.Equal (Key.Unknown, hotKey);
  101. text = "No hotkey, Upper Case";
  102. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  103. Assert.False (result);
  104. Assert.Equal (-1, hotPos);
  105. Assert.Equal (Key.Unknown, hotKey);
  106. text = "Non-english: Сохранить";
  107. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  108. Assert.False (result);
  109. Assert.Equal (-1, hotPos);
  110. Assert.Equal (Key.Unknown, hotKey);
  111. }
  112. [Fact]
  113. public void FindHotKey_AlphaUpperCase_Succeeds ()
  114. {
  115. var text = ustring.Empty;
  116. Rune hotKeySpecifier = '_';
  117. bool supportFirstUpperCase = false;
  118. int hotPos = 0;
  119. Key hotKey = Key.Unknown;
  120. bool result = false;
  121. text = "_K Before";
  122. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  123. Assert.True (result);
  124. Assert.Equal (0, hotPos);
  125. Assert.Equal ((Key)'K', hotKey);
  126. text = "a_K Second";
  127. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  128. Assert.True (result);
  129. Assert.Equal (1, hotPos);
  130. Assert.Equal ((Key)'K', hotKey);
  131. text = "Last _K";
  132. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  133. Assert.True (result);
  134. Assert.Equal (5, hotPos);
  135. Assert.Equal ((Key)'K', hotKey);
  136. text = "After K_";
  137. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  138. Assert.False (result);
  139. Assert.Equal (-1, hotPos);
  140. Assert.Equal (Key.Unknown, hotKey);
  141. text = "Multiple _K and _R";
  142. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  143. Assert.True (result);
  144. Assert.Equal (9, hotPos);
  145. Assert.Equal ((Key)'K', hotKey);
  146. // Cryllic K (К)
  147. text = "Non-english: _Кдать";
  148. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  149. Assert.True (result);
  150. Assert.Equal (13, hotPos);
  151. Assert.Equal ((Key)'К', hotKey);
  152. // Turn on FirstUpperCase and verify same results
  153. supportFirstUpperCase = true;
  154. text = "_K Before";
  155. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  156. Assert.True (result);
  157. Assert.Equal (0, hotPos);
  158. Assert.Equal ((Key)'K', hotKey);
  159. text = "a_K Second";
  160. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  161. Assert.True (result);
  162. Assert.Equal (1, hotPos);
  163. Assert.Equal ((Key)'K', hotKey);
  164. text = "Last _K";
  165. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  166. Assert.True (result);
  167. Assert.Equal (5, hotPos);
  168. Assert.Equal ((Key)'K', hotKey);
  169. text = "After K_";
  170. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  171. Assert.False (result);
  172. Assert.Equal (-1, hotPos);
  173. Assert.Equal (Key.Unknown, hotKey);
  174. text = "Multiple _K and _R";
  175. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  176. Assert.True (result);
  177. Assert.Equal (9, hotPos);
  178. Assert.Equal ((Key)'K', hotKey);
  179. // Cryllic K (К)
  180. text = "Non-english: _Кдать";
  181. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  182. Assert.True (result);
  183. Assert.Equal (13, hotPos);
  184. Assert.Equal ((Key)'К', hotKey);
  185. }
  186. [Fact]
  187. public void FindHotKey_AlphaLowerCase_Succeeds ()
  188. {
  189. var text = ustring.Empty;
  190. Rune hotKeySpecifier = '_';
  191. bool supportFirstUpperCase = false;
  192. int hotPos = 0;
  193. Key hotKey = Key.Unknown;
  194. bool result = false;
  195. // lower case should return uppercase Hotkey
  196. text = "_k Before";
  197. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  198. Assert.True (result);
  199. Assert.Equal (0, hotPos);
  200. Assert.Equal ((Key)'K', hotKey);
  201. text = "a_k Second";
  202. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  203. Assert.True (result);
  204. Assert.Equal (1, hotPos);
  205. Assert.Equal ((Key)'K', hotKey);
  206. text = "Last _k";
  207. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  208. Assert.True (result);
  209. Assert.Equal (5, hotPos);
  210. Assert.Equal ((Key)'K', hotKey);
  211. text = "After k_";
  212. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  213. Assert.False (result);
  214. Assert.Equal (-1, hotPos);
  215. Assert.Equal (Key.Unknown, hotKey);
  216. text = "Multiple _k and _R";
  217. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  218. Assert.True (result);
  219. Assert.Equal (9, hotPos);
  220. Assert.Equal ((Key)'K', hotKey);
  221. // Lower case Cryllic K (к)
  222. text = "Non-english: _кдать";
  223. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  224. Assert.True (result);
  225. Assert.Equal (13, hotPos);
  226. Assert.Equal ((Key)'К', hotKey);
  227. // Turn on FirstUpperCase and verify same results
  228. supportFirstUpperCase = true;
  229. text = "_k Before";
  230. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  231. Assert.True (result);
  232. Assert.Equal (0, hotPos);
  233. Assert.Equal ((Key)'K', hotKey);
  234. text = "a_k Second";
  235. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  236. Assert.True (result);
  237. Assert.Equal (1, hotPos);
  238. Assert.Equal ((Key)'K', hotKey);
  239. text = "Last _k";
  240. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  241. Assert.True (result);
  242. Assert.Equal (5, hotPos);
  243. Assert.Equal ((Key)'K', hotKey);
  244. text = "After k_";
  245. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  246. Assert.False (result);
  247. Assert.Equal (-1, hotPos);
  248. Assert.Equal (Key.Unknown, hotKey);
  249. text = "Multiple _k and _R";
  250. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  251. Assert.True (result);
  252. Assert.Equal (9, hotPos);
  253. Assert.Equal ((Key)'K', hotKey);
  254. // Lower case Cryllic K (к)
  255. text = "Non-english: _кдать";
  256. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  257. Assert.True (result);
  258. Assert.Equal (13, hotPos);
  259. Assert.Equal ((Key)'К', hotKey);
  260. }
  261. [Fact]
  262. public void FindHotKey_Numeric_Succeeds ()
  263. {
  264. var text = ustring.Empty;
  265. Rune hotKeySpecifier = '_';
  266. bool supportFirstUpperCase = false;
  267. int hotPos = 0;
  268. Key hotKey = Key.Unknown;
  269. bool result = false;
  270. // Digits
  271. text = "_1 Before";
  272. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  273. Assert.True (result);
  274. Assert.Equal (0, hotPos);
  275. Assert.Equal ((Key)'1', hotKey);
  276. text = "a_1 Second";
  277. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  278. Assert.True (result);
  279. Assert.Equal (1, hotPos);
  280. Assert.Equal ((Key)'1', hotKey);
  281. text = "Last _1";
  282. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  283. Assert.True (result);
  284. Assert.Equal (5, hotPos);
  285. Assert.Equal ((Key)'1', hotKey);
  286. text = "After 1_";
  287. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  288. Assert.False (result);
  289. Assert.Equal (-1, hotPos);
  290. Assert.Equal (Key.Unknown, hotKey);
  291. text = "Multiple _1 and _2";
  292. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  293. Assert.True (result);
  294. Assert.Equal (9, hotPos);
  295. Assert.Equal ((Key)'1', hotKey);
  296. // Turn on FirstUpperCase and verify same results
  297. supportFirstUpperCase = true;
  298. text = "_1 Before";
  299. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  300. Assert.True (result);
  301. Assert.Equal (0, hotPos);
  302. Assert.Equal ((Key)'1', hotKey);
  303. text = "a_1 Second";
  304. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  305. Assert.True (result);
  306. Assert.Equal (1, hotPos);
  307. Assert.Equal ((Key)'1', hotKey);
  308. text = "Last _1";
  309. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  310. Assert.True (result);
  311. Assert.Equal (5, hotPos);
  312. Assert.Equal ((Key)'1', hotKey);
  313. text = "After 1_";
  314. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  315. Assert.False (result);
  316. Assert.Equal (-1, hotPos);
  317. Assert.Equal (Key.Unknown, hotKey);
  318. text = "Multiple _1 and _2";
  319. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  320. Assert.True (result);
  321. Assert.Equal (9, hotPos);
  322. Assert.Equal ((Key)'1', hotKey);
  323. }
  324. [Fact]
  325. public void FindHotKey_Legacy_FirstUpperCase_Succeeds ()
  326. {
  327. bool supportFirstUpperCase = true;
  328. var text = ustring.Empty;
  329. Rune hotKeySpecifier = (Rune)0;
  330. int hotPos = 0;
  331. Key hotKey = Key.Unknown;
  332. bool result = false;
  333. text = "K Before";
  334. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  335. Assert.True (result);
  336. Assert.Equal (0, hotPos);
  337. Assert.Equal ((Key)'K', hotKey);
  338. text = "aK Second";
  339. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  340. Assert.True (result);
  341. Assert.Equal (1, hotPos);
  342. Assert.Equal ((Key)'K', hotKey);
  343. text = "last K";
  344. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  345. Assert.True (result);
  346. Assert.Equal (5, hotPos);
  347. Assert.Equal ((Key)'K', hotKey);
  348. text = "multiple K and R";
  349. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  350. Assert.True (result);
  351. Assert.Equal (9, hotPos);
  352. Assert.Equal ((Key)'K', hotKey);
  353. // Cryllic K (К)
  354. text = "non-english: Кдать";
  355. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  356. Assert.True (result);
  357. Assert.Equal (13, hotPos);
  358. Assert.Equal ((Key)'К', hotKey);
  359. }
  360. [Fact]
  361. public void FindHotKey_Legacy_FirstUpperCase_NotFound_Returns_False ()
  362. {
  363. bool supportFirstUpperCase = true;
  364. var text = ustring.Empty;
  365. Rune hotKeySpecifier = (Rune)0;
  366. int hotPos = 0;
  367. Key hotKey = Key.Unknown;
  368. bool result = false;
  369. text = "k before";
  370. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  371. Assert.False (result);
  372. Assert.Equal (-1, hotPos);
  373. Assert.Equal (Key.Unknown, hotKey);
  374. text = "ak second";
  375. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  376. Assert.False (result);
  377. Assert.Equal (-1, hotPos);
  378. Assert.Equal (Key.Unknown, hotKey);
  379. text = "last k";
  380. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  381. Assert.False (result);
  382. Assert.Equal (-1, hotPos);
  383. Assert.Equal (Key.Unknown, hotKey);
  384. text = "multiple k and r";
  385. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  386. Assert.False (result);
  387. Assert.Equal (-1, hotPos);
  388. Assert.Equal (Key.Unknown, hotKey);
  389. text = "12345";
  390. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  391. Assert.False (result);
  392. Assert.Equal (-1, hotPos);
  393. Assert.Equal (Key.Unknown, hotKey);
  394. // punctuation
  395. text = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
  396. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  397. Assert.False (result);
  398. Assert.Equal (-1, hotPos);
  399. Assert.Equal (Key.Unknown, hotKey);
  400. // ~IsLetterOrDigit + Unicode
  401. text = " ~  s  gui.cs   master ↑10";
  402. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  403. Assert.False (result);
  404. Assert.Equal (-1, hotPos);
  405. Assert.Equal (Key.Unknown, hotKey);
  406. // Lower case Cryllic K (к)
  407. text = "non-english: кдать";
  408. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  409. Assert.False (result);
  410. Assert.Equal (-1, hotPos);
  411. Assert.Equal (Key.Unknown, hotKey);
  412. }
  413. static ustring testHotKeyAtStart = "_K Before";
  414. static ustring testHotKeyAtSecondPos = "a_K Second";
  415. static ustring testHotKeyAtLastPos = "Last _K";
  416. static ustring testHotKeyAfterLastChar = "After K_";
  417. static ustring testMultiHotKeys = "Multiple _K and _R";
  418. static ustring testNonEnglish = "Non-english: _Кдать";
  419. [Fact]
  420. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal ()
  421. {
  422. Rune hotKeySpecifier = '_';
  423. Assert.Null (TextFormatter.RemoveHotKeySpecifier (null, 0, hotKeySpecifier));
  424. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 0, hotKeySpecifier));
  425. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", -1, hotKeySpecifier));
  426. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 100, hotKeySpecifier));
  427. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", -1, hotKeySpecifier));
  428. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", 100, hotKeySpecifier));
  429. }
  430. [Fact]
  431. public void RemoveHotKeySpecifier_Valid_ReturnsStripped ()
  432. {
  433. Rune hotKeySpecifier = '_';
  434. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("_K Before", 0, hotKeySpecifier));
  435. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("a_K Second", 1, hotKeySpecifier));
  436. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last _K", 5, hotKeySpecifier));
  437. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K_", 7, hotKeySpecifier));
  438. Assert.Equal ("Multiple K and _R", TextFormatter.RemoveHotKeySpecifier ("Multiple _K and _R", 9, hotKeySpecifier));
  439. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: _Кдать", 13, hotKeySpecifier));
  440. }
  441. [Fact]
  442. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal ()
  443. {
  444. Rune hotKeySpecifier = '_';
  445. Assert.Equal ("all lower case", TextFormatter.RemoveHotKeySpecifier ("all lower case", 0, hotKeySpecifier));
  446. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("K Before", 0, hotKeySpecifier));
  447. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("aK Second", 1, hotKeySpecifier));
  448. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last K", 5, hotKeySpecifier));
  449. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K", 7, hotKeySpecifier));
  450. Assert.Equal ("Multiple K and R", TextFormatter.RemoveHotKeySpecifier ("Multiple K and R", 9, hotKeySpecifier));
  451. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: Кдать", 13, hotKeySpecifier));
  452. }
  453. [Fact]
  454. public void CalcRect_Invalid_Returns_Empty ()
  455. {
  456. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, null));
  457. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, ""));
  458. Assert.Equal (new Rect (new Point (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, ""));
  459. Assert.Equal (new Rect (new Point (-1, -2), Size.Empty), TextFormatter.CalcRect (-1, -2, ""));
  460. }
  461. [Fact]
  462. public void CalcRect_SingleLine_Returns_1High ()
  463. {
  464. var text = ustring.Empty;
  465. text = "test";
  466. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  467. text = " ~  s  gui.cs   master ↑10";
  468. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  469. }
  470. [Fact]
  471. public void CalcRect_MultiLine_Returns_nHigh ()
  472. {
  473. var text = ustring.Empty;
  474. var lines = 0;
  475. text = "line1\nline2";
  476. lines = 2;
  477. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  478. text = "\nline2";
  479. lines = 2;
  480. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  481. text = "\n\n";
  482. lines = 3;
  483. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  484. text = "\n\n\n";
  485. lines = 4;
  486. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  487. text = "line1\nline2\nline3long!";
  488. lines = 3;
  489. Assert.Equal (new Rect (0, 0, 10, lines), TextFormatter.CalcRect (0, 0, text));
  490. text = "line1\nline2\n\n";
  491. lines = 4;
  492. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  493. text = "line1\r\nline2";
  494. lines = 2;
  495. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  496. text = " ~  s  gui.cs   master ↑10\n";
  497. lines = 2;
  498. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  499. text = "\n ~  s  gui.cs   master ↑10";
  500. lines = 2;
  501. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  502. text = " ~  s  gui.cs   master\n↑10";
  503. lines = 2;
  504. Assert.Equal (new Rect (0, 0, ustring.Make (" ~  s  gui.cs   master\n").RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  505. }
  506. [Fact]
  507. public void ClipAndJustify_Invalid_Returns_Original ()
  508. {
  509. var text = ustring.Empty;
  510. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  511. text = null;
  512. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  513. text = "test";
  514. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.ClipAndJustify (text, -1, TextAlignment.Left));
  515. }
  516. [Fact]
  517. public void ClipAndJustify_Valid_Left ()
  518. {
  519. var align = TextAlignment.Left;
  520. var text = ustring.Empty;
  521. var justifiedText = ustring.Empty;
  522. int maxWidth = 0;
  523. int expectedClippedWidth = 0;
  524. text = "test";
  525. maxWidth = 0;
  526. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  527. text = "test";
  528. maxWidth = 2;
  529. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  530. text = "test";
  531. maxWidth = int.MaxValue;
  532. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  533. Assert.True (justifiedText.RuneCount <= maxWidth);
  534. text = "A sentence has words.";
  535. // should fit
  536. maxWidth = text.RuneCount + 1;
  537. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  538. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  539. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  540. Assert.True (expectedClippedWidth <= maxWidth);
  541. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  542. // Should fit.
  543. maxWidth = text.RuneCount + 0;
  544. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  545. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  546. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  547. Assert.True (expectedClippedWidth <= maxWidth);
  548. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  549. // Should fit.
  550. maxWidth = int.MaxValue;
  551. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  552. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  553. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  554. Assert.True (expectedClippedWidth <= maxWidth);
  555. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  556. // Should not fit
  557. maxWidth = text.RuneCount - 1;
  558. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  559. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  560. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  561. Assert.True (expectedClippedWidth <= maxWidth);
  562. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  563. // Should not fit
  564. maxWidth = 10;
  565. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  566. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  567. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  568. Assert.True (expectedClippedWidth <= maxWidth);
  569. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  570. text = "A\tsentence\thas\twords.";
  571. maxWidth = int.MaxValue;
  572. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  573. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  574. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  575. Assert.True (expectedClippedWidth <= maxWidth);
  576. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  577. text = "A\tsentence\thas\twords.";
  578. maxWidth = 10;
  579. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  580. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  581. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  582. Assert.True (expectedClippedWidth <= maxWidth);
  583. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  584. text = "line1\nline2\nline3long!";
  585. maxWidth = int.MaxValue;
  586. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  587. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  588. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  589. Assert.True (expectedClippedWidth <= maxWidth);
  590. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  591. text = "line1\nline2\nline3long!";
  592. maxWidth = 10;
  593. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  594. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  595. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  596. Assert.True (expectedClippedWidth <= maxWidth);
  597. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  598. // Unicode
  599. text = " ~  s  gui.cs   master ↑10";
  600. maxWidth = 10;
  601. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  602. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  603. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  604. Assert.True (expectedClippedWidth <= maxWidth);
  605. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  606. // should fit
  607. text = "Ð ÑÐ";
  608. maxWidth = text.RuneCount + 1;
  609. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  610. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  611. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  612. Assert.True (expectedClippedWidth <= maxWidth);
  613. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  614. // Should fit.
  615. maxWidth = text.RuneCount + 0;
  616. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  617. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  618. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  619. Assert.True (expectedClippedWidth <= maxWidth);
  620. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  621. // Should not fit
  622. maxWidth = text.RuneCount - 1;
  623. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  624. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  625. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  626. Assert.True (expectedClippedWidth <= maxWidth);
  627. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  628. }
  629. [Fact]
  630. public void ClipAndJustify_Valid_Right ()
  631. {
  632. var align = TextAlignment.Right;
  633. var text = ustring.Empty;
  634. var justifiedText = ustring.Empty;
  635. int maxWidth = 0;
  636. int expectedClippedWidth = 0;
  637. text = "test";
  638. maxWidth = 0;
  639. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  640. text = "test";
  641. maxWidth = 2;
  642. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  643. text = "test";
  644. maxWidth = int.MaxValue;
  645. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  646. Assert.True (justifiedText.RuneCount <= maxWidth);
  647. text = "A sentence has words.";
  648. // should fit
  649. maxWidth = text.RuneCount + 1;
  650. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  651. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  652. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  653. Assert.True (expectedClippedWidth <= maxWidth);
  654. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  655. // Should fit.
  656. maxWidth = text.RuneCount + 0;
  657. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  658. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  659. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  660. Assert.True (expectedClippedWidth <= maxWidth);
  661. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  662. // Should fit.
  663. maxWidth = int.MaxValue;
  664. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  665. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  666. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  667. Assert.True (expectedClippedWidth <= maxWidth);
  668. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  669. // Should not fit
  670. maxWidth = text.RuneCount - 1;
  671. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  672. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  673. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  674. Assert.True (expectedClippedWidth <= maxWidth);
  675. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  676. // Should not fit
  677. maxWidth = 10;
  678. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  679. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  680. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  681. Assert.True (expectedClippedWidth <= maxWidth);
  682. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  683. text = "A\tsentence\thas\twords.";
  684. maxWidth = int.MaxValue;
  685. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  686. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  687. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  688. Assert.True (expectedClippedWidth <= maxWidth);
  689. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  690. text = "A\tsentence\thas\twords.";
  691. maxWidth = 10;
  692. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  693. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  694. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  695. Assert.True (expectedClippedWidth <= maxWidth);
  696. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  697. text = "line1\nline2\nline3long!";
  698. maxWidth = int.MaxValue;
  699. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  700. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  701. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  702. Assert.True (expectedClippedWidth <= maxWidth);
  703. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  704. text = "line1\nline2\nline3long!";
  705. maxWidth = 10;
  706. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  707. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  708. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  709. Assert.True (expectedClippedWidth <= maxWidth);
  710. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  711. // Unicode
  712. text = " ~  s  gui.cs   master ↑10";
  713. maxWidth = 10;
  714. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  715. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  716. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  717. Assert.True (expectedClippedWidth <= maxWidth);
  718. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  719. // should fit
  720. text = "Ð ÑÐ";
  721. maxWidth = text.RuneCount + 1;
  722. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  723. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  724. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  725. Assert.True (expectedClippedWidth <= maxWidth);
  726. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  727. // Should fit.
  728. maxWidth = text.RuneCount + 0;
  729. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  730. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  731. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  732. Assert.True (expectedClippedWidth <= maxWidth);
  733. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  734. // Should not fit
  735. maxWidth = text.RuneCount - 1;
  736. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  737. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  738. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  739. Assert.True (expectedClippedWidth <= maxWidth);
  740. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  741. }
  742. [Fact]
  743. public void ClipAndJustify_Valid_Centered ()
  744. {
  745. var align = TextAlignment.Centered;
  746. var text = ustring.Empty;
  747. var justifiedText = ustring.Empty;
  748. int maxWidth = 0;
  749. int expectedClippedWidth = 0;
  750. text = "test";
  751. maxWidth = 0;
  752. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  753. text = "test";
  754. maxWidth = 2;
  755. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  756. text = "test";
  757. maxWidth = int.MaxValue;
  758. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  759. Assert.True (justifiedText.RuneCount <= maxWidth);
  760. text = "A sentence has words.";
  761. // should fit
  762. maxWidth = text.RuneCount + 1;
  763. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  764. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  765. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  766. Assert.True (expectedClippedWidth <= maxWidth);
  767. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  768. // Should fit.
  769. maxWidth = text.RuneCount + 0;
  770. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  771. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  772. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  773. Assert.True (expectedClippedWidth <= maxWidth);
  774. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  775. // Should fit.
  776. maxWidth = int.MaxValue;
  777. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  778. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  779. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  780. Assert.True (expectedClippedWidth <= maxWidth);
  781. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  782. // Should not fit
  783. maxWidth = text.RuneCount - 1;
  784. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  785. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  786. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  787. Assert.True (expectedClippedWidth <= maxWidth);
  788. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  789. // Should not fit
  790. maxWidth = 10;
  791. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  792. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  793. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  794. Assert.True (expectedClippedWidth <= maxWidth);
  795. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  796. text = "A\tsentence\thas\twords.";
  797. maxWidth = int.MaxValue;
  798. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  799. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  800. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  801. Assert.True (expectedClippedWidth <= maxWidth);
  802. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  803. text = "A\tsentence\thas\twords.";
  804. maxWidth = 10;
  805. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  806. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  807. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  808. Assert.True (expectedClippedWidth <= maxWidth);
  809. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  810. text = "line1\nline2\nline3long!";
  811. maxWidth = int.MaxValue;
  812. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  813. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  814. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  815. Assert.True (expectedClippedWidth <= maxWidth);
  816. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  817. text = "line1\nline2\nline3long!";
  818. maxWidth = 10;
  819. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  820. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  821. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  822. Assert.True (expectedClippedWidth <= maxWidth);
  823. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  824. // Unicode
  825. text = " ~  s  gui.cs   master ↑10";
  826. maxWidth = 10;
  827. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  828. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  829. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  830. Assert.True (expectedClippedWidth <= maxWidth);
  831. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  832. // should fit
  833. text = "Ð ÑÐ";
  834. maxWidth = text.RuneCount + 1;
  835. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  836. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  837. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  838. Assert.True (expectedClippedWidth <= maxWidth);
  839. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  840. // Should fit.
  841. maxWidth = text.RuneCount + 0;
  842. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  843. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  844. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  845. Assert.True (expectedClippedWidth <= maxWidth);
  846. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  847. // Should not fit
  848. maxWidth = text.RuneCount - 1;
  849. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  850. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  851. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  852. Assert.True (expectedClippedWidth <= maxWidth);
  853. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  854. }
  855. [Fact]
  856. public void ClipAndJustify_Valid_Justified ()
  857. {
  858. var align = TextAlignment.Justified;
  859. var text = ustring.Empty;
  860. var justifiedText = ustring.Empty;
  861. int maxWidth = 0;
  862. int expectedClippedWidth = 0;
  863. text = "test";
  864. maxWidth = 0;
  865. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  866. text = "test";
  867. maxWidth = 2;
  868. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  869. text = "test";
  870. maxWidth = int.MaxValue;
  871. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  872. Assert.True (justifiedText.RuneCount <= maxWidth);
  873. text = "A sentence has words.";
  874. // should fit
  875. maxWidth = text.RuneCount + 1;
  876. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  877. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  878. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  879. Assert.True (expectedClippedWidth <= maxWidth);
  880. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  881. // Should fit.
  882. maxWidth = text.RuneCount + 0;
  883. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  884. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  885. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  886. Assert.True (expectedClippedWidth <= maxWidth);
  887. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  888. // Should fit.
  889. maxWidth = 500;
  890. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  891. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  892. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  893. Assert.True (expectedClippedWidth <= maxWidth);
  894. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  895. // Should not fit
  896. maxWidth = text.RuneCount - 1;
  897. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  898. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  899. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  900. Assert.True (expectedClippedWidth <= maxWidth);
  901. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  902. // Should not fit
  903. maxWidth = 10;
  904. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  905. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  906. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  907. Assert.True (expectedClippedWidth <= maxWidth);
  908. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  909. text = "A\tsentence\thas\twords.";
  910. maxWidth = int.MaxValue;
  911. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  912. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  913. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  914. Assert.True (expectedClippedWidth <= maxWidth);
  915. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  916. text = "A\tsentence\thas\twords.";
  917. maxWidth = 10;
  918. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  919. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  920. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  921. Assert.True (expectedClippedWidth <= maxWidth);
  922. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  923. text = "line1\nline2\nline3long!";
  924. maxWidth = int.MaxValue;
  925. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  926. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  927. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  928. Assert.True (expectedClippedWidth <= maxWidth);
  929. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  930. text = "line1\nline2\nline3long!";
  931. maxWidth = 10;
  932. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  933. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  934. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  935. Assert.True (expectedClippedWidth <= maxWidth);
  936. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  937. // Unicode
  938. text = " ~  s  gui.cs   master ↑10";
  939. maxWidth = 10;
  940. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  941. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  942. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  943. Assert.True (expectedClippedWidth <= maxWidth);
  944. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  945. // should fit
  946. text = "Ð ÑÐ";
  947. maxWidth = text.RuneCount + 1;
  948. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  949. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  950. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  951. Assert.True (expectedClippedWidth <= maxWidth);
  952. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  953. // Should fit.
  954. maxWidth = text.RuneCount + 0;
  955. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  956. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  957. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  958. Assert.True (expectedClippedWidth <= maxWidth);
  959. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  960. // Should not fit
  961. maxWidth = text.RuneCount - 1;
  962. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  963. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  964. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  965. Assert.True (expectedClippedWidth <= maxWidth);
  966. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  967. // see Justify_ tests below
  968. }
  969. [Fact]
  970. public void Justify_Invalid ()
  971. {
  972. var text = ustring.Empty;
  973. Assert.Equal (text, TextFormatter.Justify (text, 0));
  974. text = null;
  975. Assert.Equal (text, TextFormatter.Justify (text, 0));
  976. text = "test";
  977. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  978. }
  979. [Fact]
  980. public void Justify_SingleWord ()
  981. {
  982. var text = ustring.Empty;
  983. var justifiedText = ustring.Empty;
  984. int width = 0;
  985. char fillChar = '+';
  986. // Even # of chars
  987. text = "word";
  988. justifiedText = text;
  989. width = text.RuneCount;
  990. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  991. width = text.RuneCount + 1;
  992. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  993. width = text.RuneCount + 2;
  994. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  995. width = text.RuneCount + 10;
  996. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  997. width = text.RuneCount + 11;
  998. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  999. // Odd # of chars
  1000. text = "word.";
  1001. justifiedText = text;
  1002. width = text.RuneCount;
  1003. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1004. width = text.RuneCount + 1;
  1005. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1006. width = text.RuneCount + 2;
  1007. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1008. width = text.RuneCount + 10;
  1009. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1010. width = text.RuneCount + 11;
  1011. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1012. // Unicode (even #)
  1013. text = "пÑивеÑ";
  1014. justifiedText = text;
  1015. width = text.RuneCount;
  1016. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1017. width = text.RuneCount + 1;
  1018. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1019. width = text.RuneCount + 2;
  1020. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1021. width = text.RuneCount + 10;
  1022. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1023. width = text.RuneCount + 11;
  1024. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1025. // Unicode (odd # of chars)
  1026. text = "пÑивеÑ.";
  1027. justifiedText = text;
  1028. width = text.RuneCount;
  1029. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1030. width = text.RuneCount + 1;
  1031. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1032. width = text.RuneCount + 2;
  1033. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1034. width = text.RuneCount + 10;
  1035. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1036. width = text.RuneCount + 11;
  1037. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1038. }
  1039. [Fact]
  1040. public void Justify_Sentence ()
  1041. {
  1042. var text = ustring.Empty;
  1043. var justifiedText = ustring.Empty;
  1044. int forceToWidth = 0;
  1045. char fillChar = '+';
  1046. // Even # of spaces
  1047. // 0123456789
  1048. text = "012 456 89";
  1049. forceToWidth = text.RuneCount;
  1050. justifiedText = text.Replace (" ", "+");
  1051. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1052. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1053. justifiedText = text.Replace (" ", "+");
  1054. forceToWidth = text.RuneCount + 1;
  1055. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1056. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1057. justifiedText = text.Replace (" ", "++");
  1058. forceToWidth = text.RuneCount + 2;
  1059. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1060. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1061. justifiedText = text.Replace (" ", "++");
  1062. forceToWidth = text.RuneCount + 3;
  1063. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1064. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1065. justifiedText = text.Replace (" ", "+++");
  1066. forceToWidth = text.RuneCount + 4;
  1067. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1068. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1069. justifiedText = text.Replace (" ", "+++");
  1070. forceToWidth = text.RuneCount + 5;
  1071. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1072. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1073. justifiedText = text.Replace (" ", "++++");
  1074. forceToWidth = text.RuneCount + 6;
  1075. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1076. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1077. justifiedText = text.Replace (" ", "+++++++++++");
  1078. forceToWidth = text.RuneCount + 20;
  1079. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1080. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1081. justifiedText = text.Replace (" ", "++++++++++++");
  1082. forceToWidth = text.RuneCount + 23;
  1083. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1084. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1085. // Odd # of spaces
  1086. // 0123456789
  1087. text = "012 456 89 end";
  1088. forceToWidth = text.RuneCount;
  1089. justifiedText = text.Replace (" ", "+");
  1090. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1091. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1092. justifiedText = text.Replace (" ", "+");
  1093. forceToWidth = text.RuneCount + 1;
  1094. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1095. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1096. justifiedText = text.Replace (" ", "+");
  1097. forceToWidth = text.RuneCount + 2;
  1098. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1099. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1100. justifiedText = text.Replace (" ", "++");
  1101. forceToWidth = text.RuneCount + 3;
  1102. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1103. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1104. justifiedText = text.Replace (" ", "++");
  1105. forceToWidth = text.RuneCount + 4;
  1106. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1107. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1108. justifiedText = text.Replace (" ", "++");
  1109. forceToWidth = text.RuneCount + 5;
  1110. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1111. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1112. justifiedText = text.Replace (" ", "+++");
  1113. forceToWidth = text.RuneCount + 6;
  1114. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1115. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1116. justifiedText = text.Replace (" ", "+++++++");
  1117. forceToWidth = text.RuneCount + 20;
  1118. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1119. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1120. justifiedText = text.Replace (" ", "++++++++");
  1121. forceToWidth = text.RuneCount + 23;
  1122. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1123. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1124. // Unicode
  1125. // Even # of chars
  1126. // 0123456789
  1127. text = "пÑРвРÑ";
  1128. forceToWidth = text.RuneCount;
  1129. justifiedText = text.Replace (" ", "+");
  1130. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1131. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1132. justifiedText = text.Replace (" ", "+");
  1133. forceToWidth = text.RuneCount + 1;
  1134. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1135. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1136. justifiedText = text.Replace (" ", "++");
  1137. forceToWidth = text.RuneCount + 2;
  1138. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1139. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1140. justifiedText = text.Replace (" ", "++");
  1141. forceToWidth = text.RuneCount + 3;
  1142. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1143. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1144. justifiedText = text.Replace (" ", "+++");
  1145. forceToWidth = text.RuneCount + 4;
  1146. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1147. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1148. justifiedText = text.Replace (" ", "+++");
  1149. forceToWidth = text.RuneCount + 5;
  1150. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1151. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1152. justifiedText = text.Replace (" ", "++++");
  1153. forceToWidth = text.RuneCount + 6;
  1154. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1155. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1156. justifiedText = text.Replace (" ", "+++++++++++");
  1157. forceToWidth = text.RuneCount + 20;
  1158. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1159. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1160. justifiedText = text.Replace (" ", "++++++++++++");
  1161. forceToWidth = text.RuneCount + 23;
  1162. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1163. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1164. // Unicode
  1165. // Odd # of chars
  1166. // 0123456789
  1167. text = "Ð ÑРвРÑ";
  1168. forceToWidth = text.RuneCount;
  1169. justifiedText = text.Replace (" ", "+");
  1170. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1171. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1172. justifiedText = text.Replace (" ", "+");
  1173. forceToWidth = text.RuneCount + 1;
  1174. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1175. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1176. justifiedText = text.Replace (" ", "+");
  1177. forceToWidth = text.RuneCount + 2;
  1178. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1179. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1180. justifiedText = text.Replace (" ", "++");
  1181. forceToWidth = text.RuneCount + 3;
  1182. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1183. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1184. justifiedText = text.Replace (" ", "++");
  1185. forceToWidth = text.RuneCount + 4;
  1186. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1187. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1188. justifiedText = text.Replace (" ", "++");
  1189. forceToWidth = text.RuneCount + 5;
  1190. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1191. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1192. justifiedText = text.Replace (" ", "+++");
  1193. forceToWidth = text.RuneCount + 6;
  1194. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1195. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1196. justifiedText = text.Replace (" ", "+++++++");
  1197. forceToWidth = text.RuneCount + 20;
  1198. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1199. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1200. justifiedText = text.Replace (" ", "++++++++");
  1201. forceToWidth = text.RuneCount + 23;
  1202. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1203. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1204. }
  1205. [Fact]
  1206. public void WordWrap_Invalid ()
  1207. {
  1208. var text = ustring.Empty;
  1209. int width = 0;
  1210. Assert.Empty (TextFormatter.WordWrap (null, width));
  1211. Assert.Empty (TextFormatter.WordWrap (text, width));
  1212. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrap (text, -1));
  1213. }
  1214. [Fact]
  1215. public void WordWrap_SingleWordLine ()
  1216. {
  1217. var text = ustring.Empty;
  1218. int width = 0;
  1219. List<ustring> wrappedLines;
  1220. text = "Constantinople";
  1221. width = text.RuneCount;
  1222. wrappedLines = TextFormatter.WordWrap (text, width);
  1223. Assert.True (wrappedLines.Count == 1);
  1224. width = text.RuneCount - 1;
  1225. wrappedLines = TextFormatter.WordWrap (text, width);
  1226. Assert.Equal (2, wrappedLines.Count);
  1227. Assert.Equal (text [0, text.RuneCount - 1].ToString (), wrappedLines [0].ToString ());
  1228. Assert.Equal ("e", wrappedLines [1].ToString ());
  1229. width = text.RuneCount - 2;
  1230. wrappedLines = TextFormatter.WordWrap (text, width);
  1231. Assert.Equal (2, wrappedLines.Count);
  1232. Assert.Equal (text [0, text.RuneCount - 2].ToString (), wrappedLines [0].ToString ());
  1233. width = text.RuneCount - 5;
  1234. wrappedLines = TextFormatter.WordWrap (text, width);
  1235. Assert.Equal (2, wrappedLines.Count);
  1236. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1237. wrappedLines = TextFormatter.WordWrap (text, width);
  1238. Assert.Equal (2, wrappedLines.Count);
  1239. Assert.Equal ("Constan", wrappedLines [0].ToString ());
  1240. Assert.Equal ("tinople", wrappedLines [1].ToString ());
  1241. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1242. wrappedLines = TextFormatter.WordWrap (text, width);
  1243. Assert.Equal (3, wrappedLines.Count);
  1244. Assert.Equal ("Const", wrappedLines [0].ToString ());
  1245. Assert.Equal ("antin", wrappedLines [1].ToString ());
  1246. Assert.Equal ("ople", wrappedLines [2].ToString ());
  1247. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1248. wrappedLines = TextFormatter.WordWrap (text, width);
  1249. Assert.Equal (4, wrappedLines.Count);
  1250. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1251. wrappedLines = TextFormatter.WordWrap (text, width);
  1252. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1253. Assert.Equal ("C", wrappedLines [0].ToString ());
  1254. Assert.Equal ("o", wrappedLines [1].ToString ());
  1255. Assert.Equal ("n", wrappedLines [2].ToString ());
  1256. Assert.Equal ("s", wrappedLines [3].ToString ());
  1257. Assert.Equal ("t", wrappedLines [4].ToString ());
  1258. Assert.Equal ("a", wrappedLines [5].ToString ());
  1259. Assert.Equal ("n", wrappedLines [6].ToString ());
  1260. Assert.Equal ("t", wrappedLines [7].ToString ());
  1261. Assert.Equal ("i", wrappedLines [8].ToString ());
  1262. Assert.Equal ("n", wrappedLines [9].ToString ());
  1263. Assert.Equal ("o", wrappedLines [10].ToString ());
  1264. Assert.Equal ("p", wrappedLines [11].ToString ());
  1265. Assert.Equal ("l", wrappedLines [12].ToString ());
  1266. Assert.Equal ("e", wrappedLines [13].ToString ());
  1267. }
  1268. [Fact]
  1269. public void WordWrap_Unicode_SingleWordLine ()
  1270. {
  1271. var text = ustring.Empty;
  1272. int width = 0;
  1273. List<ustring> wrappedLines;
  1274. text = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ";
  1275. width = text.RuneCount;
  1276. wrappedLines = TextFormatter.WordWrap (text, width);
  1277. Assert.True (wrappedLines.Count == 1);
  1278. width = text.RuneCount - 1;
  1279. wrappedLines = TextFormatter.WordWrap (text, width);
  1280. Assert.Equal (2, wrappedLines.Count);
  1281. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1282. Assert.Equal ("ำ", wrappedLines [1].ToString ());
  1283. width = text.RuneCount - 2;
  1284. wrappedLines = TextFormatter.WordWrap (text, width);
  1285. Assert.Equal (2, wrappedLines.Count);
  1286. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1287. width = text.RuneCount - 5;
  1288. wrappedLines = TextFormatter.WordWrap (text, width);
  1289. Assert.Equal (2, wrappedLines.Count);
  1290. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1291. wrappedLines = TextFormatter.WordWrap (text, width);
  1292. Assert.Equal (2, wrappedLines.Count);
  1293. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", wrappedLines [0].ToString ());
  1294. Assert.Equal ("ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [1].ToString ());
  1295. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1296. wrappedLines = TextFormatter.WordWrap (text, width);
  1297. Assert.Equal (3, wrappedLines.Count);
  1298. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑ", wrappedLines [0].ToString ());
  1299. Assert.Equal ("ฒณดตถทธนบปผฝพฟภมย", wrappedLines [1].ToString ());
  1300. Assert.Equal ("รฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [2].ToString ());
  1301. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1302. wrappedLines = TextFormatter.WordWrap (text, width);
  1303. Assert.Equal (4, wrappedLines.Count);
  1304. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1305. wrappedLines = TextFormatter.WordWrap (text, width);
  1306. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1307. Assert.Equal ("ก", wrappedLines [0].ToString ());
  1308. Assert.Equal ("ข", wrappedLines [1].ToString ());
  1309. Assert.Equal ("ฃ", wrappedLines [2].ToString ());
  1310. Assert.Equal ("ำ", wrappedLines [^1].ToString ());
  1311. }
  1312. [Fact]
  1313. public void WordWrap_Unicode_LineWithNonBreakingSpace ()
  1314. {
  1315. var text = ustring.Empty;
  1316. int width = 0;
  1317. List<ustring> wrappedLines;
  1318. text = "This\u00A0is\u00A0a\u00A0sentence.";
  1319. width = text.RuneCount;
  1320. wrappedLines = TextFormatter.WordWrap (text, width);
  1321. Assert.True (wrappedLines.Count == 1);
  1322. width = text.RuneCount - 1;
  1323. wrappedLines = TextFormatter.WordWrap (text, width);
  1324. Assert.Equal (2, wrappedLines.Count);
  1325. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1326. Assert.Equal (".", wrappedLines [1].ToString ());
  1327. width = text.RuneCount - 2;
  1328. wrappedLines = TextFormatter.WordWrap (text, width);
  1329. Assert.Equal (2, wrappedLines.Count);
  1330. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1331. width = text.RuneCount - 5;
  1332. wrappedLines = TextFormatter.WordWrap (text, width);
  1333. Assert.Equal (2, wrappedLines.Count);
  1334. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1335. wrappedLines = TextFormatter.WordWrap (text, width);
  1336. Assert.Equal (2, wrappedLines.Count);
  1337. Assert.Equal ("This\u00A0is\u00A0a\u00A0", wrappedLines [0].ToString ());
  1338. Assert.Equal ("sentence.", wrappedLines [1].ToString ());
  1339. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1340. wrappedLines = TextFormatter.WordWrap (text, width);
  1341. Assert.Equal (3, wrappedLines.Count);
  1342. Assert.Equal ("This\u00A0is", wrappedLines [0].ToString ());
  1343. Assert.Equal ("\u00a0a\u00a0sent", wrappedLines [1].ToString ());
  1344. Assert.Equal ("ence.", wrappedLines [2].ToString ());
  1345. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1346. wrappedLines = TextFormatter.WordWrap (text, width);
  1347. Assert.Equal (4, wrappedLines.Count);
  1348. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1349. wrappedLines = TextFormatter.WordWrap (text, width);
  1350. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1351. Assert.Equal ("T", wrappedLines [0].ToString ());
  1352. Assert.Equal ("h", wrappedLines [1].ToString ());
  1353. Assert.Equal ("i", wrappedLines [2].ToString ());
  1354. Assert.Equal (".", wrappedLines [^1].ToString ());
  1355. }
  1356. [Fact]
  1357. public void WordWrap_Unicode_2LinesWithNonBreakingSpace ()
  1358. {
  1359. var text = ustring.Empty;
  1360. int width = 0;
  1361. List<ustring> wrappedLines;
  1362. text = "This\u00A0is\n\u00A0a\u00A0sentence.";
  1363. width = text.RuneCount;
  1364. wrappedLines = TextFormatter.WordWrap (text, width);
  1365. Assert.True (wrappedLines.Count == 1);
  1366. width = text.RuneCount - 1;
  1367. wrappedLines = TextFormatter.WordWrap (text, width);
  1368. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1369. Assert.Equal (1, wrappedLines.Count);
  1370. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1371. Assert.Equal ("This\u00A0is\u00A0a\u00A0sentence.", wrappedLines [0].ToString ());
  1372. text = "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.";
  1373. width = text.RuneCount;
  1374. wrappedLines = TextFormatter.WordWrap (text, width);
  1375. Assert.True (wrappedLines.Count == 1);
  1376. }
  1377. [Fact]
  1378. public void WordWrap_NoNewLines ()
  1379. {
  1380. var text = ustring.Empty;
  1381. int maxWidth = 0;
  1382. int expectedClippedWidth = 0;
  1383. List<ustring> wrappedLines;
  1384. text = "A sentence has words.";
  1385. maxWidth = text.RuneCount;
  1386. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1387. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1388. Assert.True (wrappedLines.Count == 1);
  1389. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1390. Assert.Equal ("A sentence has words.", wrappedLines [0].ToString ());
  1391. maxWidth = text.RuneCount - 1;
  1392. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1393. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1394. Assert.Equal (2, wrappedLines.Count);
  1395. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1396. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1397. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1398. maxWidth = text.RuneCount - "words.".Length;
  1399. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1400. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1401. Assert.Equal (2, wrappedLines.Count);
  1402. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1403. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1404. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1405. maxWidth = text.RuneCount - " words.".Length;
  1406. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1407. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1408. Assert.Equal (2, wrappedLines.Count);
  1409. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1410. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1411. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1412. maxWidth = text.RuneCount - "s words.".Length;
  1413. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1414. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1415. Assert.Equal (2, wrappedLines.Count);
  1416. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1417. Assert.Equal ("A sentence", wrappedLines [0].ToString ());
  1418. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1419. // Unicode
  1420. text = "A Unicode sentence (пÑивеÑ) has words.";
  1421. maxWidth = text.RuneCount;
  1422. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1423. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1424. Assert.True (wrappedLines.Count == 1);
  1425. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1426. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.", wrappedLines [0].ToString ());
  1427. maxWidth = text.RuneCount - 1;
  1428. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1429. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1430. Assert.Equal (2, wrappedLines.Count);
  1431. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1432. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1433. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1434. maxWidth = text.RuneCount - "words.".Length;
  1435. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1436. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1437. Assert.Equal (2, wrappedLines.Count);
  1438. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1439. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1440. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1441. maxWidth = text.RuneCount - " words.".Length;
  1442. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1443. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1444. Assert.Equal (2, wrappedLines.Count);
  1445. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1446. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1447. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1448. maxWidth = text.RuneCount - "s words.".Length;
  1449. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1450. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1451. Assert.Equal (2, wrappedLines.Count);
  1452. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1453. Assert.Equal ("A Unicode sentence (пÑивеÑ)", wrappedLines [0].ToString ());
  1454. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1455. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1456. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1457. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1458. Assert.Equal (2, wrappedLines.Count);
  1459. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1460. Assert.Equal ("A Unicode sentence", wrappedLines [0].ToString ());
  1461. Assert.Equal ("(пÑивеÑ) has words.", wrappedLines [1].ToString ());
  1462. }
  1463. /// <summary>
  1464. /// WordWrap strips CRLF
  1465. /// </summary>
  1466. [Fact]
  1467. public void WordWrap_WithNewLines ()
  1468. {
  1469. var text = ustring.Empty;
  1470. int maxWidth = 0;
  1471. int expectedClippedWidth = 0;
  1472. List<ustring> wrappedLines;
  1473. text = "A sentence has words.\nA paragraph has lines.";
  1474. maxWidth = text.RuneCount;
  1475. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1476. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1477. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1478. Assert.Equal (1, wrappedLines.Count);
  1479. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1480. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1481. Assert.Equal ("A sentence has words.A paragraph has lines.", wrappedLines [0].ToString ());
  1482. maxWidth = text.RuneCount - 1;
  1483. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1484. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1485. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1486. Assert.Equal (1, wrappedLines.Count);
  1487. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1488. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1489. Assert.Equal ("A sentence has words.A paragraph has lines.", wrappedLines [0].ToString ());
  1490. maxWidth = text.RuneCount - "words.".Length;
  1491. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1492. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1493. Assert.Equal (2, wrappedLines.Count);
  1494. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1495. Assert.Equal ("A sentence has words.A paragraph has", wrappedLines [0].ToString ());
  1496. Assert.Equal ("lines.", wrappedLines [1].ToString ());
  1497. // Unicode
  1498. text = "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии.";
  1499. maxWidth = text.RuneCount;
  1500. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1501. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1502. Assert.True (wrappedLines.Count == 1);
  1503. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1504. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии.", wrappedLines [0].ToString ());
  1505. maxWidth = text.RuneCount - 1;
  1506. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1507. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1508. Assert.Equal (2, wrappedLines.Count);
  1509. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1510. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", wrappedLines [0].ToString ());
  1511. Assert.Equal ("Линии.", wrappedLines [1].ToString ());
  1512. maxWidth = text.RuneCount - "words.".Length;
  1513. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1514. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1515. Assert.Equal (2, wrappedLines.Count);
  1516. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1517. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", wrappedLines [0].ToString ());
  1518. Assert.Equal ("Линии.", wrappedLines [1].ToString ());
  1519. maxWidth = text.RuneCount - "s words.".Length;
  1520. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1521. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1522. Assert.Equal (2, wrappedLines.Count);
  1523. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1524. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", wrappedLines [0].ToString ());
  1525. Assert.Equal ("has Линии.", wrappedLines [1].ToString ());
  1526. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1527. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1528. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1529. Assert.Equal (2, wrappedLines.Count);
  1530. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1531. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode", wrappedLines [0].ToString ());
  1532. Assert.Equal ("Пункт has Линии.", wrappedLines [1].ToString ());
  1533. }
  1534. [Fact]
  1535. public void WordWrap_Narrow ()
  1536. {
  1537. var text = ustring.Empty;
  1538. int maxWidth = 1;
  1539. int expectedClippedWidth = 1;
  1540. List<ustring> wrappedLines;
  1541. text = "A sentence has words.";
  1542. maxWidth = 3;
  1543. expectedClippedWidth = 3;
  1544. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1545. //Assert.True (wrappedLines.Count == );
  1546. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1547. Assert.Equal ("A", wrappedLines [0].ToString ());
  1548. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1549. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1550. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1551. Assert.Equal ("has", wrappedLines [4].ToString ());
  1552. Assert.Equal ("wor", wrappedLines [5].ToString ());
  1553. Assert.Equal ("ds.", wrappedLines [6].ToString ());
  1554. maxWidth = 2;
  1555. expectedClippedWidth = 2;
  1556. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1557. //Assert.True (wrappedLines.Count == );
  1558. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1559. Assert.Equal ("A", wrappedLines [0].ToString ());
  1560. Assert.Equal ("se", wrappedLines [1].ToString ());
  1561. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1562. Assert.Equal ("en", wrappedLines [3].ToString ());
  1563. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1564. maxWidth = 1;
  1565. expectedClippedWidth = 1;
  1566. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1567. //Assert.True (wrappedLines.Count == );
  1568. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1569. Assert.Equal ("A", wrappedLines [0].ToString ());
  1570. Assert.Equal ("s", wrappedLines [1].ToString ());
  1571. Assert.Equal ("e", wrappedLines [2].ToString ());
  1572. Assert.Equal ("n", wrappedLines [3].ToString ());
  1573. Assert.Equal (".", wrappedLines [^1].ToString ());
  1574. }
  1575. [Fact]
  1576. public void WordWrap_preserveTrailingSpaces ()
  1577. {
  1578. var text = ustring.Empty;
  1579. int maxWidth = 1;
  1580. int expectedClippedWidth = 1;
  1581. List<ustring> wrappedLines;
  1582. text = "A sentence has words.";
  1583. maxWidth = 14;
  1584. expectedClippedWidth = 14;
  1585. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1586. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1587. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1588. Assert.Equal (" words.", wrappedLines [1].ToString ());
  1589. Assert.True (wrappedLines.Count == 2);
  1590. maxWidth = 3;
  1591. expectedClippedWidth = 3;
  1592. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1593. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1594. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1595. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1596. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1597. Assert.Equal ("ce ", wrappedLines [3].ToString ());
  1598. Assert.Equal ("has", wrappedLines [4].ToString ());
  1599. Assert.Equal (" ", wrappedLines [5].ToString ());
  1600. Assert.Equal ("wor", wrappedLines [6].ToString ());
  1601. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  1602. Assert.True (wrappedLines.Count == 8);
  1603. maxWidth = 2;
  1604. expectedClippedWidth = 2;
  1605. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1606. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1607. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1608. Assert.Equal ("se", wrappedLines [1].ToString ());
  1609. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1610. Assert.Equal ("en", wrappedLines [3].ToString ());
  1611. Assert.Equal ("ce", wrappedLines [4].ToString ());
  1612. Assert.Equal (" ", wrappedLines [5].ToString ());
  1613. Assert.Equal ("ha", wrappedLines [6].ToString ());
  1614. Assert.Equal ("s ", wrappedLines [7].ToString ());
  1615. Assert.Equal ("wo", wrappedLines [8].ToString ());
  1616. Assert.Equal ("rd", wrappedLines [9].ToString ());
  1617. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1618. Assert.True (wrappedLines.Count == 11);
  1619. maxWidth = 1;
  1620. expectedClippedWidth = 1;
  1621. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1622. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1623. Assert.Equal ("A", wrappedLines [0].ToString ());
  1624. Assert.Equal (" ", wrappedLines [1].ToString ());
  1625. Assert.Equal ("s", wrappedLines [2].ToString ());
  1626. Assert.Equal ("e", wrappedLines [3].ToString ());
  1627. Assert.Equal ("n", wrappedLines [4].ToString ());
  1628. Assert.Equal ("t", wrappedLines [5].ToString ());
  1629. Assert.Equal ("e", wrappedLines [6].ToString ());
  1630. Assert.Equal ("n", wrappedLines [7].ToString ());
  1631. Assert.Equal ("c", wrappedLines [8].ToString ());
  1632. Assert.Equal ("e", wrappedLines [9].ToString ());
  1633. Assert.Equal (" ", wrappedLines [10].ToString ());
  1634. Assert.Equal (".", wrappedLines [^1].ToString ());
  1635. Assert.True (wrappedLines.Count == text.Length);
  1636. }
  1637. [Fact]
  1638. public void WordWrap_preserveTrailingSpaces_With_Tab ()
  1639. {
  1640. var text = ustring.Empty;
  1641. int maxWidth = 1;
  1642. int expectedClippedWidth = 1;
  1643. List<ustring> wrappedLines;
  1644. text = "A sentence\t\t\t has words.";
  1645. var tabWidth = 4;
  1646. maxWidth = 14;
  1647. expectedClippedWidth = 11;
  1648. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1649. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1650. Assert.Equal ("A sentence\t", wrappedLines [0].ToString ());
  1651. Assert.Equal ("\t\t has ", wrappedLines [1].ToString ());
  1652. Assert.Equal ("words.", wrappedLines [2].ToString ());
  1653. Assert.True (wrappedLines.Count == 3);
  1654. maxWidth = 3;
  1655. expectedClippedWidth = 3;
  1656. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1657. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1658. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1659. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1660. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1661. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1662. Assert.Equal ("\t", wrappedLines [4].ToString ());
  1663. Assert.Equal ("\t", wrappedLines [5].ToString ());
  1664. Assert.Equal ("\t", wrappedLines [6].ToString ());
  1665. Assert.Equal (" ", wrappedLines [7].ToString ());
  1666. Assert.Equal ("has", wrappedLines [8].ToString ());
  1667. Assert.Equal (" ", wrappedLines [9].ToString ());
  1668. Assert.Equal ("wor", wrappedLines [10].ToString ());
  1669. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  1670. Assert.True (wrappedLines.Count == 12);
  1671. maxWidth = 2;
  1672. expectedClippedWidth = 2;
  1673. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1674. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1675. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1676. Assert.Equal ("se", wrappedLines [1].ToString ());
  1677. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1678. Assert.Equal ("en", wrappedLines [3].ToString ());
  1679. Assert.Equal ("ce", wrappedLines [4].ToString ());
  1680. Assert.Equal ("\t", wrappedLines [5].ToString ());
  1681. Assert.Equal ("\t", wrappedLines [6].ToString ());
  1682. Assert.Equal ("\t", wrappedLines [7].ToString ());
  1683. Assert.Equal (" ", wrappedLines [8].ToString ());
  1684. Assert.Equal ("ha", wrappedLines [9].ToString ());
  1685. Assert.Equal ("s ", wrappedLines [10].ToString ());
  1686. Assert.Equal ("wo", wrappedLines [11].ToString ());
  1687. Assert.Equal ("rd", wrappedLines [12].ToString ());
  1688. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1689. Assert.True (wrappedLines.Count == 14);
  1690. maxWidth = 1;
  1691. expectedClippedWidth = 1;
  1692. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1693. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1694. Assert.Equal ("A", wrappedLines [0].ToString ());
  1695. Assert.Equal (" ", wrappedLines [1].ToString ());
  1696. Assert.Equal ("s", wrappedLines [2].ToString ());
  1697. Assert.Equal ("e", wrappedLines [3].ToString ());
  1698. Assert.Equal ("n", wrappedLines [4].ToString ());
  1699. Assert.Equal ("t", wrappedLines [5].ToString ());
  1700. Assert.Equal ("e", wrappedLines [6].ToString ());
  1701. Assert.Equal ("n", wrappedLines [7].ToString ());
  1702. Assert.Equal ("c", wrappedLines [8].ToString ());
  1703. Assert.Equal ("e", wrappedLines [9].ToString ());
  1704. Assert.Equal ("\t", wrappedLines [10].ToString ());
  1705. Assert.Equal ("\t", wrappedLines [11].ToString ());
  1706. Assert.Equal ("\t", wrappedLines [12].ToString ());
  1707. Assert.Equal (" ", wrappedLines [13].ToString ());
  1708. Assert.Equal (".", wrappedLines [^1].ToString ());
  1709. Assert.True (wrappedLines.Count == text.Length);
  1710. }
  1711. [Fact]
  1712. public void ReplaceHotKeyWithTag ()
  1713. {
  1714. var tf = new TextFormatter ();
  1715. ustring text = "test";
  1716. int hotPos = 0;
  1717. uint tag = tf.HotKeyTagMask | 't';
  1718. Assert.Equal (ustring.Make (new Rune [] { tag, 'e', 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1719. tag = tf.HotKeyTagMask | 'e';
  1720. hotPos = 1;
  1721. Assert.Equal (ustring.Make (new Rune [] { 't', tag, 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1722. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  1723. Assert.Equal ('e', (uint)(result.ToRunes () [1] & ~tf.HotKeyTagMask));
  1724. text = "Ok";
  1725. tag = 0x100000 | 'O';
  1726. hotPos = 0;
  1727. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1728. Assert.Equal ('O', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1729. text = "[◦ Ok ◦]";
  1730. text = ustring.Make (new Rune [] { '[', '◦', ' ', 'O', 'k', ' ', '◦', ']' });
  1731. var runes = text.ToRuneList ();
  1732. Assert.Equal (text.RuneCount, runes.Count);
  1733. Assert.Equal (text, ustring.Make (runes));
  1734. tag = tf.HotKeyTagMask | 'O';
  1735. hotPos = 3;
  1736. Assert.Equal (ustring.Make (new Rune [] { '[', '◦', ' ', tag, 'k', ' ', '◦', ']' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1737. Assert.Equal ('O', (uint)(result.ToRunes () [3] & ~tf.HotKeyTagMask));
  1738. text = "^k";
  1739. tag = '^';
  1740. hotPos = 0;
  1741. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1742. Assert.Equal ('^', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1743. }
  1744. [Fact]
  1745. public void Reformat_Invalid ()
  1746. {
  1747. var text = ustring.Empty;
  1748. var list = new List<ustring> ();
  1749. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, -1, TextAlignment.Left, false));
  1750. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1751. Assert.NotEmpty (list);
  1752. Assert.True (list.Count == 1);
  1753. Assert.Equal (ustring.Empty, list [0]);
  1754. text = null;
  1755. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1756. Assert.NotEmpty (list);
  1757. Assert.True (list.Count == 1);
  1758. Assert.Equal (ustring.Empty, list [0]);
  1759. list = TextFormatter.Format (text, 0, TextAlignment.Left, true);
  1760. Assert.NotEmpty (list);
  1761. Assert.True (list.Count == 1);
  1762. Assert.Equal (ustring.Empty, list [0]);
  1763. }
  1764. [Fact]
  1765. public void Reformat_NoWordrap_SingleLine ()
  1766. {
  1767. var text = ustring.Empty;
  1768. var list = new List<ustring> ();
  1769. var maxWidth = 0;
  1770. var expectedClippedWidth = 0;
  1771. var wrap = false;
  1772. maxWidth = 0;
  1773. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1774. Assert.True (list.Count == 1);
  1775. maxWidth = 1;
  1776. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1777. Assert.True (list.Count == 1);
  1778. text = "A sentence has words.";
  1779. maxWidth = 0;
  1780. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1781. Assert.True (list.Count == 1);
  1782. Assert.Equal (ustring.Empty, list [0]);
  1783. maxWidth = 1;
  1784. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1785. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1786. Assert.True (list.Count == 1);
  1787. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1788. maxWidth = 5;
  1789. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1790. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1791. Assert.True (list.Count == 1);
  1792. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1793. maxWidth = text.RuneCount - 1;
  1794. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1795. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1796. Assert.True (list.Count == 1);
  1797. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1798. // no clip
  1799. maxWidth = text.RuneCount + 0;
  1800. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1801. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1802. Assert.True (list.Count == 1);
  1803. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1804. maxWidth = text.RuneCount + 1;
  1805. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1806. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1807. Assert.True (list.Count == 1);
  1808. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1809. }
  1810. [Fact]
  1811. public void Reformat_NoWordrap_NewLines ()
  1812. {
  1813. var text = ustring.Empty;
  1814. var list = new List<ustring> ();
  1815. var maxWidth = 0;
  1816. var expectedClippedWidth = 0;
  1817. var wrap = false;
  1818. text = "A sentence has words.\nLine 2.";
  1819. maxWidth = 0;
  1820. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1821. Assert.True (list.Count == 1);
  1822. Assert.Equal (ustring.Empty, list [0]);
  1823. maxWidth = 1;
  1824. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1825. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1826. Assert.True (list.Count == 1);
  1827. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1828. maxWidth = 5;
  1829. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1830. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1831. Assert.True (list.Count == 1);
  1832. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1833. maxWidth = text.RuneCount - 1;
  1834. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1835. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1836. Assert.True (list.Count == 1);
  1837. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1838. // no clip
  1839. maxWidth = text.RuneCount + 0;
  1840. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1841. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1842. Assert.True (list.Count == 1);
  1843. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1844. maxWidth = text.RuneCount + 1;
  1845. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1846. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1847. Assert.True (list.Count == 1);
  1848. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1849. text = "A sentence has words.\r\nLine 2.";
  1850. maxWidth = 0;
  1851. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1852. Assert.True (list.Count == 1);
  1853. Assert.Equal (ustring.Empty, list [0]);
  1854. maxWidth = 1;
  1855. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1856. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1857. Assert.True (list.Count == 1);
  1858. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1859. maxWidth = 5;
  1860. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1861. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1862. Assert.True (list.Count == 1);
  1863. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1864. maxWidth = text.RuneCount - 1;
  1865. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth) + 1;
  1866. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1867. Assert.True (list.Count == 1);
  1868. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " ").ToString (), list [0].ToString ());
  1869. // no clip
  1870. maxWidth = text.RuneCount + 0;
  1871. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1872. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1873. Assert.True (list.Count == 1);
  1874. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1875. maxWidth = text.RuneCount + 1;
  1876. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1877. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1878. Assert.True (list.Count == 1);
  1879. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1880. }
  1881. [Fact]
  1882. public void Reformat_Wrap_Spaces_No_NewLines ()
  1883. {
  1884. var text = ustring.Empty;
  1885. var list = new List<ustring> ();
  1886. var maxWidth = 0;
  1887. var expectedClippedWidth = 0;
  1888. var wrap = true;
  1889. // Even # of spaces
  1890. // 0123456789
  1891. text = "012 456 89";
  1892. // See WordWrap BUGBUGs above.
  1893. //maxWidth = 0;
  1894. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1895. //Assert.True (list.Count == 1);
  1896. //Assert.Equal (ustring.Empty, list [0]);
  1897. //maxWidth = 1;
  1898. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1899. //// remove 3 whitespace chars
  1900. //expectedLines = text.RuneCount;
  1901. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1902. //Assert.Equal (expectedLines, list.Count);
  1903. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1904. ////width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1905. //maxWidth = 5;
  1906. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1907. //expectedLines = (int)Math.Ceiling ((double)(text.RuneCount / maxWidth));
  1908. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1909. //Assert.Equal (expectedLines, list.Count);
  1910. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1911. maxWidth = text.RuneCount - 1;
  1912. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1913. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1914. Assert.True (list.Count == 2);
  1915. Assert.Equal ("012 456", list [0]);
  1916. Assert.Equal ("89", list [1]);
  1917. // no clip
  1918. maxWidth = text.RuneCount + 0;
  1919. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1920. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1921. Assert.True (list.Count == 1);
  1922. Assert.Equal ("012 456 89", list [0]);
  1923. maxWidth = text.RuneCount + 1;
  1924. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1925. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1926. Assert.True (list.Count == 1);
  1927. Assert.Equal ("012 456 89", list [0]);
  1928. // Odd # of spaces
  1929. // 0123456789
  1930. text = "012 456 89 end";
  1931. maxWidth = text.RuneCount - 1;
  1932. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1933. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1934. Assert.True (list.Count == 2);
  1935. Assert.Equal ("012 456 89", list [0]);
  1936. Assert.Equal ("end", list [1]);
  1937. // no clip
  1938. maxWidth = text.RuneCount + 0;
  1939. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1940. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1941. Assert.True (list.Count == 1);
  1942. Assert.Equal ("012 456 89 end", list [0]);
  1943. maxWidth = text.RuneCount + 1;
  1944. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1945. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1946. Assert.True (list.Count == 1);
  1947. Assert.Equal ("012 456 89 end", list [0]);
  1948. }
  1949. [Fact]
  1950. public void Reformat_Unicode_Wrap_Spaces_No_NewLines ()
  1951. {
  1952. var text = ustring.Empty;
  1953. var list = new List<ustring> ();
  1954. var maxWidth = 0;
  1955. var expectedClippedWidth = 0;
  1956. var wrap = true;
  1957. // Unicode
  1958. // Even # of chars
  1959. // 0123456789
  1960. text = "\u2660пÑРвРÑ";
  1961. maxWidth = text.RuneCount - 1;
  1962. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1963. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1964. Assert.True (list.Count == 2);
  1965. Assert.Equal ("\u2660пÑРвÐ", list [0]);
  1966. Assert.Equal ("Ñ", list [1]);
  1967. // no clip
  1968. maxWidth = text.RuneCount + 0;
  1969. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1970. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1971. Assert.True (list.Count == 1);
  1972. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  1973. maxWidth = text.RuneCount + 1;
  1974. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1975. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1976. Assert.True (list.Count == 1);
  1977. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  1978. // Unicode
  1979. // Odd # of chars
  1980. // 0123456789
  1981. text = "\u2660 ÑРвРÑ";
  1982. maxWidth = text.RuneCount - 1;
  1983. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1984. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1985. Assert.True (list.Count == 2);
  1986. Assert.Equal ("\u2660 ÑРвÐ", list [0]);
  1987. Assert.Equal ("Ñ", list [1]);
  1988. // no clip
  1989. maxWidth = text.RuneCount + 0;
  1990. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1991. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1992. Assert.True (list.Count == 1);
  1993. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  1994. maxWidth = text.RuneCount + 1;
  1995. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1996. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1997. Assert.True (list.Count == 1);
  1998. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  1999. }
  2000. [Fact]
  2001. public void Reformat_Unicode_Wrap_Spaces_NewLines ()
  2002. {
  2003. var text = ustring.Empty;
  2004. var list = new List<ustring> ();
  2005. var maxWidth = 0;
  2006. var expectedClippedWidth = 0;
  2007. var wrap = true;
  2008. // Unicode
  2009. text = "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464";
  2010. maxWidth = text.RuneCount - 1;
  2011. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2012. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2013. Assert.Equal (2, list.Count);
  2014. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2015. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2016. // no clip
  2017. maxWidth = text.RuneCount + 0;
  2018. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2019. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2020. Assert.Equal (2, list.Count);
  2021. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2022. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2023. maxWidth = text.RuneCount + 1;
  2024. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2025. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2026. Assert.Equal (2, list.Count);
  2027. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2028. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2029. }
  2030. [Fact]
  2031. public void System_Rune_ColumnWidth ()
  2032. {
  2033. var c = new System.Rune ('a');
  2034. Assert.Equal (1, Rune.ColumnWidth (c));
  2035. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2036. Assert.Equal (1, ustring.Make (c).Length);
  2037. c = new System.Rune ('b');
  2038. Assert.Equal (1, Rune.ColumnWidth (c));
  2039. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2040. Assert.Equal (1, ustring.Make (c).Length);
  2041. c = new System.Rune (123);
  2042. Assert.Equal (1, Rune.ColumnWidth (c));
  2043. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2044. Assert.Equal (1, ustring.Make (c).Length);
  2045. c = new System.Rune ('\u1150');
  2046. Assert.Equal (2, Rune.ColumnWidth (c)); // 0x1150 ᅐ Unicode Technical Report #11
  2047. Assert.Equal (2, ustring.Make (c).ConsoleWidth);
  2048. Assert.Equal (3, ustring.Make (c).Length);
  2049. c = new System.Rune ('\u1161');
  2050. Assert.Equal (0, Rune.ColumnWidth (c)); // 0x1161 ᅡ column width of 0
  2051. Assert.Equal (0, ustring.Make (c).ConsoleWidth);
  2052. Assert.Equal (3, ustring.Make (c).Length);
  2053. c = new System.Rune (31);
  2054. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2055. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2056. Assert.Equal (1, ustring.Make (c).Length);
  2057. c = new System.Rune (127);
  2058. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2059. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2060. Assert.Equal (1, ustring.Make (c).Length);
  2061. }
  2062. [Fact]
  2063. public void System_Text_Rune ()
  2064. {
  2065. var c = new System.Text.Rune ('a');
  2066. Assert.Equal (1, c.Utf8SequenceLength);
  2067. c = new System.Text.Rune ('b');
  2068. Assert.Equal (1, c.Utf8SequenceLength);
  2069. c = new System.Text.Rune (123);
  2070. Assert.Equal (1, c.Utf8SequenceLength);
  2071. c = new System.Text.Rune ('\u1150');
  2072. Assert.Equal (3, c.Utf8SequenceLength); // 0x1150 ᅐ Unicode Technical Report #11
  2073. c = new System.Text.Rune ('\u1161');
  2074. Assert.Equal (3, c.Utf8SequenceLength); // 0x1161 ᅡ column width of 0
  2075. c = new System.Text.Rune (31);
  2076. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2077. c = new System.Text.Rune (127);
  2078. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2079. }
  2080. [Fact]
  2081. public void Format_WordWrap_preserveTrailingSpaces ()
  2082. {
  2083. ustring text = " A sentence has words. \n This is the second Line - 2. ";
  2084. // With preserveTrailingSpaces = false by default.
  2085. var list1 = TextFormatter.Format (text, 4, TextAlignment.Left, true);
  2086. ustring wrappedText1 = ustring.Empty;
  2087. Assert.Equal (" A", list1 [0].ToString ());
  2088. Assert.Equal ("sent", list1 [1].ToString ());
  2089. Assert.Equal ("ence", list1 [2].ToString ());
  2090. Assert.Equal ("has", list1 [3].ToString ());
  2091. Assert.Equal ("word", list1 [4].ToString ());
  2092. Assert.Equal ("s. ", list1 [5].ToString ());
  2093. Assert.Equal (" Thi", list1 [6].ToString ());
  2094. Assert.Equal ("s is", list1 [7].ToString ());
  2095. Assert.Equal ("the", list1 [8].ToString ());
  2096. Assert.Equal ("seco", list1 [9].ToString ());
  2097. Assert.Equal ("nd", list1 [10].ToString ());
  2098. Assert.Equal ("Line", list1 [11].ToString ());
  2099. Assert.Equal ("- 2.", list1 [^1].ToString ());
  2100. foreach (var txt in list1) {
  2101. wrappedText1 += txt;
  2102. }
  2103. Assert.Equal (" Asentencehaswords. This isthesecondLine- 2.", wrappedText1);
  2104. // With preserveTrailingSpaces = true.
  2105. var list2 = TextFormatter.Format (text, 4, TextAlignment.Left, true, true);
  2106. ustring wrappedText2 = ustring.Empty;
  2107. Assert.Equal (" A ", list2 [0].ToString ());
  2108. Assert.Equal ("sent", list2 [1].ToString ());
  2109. Assert.Equal ("ence", list2 [2].ToString ());
  2110. Assert.Equal (" has", list2 [3].ToString ());
  2111. Assert.Equal (" ", list2 [4].ToString ());
  2112. Assert.Equal ("word", list2 [5].ToString ());
  2113. Assert.Equal ("s. ", list2 [6].ToString ());
  2114. Assert.Equal (" ", list2 [7].ToString ());
  2115. Assert.Equal ("This", list2 [8].ToString ());
  2116. Assert.Equal (" is ", list2 [9].ToString ());
  2117. Assert.Equal ("the ", list2 [10].ToString ());
  2118. Assert.Equal ("seco", list2 [11].ToString ());
  2119. Assert.Equal ("nd ", list2 [12].ToString ());
  2120. Assert.Equal ("Line", list2 [13].ToString ());
  2121. Assert.Equal (" - ", list2 [14].ToString ());
  2122. Assert.Equal ("2. ", list2 [^1].ToString ());
  2123. foreach (var txt in list2) {
  2124. wrappedText2 += txt;
  2125. }
  2126. Assert.Equal (" A sentence has words. This is the second Line - 2. ", wrappedText2);
  2127. }
  2128. [Fact]
  2129. public void Format_Throw_ArgumentException_With_WordWrap_As_False_And_Keep_End_Spaces_As_True ()
  2130. {
  2131. Assert.Throws<ArgumentException> (() => TextFormatter.Format ("Some text", 4, TextAlignment.Left, false, true));
  2132. }
  2133. [Fact]
  2134. public void Draw_Horizontal_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2135. {
  2136. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2137. var top = Application.Top;
  2138. var view = new View ("view") { X = -2 };
  2139. top.Add (view);
  2140. Application.Iteration += () => {
  2141. Assert.Equal (-2, view.X);
  2142. Application.RequestStop ();
  2143. };
  2144. try {
  2145. Application.Run ();
  2146. } catch (IndexOutOfRangeException ex) {
  2147. // After the fix this exception will not be caught.
  2148. Assert.IsType<IndexOutOfRangeException> (ex);
  2149. }
  2150. // Shutdown must be called to safely clean up Application if Init has been called
  2151. Application.Shutdown ();
  2152. }
  2153. [Fact]
  2154. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2155. {
  2156. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2157. var top = Application.Top;
  2158. var view = new View ("view") {
  2159. Y = -2,
  2160. Height = 10,
  2161. TextDirection = TextDirection.TopBottom_LeftRight
  2162. };
  2163. top.Add (view);
  2164. Application.Iteration += () => {
  2165. Assert.Equal (-2, view.Y);
  2166. Application.RequestStop ();
  2167. };
  2168. try {
  2169. Application.Run ();
  2170. } catch (IndexOutOfRangeException ex) {
  2171. // After the fix this exception will not be caught.
  2172. Assert.IsType<IndexOutOfRangeException> (ex);
  2173. }
  2174. // Shutdown must be called to safely clean up Application if Init has been called
  2175. Application.Shutdown ();
  2176. }
  2177. }
  2178. }