TextFormatterTests.cs 125 KB

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