2
0

TextFormatterTests.cs 94 KB

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