TextFormatterTests.cs 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  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. // BUGBUG: WordWrap breaks down with small widths. It should not
  1413. // the following line should be "sen"...
  1414. Assert.Equal ("se", wrappedLines [1].ToString ());
  1415. Assert.Equal ("nte", wrappedLines [2].ToString ());
  1416. Assert.Equal ("nce", wrappedLines [3].ToString ());
  1417. Assert.Equal ("ha", wrappedLines [4].ToString ());
  1418. Assert.Equal ("s", wrappedLines [5].ToString ());
  1419. Assert.Equal ("wo", wrappedLines [6].ToString ());
  1420. Assert.Equal ("rds", wrappedLines [7].ToString ());
  1421. Assert.Equal (".", wrappedLines [^1].ToString ());
  1422. maxWidth = 2;
  1423. expectedClippedWidth = 2;
  1424. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1425. //Assert.True (wrappedLines.Count == );
  1426. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1427. Assert.Equal ("A", wrappedLines [0].ToString ());
  1428. Assert.Equal ("s", wrappedLines [1].ToString ());
  1429. Assert.Equal ("en", wrappedLines [2].ToString ());
  1430. Assert.Equal ("te", wrappedLines [3].ToString ());
  1431. Assert.Equal (".", wrappedLines [^1].ToString ());
  1432. maxWidth = 1;
  1433. expectedClippedWidth = 1;
  1434. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1435. //Assert.True (wrappedLines.Count == );
  1436. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1437. Assert.Equal ("A", wrappedLines [0].ToString ());
  1438. // BUGBUG: WordWrap breaks down with a width of one. It should not
  1439. // provide blank lines like it does.
  1440. Assert.Equal ("", wrappedLines [1].ToString ());
  1441. Assert.Equal ("s", wrappedLines [2].ToString ());
  1442. Assert.Equal ("e", wrappedLines [3].ToString ());
  1443. Assert.Equal ("n", wrappedLines [4].ToString ());
  1444. Assert.Equal (".", wrappedLines [^1].ToString ());
  1445. }
  1446. [Fact]
  1447. public void ReplaceHotKeyWithTag ()
  1448. {
  1449. var tf = new TextFormatter ();
  1450. ustring text = "test";
  1451. int hotPos = 0;
  1452. uint tag = tf.HotKeyTagMask | 't';
  1453. Assert.Equal (ustring.Make (new Rune [] { tag, 'e', 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1454. tag = tf.HotKeyTagMask | 'e';
  1455. hotPos = 1;
  1456. Assert.Equal (ustring.Make (new Rune [] { 't', tag, 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1457. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  1458. Assert.Equal ('e', (uint)(result.ToRunes () [1] & ~tf.HotKeyTagMask));
  1459. text = "Ok";
  1460. tag = 0x100000 | 'O';
  1461. hotPos = 0;
  1462. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1463. Assert.Equal ('O', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1464. text = "[◦ Ok ◦]";
  1465. text = ustring.Make (new Rune [] { '[', '◦', ' ', 'O', 'k', ' ', '◦', ']' });
  1466. var runes = text.ToRuneList ();
  1467. Assert.Equal (text.RuneCount, runes.Count);
  1468. Assert.Equal (text, ustring.Make (runes));
  1469. tag = tf.HotKeyTagMask | 'O';
  1470. hotPos = 3;
  1471. Assert.Equal (ustring.Make (new Rune [] { '[', '◦', ' ', tag, 'k', ' ', '◦', ']' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1472. Assert.Equal ('O', (uint)(result.ToRunes () [3] & ~tf.HotKeyTagMask));
  1473. text = "^k";
  1474. tag = '^';
  1475. hotPos = 0;
  1476. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1477. Assert.Equal ('^', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1478. }
  1479. [Fact]
  1480. public void Reformat_Invalid ()
  1481. {
  1482. var text = ustring.Empty;
  1483. var list = new List<ustring> ();
  1484. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, -1, TextAlignment.Left, false));
  1485. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1486. Assert.NotEmpty (list);
  1487. Assert.True (list.Count == 1);
  1488. Assert.Equal (ustring.Empty, list [0]);
  1489. text = null;
  1490. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1491. Assert.NotEmpty (list);
  1492. Assert.True (list.Count == 1);
  1493. Assert.Equal (ustring.Empty, list [0]);
  1494. list = TextFormatter.Format (text, 0, TextAlignment.Left, true);
  1495. Assert.NotEmpty (list);
  1496. Assert.True (list.Count == 1);
  1497. Assert.Equal (ustring.Empty, list [0]);
  1498. }
  1499. [Fact]
  1500. public void Reformat_NoWordrap_SingleLine ()
  1501. {
  1502. var text = ustring.Empty;
  1503. var list = new List<ustring> ();
  1504. var maxWidth = 0;
  1505. var expectedClippedWidth = 0;
  1506. var wrap = false;
  1507. maxWidth = 0;
  1508. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1509. Assert.True (list.Count == 1);
  1510. maxWidth = 1;
  1511. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1512. Assert.True (list.Count == 1);
  1513. text = "A sentence has words.";
  1514. maxWidth = 0;
  1515. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1516. Assert.True (list.Count == 1);
  1517. Assert.Equal (ustring.Empty, list [0]);
  1518. maxWidth = 1;
  1519. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1520. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1521. Assert.True (list.Count == 1);
  1522. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1523. maxWidth = 5;
  1524. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1525. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1526. Assert.True (list.Count == 1);
  1527. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list[0]);
  1528. maxWidth = text.RuneCount - 1;
  1529. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1530. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1531. Assert.True (list.Count == 1);
  1532. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1533. // no clip
  1534. maxWidth = text.RuneCount + 0;
  1535. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1536. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1537. Assert.True (list.Count == 1);
  1538. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1539. maxWidth = text.RuneCount + 1;
  1540. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1541. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1542. Assert.True (list.Count == 1);
  1543. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1544. }
  1545. [Fact]
  1546. public void Reformat_NoWordrap_NewLines ()
  1547. {
  1548. var text = ustring.Empty;
  1549. var list = new List<ustring> ();
  1550. var maxWidth = 0;
  1551. var expectedClippedWidth = 0;
  1552. var wrap = false;
  1553. text = "A sentence has words.\nLine 2.";
  1554. maxWidth = 0;
  1555. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1556. Assert.True (list.Count == 1);
  1557. Assert.Equal (ustring.Empty, list [0]);
  1558. maxWidth = 1;
  1559. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1560. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1561. Assert.True (list.Count == 1);
  1562. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1563. maxWidth = 5;
  1564. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1565. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1566. Assert.True (list.Count == 1);
  1567. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1568. maxWidth = text.RuneCount - 1;
  1569. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1570. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1571. Assert.True (list.Count == 1);
  1572. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace("\n", " "), list [0]);
  1573. // no clip
  1574. maxWidth = text.RuneCount + 0;
  1575. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1576. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1577. Assert.True (list.Count == 1);
  1578. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1579. maxWidth = text.RuneCount + 1;
  1580. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1581. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1582. Assert.True (list.Count == 1);
  1583. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1584. text = "A sentence has words.\r\nLine 2.";
  1585. maxWidth = 0;
  1586. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1587. Assert.True (list.Count == 1);
  1588. Assert.Equal (ustring.Empty, list [0]);
  1589. maxWidth = 1;
  1590. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1591. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1592. Assert.True (list.Count == 1);
  1593. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1594. maxWidth = 5;
  1595. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1596. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1597. Assert.True (list.Count == 1);
  1598. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1599. maxWidth = text.RuneCount - 1;
  1600. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth) + 1;
  1601. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1602. Assert.True (list.Count == 1);
  1603. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " ").ToString(), list [0].ToString());
  1604. // no clip
  1605. maxWidth = text.RuneCount + 0;
  1606. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1607. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1608. Assert.True (list.Count == 1);
  1609. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1610. maxWidth = text.RuneCount + 1;
  1611. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1612. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1613. Assert.True (list.Count == 1);
  1614. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1615. }
  1616. [Fact]
  1617. public void Reformat_Wrap_Spaces_No_NewLines ()
  1618. {
  1619. var text = ustring.Empty;
  1620. var list = new List<ustring> ();
  1621. var maxWidth = 0;
  1622. var expectedClippedWidth = 0;
  1623. var wrap = true;
  1624. // Even # of spaces
  1625. // 0123456789
  1626. text = "012 456 89";
  1627. // See WordWrap BUGBUGs above.
  1628. //maxWidth = 0;
  1629. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1630. //Assert.True (list.Count == 1);
  1631. //Assert.Equal (ustring.Empty, list [0]);
  1632. //maxWidth = 1;
  1633. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1634. //// remove 3 whitespace chars
  1635. //expectedLines = text.RuneCount;
  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. ////width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1640. //maxWidth = 5;
  1641. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1642. //expectedLines = (int)Math.Ceiling ((double)(text.RuneCount / maxWidth));
  1643. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1644. //Assert.Equal (expectedLines, list.Count);
  1645. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1646. maxWidth = text.RuneCount - 1;
  1647. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1648. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1649. Assert.True (list.Count == 2);
  1650. Assert.Equal ("012 456", list [0]);
  1651. Assert.Equal ("89", list [1]);
  1652. // no clip
  1653. maxWidth = text.RuneCount + 0;
  1654. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1655. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1656. Assert.True (list.Count == 1);
  1657. Assert.Equal ("012 456 89", list [0]);
  1658. maxWidth = text.RuneCount + 1;
  1659. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1660. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1661. Assert.True (list.Count == 1);
  1662. Assert.Equal ("012 456 89", list [0]);
  1663. // Odd # of spaces
  1664. // 0123456789
  1665. text = "012 456 89 end";
  1666. maxWidth = text.RuneCount - 1;
  1667. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1668. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1669. Assert.True (list.Count == 2);
  1670. Assert.Equal ("012 456 89", list [0]);
  1671. Assert.Equal ("end", list [1]);
  1672. // no clip
  1673. maxWidth = text.RuneCount + 0;
  1674. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1675. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1676. Assert.True (list.Count == 1);
  1677. Assert.Equal ("012 456 89 end", list [0]);
  1678. maxWidth = text.RuneCount + 1;
  1679. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1680. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1681. Assert.True (list.Count == 1);
  1682. Assert.Equal ("012 456 89 end", list [0]);
  1683. }
  1684. [Fact]
  1685. public void Reformat_Unicode_Wrap_Spaces_No_NewLines ()
  1686. {
  1687. var text = ustring.Empty;
  1688. var list = new List<ustring> ();
  1689. var maxWidth = 0;
  1690. var expectedClippedWidth = 0;
  1691. var wrap = true;
  1692. // Unicode
  1693. // Even # of chars
  1694. // 0123456789
  1695. text = "пÑРвРÑ";
  1696. maxWidth = text.RuneCount - 1;
  1697. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1698. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1699. Assert.True (list.Count == 2);
  1700. Assert.Equal ("пÑРвÐ", list [0]);
  1701. Assert.Equal ("Ñ", list [1]);
  1702. // no clip
  1703. maxWidth = text.RuneCount + 0;
  1704. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1705. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1706. Assert.True (list.Count == 1);
  1707. Assert.Equal ("пÑРвРÑ", list [0]);
  1708. maxWidth = text.RuneCount + 1;
  1709. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1710. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1711. Assert.True (list.Count == 1);
  1712. Assert.Equal ("пÑРвРÑ", list [0]);
  1713. // Unicode
  1714. // Odd # of chars
  1715. // 0123456789
  1716. text = "Ð ÑРвРÑ";
  1717. maxWidth = text.RuneCount - 1;
  1718. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1719. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1720. Assert.True (list.Count == 2);
  1721. Assert.Equal ("Ð ÑРвÐ", list [0]);
  1722. Assert.Equal ("Ñ", list [1]);
  1723. // no clip
  1724. maxWidth = text.RuneCount + 0;
  1725. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1726. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1727. Assert.True (list.Count == 1);
  1728. Assert.Equal ("Ð ÑРвРÑ", list [0]);
  1729. maxWidth = text.RuneCount + 1;
  1730. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1731. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1732. Assert.True (list.Count == 1);
  1733. Assert.Equal ("Ð ÑРвРÑ", list [0]);
  1734. }
  1735. }
  1736. }