TextFormatterTests.cs 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Terminal.Gui.Views;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.Core {
  11. public class TextFormatterTests {
  12. readonly ITestOutputHelper output;
  13. public TextFormatterTests (ITestOutputHelper output)
  14. {
  15. this.output = output;
  16. }
  17. [Fact]
  18. public void Basic_Usage ()
  19. {
  20. var testText = ustring.Make ("test");
  21. var expectedSize = new Size ();
  22. var testBounds = new Rect (0, 0, 100, 1);
  23. var tf = new TextFormatter ();
  24. tf.Text = testText;
  25. expectedSize = new Size (testText.Length, 1);
  26. Assert.Equal (testText, tf.Text);
  27. Assert.Equal (TextAlignment.Left, tf.Alignment);
  28. Assert.Equal (expectedSize, tf.Size);
  29. tf.Draw (testBounds, new Attribute (), new Attribute ());
  30. Assert.Equal (expectedSize, tf.Size);
  31. Assert.NotEmpty (tf.Lines);
  32. tf.Alignment = TextAlignment.Right;
  33. expectedSize = new Size (testText.Length, 1);
  34. Assert.Equal (testText, tf.Text);
  35. Assert.Equal (TextAlignment.Right, tf.Alignment);
  36. Assert.Equal (expectedSize, tf.Size);
  37. tf.Draw (testBounds, new Attribute (), new Attribute ());
  38. Assert.Equal (expectedSize, tf.Size);
  39. Assert.NotEmpty (tf.Lines);
  40. tf.Alignment = TextAlignment.Right;
  41. expectedSize = new Size (testText.Length * 2, 1);
  42. tf.Size = expectedSize;
  43. Assert.Equal (testText, tf.Text);
  44. Assert.Equal (TextAlignment.Right, tf.Alignment);
  45. Assert.Equal (expectedSize, tf.Size);
  46. tf.Draw (testBounds, new Attribute (), new Attribute ());
  47. Assert.Equal (expectedSize, tf.Size);
  48. Assert.NotEmpty (tf.Lines);
  49. tf.Alignment = TextAlignment.Centered;
  50. expectedSize = new Size (testText.Length * 2, 1);
  51. tf.Size = expectedSize;
  52. Assert.Equal (testText, tf.Text);
  53. Assert.Equal (TextAlignment.Centered, tf.Alignment);
  54. Assert.Equal (expectedSize, tf.Size);
  55. tf.Draw (testBounds, new Attribute (), new Attribute ());
  56. Assert.Equal (expectedSize, tf.Size);
  57. Assert.NotEmpty (tf.Lines);
  58. }
  59. [Fact]
  60. public void TestSize_TextChange ()
  61. {
  62. var tf = new TextFormatter () { Text = "你" };
  63. Assert.Equal (2,tf.Size.Width);
  64. tf.Text = "你你";
  65. Assert.Equal (4, tf.Size.Width);
  66. }
  67. [Fact]
  68. public void NeedsFormat_Sets ()
  69. {
  70. var testText = ustring.Make ("test");
  71. var testBounds = new Rect (0, 0, 100, 1);
  72. var tf = new TextFormatter ();
  73. tf.Text = "test";
  74. Assert.True (tf.NeedsFormat); // get_Lines causes a Format
  75. Assert.NotEmpty (tf.Lines);
  76. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  77. Assert.Equal (testText, tf.Text);
  78. tf.Draw (testBounds, new Attribute (), new Attribute ());
  79. Assert.False (tf.NeedsFormat);
  80. tf.Size = new Size (1, 1);
  81. Assert.True (tf.NeedsFormat);
  82. Assert.NotEmpty (tf.Lines);
  83. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  84. tf.Alignment = TextAlignment.Centered;
  85. Assert.True (tf.NeedsFormat);
  86. Assert.NotEmpty (tf.Lines);
  87. Assert.False (tf.NeedsFormat); // get_Lines causes a Format
  88. }
  89. [Fact]
  90. public void FindHotKey_Invalid_ReturnsFalse ()
  91. {
  92. var text = ustring.Empty;
  93. Rune hotKeySpecifier = '_';
  94. bool supportFirstUpperCase = false;
  95. int hotPos = 0;
  96. Key hotKey = Key.Unknown;
  97. bool result = false;
  98. text = null;
  99. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  100. Assert.False (result);
  101. Assert.Equal (-1, hotPos);
  102. Assert.Equal (Key.Unknown, hotKey);
  103. text = "";
  104. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  105. Assert.False (result);
  106. Assert.Equal (-1, hotPos);
  107. Assert.Equal (Key.Unknown, hotKey);
  108. text = "no hotkey";
  109. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  110. Assert.False (result);
  111. Assert.Equal (-1, hotPos);
  112. Assert.Equal (Key.Unknown, hotKey);
  113. text = "No hotkey, Upper Case";
  114. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  115. Assert.False (result);
  116. Assert.Equal (-1, hotPos);
  117. Assert.Equal (Key.Unknown, hotKey);
  118. text = "Non-english: Сохранить";
  119. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  120. Assert.False (result);
  121. Assert.Equal (-1, hotPos);
  122. Assert.Equal (Key.Unknown, hotKey);
  123. }
  124. [Fact]
  125. public void FindHotKey_AlphaUpperCase_Succeeds ()
  126. {
  127. var text = ustring.Empty;
  128. Rune hotKeySpecifier = '_';
  129. bool supportFirstUpperCase = false;
  130. int hotPos = 0;
  131. Key hotKey = Key.Unknown;
  132. bool result = false;
  133. text = "_K Before";
  134. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  135. Assert.True (result);
  136. Assert.Equal (0, hotPos);
  137. Assert.Equal ((Key)'K', hotKey);
  138. text = "a_K Second";
  139. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  140. Assert.True (result);
  141. Assert.Equal (1, hotPos);
  142. Assert.Equal ((Key)'K', hotKey);
  143. text = "Last _K";
  144. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  145. Assert.True (result);
  146. Assert.Equal (5, hotPos);
  147. Assert.Equal ((Key)'K', hotKey);
  148. text = "After K_";
  149. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  150. Assert.False (result);
  151. Assert.Equal (-1, hotPos);
  152. Assert.Equal (Key.Unknown, hotKey);
  153. text = "Multiple _K and _R";
  154. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  155. Assert.True (result);
  156. Assert.Equal (9, hotPos);
  157. Assert.Equal ((Key)'K', hotKey);
  158. // Cryllic K (К)
  159. text = "Non-english: _Кдать";
  160. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  161. Assert.True (result);
  162. Assert.Equal (13, hotPos);
  163. Assert.Equal ((Key)'К', hotKey);
  164. // Turn on FirstUpperCase and verify same results
  165. supportFirstUpperCase = true;
  166. text = "_K Before";
  167. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  168. Assert.True (result);
  169. Assert.Equal (0, hotPos);
  170. Assert.Equal ((Key)'K', hotKey);
  171. text = "a_K Second";
  172. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  173. Assert.True (result);
  174. Assert.Equal (1, hotPos);
  175. Assert.Equal ((Key)'K', hotKey);
  176. text = "Last _K";
  177. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  178. Assert.True (result);
  179. Assert.Equal (5, hotPos);
  180. Assert.Equal ((Key)'K', hotKey);
  181. text = "After K_";
  182. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  183. Assert.False (result);
  184. Assert.Equal (-1, hotPos);
  185. Assert.Equal (Key.Unknown, hotKey);
  186. text = "Multiple _K and _R";
  187. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  188. Assert.True (result);
  189. Assert.Equal (9, hotPos);
  190. Assert.Equal ((Key)'K', hotKey);
  191. // Cryllic K (К)
  192. text = "Non-english: _Кдать";
  193. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  194. Assert.True (result);
  195. Assert.Equal (13, hotPos);
  196. Assert.Equal ((Key)'К', hotKey);
  197. }
  198. [Fact]
  199. public void FindHotKey_AlphaLowerCase_Succeeds ()
  200. {
  201. var text = ustring.Empty;
  202. Rune hotKeySpecifier = '_';
  203. bool supportFirstUpperCase = false;
  204. int hotPos = 0;
  205. Key hotKey = Key.Unknown;
  206. bool result = false;
  207. // lower case should return uppercase Hotkey
  208. text = "_k Before";
  209. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  210. Assert.True (result);
  211. Assert.Equal (0, hotPos);
  212. Assert.Equal ((Key)'K', hotKey);
  213. text = "a_k Second";
  214. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  215. Assert.True (result);
  216. Assert.Equal (1, hotPos);
  217. Assert.Equal ((Key)'K', hotKey);
  218. text = "Last _k";
  219. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  220. Assert.True (result);
  221. Assert.Equal (5, hotPos);
  222. Assert.Equal ((Key)'K', hotKey);
  223. text = "After k_";
  224. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  225. Assert.False (result);
  226. Assert.Equal (-1, hotPos);
  227. Assert.Equal (Key.Unknown, hotKey);
  228. text = "Multiple _k and _R";
  229. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  230. Assert.True (result);
  231. Assert.Equal (9, hotPos);
  232. Assert.Equal ((Key)'K', hotKey);
  233. // Lower case Cryllic K (к)
  234. text = "Non-english: _кдать";
  235. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  236. Assert.True (result);
  237. Assert.Equal (13, hotPos);
  238. Assert.Equal ((Key)'К', hotKey);
  239. // Turn on FirstUpperCase and verify same results
  240. supportFirstUpperCase = true;
  241. text = "_k Before";
  242. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  243. Assert.True (result);
  244. Assert.Equal (0, hotPos);
  245. Assert.Equal ((Key)'K', hotKey);
  246. text = "a_k Second";
  247. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  248. Assert.True (result);
  249. Assert.Equal (1, hotPos);
  250. Assert.Equal ((Key)'K', hotKey);
  251. text = "Last _k";
  252. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  253. Assert.True (result);
  254. Assert.Equal (5, hotPos);
  255. Assert.Equal ((Key)'K', hotKey);
  256. text = "After k_";
  257. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  258. Assert.False (result);
  259. Assert.Equal (-1, hotPos);
  260. Assert.Equal (Key.Unknown, hotKey);
  261. text = "Multiple _k and _R";
  262. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  263. Assert.True (result);
  264. Assert.Equal (9, hotPos);
  265. Assert.Equal ((Key)'K', hotKey);
  266. // Lower case Cryllic K (к)
  267. text = "Non-english: _кдать";
  268. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  269. Assert.True (result);
  270. Assert.Equal (13, hotPos);
  271. Assert.Equal ((Key)'К', hotKey);
  272. }
  273. [Fact]
  274. public void FindHotKey_Numeric_Succeeds ()
  275. {
  276. var text = ustring.Empty;
  277. Rune hotKeySpecifier = '_';
  278. bool supportFirstUpperCase = false;
  279. int hotPos = 0;
  280. Key hotKey = Key.Unknown;
  281. bool result = false;
  282. // Digits
  283. text = "_1 Before";
  284. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  285. Assert.True (result);
  286. Assert.Equal (0, hotPos);
  287. Assert.Equal ((Key)'1', hotKey);
  288. text = "a_1 Second";
  289. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  290. Assert.True (result);
  291. Assert.Equal (1, hotPos);
  292. Assert.Equal ((Key)'1', hotKey);
  293. text = "Last _1";
  294. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  295. Assert.True (result);
  296. Assert.Equal (5, hotPos);
  297. Assert.Equal ((Key)'1', hotKey);
  298. text = "After 1_";
  299. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  300. Assert.False (result);
  301. Assert.Equal (-1, hotPos);
  302. Assert.Equal (Key.Unknown, hotKey);
  303. text = "Multiple _1 and _2";
  304. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  305. Assert.True (result);
  306. Assert.Equal (9, hotPos);
  307. Assert.Equal ((Key)'1', hotKey);
  308. // Turn on FirstUpperCase and verify same results
  309. supportFirstUpperCase = true;
  310. text = "_1 Before";
  311. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  312. Assert.True (result);
  313. Assert.Equal (0, hotPos);
  314. Assert.Equal ((Key)'1', hotKey);
  315. text = "a_1 Second";
  316. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  317. Assert.True (result);
  318. Assert.Equal (1, hotPos);
  319. Assert.Equal ((Key)'1', hotKey);
  320. text = "Last _1";
  321. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  322. Assert.True (result);
  323. Assert.Equal (5, hotPos);
  324. Assert.Equal ((Key)'1', hotKey);
  325. text = "After 1_";
  326. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  327. Assert.False (result);
  328. Assert.Equal (-1, hotPos);
  329. Assert.Equal (Key.Unknown, hotKey);
  330. text = "Multiple _1 and _2";
  331. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  332. Assert.True (result);
  333. Assert.Equal (9, hotPos);
  334. Assert.Equal ((Key)'1', hotKey);
  335. }
  336. [Fact]
  337. public void FindHotKey_Legacy_FirstUpperCase_Succeeds ()
  338. {
  339. bool supportFirstUpperCase = true;
  340. var text = ustring.Empty;
  341. Rune hotKeySpecifier = (Rune)0;
  342. int hotPos = 0;
  343. Key hotKey = Key.Unknown;
  344. bool result = false;
  345. text = "K Before";
  346. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  347. Assert.True (result);
  348. Assert.Equal (0, hotPos);
  349. Assert.Equal ((Key)'K', hotKey);
  350. text = "aK Second";
  351. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  352. Assert.True (result);
  353. Assert.Equal (1, hotPos);
  354. Assert.Equal ((Key)'K', hotKey);
  355. text = "last K";
  356. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  357. Assert.True (result);
  358. Assert.Equal (5, hotPos);
  359. Assert.Equal ((Key)'K', hotKey);
  360. text = "multiple K and R";
  361. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  362. Assert.True (result);
  363. Assert.Equal (9, hotPos);
  364. Assert.Equal ((Key)'K', hotKey);
  365. // Cryllic K (К)
  366. text = "non-english: Кдать";
  367. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  368. Assert.True (result);
  369. Assert.Equal (13, hotPos);
  370. Assert.Equal ((Key)'К', hotKey);
  371. }
  372. [Fact]
  373. public void FindHotKey_Legacy_FirstUpperCase_NotFound_Returns_False ()
  374. {
  375. bool supportFirstUpperCase = true;
  376. var text = ustring.Empty;
  377. Rune hotKeySpecifier = (Rune)0;
  378. int hotPos = 0;
  379. Key hotKey = Key.Unknown;
  380. bool result = false;
  381. text = "k before";
  382. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  383. Assert.False (result);
  384. Assert.Equal (-1, hotPos);
  385. Assert.Equal (Key.Unknown, hotKey);
  386. text = "ak second";
  387. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  388. Assert.False (result);
  389. Assert.Equal (-1, hotPos);
  390. Assert.Equal (Key.Unknown, hotKey);
  391. text = "last k";
  392. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  393. Assert.False (result);
  394. Assert.Equal (-1, hotPos);
  395. Assert.Equal (Key.Unknown, hotKey);
  396. text = "multiple k and r";
  397. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  398. Assert.False (result);
  399. Assert.Equal (-1, hotPos);
  400. Assert.Equal (Key.Unknown, hotKey);
  401. text = "12345";
  402. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  403. Assert.False (result);
  404. Assert.Equal (-1, hotPos);
  405. Assert.Equal (Key.Unknown, hotKey);
  406. // punctuation
  407. text = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
  408. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  409. Assert.False (result);
  410. Assert.Equal (-1, hotPos);
  411. Assert.Equal (Key.Unknown, hotKey);
  412. // ~IsLetterOrDigit + Unicode
  413. text = " ~  s  gui.cs   master ↑10";
  414. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  415. Assert.False (result);
  416. Assert.Equal (-1, hotPos);
  417. Assert.Equal (Key.Unknown, hotKey);
  418. // Lower case Cryllic K (к)
  419. text = "non-english: кдать";
  420. result = TextFormatter.FindHotKey (text, hotKeySpecifier, supportFirstUpperCase, out hotPos, out hotKey);
  421. Assert.False (result);
  422. Assert.Equal (-1, hotPos);
  423. Assert.Equal (Key.Unknown, hotKey);
  424. }
  425. static ustring testHotKeyAtStart = "_K Before";
  426. static ustring testHotKeyAtSecondPos = "a_K Second";
  427. static ustring testHotKeyAtLastPos = "Last _K";
  428. static ustring testHotKeyAfterLastChar = "After K_";
  429. static ustring testMultiHotKeys = "Multiple _K and _R";
  430. static ustring testNonEnglish = "Non-english: _Кдать";
  431. [Fact]
  432. public void RemoveHotKeySpecifier_InValid_ReturnsOriginal ()
  433. {
  434. Rune hotKeySpecifier = '_';
  435. Assert.Null (TextFormatter.RemoveHotKeySpecifier (null, 0, hotKeySpecifier));
  436. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 0, hotKeySpecifier));
  437. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", -1, hotKeySpecifier));
  438. Assert.Equal ("", TextFormatter.RemoveHotKeySpecifier ("", 100, hotKeySpecifier));
  439. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", -1, hotKeySpecifier));
  440. Assert.Equal ("a", TextFormatter.RemoveHotKeySpecifier ("a", 100, hotKeySpecifier));
  441. }
  442. [Fact]
  443. public void RemoveHotKeySpecifier_Valid_ReturnsStripped ()
  444. {
  445. Rune hotKeySpecifier = '_';
  446. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("_K Before", 0, hotKeySpecifier));
  447. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("a_K Second", 1, hotKeySpecifier));
  448. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last _K", 5, hotKeySpecifier));
  449. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K_", 7, hotKeySpecifier));
  450. Assert.Equal ("Multiple K and _R", TextFormatter.RemoveHotKeySpecifier ("Multiple _K and _R", 9, hotKeySpecifier));
  451. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: _Кдать", 13, hotKeySpecifier));
  452. }
  453. [Fact]
  454. public void RemoveHotKeySpecifier_Valid_Legacy_ReturnsOriginal ()
  455. {
  456. Rune hotKeySpecifier = '_';
  457. Assert.Equal ("all lower case", TextFormatter.RemoveHotKeySpecifier ("all lower case", 0, hotKeySpecifier));
  458. Assert.Equal ("K Before", TextFormatter.RemoveHotKeySpecifier ("K Before", 0, hotKeySpecifier));
  459. Assert.Equal ("aK Second", TextFormatter.RemoveHotKeySpecifier ("aK Second", 1, hotKeySpecifier));
  460. Assert.Equal ("Last K", TextFormatter.RemoveHotKeySpecifier ("Last K", 5, hotKeySpecifier));
  461. Assert.Equal ("After K", TextFormatter.RemoveHotKeySpecifier ("After K", 7, hotKeySpecifier));
  462. Assert.Equal ("Multiple K and R", TextFormatter.RemoveHotKeySpecifier ("Multiple K and R", 9, hotKeySpecifier));
  463. Assert.Equal ("Non-english: Кдать", TextFormatter.RemoveHotKeySpecifier ("Non-english: Кдать", 13, hotKeySpecifier));
  464. }
  465. [Fact]
  466. public void CalcRect_Invalid_Returns_Empty ()
  467. {
  468. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, null));
  469. Assert.Equal (Rect.Empty, TextFormatter.CalcRect (0, 0, ""));
  470. Assert.Equal (new Rect (new Point (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, ""));
  471. Assert.Equal (new Rect (new Point (-1, -2), Size.Empty), TextFormatter.CalcRect (-1, -2, ""));
  472. }
  473. [Fact]
  474. public void CalcRect_SingleLine_Returns_1High ()
  475. {
  476. var text = ustring.Empty;
  477. text = "test";
  478. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  479. text = " ~  s  gui.cs   master ↑10";
  480. Assert.Equal (new Rect (0, 0, text.RuneCount, 1), TextFormatter.CalcRect (0, 0, text));
  481. }
  482. [Fact]
  483. public void CalcRect_MultiLine_Returns_nHigh ()
  484. {
  485. var text = ustring.Empty;
  486. var lines = 0;
  487. text = "line1\nline2";
  488. lines = 2;
  489. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  490. text = "\nline2";
  491. lines = 2;
  492. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  493. text = "\n\n";
  494. lines = 3;
  495. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  496. text = "\n\n\n";
  497. lines = 4;
  498. Assert.Equal (new Rect (0, 0, 0, lines), TextFormatter.CalcRect (0, 0, text));
  499. text = "line1\nline2\nline3long!";
  500. lines = 3;
  501. Assert.Equal (new Rect (0, 0, 10, lines), TextFormatter.CalcRect (0, 0, text));
  502. text = "line1\nline2\n\n";
  503. lines = 4;
  504. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  505. text = "line1\r\nline2";
  506. lines = 2;
  507. Assert.Equal (new Rect (0, 0, 5, lines), TextFormatter.CalcRect (0, 0, text));
  508. text = " ~  s  gui.cs   master ↑10\n";
  509. lines = 2;
  510. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  511. text = "\n ~  s  gui.cs   master ↑10";
  512. lines = 2;
  513. Assert.Equal (new Rect (0, 0, text.RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  514. text = " ~  s  gui.cs   master\n↑10";
  515. lines = 2;
  516. Assert.Equal (new Rect (0, 0, ustring.Make (" ~  s  gui.cs   master\n").RuneCount - 1, lines), TextFormatter.CalcRect (0, 0, text));
  517. }
  518. [Fact]
  519. public void ClipAndJustify_Invalid_Returns_Original ()
  520. {
  521. var text = ustring.Empty;
  522. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  523. text = null;
  524. Assert.Equal (text, TextFormatter.ClipAndJustify (text, 0, TextAlignment.Left));
  525. text = "test";
  526. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.ClipAndJustify (text, -1, TextAlignment.Left));
  527. }
  528. [Fact]
  529. public void ClipAndJustify_Valid_Left ()
  530. {
  531. var align = TextAlignment.Left;
  532. var text = ustring.Empty;
  533. var justifiedText = ustring.Empty;
  534. int maxWidth = 0;
  535. int expectedClippedWidth = 0;
  536. text = "test";
  537. maxWidth = 0;
  538. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  539. text = "test";
  540. maxWidth = 2;
  541. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  542. text = "test";
  543. maxWidth = int.MaxValue;
  544. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  545. Assert.True (justifiedText.RuneCount <= maxWidth);
  546. text = "A sentence has words.";
  547. // should fit
  548. maxWidth = text.RuneCount + 1;
  549. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  550. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  551. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  552. Assert.True (expectedClippedWidth <= maxWidth);
  553. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  554. // Should fit.
  555. maxWidth = text.RuneCount + 0;
  556. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  557. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  558. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  559. Assert.True (expectedClippedWidth <= maxWidth);
  560. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  561. // Should fit.
  562. maxWidth = int.MaxValue;
  563. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  564. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  565. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  566. Assert.True (expectedClippedWidth <= maxWidth);
  567. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  568. // Should not fit
  569. maxWidth = text.RuneCount - 1;
  570. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  571. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  572. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  573. Assert.True (expectedClippedWidth <= maxWidth);
  574. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  575. // Should not fit
  576. maxWidth = 10;
  577. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  578. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  579. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  580. Assert.True (expectedClippedWidth <= maxWidth);
  581. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  582. text = "A\tsentence\thas\twords.";
  583. maxWidth = int.MaxValue;
  584. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  585. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  586. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  587. Assert.True (expectedClippedWidth <= maxWidth);
  588. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  589. text = "A\tsentence\thas\twords.";
  590. maxWidth = 10;
  591. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  592. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  593. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  594. Assert.True (expectedClippedWidth <= maxWidth);
  595. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  596. text = "line1\nline2\nline3long!";
  597. maxWidth = int.MaxValue;
  598. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  599. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  600. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  601. Assert.True (expectedClippedWidth <= maxWidth);
  602. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  603. text = "line1\nline2\nline3long!";
  604. maxWidth = 10;
  605. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  606. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  607. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  608. Assert.True (expectedClippedWidth <= maxWidth);
  609. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  610. // Unicode
  611. text = " ~  s  gui.cs   master ↑10";
  612. maxWidth = 10;
  613. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  614. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  615. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  616. Assert.True (expectedClippedWidth <= maxWidth);
  617. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  618. // should fit
  619. text = "Ð ÑÐ";
  620. maxWidth = text.RuneCount + 1;
  621. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  622. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  623. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  624. Assert.True (expectedClippedWidth <= maxWidth);
  625. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  626. // Should fit.
  627. maxWidth = text.RuneCount + 0;
  628. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  629. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  630. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  631. Assert.True (expectedClippedWidth <= maxWidth);
  632. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  633. // Should not fit
  634. maxWidth = text.RuneCount - 1;
  635. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  636. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  637. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  638. Assert.True (expectedClippedWidth <= maxWidth);
  639. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  640. }
  641. [Fact]
  642. public void ClipAndJustify_Valid_Right ()
  643. {
  644. var align = TextAlignment.Right;
  645. var text = ustring.Empty;
  646. var justifiedText = ustring.Empty;
  647. int maxWidth = 0;
  648. int expectedClippedWidth = 0;
  649. text = "test";
  650. maxWidth = 0;
  651. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  652. text = "test";
  653. maxWidth = 2;
  654. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  655. text = "test";
  656. maxWidth = int.MaxValue;
  657. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  658. Assert.True (justifiedText.RuneCount <= maxWidth);
  659. text = "A sentence has words.";
  660. // should fit
  661. maxWidth = text.RuneCount + 1;
  662. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  663. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  664. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  665. Assert.True (expectedClippedWidth <= maxWidth);
  666. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  667. // Should fit.
  668. maxWidth = text.RuneCount + 0;
  669. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  670. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  671. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  672. Assert.True (expectedClippedWidth <= maxWidth);
  673. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  674. // Should fit.
  675. maxWidth = int.MaxValue;
  676. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  677. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  678. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  679. Assert.True (expectedClippedWidth <= maxWidth);
  680. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  681. // Should not fit
  682. maxWidth = text.RuneCount - 1;
  683. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  684. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  685. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  686. Assert.True (expectedClippedWidth <= maxWidth);
  687. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  688. // Should not fit
  689. maxWidth = 10;
  690. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  691. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  692. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  693. Assert.True (expectedClippedWidth <= maxWidth);
  694. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  695. text = "A\tsentence\thas\twords.";
  696. maxWidth = int.MaxValue;
  697. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  698. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  699. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  700. Assert.True (expectedClippedWidth <= maxWidth);
  701. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  702. text = "A\tsentence\thas\twords.";
  703. maxWidth = 10;
  704. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  705. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  706. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  707. Assert.True (expectedClippedWidth <= maxWidth);
  708. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  709. text = "line1\nline2\nline3long!";
  710. maxWidth = int.MaxValue;
  711. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  712. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  713. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  714. Assert.True (expectedClippedWidth <= maxWidth);
  715. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  716. text = "line1\nline2\nline3long!";
  717. maxWidth = 10;
  718. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  719. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  720. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  721. Assert.True (expectedClippedWidth <= maxWidth);
  722. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  723. // Unicode
  724. text = " ~  s  gui.cs   master ↑10";
  725. maxWidth = 10;
  726. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  727. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  728. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  729. Assert.True (expectedClippedWidth <= maxWidth);
  730. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  731. // should fit
  732. text = "Ð ÑÐ";
  733. maxWidth = text.RuneCount + 1;
  734. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  735. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  736. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  737. Assert.True (expectedClippedWidth <= maxWidth);
  738. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  739. // Should fit.
  740. maxWidth = text.RuneCount + 0;
  741. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  742. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  743. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  744. Assert.True (expectedClippedWidth <= maxWidth);
  745. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  746. // Should not fit
  747. maxWidth = text.RuneCount - 1;
  748. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  749. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  750. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  751. Assert.True (expectedClippedWidth <= maxWidth);
  752. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  753. }
  754. [Fact]
  755. public void ClipAndJustify_Valid_Centered ()
  756. {
  757. var align = TextAlignment.Centered;
  758. var text = ustring.Empty;
  759. var justifiedText = ustring.Empty;
  760. int maxWidth = 0;
  761. int expectedClippedWidth = 0;
  762. text = "test";
  763. maxWidth = 0;
  764. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  765. text = "test";
  766. maxWidth = 2;
  767. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  768. text = "test";
  769. maxWidth = int.MaxValue;
  770. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  771. Assert.True (justifiedText.RuneCount <= maxWidth);
  772. text = "A sentence has words.";
  773. // should fit
  774. maxWidth = text.RuneCount + 1;
  775. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  776. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  777. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  778. Assert.True (expectedClippedWidth <= maxWidth);
  779. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  780. // Should fit.
  781. maxWidth = text.RuneCount + 0;
  782. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  783. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  784. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  785. Assert.True (expectedClippedWidth <= maxWidth);
  786. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  787. // Should fit.
  788. maxWidth = int.MaxValue;
  789. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  790. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  791. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  792. Assert.True (expectedClippedWidth <= maxWidth);
  793. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  794. // Should not fit
  795. maxWidth = text.RuneCount - 1;
  796. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  797. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  798. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  799. Assert.True (expectedClippedWidth <= maxWidth);
  800. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  801. // Should not fit
  802. maxWidth = 10;
  803. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  804. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  805. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  806. Assert.True (expectedClippedWidth <= maxWidth);
  807. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  808. text = "A\tsentence\thas\twords.";
  809. maxWidth = int.MaxValue;
  810. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  811. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  812. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  813. Assert.True (expectedClippedWidth <= maxWidth);
  814. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  815. text = "A\tsentence\thas\twords.";
  816. maxWidth = 10;
  817. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  818. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  819. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  820. Assert.True (expectedClippedWidth <= maxWidth);
  821. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  822. text = "line1\nline2\nline3long!";
  823. maxWidth = int.MaxValue;
  824. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  825. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  826. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  827. Assert.True (expectedClippedWidth <= maxWidth);
  828. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  829. text = "line1\nline2\nline3long!";
  830. maxWidth = 10;
  831. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  832. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  833. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  834. Assert.True (expectedClippedWidth <= maxWidth);
  835. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  836. // Unicode
  837. text = " ~  s  gui.cs   master ↑10";
  838. maxWidth = 10;
  839. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  840. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  841. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  842. Assert.True (expectedClippedWidth <= maxWidth);
  843. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  844. // should fit
  845. text = "Ð ÑÐ";
  846. maxWidth = text.RuneCount + 1;
  847. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  848. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  849. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  850. Assert.True (expectedClippedWidth <= maxWidth);
  851. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  852. // Should fit.
  853. maxWidth = text.RuneCount + 0;
  854. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  855. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  856. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  857. Assert.True (expectedClippedWidth <= maxWidth);
  858. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  859. // Should not fit
  860. maxWidth = text.RuneCount - 1;
  861. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  862. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  863. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  864. Assert.True (expectedClippedWidth <= maxWidth);
  865. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  866. }
  867. [Fact]
  868. public void ClipAndJustify_Valid_Justified ()
  869. {
  870. var align = TextAlignment.Justified;
  871. var text = ustring.Empty;
  872. var justifiedText = ustring.Empty;
  873. int maxWidth = 0;
  874. int expectedClippedWidth = 0;
  875. text = "test";
  876. maxWidth = 0;
  877. Assert.Equal (ustring.Empty, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  878. text = "test";
  879. maxWidth = 2;
  880. Assert.Equal (text.ToRunes () [0..maxWidth], justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  881. text = "test";
  882. maxWidth = int.MaxValue;
  883. Assert.Equal (text, justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align));
  884. Assert.True (justifiedText.RuneCount <= maxWidth);
  885. text = "A sentence has words.";
  886. // should fit
  887. maxWidth = text.RuneCount + 1;
  888. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  889. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  890. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  891. Assert.True (expectedClippedWidth <= maxWidth);
  892. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  893. // Should fit.
  894. maxWidth = text.RuneCount + 0;
  895. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  896. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  897. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  898. Assert.True (expectedClippedWidth <= maxWidth);
  899. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  900. // Should fit.
  901. maxWidth = 500;
  902. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  903. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  904. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  905. Assert.True (expectedClippedWidth <= maxWidth);
  906. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  907. // Should not fit
  908. maxWidth = text.RuneCount - 1;
  909. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  910. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  911. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  912. Assert.True (expectedClippedWidth <= maxWidth);
  913. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  914. // Should not fit
  915. maxWidth = 10;
  916. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  917. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  918. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  919. Assert.True (expectedClippedWidth <= maxWidth);
  920. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  921. text = "A\tsentence\thas\twords.";
  922. maxWidth = int.MaxValue;
  923. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  924. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  925. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  926. Assert.True (expectedClippedWidth <= maxWidth);
  927. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  928. text = "A\tsentence\thas\twords.";
  929. maxWidth = 10;
  930. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  931. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  932. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  933. Assert.True (expectedClippedWidth <= maxWidth);
  934. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  935. text = "line1\nline2\nline3long!";
  936. maxWidth = int.MaxValue;
  937. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  938. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  939. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  940. Assert.True (expectedClippedWidth <= maxWidth);
  941. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  942. text = "line1\nline2\nline3long!";
  943. maxWidth = 10;
  944. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  945. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  946. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  947. Assert.True (expectedClippedWidth <= maxWidth);
  948. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  949. // Unicode
  950. text = " ~  s  gui.cs   master ↑10";
  951. maxWidth = 10;
  952. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  953. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  954. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  955. Assert.True (expectedClippedWidth <= maxWidth);
  956. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  957. // should fit
  958. text = "Ð ÑÐ";
  959. maxWidth = text.RuneCount + 1;
  960. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  961. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  962. //Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  963. Assert.True (expectedClippedWidth <= maxWidth);
  964. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  965. // Should fit.
  966. maxWidth = text.RuneCount + 0;
  967. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  968. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  969. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  970. Assert.True (expectedClippedWidth <= maxWidth);
  971. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  972. // Should not fit
  973. maxWidth = text.RuneCount - 1;
  974. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  975. justifiedText = TextFormatter.ClipAndJustify (text, maxWidth, align);
  976. Assert.Equal (expectedClippedWidth, justifiedText.RuneCount);
  977. Assert.True (expectedClippedWidth <= maxWidth);
  978. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), justifiedText);
  979. // see Justify_ tests below
  980. }
  981. [Fact]
  982. public void Justify_Invalid ()
  983. {
  984. var text = ustring.Empty;
  985. Assert.Equal (text, TextFormatter.Justify (text, 0));
  986. text = null;
  987. Assert.Equal (text, TextFormatter.Justify (text, 0));
  988. text = "test";
  989. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Justify (text, -1));
  990. }
  991. [Fact]
  992. public void Justify_SingleWord ()
  993. {
  994. var text = ustring.Empty;
  995. var justifiedText = ustring.Empty;
  996. int width = 0;
  997. char fillChar = '+';
  998. // Even # of chars
  999. text = "word";
  1000. justifiedText = text;
  1001. width = text.RuneCount;
  1002. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1003. width = text.RuneCount + 1;
  1004. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1005. width = text.RuneCount + 2;
  1006. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1007. width = text.RuneCount + 10;
  1008. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1009. width = text.RuneCount + 11;
  1010. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1011. // Odd # of chars
  1012. text = "word.";
  1013. justifiedText = text;
  1014. width = text.RuneCount;
  1015. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1016. width = text.RuneCount + 1;
  1017. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1018. width = text.RuneCount + 2;
  1019. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1020. width = text.RuneCount + 10;
  1021. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1022. width = text.RuneCount + 11;
  1023. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1024. // Unicode (even #)
  1025. text = "пÑивеÑ";
  1026. justifiedText = text;
  1027. width = text.RuneCount;
  1028. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1029. width = text.RuneCount + 1;
  1030. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1031. width = text.RuneCount + 2;
  1032. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1033. width = text.RuneCount + 10;
  1034. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1035. width = text.RuneCount + 11;
  1036. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1037. // Unicode (odd # of chars)
  1038. text = "пÑивеÑ.";
  1039. justifiedText = text;
  1040. width = text.RuneCount;
  1041. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1042. width = text.RuneCount + 1;
  1043. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1044. width = text.RuneCount + 2;
  1045. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1046. width = text.RuneCount + 10;
  1047. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1048. width = text.RuneCount + 11;
  1049. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, width, fillChar).ToString ());
  1050. }
  1051. [Fact]
  1052. public void Justify_Sentence ()
  1053. {
  1054. var text = ustring.Empty;
  1055. var justifiedText = ustring.Empty;
  1056. int forceToWidth = 0;
  1057. char fillChar = '+';
  1058. // Even # of spaces
  1059. // 0123456789
  1060. text = "012 456 89";
  1061. forceToWidth = text.RuneCount;
  1062. justifiedText = text.Replace (" ", "+");
  1063. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1064. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1065. justifiedText = text.Replace (" ", "+");
  1066. forceToWidth = text.RuneCount + 1;
  1067. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1068. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1069. justifiedText = text.Replace (" ", "++");
  1070. forceToWidth = text.RuneCount + 2;
  1071. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1072. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1073. justifiedText = text.Replace (" ", "++");
  1074. forceToWidth = text.RuneCount + 3;
  1075. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1076. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1077. justifiedText = text.Replace (" ", "+++");
  1078. forceToWidth = text.RuneCount + 4;
  1079. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1080. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1081. justifiedText = text.Replace (" ", "+++");
  1082. forceToWidth = text.RuneCount + 5;
  1083. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1084. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1085. justifiedText = text.Replace (" ", "++++");
  1086. forceToWidth = text.RuneCount + 6;
  1087. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1088. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1089. justifiedText = text.Replace (" ", "+++++++++++");
  1090. forceToWidth = text.RuneCount + 20;
  1091. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1092. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1093. justifiedText = text.Replace (" ", "++++++++++++");
  1094. forceToWidth = text.RuneCount + 23;
  1095. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1096. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1097. // Odd # of spaces
  1098. // 0123456789
  1099. text = "012 456 89 end";
  1100. forceToWidth = text.RuneCount;
  1101. justifiedText = text.Replace (" ", "+");
  1102. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1103. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1104. justifiedText = text.Replace (" ", "+");
  1105. forceToWidth = text.RuneCount + 1;
  1106. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1107. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1108. justifiedText = text.Replace (" ", "+");
  1109. forceToWidth = text.RuneCount + 2;
  1110. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1111. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1112. justifiedText = text.Replace (" ", "++");
  1113. forceToWidth = text.RuneCount + 3;
  1114. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1115. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1116. justifiedText = text.Replace (" ", "++");
  1117. forceToWidth = text.RuneCount + 4;
  1118. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1119. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1120. justifiedText = text.Replace (" ", "++");
  1121. forceToWidth = text.RuneCount + 5;
  1122. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1123. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1124. justifiedText = text.Replace (" ", "+++");
  1125. forceToWidth = text.RuneCount + 6;
  1126. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1127. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1128. justifiedText = text.Replace (" ", "+++++++");
  1129. forceToWidth = text.RuneCount + 20;
  1130. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1131. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1132. justifiedText = text.Replace (" ", "++++++++");
  1133. forceToWidth = text.RuneCount + 23;
  1134. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1135. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1136. // Unicode
  1137. // Even # of chars
  1138. // 0123456789
  1139. text = "пÑРвРÑ";
  1140. forceToWidth = text.RuneCount;
  1141. justifiedText = text.Replace (" ", "+");
  1142. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1143. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1144. justifiedText = text.Replace (" ", "+");
  1145. forceToWidth = text.RuneCount + 1;
  1146. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1147. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1148. justifiedText = text.Replace (" ", "++");
  1149. forceToWidth = text.RuneCount + 2;
  1150. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1151. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1152. justifiedText = text.Replace (" ", "++");
  1153. forceToWidth = text.RuneCount + 3;
  1154. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1155. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1156. justifiedText = text.Replace (" ", "+++");
  1157. forceToWidth = text.RuneCount + 4;
  1158. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1159. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1160. justifiedText = text.Replace (" ", "+++");
  1161. forceToWidth = text.RuneCount + 5;
  1162. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1163. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1164. justifiedText = text.Replace (" ", "++++");
  1165. forceToWidth = text.RuneCount + 6;
  1166. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1167. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1168. justifiedText = text.Replace (" ", "+++++++++++");
  1169. forceToWidth = text.RuneCount + 20;
  1170. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1171. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1172. justifiedText = text.Replace (" ", "++++++++++++");
  1173. forceToWidth = text.RuneCount + 23;
  1174. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1175. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1176. // Unicode
  1177. // Odd # of chars
  1178. // 0123456789
  1179. text = "Ð ÑРвРÑ";
  1180. forceToWidth = text.RuneCount;
  1181. justifiedText = text.Replace (" ", "+");
  1182. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1183. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1184. justifiedText = text.Replace (" ", "+");
  1185. forceToWidth = text.RuneCount + 1;
  1186. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1187. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1188. justifiedText = text.Replace (" ", "+");
  1189. forceToWidth = text.RuneCount + 2;
  1190. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1191. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1192. justifiedText = text.Replace (" ", "++");
  1193. forceToWidth = text.RuneCount + 3;
  1194. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1195. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1196. justifiedText = text.Replace (" ", "++");
  1197. forceToWidth = text.RuneCount + 4;
  1198. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1199. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1200. justifiedText = text.Replace (" ", "++");
  1201. forceToWidth = text.RuneCount + 5;
  1202. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1203. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1204. justifiedText = text.Replace (" ", "+++");
  1205. forceToWidth = text.RuneCount + 6;
  1206. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1207. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1208. justifiedText = text.Replace (" ", "+++++++");
  1209. forceToWidth = text.RuneCount + 20;
  1210. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1211. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1212. justifiedText = text.Replace (" ", "++++++++");
  1213. forceToWidth = text.RuneCount + 23;
  1214. Assert.Equal (justifiedText.ToString (), TextFormatter.Justify (text, forceToWidth, fillChar).ToString ());
  1215. Assert.True (Math.Abs (forceToWidth - justifiedText.RuneCount) < text.Count (" "));
  1216. }
  1217. [Fact]
  1218. public void WordWrap_Invalid ()
  1219. {
  1220. var text = ustring.Empty;
  1221. int width = 0;
  1222. Assert.Empty (TextFormatter.WordWrap (null, width));
  1223. Assert.Empty (TextFormatter.WordWrap (text, width));
  1224. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.WordWrap (text, -1));
  1225. }
  1226. [Fact]
  1227. public void WordWrap_SingleWordLine ()
  1228. {
  1229. var text = ustring.Empty;
  1230. int width = 0;
  1231. List<ustring> wrappedLines;
  1232. text = "Constantinople";
  1233. width = text.RuneCount;
  1234. wrappedLines = TextFormatter.WordWrap (text, width);
  1235. Assert.True (wrappedLines.Count == 1);
  1236. width = text.RuneCount - 1;
  1237. wrappedLines = TextFormatter.WordWrap (text, width);
  1238. Assert.Equal (2, wrappedLines.Count);
  1239. Assert.Equal (text [0, text.RuneCount - 1].ToString (), wrappedLines [0].ToString ());
  1240. Assert.Equal ("e", wrappedLines [1].ToString ());
  1241. width = text.RuneCount - 2;
  1242. wrappedLines = TextFormatter.WordWrap (text, width);
  1243. Assert.Equal (2, wrappedLines.Count);
  1244. Assert.Equal (text [0, text.RuneCount - 2].ToString (), wrappedLines [0].ToString ());
  1245. width = text.RuneCount - 5;
  1246. wrappedLines = TextFormatter.WordWrap (text, width);
  1247. Assert.Equal (2, wrappedLines.Count);
  1248. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1249. wrappedLines = TextFormatter.WordWrap (text, width);
  1250. Assert.Equal (2, wrappedLines.Count);
  1251. Assert.Equal ("Constan", wrappedLines [0].ToString ());
  1252. Assert.Equal ("tinople", wrappedLines [1].ToString ());
  1253. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1254. wrappedLines = TextFormatter.WordWrap (text, width);
  1255. Assert.Equal (3, wrappedLines.Count);
  1256. Assert.Equal ("Const", wrappedLines [0].ToString ());
  1257. Assert.Equal ("antin", wrappedLines [1].ToString ());
  1258. Assert.Equal ("ople", wrappedLines [2].ToString ());
  1259. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1260. wrappedLines = TextFormatter.WordWrap (text, width);
  1261. Assert.Equal (4, wrappedLines.Count);
  1262. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1263. wrappedLines = TextFormatter.WordWrap (text, width);
  1264. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1265. Assert.Equal ("C", wrappedLines [0].ToString ());
  1266. Assert.Equal ("o", wrappedLines [1].ToString ());
  1267. Assert.Equal ("n", wrappedLines [2].ToString ());
  1268. Assert.Equal ("s", wrappedLines [3].ToString ());
  1269. Assert.Equal ("t", wrappedLines [4].ToString ());
  1270. Assert.Equal ("a", wrappedLines [5].ToString ());
  1271. Assert.Equal ("n", wrappedLines [6].ToString ());
  1272. Assert.Equal ("t", wrappedLines [7].ToString ());
  1273. Assert.Equal ("i", wrappedLines [8].ToString ());
  1274. Assert.Equal ("n", wrappedLines [9].ToString ());
  1275. Assert.Equal ("o", wrappedLines [10].ToString ());
  1276. Assert.Equal ("p", wrappedLines [11].ToString ());
  1277. Assert.Equal ("l", wrappedLines [12].ToString ());
  1278. Assert.Equal ("e", wrappedLines [13].ToString ());
  1279. }
  1280. [Fact]
  1281. public void WordWrap_Unicode_SingleWordLine ()
  1282. {
  1283. var text = ustring.Empty;
  1284. int width = 0;
  1285. List<ustring> wrappedLines;
  1286. text = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ";
  1287. width = text.RuneCount;
  1288. wrappedLines = TextFormatter.WordWrap (text, width);
  1289. Assert.True (wrappedLines.Count == 1);
  1290. width = text.RuneCount - 1;
  1291. wrappedLines = TextFormatter.WordWrap (text, width);
  1292. Assert.Equal (2, wrappedLines.Count);
  1293. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1294. Assert.Equal ("ำ", wrappedLines [1].ToString ());
  1295. width = text.RuneCount - 2;
  1296. wrappedLines = TextFormatter.WordWrap (text, width);
  1297. Assert.Equal (2, wrappedLines.Count);
  1298. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1299. width = text.RuneCount - 5;
  1300. wrappedLines = TextFormatter.WordWrap (text, width);
  1301. Assert.Equal (2, wrappedLines.Count);
  1302. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1303. wrappedLines = TextFormatter.WordWrap (text, width);
  1304. Assert.Equal (2, wrappedLines.Count);
  1305. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบ", wrappedLines [0].ToString ());
  1306. Assert.Equal ("ปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [1].ToString ());
  1307. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1308. wrappedLines = TextFormatter.WordWrap (text, width);
  1309. Assert.Equal (3, wrappedLines.Count);
  1310. Assert.Equal ("กขฃคฅฆงจฉชซฌญฎฏฐฑ", wrappedLines [0].ToString ());
  1311. Assert.Equal ("ฒณดตถทธนบปผฝพฟภมย", wrappedLines [1].ToString ());
  1312. Assert.Equal ("รฤลฦวศษสหฬอฮฯะัาำ", wrappedLines [2].ToString ());
  1313. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1314. wrappedLines = TextFormatter.WordWrap (text, width);
  1315. Assert.Equal (4, wrappedLines.Count);
  1316. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1317. wrappedLines = TextFormatter.WordWrap (text, width);
  1318. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1319. Assert.Equal ("ก", wrappedLines [0].ToString ());
  1320. Assert.Equal ("ข", wrappedLines [1].ToString ());
  1321. Assert.Equal ("ฃ", wrappedLines [2].ToString ());
  1322. Assert.Equal ("ำ", wrappedLines [^1].ToString ());
  1323. }
  1324. [Fact]
  1325. public void WordWrap_Unicode_LineWithNonBreakingSpace ()
  1326. {
  1327. var text = ustring.Empty;
  1328. int width = 0;
  1329. List<ustring> wrappedLines;
  1330. text = "This\u00A0is\u00A0a\u00A0sentence.";
  1331. width = text.RuneCount;
  1332. wrappedLines = TextFormatter.WordWrap (text, width);
  1333. Assert.True (wrappedLines.Count == 1);
  1334. width = text.RuneCount - 1;
  1335. wrappedLines = TextFormatter.WordWrap (text, width);
  1336. Assert.Equal (2, wrappedLines.Count);
  1337. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 1)]).ToString (), wrappedLines [0].ToString ());
  1338. Assert.Equal (".", wrappedLines [1].ToString ());
  1339. width = text.RuneCount - 2;
  1340. wrappedLines = TextFormatter.WordWrap (text, width);
  1341. Assert.Equal (2, wrappedLines.Count);
  1342. Assert.Equal (ustring.Make (text.ToRunes () [0..(text.RuneCount - 2)]).ToString (), wrappedLines [0].ToString ());
  1343. width = text.RuneCount - 5;
  1344. wrappedLines = TextFormatter.WordWrap (text, width);
  1345. Assert.Equal (2, wrappedLines.Count);
  1346. width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  1347. wrappedLines = TextFormatter.WordWrap (text, width);
  1348. Assert.Equal (2, wrappedLines.Count);
  1349. Assert.Equal ("This\u00A0is\u00A0a\u00A0", wrappedLines [0].ToString ());
  1350. Assert.Equal ("sentence.", wrappedLines [1].ToString ());
  1351. width = (int)Math.Ceiling ((double)(text.RuneCount / 3F));
  1352. wrappedLines = TextFormatter.WordWrap (text, width);
  1353. Assert.Equal (3, wrappedLines.Count);
  1354. Assert.Equal ("This\u00A0is", wrappedLines [0].ToString ());
  1355. Assert.Equal ("\u00a0a\u00a0sent", wrappedLines [1].ToString ());
  1356. Assert.Equal ("ence.", wrappedLines [2].ToString ());
  1357. width = (int)Math.Ceiling ((double)(text.RuneCount / 4F));
  1358. wrappedLines = TextFormatter.WordWrap (text, width);
  1359. Assert.Equal (4, wrappedLines.Count);
  1360. width = (int)Math.Ceiling ((double)text.RuneCount / text.RuneCount);
  1361. wrappedLines = TextFormatter.WordWrap (text, width);
  1362. Assert.Equal (text.RuneCount, wrappedLines.Count);
  1363. Assert.Equal ("T", wrappedLines [0].ToString ());
  1364. Assert.Equal ("h", wrappedLines [1].ToString ());
  1365. Assert.Equal ("i", wrappedLines [2].ToString ());
  1366. Assert.Equal (".", wrappedLines [^1].ToString ());
  1367. }
  1368. [Fact]
  1369. public void WordWrap_Unicode_2LinesWithNonBreakingSpace ()
  1370. {
  1371. var text = ustring.Empty;
  1372. int width = 0;
  1373. List<ustring> wrappedLines;
  1374. text = "This\u00A0is\n\u00A0a\u00A0sentence.";
  1375. width = text.RuneCount;
  1376. wrappedLines = TextFormatter.WordWrap (text, width);
  1377. Assert.True (wrappedLines.Count == 1);
  1378. width = text.RuneCount - 1;
  1379. wrappedLines = TextFormatter.WordWrap (text, width);
  1380. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1381. Assert.Equal (1, wrappedLines.Count);
  1382. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1383. Assert.Equal ("This\u00A0is\u00A0a\u00A0sentence.", wrappedLines [0].ToString ());
  1384. text = "\u00A0\u00A0\u00A0\u00A0\u00A0test\u00A0sentence.";
  1385. width = text.RuneCount;
  1386. wrappedLines = TextFormatter.WordWrap (text, width);
  1387. Assert.True (wrappedLines.Count == 1);
  1388. }
  1389. [Fact]
  1390. public void WordWrap_NoNewLines ()
  1391. {
  1392. var text = ustring.Empty;
  1393. int maxWidth = 0;
  1394. int expectedClippedWidth = 0;
  1395. List<ustring> wrappedLines;
  1396. text = "A sentence has words.";
  1397. maxWidth = text.RuneCount;
  1398. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1399. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1400. Assert.True (wrappedLines.Count == 1);
  1401. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1402. Assert.Equal ("A sentence has words.", wrappedLines [0].ToString ());
  1403. maxWidth = text.RuneCount - 1;
  1404. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1405. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1406. Assert.Equal (2, wrappedLines.Count);
  1407. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1408. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1409. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1410. maxWidth = text.RuneCount - "words.".Length;
  1411. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1412. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1413. Assert.Equal (2, wrappedLines.Count);
  1414. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1415. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1416. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1417. maxWidth = text.RuneCount - " words.".Length;
  1418. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1419. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1420. Assert.Equal (2, wrappedLines.Count);
  1421. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1422. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1423. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1424. maxWidth = text.RuneCount - "s words.".Length;
  1425. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1426. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1427. Assert.Equal (2, wrappedLines.Count);
  1428. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1429. Assert.Equal ("A sentence", wrappedLines [0].ToString ());
  1430. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1431. // Unicode
  1432. text = "A Unicode sentence (пÑивеÑ) has words.";
  1433. maxWidth = text.RuneCount;
  1434. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1435. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1436. Assert.True (wrappedLines.Count == 1);
  1437. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1438. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.", wrappedLines [0].ToString ());
  1439. maxWidth = text.RuneCount - 1;
  1440. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1441. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1442. Assert.Equal (2, wrappedLines.Count);
  1443. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1444. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1445. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1446. maxWidth = text.RuneCount - "words.".Length;
  1447. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1448. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1449. Assert.Equal (2, wrappedLines.Count);
  1450. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1451. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1452. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1453. maxWidth = text.RuneCount - " words.".Length;
  1454. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1455. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1456. Assert.Equal (2, wrappedLines.Count);
  1457. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1458. Assert.Equal ("A Unicode sentence (пÑивеÑ) has", wrappedLines [0].ToString ());
  1459. Assert.Equal ("words.", wrappedLines [1].ToString ());
  1460. maxWidth = text.RuneCount - "s words.".Length;
  1461. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1462. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1463. Assert.Equal (2, wrappedLines.Count);
  1464. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1465. Assert.Equal ("A Unicode sentence (пÑивеÑ)", wrappedLines [0].ToString ());
  1466. Assert.Equal ("has words.", wrappedLines [1].ToString ());
  1467. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1468. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1469. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1470. Assert.Equal (2, wrappedLines.Count);
  1471. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1472. Assert.Equal ("A Unicode sentence", wrappedLines [0].ToString ());
  1473. Assert.Equal ("(пÑивеÑ) has words.", wrappedLines [1].ToString ());
  1474. }
  1475. /// <summary>
  1476. /// WordWrap strips CRLF
  1477. /// </summary>
  1478. [Fact]
  1479. public void WordWrap_WithNewLines ()
  1480. {
  1481. var text = ustring.Empty;
  1482. int maxWidth = 0;
  1483. int expectedClippedWidth = 0;
  1484. List<ustring> wrappedLines;
  1485. text = "A sentence has words.\nA paragraph has lines.";
  1486. maxWidth = text.RuneCount;
  1487. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1488. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1489. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1490. Assert.Equal (1, wrappedLines.Count);
  1491. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1492. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1493. Assert.Equal ("A sentence has words.A paragraph has lines.", wrappedLines [0].ToString ());
  1494. maxWidth = text.RuneCount - 1;
  1495. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1496. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1497. #pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
  1498. Assert.Equal (1, wrappedLines.Count);
  1499. #pragma warning restore xUnit2013 // Do not use equality check to check for collection size.
  1500. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1501. Assert.Equal ("A sentence has words.A paragraph has lines.", wrappedLines [0].ToString ());
  1502. maxWidth = text.RuneCount - "words.".Length;
  1503. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1504. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1505. Assert.Equal (2, wrappedLines.Count);
  1506. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1507. Assert.Equal ("A sentence has words.A paragraph has", wrappedLines [0].ToString ());
  1508. Assert.Equal ("lines.", wrappedLines [1].ToString ());
  1509. // Unicode
  1510. text = "A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии.";
  1511. maxWidth = text.RuneCount;
  1512. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1513. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1514. Assert.True (wrappedLines.Count == 1);
  1515. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1516. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has Линии.", wrappedLines [0].ToString ());
  1517. maxWidth = text.RuneCount - 1;
  1518. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1519. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1520. Assert.Equal (2, wrappedLines.Count);
  1521. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1522. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", wrappedLines [0].ToString ());
  1523. Assert.Equal ("Линии.", wrappedLines [1].ToString ());
  1524. maxWidth = text.RuneCount - "words.".Length;
  1525. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1526. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1527. Assert.Equal (2, wrappedLines.Count);
  1528. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1529. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт has", wrappedLines [0].ToString ());
  1530. Assert.Equal ("Линии.", wrappedLines [1].ToString ());
  1531. maxWidth = text.RuneCount - "s words.".Length;
  1532. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1533. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1534. Assert.Equal (2, wrappedLines.Count);
  1535. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1536. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode Пункт", wrappedLines [0].ToString ());
  1537. Assert.Equal ("has Линии.", wrappedLines [1].ToString ());
  1538. maxWidth = text.RuneCount - "веÑ) has words.".Length;
  1539. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  1540. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1541. Assert.Equal (2, wrappedLines.Count);
  1542. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1543. Assert.Equal ("A Unicode sentence (пÑивеÑ) has words.A Unicode", wrappedLines [0].ToString ());
  1544. Assert.Equal ("Пункт has Линии.", wrappedLines [1].ToString ());
  1545. }
  1546. [Fact]
  1547. public void WordWrap_Narrow ()
  1548. {
  1549. var text = ustring.Empty;
  1550. int maxWidth = 1;
  1551. int expectedClippedWidth = 1;
  1552. List<ustring> wrappedLines;
  1553. text = "A sentence has words.";
  1554. maxWidth = 3;
  1555. expectedClippedWidth = 3;
  1556. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1557. //Assert.True (wrappedLines.Count == );
  1558. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1559. Assert.Equal ("A", wrappedLines [0].ToString ());
  1560. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1561. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1562. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1563. Assert.Equal ("has", wrappedLines [4].ToString ());
  1564. Assert.Equal ("wor", wrappedLines [5].ToString ());
  1565. Assert.Equal ("ds.", wrappedLines [6].ToString ());
  1566. maxWidth = 2;
  1567. expectedClippedWidth = 2;
  1568. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1569. //Assert.True (wrappedLines.Count == );
  1570. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1571. Assert.Equal ("A", wrappedLines [0].ToString ());
  1572. Assert.Equal ("se", wrappedLines [1].ToString ());
  1573. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1574. Assert.Equal ("en", wrappedLines [3].ToString ());
  1575. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1576. maxWidth = 1;
  1577. expectedClippedWidth = 1;
  1578. wrappedLines = TextFormatter.WordWrap (text, maxWidth);
  1579. //Assert.True (wrappedLines.Count == );
  1580. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1581. Assert.Equal ("A", wrappedLines [0].ToString ());
  1582. Assert.Equal ("s", wrappedLines [1].ToString ());
  1583. Assert.Equal ("e", wrappedLines [2].ToString ());
  1584. Assert.Equal ("n", wrappedLines [3].ToString ());
  1585. Assert.Equal (".", wrappedLines [^1].ToString ());
  1586. }
  1587. [Fact]
  1588. public void WordWrap_preserveTrailingSpaces ()
  1589. {
  1590. var text = ustring.Empty;
  1591. int maxWidth = 1;
  1592. int expectedClippedWidth = 1;
  1593. List<ustring> wrappedLines;
  1594. text = "A sentence has words.";
  1595. maxWidth = 14;
  1596. expectedClippedWidth = 14;
  1597. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1598. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1599. Assert.Equal ("A sentence has", wrappedLines [0].ToString ());
  1600. Assert.Equal (" words.", wrappedLines [1].ToString ());
  1601. Assert.True (wrappedLines.Count == 2);
  1602. maxWidth = 3;
  1603. expectedClippedWidth = 3;
  1604. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1605. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1606. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1607. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1608. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1609. Assert.Equal ("ce ", wrappedLines [3].ToString ());
  1610. Assert.Equal ("has", wrappedLines [4].ToString ());
  1611. Assert.Equal (" ", wrappedLines [5].ToString ());
  1612. Assert.Equal ("wor", wrappedLines [6].ToString ());
  1613. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  1614. Assert.True (wrappedLines.Count == 8);
  1615. maxWidth = 2;
  1616. expectedClippedWidth = 2;
  1617. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1618. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1619. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1620. Assert.Equal ("se", wrappedLines [1].ToString ());
  1621. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1622. Assert.Equal ("en", wrappedLines [3].ToString ());
  1623. Assert.Equal ("ce", wrappedLines [4].ToString ());
  1624. Assert.Equal (" ", wrappedLines [5].ToString ());
  1625. Assert.Equal ("ha", wrappedLines [6].ToString ());
  1626. Assert.Equal ("s ", wrappedLines [7].ToString ());
  1627. Assert.Equal ("wo", wrappedLines [8].ToString ());
  1628. Assert.Equal ("rd", wrappedLines [9].ToString ());
  1629. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1630. Assert.True (wrappedLines.Count == 11);
  1631. maxWidth = 1;
  1632. expectedClippedWidth = 1;
  1633. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1634. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1635. Assert.Equal ("A", wrappedLines [0].ToString ());
  1636. Assert.Equal (" ", wrappedLines [1].ToString ());
  1637. Assert.Equal ("s", wrappedLines [2].ToString ());
  1638. Assert.Equal ("e", wrappedLines [3].ToString ());
  1639. Assert.Equal ("n", wrappedLines [4].ToString ());
  1640. Assert.Equal ("t", wrappedLines [5].ToString ());
  1641. Assert.Equal ("e", wrappedLines [6].ToString ());
  1642. Assert.Equal ("n", wrappedLines [7].ToString ());
  1643. Assert.Equal ("c", wrappedLines [8].ToString ());
  1644. Assert.Equal ("e", wrappedLines [9].ToString ());
  1645. Assert.Equal (" ", wrappedLines [10].ToString ());
  1646. Assert.Equal (".", wrappedLines [^1].ToString ());
  1647. Assert.True (wrappedLines.Count == text.Length);
  1648. }
  1649. [Fact]
  1650. public void WordWrap_preserveTrailingSpaces_Wide_Runes ()
  1651. {
  1652. var text = ustring.Empty;
  1653. int maxWidth = 1;
  1654. int expectedClippedWidth = 1;
  1655. List<ustring> wrappedLines;
  1656. text = "文に は言葉 があり ます。";
  1657. maxWidth = 14;
  1658. expectedClippedWidth = 14;
  1659. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1660. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1661. Assert.Equal ("文に は言葉 ", wrappedLines [0].ToString ());
  1662. Assert.Equal ("があり ます。", wrappedLines [1].ToString ());
  1663. Assert.True (wrappedLines.Count == 2);
  1664. maxWidth = 3;
  1665. expectedClippedWidth = 3;
  1666. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1667. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1668. Assert.Equal ("文に", wrappedLines [0].ToString ());
  1669. Assert.Equal (" ", wrappedLines [1].ToString ());
  1670. Assert.Equal ("は言", wrappedLines [2].ToString ());
  1671. Assert.Equal ("葉 ", wrappedLines [3].ToString ());
  1672. Assert.Equal ("があ", wrappedLines [4].ToString ());
  1673. Assert.Equal ("り ", wrappedLines [5].ToString ());
  1674. Assert.Equal ("ます", wrappedLines [6].ToString ());
  1675. Assert.Equal ("。", wrappedLines [^1].ToString ());
  1676. Assert.True (wrappedLines.Count == 8);
  1677. maxWidth = 2;
  1678. expectedClippedWidth = 2;
  1679. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1680. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1681. Assert.Equal ("文", wrappedLines [0].ToString ());
  1682. Assert.Equal ("に", wrappedLines [1].ToString ());
  1683. Assert.Equal (" ", wrappedLines [2].ToString ());
  1684. Assert.Equal ("は", wrappedLines [3].ToString ());
  1685. Assert.Equal ("言", wrappedLines [4].ToString ());
  1686. Assert.Equal ("葉", wrappedLines [5].ToString ());
  1687. Assert.Equal (" ", wrappedLines [6].ToString ());
  1688. Assert.Equal ("が", wrappedLines [7].ToString ());
  1689. Assert.Equal ("あ", wrappedLines [8].ToString ());
  1690. Assert.Equal ("り", wrappedLines [9].ToString ());
  1691. Assert.Equal (" ", wrappedLines [10].ToString ());
  1692. Assert.Equal ("ま", wrappedLines [11].ToString ());
  1693. Assert.Equal ("す", wrappedLines [12].ToString ());
  1694. Assert.Equal ("。", wrappedLines [^1].ToString ());
  1695. Assert.True (wrappedLines.Count == 14);
  1696. maxWidth = 1;
  1697. expectedClippedWidth = 1;
  1698. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true);
  1699. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1700. Assert.Equal ("文", wrappedLines [0].ToString ());
  1701. Assert.Equal ("に", wrappedLines [1].ToString ());
  1702. Assert.Equal (" ", wrappedLines [2].ToString ());
  1703. Assert.Equal ("は", wrappedLines [3].ToString ());
  1704. Assert.Equal ("言", wrappedLines [4].ToString ());
  1705. Assert.Equal ("葉", wrappedLines [5].ToString ());
  1706. Assert.Equal (" ", wrappedLines [6].ToString ());
  1707. Assert.Equal ("が", wrappedLines [7].ToString ());
  1708. Assert.Equal ("あ", wrappedLines [8].ToString ());
  1709. Assert.Equal ("り", wrappedLines [9].ToString ());
  1710. Assert.Equal (" ", wrappedLines [10].ToString ());
  1711. Assert.Equal ("ま", wrappedLines [11].ToString ());
  1712. Assert.Equal ("す", wrappedLines [12].ToString ());
  1713. Assert.Equal ("。", wrappedLines [^1].ToString ());
  1714. Assert.False (wrappedLines.Count == text.Length);
  1715. Assert.True (wrappedLines.Count == text.RuneCount);
  1716. Assert.Equal (25, text.ConsoleWidth);
  1717. Assert.Equal (25, TextFormatter.GetTextWidth (text));
  1718. }
  1719. [Fact, AutoInitShutdown]
  1720. public void WordWrap_preserveTrailingSpaces_Horizontal_With_Simple_Runes ()
  1721. {
  1722. var text = "A sentence has words.";
  1723. var width = 3;
  1724. var height = 8;
  1725. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1726. var breakLines = "";
  1727. foreach (var line in wrappedLines) {
  1728. breakLines += $"{line}{Environment.NewLine}";
  1729. }
  1730. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  1731. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1732. frame.Add (label);
  1733. Application.Top.Add (frame);
  1734. Application.Begin (Application.Top);
  1735. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1736. Assert.False (label.AutoSize);
  1737. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1738. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1739. var expected = @"
  1740. ┌───┐
  1741. │A │
  1742. │sen│
  1743. │ten│
  1744. │ce │
  1745. │has│
  1746. │ │
  1747. │wor│
  1748. │ds.│
  1749. └───┘
  1750. ";
  1751. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1752. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1753. }
  1754. [Fact, AutoInitShutdown]
  1755. public void WordWrap_preserveTrailingSpaces_Vertical_With_Simple_Runes ()
  1756. {
  1757. var text = "A sentence has words.";
  1758. var width = 8;
  1759. var height = 3;
  1760. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1761. var breakLines = "";
  1762. foreach (var line in wrappedLines) {
  1763. breakLines += $"{line}{Environment.NewLine}";
  1764. }
  1765. var label = new Label (breakLines) {
  1766. TextDirection = TextDirection.TopBottom_LeftRight,
  1767. Width = Dim.Fill (),
  1768. Height = Dim.Fill ()
  1769. };
  1770. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1771. frame.Add (label);
  1772. Application.Top.Add (frame);
  1773. Application.Begin (Application.Top);
  1774. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1775. Assert.False (label.AutoSize);
  1776. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1777. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1778. var expected = @"
  1779. ┌────────┐
  1780. │Astc swd│
  1781. │ eeeh os│
  1782. │ nn a r.│
  1783. └────────┘
  1784. ";
  1785. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1786. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1787. }
  1788. [Fact, AutoInitShutdown]
  1789. public void WordWrap_preserveTrailingSpaces_Horizontal_With_Wide_Runes ()
  1790. {
  1791. var text = "文に は言葉 があり ます。";
  1792. var width = 6;
  1793. var height = 8;
  1794. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1795. var breakLines = "";
  1796. foreach (var line in wrappedLines) {
  1797. breakLines += $"{line}{Environment.NewLine}";
  1798. }
  1799. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  1800. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1801. frame.Add (label);
  1802. Application.Top.Add (frame);
  1803. Application.Begin (Application.Top);
  1804. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1805. Assert.False (label.AutoSize);
  1806. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1807. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1808. var expected = @"
  1809. ┌──────┐
  1810. │文に │
  1811. │は言葉│
  1812. │ があ │
  1813. │り │
  1814. │ ます │
  1815. │。 │
  1816. │ │
  1817. │ │
  1818. └──────┘
  1819. ";
  1820. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1821. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1822. }
  1823. [Fact, AutoInitShutdown]
  1824. public void WordWrap_preserveTrailingSpaces_Vertical_With_Wide_Runes ()
  1825. {
  1826. var text = "文に は言葉 があり ます。";
  1827. var width = 8;
  1828. var height = 4;
  1829. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1830. var breakLines = "";
  1831. foreach (var line in wrappedLines) {
  1832. breakLines += $"{line}{Environment.NewLine}";
  1833. }
  1834. var label = new Label (breakLines) {
  1835. TextDirection = TextDirection.TopBottom_LeftRight,
  1836. Width = Dim.Fill (),
  1837. Height = Dim.Fill ()
  1838. };
  1839. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1840. frame.Add (label);
  1841. Application.Top.Add (frame);
  1842. Application.Begin (Application.Top);
  1843. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1844. Assert.False (label.AutoSize);
  1845. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1846. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1847. var expected = @"
  1848. ┌────────┐
  1849. │文はがま│
  1850. │に言あす│
  1851. │ 葉り。│
  1852. │ │
  1853. └────────┘
  1854. ";
  1855. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1856. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1857. }
  1858. [Fact]
  1859. public void WordWrap_preserveTrailingSpaces_With_Tab ()
  1860. {
  1861. var text = ustring.Empty;
  1862. int maxWidth = 1;
  1863. int expectedClippedWidth = 1;
  1864. List<ustring> wrappedLines;
  1865. text = "A sentence\t\t\t has words.";
  1866. var tabWidth = 4;
  1867. maxWidth = 14;
  1868. expectedClippedWidth = 11;
  1869. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1870. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1871. Assert.Equal ("A sentence\t", wrappedLines [0].ToString ());
  1872. Assert.Equal ("\t\t has ", wrappedLines [1].ToString ());
  1873. Assert.Equal ("words.", wrappedLines [2].ToString ());
  1874. Assert.True (wrappedLines.Count == 3);
  1875. maxWidth = 3;
  1876. expectedClippedWidth = 3;
  1877. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1878. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1879. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1880. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1881. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1882. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1883. Assert.Equal ("\t", wrappedLines [4].ToString ());
  1884. Assert.Equal ("\t", wrappedLines [5].ToString ());
  1885. Assert.Equal ("\t", wrappedLines [6].ToString ());
  1886. Assert.Equal (" ", wrappedLines [7].ToString ());
  1887. Assert.Equal ("has", wrappedLines [8].ToString ());
  1888. Assert.Equal (" ", wrappedLines [9].ToString ());
  1889. Assert.Equal ("wor", wrappedLines [10].ToString ());
  1890. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  1891. Assert.True (wrappedLines.Count == 12);
  1892. maxWidth = 2;
  1893. expectedClippedWidth = 2;
  1894. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1895. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1896. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1897. Assert.Equal ("se", wrappedLines [1].ToString ());
  1898. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1899. Assert.Equal ("en", wrappedLines [3].ToString ());
  1900. Assert.Equal ("ce", wrappedLines [4].ToString ());
  1901. Assert.Equal ("\t", wrappedLines [5].ToString ());
  1902. Assert.Equal ("\t", wrappedLines [6].ToString ());
  1903. Assert.Equal ("\t", wrappedLines [7].ToString ());
  1904. Assert.Equal (" ", wrappedLines [8].ToString ());
  1905. Assert.Equal ("ha", wrappedLines [9].ToString ());
  1906. Assert.Equal ("s ", wrappedLines [10].ToString ());
  1907. Assert.Equal ("wo", wrappedLines [11].ToString ());
  1908. Assert.Equal ("rd", wrappedLines [12].ToString ());
  1909. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1910. Assert.True (wrappedLines.Count == 14);
  1911. maxWidth = 1;
  1912. expectedClippedWidth = 1;
  1913. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1914. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1915. Assert.Equal ("A", wrappedLines [0].ToString ());
  1916. Assert.Equal (" ", wrappedLines [1].ToString ());
  1917. Assert.Equal ("s", wrappedLines [2].ToString ());
  1918. Assert.Equal ("e", wrappedLines [3].ToString ());
  1919. Assert.Equal ("n", wrappedLines [4].ToString ());
  1920. Assert.Equal ("t", wrappedLines [5].ToString ());
  1921. Assert.Equal ("e", wrappedLines [6].ToString ());
  1922. Assert.Equal ("n", wrappedLines [7].ToString ());
  1923. Assert.Equal ("c", wrappedLines [8].ToString ());
  1924. Assert.Equal ("e", wrappedLines [9].ToString ());
  1925. Assert.Equal ("\t", wrappedLines [10].ToString ());
  1926. Assert.Equal ("\t", wrappedLines [11].ToString ());
  1927. Assert.Equal ("\t", wrappedLines [12].ToString ());
  1928. Assert.Equal (" ", wrappedLines [13].ToString ());
  1929. Assert.Equal (".", wrappedLines [^1].ToString ());
  1930. Assert.True (wrappedLines.Count == text.Length);
  1931. }
  1932. [Fact]
  1933. public void WordWrap_Unicode_Wide_Runes ()
  1934. {
  1935. ustring text = "これが最初の行です。 こんにちは世界。 これが2行目です。";
  1936. var width = text.RuneCount;
  1937. var wrappedLines = TextFormatter.WordWrap (text, width);
  1938. Assert.Equal (3, wrappedLines.Count);
  1939. Assert.Equal ("これが最初の行です。", wrappedLines [0].ToString ());
  1940. Assert.Equal ("こんにちは世界。", wrappedLines [1].ToString ());
  1941. Assert.Equal ("これが2行目です。", wrappedLines [^1].ToString ());
  1942. }
  1943. [Fact]
  1944. public void ReplaceHotKeyWithTag ()
  1945. {
  1946. var tf = new TextFormatter ();
  1947. ustring text = "test";
  1948. int hotPos = 0;
  1949. uint tag = tf.HotKeyTagMask | 't';
  1950. Assert.Equal (ustring.Make (new Rune [] { tag, 'e', 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1951. tag = tf.HotKeyTagMask | 'e';
  1952. hotPos = 1;
  1953. Assert.Equal (ustring.Make (new Rune [] { 't', tag, 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1954. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  1955. Assert.Equal ('e', (uint)(result.ToRunes () [1] & ~tf.HotKeyTagMask));
  1956. text = "Ok";
  1957. tag = 0x100000 | 'O';
  1958. hotPos = 0;
  1959. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1960. Assert.Equal ('O', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1961. text = "[◦ Ok ◦]";
  1962. text = ustring.Make (new Rune [] { '[', '◦', ' ', 'O', 'k', ' ', '◦', ']' });
  1963. var runes = text.ToRuneList ();
  1964. Assert.Equal (text.RuneCount, runes.Count);
  1965. Assert.Equal (text, ustring.Make (runes));
  1966. tag = tf.HotKeyTagMask | 'O';
  1967. hotPos = 3;
  1968. Assert.Equal (ustring.Make (new Rune [] { '[', '◦', ' ', tag, 'k', ' ', '◦', ']' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1969. Assert.Equal ('O', (uint)(result.ToRunes () [3] & ~tf.HotKeyTagMask));
  1970. text = "^k";
  1971. tag = '^';
  1972. hotPos = 0;
  1973. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1974. Assert.Equal ('^', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1975. }
  1976. [Fact]
  1977. public void Reformat_Invalid ()
  1978. {
  1979. var text = ustring.Empty;
  1980. var list = new List<ustring> ();
  1981. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, -1, TextAlignment.Left, false));
  1982. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1983. Assert.NotEmpty (list);
  1984. Assert.True (list.Count == 1);
  1985. Assert.Equal (ustring.Empty, list [0]);
  1986. text = null;
  1987. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1988. Assert.NotEmpty (list);
  1989. Assert.True (list.Count == 1);
  1990. Assert.Equal (ustring.Empty, list [0]);
  1991. list = TextFormatter.Format (text, 0, TextAlignment.Left, true);
  1992. Assert.NotEmpty (list);
  1993. Assert.True (list.Count == 1);
  1994. Assert.Equal (ustring.Empty, list [0]);
  1995. }
  1996. [Fact]
  1997. public void Reformat_NoWordrap_SingleLine ()
  1998. {
  1999. var text = ustring.Empty;
  2000. var list = new List<ustring> ();
  2001. var maxWidth = 0;
  2002. var expectedClippedWidth = 0;
  2003. var wrap = false;
  2004. maxWidth = 0;
  2005. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2006. Assert.True (list.Count == 1);
  2007. maxWidth = 1;
  2008. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2009. Assert.True (list.Count == 1);
  2010. text = "A sentence has words.";
  2011. maxWidth = 0;
  2012. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2013. Assert.True (list.Count == 1);
  2014. Assert.Equal (ustring.Empty, list [0]);
  2015. maxWidth = 1;
  2016. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2017. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2018. Assert.True (list.Count == 1);
  2019. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2020. maxWidth = 5;
  2021. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2022. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2023. Assert.True (list.Count == 1);
  2024. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2025. maxWidth = text.RuneCount - 1;
  2026. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2027. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2028. Assert.True (list.Count == 1);
  2029. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2030. // no clip
  2031. maxWidth = text.RuneCount + 0;
  2032. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2033. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2034. Assert.True (list.Count == 1);
  2035. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2036. maxWidth = text.RuneCount + 1;
  2037. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2038. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2039. Assert.True (list.Count == 1);
  2040. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2041. }
  2042. [Fact]
  2043. public void Reformat_NoWordrap_NewLines ()
  2044. {
  2045. var text = ustring.Empty;
  2046. var list = new List<ustring> ();
  2047. var maxWidth = 0;
  2048. var expectedClippedWidth = 0;
  2049. var wrap = false;
  2050. text = "A sentence has words.\nLine 2.";
  2051. maxWidth = 0;
  2052. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2053. Assert.True (list.Count == 1);
  2054. Assert.Equal (ustring.Empty, list [0]);
  2055. maxWidth = 1;
  2056. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2057. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2058. Assert.True (list.Count == 1);
  2059. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2060. maxWidth = 5;
  2061. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2062. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2063. Assert.True (list.Count == 1);
  2064. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2065. maxWidth = text.RuneCount - 1;
  2066. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2067. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2068. Assert.True (list.Count == 1);
  2069. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2070. // no clip
  2071. maxWidth = text.RuneCount + 0;
  2072. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2073. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2074. Assert.True (list.Count == 1);
  2075. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2076. maxWidth = text.RuneCount + 1;
  2077. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2078. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2079. Assert.True (list.Count == 1);
  2080. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2081. text = "A sentence has words.\r\nLine 2.";
  2082. maxWidth = 0;
  2083. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2084. Assert.True (list.Count == 1);
  2085. Assert.Equal (ustring.Empty, list [0]);
  2086. maxWidth = 1;
  2087. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2088. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2089. Assert.True (list.Count == 1);
  2090. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2091. maxWidth = 5;
  2092. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2093. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2094. Assert.True (list.Count == 1);
  2095. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2096. maxWidth = text.RuneCount - 1;
  2097. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth) + 1;
  2098. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2099. Assert.True (list.Count == 1);
  2100. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " ").ToString (), list [0].ToString ());
  2101. // no clip
  2102. maxWidth = text.RuneCount + 0;
  2103. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2104. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2105. Assert.True (list.Count == 1);
  2106. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  2107. maxWidth = text.RuneCount + 1;
  2108. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2109. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2110. Assert.True (list.Count == 1);
  2111. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  2112. }
  2113. [Fact]
  2114. public void Reformat_Wrap_Spaces_No_NewLines ()
  2115. {
  2116. var text = ustring.Empty;
  2117. var list = new List<ustring> ();
  2118. var maxWidth = 0;
  2119. var expectedClippedWidth = 0;
  2120. var wrap = true;
  2121. // Even # of spaces
  2122. // 0123456789
  2123. text = "012 456 89";
  2124. // See WordWrap BUGBUGs above.
  2125. //maxWidth = 0;
  2126. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  2127. //Assert.True (list.Count == 1);
  2128. //Assert.Equal (ustring.Empty, list [0]);
  2129. //maxWidth = 1;
  2130. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2131. //// remove 3 whitespace chars
  2132. //expectedLines = text.RuneCount;
  2133. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  2134. //Assert.Equal (expectedLines, list.Count);
  2135. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2136. ////width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  2137. //maxWidth = 5;
  2138. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2139. //expectedLines = (int)Math.Ceiling ((double)(text.RuneCount / maxWidth));
  2140. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  2141. //Assert.Equal (expectedLines, list.Count);
  2142. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2143. maxWidth = text.RuneCount - 1;
  2144. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2145. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2146. Assert.True (list.Count == 2);
  2147. Assert.Equal ("012 456", list [0]);
  2148. Assert.Equal ("89", list [1]);
  2149. // no clip
  2150. maxWidth = text.RuneCount + 0;
  2151. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2152. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2153. Assert.True (list.Count == 1);
  2154. Assert.Equal ("012 456 89", list [0]);
  2155. maxWidth = text.RuneCount + 1;
  2156. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2157. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2158. Assert.True (list.Count == 1);
  2159. Assert.Equal ("012 456 89", list [0]);
  2160. // Odd # of spaces
  2161. // 0123456789
  2162. text = "012 456 89 end";
  2163. maxWidth = text.RuneCount - 1;
  2164. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2165. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2166. Assert.True (list.Count == 2);
  2167. Assert.Equal ("012 456 89", list [0]);
  2168. Assert.Equal ("end", list [1]);
  2169. // no clip
  2170. maxWidth = text.RuneCount + 0;
  2171. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2172. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2173. Assert.True (list.Count == 1);
  2174. Assert.Equal ("012 456 89 end", list [0]);
  2175. maxWidth = text.RuneCount + 1;
  2176. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2177. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2178. Assert.True (list.Count == 1);
  2179. Assert.Equal ("012 456 89 end", list [0]);
  2180. }
  2181. [Fact]
  2182. public void Reformat_Unicode_Wrap_Spaces_No_NewLines ()
  2183. {
  2184. var text = ustring.Empty;
  2185. var list = new List<ustring> ();
  2186. var maxWidth = 0;
  2187. var expectedClippedWidth = 0;
  2188. var wrap = true;
  2189. // Unicode
  2190. // Even # of chars
  2191. // 0123456789
  2192. text = "\u2660пÑРвРÑ";
  2193. maxWidth = text.RuneCount - 1;
  2194. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2195. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2196. Assert.True (list.Count == 2);
  2197. Assert.Equal ("\u2660пÑРвÐ", list [0]);
  2198. Assert.Equal ("Ñ", list [1]);
  2199. // no clip
  2200. maxWidth = text.RuneCount + 0;
  2201. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2202. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2203. Assert.True (list.Count == 1);
  2204. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  2205. maxWidth = text.RuneCount + 1;
  2206. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2207. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2208. Assert.True (list.Count == 1);
  2209. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  2210. // Unicode
  2211. // Odd # of chars
  2212. // 0123456789
  2213. text = "\u2660 ÑРвРÑ";
  2214. maxWidth = text.RuneCount - 1;
  2215. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2216. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2217. Assert.True (list.Count == 2);
  2218. Assert.Equal ("\u2660 ÑРвÐ", list [0]);
  2219. Assert.Equal ("Ñ", list [1]);
  2220. // no clip
  2221. maxWidth = text.RuneCount + 0;
  2222. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2223. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2224. Assert.True (list.Count == 1);
  2225. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  2226. maxWidth = text.RuneCount + 1;
  2227. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2228. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2229. Assert.True (list.Count == 1);
  2230. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  2231. }
  2232. [Fact]
  2233. public void Reformat_Unicode_Wrap_Spaces_NewLines ()
  2234. {
  2235. var text = ustring.Empty;
  2236. var list = new List<ustring> ();
  2237. var maxWidth = 0;
  2238. var expectedClippedWidth = 0;
  2239. var wrap = true;
  2240. // Unicode
  2241. text = "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464";
  2242. maxWidth = text.RuneCount - 1;
  2243. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2244. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2245. Assert.Equal (2, list.Count);
  2246. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2247. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2248. // no clip
  2249. maxWidth = text.RuneCount + 0;
  2250. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2251. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2252. Assert.Equal (2, list.Count);
  2253. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2254. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2255. maxWidth = text.RuneCount + 1;
  2256. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2257. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2258. Assert.Equal (2, list.Count);
  2259. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2260. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2261. }
  2262. [Fact]
  2263. public void System_Rune_ColumnWidth ()
  2264. {
  2265. var c = new System.Rune ('a');
  2266. Assert.Equal (1, Rune.ColumnWidth (c));
  2267. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2268. Assert.Equal (1, ustring.Make (c).Length);
  2269. c = new System.Rune ('b');
  2270. Assert.Equal (1, Rune.ColumnWidth (c));
  2271. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2272. Assert.Equal (1, ustring.Make (c).Length);
  2273. c = new System.Rune (123);
  2274. Assert.Equal (1, Rune.ColumnWidth (c));
  2275. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2276. Assert.Equal (1, ustring.Make (c).Length);
  2277. c = new System.Rune ('\u1150');
  2278. Assert.Equal (2, Rune.ColumnWidth (c)); // 0x1150 ᅐ Unicode Technical Report #11
  2279. Assert.Equal (2, ustring.Make (c).ConsoleWidth);
  2280. Assert.Equal (3, ustring.Make (c).Length);
  2281. c = new System.Rune ('\u1161');
  2282. Assert.Equal (0, Rune.ColumnWidth (c)); // 0x1161 ᅡ column width of 0
  2283. Assert.Equal (0, ustring.Make (c).ConsoleWidth);
  2284. Assert.Equal (3, ustring.Make (c).Length);
  2285. c = new System.Rune (31);
  2286. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2287. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2288. Assert.Equal (1, ustring.Make (c).Length);
  2289. c = new System.Rune (127);
  2290. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2291. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2292. Assert.Equal (1, ustring.Make (c).Length);
  2293. }
  2294. [Fact]
  2295. public void System_Text_Rune ()
  2296. {
  2297. var c = new System.Text.Rune ('a');
  2298. Assert.Equal (1, c.Utf8SequenceLength);
  2299. c = new System.Text.Rune ('b');
  2300. Assert.Equal (1, c.Utf8SequenceLength);
  2301. c = new System.Text.Rune (123);
  2302. Assert.Equal (1, c.Utf8SequenceLength);
  2303. c = new System.Text.Rune ('\u1150');
  2304. Assert.Equal (3, c.Utf8SequenceLength); // 0x1150 ᅐ Unicode Technical Report #11
  2305. c = new System.Text.Rune ('\u1161');
  2306. Assert.Equal (3, c.Utf8SequenceLength); // 0x1161 ᅡ column width of 0
  2307. c = new System.Text.Rune (31);
  2308. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2309. c = new System.Text.Rune (127);
  2310. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2311. }
  2312. [Fact]
  2313. public void Format_WordWrap_preserveTrailingSpaces ()
  2314. {
  2315. ustring text = " A sentence has words. \n This is the second Line - 2. ";
  2316. // With preserveTrailingSpaces = false by default.
  2317. var list1 = TextFormatter.Format (text, 4, TextAlignment.Left, true);
  2318. ustring wrappedText1 = ustring.Empty;
  2319. Assert.Equal (" A", list1 [0].ToString ());
  2320. Assert.Equal ("sent", list1 [1].ToString ());
  2321. Assert.Equal ("ence", list1 [2].ToString ());
  2322. Assert.Equal ("has", list1 [3].ToString ());
  2323. Assert.Equal ("word", list1 [4].ToString ());
  2324. Assert.Equal ("s. ", list1 [5].ToString ());
  2325. Assert.Equal (" Thi", list1 [6].ToString ());
  2326. Assert.Equal ("s is", list1 [7].ToString ());
  2327. Assert.Equal ("the", list1 [8].ToString ());
  2328. Assert.Equal ("seco", list1 [9].ToString ());
  2329. Assert.Equal ("nd", list1 [10].ToString ());
  2330. Assert.Equal ("Line", list1 [11].ToString ());
  2331. Assert.Equal ("- 2.", list1 [^1].ToString ());
  2332. foreach (var txt in list1) {
  2333. wrappedText1 += txt;
  2334. }
  2335. Assert.Equal (" Asentencehaswords. This isthesecondLine- 2.", wrappedText1);
  2336. // With preserveTrailingSpaces = true.
  2337. var list2 = TextFormatter.Format (text, 4, TextAlignment.Left, true, true);
  2338. ustring wrappedText2 = ustring.Empty;
  2339. Assert.Equal (" A ", list2 [0].ToString ());
  2340. Assert.Equal ("sent", list2 [1].ToString ());
  2341. Assert.Equal ("ence", list2 [2].ToString ());
  2342. Assert.Equal (" has", list2 [3].ToString ());
  2343. Assert.Equal (" ", list2 [4].ToString ());
  2344. Assert.Equal ("word", list2 [5].ToString ());
  2345. Assert.Equal ("s. ", list2 [6].ToString ());
  2346. Assert.Equal (" ", list2 [7].ToString ());
  2347. Assert.Equal ("This", list2 [8].ToString ());
  2348. Assert.Equal (" is ", list2 [9].ToString ());
  2349. Assert.Equal ("the ", list2 [10].ToString ());
  2350. Assert.Equal ("seco", list2 [11].ToString ());
  2351. Assert.Equal ("nd ", list2 [12].ToString ());
  2352. Assert.Equal ("Line", list2 [13].ToString ());
  2353. Assert.Equal (" - ", list2 [14].ToString ());
  2354. Assert.Equal ("2. ", list2 [^1].ToString ());
  2355. foreach (var txt in list2) {
  2356. wrappedText2 += txt;
  2357. }
  2358. Assert.Equal (" A sentence has words. This is the second Line - 2. ", wrappedText2);
  2359. }
  2360. [Fact]
  2361. public void Format_Throw_ArgumentException_With_WordWrap_As_False_And_Keep_End_Spaces_As_True ()
  2362. {
  2363. Assert.Throws<ArgumentException> (() => TextFormatter.Format ("Some text", 4, TextAlignment.Left, false, true));
  2364. }
  2365. [Fact]
  2366. public void Draw_Horizontal_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2367. {
  2368. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2369. var top = Application.Top;
  2370. var view = new View ("view") { X = -2 };
  2371. top.Add (view);
  2372. Application.Iteration += () => {
  2373. Assert.Equal (-2, view.X);
  2374. Application.RequestStop ();
  2375. };
  2376. try {
  2377. Application.Run ();
  2378. } catch (IndexOutOfRangeException ex) {
  2379. // After the fix this exception will not be caught.
  2380. Assert.IsType<IndexOutOfRangeException> (ex);
  2381. }
  2382. // Shutdown must be called to safely clean up Application if Init has been called
  2383. Application.Shutdown ();
  2384. }
  2385. [Fact]
  2386. public void TestClipOrPad_ShortWord ()
  2387. {
  2388. // word is short but we want it to fill 6 so it should be padded
  2389. Assert.Equal ("fff ", TextFormatter.ClipOrPad ("fff", 6));
  2390. }
  2391. [Fact]
  2392. public void TestClipOrPad_LongWord ()
  2393. {
  2394. // word is long but we want it to fill 3 space only
  2395. Assert.Equal ("123", TextFormatter.ClipOrPad ("123456789", 3));
  2396. }
  2397. [Fact]
  2398. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2399. {
  2400. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2401. var top = Application.Top;
  2402. var view = new View ("view") {
  2403. Y = -2,
  2404. Height = 10,
  2405. TextDirection = TextDirection.TopBottom_LeftRight
  2406. };
  2407. top.Add (view);
  2408. Application.Iteration += () => {
  2409. Assert.Equal (-2, view.Y);
  2410. Application.RequestStop ();
  2411. };
  2412. try {
  2413. Application.Run ();
  2414. } catch (IndexOutOfRangeException ex) {
  2415. // After the fix this exception will not be caught.
  2416. Assert.IsType<IndexOutOfRangeException> (ex);
  2417. }
  2418. // Shutdown must be called to safely clean up Application if Init has been called
  2419. Application.Shutdown ();
  2420. }
  2421. [Fact]
  2422. public void Internal_Tests ()
  2423. {
  2424. var tf = new TextFormatter ();
  2425. Assert.Equal (Key.Null, tf.HotKey);
  2426. tf.HotKey = Key.CtrlMask | Key.Q;
  2427. Assert.Equal (Key.CtrlMask | Key.Q, tf.HotKey);
  2428. }
  2429. [Fact, AutoInitShutdown]
  2430. public void Draw_Horizontal_Simple_Runes ()
  2431. {
  2432. var label = new Label ("Demo Simple Rune");
  2433. Application.Top.Add (label);
  2434. Application.Begin (Application.Top);
  2435. Assert.True (label.AutoSize);
  2436. Assert.Equal (new Rect (0, 0, 16, 1), label.Frame);
  2437. var expected = @"
  2438. Demo Simple Rune
  2439. ";
  2440. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2441. Assert.Equal (new Rect (0, 0, 16, 1), pos);
  2442. }
  2443. [Fact, AutoInitShutdown]
  2444. public void Draw_Vertical_Simple_Runes ()
  2445. {
  2446. var label = new Label ("Demo Simple Rune") {
  2447. TextDirection = TextDirection.TopBottom_LeftRight
  2448. };
  2449. Application.Top.Add (label);
  2450. Application.Begin (Application.Top);
  2451. Assert.True (label.AutoSize);
  2452. Assert.Equal (new Rect (0, 0, 1, 16), label.Frame);
  2453. var expected = @"
  2454. D
  2455. e
  2456. m
  2457. o
  2458. S
  2459. i
  2460. m
  2461. p
  2462. l
  2463. e
  2464. R
  2465. u
  2466. n
  2467. e
  2468. ";
  2469. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2470. Assert.Equal (new Rect (0, 0, 1, 16), pos);
  2471. }
  2472. [Fact, AutoInitShutdown]
  2473. public void Draw_Horizontal_Wide_Runes ()
  2474. {
  2475. var label = new Label ("デモエムポンズ");
  2476. Application.Top.Add (label);
  2477. Application.Begin (Application.Top);
  2478. Assert.True (label.AutoSize);
  2479. Assert.Equal (new Rect (0, 0, 14, 1), label.Frame);
  2480. var expected = @"
  2481. デモエムポンズ
  2482. ";
  2483. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2484. Assert.Equal (new Rect (0, 0, 14, 1), pos);
  2485. }
  2486. [Fact, AutoInitShutdown]
  2487. public void Draw_Vertical_Wide_Runes ()
  2488. {
  2489. var label = new Label ("デモエムポンズ") {
  2490. TextDirection = TextDirection.TopBottom_LeftRight
  2491. };
  2492. Application.Top.Add (label);
  2493. Application.Begin (Application.Top);
  2494. Assert.True (label.AutoSize);
  2495. Assert.Equal (new Rect (0, 0, 2, 7), label.Frame);
  2496. var expected = @"
  2497. ";
  2498. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2499. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  2500. }
  2501. [Fact, AutoInitShutdown]
  2502. public void Draw_Horizontal_Simple_TextAlignments ()
  2503. {
  2504. var text = "Hello World";
  2505. var width = 20;
  2506. var lblLeft = new Label (text) { Width = width };
  2507. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2508. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2509. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2510. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2511. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2512. Application.Top.Add (frame);
  2513. Application.Begin (Application.Top);
  2514. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2515. Assert.False (lblLeft.AutoSize);
  2516. Assert.False (lblCenter.AutoSize);
  2517. Assert.False (lblRight.AutoSize);
  2518. Assert.False (lblJust.AutoSize);
  2519. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2520. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2521. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2522. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2523. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2524. var expected = @"
  2525. ┌────────────────────┐
  2526. │Hello World │
  2527. │ Hello World │
  2528. │ Hello World│
  2529. │Hello World│
  2530. └────────────────────┘
  2531. ";
  2532. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2533. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2534. }
  2535. [Fact, AutoInitShutdown]
  2536. public void Draw_Vertical_Simple_TextAlignments ()
  2537. {
  2538. var text = "Hello World";
  2539. var height = 20;
  2540. var lblLeft = new Label (text, direction: TextDirection.TopBottom_LeftRight) { Height = height };
  2541. var lblCenter = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 2, Height = height, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2542. var lblRight = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 4, Height = height, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2543. var lblJust = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 6, Height = height, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2544. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2545. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2546. Application.Top.Add (frame);
  2547. Application.Begin (Application.Top);
  2548. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  2549. Assert.False (lblLeft.AutoSize);
  2550. Assert.False (lblCenter.AutoSize);
  2551. Assert.False (lblRight.AutoSize);
  2552. Assert.False (lblJust.AutoSize);
  2553. Assert.Equal (new Rect (0, 0, 1, height), lblLeft.Frame);
  2554. Assert.Equal (new Rect (2, 0, 1, height), lblCenter.Frame);
  2555. Assert.Equal (new Rect (4, 0, 1, height), lblRight.Frame);
  2556. Assert.Equal (new Rect (6, 0, 1, height), lblJust.Frame);
  2557. Assert.Equal (new Rect (0, 0, 9, height + 2), frame.Frame);
  2558. var expected = @"
  2559. ┌───────┐
  2560. │H H│
  2561. │e e│
  2562. │l l│
  2563. │l l│
  2564. │o H o│
  2565. │ e │
  2566. │W l │
  2567. │o l │
  2568. │r o │
  2569. │l H │
  2570. │d W e │
  2571. │ o l │
  2572. │ r l │
  2573. │ l o │
  2574. │ d │
  2575. │ W W│
  2576. │ o o│
  2577. │ r r│
  2578. │ l l│
  2579. │ d d│
  2580. └───────┘
  2581. ";
  2582. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2583. Assert.Equal (new Rect (0, 0, 9, height + 2), pos);
  2584. }
  2585. [Fact, AutoInitShutdown]
  2586. public void Draw_Horizontal_Wide_TextAlignments ()
  2587. {
  2588. var text = "こんにちは 世界";
  2589. var width = 25;
  2590. var lblLeft = new Label (text) { Width = width };
  2591. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2592. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2593. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2594. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2595. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2596. Application.Top.Add (frame);
  2597. Application.Begin (Application.Top);
  2598. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2599. Assert.False (lblLeft.AutoSize);
  2600. Assert.False (lblCenter.AutoSize);
  2601. Assert.False (lblRight.AutoSize);
  2602. Assert.False (lblJust.AutoSize);
  2603. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2604. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2605. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2606. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2607. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2608. var expected = @"
  2609. ┌─────────────────────────┐
  2610. │こんにちは 世界 │
  2611. │ こんにちは 世界 │
  2612. │ こんにちは 世界│
  2613. │こんにちは 世界│
  2614. └─────────────────────────┘
  2615. ";
  2616. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2617. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2618. }
  2619. [Fact, AutoInitShutdown]
  2620. public void Draw_Vertical_Wide_TextAlignments ()
  2621. {
  2622. var text = "こんにちは 世界";
  2623. var height = 23;
  2624. var lblLeft = new Label (text) { Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight };
  2625. var lblCenter = new Label (text) { X = 3, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2626. var lblRight = new Label (text) { X = 6, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2627. var lblJust = new Label (text) { X = 9, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2628. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2629. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2630. Application.Top.Add (frame);
  2631. Application.Begin (Application.Top);
  2632. ((FakeDriver)Application.Driver).SetBufferSize (13, height + 2);
  2633. Assert.False (lblLeft.AutoSize);
  2634. Assert.False (lblCenter.AutoSize);
  2635. Assert.False (lblRight.AutoSize);
  2636. Assert.False (lblJust.AutoSize);
  2637. Assert.Equal (new Rect (0, 0, 2, height), lblLeft.Frame);
  2638. Assert.Equal (new Rect (3, 0, 2, height), lblCenter.Frame);
  2639. Assert.Equal (new Rect (6, 0, 2, height), lblRight.Frame);
  2640. Assert.Equal (new Rect (9, 0, 2, height), lblJust.Frame);
  2641. Assert.Equal (new Rect (0, 0, 13, height + 2), frame.Frame);
  2642. var expected = @"
  2643. ┌───────────┐
  2644. │こ こ│
  2645. │ん ん│
  2646. │に に│
  2647. │ち ち│
  2648. │は は│
  2649. │ │
  2650. │世 │
  2651. │界 こ │
  2652. │ ん │
  2653. │ に │
  2654. │ ち │
  2655. │ は │
  2656. │ │
  2657. │ 世 │
  2658. │ 界 │
  2659. │ こ │
  2660. │ ん │
  2661. │ に │
  2662. │ ち │
  2663. │ は │
  2664. │ │
  2665. │ 世 世│
  2666. │ 界 界│
  2667. └───────────┘
  2668. ";
  2669. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2670. Assert.Equal (new Rect (0, 0, 13, height + 2), pos);
  2671. }
  2672. [Fact]
  2673. public void GetTextWidth_Simple_And_Wide_Runes ()
  2674. {
  2675. ustring text = "Hello World";
  2676. Assert.Equal (11, TextFormatter.GetTextWidth (text));
  2677. text = "こんにちは世界";
  2678. Assert.Equal (14, TextFormatter.GetTextWidth (text));
  2679. }
  2680. [Fact]
  2681. public void GetSumMaxCharWidth_Simple_And_Wide_Runes ()
  2682. {
  2683. ustring text = "Hello World";
  2684. Assert.Equal (11, TextFormatter.GetSumMaxCharWidth (text));
  2685. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2686. text = "こんにちは 世界";
  2687. Assert.Equal (15, TextFormatter.GetSumMaxCharWidth (text));
  2688. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2689. }
  2690. [Fact]
  2691. public void GetSumMaxCharWidth_List_Simple_And_Wide_Runes ()
  2692. {
  2693. List<ustring> text = new List<ustring> () { "Hello", "World" };
  2694. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text));
  2695. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2696. text = new List<ustring> () { "こんにちは", "世界" };
  2697. Assert.Equal (4, TextFormatter.GetSumMaxCharWidth (text));
  2698. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2699. }
  2700. [Fact]
  2701. public void GetMaxLengthForWidth_Simple_And_Wide_Runes ()
  2702. {
  2703. ustring text = "Hello World";
  2704. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (text, 6));
  2705. text = "こんにちは 世界";
  2706. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (text, 6));
  2707. }
  2708. [Fact]
  2709. public void GetMaxLengthForWidth_List_Simple_And_Wide_Runes ()
  2710. {
  2711. var runes = ustring.Make ("Hello World").ToRuneList ();
  2712. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2713. runes = ustring.Make ("こんにちは 世界").ToRuneList ();
  2714. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2715. runes = ustring.Make ("[ Say Hello 你 ]").ToRuneList ();
  2716. Assert.Equal (15, TextFormatter.GetMaxLengthForWidth (runes, 16));
  2717. }
  2718. [Fact]
  2719. public void Format_Truncate_Simple_And_Wide_Runes ()
  2720. {
  2721. var text = "Truncate";
  2722. var list = TextFormatter.Format (text, 3, false, false);
  2723. Assert.Equal ("Tru", list [^1].ToString ());
  2724. text = "デモエムポンズ";
  2725. list = TextFormatter.Format (text, 3, false, false);
  2726. Assert.Equal ("デ", list [^1].ToString ());
  2727. }
  2728. }
  2729. }