TextFormatterTests.cs 90 KB

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