TextFormatterTests.cs 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. using System.Text;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. // Alias Console to MockConsole so we don't accidentally use Console
  8. using Console = Terminal.Gui.FakeConsole;
  9. namespace Terminal.Gui.TextTests {
  10. public class TextFormatterTests {
  11. readonly ITestOutputHelper output;
  12. public TextFormatterTests (ITestOutputHelper output)
  13. {
  14. this.output = output;
  15. }
  16. [Fact]
  17. public void Basic_Usage ()
  18. {
  19. var testText = "test";
  20. var expectedSize = new Size ();
  21. var testBounds = new Rect (0, 0, 100, 1);
  22. var tf = new TextFormatter ();
  23. tf.Text = testText;
  24. expectedSize = new Size (testText.Length, 1);
  25. Assert.Equal (testText, tf.Text);
  26. Assert.Equal (TextAlignment.Left, tf.Alignment);
  27. Assert.Equal (expectedSize, tf.Size);
  28. tf.Draw (testBounds, new Attribute (), new Attribute ());
  29. Assert.Equal (expectedSize, tf.Size);
  30. Assert.NotEmpty (tf.Lines);
  31. tf.Alignment = TextAlignment.Right;
  32. expectedSize = new Size (testText.Length, 1);
  33. Assert.Equal (testText, tf.Text);
  34. Assert.Equal (TextAlignment.Right, tf.Alignment);
  35. Assert.Equal (expectedSize, tf.Size);
  36. tf.Draw (testBounds, new Attribute (), new Attribute ());
  37. Assert.Equal (expectedSize, tf.Size);
  38. Assert.NotEmpty (tf.Lines);
  39. tf.Alignment = TextAlignment.Right;
  40. expectedSize = new Size (testText.Length * 2, 1);
  41. tf.Size = expectedSize;
  42. Assert.Equal (testText, tf.Text);
  43. Assert.Equal (TextAlignment.Right, tf.Alignment);
  44. Assert.Equal (expectedSize, tf.Size);
  45. tf.Draw (testBounds, new Attribute (), new Attribute ());
  46. Assert.Equal (expectedSize, tf.Size);
  47. Assert.NotEmpty (tf.Lines);
  48. tf.Alignment = TextAlignment.Centered;
  49. expectedSize = new Size (testText.Length * 2, 1);
  50. tf.Size = expectedSize;
  51. Assert.Equal (testText, tf.Text);
  52. Assert.Equal (TextAlignment.Centered, tf.Alignment);
  53. Assert.Equal (expectedSize, tf.Size);
  54. tf.Draw (testBounds, new Attribute (), new Attribute ());
  55. Assert.Equal (expectedSize, tf.Size);
  56. Assert.NotEmpty (tf.Lines);
  57. }
  58. [Theory]
  59. [InlineData (TextDirection.LeftRight_TopBottom, false)]
  60. [InlineData (TextDirection.LeftRight_TopBottom, true)]
  61. [InlineData (TextDirection.TopBottom_LeftRight, false)]
  62. [InlineData (TextDirection.TopBottom_LeftRight, true)]
  63. public void TestSize_TextChange (TextDirection textDirection, bool autoSize)
  64. {
  65. var tf = new TextFormatter () { Direction = textDirection, Text = "你", AutoSize = autoSize };
  66. Assert.Equal (2, tf.Size.Width);
  67. Assert.Equal (1, tf.Size.Height);
  68. tf.Text = "你你";
  69. if (autoSize) {
  70. if (textDirection == TextDirection.LeftRight_TopBottom) {
  71. Assert.Equal (4, tf.Size.Width);
  72. Assert.Equal (1, tf.Size.Height);
  73. } else {
  74. Assert.Equal (2, tf.Size.Width);
  75. Assert.Equal (2, tf.Size.Height);
  76. }
  77. } else {
  78. Assert.Equal (2, tf.Size.Width);
  79. Assert.Equal (1, tf.Size.Height);
  80. }
  81. }
  82. [Theory]
  83. [InlineData (TextDirection.LeftRight_TopBottom)]
  84. [InlineData (TextDirection.TopBottom_LeftRight)]
  85. public void TestSize_AutoSizeChange (TextDirection textDirection)
  86. {
  87. var tf = new TextFormatter () { Direction = textDirection, Text = "你你" };
  88. if (textDirection == TextDirection.LeftRight_TopBottom) {
  89. Assert.Equal (4, tf.Size.Width);
  90. Assert.Equal (1, tf.Size.Height);
  91. } else {
  92. Assert.Equal (2, tf.Size.Width);
  93. Assert.Equal (2, tf.Size.Height);
  94. }
  95. Assert.False (tf.AutoSize);
  96. tf.Size = new Size (1, 1);
  97. Assert.Equal (1, tf.Size.Width);
  98. Assert.Equal (1, tf.Size.Height);
  99. tf.AutoSize = true;
  100. if (textDirection == TextDirection.LeftRight_TopBottom) {
  101. Assert.Equal (4, tf.Size.Width);
  102. Assert.Equal (1, tf.Size.Height);
  103. } else {
  104. Assert.Equal (2, tf.Size.Width);
  105. Assert.Equal (2, tf.Size.Height);
  106. }
  107. }
  108. [Theory]
  109. [InlineData (TextDirection.LeftRight_TopBottom, false)]
  110. [InlineData (TextDirection.LeftRight_TopBottom, true)]
  111. [InlineData (TextDirection.TopBottom_LeftRight, false)]
  112. [InlineData (TextDirection.TopBottom_LeftRight, true)]
  113. public void TestSize_SizeChange_AutoSize_True_Or_False (TextDirection textDirection, bool autoSize)
  114. {
  115. var tf = new TextFormatter () { Direction = textDirection, Text = "你你", AutoSize = autoSize };
  116. if (textDirection == TextDirection.LeftRight_TopBottom) {
  117. Assert.Equal (4, tf.Size.Width);
  118. Assert.Equal (1, tf.Size.Height);
  119. } else {
  120. Assert.Equal (2, tf.Size.Width);
  121. Assert.Equal (2, tf.Size.Height);
  122. }
  123. tf.Size = new Size (1, 1);
  124. if (autoSize) {
  125. if (textDirection == TextDirection.LeftRight_TopBottom) {
  126. Assert.Equal (4, tf.Size.Width);
  127. Assert.Equal (1, tf.Size.Height);
  128. } else {
  129. Assert.Equal (2, tf.Size.Width);
  130. Assert.Equal (2, tf.Size.Height);
  131. }
  132. } else {
  133. Assert.Equal (1, tf.Size.Width);
  134. Assert.Equal (1, tf.Size.Height);
  135. }
  136. }
  137. [Theory]
  138. [InlineData (TextAlignment.Left, false)]
  139. [InlineData (TextAlignment.Centered, true)]
  140. [InlineData (TextAlignment.Right, false)]
  141. [InlineData (TextAlignment.Justified, true)]
  142. public void TestSize_SizeChange_AutoSize_True_Or_False_Horizontal (TextAlignment textAlignment, bool autoSize)
  143. {
  144. var tf = new TextFormatter () { Direction = TextDirection.LeftRight_TopBottom, Text = "你你", Alignment = textAlignment, AutoSize = autoSize };
  145. Assert.Equal (4, tf.Size.Width);
  146. Assert.Equal (1, tf.Size.Height);
  147. tf.Size = new Size (1, 1);
  148. if (autoSize && textAlignment != TextAlignment.Justified) {
  149. Assert.Equal (4, tf.Size.Width);
  150. Assert.Equal (1, tf.Size.Height);
  151. } else {
  152. Assert.Equal (1, tf.Size.Width);
  153. Assert.Equal (1, tf.Size.Height);
  154. }
  155. }
  156. [Theory]
  157. [InlineData (VerticalTextAlignment.Top, false)]
  158. [InlineData (VerticalTextAlignment.Middle, true)]
  159. [InlineData (VerticalTextAlignment.Bottom, false)]
  160. [InlineData (VerticalTextAlignment.Justified, true)]
  161. public void TestSize_SizeChange_AutoSize_True_Or_False_Vertical (VerticalTextAlignment textAlignment, bool autoSize)
  162. {
  163. var tf = new TextFormatter () { Direction = TextDirection.TopBottom_LeftRight, Text = "你你", VerticalAlignment = textAlignment, AutoSize = autoSize };
  164. Assert.Equal (2, tf.Size.Width);
  165. Assert.Equal (2, tf.Size.Height);
  166. tf.Size = new Size (1, 1);
  167. if (autoSize && textAlignment != VerticalTextAlignment.Justified) {
  168. Assert.Equal (2, tf.Size.Width);
  169. Assert.Equal (2, tf.Size.Height);
  170. } else {
  171. Assert.Equal (1, tf.Size.Width);
  172. Assert.Equal (1, tf.Size.Height);
  173. }
  174. }
  175. [Fact]
  176. public void NeedsFormat_Sets ()
  177. {
  178. var testText = "test";
  179. var testBounds = new Rect (0, 0, 100, 1);
  180. var tf = new TextFormatter ();
  181. tf.Text = "test";
  182. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  183. Assert.NotEmpty (tf.Lines);
  184. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  185. Assert.Equal (testText, tf.Text);
  186. tf.Draw (testBounds, new Attribute (), new Attribute ());
  187. Assert.False (tf.NeedsFormat);
  188. tf.Size = new Size (1, 1);
  189. Assert.True (tf.NeedsFormat);
  190. Assert.NotEmpty (tf.Lines);
  191. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  192. tf.Alignment = TextAlignment.Centered;
  193. Assert.True (tf.NeedsFormat);
  194. Assert.NotEmpty (tf.Lines);
  195. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  196. }
  197. [Theory]
  198. [InlineData (null)]
  199. [InlineData ("")]
  200. [InlineData ("no hotkey")]
  201. [InlineData ("No hotkey, Upper Case")]
  202. [InlineData ("Non-english: Сохранить")]
  203. public void FindHotKey_Invalid_ReturnsFalse (string text)
  204. {
  205. Rune hotKeySpecifier = (Rune)'_';
  206. bool supportFirstUpperCase = false;
  207. int hotPos = 0;
  208. Key hotKey = Key.Unknown;
  209. bool result = false;
  210. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  211. Assert.False (result);
  212. Assert.Equal (-1, hotPos);
  213. Assert.Equal (Key.Unknown, hotKey);
  214. }
  215. [Theory]
  216. [InlineData ("_K Before", true, 0, (Key)'K')]
  217. [InlineData ("a_K Second", true, 1, (Key)'K')]
  218. [InlineData ("Last _K", true, 5, (Key)'K')]
  219. [InlineData ("After K_", false, -1, Key.Unknown)]
  220. [InlineData ("Multiple _K and _R", true, 9, (Key)'K')]
  221. [InlineData ("Non-english: _Кдать", true, 13, (Key)'К')] // Cryllic K (К)
  222. [InlineData ("_K Before", true, 0, (Key)'K', true)] // Turn on FirstUpperCase and verify same results
  223. [InlineData ("a_K Second", true, 1, (Key)'K', true)]
  224. [InlineData ("Last _K", true, 5, (Key)'K', true)]
  225. [InlineData ("After K_", false, -1, Key.Unknown, true)]
  226. [InlineData ("Multiple _K and _R", true, 9, (Key)'K', true)]
  227. [InlineData ("Non-english: _Кдать", true, 13, (Key)'К', true)] // Cryllic K (К)
  228. public void FindHotKey_AlphaUpperCase_Succeeds (string text, bool expectedResult, int expectedHotPos, Key expectedKey, bool supportFirstUpperCase = false)
  229. {
  230. Rune hotKeySpecifier = (Rune)'_';
  231. var result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out int hotPos, out Key hotKey);
  232. if (expectedResult) {
  233. Assert.True (result);
  234. } else {
  235. Assert.False (result);
  236. }
  237. Assert.Equal (expectedResult, result);
  238. Assert.Equal (expectedHotPos, hotPos);
  239. Assert.Equal (expectedKey, hotKey);
  240. }
  241. [Theory]
  242. [InlineData ("_k Before", true, 0, (Key)'K')] // lower case should return uppercase Hotkey
  243. [InlineData ("a_k Second", true, 1, (Key)'K')]
  244. [InlineData ("Last _k", true, 5, (Key)'K')]
  245. [InlineData ("After k_", false, -1, Key.Unknown)]
  246. [InlineData ("Multiple _k and _R", true, 9, (Key)'K')]
  247. [InlineData ("Non-english: _кдать", true, 13, (Key)'К')] // Lower case Cryllic K (к)
  248. [InlineData ("_k Before", true, 0, (Key)'K', true)] // Turn on FirstUpperCase and verify same results
  249. [InlineData ("a_k Second", true, 1, (Key)'K', true)]
  250. [InlineData ("Last _k", true, 5, (Key)'K', true)]
  251. [InlineData ("After k_", false, -1, Key.Unknown, true)]
  252. [InlineData ("Multiple _k and _r", true, 9, (Key)'K', true)]
  253. [InlineData ("Non-english: _кдать", true, 13, (Key)'К', true)] // Cryllic K (К)
  254. public void FindHotKey_AlphaLowerCase_Succeeds (string text, bool expectedResult, int expectedHotPos, Key expectedKey, bool supportFirstUpperCase = false)
  255. {
  256. Rune hotKeySpecifier = (Rune)'_';
  257. var result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out int hotPos, out Key hotKey);
  258. if (expectedResult) {
  259. Assert.True (result);
  260. } else {
  261. Assert.False (result);
  262. }
  263. Assert.Equal (expectedResult, result);
  264. Assert.Equal (expectedHotPos, hotPos);
  265. Assert.Equal (expectedKey, hotKey);
  266. }
  267. [Theory]
  268. [InlineData ("_1 Before", true, 0, (Key)'1')] // Digits
  269. [InlineData ("a_1 Second", true, 1, (Key)'1')]
  270. [InlineData ("Last _1", true, 5, (Key)'1')]
  271. [InlineData ("After 1_", false, -1, Key.Unknown)]
  272. [InlineData ("Multiple _1 and _2", true, 9, (Key)'1')]
  273. [InlineData ("_1 Before", true, 0, (Key)'1', true)] // Turn on FirstUpperCase and verify same results
  274. [InlineData ("a_1 Second", true, 1, (Key)'1', true)]
  275. [InlineData ("Last _1", true, 5, (Key)'1', true)]
  276. [InlineData ("After 1_", false, -1, Key.Unknown, true)]
  277. [InlineData ("Multiple _1 and _2", true, 9, (Key)'1', true)]
  278. public void FindHotKey_Numeric_Succeeds (string text, bool expectedResult, int expectedHotPos, Key expectedKey, bool supportFirstUpperCase = false)
  279. {
  280. Rune hotKeySpecifier = (Rune)'_';
  281. var result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out int hotPos, out Key hotKey);
  282. if (expectedResult) {
  283. Assert.True (result);
  284. } else {
  285. Assert.False (result);
  286. }
  287. Assert.Equal (expectedResult, result);
  288. Assert.Equal (expectedHotPos, hotPos);
  289. Assert.Equal (expectedKey, hotKey);
  290. }
  291. [Theory]
  292. [InlineData ("K Before", true, 0, (Key)'K')]
  293. [InlineData ("aK Second", true, 1, (Key)'K')]
  294. [InlineData ("last K", true, 5, (Key)'K')]
  295. [InlineData ("multiple K and R", true, 9, (Key)'K')]
  296. [InlineData ("non-english: Кдать", true, 13, (Key)'К')] // Cryllic K (К)
  297. public void FindHotKey_Legacy_FirstUpperCase_Succeeds (string text, bool expectedResult, int expectedHotPos, Key expectedKey)
  298. {
  299. var supportFirstUpperCase = true;
  300. Rune hotKeySpecifier = (Rune)0;
  301. var result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out int hotPos, out Key hotKey);
  302. if (expectedResult) {
  303. Assert.True (result);
  304. } else {
  305. Assert.False (result);
  306. }
  307. Assert.Equal (expectedResult, result);
  308. Assert.Equal (expectedHotPos, hotPos);
  309. Assert.Equal (expectedKey, hotKey);
  310. }
  311. [Theory]
  312. [InlineData ("\"k before")]
  313. [InlineData ("ak second")]
  314. [InlineData ("last k")]
  315. [InlineData ("multiple k and r")]
  316. [InlineData ("12345")]
  317. [InlineData ("`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?")] // punctuation
  318. [InlineData (" ~  s  gui.cs   master ↑10")] // ~IsLetterOrDigit + Unicode
  319. [InlineData ("non-english: кдать")] // Lower case Cryllic K (к)
  320. public void FindHotKey_Legacy_FirstUpperCase_NotFound_Returns_False (string text)
  321. {
  322. bool supportFirstUpperCase = true;
  323. var hotKeySpecifier = (Rune)0;
  324. var result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out int hotPos, out Key hotKey);
  325. Assert.False (result);
  326. Assert.Equal (-1, hotPos);
  327. Assert.Equal (Key.Unknown, hotKey);
  328. }
  329. [Theory]
  330. [InlineData (null)]
  331. [InlineData ("")]
  332. [InlineData ("a")]
  333. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal (string text)
  334. {
  335. Rune hotKeySpecifier = (Rune)'_';
  336. if (text == null) {
  337. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  338. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  339. Assert.Null (TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  340. } else {
  341. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 0, hotKeySpecifier));
  342. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, -1, hotKeySpecifier));
  343. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, 100, hotKeySpecifier));
  344. }
  345. }
  346. [Theory]
  347. [InlineData ("_K Before", 0, "K Before")]
  348. [InlineData ("a_K Second", 1, "aK Second")]
  349. [InlineData ("Last _K", 5, "Last K")]
  350. [InlineData ("After K_", 7, "After K")]
  351. [InlineData ("Multiple _K and _R", 9, "Multiple K and _R")]
  352. [InlineData ("Non-english: _Кдать", 13, "Non-english: Кдать")]
  353. public void RemoveHotKeySpecifier_Valid_ReturnsStripped (string text, int hotPos, string expectedText)
  354. {
  355. Rune hotKeySpecifier = (Rune)'_';
  356. Assert.Equal (expectedText, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  357. }
  358. [Theory]
  359. [InlineData ("all lower case", 0)]
  360. [InlineData ("K Before", 0)]
  361. [InlineData ("aK Second", 1)]
  362. [InlineData ("Last K", 5)]
  363. [InlineData ("fter K", 7)]
  364. [InlineData ("Multiple K and R", 9)]
  365. [InlineData ("Non-english: Кдать", 13)]
  366. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal (string text, int hotPos)
  367. {
  368. Rune hotKeySpecifier = (Rune)'_';
  369. Assert.Equal (text, TextFormatter.RemoveHotKeySpecifier (text, hotPos, hotKeySpecifier));
  370. }
  371. [Theory]
  372. [InlineData (null)]
  373. [InlineData ("")]
  374. public void CalcRect_Invalid_Returns_Empty (string text)
  375. {
  376. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, text));
  377. Assert.Equal (new Rect (new Point (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, text));
  378. Assert.Equal (new Rect (new Point (-1, -2), Size.Empty), TextFormatter.CalcRect (-1, -2, text));
  379. }
  380. [Theory]
  381. [InlineData ("test")]
  382. [InlineData (" ~  s  gui.cs   master ↑10")]
  383. public void CalcRect_SingleLine_Returns_1High (string text)
  384. {
  385. Assert.Equal (new Rect (0, 0, text.GetRuneCount (), 1), TextFormatter.CalcRect (0, 0, text));
  386. Assert.Equal (new Rect (0, 0, text.GetColumns (), 1), TextFormatter.CalcRect (0, 0, text));
  387. }
  388. [Theory]
  389. [InlineData ("line1\nline2", 5, 2)]
  390. [InlineData ("\nline2", 5, 2)]
  391. [InlineData ("\n\n", 0, 3)]
  392. [InlineData ("\n\n\n", 0, 4)]
  393. [InlineData ("line1\nline2\nline3long!", 10, 3)]
  394. [InlineData ("line1\nline2\n\n", 5, 4)]
  395. [InlineData ("line1\r\nline2", 5, 2)]
  396. [InlineData (" ~  s  gui.cs   master ↑10\n", 31, 2)]
  397. [InlineData ("\n ~  s  gui.cs   master ↑10", 31, 2)]
  398. [InlineData (" ~  s  gui.cs   master\n↑10", 27, 2)]
  399. public void CalcRect_MultiLine_Returns_nHigh (string text, int expectedWidth, int expectedLines)
  400. {
  401. Assert.Equal (new Rect (0, 0, expectedWidth, expectedLines), TextFormatter.CalcRect (0, 0, text));
  402. var lines = text.Split (text.Contains (Environment.NewLine) ? Environment.NewLine : "\n");
  403. var maxWidth = lines.Max (s => s.GetColumns ());
  404. var lineWider = 0;
  405. for (int i = 0; i < lines.Length; i++) {
  406. var w = lines [i].GetColumns ();
  407. if (w == maxWidth) {
  408. lineWider = i;
  409. }
  410. }
  411. Assert.Equal (new Rect (0, 0, maxWidth, expectedLines), TextFormatter.CalcRect (0, 0, text));
  412. Assert.Equal (new Rect (0, 0, lines [lineWider].ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 0)), expectedLines), TextFormatter.CalcRect (0, 0, text));
  413. }
  414. [Theory]
  415. [InlineData ("")]
  416. [InlineData (null)]
  417. [InlineData ("test")]
  418. public void ClipAndJustify_Invalid_Returns_Original (string text)
  419. {
  420. var expected = string.IsNullOrEmpty (text) ? text : "";
  421. Assert.Equal (expected, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  422. Assert.Equal (expected, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  423. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.ClipAndJustify (text, -1, TextAlignment.Left));
  424. }
  425. [Theory]
  426. [InlineData ("test", "", 0)]
  427. [InlineData ("test", "te", 2)]
  428. [InlineData ("test", "test", int.MaxValue)]
  429. [InlineData ("A sentence has words.", "A sentence has words.", 22)] // should fit
  430. [InlineData ("A sentence has words.", "A sentence has words.", 21)] // should fit
  431. [InlineData ("A sentence has words.", "A sentence has words.", int.MaxValue)] // should fit
  432. [InlineData ("A sentence has words.", "A sentence has words", 20)] // Should not fit
  433. [InlineData ("A sentence has words.", "A sentence", 10)] // Should not fit
  434. [InlineData ("A\tsentence\thas\twords.", "A sentence has words.", int.MaxValue)]
  435. [InlineData ("A\tsentence\thas\twords.", "A sentence", 10)]
  436. [InlineData ("line1\nline2\nline3long!", "line1\nline2\nline3long!", int.MaxValue)]
  437. [InlineData ("line1\nline2\nline3long!", "line1\nline", 10)]
  438. [InlineData (" ~  s  gui.cs   master ↑10", " ~  s  ", 10)] // Unicode
  439. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 5)] // should fit
  440. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 4)] // should fit
  441. [InlineData ("Ð ÑÐ", "Ð Ñ", 3)] // Should not fit
  442. public void ClipAndJustify_Valid_Left (string text, string justifiedText, int maxWidth)
  443. {
  444. var align = TextAlignment.Left;
  445. var textDirection = TextDirection.LeftRight_BottomTop;
  446. var tabWidth = 1;
  447. Assert.Equal (justifiedText, TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth));
  448. var expectedClippedWidth = Math.Min (justifiedText.GetRuneCount (), maxWidth);
  449. Assert.Equal (justifiedText, TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth));
  450. Assert.True (justifiedText.GetRuneCount () <= maxWidth);
  451. Assert.True (justifiedText.GetColumns () <= maxWidth);
  452. Assert.Equal (expectedClippedWidth, justifiedText.GetRuneCount ());
  453. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 1)));
  454. Assert.True (expectedClippedWidth <= maxWidth);
  455. Assert.Equal (StringExtensions.ToString (justifiedText.ToRunes () [0..expectedClippedWidth]), justifiedText);
  456. }
  457. [Theory]
  458. [InlineData ("test", "", 0)]
  459. [InlineData ("test", "te", 2)]
  460. [InlineData ("test", "test", int.MaxValue)]
  461. [InlineData ("A sentence has words.", "A sentence has words.", 22)] // should fit
  462. [InlineData ("A sentence has words.", "A sentence has words.", 21)] // should fit
  463. [InlineData ("A sentence has words.", "A sentence has words.", int.MaxValue)] // should fit
  464. [InlineData ("A sentence has words.", "A sentence has words", 20)] // Should not fit
  465. [InlineData ("A sentence has words.", "A sentence", 10)] // Should not fit
  466. [InlineData ("A\tsentence\thas\twords.", "A sentence has words.", int.MaxValue)]
  467. [InlineData ("A\tsentence\thas\twords.", "A sentence", 10)]
  468. [InlineData ("line1\nline2\nline3long!", "line1\nline2\nline3long!", int.MaxValue)]
  469. [InlineData ("line1\nline2\nline3long!", "line1\nline", 10)]
  470. [InlineData (" ~  s  gui.cs   master ↑10", " ~  s  ", 10)] // Unicode
  471. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 5)] // should fit
  472. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 4)] // should fit
  473. [InlineData ("Ð ÑÐ", "Ð Ñ", 3)] // Should not fit
  474. public void ClipAndJustify_Valid_Right (string text, string justifiedText, int maxWidth)
  475. {
  476. var align = TextAlignment.Right;
  477. var textDirection = TextDirection.LeftRight_BottomTop;
  478. var tabWidth = 1;
  479. Assert.Equal (justifiedText, TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth));
  480. var expectedClippedWidth = Math.Min (justifiedText.GetRuneCount (), maxWidth);
  481. Assert.Equal (justifiedText, TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth));
  482. Assert.True (justifiedText.GetRuneCount () <= maxWidth);
  483. Assert.True (justifiedText.GetColumns () <= maxWidth);
  484. Assert.Equal (expectedClippedWidth, justifiedText.GetRuneCount ());
  485. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 1)));
  486. Assert.True (expectedClippedWidth <= maxWidth);
  487. Assert.Equal (StringExtensions.ToString (justifiedText.ToRunes () [0..expectedClippedWidth]), justifiedText);
  488. }
  489. [Theory]
  490. [InlineData ("test", "", 0)]
  491. [InlineData ("test", "te", 2)]
  492. [InlineData ("test", "test", int.MaxValue)]
  493. [InlineData ("A sentence has words.", "A sentence has words.", 22)] // should fit
  494. [InlineData ("A sentence has words.", "A sentence has words.", 21)] // should fit
  495. [InlineData ("A sentence has words.", "A sentence has words.", int.MaxValue)] // should fit
  496. [InlineData ("A sentence has words.", "A sentence has words", 20)] // Should not fit
  497. [InlineData ("A sentence has words.", "A sentence", 10)] // Should not fit
  498. [InlineData ("A\tsentence\thas\twords.", "A sentence has words.", int.MaxValue)]
  499. [InlineData ("A\tsentence\thas\twords.", "A sentence", 10)]
  500. [InlineData ("line1\nline2\nline3long!", "line1\nline2\nline3long!", int.MaxValue)]
  501. [InlineData ("line1\nline2\nline3long!", "line1\nline", 10)]
  502. [InlineData (" ~  s  gui.cs   master ↑10", " ~  s  ", 10)] // Unicode
  503. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 5)] // should fit
  504. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 4)] // should fit
  505. [InlineData ("Ð ÑÐ", "Ð Ñ", 3)] // Should not fit
  506. public void ClipAndJustify_Valid_Centered (string text, string justifiedText, int maxWidth)
  507. {
  508. var align = TextAlignment.Centered;
  509. var textDirection = TextDirection.LeftRight_TopBottom;
  510. var tabWidth = 1;
  511. Assert.Equal (justifiedText, TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth));
  512. var expectedClippedWidth = Math.Min (justifiedText.GetRuneCount (), maxWidth);
  513. Assert.Equal (justifiedText, TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth));
  514. Assert.True (justifiedText.GetRuneCount () <= maxWidth);
  515. Assert.True (justifiedText.GetColumns () <= maxWidth);
  516. Assert.Equal (expectedClippedWidth, justifiedText.GetRuneCount ());
  517. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 1)));
  518. Assert.True (expectedClippedWidth <= maxWidth);
  519. Assert.Equal (StringExtensions.ToString (justifiedText.ToRunes () [0..expectedClippedWidth]), justifiedText);
  520. }
  521. [Theory]
  522. [InlineData ("test", "", 0)]
  523. [InlineData ("test", "te", 2)]
  524. [InlineData ("test", "test", int.MaxValue)] // This doesn't throw because it only create a word with length 1
  525. [InlineData ("A sentence has words.", "A sentence has words.", 22)] // should fit
  526. [InlineData ("A sentence has words.", "A sentence has words.", 21)] // should fit
  527. [InlineData ("A sentence has words.", "A sentence has words.", 500)] // should fit
  528. [InlineData ("A sentence has words.", "A sentence has words", 20)] // Should not fit
  529. [InlineData ("A sentence has words.", "A sentence", 10)] // Should not fit
  530. // Now throw System.OutOfMemoryException. See https://stackoverflow.com/questions/20672920/maxcapacity-of-stringbuilder
  531. //[InlineData ("A\tsentence\thas\twords.", "A sentence has words.", int.MaxValue)]
  532. [InlineData ("A\tsentence\thas\twords.", "A sentence", 10)]
  533. [InlineData ("line1\nline2\nline3long!", "line1\nline2\nline3long!", int.MaxValue)] // This doesn't throw because it only create a line with length 1
  534. [InlineData ("line1\nline2\nline3long!", "line1\nline", 10)]
  535. [InlineData (" ~  s  gui.cs   master ↑10", " ~  s  ", 10)] // Unicode
  536. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 5)] // should fit
  537. [InlineData ("Ð ÑÐ", "Ð ÑÐ", 4)] // should fit
  538. [InlineData ("Ð ÑÐ", "Ð Ñ", 3)] // Should not fit
  539. public void ClipAndJustify_Valid_Justified (string text, string justifiedText, int maxWidth)
  540. {
  541. var align = TextAlignment.Justified;
  542. var textDirection = TextDirection.LeftRight_TopBottom;
  543. var tabWidth = 1;
  544. Assert.Equal (justifiedText, TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth));
  545. var expectedClippedWidth = Math.Min (justifiedText.GetRuneCount (), maxWidth);
  546. Assert.Equal (justifiedText, TextFormatter.ClipAndJustify (text, maxWidth, align, textDirection, tabWidth));
  547. Assert.True (justifiedText.GetRuneCount () <= maxWidth);
  548. Assert.True (justifiedText.GetColumns () <= maxWidth);
  549. Assert.Equal (expectedClippedWidth, justifiedText.GetRuneCount ());
  550. Assert.Equal (expectedClippedWidth, justifiedText.ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 1)));
  551. Assert.True (expectedClippedWidth <= maxWidth);
  552. Assert.Equal (StringExtensions.ToString (justifiedText.ToRunes () [0..expectedClippedWidth]), justifiedText);
  553. // see Justify_ tests below
  554. }
  555. [Theory]
  556. [InlineData ("")]
  557. [InlineData (null)]
  558. [InlineData ("test")]
  559. public void Justify_Invalid (string text)
  560. {
  561. Assert.Equal (text, TextFormatter.Justify (text, 0));
  562. Assert.Equal (text, TextFormatter.Justify (text, 0));
  563. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  564. }
  565. [Theory]
  566. [InlineData ("word")] // Even # of chars
  567. [InlineData ("word.")] // Odd # of chars
  568. [InlineData ("пÑивеÑ")] // Unicode (even #)
  569. [InlineData ("пÑивеÑ.")] // Unicode (odd # of chars)
  570. public void Justify_SingleWord (string text)
  571. {
  572. var justifiedText = text;
  573. char fillChar = '+';
  574. int width = text.GetRuneCount ();
  575. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  576. width = text.GetRuneCount () + 1;
  577. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  578. width = text.GetRuneCount () + 2;
  579. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  580. width = text.GetRuneCount () + 10;
  581. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  582. width = text.GetRuneCount () + 11;
  583. Assert.Equal (justifiedText, TextFormatter.Justify (text, width, fillChar));
  584. }
  585. [Theory]
  586. // Even # of spaces
  587. // 0123456789
  588. [InlineData ("012 456 89", "012 456 89", 10, 0, "+", true)]
  589. [InlineData ("012 456 89", "012++456+89", 11, 1)]
  590. [InlineData ("012 456 89", "012 456 89", 12, 2, "++", true)]
  591. [InlineData ("012 456 89", "012+++456++89", 13, 3)]
  592. [InlineData ("012 456 89", "012 456 89", 14, 4, "+++", true)]
  593. [InlineData ("012 456 89", "012++++456+++89", 15, 5)]
  594. [InlineData ("012 456 89", "012 456 89", 16, 6, "++++", true)]
  595. [InlineData ("012 456 89", "012 456 89", 30, 20, "+++++++++++", true)]
  596. [InlineData ("012 456 89", "012+++++++++++++456++++++++++++89", 33, 23)]
  597. // Odd # of spaces
  598. // 01234567890123
  599. [InlineData ("012 456 89 end", "012 456 89 end", 14, 0, "+", true)]
  600. [InlineData ("012 456 89 end", "012++456+89+end", 15, 1)]
  601. [InlineData ("012 456 89 end", "012++456++89+end", 16, 2)]
  602. [InlineData ("012 456 89 end", "012 456 89 end", 17, 3, "++", true)]
  603. [InlineData ("012 456 89 end", "012+++456++89++end", 18, 4)]
  604. [InlineData ("012 456 89 end", "012+++456+++89++end", 19, 5)]
  605. [InlineData ("012 456 89 end", "012 456 89 end", 20, 6, "+++", true)]
  606. [InlineData ("012 456 89 end", "012++++++++456++++++++89+++++++end", 34, 20)]
  607. [InlineData ("012 456 89 end", "012+++++++++456+++++++++89++++++++end", 37, 23)]
  608. // Unicode
  609. // Even # of chars
  610. // 0123456789
  611. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 10, 0, "+", true)]
  612. [InlineData ("пÑРвРÑ", "пÑÐ++вÐ+Ñ", 11, 1)]
  613. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 12, 2, "++", true)]
  614. [InlineData ("пÑРвРÑ", "пÑÐ+++вÐ++Ñ", 13, 3)]
  615. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 14, 4, "+++", true)]
  616. [InlineData ("пÑРвРÑ", "пÑÐ++++вÐ+++Ñ", 15, 5)]
  617. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 16, 6, "++++", true)]
  618. [InlineData ("пÑРвРÑ", "пÑРвРÑ", 30, 20, "+++++++++++", true)]
  619. [InlineData ("пÑРвРÑ", "пÑÐ+++++++++++++вÐ++++++++++++Ñ", 33, 23)]
  620. // Unicode
  621. // Odd # of chars
  622. // 0123456789
  623. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 10, 0, "+", true)]
  624. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ+вÐ+Ñ", 11, 1)]
  625. [InlineData ("Ð ÑРвРÑ", "Ð++ÑÐ++вÐ+Ñ", 12, 2)]
  626. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 13, 3, "++", true)]
  627. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ++вÐ++Ñ", 14, 4)]
  628. [InlineData ("Ð ÑРвРÑ", "Ð+++ÑÐ+++вÐ++Ñ", 15, 5)]
  629. [InlineData ("Ð ÑРвРÑ", "Ð ÑРвРÑ", 16, 6, "+++", true)]
  630. [InlineData ("Ð ÑРвРÑ", "Ð++++++++ÑÐ++++++++вÐ+++++++Ñ", 30, 20)]
  631. [InlineData ("Ð ÑРвРÑ", "Ð+++++++++ÑÐ+++++++++вÐ++++++++Ñ", 33, 23)]
  632. public void Justify_Sentence (string text, string justifiedText, int forceToWidth, int widthOffset, string replaceWith = null, bool replace = false)
  633. {
  634. char fillChar = '+';
  635. Assert.Equal (forceToWidth, text.GetRuneCount () + widthOffset);
  636. if (replace) {
  637. justifiedText = text.Replace (" ", replaceWith);
  638. }
  639. Assert.Equal (justifiedText, TextFormatter.Justify (text, forceToWidth, fillChar));
  640. Assert.True (Math.Abs (forceToWidth - justifiedText.GetRuneCount ()) < text.Count (s => s == ' '));
  641. Assert.True (Math.Abs (forceToWidth - justifiedText.GetColumns ()) < text.Count (s => s == ' '));
  642. }
  643. [Fact]
  644. public void WordWrap_Invalid ()
  645. {
  646. var text = string.Empty;
  647. int width = 0;
  648. Assert.Empty (TextFormatter.WordWrapText (null, width));
  649. Assert.Empty (TextFormatter.WordWrapText (text, width));
  650. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrapText (text, -1));
  651. }
  652. [Fact]
  653. public void WordWrap_BigWidth ()
  654. {
  655. List<string> wrappedLines;
  656. var text = "Constantinople";
  657. wrappedLines = TextFormatter.WordWrapText (text, 100);
  658. Assert.True (wrappedLines.Count == 1);
  659. Assert.Equal ("Constantinople", wrappedLines [0]);
  660. }
  661. [Theory]
  662. [InlineData ("Constantinople", 14, 0, new string [] { "Constantinople" })]
  663. [InlineData ("Constantinople", 12, -2, new string [] { "Constantinop", "le" })]
  664. [InlineData ("Constantinople", 9, -5, new string [] { "Constanti", "nople" })]
  665. [InlineData ("Constantinople", 7, -7, new string [] { "Constan", "tinople" })]
  666. [InlineData ("Constantinople", 5, -9, new string [] { "Const", "antin", "ople" })]
  667. [InlineData ("Constantinople", 4, -10, new string [] { "Cons", "tant", "inop", "le" })]
  668. [InlineData ("Constantinople", 1, -13, new string [] { "C", "o", "n", "s", "t", "a", "n", "t", "i", "n", "o", "p", "l", "e" })]
  669. public void WordWrap_SingleWordLine (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  670. {
  671. List<string> wrappedLines;
  672. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  673. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  674. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  675. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  676. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  677. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  678. Assert.Equal (resultLines, wrappedLines);
  679. }
  680. [Theory]
  681. [InlineData ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", 51, 0, new string [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" })]
  682. [InlineData ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", 50, -1, new string [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" })]
  683. [InlineData ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", 46, -5, new string [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮ", "ฯะัาำ" })]
  684. [InlineData ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", 26, -25, new string [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ" })]
  685. [InlineData ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", 17, -34, new string [] { "กขฃคฅฆงจฉชซฌญฎฏฐฑ", "ฒณดตถทธนบปผฝพฟภมย", "รฤลฦวศษสหฬอฮฯะัาำ" })]
  686. [InlineData ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", 13, -38, new string [] { "กขฃคฅฆงจฉชซฌญ", "ฎฏฐฑฒณดตถทธนบ", "ปผฝพฟภมยรฤลฦว", "ศษสหฬอฮฯะัาำ" })]
  687. [InlineData ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", 1, -50, new string [] { "ก", "ข", "ฃ", "ค", "ฅ", "ฆ", "ง", "จ", "ฉ", "ช", "ซ", "ฌ", "ญ", "ฎ", "ฏ", "ฐ", "ฑ", "ฒ", "ณ", "ด", "ต", "ถ", "ท", "ธ", "น", "บ", "ป", "ผ", "ฝ", "พ", "ฟ", "ภ", "ม", "ย", "ร", "ฤ", "ล", "ฦ", "ว", "ศ", "ษ", "ส", "ห", "ฬ", "อ", "ฮ", "ฯ", "ะั", "า", "ำ" })]
  688. public void WordWrap_Unicode_SingleWordLine (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  689. {
  690. List<string> wrappedLines;
  691. var zeroWidth = text.EnumerateRunes ().Where (r => r.GetColumns () == 0);
  692. Assert.Single (zeroWidth);
  693. Assert.Equal ('ั', zeroWidth.ElementAt (0).Value);
  694. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  695. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  696. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  697. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  698. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount () + zeroWidth.Count () - 1 + widthOffset) : 0));
  699. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  700. Assert.Equal (resultLines, wrappedLines);
  701. }
  702. [Theory]
  703. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 19, 0, new string [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  704. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 18, -1, new string [] { "This\u00A0is\u00A0a\u00A0sentence", "." })]
  705. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 17, -2, new string [] { "This\u00A0is\u00A0a\u00A0sentenc", "e." })]
  706. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 14, -5, new string [] { "This\u00A0is\u00A0a\u00A0sent", "ence." })]
  707. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 10, -9, new string [] { "This\u00A0is\u00A0a\u00A0", "sentence." })]
  708. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 7, -12, new string [] { "This\u00A0is", "\u00A0a\u00A0sent", "ence." })]
  709. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 5, -14, new string [] { "This\u00A0", "is\u00A0a\u00A0", "sente", "nce." })]
  710. [InlineData ("This\u00A0is\u00A0a\u00A0sentence.", 1, -18, new string [] { "T", "h", "i", "s", "\u00A0", "i", "s", "\u00A0", "a", "\u00A0", "s", "e", "n", "t", "e", "n", "c", "e", "." })]
  711. public void WordWrap_Unicode_LineWithNonBreakingSpace (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  712. {
  713. List<string> wrappedLines;
  714. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  715. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  716. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  717. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  718. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  719. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  720. Assert.Equal (resultLines, wrappedLines);
  721. }
  722. [Theory]
  723. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 20, 0, new string [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  724. [InlineData ("This\u00A0is\n\u00A0a\u00A0sentence.", 19, -1, new string [] { "This\u00A0is\u00A0a\u00A0sentence." })]
  725. [InlineData ("\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.", 19, 0, new string [] { "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence." })]
  726. public void WordWrap_Unicode_2LinesWithNonBreakingSpace (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  727. {
  728. List<string> wrappedLines;
  729. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  730. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  731. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  732. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  733. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  734. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  735. Assert.Equal (resultLines, wrappedLines);
  736. }
  737. [Theory]
  738. [InlineData ("A sentence has words.", 21, 0, new string [] { "A sentence has words." })]
  739. [InlineData ("A sentence has words.", 20, -1, new string [] { "A sentence has", "words." })]
  740. [InlineData ("A sentence has words.", 15, -6, new string [] { "A sentence has", "words." })]
  741. [InlineData ("A sentence has words.", 14, -7, new string [] { "A sentence has", "words." })]
  742. [InlineData ("A sentence has words.", 13, -8, new string [] { "A sentence", "has words." })]
  743. // Unicode
  744. [InlineData ("A Unicode sentence (пÑивеÑ) has words.", 42, 0, new string [] { "A Unicode sentence (пÑивеÑ) has words." })]
  745. [InlineData ("A Unicode sentence (пÑивеÑ) has words.", 41, -1, new string [] { "A Unicode sentence (пÑивеÑ) has", "words." })]
  746. [InlineData ("A Unicode sentence (пÑивеÑ) has words.", 36, -6, new string [] { "A Unicode sentence (пÑивеÑ) has", "words." })]
  747. [InlineData ("A Unicode sentence (пÑивеÑ) has words.", 35, -7, new string [] { "A Unicode sentence (пÑивеÑ) has", "words." })]
  748. [InlineData ("A Unicode sentence (пÑивеÑ) has words.", 34, -8, new string [] { "A Unicode sentence (пÑивеÑ)", "has words." })]
  749. [InlineData ("A Unicode sentence (пÑивеÑ) has words.", 25, -17, new string [] { "A Unicode sentence", "(пÑивеÑ) has words." })]
  750. public void WordWrap_NoNewLines_Default (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  751. {
  752. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  753. List<string> wrappedLines;
  754. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  755. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  756. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  757. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  758. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  759. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  760. Assert.Equal (resultLines, wrappedLines);
  761. }
  762. /// <summary>
  763. /// WordWrap strips CRLF
  764. /// </summary>
  765. [Theory]
  766. [InlineData ("A sentence has words.\nA paragraph has lines.", 44, 0, new string [] { "A sentence has words.A paragraph has lines." })]
  767. [InlineData ("A sentence has words.\nA paragraph has lines.", 43, -1, new string [] { "A sentence has words.A paragraph has lines." })]
  768. [InlineData ("A sentence has words.\nA paragraph has lines.", 38, -6, new string [] { "A sentence has words.A paragraph has", "lines." })]
  769. [InlineData ("A sentence has words.\nA paragraph has lines.", 34, -10, new string [] { "A sentence has words.A paragraph", "has lines." })]
  770. [InlineData ("A sentence has words.\nA paragraph has lines.", 27, -17, new string [] { "A sentence has words.A", "paragraph has lines." })]
  771. // Unicode
  772. [InlineData ("A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.", 69, 0, new string [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." })]
  773. [InlineData ("A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.", 68, -1, new string [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии." })]
  774. [InlineData ("A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.", 63, -6, new string [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", "Линии." })]
  775. [InlineData ("A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.", 59, -10, new string [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", "has Линии." })]
  776. [InlineData ("A Unicode sentence (пÑивеÑ) has words.\nA Unicode Пункт has Линии.", 52, -17, new string [] { "A Unicode sentence (пÑивеÑ) has words.A Unicode", "Пункт has Линии." })]
  777. public void WordWrap_WithNewLines (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  778. {
  779. List<string> wrappedLines;
  780. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  781. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  782. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  783. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  784. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  785. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  786. Assert.Equal (resultLines, wrappedLines);
  787. }
  788. [Theory]
  789. [InlineData ("A sentence has words.", 3, -18, new string [] { "A", "sen", "ten", "ce", "has", "wor", "ds." })]
  790. [InlineData ("A sentence has words.", 2, -19, new string [] { "A", "se", "nt", "en", "ce", "ha", "s", "wo", "rd", "s." })]
  791. [InlineData ("A sentence has words.", 1, -20, new string [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." })]
  792. public void WordWrap_Narrow_Default (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  793. {
  794. // Calls WordWrapText (text, width) and thus preserveTrailingSpaces defaults to false
  795. List<string> wrappedLines;
  796. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  797. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  798. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  799. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  800. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  801. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  802. Assert.Equal (resultLines, wrappedLines);
  803. }
  804. [Theory]
  805. [InlineData ("A sentence has words.", 14, -7, new string [] { "A sentence ", "has words." })]
  806. [InlineData ("A sentence has words.", 8, -13, new string [] { "A ", "sentence", " has ", "words." })]
  807. [InlineData ("A sentence has words.", 6, -15, new string [] { "A ", "senten", "ce ", "has ", "words." })]
  808. [InlineData ("A sentence has words.", 3, -18, new string [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds." })]
  809. [InlineData ("A sentence has words.", 2, -19, new string [] { "A ", "se", "nt", "en", "ce", " ", "ha", "s ", "wo", "rd", "s." })]
  810. [InlineData ("A sentence has words.", 1, -20, new string [] { "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", " ", "w", "o", "r", "d", "s", "." })]
  811. public void WordWrap_PreserveTrailingSpaces_True (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  812. {
  813. List<string> wrappedLines;
  814. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  815. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  816. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, preserveTrailingSpaces: true);
  817. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  818. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  819. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  820. Assert.Equal (resultLines, wrappedLines);
  821. }
  822. [Theory]
  823. [InlineData ("文に は言葉 があり ます。", 14, 0, new string [] { "文に は言葉 ", "があり ます。" })]
  824. [InlineData ("文に は言葉 があり ます。", 3, -11, new string [] { "文", "に ", "は", "言", "葉 ", "が", "あ", "り ", "ま", "す", "。" })]
  825. [InlineData ("文に は言葉 があり ます。", 2, -12, new string [] { "文", "に", " ", "は", "言", "葉", " ", "が", "あ", "り", " ", "ま", "す", "。" })]
  826. [InlineData ("文に は言葉 があり ます。", 1, -13, new string [] { })]
  827. public void WordWrap_PreserveTrailingSpaces_True_Wide_Runes (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  828. {
  829. List<string> wrappedLines;
  830. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  831. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  832. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, preserveTrailingSpaces: true);
  833. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  834. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  835. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  836. Assert.Equal (resultLines, wrappedLines);
  837. }
  838. [Theory]
  839. [InlineData ("文に は言葉 があり ます。", 14, 0, new string [] { "文に は言葉", "があり ます。" })]
  840. [InlineData ("文に は言葉 があり ます。", 3, -11, new string [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  841. [InlineData ("文に は言葉 があり ます。", 2, -12, new string [] { "文", "に", "は", "言", "葉", "が", "あ", "り", "ま", "す", "。" })]
  842. [InlineData ("文に は言葉 があり ます。", 1, -13, new string [] { " ", " ", " " })] // Just Spaces; should result in a single space for each line
  843. public void WordWrap_PreserveTrailingSpaces_False_Wide_Runes (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  844. {
  845. List<string> wrappedLines;
  846. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  847. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  848. wrappedLines = TextFormatter.WordWrapText (text, maxWidth);
  849. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  850. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  851. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  852. Assert.Equal (resultLines, wrappedLines);
  853. }
  854. [Theory]
  855. [InlineData ("A sentence has words. ", 3, new string [] { "A ", "sen", "ten", "ce ", "has", " ", "wor", "ds.", " " })]
  856. [InlineData ("A sentence has words. ", 3, new string [] { "A ", " ", "sen", "ten", "ce ", " ", " ", " ", "has", " ", "wor", "ds.", " " })]
  857. public void WordWrap_PreserveTrailingSpaces_True_With_Simple_Runes_Width_3 (string text, int width, IEnumerable<string> resultLines)
  858. {
  859. var wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: true);
  860. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  861. Assert.Equal (resultLines, wrappedLines);
  862. var breakLines = "";
  863. foreach (var line in wrappedLines) {
  864. breakLines += $"{line}{Environment.NewLine}";
  865. }
  866. var expected = string.Empty;
  867. foreach (var line in resultLines) {
  868. expected += $"{line}{Environment.NewLine}";
  869. }
  870. Assert.Equal (expected, breakLines);
  871. // Double space Complex example - this is how VS 2022 does it
  872. //text = "A sentence has words. ";
  873. //breakLines = "";
  874. //wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: true);
  875. //foreach (var line in wrappedLines) {
  876. // breakLines += $"{line}{Environment.NewLine}";
  877. //}
  878. //expected = "A " + Environment.NewLine +
  879. // " se" + Environment.NewLine +
  880. // " nt" + Environment.NewLine +
  881. // " en" + Environment.NewLine +
  882. // " ce" + Environment.NewLine +
  883. // " " + Environment.NewLine +
  884. // " " + Environment.NewLine +
  885. // " " + Environment.NewLine +
  886. // " ha" + Environment.NewLine +
  887. // " s " + Environment.NewLine +
  888. // " wo" + Environment.NewLine +
  889. // " rd" + Environment.NewLine +
  890. // " s." + Environment.NewLine;
  891. //Assert.Equal (expected, breakLines);
  892. }
  893. [Theory]
  894. [InlineData (null, 1, new string [] { })] // null input
  895. [InlineData ("", 1, new string [] { })] // Empty input
  896. [InlineData ("1 34", 1, new string [] { "1", "3", "4" })] // Single Spaces
  897. [InlineData ("1", 1, new string [] { "1" })] // Short input
  898. [InlineData ("12", 1, new string [] { "1", "2" })]
  899. [InlineData ("123", 1, new string [] { "1", "2", "3" })]
  900. [InlineData ("123456", 1, new string [] { "1", "2", "3", "4", "5", "6" })] // No spaces
  901. [InlineData (" ", 1, new string [] { " " })] // Just Spaces; should result in a single space
  902. [InlineData (" ", 1, new string [] { " " })]
  903. [InlineData (" ", 1, new string [] { " ", " " })]
  904. [InlineData (" ", 1, new string [] { " ", " " })]
  905. [InlineData ("12 456", 1, new string [] { "1", "2", "4", "5", "6" })] // Single Spaces
  906. [InlineData (" 2 456", 1, new string [] { " ", "2", "4", "5", "6" })] // Leading spaces should be preserved.
  907. [InlineData (" 2 456 8", 1, new string [] { " ", "2", "4", "5", "6", "8" })]
  908. [InlineData ("A sentence has words. ", 1, new string [] { "A", "s", "e", "n", "t", "e", "n", "c", "e", "h", "a", "s", "w", "o", "r", "d", "s", "." })] // Complex example
  909. [InlineData ("12 567", 1, new string [] { "1", "2", " ", "5", "6", "7" })] // Double Spaces
  910. [InlineData (" 3 567", 1, new string [] { " ", "3", "5", "6", "7" })] // Double Leading spaces should be preserved.
  911. [InlineData (" 3 678 1", 1, new string [] { " ", "3", " ", "6", "7", "8", " ", "1" })]
  912. [InlineData ("1 456", 1, new string [] { "1", " ", "4", "5", "6" })]
  913. [InlineData ("A sentence has words. ", 1, new string [] { "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", " ", "h", "a", "s", "w", "o", "r", "d", "s", ".", " " })] // Double space Complex example
  914. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_1 (string text, int width, IEnumerable<string> resultLines)
  915. {
  916. var wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: false);
  917. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  918. Assert.Equal (resultLines, wrappedLines);
  919. var breakLines = "";
  920. foreach (var line in wrappedLines) {
  921. breakLines += $"{line}{Environment.NewLine}";
  922. }
  923. var expected = string.Empty;
  924. foreach (var line in resultLines) {
  925. expected += $"{line}{Environment.NewLine}";
  926. }
  927. Assert.Equal (expected, breakLines);
  928. }
  929. [Theory]
  930. [InlineData (null, 3, new string [] { })] // null input
  931. [InlineData ("", 3, new string [] { })] // Empty input
  932. [InlineData ("1", 3, new string [] { "1" })] // Short input
  933. [InlineData ("12", 3, new string [] { "12" })]
  934. [InlineData ("123", 3, new string [] { "123" })]
  935. [InlineData ("123456", 3, new string [] { "123", "456" })] // No spaces
  936. [InlineData ("1234567", 3, new string [] { "123", "456", "7" })] // No spaces
  937. [InlineData (" ", 3, new string [] { " " })] // Just Spaces; should result in a single space
  938. [InlineData (" ", 3, new string [] { " " })]
  939. [InlineData (" ", 3, new string [] { " " })]
  940. [InlineData (" ", 3, new string [] { " " })]
  941. [InlineData ("12 456", 3, new string [] { "12", "456" })] // Single Spaces
  942. [InlineData (" 2 456", 3, new string [] { " 2", "456" })] // Leading spaces should be preserved.
  943. [InlineData (" 2 456 8", 3, new string [] { " 2", "456", "8" })]
  944. [InlineData ("A sentence has words. ", 3, new string [] { "A", "sen", "ten", "ce", "has", "wor", "ds." })] // Complex example
  945. [InlineData ("12 567", 3, new string [] { "12 ", "567" })] // Double Spaces
  946. [InlineData (" 3 567", 3, new string [] { " 3", "567" })] // Double Leading spaces should be preserved.
  947. [InlineData (" 3 678 1", 3, new string [] { " 3", " 67", "8 ", "1" })]
  948. [InlineData ("1 456", 3, new string [] { "1 ", "456" })]
  949. [InlineData ("A sentence has words. ", 3, new string [] { "A ", "sen", "ten", "ce ", " ", "has", "wor", "ds.", " " })] // Double space Complex example
  950. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_3 (string text, int width, IEnumerable<string> resultLines)
  951. {
  952. var wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: false);
  953. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  954. Assert.Equal (resultLines, wrappedLines);
  955. var breakLines = "";
  956. foreach (var line in wrappedLines) {
  957. breakLines += $"{line}{Environment.NewLine}";
  958. }
  959. var expected = string.Empty;
  960. foreach (var line in resultLines) {
  961. expected += $"{line}{Environment.NewLine}";
  962. }
  963. Assert.Equal (expected, breakLines);
  964. }
  965. [Theory]
  966. [InlineData (null, 50, new string [] { })] // null input
  967. [InlineData ("", 50, new string [] { })] // Empty input
  968. [InlineData ("1", 50, new string [] { "1" })] // Short input
  969. [InlineData ("12", 50, new string [] { "12" })]
  970. [InlineData ("123", 50, new string [] { "123" })]
  971. [InlineData ("123456", 50, new string [] { "123456" })] // No spaces
  972. [InlineData ("1234567", 50, new string [] { "1234567" })] // No spaces
  973. [InlineData (" ", 50, new string [] { " " })] // Just Spaces; should result in a single space
  974. [InlineData (" ", 50, new string [] { " " })]
  975. [InlineData (" ", 50, new string [] { " " })]
  976. [InlineData ("12 456", 50, new string [] { "12 456" })] // Single Spaces
  977. [InlineData (" 2 456", 50, new string [] { " 2 456" })] // Leading spaces should be preserved.
  978. [InlineData (" 2 456 8", 50, new string [] { " 2 456 8" })]
  979. [InlineData ("A sentence has words. ", 50, new string [] { "A sentence has words. " })] // Complex example
  980. [InlineData ("12 567", 50, new string [] { "12 567" })] // Double Spaces
  981. [InlineData (" 3 567", 50, new string [] { " 3 567" })] // Double Leading spaces should be preserved.
  982. [InlineData (" 3 678 1", 50, new string [] { " 3 678 1" })]
  983. [InlineData ("1 456", 50, new string [] { "1 456" })]
  984. [InlineData ("A sentence has words. ", 50, new string [] { "A sentence has words. " })] // Double space Complex example
  985. public void WordWrap_PreserveTrailingSpaces_False_With_Simple_Runes_Width_50 (string text, int width, IEnumerable<string> resultLines)
  986. {
  987. var wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: false);
  988. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  989. Assert.Equal (resultLines, wrappedLines);
  990. var breakLines = "";
  991. foreach (var line in wrappedLines) {
  992. breakLines += $"{line}{Environment.NewLine}";
  993. }
  994. var expected = string.Empty;
  995. foreach (var line in resultLines) {
  996. expected += $"{line}{Environment.NewLine}";
  997. }
  998. Assert.Equal (expected, breakLines);
  999. }
  1000. [Theory]
  1001. [InlineData ("A sentence\t\t\t has words.", 14, -10, new string [] { "A sentence\t", "\t\t has ", "words." })]
  1002. [InlineData ("A sentence\t\t\t has words.", 8, -16, new string [] { "A ", "sentence", "\t\t", "\t ", "has ", "words." })]
  1003. [InlineData ("A sentence\t\t\t has words.", 3, -21, new string [] { "A ", "sen", "ten", "ce", "\t", "\t", "\t", " ", "has", " ", "wor", "ds." })]
  1004. [InlineData ("A sentence\t\t\t has words.", 2, -22, new string [] { "A ", "se", "nt", "en", "ce", "\t", "\t", "\t", " ", "ha", "s ", "wo", "rd", "s." })]
  1005. [InlineData ("A sentence\t\t\t has words.", 1, -23, new string [] { "A", " ", "s", "e", "n", "t", "e", "n", "c", "e", "\t", "\t", "\t", " ", "h", "a", "s", " ", "w", "o", "r", "d", "s", "." })]
  1006. public void WordWrap_PreserveTrailingSpaces_True_With_Tab (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines, int tabWidth = 4)
  1007. {
  1008. List<string> wrappedLines;
  1009. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1010. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1011. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, preserveTrailingSpaces: true, tabWidth: tabWidth);
  1012. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1013. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  1014. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  1015. Assert.Equal (resultLines, wrappedLines);
  1016. }
  1017. [Theory]
  1018. [InlineData ("これが最初の行です。 こんにちは世界。 これが2行目です。", 29, 0, new string [] { "これが最初の行です。", "こんにちは世界。", "これが2行目です。" })]
  1019. public void WordWrap_PreserveTrailingSpaces_False_Unicode_Wide_Runes (string text, int maxWidth, int widthOffset, IEnumerable<string> resultLines)
  1020. {
  1021. List<string> wrappedLines;
  1022. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1023. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1024. wrappedLines = TextFormatter.WordWrapText (text, maxWidth, preserveTrailingSpaces: false);
  1025. Assert.Equal (wrappedLines.Count, resultLines.Count ());
  1026. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetRuneCount ()) : 0));
  1027. Assert.True (expectedClippedWidth >= (wrappedLines.Count > 0 ? wrappedLines.Max (l => l.GetColumns ()) : 0));
  1028. Assert.Equal (resultLines, wrappedLines);
  1029. }
  1030. [Theory]
  1031. [InlineData ("test", 0, 't', "test")]
  1032. [InlineData ("test", 1, 'e', "test")]
  1033. [InlineData ("Ok", 0, 'O', "Ok")]
  1034. [InlineData ("[◦ Ok ◦]", 3, 'O', "[◦ Ok ◦]")]
  1035. [InlineData ("^k", 0, '^', "^k")]
  1036. public void ReplaceHotKeyWithTag (string text, int hotPos, uint tag, string expected)
  1037. {
  1038. var tf = new TextFormatter ();
  1039. var runes = text.ToRuneList ();
  1040. Rune rune;
  1041. if (Rune.TryGetRuneAt (text, hotPos, out rune)) {
  1042. Assert.Equal (rune, (Rune)tag);
  1043. }
  1044. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  1045. Assert.Equal (result, expected);
  1046. Assert.Equal ((Rune)tag, result.ToRunes () [hotPos]);
  1047. Assert.Equal (text.GetRuneCount (), runes.Count);
  1048. Assert.Equal (text, StringExtensions.ToString (runes));
  1049. }
  1050. [Theory]
  1051. [InlineData ("", -1, TextAlignment.Left, false, 0)]
  1052. [InlineData (null, 0, TextAlignment.Left, false, 1)]
  1053. [InlineData (null, 0, TextAlignment.Left, true, 1)]
  1054. [InlineData ("", 0, TextAlignment.Left, false, 1)]
  1055. [InlineData ("", 0, TextAlignment.Left, true, 1)]
  1056. public void Reformat_Invalid (string text, int maxWidth, TextAlignment textAlignment, bool wrap, int linesCount)
  1057. {
  1058. if (maxWidth < 0) {
  1059. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, maxWidth, textAlignment, wrap));
  1060. } else {
  1061. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1062. Assert.NotEmpty (list);
  1063. Assert.True (list.Count == linesCount);
  1064. Assert.Equal (string.Empty, list [0]);
  1065. }
  1066. }
  1067. [Theory]
  1068. [InlineData ("", 0, 0, TextAlignment.Left, false, 1, true)]
  1069. [InlineData ("", 1, 1, TextAlignment.Left, false, 1, true)]
  1070. [InlineData ("A sentence has words.", 0, -21, TextAlignment.Left, false, 1, true)]
  1071. [InlineData ("A sentence has words.", 1, -20, TextAlignment.Left, false, 1, false)]
  1072. [InlineData ("A sentence has words.", 5, -16, TextAlignment.Left, false, 1, false)]
  1073. [InlineData ("A sentence has words.", 20, -1, TextAlignment.Left, false, 1, false)]
  1074. // no clip
  1075. [InlineData ("A sentence has words.", 21, 0, TextAlignment.Left, false, 1, false)]
  1076. [InlineData ("A sentence has words.", 22, 1, TextAlignment.Left, false, 1, false)]
  1077. public void Reformat_NoWordrap_SingleLine (string text, int maxWidth, int widthOffset, TextAlignment textAlignment, bool wrap, int linesCount, bool stringEmpty)
  1078. {
  1079. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1080. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1081. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1082. Assert.NotEmpty (list);
  1083. Assert.True (list.Count == linesCount);
  1084. if (stringEmpty) {
  1085. Assert.Equal (string.Empty, list [0]);
  1086. } else {
  1087. Assert.NotEqual (string.Empty, list [0]);
  1088. }
  1089. Assert.Equal (StringExtensions.ToString (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1090. }
  1091. [Theory]
  1092. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true)]
  1093. [InlineData ("A sentence has words.\nLine 2.", 1, -28, TextAlignment.Left, false, 1, false)]
  1094. [InlineData ("A sentence has words.\nLine 2.", 5, -24, TextAlignment.Left, false, 1, false)]
  1095. [InlineData ("A sentence has words.\nLine 2.", 28, -1, TextAlignment.Left, false, 1, false)]
  1096. // no clip
  1097. [InlineData ("A sentence has words.\nLine 2.", 29, 0, TextAlignment.Left, false, 1, false)]
  1098. [InlineData ("A sentence has words.\nLine 2.", 30, 1, TextAlignment.Left, false, 1, false)]
  1099. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true)]
  1100. [InlineData ("A sentence has words.\r\nLine 2.", 1, -29, TextAlignment.Left, false, 1, false)]
  1101. [InlineData ("A sentence has words.\r\nLine 2.", 5, -25, TextAlignment.Left, false, 1, false)]
  1102. [InlineData ("A sentence has words.\r\nLine 2.", 29, -1, TextAlignment.Left, false, 1, false, 1)]
  1103. [InlineData ("A sentence has words.\r\nLine 2.", 30, 0, TextAlignment.Left, false, 1, false)]
  1104. [InlineData ("A sentence has words.\r\nLine 2.", 31, 1, TextAlignment.Left, false, 1, false)]
  1105. public void Reformat_NoWordrap_NewLines_MultiLine_False (string text, int maxWidth, int widthOffset, TextAlignment textAlignment, bool wrap, int linesCount, bool stringEmpty, int clipWidthOffset = 0)
  1106. {
  1107. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1108. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth) + clipWidthOffset;
  1109. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap);
  1110. Assert.NotEmpty (list);
  1111. Assert.True (list.Count == linesCount);
  1112. if (stringEmpty) {
  1113. Assert.Equal (string.Empty, list [0]);
  1114. } else {
  1115. Assert.NotEqual (string.Empty, list [0]);
  1116. }
  1117. if (text.Contains ("\r\n") && maxWidth > 0) {
  1118. Assert.Equal (StringExtensions.ToString (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  1119. } else if (text.Contains ('\n') && maxWidth > 0) {
  1120. Assert.Equal (StringExtensions.ToString (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  1121. } else {
  1122. Assert.Equal (StringExtensions.ToString (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1123. }
  1124. }
  1125. [Theory]
  1126. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true, new string [] { "" })]
  1127. [InlineData ("A sentence has words.\nLine 2.", 1, -28, TextAlignment.Left, false, 2, false, new string [] { "A", "L" })]
  1128. [InlineData ("A sentence has words.\nLine 2.", 5, -24, TextAlignment.Left, false, 2, false, new string [] { "A sen", "Line " })]
  1129. [InlineData ("A sentence has words.\nLine 2.", 28, -1, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1130. //// no clip
  1131. [InlineData ("A sentence has words.\nLine 2.", 29, 0, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1132. [InlineData ("A sentence has words.\nLine 2.", 30, 1, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1133. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true, new string [] { "" })]
  1134. [InlineData ("A sentence has words.\r\nLine 2.", 1, -29, TextAlignment.Left, false, 2, false, new string [] { "A", "L" })]
  1135. [InlineData ("A sentence has words.\r\nLine 2.", 5, -25, TextAlignment.Left, false, 2, false, new string [] { "A sen", "Line " })]
  1136. [InlineData ("A sentence has words.\r\nLine 2.", 29, -1, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1137. [InlineData ("A sentence has words.\r\nLine 2.", 30, 0, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1138. [InlineData ("A sentence has words.\r\nLine 2.", 31, 1, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1139. public void Reformat_NoWordrap_NewLines_MultiLine_True (string text, int maxWidth, int widthOffset, TextAlignment textAlignment, bool wrap, int linesCount, bool stringEmpty, IEnumerable<string> resultLines)
  1140. {
  1141. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1142. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, false, 0, TextDirection.LeftRight_TopBottom, true);
  1143. Assert.NotEmpty (list);
  1144. Assert.True (list.Count == linesCount);
  1145. if (stringEmpty) {
  1146. Assert.Equal (string.Empty, list [0]);
  1147. } else {
  1148. Assert.NotEqual (string.Empty, list [0]);
  1149. }
  1150. Assert.Equal (list, resultLines);
  1151. }
  1152. [Theory]
  1153. [InlineData ("A sentence has words.\nLine 2.", 0, -29, TextAlignment.Left, false, 1, true, new string [] { "" })]
  1154. [InlineData ("A sentence has words.\nLine 2.", 1, -28, TextAlignment.Left, false, 2, false, new string [] { "A", "L" })]
  1155. [InlineData ("A sentence has words.\nLine 2.", 5, -24, TextAlignment.Left, false, 2, false, new string [] { "A sen", "Line " })]
  1156. [InlineData ("A sentence has words.\nLine 2.", 28, -1, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1157. //// no clip
  1158. [InlineData ("A sentence has words.\nLine 2.", 29, 0, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1159. [InlineData ("A sentence has words.\nLine 2.", 30, 1, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1160. [InlineData ("A sentence has words.\r\nLine 2.", 0, -30, TextAlignment.Left, false, 1, true, new string [] { "" })]
  1161. [InlineData ("A sentence has words.\r\nLine 2.", 1, -29, TextAlignment.Left, false, 2, false, new string [] { "A", "L" })]
  1162. [InlineData ("A sentence has words.\r\nLine 2.", 5, -25, TextAlignment.Left, false, 2, false, new string [] { "A sen", "Line " })]
  1163. [InlineData ("A sentence has words.\r\nLine 2.", 29, -1, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1164. [InlineData ("A sentence has words.\r\nLine 2.", 30, 0, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1165. [InlineData ("A sentence has words.\r\nLine 2.", 31, 1, TextAlignment.Left, false, 2, false, new string [] { "A sentence has words.", "Line 2." })]
  1166. public void Reformat_NoWordrap_NewLines_MultiLine_True_Vertical (string text, int maxWidth, int widthOffset, TextAlignment textAlignment, bool wrap, int linesCount, bool stringEmpty, IEnumerable<string> resultLines)
  1167. {
  1168. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1169. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, false, 0, TextDirection.TopBottom_LeftRight, true);
  1170. Assert.NotEmpty (list);
  1171. Assert.True (list.Count == linesCount);
  1172. if (stringEmpty) {
  1173. Assert.Equal (string.Empty, list [0]);
  1174. } else {
  1175. Assert.NotEqual (string.Empty, list [0]);
  1176. }
  1177. Assert.Equal (list, resultLines);
  1178. }
  1179. [Theory]
  1180. // Even # of spaces
  1181. // 0123456789
  1182. [InlineData ("012 456 89", 0, -10, TextAlignment.Left, true, true, true, new string [] { "" })]
  1183. [InlineData ("012 456 89", 1, -9, TextAlignment.Left, true, true, false, new string [] { "0", "1", "2", " ", "4", "5", "6", " ", "8", "9" }, "01245689")]
  1184. [InlineData ("012 456 89", 5, -5, TextAlignment.Left, true, true, false, new string [] { "012 ", "456 ", "89" })]
  1185. [InlineData ("012 456 89", 9, -1, TextAlignment.Left, true, true, false, new string [] { "012 456 ", "89" })]
  1186. // no clip
  1187. [InlineData ("012 456 89", 10, 0, TextAlignment.Left, true, true, false, new string [] { "012 456 89" })]
  1188. [InlineData ("012 456 89", 11, 1, TextAlignment.Left, true, true, false, new string [] { "012 456 89" })]
  1189. // Odd # of spaces
  1190. // 01234567890123
  1191. [InlineData ("012 456 89 end", 13, -1, TextAlignment.Left, true, true, false, new string [] { "012 456 89 ", "end" })]
  1192. // no clip
  1193. [InlineData ("012 456 89 end", 14, 0, TextAlignment.Left, true, true, false, new string [] { "012 456 89 end" })]
  1194. [InlineData ("012 456 89 end", 15, 1, TextAlignment.Left, true, true, false, new string [] { "012 456 89 end" })]
  1195. public void Reformat_Wrap_Spaces_No_NewLines (string text, int maxWidth, int widthOffset, TextAlignment textAlignment, bool wrap, bool preserveTrailingSpaces, bool stringEmpty, IEnumerable<string> resultLines, string noSpaceText = "")
  1196. {
  1197. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1198. var expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth);
  1199. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1200. Assert.NotEmpty (list);
  1201. Assert.True (list.Count == resultLines.Count ());
  1202. if (stringEmpty) {
  1203. Assert.Equal (string.Empty, list [0]);
  1204. } else {
  1205. Assert.NotEqual (string.Empty, list [0]);
  1206. }
  1207. Assert.Equal (resultLines, list);
  1208. if (maxWidth > 0) {
  1209. // remove whitespace chars
  1210. if (maxWidth < 5) {
  1211. expectedClippedWidth = text.GetRuneCount () - text.Sum (r => r == ' ' ? 1 : 0);
  1212. } else {
  1213. expectedClippedWidth = Math.Min (text.GetRuneCount (), maxWidth - text.Sum (r => r == ' ' ? 1 : 0));
  1214. }
  1215. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap, preserveTrailingSpaces: false);
  1216. if (maxWidth == 1) {
  1217. Assert.Equal (expectedClippedWidth, list.Count);
  1218. Assert.Equal (noSpaceText, string.Concat (list.ToArray ()));
  1219. }
  1220. if (maxWidth > 1 && maxWidth < 10) {
  1221. Assert.Equal (StringExtensions.ToString (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  1222. }
  1223. }
  1224. }
  1225. [Theory]
  1226. // Unicode
  1227. // Even # of chars
  1228. // 0123456789
  1229. [InlineData ("\u2660пÑРвРÑ", 10, -1, TextAlignment.Left, true, false, new string [] { "\u2660пÑРвÐ", "Ñ" })]
  1230. // no clip
  1231. [InlineData ("\u2660пÑРвРÑ", 11, 0, TextAlignment.Left, true, false, new string [] { "\u2660пÑРвРÑ" })]
  1232. [InlineData ("\u2660пÑРвРÑ", 12, 1, TextAlignment.Left, true, false, new string [] { "\u2660пÑРвРÑ" })]
  1233. // Unicode
  1234. // Odd # of chars
  1235. // 0123456789
  1236. [InlineData ("\u2660 ÑРвРÑ", 9, -1, TextAlignment.Left, true, false, new string [] { "\u2660 ÑРвÐ", "Ñ" })]
  1237. // no clip
  1238. [InlineData ("\u2660 ÑРвРÑ", 10, 0, TextAlignment.Left, true, false, new string [] { "\u2660 ÑРвРÑ" })]
  1239. [InlineData ("\u2660 ÑРвРÑ", 11, 1, TextAlignment.Left, true, false, new string [] { "\u2660 ÑРвРÑ" })]
  1240. public void Reformat_Unicode_Wrap_Spaces_No_NewLines (string text, int maxWidth, int widthOffset, TextAlignment textAlignment, bool wrap, bool preserveTrailingSpaces, IEnumerable<string> resultLines)
  1241. {
  1242. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1243. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1244. Assert.Equal (list.Count, resultLines.Count ());
  1245. Assert.Equal (resultLines, list);
  1246. }
  1247. [Theory]
  1248. // Unicode
  1249. [InlineData ("\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464", 8, -1, TextAlignment.Left, true, false, new string [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" })]
  1250. // no clip
  1251. [InlineData ("\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464", 9, 0, TextAlignment.Left, true, false, new string [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" })]
  1252. [InlineData ("\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464", 10, 1, TextAlignment.Left, true, false, new string [] { "\u2460\u2461\u2462", "\u2460\u2461\u2462\u2463\u2464" })]
  1253. public void Reformat_Unicode_Wrap_Spaces_NewLines (string text, int maxWidth, int widthOffset, TextAlignment textAlignment, bool wrap, bool preserveTrailingSpaces, IEnumerable<string> resultLines)
  1254. {
  1255. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1256. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1257. Assert.Equal (list.Count, resultLines.Count ());
  1258. Assert.Equal (resultLines, list);
  1259. }
  1260. [Theory]
  1261. [InlineData (" A sentence has words. \n This is the second Line - 2. ", 4, -50, TextAlignment.Left, true, false, new string [] { " A", "sent", "ence", "has", "word", "s. ", " Thi", "s is", "the", "seco", "nd", "Line", "- 2." }, " Asentencehaswords. This isthesecondLine- 2.")]
  1262. [InlineData (" A sentence has words. \n This is the second Line - 2. ", 4, -50, TextAlignment.Left, true, true, new string [] { " A ", "sent", "ence", " ", "has ", "word", "s. ", " ", "This", " is ", "the ", "seco", "nd ", "Line", " - ", "2. " }, " A sentence has words. This is the second Line - 2. ")]
  1263. public void Format_WordWrap_PreserveTrailingSpaces (string text, int maxWidth, int widthOffset, TextAlignment textAlignment, bool wrap, bool preserveTrailingSpaces, IEnumerable<string> resultLines, string expectedWrappedText)
  1264. {
  1265. Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
  1266. var list = TextFormatter.Format (text, maxWidth, textAlignment, wrap, preserveTrailingSpaces);
  1267. Assert.Equal (list.Count, resultLines.Count ());
  1268. Assert.Equal (resultLines, list);
  1269. string wrappedText = string.Empty;
  1270. foreach (var txt in list) wrappedText += txt;
  1271. Assert.Equal (expectedWrappedText, wrappedText);
  1272. }
  1273. [Fact]
  1274. public void Format_Dont_Throw_ArgumentException_With_WordWrap_As_False_And_Keep_End_Spaces_As_True ()
  1275. {
  1276. var exception = Record.Exception (() => TextFormatter.Format ("Some text", 4, TextAlignment.Left, false, true));
  1277. Assert.Null (exception);
  1278. }
  1279. [Theory]
  1280. [InlineData ("Hello world, how are you today? Pretty neat!", 44, 80, "Hello world, how are you today? Pretty neat!")]
  1281. public void Format_Justified_Always_Returns_Text_Width_Equal_To_Passed_Width_Horizontal (string text, int runeCount, int maxWidth, string justifiedText)
  1282. {
  1283. Assert.Equal (runeCount, text.GetRuneCount ());
  1284. var fmtText = string.Empty;
  1285. for (int i = text.GetRuneCount (); i < maxWidth; i++) {
  1286. fmtText = TextFormatter.Format (text, i, TextAlignment.Justified, false, true) [0];
  1287. Assert.Equal (i, fmtText.GetRuneCount ());
  1288. var c = fmtText [^1];
  1289. Assert.True (text.EndsWith (c));
  1290. }
  1291. Assert.Equal (justifiedText, fmtText);
  1292. }
  1293. [Theory]
  1294. [InlineData ("Hello world, how are you today? Pretty neat!", 44, 80, "Hello world, how are you today? Pretty neat!")]
  1295. public void Format_Justified_Always_Returns_Text_Width_Equal_To_Passed_Width_Vertical (string text, int runeCount, int maxWidth, string justifiedText)
  1296. {
  1297. Assert.Equal (runeCount, text.GetRuneCount ());
  1298. var fmtText = string.Empty;
  1299. for (int i = text.GetRuneCount (); i < maxWidth; i++) {
  1300. fmtText = TextFormatter.Format (text, i, TextAlignment.Justified, false, true, 0, TextDirection.TopBottom_LeftRight) [0];
  1301. Assert.Equal (i, fmtText.GetRuneCount ());
  1302. var c = fmtText [^1];
  1303. Assert.True (text.EndsWith (c));
  1304. }
  1305. Assert.Equal (justifiedText, fmtText);
  1306. }
  1307. [Theory]
  1308. [InlineData ("fff", 6, "fff ")]
  1309. [InlineData ("Hello World", 16, "Hello World ")]
  1310. public void TestClipOrPad_ShortWord (string text, int fillPad, string expectedText)
  1311. {
  1312. // word is short but we want it to fill # so it should be padded
  1313. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1314. }
  1315. [Theory]
  1316. [InlineData ("123456789", 3, "123")]
  1317. [InlineData ("Hello World", 8, "Hello Wo")]
  1318. public void TestClipOrPad_LongWord (string text, int fillPad, string expectedText)
  1319. {
  1320. // word is long but we want it to fill # space only
  1321. Assert.Equal (expectedText, TextFormatter.ClipOrPad (text, fillPad));
  1322. }
  1323. [Fact]
  1324. public void Internal_Tests ()
  1325. {
  1326. var tf = new TextFormatter ();
  1327. Assert.Equal (Key.Null, tf.HotKey);
  1328. tf.HotKey = Key.CtrlMask | Key.Q;
  1329. Assert.Equal (Key.CtrlMask | Key.Q, tf.HotKey);
  1330. }
  1331. [Theory]
  1332. [InlineData ("Hello World", 11)]
  1333. [InlineData ("こんにちは世界", 14)]
  1334. public void GetColumns_Simple_And_Wide_Runes (string text, int width)
  1335. {
  1336. Assert.Equal (width, text.GetColumns ());
  1337. }
  1338. [Theory]
  1339. [InlineData ("Hello World", 11, 6, 1, 1)]
  1340. [InlineData ("こんにちは 世界", 15, 6, 1, 2)]
  1341. public void GetSumMaxCharWidth_Simple_And_Wide_Runes (string text, int width, int index, int length, int indexWidth)
  1342. {
  1343. Assert.Equal (width, TextFormatter.GetSumMaxCharWidth (text));
  1344. Assert.Equal (indexWidth, TextFormatter.GetSumMaxCharWidth (text, index, length));
  1345. }
  1346. [Theory]
  1347. [InlineData (new string [] { "Hello", "World" }, 2, 1, 1, 1)]
  1348. [InlineData (new string [] { "こんにちは", "世界" }, 4, 1, 1, 2)]
  1349. public void GetSumMaxCharWidth_List_Simple_And_Wide_Runes (IEnumerable<string> text, int width, int index, int length, int indexWidth)
  1350. {
  1351. Assert.Equal (width, TextFormatter.GetSumMaxCharWidth (text.ToList ()));
  1352. Assert.Equal (indexWidth, TextFormatter.GetSumMaxCharWidth (text.ToList (), index, length));
  1353. }
  1354. [Theory]
  1355. [InlineData ("test", 3, 3)]
  1356. [InlineData ("test", 4, 4)]
  1357. [InlineData ("test", 10, 4)]
  1358. public void GetLengthThatFits_Runelist (string text, int columns, int expectedLength)
  1359. {
  1360. var runes = text.ToRuneList ();
  1361. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (runes, columns));
  1362. }
  1363. [Theory]
  1364. [InlineData ("test", 3, 3)]
  1365. [InlineData ("test", 4, 4)]
  1366. [InlineData ("test", 10, 4)]
  1367. [InlineData ("test", 1, 1)]
  1368. [InlineData ("test", 0, 0)]
  1369. [InlineData ("test", -1, 0)]
  1370. [InlineData (null, -1, 0)]
  1371. [InlineData ("", -1, 0)]
  1372. public void GetLengthThatFits_String (string text, int columns, int expectedLength)
  1373. {
  1374. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  1375. }
  1376. [Theory]
  1377. [InlineData ("Hello World", 6, 6)]
  1378. [InlineData ("こんにちは 世界", 6, 3)]
  1379. public void GetLengthThatFits_Simple_And_Wide_Runes (string text, int columns, int expectedLength)
  1380. {
  1381. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (text, columns));
  1382. }
  1383. [Theory]
  1384. [InlineData ("Hello World", 6, 6)]
  1385. [InlineData ("こんにちは 世界", 6, 3)]
  1386. [MemberData (nameof (CMGlyphs))]
  1387. public void GetLengthThatFits_List_Simple_And_Wide_Runes (string text, int columns, int expectedLength)
  1388. {
  1389. var runes = text.ToRuneList ();
  1390. Assert.Equal (expectedLength, TextFormatter.GetLengthThatFits (runes, columns));
  1391. }
  1392. public static IEnumerable<object []> CMGlyphs =>
  1393. new List<object []>
  1394. {
  1395. new object[] { $"{CM.Glyphs.LeftBracket} Say Hello 你 {CM.Glyphs.RightBracket}", 16, 15 }
  1396. };
  1397. [Theory]
  1398. [InlineData ("Truncate", 3, "Tru")]
  1399. [InlineData ("デモエムポンズ", 3, "デ")]
  1400. public void Format_Truncate_Simple_And_Wide_Runes (string text, int width, string expected)
  1401. {
  1402. var list = TextFormatter.Format (text, width, false, false);
  1403. Assert.Equal (expected, list [^1]);
  1404. }
  1405. [Theory]
  1406. [MemberData (nameof (FormatEnvironmentNewLine))]
  1407. public void Format_With_PreserveTrailingSpaces_And_Without_PreserveTrailingSpaces (string text, int width, IEnumerable<string> expected)
  1408. {
  1409. var preserveTrailingSpaces = false;
  1410. var formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
  1411. Assert.Equal (expected, formated);
  1412. preserveTrailingSpaces = true;
  1413. formated = TextFormatter.Format (text, width, false, true, preserveTrailingSpaces);
  1414. Assert.Equal (expected, formated);
  1415. }
  1416. public static IEnumerable<object []> FormatEnvironmentNewLine =>
  1417. new List<object []>
  1418. {
  1419. new object[] { $"Line1{Environment.NewLine}Line2{Environment.NewLine}Line3{Environment.NewLine}", 60, new string [] { "Line1", "Line2", "Line3" } }
  1420. };
  1421. [Theory]
  1422. [MemberData (nameof (SplitEnvironmentNewLine))]
  1423. public void SplitNewLine_Ending__With_Or_Without_NewLine_Probably_CRLF (string text, IEnumerable<string> expected)
  1424. {
  1425. var splited = TextFormatter.SplitNewLine (text);
  1426. Assert.Equal (expected, splited);
  1427. }
  1428. public static IEnumerable<object []> SplitEnvironmentNewLine =>
  1429. new List<object []>
  1430. {
  1431. new object[] { $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界", new string [] { "First Line 界", "Second Line 界", "Third Line 界" } },
  1432. new object[] { $"First Line 界{Environment.NewLine}Second Line 界{Environment.NewLine}Third Line 界{Environment.NewLine}", new string [] { "First Line 界", "Second Line 界", "Third Line 界", "" } }
  1433. };
  1434. [Theory]
  1435. [InlineData ($"First Line 界\nSecond Line 界\nThird Line 界", new string [] { "First Line 界", "Second Line 界", "Third Line 界" })]
  1436. public void SplitNewLine_Ending_Without_NewLine_Only_LF (string text, IEnumerable<string> expected)
  1437. {
  1438. var splited = TextFormatter.SplitNewLine (text);
  1439. Assert.Equal (expected, splited);
  1440. }
  1441. [Theory]
  1442. [InlineData ($"First Line 界\nSecond Line 界\nThird Line 界\n", new string [] { "First Line 界", "Second Line 界", "Third Line 界", "" })]
  1443. public void SplitNewLine_Ending_With_NewLine_Only_LF (string text, IEnumerable<string> expected)
  1444. {
  1445. var splited = TextFormatter.SplitNewLine (text);
  1446. Assert.Equal (expected, splited);
  1447. }
  1448. [Theory]
  1449. [InlineData ("Single Line 界", 14)]
  1450. [InlineData ($"First Line 界\nSecond Line 界\nThird Line 界\n", 14)]
  1451. public void MaxWidthLine_With_And_Without_Newlines (string text, int expected)
  1452. {
  1453. Assert.Equal (expected, TextFormatter.MaxWidthLine (text));
  1454. }
  1455. [Theory]
  1456. [InlineData ("New Test 你", 10, 10, 20320, 20320, 9, "你")]
  1457. [InlineData ("New Test \U0001d539", 10, 11, 120121, 55349, 9, "𝔹")]
  1458. public void String_Array_Is_Not_Always_Equal_ToRunes_Array (string text, int runesLength, int stringLength, int runeValue, int stringValue, int index, string expected)
  1459. {
  1460. var usToRunes = text.ToRunes ();
  1461. Assert.Equal (runesLength, usToRunes.Length);
  1462. Assert.Equal (stringLength, text.Length);
  1463. Assert.Equal (runeValue, usToRunes [index].Value);
  1464. Assert.Equal (stringValue, text [index]);
  1465. Assert.Equal (expected, usToRunes [index].ToString ());
  1466. if (char.IsHighSurrogate (text [index])) {
  1467. // Rune array length isn't equal to string array
  1468. Assert.Equal (expected, new string (new char [] { text [index], text [index + 1] }));
  1469. } else {
  1470. // Rune array length is equal to string array
  1471. Assert.Equal (expected, text [index].ToString ());
  1472. }
  1473. }
  1474. [Fact]
  1475. public void GetLengthThatFits_With_Combining_Runes ()
  1476. {
  1477. var text = "Les Mise\u0328\u0301rables";
  1478. Assert.Equal (16, TextFormatter.GetLengthThatFits (text, 14));
  1479. }
  1480. [Fact]
  1481. public void GetMaxColsForWidth_With_Combining_Runes ()
  1482. {
  1483. var text = new List<string> () { "Les Mis", "e\u0328\u0301", "rables" };
  1484. Assert.Equal (1, TextFormatter.GetMaxColsForWidth (text, 1));
  1485. }
  1486. [Fact]
  1487. public void GetSumMaxCharWidth_With_Combining_Runes ()
  1488. {
  1489. var text = "Les Mise\u0328\u0301rables";
  1490. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  1491. }
  1492. [Fact]
  1493. public void GetSumMaxCharWidth_List_With_Combining_Runes ()
  1494. {
  1495. var text = new List<string> () { "Les Mis", "e\u0328\u0301", "rables" };
  1496. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  1497. }
  1498. [Theory]
  1499. [InlineData (14, 1, TextDirection.LeftRight_TopBottom)]
  1500. [InlineData (1, 14, TextDirection.TopBottom_LeftRight)]
  1501. public void CalcRect_With_Combining_Runes (int width, int height, TextDirection textDirection)
  1502. {
  1503. var text = "Les Mise\u0328\u0301rables";
  1504. Assert.Equal (new Rect (0, 0, width, height), TextFormatter.CalcRect (0, 0, text, textDirection));
  1505. }
  1506. [Theory]
  1507. [InlineData (14, 1, TextDirection.LeftRight_TopBottom, "Les Misęrables")]
  1508. [InlineData (1, 14, TextDirection.TopBottom_LeftRight, "L\ne\ns\n \nM\ni\ns\nę\nr\na\nb\nl\ne\ns")]
  1509. [InlineData (4, 4, TextDirection.TopBottom_LeftRight, @"
  1510. LMre
  1511. eias
  1512. ssb
  1513. ęl ")]
  1514. public void Draw_With_Combining_Runes (int width, int height, TextDirection textDirection, string expected)
  1515. {
  1516. var driver = new FakeDriver ();
  1517. driver.Init ();
  1518. var text = "Les Mise\u0328\u0301rables";
  1519. var tf = new TextFormatter ();
  1520. tf.Direction = textDirection;
  1521. tf.Text = text;
  1522. Assert.True (tf.WordWrap);
  1523. if (textDirection == TextDirection.LeftRight_TopBottom) {
  1524. Assert.Equal (new Size (width, height), tf.Size);
  1525. } else {
  1526. Assert.Equal (new Size (1, text.GetColumns ()), tf.Size);
  1527. tf.Size = new Size (width, height);
  1528. }
  1529. tf.Draw (new Rect (0, 0, width, height), new Attribute (ColorName.White, ColorName.Black), new Attribute (ColorName.Blue, ColorName.Black), default, true, driver);
  1530. TestHelpers.AssertDriverContentsWithFrameAre (expected, output, driver);
  1531. driver.End ();
  1532. }
  1533. [Theory]
  1534. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1535. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1536. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1537. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1538. public void TabWith_PreserveTrailingSpaces_False (int width, int height, TextDirection textDirection, int tabWidth, string expected)
  1539. {
  1540. var driver = new FakeDriver ();
  1541. driver.Init ();
  1542. var text = "This is a \tTab";
  1543. var tf = new TextFormatter ();
  1544. tf.Direction = textDirection;
  1545. tf.TabWidth = tabWidth;
  1546. tf.Text = text;
  1547. Assert.True (tf.WordWrap);
  1548. Assert.False (tf.PreserveTrailingSpaces);
  1549. Assert.Equal (new Size (width, height), tf.Size);
  1550. tf.Draw (new Rect (0, 0, width, height), new Attribute (ColorName.White, ColorName.Black), new Attribute (ColorName.Blue, ColorName.Black), default, true, driver);
  1551. TestHelpers.AssertDriverContentsWithFrameAre (expected, output, driver);
  1552. driver.End ();
  1553. }
  1554. [Theory]
  1555. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1556. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1557. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1558. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1559. public void TabWith_PreserveTrailingSpaces_True (int width, int height, TextDirection textDirection, int tabWidth, string expected)
  1560. {
  1561. var driver = new FakeDriver ();
  1562. driver.Init ();
  1563. var text = "This is a \tTab";
  1564. var tf = new TextFormatter ();
  1565. tf.Direction = textDirection;
  1566. tf.TabWidth = tabWidth;
  1567. tf.PreserveTrailingSpaces = true;
  1568. tf.Text = text;
  1569. Assert.True (tf.WordWrap);
  1570. Assert.Equal (new Size (width, height), tf.Size);
  1571. tf.Draw (new Rect (0, 0, width, height), new Attribute (ColorName.White, ColorName.Black), new Attribute (ColorName.Blue, ColorName.Black), default, true, driver);
  1572. TestHelpers.AssertDriverContentsWithFrameAre (expected, output, driver);
  1573. driver.End ();
  1574. }
  1575. [Theory]
  1576. [InlineData (17, 1, TextDirection.LeftRight_TopBottom, 4, "This is a Tab")]
  1577. [InlineData (1, 17, TextDirection.TopBottom_LeftRight, 4, "T\nh\ni\ns\n \ni\ns\n \na\n \n \n \n \n \nT\na\nb")]
  1578. [InlineData (13, 1, TextDirection.LeftRight_TopBottom, 0, "This is a Tab")]
  1579. [InlineData (1, 13, TextDirection.TopBottom_LeftRight, 0, "T\nh\ni\ns\n \ni\ns\n \na\n \nT\na\nb")]
  1580. public void TabWith_WordWrap_True (int width, int height, TextDirection textDirection, int tabWidth, string expected)
  1581. {
  1582. var driver = new FakeDriver ();
  1583. driver.Init ();
  1584. var text = "This is a \tTab";
  1585. var tf = new TextFormatter ();
  1586. tf.Direction = textDirection;
  1587. tf.TabWidth = tabWidth;
  1588. tf.WordWrap = true;
  1589. tf.Text = text;
  1590. Assert.False (tf.PreserveTrailingSpaces);
  1591. Assert.Equal (new Size (width, height), tf.Size);
  1592. tf.Draw (new Rect (0, 0, width, height), new Attribute (ColorName.White, ColorName.Black), new Attribute (ColorName.Blue, ColorName.Black), default, true, driver);
  1593. TestHelpers.AssertDriverContentsWithFrameAre (expected, output, driver);
  1594. driver.End ();
  1595. }
  1596. }
  1597. }