TextFormatterTests.cs 91 KB

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