TextFormatterTests.cs 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  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 tf = new TextFormatter ();
  17. }
  18. [Fact]
  19. public void FindHotKey_Invalid_ReturnsFalse ()
  20. {
  21. var text = ustring.Empty;
  22. Rune hotKeySpecifier = '_';
  23. bool supportFirstUpperCase = false;
  24. int hotPos = 0;
  25. Key hotKey = Key.Unknown;
  26. bool result = false;
  27. text = null;
  28. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  29. Assert.False (result);
  30. Assert.Equal (-1, hotPos);
  31. Assert.Equal (Key.Unknown, hotKey);
  32. text = "";
  33. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  34. Assert.False (result);
  35. Assert.Equal (-1, hotPos);
  36. Assert.Equal (Key.Unknown, hotKey);
  37. text = "no hotkey";
  38. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  39. Assert.False (result);
  40. Assert.Equal (-1, hotPos);
  41. Assert.Equal (Key.Unknown, hotKey);
  42. text = "No hotkey, Upper Case";
  43. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  44. Assert.False (result);
  45. Assert.Equal (-1, hotPos);
  46. Assert.Equal (Key.Unknown, hotKey);
  47. text = "Non-english: Сохранить";
  48. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  49. Assert.False (result);
  50. Assert.Equal (-1, hotPos);
  51. Assert.Equal (Key.Unknown, hotKey);
  52. }
  53. [Fact]
  54. public void FindHotKey_AlphaUpperCase_Succeeds ()
  55. {
  56. var text = ustring.Empty;
  57. Rune hotKeySpecifier = '_';
  58. bool supportFirstUpperCase = false;
  59. int hotPos = 0;
  60. Key hotKey = Key.Unknown;
  61. bool result = false;
  62. text = "_K Before";
  63. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  64. Assert.True (result);
  65. Assert.Equal (0, hotPos);
  66. Assert.Equal ((Key)'K', hotKey);
  67. text = "a_K Second";
  68. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  69. Assert.True (result);
  70. Assert.Equal (1, hotPos);
  71. Assert.Equal ((Key)'K', hotKey);
  72. text = "Last _K";
  73. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  74. Assert.True (result);
  75. Assert.Equal (5, hotPos);
  76. Assert.Equal ((Key)'K', hotKey);
  77. text = "After K_";
  78. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  79. Assert.False (result);
  80. Assert.Equal (-1, hotPos);
  81. Assert.Equal (Key.Unknown, hotKey);
  82. text = "Multiple _K and _R";
  83. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  84. Assert.True (result);
  85. Assert.Equal (9, hotPos);
  86. Assert.Equal ((Key)'K', hotKey);
  87. // Cryllic K (К)
  88. text = "Non-english: _Кдать";
  89. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  90. Assert.True (result);
  91. Assert.Equal (13, hotPos);
  92. Assert.Equal ((Key)'К', hotKey);
  93. // Turn on FirstUpperCase and verify same results
  94. supportFirstUpperCase = true;
  95. text = "_K Before";
  96. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  97. Assert.True (result);
  98. Assert.Equal (0, hotPos);
  99. Assert.Equal ((Key)'K', hotKey);
  100. text = "a_K Second";
  101. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  102. Assert.True (result);
  103. Assert.Equal (1, hotPos);
  104. Assert.Equal ((Key)'K', hotKey);
  105. text = "Last _K";
  106. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  107. Assert.True (result);
  108. Assert.Equal (5, hotPos);
  109. Assert.Equal ((Key)'K', hotKey);
  110. text = "After K_";
  111. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  112. Assert.False (result);
  113. Assert.Equal (-1, hotPos);
  114. Assert.Equal (Key.Unknown, hotKey);
  115. text = "Multiple _K and _R";
  116. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  117. Assert.True (result);
  118. Assert.Equal (9, hotPos);
  119. Assert.Equal ((Key)'K', hotKey);
  120. // Cryllic K (К)
  121. text = "Non-english: _Кдать";
  122. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  123. Assert.True (result);
  124. Assert.Equal (13, hotPos);
  125. Assert.Equal ((Key)'К', hotKey);
  126. }
  127. [Fact]
  128. public void FindHotKey_AlphaLowerCase_Succeeds ()
  129. {
  130. var text = ustring.Empty;
  131. Rune hotKeySpecifier = '_';
  132. bool supportFirstUpperCase = false;
  133. int hotPos = 0;
  134. Key hotKey = Key.Unknown;
  135. bool result = false;
  136. // lower case should return uppercase Hotkey
  137. text = "_k Before";
  138. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  139. Assert.True (result);
  140. Assert.Equal (0, hotPos);
  141. Assert.Equal ((Key)'K', hotKey);
  142. text = "a_k Second";
  143. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  144. Assert.True (result);
  145. Assert.Equal (1, hotPos);
  146. Assert.Equal ((Key)'K', hotKey);
  147. text = "Last _k";
  148. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  149. Assert.True (result);
  150. Assert.Equal (5, hotPos);
  151. Assert.Equal ((Key)'K', hotKey);
  152. text = "After k_";
  153. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  154. Assert.False (result);
  155. Assert.Equal (-1, hotPos);
  156. Assert.Equal (Key.Unknown, hotKey);
  157. text = "Multiple _k and _R";
  158. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  159. Assert.True (result);
  160. Assert.Equal (9, hotPos);
  161. Assert.Equal ((Key)'K', hotKey);
  162. // Lower case Cryllic K (к)
  163. text = "Non-english: _кдать";
  164. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  165. Assert.True (result);
  166. Assert.Equal (13, hotPos);
  167. Assert.Equal ((Key)'К', hotKey);
  168. // Turn on FirstUpperCase and verify same results
  169. supportFirstUpperCase = true;
  170. text = "_k Before";
  171. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  172. Assert.True (result);
  173. Assert.Equal (0, hotPos);
  174. Assert.Equal ((Key)'K', hotKey);
  175. text = "a_k Second";
  176. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  177. Assert.True (result);
  178. Assert.Equal (1, hotPos);
  179. Assert.Equal ((Key)'K', hotKey);
  180. text = "Last _k";
  181. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  182. Assert.True (result);
  183. Assert.Equal (5, hotPos);
  184. Assert.Equal ((Key)'K', hotKey);
  185. text = "After k_";
  186. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  187. Assert.False (result);
  188. Assert.Equal (-1, hotPos);
  189. Assert.Equal (Key.Unknown, hotKey);
  190. text = "Multiple _k and _R";
  191. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  192. Assert.True (result);
  193. Assert.Equal (9, hotPos);
  194. Assert.Equal ((Key)'K', hotKey);
  195. // Lower case Cryllic K (к)
  196. text = "Non-english: _кдать";
  197. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  198. Assert.True (result);
  199. Assert.Equal (13, hotPos);
  200. Assert.Equal ((Key)'К', hotKey);
  201. }
  202. [Fact]
  203. public void FindHotKey_Numeric_Succeeds ()
  204. {
  205. var text = ustring.Empty;
  206. Rune hotKeySpecifier = '_';
  207. bool supportFirstUpperCase = false;
  208. int hotPos = 0;
  209. Key hotKey = Key.Unknown;
  210. bool result = false;
  211. // Digits
  212. text = "_1 Before";
  213. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  214. Assert.True (result);
  215. Assert.Equal (0, hotPos);
  216. Assert.Equal ((Key)'1', hotKey);
  217. text = "a_1 Second";
  218. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  219. Assert.True (result);
  220. Assert.Equal (1, hotPos);
  221. Assert.Equal ((Key)'1', hotKey);
  222. text = "Last _1";
  223. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  224. Assert.True (result);
  225. Assert.Equal (5, hotPos);
  226. Assert.Equal ((Key)'1', hotKey);
  227. text = "After 1_";
  228. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  229. Assert.False (result);
  230. Assert.Equal (-1, hotPos);
  231. Assert.Equal (Key.Unknown, hotKey);
  232. text = "Multiple _1 and _2";
  233. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  234. Assert.True (result);
  235. Assert.Equal (9, hotPos);
  236. Assert.Equal ((Key)'1', hotKey);
  237. // Turn on FirstUpperCase and verify same results
  238. supportFirstUpperCase = true;
  239. text = "_1 Before";
  240. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  241. Assert.True (result);
  242. Assert.Equal (0, hotPos);
  243. Assert.Equal ((Key)'1', hotKey);
  244. text = "a_1 Second";
  245. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  246. Assert.True (result);
  247. Assert.Equal (1, hotPos);
  248. Assert.Equal ((Key)'1', hotKey);
  249. text = "Last _1";
  250. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  251. Assert.True (result);
  252. Assert.Equal (5, hotPos);
  253. Assert.Equal ((Key)'1', hotKey);
  254. text = "After 1_";
  255. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  256. Assert.False (result);
  257. Assert.Equal (-1, hotPos);
  258. Assert.Equal (Key.Unknown, hotKey);
  259. text = "Multiple _1 and _2";
  260. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  261. Assert.True (result);
  262. Assert.Equal (9, hotPos);
  263. Assert.Equal ((Key)'1', hotKey);
  264. }
  265. [Fact]
  266. public void FindHotKey_Legacy_FirstUpperCase_Succeeds ()
  267. {
  268. bool supportFirstUpperCase = true;
  269. var text = ustring.Empty;
  270. Rune hotKeySpecifier = (Rune)0;
  271. int hotPos = 0;
  272. Key hotKey = Key.Unknown;
  273. bool result = false;
  274. text = "K Before";
  275. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  276. Assert.True (result);
  277. Assert.Equal (0, hotPos);
  278. Assert.Equal ((Key)'K', hotKey);
  279. text = "aK Second";
  280. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  281. Assert.True (result);
  282. Assert.Equal (1, hotPos);
  283. Assert.Equal ((Key)'K', hotKey);
  284. text = "last K";
  285. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  286. Assert.True (result);
  287. Assert.Equal (5, hotPos);
  288. Assert.Equal ((Key)'K', hotKey);
  289. text = "multiple K and R";
  290. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  291. Assert.True (result);
  292. Assert.Equal (9, hotPos);
  293. Assert.Equal ((Key)'K', hotKey);
  294. // Cryllic K (К)
  295. text = "non-english: Кдать";
  296. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  297. Assert.True (result);
  298. Assert.Equal (13, hotPos);
  299. Assert.Equal ((Key)'К', hotKey);
  300. }
  301. [Fact]
  302. public void FindHotKey_Legacy_FirstUpperCase_NotFound_Returns_False ()
  303. {
  304. bool supportFirstUpperCase = true;
  305. var text = ustring.Empty;
  306. Rune hotKeySpecifier = (Rune)0;
  307. int hotPos = 0;
  308. Key hotKey = Key.Unknown;
  309. bool result = false;
  310. text = "k before";
  311. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  312. Assert.False (result);
  313. Assert.Equal (-1, hotPos);
  314. Assert.Equal (Key.Unknown, hotKey);
  315. text = "ak second";
  316. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  317. Assert.False (result);
  318. Assert.Equal (-1, hotPos);
  319. Assert.Equal (Key.Unknown, hotKey);
  320. text = "last k";
  321. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  322. Assert.False (result);
  323. Assert.Equal (-1, hotPos);
  324. Assert.Equal (Key.Unknown, hotKey);
  325. text = "multiple k and r";
  326. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  327. Assert.False (result);
  328. Assert.Equal (-1, hotPos);
  329. Assert.Equal (Key.Unknown, hotKey);
  330. text = "12345";
  331. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  332. Assert.False (result);
  333. Assert.Equal (-1, hotPos);
  334. Assert.Equal (Key.Unknown, hotKey);
  335. // punctuation
  336. text = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
  337. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  338. Assert.False (result);
  339. Assert.Equal (-1, hotPos);
  340. Assert.Equal (Key.Unknown, hotKey);
  341. // ~IsLetterOrDigit + Unicode
  342. text = " ~  s  gui.cs   master ↑10";
  343. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  344. Assert.False (result);
  345. Assert.Equal (-1, hotPos);
  346. Assert.Equal (Key.Unknown, hotKey);
  347. // Lower case Cryllic K (к)
  348. text = "non-english: кдать";
  349. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  350. Assert.False (result);
  351. Assert.Equal (-1, hotPos);
  352. Assert.Equal (Key.Unknown, hotKey);
  353. }
  354. static ustring testHotKeyAtStart = "_K Before";
  355. static ustring testHotKeyAtSecondPos = "a_K Second";
  356. static ustring testHotKeyAtLastPos = "Last _K";
  357. static ustring testHotKeyAfterLastChar = "After K_";
  358. static ustring testMultiHotKeys = "Multiple _K and _R";
  359. static ustring testNonEnglish = "Non-english: _Кдать";
  360. [Fact]
  361. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal ()
  362. {
  363. Rune hotKeySpecifier = '_';
  364. Assert.Null (TextFormatter.RemoveHotKeySpecifier (null, 0, hotKeySpecifier));
  365. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 0, hotKeySpecifier));
  366. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", -1, hotKeySpecifier));
  367. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 100, hotKeySpecifier));
  368. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", -1, hotKeySpecifier));
  369. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", 100, hotKeySpecifier));
  370. }
  371. [Fact]
  372. public void RemoveHotKeySpecifier_Valid_ReturnsStripped ()
  373. {
  374. Rune hotKeySpecifier = '_';
  375. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("_K Before", 0, hotKeySpecifier));
  376. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("a_K Second", 1, hotKeySpecifier));
  377. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last _K", 5, hotKeySpecifier));
  378. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K_", 7, hotKeySpecifier));
  379. Assert.Equal ("Multiple K and _R", TextFormatter.RemoveHotKeySpecifier ("Multiple _K and _R", 9, hotKeySpecifier));
  380. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: _Кдать", 13, hotKeySpecifier));
  381. }
  382. [Fact]
  383. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal ()
  384. {
  385. Rune hotKeySpecifier = '_';
  386. Assert.Equal ("all lower case", TextFormatter.RemoveHotKeySpecifier ("all lower case", 0, hotKeySpecifier));
  387. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("K Before", 0, hotKeySpecifier));
  388. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("aK Second", 1, hotKeySpecifier));
  389. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last K", 5, hotKeySpecifier));
  390. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K", 7, hotKeySpecifier));
  391. Assert.Equal ("Multiple K and R", TextFormatter.RemoveHotKeySpecifier ("Multiple K and R", 9, hotKeySpecifier));
  392. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: Кдать", 13, hotKeySpecifier));
  393. }
  394. [Fact]
  395. public void CalcRect_Invalid_Returns_Empty ()
  396. {
  397. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, null));
  398. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, ""));
  399. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (1, 2, ""));
  400. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (-1, -2, ""));
  401. }
  402. [Fact]
  403. public void CalcRect_SingleLine_Returns_1High ()
  404. {
  405. var text = ustring.Empty;
  406. text = "test";
  407. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  408. text = " ~  s  gui.cs   master ↑10";
  409. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  410. }
  411. [Fact]
  412. public void CalcRect_MultiLine_Returns_nHigh ()
  413. {
  414. var text = ustring.Empty;
  415. var lines = 0;
  416. text = "line1\nline2";
  417. lines = 2;
  418. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  419. text = "\nline2";
  420. lines = 2;
  421. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  422. text = "\n\n";
  423. lines = 3;
  424. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  425. text = "\n\n\n";
  426. lines = 4;
  427. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  428. text = "line1\nline2\nline3long!";
  429. lines = 3;
  430. Assert.Equal (new Rect (0, 0, 10, lines), TextFormatter.CalcRect (0, 0, text));
  431. text = "line1\nline2\n\n";
  432. lines = 4;
  433. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  434. text = "line1\r\nline2";
  435. lines = 2;
  436. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  437. text = " ~  s  gui.cs   master ↑10\n";
  438. lines = 2;
  439. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  440. text = "\n ~  s  gui.cs   master ↑10";
  441. lines = 2;
  442. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  443. text = " ~  s  gui.cs   master\n↑10";
  444. lines = 2;
  445. Assert.Equal (new Rect (0, 0, ustring.Make (" ~  s  gui.cs   master\n").RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  446. }
  447. [Fact]
  448. public void ClipAndJustify_Invalid_Returns_Original ()
  449. {
  450. var text = ustring.Empty;
  451. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  452. text = null;
  453. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  454. text = "test";
  455. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.ClipAndJustify (text, -1, TextAlignment.Left));
  456. }
  457. [Fact]
  458. public void ClipAndJustify_Valid_Left ()
  459. {
  460. var align = TextAlignment.Left;
  461. var text = ustring.Empty;
  462. var justifiedText = ustring.Empty;
  463. int maxWidth = 0;
  464. int expectedClippedWidth = 0;
  465. text = "test";
  466. maxWidth = 0;
  467. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  468. text = "test";
  469. maxWidth = 2;
  470. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  471. text = "test";
  472. maxWidth = int.MaxValue;
  473. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  474. Assert.True (justifiedText.RuneCount <= maxWidth);
  475. text = "A sentence has words.";
  476. // should fit
  477. maxWidth = text.RuneCount + 1;
  478. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  479. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  480. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  481. Assert.True (expectedClippedWidth <= maxWidth);
  482. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  483. // Should fit.
  484. maxWidth = text.RuneCount + 0;
  485. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  486. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  487. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  488. Assert.True (expectedClippedWidth <= maxWidth);
  489. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  490. // Should fit.
  491. maxWidth = int.MaxValue;
  492. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  493. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  494. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  495. Assert.True (expectedClippedWidth <= maxWidth);
  496. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  497. // Should not fit
  498. maxWidth = text.RuneCount - 1;
  499. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  500. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  501. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  502. Assert.True (expectedClippedWidth <= maxWidth);
  503. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  504. // Should not fit
  505. maxWidth = 10;
  506. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  507. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  508. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  509. Assert.True (expectedClippedWidth <= maxWidth);
  510. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  511. text = "A\tsentence\thas\twords.";
  512. maxWidth = int.MaxValue;
  513. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  514. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  515. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  516. Assert.True (expectedClippedWidth <= maxWidth);
  517. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  518. text = "A\tsentence\thas\twords.";
  519. maxWidth = 10;
  520. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  521. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  522. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  523. Assert.True (expectedClippedWidth <= maxWidth);
  524. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  525. text = "line1\nline2\nline3long!";
  526. maxWidth = int.MaxValue;
  527. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  528. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  529. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  530. Assert.True (expectedClippedWidth <= maxWidth);
  531. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  532. text = "line1\nline2\nline3long!";
  533. maxWidth = 10;
  534. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  535. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  536. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  537. Assert.True (expectedClippedWidth <= maxWidth);
  538. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  539. // Unicode
  540. text = " ~  s  gui.cs   master ↑10";
  541. maxWidth = 10;
  542. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  543. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  544. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  545. Assert.True (expectedClippedWidth <= maxWidth);
  546. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  547. // should fit
  548. text = "Ð ÑÐ";
  549. maxWidth = text.RuneCount + 1;
  550. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  551. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  552. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  553. Assert.True (expectedClippedWidth <= maxWidth);
  554. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  555. // Should fit.
  556. maxWidth = text.RuneCount + 0;
  557. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  558. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  559. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  560. Assert.True (expectedClippedWidth <= maxWidth);
  561. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  562. // Should not fit
  563. maxWidth = text.RuneCount - 1;
  564. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  565. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  566. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  567. Assert.True (expectedClippedWidth <= maxWidth);
  568. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  569. }
  570. [Fact]
  571. public void ClipAndJustify_Valid_Right ()
  572. {
  573. var align = TextAlignment.Right;
  574. var text = ustring.Empty;
  575. var justifiedText = ustring.Empty;
  576. int maxWidth = 0;
  577. int expectedClippedWidth = 0;
  578. text = "test";
  579. maxWidth = 0;
  580. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  581. text = "test";
  582. maxWidth = 2;
  583. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  584. text = "test";
  585. maxWidth = int.MaxValue;
  586. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  587. Assert.True (justifiedText.RuneCount <= maxWidth);
  588. text = "A sentence has words.";
  589. // should fit
  590. maxWidth = text.RuneCount + 1;
  591. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  592. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  593. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  594. Assert.True (expectedClippedWidth <= maxWidth);
  595. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  596. // Should fit.
  597. maxWidth = text.RuneCount + 0;
  598. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  599. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  600. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  601. Assert.True (expectedClippedWidth <= maxWidth);
  602. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  603. // Should fit.
  604. maxWidth = int.MaxValue;
  605. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  606. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  607. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  608. Assert.True (expectedClippedWidth <= maxWidth);
  609. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  610. // Should not fit
  611. maxWidth = text.RuneCount - 1;
  612. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  613. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  614. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  615. Assert.True (expectedClippedWidth <= maxWidth);
  616. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  617. // Should not fit
  618. maxWidth = 10;
  619. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  620. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  621. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  622. Assert.True (expectedClippedWidth <= maxWidth);
  623. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  624. text = "A\tsentence\thas\twords.";
  625. maxWidth = int.MaxValue;
  626. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  627. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  628. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  629. Assert.True (expectedClippedWidth <= maxWidth);
  630. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  631. text = "A\tsentence\thas\twords.";
  632. maxWidth = 10;
  633. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  634. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  635. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  636. Assert.True (expectedClippedWidth <= maxWidth);
  637. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  638. text = "line1\nline2\nline3long!";
  639. maxWidth = int.MaxValue;
  640. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  641. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  642. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  643. Assert.True (expectedClippedWidth <= maxWidth);
  644. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  645. text = "line1\nline2\nline3long!";
  646. maxWidth = 10;
  647. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  648. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  649. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  650. Assert.True (expectedClippedWidth <= maxWidth);
  651. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  652. // Unicode
  653. text = " ~  s  gui.cs   master ↑10";
  654. maxWidth = 10;
  655. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  656. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  657. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  658. Assert.True (expectedClippedWidth <= maxWidth);
  659. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  660. // should fit
  661. text = "Ð ÑÐ";
  662. maxWidth = text.RuneCount + 1;
  663. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  664. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  665. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  666. Assert.True (expectedClippedWidth <= maxWidth);
  667. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  668. // Should fit.
  669. maxWidth = text.RuneCount + 0;
  670. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  671. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  672. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  673. Assert.True (expectedClippedWidth <= maxWidth);
  674. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  675. // Should not fit
  676. maxWidth = text.RuneCount - 1;
  677. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  678. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  679. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  680. Assert.True (expectedClippedWidth <= maxWidth);
  681. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  682. }
  683. [Fact]
  684. public void ClipAndJustify_Valid_Centered ()
  685. {
  686. var align = TextAlignment.Centered;
  687. var text = ustring.Empty;
  688. var justifiedText = ustring.Empty;
  689. int maxWidth = 0;
  690. int expectedClippedWidth = 0;
  691. text = "test";
  692. maxWidth = 0;
  693. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  694. text = "test";
  695. maxWidth = 2;
  696. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  697. text = "test";
  698. maxWidth = int.MaxValue;
  699. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  700. Assert.True (justifiedText.RuneCount <= maxWidth);
  701. text = "A sentence has words.";
  702. // should fit
  703. maxWidth = text.RuneCount + 1;
  704. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  705. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  706. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  707. Assert.True (expectedClippedWidth <= maxWidth);
  708. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  709. // Should fit.
  710. maxWidth = text.RuneCount + 0;
  711. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  712. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  713. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  714. Assert.True (expectedClippedWidth <= maxWidth);
  715. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  716. // Should fit.
  717. maxWidth = int.MaxValue;
  718. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  719. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  720. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  721. Assert.True (expectedClippedWidth <= maxWidth);
  722. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  723. // Should not fit
  724. maxWidth = text.RuneCount - 1;
  725. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  726. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  727. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  728. Assert.True (expectedClippedWidth <= maxWidth);
  729. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  730. // Should not fit
  731. maxWidth = 10;
  732. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  733. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  734. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  735. Assert.True (expectedClippedWidth <= maxWidth);
  736. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  737. text = "A\tsentence\thas\twords.";
  738. maxWidth = int.MaxValue;
  739. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  740. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  741. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  742. Assert.True (expectedClippedWidth <= maxWidth);
  743. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  744. text = "A\tsentence\thas\twords.";
  745. maxWidth = 10;
  746. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  747. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  748. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  749. Assert.True (expectedClippedWidth <= maxWidth);
  750. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  751. text = "line1\nline2\nline3long!";
  752. maxWidth = int.MaxValue;
  753. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  754. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  755. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  756. Assert.True (expectedClippedWidth <= maxWidth);
  757. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  758. text = "line1\nline2\nline3long!";
  759. maxWidth = 10;
  760. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  761. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  762. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  763. Assert.True (expectedClippedWidth <= maxWidth);
  764. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  765. // Unicode
  766. text = " ~  s  gui.cs   master ↑10";
  767. maxWidth = 10;
  768. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  769. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  770. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  771. Assert.True (expectedClippedWidth <= maxWidth);
  772. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  773. // should fit
  774. text = "Ð ÑÐ";
  775. maxWidth = text.RuneCount + 1;
  776. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  777. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  778. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  779. Assert.True (expectedClippedWidth <= maxWidth);
  780. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  781. // Should fit.
  782. maxWidth = text.RuneCount + 0;
  783. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  784. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  785. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  786. Assert.True (expectedClippedWidth <= maxWidth);
  787. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  788. // Should not fit
  789. maxWidth = text.RuneCount - 1;
  790. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  791. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  792. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  793. Assert.True (expectedClippedWidth <= maxWidth);
  794. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  795. }
  796. [Fact]
  797. public void ClipAndJustify_Valid_Justified ()
  798. {
  799. var align = TextAlignment.Justified;
  800. var text = ustring.Empty;
  801. var justifiedText = ustring.Empty;
  802. int maxWidth = 0;
  803. int expectedClippedWidth = 0;
  804. text = "test";
  805. maxWidth = 0;
  806. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  807. text = "test";
  808. maxWidth = 2;
  809. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  810. text = "test";
  811. maxWidth = int.MaxValue;
  812. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  813. Assert.True (justifiedText.RuneCount <= maxWidth);
  814. text = "A sentence has words.";
  815. // should fit
  816. maxWidth = text.RuneCount + 1;
  817. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  818. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  819. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  820. Assert.True (expectedClippedWidth <= maxWidth);
  821. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  822. // Should fit.
  823. maxWidth = text.RuneCount + 0;
  824. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  825. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  826. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  827. Assert.True (expectedClippedWidth <= maxWidth);
  828. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  829. // Should fit.
  830. maxWidth = 500;
  831. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  832. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  833. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  834. Assert.True (expectedClippedWidth <= maxWidth);
  835. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  836. // Should not fit
  837. maxWidth = text.RuneCount - 1;
  838. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  839. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  840. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  841. Assert.True (expectedClippedWidth <= maxWidth);
  842. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  843. // Should not fit
  844. maxWidth = 10;
  845. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  846. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  847. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  848. Assert.True (expectedClippedWidth <= maxWidth);
  849. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  850. text = "A\tsentence\thas\twords.";
  851. maxWidth = int.MaxValue;
  852. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  853. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  854. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  855. Assert.True (expectedClippedWidth <= maxWidth);
  856. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  857. text = "A\tsentence\thas\twords.";
  858. maxWidth = 10;
  859. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  860. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  861. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  862. Assert.True (expectedClippedWidth <= maxWidth);
  863. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  864. text = "line1\nline2\nline3long!";
  865. maxWidth = int.MaxValue;
  866. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  867. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  868. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  869. Assert.True (expectedClippedWidth <= maxWidth);
  870. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  871. text = "line1\nline2\nline3long!";
  872. maxWidth = 10;
  873. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  874. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  875. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  876. Assert.True (expectedClippedWidth <= maxWidth);
  877. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  878. // Unicode
  879. text = " ~  s  gui.cs   master ↑10";
  880. maxWidth = 10;
  881. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  882. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  883. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  884. Assert.True (expectedClippedWidth <= maxWidth);
  885. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  886. // should fit
  887. text = "Ð ÑÐ";
  888. maxWidth = text.RuneCount + 1;
  889. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  890. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  891. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  892. Assert.True (expectedClippedWidth <= maxWidth);
  893. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  894. // Should fit.
  895. maxWidth = text.RuneCount + 0;
  896. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  897. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  898. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  899. Assert.True (expectedClippedWidth <= maxWidth);
  900. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  901. // Should not fit
  902. maxWidth = text.RuneCount - 1;
  903. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  904. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  905. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  906. Assert.True (expectedClippedWidth <= maxWidth);
  907. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  908. // see Justify_ tests below
  909. }
  910. [Fact]
  911. public void Justify_Invalid ()
  912. {
  913. var text = ustring.Empty;
  914. Assert.Equal (text, TextFormatter.Justify (text, 0));
  915. text = null;
  916. Assert.Equal (text, TextFormatter.Justify (text, 0));
  917. text = "test";
  918. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  919. }
  920. [Fact]
  921. public void Justify_SingleWord ()
  922. {
  923. var text = ustring.Empty;
  924. var justifiedText = ustring.Empty;
  925. int width = 0;
  926. char fillChar = '+';
  927. // Even # of chars
  928. text = "word";
  929. justifiedText = text;
  930. width = text.RuneCount;
  931. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  932. width = text.RuneCount + 1;
  933. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  934. width = text.RuneCount + 2;
  935. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  936. width = text.RuneCount + 10;
  937. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  938. width = text.RuneCount + 11;
  939. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  940. // Odd # of chars
  941. text = "word.";
  942. justifiedText = text;
  943. width = text.RuneCount;
  944. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  945. width = text.RuneCount + 1;
  946. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  947. width = text.RuneCount + 2;
  948. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  949. width = text.RuneCount + 10;
  950. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  951. width = text.RuneCount + 11;
  952. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  953. // Unicode (even #)
  954. text = "пÑивеÑ";
  955. justifiedText = text;
  956. width = text.RuneCount;
  957. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  958. width = text.RuneCount + 1;
  959. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  960. width = text.RuneCount + 2;
  961. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  962. width = text.RuneCount + 10;
  963. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  964. width = text.RuneCount + 11;
  965. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  966. // Unicode (odd # of chars)
  967. text = "пÑивеÑ.";
  968. justifiedText = text;
  969. width = text.RuneCount;
  970. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  971. width = text.RuneCount + 1;
  972. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  973. width = text.RuneCount + 2;
  974. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  975. width = text.RuneCount + 10;
  976. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  977. width = text.RuneCount + 11;
  978. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  979. }
  980. [Fact]
  981. public void Justify_Sentence ()
  982. {
  983. var text = ustring.Empty;
  984. var justifiedText = ustring.Empty;
  985. int forceToWidth = 0;
  986. char fillChar = '+';
  987. // Even # of spaces
  988. // 0123456789
  989. text = "012 456 89";
  990. forceToWidth = text.RuneCount;
  991. justifiedText = text.Replace (" ", "+");
  992. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  993. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  994. justifiedText = text.Replace (" ", "+");
  995. forceToWidth = text.RuneCount + 1;
  996. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  997. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  998. justifiedText = text.Replace (" ", "++");
  999. forceToWidth = text.RuneCount + 2;
  1000. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1001. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1002. justifiedText = text.Replace (" ", "++");
  1003. forceToWidth = text.RuneCount + 3;
  1004. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1005. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1006. justifiedText = text.Replace (" ", "+++");
  1007. forceToWidth = text.RuneCount + 4;
  1008. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1009. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1010. justifiedText = text.Replace (" ", "+++");
  1011. forceToWidth = text.RuneCount + 5;
  1012. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1013. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1014. justifiedText = text.Replace (" ", "++++");
  1015. forceToWidth = text.RuneCount + 6;
  1016. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1017. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1018. justifiedText = text.Replace (" ", "+++++++++++");
  1019. forceToWidth = text.RuneCount + 20;
  1020. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1021. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1022. justifiedText = text.Replace (" ", "++++++++++++");
  1023. forceToWidth = text.RuneCount + 23;
  1024. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1025. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1026. // Odd # of spaces
  1027. // 0123456789
  1028. text = "012 456 89 end";
  1029. forceToWidth = text.RuneCount;
  1030. justifiedText = text.Replace (" ", "+");
  1031. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1032. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1033. justifiedText = text.Replace (" ", "+");
  1034. forceToWidth = text.RuneCount + 1;
  1035. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1036. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1037. justifiedText = text.Replace (" ", "+");
  1038. forceToWidth = text.RuneCount + 2;
  1039. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1040. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1041. justifiedText = text.Replace (" ", "++");
  1042. forceToWidth = text.RuneCount + 3;
  1043. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1044. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1045. justifiedText = text.Replace (" ", "++");
  1046. forceToWidth = text.RuneCount + 4;
  1047. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1048. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1049. justifiedText = text.Replace (" ", "++");
  1050. forceToWidth = text.RuneCount + 5;
  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 + 6;
  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 + 20;
  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 + 23;
  1063. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1064. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1065. // Unicode
  1066. // Even # of chars
  1067. // 0123456789
  1068. text = "пÑРвРÑ";
  1069. forceToWidth = text.RuneCount;
  1070. justifiedText = text.Replace (" ", "+");
  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 + 1;
  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 + 2;
  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 + 3;
  1083. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1084. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1085. justifiedText = text.Replace (" ", "+++");
  1086. forceToWidth = text.RuneCount + 4;
  1087. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1088. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1089. justifiedText = text.Replace (" ", "+++");
  1090. forceToWidth = text.RuneCount + 5;
  1091. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1092. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1093. justifiedText = text.Replace (" ", "++++");
  1094. forceToWidth = text.RuneCount + 6;
  1095. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1096. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1097. justifiedText = text.Replace (" ", "+++++++++++");
  1098. forceToWidth = text.RuneCount + 20;
  1099. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1100. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1101. justifiedText = text.Replace (" ", "++++++++++++");
  1102. forceToWidth = text.RuneCount + 23;
  1103. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1104. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1105. // Unicode
  1106. // Odd # of chars
  1107. // 0123456789
  1108. text = "Ð ÑРвРÑ";
  1109. forceToWidth = text.RuneCount;
  1110. justifiedText = text.Replace (" ", "+");
  1111. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1112. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1113. justifiedText = text.Replace (" ", "+");
  1114. forceToWidth = text.RuneCount + 1;
  1115. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1116. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1117. justifiedText = text.Replace (" ", "+");
  1118. forceToWidth = text.RuneCount + 2;
  1119. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1120. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1121. justifiedText = text.Replace (" ", "++");
  1122. forceToWidth = text.RuneCount + 3;
  1123. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1124. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1125. justifiedText = text.Replace (" ", "++");
  1126. forceToWidth = text.RuneCount + 4;
  1127. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1128. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1129. justifiedText = text.Replace (" ", "++");
  1130. forceToWidth = text.RuneCount + 5;
  1131. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1132. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1133. justifiedText = text.Replace (" ", "+++");
  1134. forceToWidth = text.RuneCount + 6;
  1135. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1136. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1137. justifiedText = text.Replace (" ", "+++++++");
  1138. forceToWidth = text.RuneCount + 20;
  1139. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1140. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1141. justifiedText = text.Replace (" ", "++++++++");
  1142. forceToWidth = text.RuneCount + 23;
  1143. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1144. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1145. }
  1146. [Fact]
  1147. public void WordWrap_Invalid ()
  1148. {
  1149. var text = ustring.Empty;
  1150. int width = 0;
  1151. Assert.Empty (TextFormatter.WordWrap (null, width));
  1152. Assert.Empty (TextFormatter.WordWrap (text, width));
  1153. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrap (text, -1));
  1154. }
  1155. [Fact]
  1156. public void WordWrap_SingleWordLine ()
  1157. {
  1158. var text = ustring.Empty;
  1159. int width = 0;
  1160. List<ustring> wrappedLines;
  1161. text = "Constantinople";
  1162. width = text.RuneCount;
  1163. wrappedLines = TextFormatter.WordWrap (text, width);
  1164. Assert.True (wrappedLines.Count == 1);
  1165. width = text.RuneCount - 1;
  1166. wrappedLines = TextFormatter.WordWrap (text, width);
  1167. Assert.Equal (2, wrappedLines.Count);
  1168. Assert.Equal (text [0, text.RuneCount - 1].ToString (), wrappedLines [0].ToString ());
  1169. Assert.Equal ("e", wrappedLines [1].ToString ());
  1170. width = text.RuneCount - 2;
  1171. wrappedLines = TextFormatter.WordWrap (text, width);
  1172. Assert.Equal (2, wrappedLines.Count);
  1173. Assert.Equal (text [0, text.RuneCount - 2].ToString (), wrappedLines [0].ToString ());
  1174. width = text.RuneCount - 5;
  1175. wrappedLines = TextFormatter.WordWrap (text, width);
  1176. Assert.Equal (2, wrappedLines.Count);
  1177. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1178. wrappedLines = TextFormatter.WordWrap (text, width);
  1179. Assert.Equal (2, wrappedLines.Count);
  1180. Assert.Equal ("Constan", wrappedLines [0].ToString ());
  1181. Assert.Equal ("tinople", wrappedLines [1].ToString ());
  1182. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1183. wrappedLines = TextFormatter.WordWrap (text, width);
  1184. Assert.Equal (3, wrappedLines.Count);
  1185. Assert.Equal ("Const", wrappedLines [0].ToString ());
  1186. Assert.Equal ("antin", wrappedLines [1].ToString ());
  1187. Assert.Equal ("ople", wrappedLines [2].ToString ());
  1188. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1189. wrappedLines = TextFormatter.WordWrap (text, width);
  1190. Assert.Equal (4, wrappedLines.Count);
  1191. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1192. wrappedLines = TextFormatter.WordWrap (text, width);
  1193. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1194. Assert.Equal ("C", wrappedLines [0].ToString ());
  1195. Assert.Equal ("o", wrappedLines [1].ToString ());
  1196. Assert.Equal ("n", wrappedLines [2].ToString ());
  1197. Assert.Equal ("s", wrappedLines [3].ToString ());
  1198. Assert.Equal ("t", wrappedLines [4].ToString ());
  1199. Assert.Equal ("a", wrappedLines [5].ToString ());
  1200. Assert.Equal ("n", wrappedLines [6].ToString ());
  1201. Assert.Equal ("t", wrappedLines [7].ToString ());
  1202. Assert.Equal ("i", wrappedLines [8].ToString ());
  1203. Assert.Equal ("n", wrappedLines [9].ToString ());
  1204. Assert.Equal ("o", wrappedLines [10].ToString ());
  1205. Assert.Equal ("p", wrappedLines [11].ToString ());
  1206. Assert.Equal ("l", wrappedLines [12].ToString ());
  1207. Assert.Equal ("e", wrappedLines [13].ToString ());
  1208. }
  1209. [Fact]
  1210. public void WordWrap_Unicode_SingleWordLine ()
  1211. {
  1212. var text = ustring.Empty;
  1213. int width = 0;
  1214. List<ustring> wrappedLines;
  1215. text = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ";
  1216. width = text.RuneCount;
  1217. wrappedLines = TextFormatter.WordWrap (text, width);
  1218. Assert.True (wrappedLines.Count == 1);
  1219. width = text.RuneCount - 1;
  1220. wrappedLines = TextFormatter.WordWrap (text, width);
  1221. Assert.Equal (2, wrappedLines.Count);
  1222. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1223. Assert.Equal ("ำ", wrappedLines [1].ToString ());
  1224. width = text.RuneCount - 2;
  1225. wrappedLines = TextFormatter.WordWrap (text, width);
  1226. Assert.Equal (2, wrappedLines.Count);
  1227. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1228. width = text.RuneCount - 5;
  1229. wrappedLines = TextFormatter.WordWrap (text, width);
  1230. Assert.Equal (2, wrappedLines.Count);
  1231. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1232. wrappedLines = TextFormatter.WordWrap (text, width);
  1233. Assert.Equal (2, wrappedLines.Count);
  1234. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", wrappedLines [0].ToString ());
  1235. Assert.Equal ("ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [1].ToString ());
  1236. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1237. wrappedLines = TextFormatter.WordWrap (text, width);
  1238. Assert.Equal (3, wrappedLines.Count);
  1239. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑ", wrappedLines [0].ToString ());
  1240. Assert.Equal ("ฒณดตถทธนบปผฝพฟภมย", wrappedLines [1].ToString ());
  1241. Assert.Equal ("รฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [2].ToString ());
  1242. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1243. wrappedLines = TextFormatter.WordWrap (text, width);
  1244. Assert.Equal (4, wrappedLines.Count);
  1245. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1246. wrappedLines = TextFormatter.WordWrap (text, width);
  1247. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1248. Assert.Equal ("ก", wrappedLines [0].ToString ());
  1249. Assert.Equal ("ข", wrappedLines [1].ToString ());
  1250. Assert.Equal ("ฃ", wrappedLines [2].ToString ());
  1251. Assert.Equal ("ำ", wrappedLines [^1].ToString ());
  1252. }
  1253. [Fact]
  1254. public void WordWrap_NoNewLines ()
  1255. {
  1256. var text = ustring.Empty;
  1257. int maxWidth = 0;
  1258. int expectedClippedWidth = 0;
  1259. List<ustring> wrappedLines;
  1260. text = "A sentence has words.";
  1261. maxWidth = text.RuneCount;
  1262. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1263. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1264. Assert.True (wrappedLines.Count == 1);
  1265. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1266. Assert.Equal ("A sentence has words.", wrappedLines [0].ToString ());
  1267. maxWidth = text.RuneCount - 1;
  1268. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1269. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1270. Assert.Equal (2, wrappedLines.Count);
  1271. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1272. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1273. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1274. maxWidth = text.RuneCount - "words.".Length;
  1275. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1276. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1277. Assert.Equal (2, wrappedLines.Count);
  1278. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1279. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1280. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1281. maxWidth = text.RuneCount - " words.".Length;
  1282. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1283. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1284. Assert.Equal (2, wrappedLines.Count);
  1285. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1286. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1287. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1288. maxWidth = text.RuneCount - "s words.".Length;
  1289. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1290. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1291. Assert.Equal (2, wrappedLines.Count);
  1292. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1293. Assert.Equal ("A sentence", wrappedLines [0].ToString ());
  1294. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1295. // Unicode
  1296. text = "A Unicode sentence (пÑивеÑ) has words.";
  1297. maxWidth = text.RuneCount;
  1298. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1299. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1300. Assert.True (wrappedLines.Count == 1);
  1301. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1302. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.", wrappedLines [0].ToString ());
  1303. maxWidth = text.RuneCount - 1;
  1304. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1305. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1306. Assert.Equal (2, wrappedLines.Count);
  1307. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1308. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1309. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1310. maxWidth = text.RuneCount - "words.".Length;
  1311. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1312. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1313. Assert.Equal (2, wrappedLines.Count);
  1314. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1315. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1316. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1317. maxWidth = text.RuneCount - " words.".Length;
  1318. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1319. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1320. Assert.Equal (2, wrappedLines.Count);
  1321. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1322. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1323. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1324. maxWidth = text.RuneCount - "s words.".Length;
  1325. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1326. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1327. Assert.Equal (2, wrappedLines.Count);
  1328. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1329. Assert.Equal ("A Unicode sentence (пÑивеÑ)", wrappedLines [0].ToString ());
  1330. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1331. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1332. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1333. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1334. Assert.Equal (2, wrappedLines.Count);
  1335. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1336. Assert.Equal ("A Unicode sentence", wrappedLines [0].ToString ());
  1337. Assert.Equal ("(пÑивеÑ) has words.", wrappedLines [1].ToString ());
  1338. }
  1339. [Fact]
  1340. public void WordWrap_Narrow ()
  1341. {
  1342. var text = ustring.Empty;
  1343. int maxWidth = 1;
  1344. int expectedClippedWidth = 1;
  1345. List<ustring> wrappedLines;
  1346. text = "A sentence has words.";
  1347. maxWidth = 3;
  1348. expectedClippedWidth = 3;
  1349. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1350. //Assert.True (wrappedLines.Count == );
  1351. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1352. Assert.Equal ("A", wrappedLines [0].ToString ());
  1353. // BUGBUG: WordWrap breaks down with small widths. It should not
  1354. // the following line should be "sen"...
  1355. Assert.Equal ("se", wrappedLines [1].ToString ());
  1356. Assert.Equal ("nte", wrappedLines [2].ToString ());
  1357. Assert.Equal ("nce", wrappedLines [3].ToString ());
  1358. Assert.Equal ("ha", wrappedLines [4].ToString ());
  1359. Assert.Equal ("s", wrappedLines [5].ToString ());
  1360. Assert.Equal ("wo", wrappedLines [6].ToString ());
  1361. Assert.Equal ("rds", wrappedLines [7].ToString ());
  1362. Assert.Equal (".", wrappedLines [^1].ToString ());
  1363. maxWidth = 2;
  1364. expectedClippedWidth = 2;
  1365. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1366. //Assert.True (wrappedLines.Count == );
  1367. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1368. Assert.Equal ("A", wrappedLines [0].ToString ());
  1369. Assert.Equal ("s", wrappedLines [1].ToString ());
  1370. Assert.Equal ("en", wrappedLines [2].ToString ());
  1371. Assert.Equal ("te", wrappedLines [3].ToString ());
  1372. Assert.Equal (".", wrappedLines [^1].ToString ());
  1373. maxWidth = 1;
  1374. expectedClippedWidth = 1;
  1375. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1376. //Assert.True (wrappedLines.Count == );
  1377. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1378. Assert.Equal ("A", wrappedLines [0].ToString ());
  1379. // BUGBUG: WordWrap breaks down with a width of one. It should not
  1380. // provide blank lines like it does.
  1381. Assert.Equal ("", wrappedLines [1].ToString ());
  1382. Assert.Equal ("s", wrappedLines [2].ToString ());
  1383. Assert.Equal ("e", wrappedLines [3].ToString ());
  1384. Assert.Equal ("n", wrappedLines [4].ToString ());
  1385. Assert.Equal (".", wrappedLines [^1].ToString ());
  1386. }
  1387. [Fact]
  1388. public void ReplaceHotKeyWithTag ()
  1389. {
  1390. var tf = new TextFormatter ();
  1391. ustring text = "test";
  1392. int hotPos = 0;
  1393. uint tag = tf.HotKeyTagMask | 't';
  1394. Assert.Equal (ustring.Make (new Rune [] { tag, 'e', 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1395. tag = tf.HotKeyTagMask | 'e';
  1396. hotPos = 1;
  1397. Assert.Equal (ustring.Make (new Rune [] { 't', tag, 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1398. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  1399. Assert.Equal ('e', (uint)(result.ToRunes () [1] & ~tf.HotKeyTagMask));
  1400. text = "Ok";
  1401. tag = 0x100000 | 'O';
  1402. hotPos = 0;
  1403. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1404. Assert.Equal ('O', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1405. text = "[◦ Ok ◦]";
  1406. text = ustring.Make (new Rune [] { '[', '◦', ' ', 'O', 'k', ' ', '◦', ']' });
  1407. var runes = text.ToRuneList ();
  1408. Assert.Equal (text.RuneCount, runes.Count);
  1409. Assert.Equal (text, ustring.Make (runes));
  1410. tag = tf.HotKeyTagMask | 'O';
  1411. hotPos = 3;
  1412. Assert.Equal (ustring.Make (new Rune [] { '[', '◦', ' ', tag, 'k', ' ', '◦', ']' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1413. Assert.Equal ('O', (uint)(result.ToRunes () [3] & ~tf.HotKeyTagMask));
  1414. text = "^k";
  1415. tag = '^';
  1416. hotPos = 0;
  1417. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1418. Assert.Equal ('^', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1419. }
  1420. [Fact]
  1421. public void Reformat_Invalid ()
  1422. {
  1423. var text = ustring.Empty;
  1424. var list = new List<ustring> ();
  1425. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, -1, TextAlignment.Left, false));
  1426. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1427. Assert.NotEmpty (list);
  1428. Assert.True (list.Count == 1);
  1429. Assert.Equal (ustring.Empty, list [0]);
  1430. text = null;
  1431. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1432. Assert.NotEmpty (list);
  1433. Assert.True (list.Count == 1);
  1434. Assert.Equal (ustring.Empty, list [0]);
  1435. list = TextFormatter.Format (text, 0, TextAlignment.Left, true);
  1436. Assert.NotEmpty (list);
  1437. Assert.True (list.Count == 1);
  1438. Assert.Equal (ustring.Empty, list [0]);
  1439. }
  1440. [Fact]
  1441. public void Reformat_NoWordrap_SingleLine ()
  1442. {
  1443. var text = ustring.Empty;
  1444. var list = new List<ustring> ();
  1445. var maxWidth = 0;
  1446. var expectedClippedWidth = 0;
  1447. var wrap = false;
  1448. maxWidth = 0;
  1449. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1450. Assert.True (list.Count == 1);
  1451. maxWidth = 1;
  1452. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1453. Assert.True (list.Count == 1);
  1454. text = "A sentence has words.";
  1455. maxWidth = 0;
  1456. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1457. Assert.True (list.Count == 1);
  1458. Assert.Equal (ustring.Empty, list [0]);
  1459. maxWidth = 1;
  1460. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1461. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1462. Assert.True (list.Count == 1);
  1463. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1464. maxWidth = 5;
  1465. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1466. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1467. Assert.True (list.Count == 1);
  1468. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list[0]);
  1469. maxWidth = text.RuneCount - 1;
  1470. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1471. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1472. Assert.True (list.Count == 1);
  1473. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1474. // no clip
  1475. maxWidth = text.RuneCount + 0;
  1476. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1477. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1478. Assert.True (list.Count == 1);
  1479. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1480. maxWidth = text.RuneCount + 1;
  1481. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1482. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1483. Assert.True (list.Count == 1);
  1484. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1485. }
  1486. [Fact]
  1487. public void Reformat_NoWordrap_NewLines ()
  1488. {
  1489. var text = ustring.Empty;
  1490. var list = new List<ustring> ();
  1491. var maxWidth = 0;
  1492. var expectedClippedWidth = 0;
  1493. var wrap = false;
  1494. text = "A sentence has words.\nLine 2.";
  1495. maxWidth = 0;
  1496. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1497. Assert.True (list.Count == 1);
  1498. Assert.Equal (ustring.Empty, list [0]);
  1499. maxWidth = 1;
  1500. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1501. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1502. Assert.True (list.Count == 1);
  1503. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1504. maxWidth = 5;
  1505. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1506. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1507. Assert.True (list.Count == 1);
  1508. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1509. maxWidth = text.RuneCount - 1;
  1510. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1511. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1512. Assert.True (list.Count == 1);
  1513. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace("\n", " "), list [0]);
  1514. // no clip
  1515. maxWidth = text.RuneCount + 0;
  1516. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1517. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1518. Assert.True (list.Count == 1);
  1519. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1520. maxWidth = text.RuneCount + 1;
  1521. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1522. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1523. Assert.True (list.Count == 1);
  1524. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1525. text = "A sentence has words.\r\nLine 2.";
  1526. maxWidth = 0;
  1527. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1528. Assert.True (list.Count == 1);
  1529. Assert.Equal (ustring.Empty, list [0]);
  1530. maxWidth = 1;
  1531. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1532. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1533. Assert.True (list.Count == 1);
  1534. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1535. maxWidth = 5;
  1536. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1537. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1538. Assert.True (list.Count == 1);
  1539. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1540. maxWidth = text.RuneCount - 1;
  1541. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth) + 1;
  1542. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1543. Assert.True (list.Count == 1);
  1544. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " ").ToString(), list [0].ToString());
  1545. // no clip
  1546. maxWidth = text.RuneCount + 0;
  1547. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1548. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1549. Assert.True (list.Count == 1);
  1550. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1551. maxWidth = text.RuneCount + 1;
  1552. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1553. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1554. Assert.True (list.Count == 1);
  1555. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1556. }
  1557. [Fact]
  1558. public void Reformat_Wrap_Spaces_No_NewLines ()
  1559. {
  1560. var text = ustring.Empty;
  1561. var list = new List<ustring> ();
  1562. var maxWidth = 0;
  1563. var expectedClippedWidth = 0;
  1564. var wrap = true;
  1565. // Even # of spaces
  1566. // 0123456789
  1567. text = "012 456 89";
  1568. // See WordWrap BUGBUGs above.
  1569. //maxWidth = 0;
  1570. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1571. //Assert.True (list.Count == 1);
  1572. //Assert.Equal (ustring.Empty, list [0]);
  1573. //maxWidth = 1;
  1574. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1575. //// remove 3 whitespace chars
  1576. //expectedLines = text.RuneCount;
  1577. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1578. //Assert.Equal (expectedLines, list.Count);
  1579. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1580. ////width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1581. //maxWidth = 5;
  1582. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1583. //expectedLines = (int)Math.Ceiling ((double)(text.RuneCount / maxWidth));
  1584. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  1585. //Assert.Equal (expectedLines, list.Count);
  1586. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1587. maxWidth = text.RuneCount - 1;
  1588. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1589. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1590. Assert.True (list.Count == 2);
  1591. Assert.Equal ("012 456", list [0]);
  1592. Assert.Equal ("89", list [1]);
  1593. // no clip
  1594. maxWidth = text.RuneCount + 0;
  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 ("012 456 89", list [0]);
  1599. maxWidth = text.RuneCount + 1;
  1600. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1601. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1602. Assert.True (list.Count == 1);
  1603. Assert.Equal ("012 456 89", list [0]);
  1604. // Odd # of spaces
  1605. // 0123456789
  1606. text = "012 456 89 end";
  1607. maxWidth = text.RuneCount - 1;
  1608. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1609. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1610. Assert.True (list.Count == 2);
  1611. Assert.Equal ("012 456 89", list [0]);
  1612. Assert.Equal ("end", list [1]);
  1613. // no clip
  1614. maxWidth = text.RuneCount + 0;
  1615. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1616. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1617. Assert.True (list.Count == 1);
  1618. Assert.Equal ("012 456 89 end", list [0]);
  1619. maxWidth = text.RuneCount + 1;
  1620. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1621. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1622. Assert.True (list.Count == 1);
  1623. Assert.Equal ("012 456 89 end", list [0]);
  1624. }
  1625. [Fact]
  1626. public void Reformat_Unicode_Wrap_Spaces_No_NewLines ()
  1627. {
  1628. var text = ustring.Empty;
  1629. var list = new List<ustring> ();
  1630. var maxWidth = 0;
  1631. var expectedClippedWidth = 0;
  1632. var wrap = true;
  1633. // Unicode
  1634. // Even # of chars
  1635. // 0123456789
  1636. text = "пÑРвРÑ";
  1637. maxWidth = text.RuneCount - 1;
  1638. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1639. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1640. Assert.True (list.Count == 2);
  1641. Assert.Equal ("пÑРвÐ", list [0]);
  1642. Assert.Equal ("Ñ", list [1]);
  1643. // no clip
  1644. maxWidth = text.RuneCount + 0;
  1645. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1646. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1647. Assert.True (list.Count == 1);
  1648. Assert.Equal ("пÑРвРÑ", list [0]);
  1649. maxWidth = text.RuneCount + 1;
  1650. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1651. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1652. Assert.True (list.Count == 1);
  1653. Assert.Equal ("пÑРвРÑ", list [0]);
  1654. // Unicode
  1655. // Odd # of chars
  1656. // 0123456789
  1657. text = "Ð ÑРвРÑ";
  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 == 2);
  1662. Assert.Equal ("Ð ÑРвÐ", list [0]);
  1663. Assert.Equal ("Ñ", list [1]);
  1664. // no clip
  1665. maxWidth = text.RuneCount + 0;
  1666. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1667. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1668. Assert.True (list.Count == 1);
  1669. Assert.Equal ("Ð ÑРвРÑ", list [0]);
  1670. maxWidth = text.RuneCount + 1;
  1671. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1672. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1673. Assert.True (list.Count == 1);
  1674. Assert.Equal ("Ð ÑРвРÑ", list [0]);
  1675. }
  1676. }
  1677. }