TextFormatterTests.cs 74 KB

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