TextFormatterTests.cs 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  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. // Alais Console to MockConsole so we don't accidentally use Console
  10. using Console = Terminal.Gui.FakeConsole;
  11. namespace Terminal.Gui {
  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 (Rect.Empty, TextFormatter.CalcRect (1, 2, ""));
  459. Assert.Equal (Rect.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_NoNewLines ()
  1314. {
  1315. var text = ustring.Empty;
  1316. int maxWidth = 0;
  1317. int expectedClippedWidth = 0;
  1318. List<ustring> wrappedLines;
  1319. text = "A sentence has words.";
  1320. maxWidth = text.RuneCount;
  1321. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1322. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1323. Assert.True (wrappedLines.Count == 1);
  1324. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1325. Assert.Equal ("A sentence has words.", wrappedLines [0].ToString ());
  1326. maxWidth = text.RuneCount - 1;
  1327. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1328. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1329. Assert.Equal (2, wrappedLines.Count);
  1330. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1331. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1332. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1333. maxWidth = text.RuneCount - "words.".Length;
  1334. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1335. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1336. Assert.Equal (2, wrappedLines.Count);
  1337. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1338. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1339. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1340. maxWidth = text.RuneCount - " words.".Length;
  1341. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1342. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1343. Assert.Equal (2, wrappedLines.Count);
  1344. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1345. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1346. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1347. maxWidth = text.RuneCount - "s words.".Length;
  1348. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1349. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1350. Assert.Equal (2, wrappedLines.Count);
  1351. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1352. Assert.Equal ("A sentence", wrappedLines [0].ToString ());
  1353. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1354. // Unicode
  1355. text = "A Unicode sentence (пÑивеÑ) has words.";
  1356. maxWidth = text.RuneCount;
  1357. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1358. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1359. Assert.True (wrappedLines.Count == 1);
  1360. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1361. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.", wrappedLines [0].ToString ());
  1362. maxWidth = text.RuneCount - 1;
  1363. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1364. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1365. Assert.Equal (2, wrappedLines.Count);
  1366. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1367. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1368. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1369. maxWidth = text.RuneCount - "words.".Length;
  1370. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1371. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1372. Assert.Equal (2, wrappedLines.Count);
  1373. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1374. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1375. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1376. maxWidth = text.RuneCount - " words.".Length;
  1377. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1378. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1379. Assert.Equal (2, wrappedLines.Count);
  1380. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1381. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1382. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1383. maxWidth = text.RuneCount - "s words.".Length;
  1384. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1385. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1386. Assert.Equal (2, wrappedLines.Count);
  1387. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1388. Assert.Equal ("A Unicode sentence (пÑивеÑ)", wrappedLines [0].ToString ());
  1389. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1390. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1391. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1392. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1393. Assert.Equal (2, wrappedLines.Count);
  1394. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1395. Assert.Equal ("A Unicode sentence", wrappedLines [0].ToString ());
  1396. Assert.Equal ("(пÑивеÑ) has words.", wrappedLines [1].ToString ());
  1397. }
  1398. [Fact]
  1399. public void WordWrap_Narrow ()
  1400. {
  1401. var text = ustring.Empty;
  1402. int maxWidth = 1;
  1403. int expectedClippedWidth = 1;
  1404. List<ustring> wrappedLines;
  1405. text = "A sentence has words.";
  1406. maxWidth = 3;
  1407. expectedClippedWidth = 3;
  1408. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1409. //Assert.True (wrappedLines.Count == );
  1410. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1411. Assert.Equal ("A", wrappedLines [0].ToString ());
  1412. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1413. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1414. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1415. Assert.Equal ("has", wrappedLines [4].ToString ());
  1416. Assert.Equal ("wor", wrappedLines [5].ToString ());
  1417. Assert.Equal ("ds.", wrappedLines [6].ToString ());
  1418. maxWidth = 2;
  1419. expectedClippedWidth = 2;
  1420. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1421. //Assert.True (wrappedLines.Count == );
  1422. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1423. Assert.Equal ("A", wrappedLines [0].ToString ());
  1424. Assert.Equal ("se", wrappedLines [1].ToString ());
  1425. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1426. Assert.Equal ("en", wrappedLines [3].ToString ());
  1427. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1428. maxWidth = 1;
  1429. expectedClippedWidth = 1;
  1430. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1431. //Assert.True (wrappedLines.Count == );
  1432. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1433. Assert.Equal ("A", wrappedLines [0].ToString ());
  1434. Assert.Equal ("s", wrappedLines [1].ToString ());
  1435. Assert.Equal ("e", wrappedLines [2].ToString ());
  1436. Assert.Equal ("n", wrappedLines [3].ToString ());
  1437. Assert.Equal (".", wrappedLines [^1].ToString ());
  1438. }
  1439. [Fact]
  1440. public void ReplaceHotKeyWithTag ()
  1441. {
  1442. var tf = new TextFormatter ();
  1443. ustring text = "test";
  1444. int hotPos = 0;
  1445. uint tag = tf.HotKeyTagMask | 't';
  1446. Assert.Equal (ustring.Make (new Rune [] { tag, 'e', 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1447. tag = tf.HotKeyTagMask | 'e';
  1448. hotPos = 1;
  1449. Assert.Equal (ustring.Make (new Rune [] { 't', tag, 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1450. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  1451. Assert.Equal ('e', (uint)(result.ToRunes () [1] & ~tf.HotKeyTagMask));
  1452. text = "Ok";
  1453. tag = 0x100000 | 'O';
  1454. hotPos = 0;
  1455. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1456. Assert.Equal ('O', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1457. text = "[◦ Ok ◦]";
  1458. text = ustring.Make (new Rune [] { '[', '◦', ' ', 'O', 'k', ' ', '◦', ']' });
  1459. var runes = text.ToRuneList ();
  1460. Assert.Equal (text.RuneCount, runes.Count);
  1461. Assert.Equal (text, ustring.Make (runes));
  1462. tag = tf.HotKeyTagMask | 'O';
  1463. hotPos = 3;
  1464. Assert.Equal (ustring.Make (new Rune [] { '[', '◦', ' ', tag, 'k', ' ', '◦', ']' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1465. Assert.Equal ('O', (uint)(result.ToRunes () [3] & ~tf.HotKeyTagMask));
  1466. text = "^k";
  1467. tag = '^';
  1468. hotPos = 0;
  1469. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1470. Assert.Equal ('^', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1471. }
  1472. [Fact]
  1473. public void Reformat_Invalid ()
  1474. {
  1475. var text = ustring.Empty;
  1476. var list = new List<ustring> ();
  1477. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, -1, TextAlignment.Left, false));
  1478. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1479. Assert.NotEmpty (list);
  1480. Assert.True (list.Count == 1);
  1481. Assert.Equal (ustring.Empty, list [0]);
  1482. text = null;
  1483. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1484. Assert.NotEmpty (list);
  1485. Assert.True (list.Count == 1);
  1486. Assert.Equal (ustring.Empty, list [0]);
  1487. list = TextFormatter.Format (text, 0, TextAlignment.Left, true);
  1488. Assert.NotEmpty (list);
  1489. Assert.True (list.Count == 1);
  1490. Assert.Equal (ustring.Empty, list [0]);
  1491. }
  1492. [Fact]
  1493. public void Reformat_NoWordrap_SingleLine ()
  1494. {
  1495. var text = ustring.Empty;
  1496. var list = new List<ustring> ();
  1497. var maxWidth = 0;
  1498. var expectedClippedWidth = 0;
  1499. var wrap = false;
  1500. maxWidth = 0;
  1501. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1502. Assert.True (list.Count == 1);
  1503. maxWidth = 1;
  1504. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1505. Assert.True (list.Count == 1);
  1506. text = "A sentence has words.";
  1507. maxWidth = 0;
  1508. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1509. Assert.True (list.Count == 1);
  1510. Assert.Equal (ustring.Empty, list [0]);
  1511. maxWidth = 1;
  1512. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1513. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1514. Assert.True (list.Count == 1);
  1515. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1516. maxWidth = 5;
  1517. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1518. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1519. Assert.True (list.Count == 1);
  1520. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list[0]);
  1521. maxWidth = text.RuneCount - 1;
  1522. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1523. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1524. Assert.True (list.Count == 1);
  1525. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1526. // no clip
  1527. maxWidth = text.RuneCount + 0;
  1528. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1529. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1530. Assert.True (list.Count == 1);
  1531. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1532. maxWidth = text.RuneCount + 1;
  1533. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1534. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1535. Assert.True (list.Count == 1);
  1536. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1537. }
  1538. [Fact]
  1539. public void Reformat_NoWordrap_NewLines ()
  1540. {
  1541. var text = ustring.Empty;
  1542. var list = new List<ustring> ();
  1543. var maxWidth = 0;
  1544. var expectedClippedWidth = 0;
  1545. var wrap = false;
  1546. text = "A sentence has words.\nLine 2.";
  1547. maxWidth = 0;
  1548. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1549. Assert.True (list.Count == 1);
  1550. Assert.Equal (ustring.Empty, list [0]);
  1551. maxWidth = 1;
  1552. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1553. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1554. Assert.True (list.Count == 1);
  1555. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1556. maxWidth = 5;
  1557. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1558. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1559. Assert.True (list.Count == 1);
  1560. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1561. maxWidth = text.RuneCount - 1;
  1562. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1563. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1564. Assert.True (list.Count == 1);
  1565. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace("\n", " "), list [0]);
  1566. // no clip
  1567. maxWidth = text.RuneCount + 0;
  1568. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1569. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1570. Assert.True (list.Count == 1);
  1571. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1572. maxWidth = text.RuneCount + 1;
  1573. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1574. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1575. Assert.True (list.Count == 1);
  1576. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1577. text = "A sentence has words.\r\nLine 2.";
  1578. maxWidth = 0;
  1579. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1580. Assert.True (list.Count == 1);
  1581. Assert.Equal (ustring.Empty, list [0]);
  1582. maxWidth = 1;
  1583. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1584. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1585. Assert.True (list.Count == 1);
  1586. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1587. maxWidth = 5;
  1588. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1589. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1590. Assert.True (list.Count == 1);
  1591. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1592. maxWidth = text.RuneCount - 1;
  1593. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth) + 1;
  1594. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1595. Assert.True (list.Count == 1);
  1596. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " ").ToString(), list [0].ToString());
  1597. // no clip
  1598. maxWidth = text.RuneCount + 0;
  1599. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1600. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1601. Assert.True (list.Count == 1);
  1602. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1603. maxWidth = text.RuneCount + 1;
  1604. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1605. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1606. Assert.True (list.Count == 1);
  1607. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1608. }
  1609. [Fact]
  1610. public void Reformat_Wrap_Spaces_No_NewLines ()
  1611. {
  1612. var text = ustring.Empty;
  1613. var list = new List<ustring> ();
  1614. var maxWidth = 0;
  1615. var expectedClippedWidth = 0;
  1616. var wrap = true;
  1617. // Even # of spaces
  1618. // 0123456789
  1619. text = "012 456 89";
  1620. // See WordWrap BUGBUGs above.
  1621. //maxWidth = 0;
  1622. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1623. //Assert.True (list.Count == 1);
  1624. //Assert.Equal (ustring.Empty, list [0]);
  1625. //maxWidth = 1;
  1626. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1627. //// remove 3 whitespace chars
  1628. //expectedLines = text.RuneCount;
  1629. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1630. //Assert.Equal (expectedLines, list.Count);
  1631. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1632. ////width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1633. //maxWidth = 5;
  1634. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1635. //expectedLines = (int)Math.Ceiling ((double)(text.RuneCount / maxWidth));
  1636. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1637. //Assert.Equal (expectedLines, list.Count);
  1638. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1639. maxWidth = text.RuneCount - 1;
  1640. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1641. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1642. Assert.True (list.Count == 2);
  1643. Assert.Equal ("012 456", list [0]);
  1644. Assert.Equal ("89", list [1]);
  1645. // no clip
  1646. maxWidth = text.RuneCount + 0;
  1647. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1648. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1649. Assert.True (list.Count == 1);
  1650. Assert.Equal ("012 456 89", list [0]);
  1651. maxWidth = text.RuneCount + 1;
  1652. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1653. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1654. Assert.True (list.Count == 1);
  1655. Assert.Equal ("012 456 89", list [0]);
  1656. // Odd # of spaces
  1657. // 0123456789
  1658. text = "012 456 89 end";
  1659. maxWidth = text.RuneCount - 1;
  1660. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1661. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1662. Assert.True (list.Count == 2);
  1663. Assert.Equal ("012 456 89", list [0]);
  1664. Assert.Equal ("end", list [1]);
  1665. // no clip
  1666. maxWidth = text.RuneCount + 0;
  1667. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1668. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1669. Assert.True (list.Count == 1);
  1670. Assert.Equal ("012 456 89 end", list [0]);
  1671. maxWidth = text.RuneCount + 1;
  1672. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1673. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1674. Assert.True (list.Count == 1);
  1675. Assert.Equal ("012 456 89 end", list [0]);
  1676. }
  1677. [Fact]
  1678. public void Reformat_Unicode_Wrap_Spaces_No_NewLines ()
  1679. {
  1680. var text = ustring.Empty;
  1681. var list = new List<ustring> ();
  1682. var maxWidth = 0;
  1683. var expectedClippedWidth = 0;
  1684. var wrap = true;
  1685. // Unicode
  1686. // Even # of chars
  1687. // 0123456789
  1688. text = "пÑРвРÑ";
  1689. maxWidth = text.RuneCount - 1;
  1690. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1691. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1692. Assert.True (list.Count == 2);
  1693. Assert.Equal ("пÑРвÐ", list [0]);
  1694. Assert.Equal ("Ñ", list [1]);
  1695. // no clip
  1696. maxWidth = text.RuneCount + 0;
  1697. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1698. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1699. Assert.True (list.Count == 1);
  1700. Assert.Equal ("пÑРвРÑ", list [0]);
  1701. maxWidth = text.RuneCount + 1;
  1702. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1703. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1704. Assert.True (list.Count == 1);
  1705. Assert.Equal ("пÑРвРÑ", list [0]);
  1706. // Unicode
  1707. // Odd # of chars
  1708. // 0123456789
  1709. text = "Ð ÑРвРÑ";
  1710. maxWidth = text.RuneCount - 1;
  1711. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1712. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1713. Assert.True (list.Count == 2);
  1714. Assert.Equal ("Ð ÑРвÐ", list [0]);
  1715. Assert.Equal ("Ñ", list [1]);
  1716. // no clip
  1717. maxWidth = text.RuneCount + 0;
  1718. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1719. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1720. Assert.True (list.Count == 1);
  1721. Assert.Equal ("Ð ÑРвРÑ", list [0]);
  1722. maxWidth = text.RuneCount + 1;
  1723. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1724. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1725. Assert.True (list.Count == 1);
  1726. Assert.Equal ("Ð ÑРвРÑ", list [0]);
  1727. }
  1728. }
  1729. }