TextFormatterTests.cs 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194
  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 (), Height = Dim.Fill ()
  1760. };
  1761. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1762. frame.Add (label);
  1763. Application.Top.Add (frame);
  1764. Application.Begin (Application.Top);
  1765. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1766. Assert.False (label.AutoSize);
  1767. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1768. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1769. var expected = @"
  1770. ┌────────┐
  1771. │Astc swd│
  1772. │ eeeh os│
  1773. │ nn a r.│
  1774. └────────┘
  1775. ";
  1776. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1777. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1778. }
  1779. [Fact, AutoInitShutdown]
  1780. public void WordWrap_preserveTrailingSpaces_Horizontal_With_Wide_Runes ()
  1781. {
  1782. var text = "文に は言葉 があり ます。";
  1783. var width = 6;
  1784. var height = 8;
  1785. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1786. var breakLines = "";
  1787. foreach (var line in wrappedLines) {
  1788. breakLines += $"{line}{Environment.NewLine}";
  1789. }
  1790. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  1791. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1792. frame.Add (label);
  1793. Application.Top.Add (frame);
  1794. Application.Begin (Application.Top);
  1795. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1796. Assert.False (label.AutoSize);
  1797. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1798. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1799. var expected = @"
  1800. ┌──────┐
  1801. │文に │
  1802. │は言葉│
  1803. │ があ │
  1804. │り │
  1805. │ ます │
  1806. │。 │
  1807. │ │
  1808. │ │
  1809. └──────┘
  1810. ";
  1811. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1812. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1813. }
  1814. [Fact, AutoInitShutdown]
  1815. public void WordWrap_preserveTrailingSpaces_Vertical_With_Wide_Runes ()
  1816. {
  1817. var text = "文に は言葉 があり ます。";
  1818. var width = 8;
  1819. var height = 4;
  1820. var wrappedLines = TextFormatter.WordWrap (text, width, true);
  1821. var breakLines = "";
  1822. foreach (var line in wrappedLines) {
  1823. breakLines += $"{line}{Environment.NewLine}";
  1824. }
  1825. var label = new Label (breakLines) {
  1826. TextDirection = TextDirection.TopBottom_LeftRight,
  1827. Width = Dim.Fill (),
  1828. Height = Dim.Fill ()
  1829. };
  1830. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  1831. frame.Add (label);
  1832. Application.Top.Add (frame);
  1833. Application.Begin (Application.Top);
  1834. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  1835. Assert.False (label.AutoSize);
  1836. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  1837. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  1838. var expected = @"
  1839. ┌────────┐
  1840. │文はがま│
  1841. │に言あす│
  1842. │ 葉り。│
  1843. │ │
  1844. └────────┘
  1845. ";
  1846. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1847. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  1848. }
  1849. [Fact]
  1850. public void WordWrap_preserveTrailingSpaces_With_Tab ()
  1851. {
  1852. var text = ustring.Empty;
  1853. int maxWidth = 1;
  1854. int expectedClippedWidth = 1;
  1855. List<ustring> wrappedLines;
  1856. text = "A sentence\t\t\t has words.";
  1857. var tabWidth = 4;
  1858. maxWidth = 14;
  1859. expectedClippedWidth = 11;
  1860. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1861. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1862. Assert.Equal ("A sentence\t", wrappedLines [0].ToString ());
  1863. Assert.Equal ("\t\t has ", wrappedLines [1].ToString ());
  1864. Assert.Equal ("words.", wrappedLines [2].ToString ());
  1865. Assert.True (wrappedLines.Count == 3);
  1866. maxWidth = 3;
  1867. expectedClippedWidth = 3;
  1868. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1869. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1870. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1871. Assert.Equal ("sen", wrappedLines [1].ToString ());
  1872. Assert.Equal ("ten", wrappedLines [2].ToString ());
  1873. Assert.Equal ("ce", wrappedLines [3].ToString ());
  1874. Assert.Equal ("\t", wrappedLines [4].ToString ());
  1875. Assert.Equal ("\t", wrappedLines [5].ToString ());
  1876. Assert.Equal ("\t", wrappedLines [6].ToString ());
  1877. Assert.Equal (" ", wrappedLines [7].ToString ());
  1878. Assert.Equal ("has", wrappedLines [8].ToString ());
  1879. Assert.Equal (" ", wrappedLines [9].ToString ());
  1880. Assert.Equal ("wor", wrappedLines [10].ToString ());
  1881. Assert.Equal ("ds.", wrappedLines [^1].ToString ());
  1882. Assert.True (wrappedLines.Count == 12);
  1883. maxWidth = 2;
  1884. expectedClippedWidth = 2;
  1885. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1886. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1887. Assert.Equal ("A ", wrappedLines [0].ToString ());
  1888. Assert.Equal ("se", wrappedLines [1].ToString ());
  1889. Assert.Equal ("nt", wrappedLines [2].ToString ());
  1890. Assert.Equal ("en", wrappedLines [3].ToString ());
  1891. Assert.Equal ("ce", wrappedLines [4].ToString ());
  1892. Assert.Equal ("\t", wrappedLines [5].ToString ());
  1893. Assert.Equal ("\t", wrappedLines [6].ToString ());
  1894. Assert.Equal ("\t", wrappedLines [7].ToString ());
  1895. Assert.Equal (" ", wrappedLines [8].ToString ());
  1896. Assert.Equal ("ha", wrappedLines [9].ToString ());
  1897. Assert.Equal ("s ", wrappedLines [10].ToString ());
  1898. Assert.Equal ("wo", wrappedLines [11].ToString ());
  1899. Assert.Equal ("rd", wrappedLines [12].ToString ());
  1900. Assert.Equal ("s.", wrappedLines [^1].ToString ());
  1901. Assert.True (wrappedLines.Count == 14);
  1902. maxWidth = 1;
  1903. expectedClippedWidth = 1;
  1904. wrappedLines = TextFormatter.WordWrap (text, maxWidth, true, tabWidth);
  1905. Assert.True (expectedClippedWidth >= wrappedLines.Max (l => l.RuneCount));
  1906. Assert.Equal ("A", wrappedLines [0].ToString ());
  1907. Assert.Equal (" ", wrappedLines [1].ToString ());
  1908. Assert.Equal ("s", wrappedLines [2].ToString ());
  1909. Assert.Equal ("e", wrappedLines [3].ToString ());
  1910. Assert.Equal ("n", wrappedLines [4].ToString ());
  1911. Assert.Equal ("t", wrappedLines [5].ToString ());
  1912. Assert.Equal ("e", wrappedLines [6].ToString ());
  1913. Assert.Equal ("n", wrappedLines [7].ToString ());
  1914. Assert.Equal ("c", wrappedLines [8].ToString ());
  1915. Assert.Equal ("e", wrappedLines [9].ToString ());
  1916. Assert.Equal ("\t", wrappedLines [10].ToString ());
  1917. Assert.Equal ("\t", wrappedLines [11].ToString ());
  1918. Assert.Equal ("\t", wrappedLines [12].ToString ());
  1919. Assert.Equal (" ", wrappedLines [13].ToString ());
  1920. Assert.Equal (".", wrappedLines [^1].ToString ());
  1921. Assert.True (wrappedLines.Count == text.Length);
  1922. }
  1923. [Fact]
  1924. public void WordWrap_Unicode_Wide_Runes ()
  1925. {
  1926. ustring text = "これが最初の行です。 こんにちは世界。 これが2行目です。";
  1927. var width = text.RuneCount;
  1928. var wrappedLines = TextFormatter.WordWrap (text, width);
  1929. Assert.Equal (3, wrappedLines.Count);
  1930. Assert.Equal ("これが最初の行です。", wrappedLines [0].ToString ());
  1931. Assert.Equal ("こんにちは世界。", wrappedLines [1].ToString ());
  1932. Assert.Equal ("これが2行目です。", wrappedLines [^1].ToString ());
  1933. }
  1934. [Fact]
  1935. public void ReplaceHotKeyWithTag ()
  1936. {
  1937. var tf = new TextFormatter ();
  1938. ustring text = "test";
  1939. int hotPos = 0;
  1940. uint tag = tf.HotKeyTagMask | 't';
  1941. Assert.Equal (ustring.Make (new Rune [] { tag, 'e', 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1942. tag = tf.HotKeyTagMask | 'e';
  1943. hotPos = 1;
  1944. Assert.Equal (ustring.Make (new Rune [] { 't', tag, 's', 't' }), tf.ReplaceHotKeyWithTag (text, hotPos));
  1945. var result = tf.ReplaceHotKeyWithTag (text, hotPos);
  1946. Assert.Equal ('e', (uint)(result.ToRunes () [1] & ~tf.HotKeyTagMask));
  1947. text = "Ok";
  1948. tag = 0x100000 | 'O';
  1949. hotPos = 0;
  1950. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1951. Assert.Equal ('O', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1952. text = "[◦ Ok ◦]";
  1953. text = ustring.Make (new Rune [] { '[', '◦', ' ', 'O', 'k', ' ', '◦', ']' });
  1954. var runes = text.ToRuneList ();
  1955. Assert.Equal (text.RuneCount, runes.Count);
  1956. Assert.Equal (text, ustring.Make (runes));
  1957. tag = tf.HotKeyTagMask | 'O';
  1958. hotPos = 3;
  1959. Assert.Equal (ustring.Make (new Rune [] { '[', '◦', ' ', tag, 'k', ' ', '◦', ']' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1960. Assert.Equal ('O', (uint)(result.ToRunes () [3] & ~tf.HotKeyTagMask));
  1961. text = "^k";
  1962. tag = '^';
  1963. hotPos = 0;
  1964. Assert.Equal (ustring.Make (new Rune [] { tag, 'k' }), result = tf.ReplaceHotKeyWithTag (text, hotPos));
  1965. Assert.Equal ('^', (uint)(result.ToRunes () [0] & ~tf.HotKeyTagMask));
  1966. }
  1967. [Fact]
  1968. public void Reformat_Invalid ()
  1969. {
  1970. var text = ustring.Empty;
  1971. var list = new List<ustring> ();
  1972. Assert.Throws<ArgumentOutOfRangeException> (() => TextFormatter.Format (text, -1, TextAlignment.Left, false));
  1973. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1974. Assert.NotEmpty (list);
  1975. Assert.True (list.Count == 1);
  1976. Assert.Equal (ustring.Empty, list [0]);
  1977. text = null;
  1978. list = TextFormatter.Format (text, 0, TextAlignment.Left, false);
  1979. Assert.NotEmpty (list);
  1980. Assert.True (list.Count == 1);
  1981. Assert.Equal (ustring.Empty, list [0]);
  1982. list = TextFormatter.Format (text, 0, TextAlignment.Left, true);
  1983. Assert.NotEmpty (list);
  1984. Assert.True (list.Count == 1);
  1985. Assert.Equal (ustring.Empty, list [0]);
  1986. }
  1987. [Fact]
  1988. public void Reformat_NoWordrap_SingleLine ()
  1989. {
  1990. var text = ustring.Empty;
  1991. var list = new List<ustring> ();
  1992. var maxWidth = 0;
  1993. var expectedClippedWidth = 0;
  1994. var wrap = false;
  1995. maxWidth = 0;
  1996. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  1997. Assert.True (list.Count == 1);
  1998. maxWidth = 1;
  1999. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2000. Assert.True (list.Count == 1);
  2001. text = "A sentence has words.";
  2002. maxWidth = 0;
  2003. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2004. Assert.True (list.Count == 1);
  2005. Assert.Equal (ustring.Empty, list [0]);
  2006. maxWidth = 1;
  2007. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2008. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2009. Assert.True (list.Count == 1);
  2010. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2011. maxWidth = 5;
  2012. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2013. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2014. Assert.True (list.Count == 1);
  2015. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2016. maxWidth = text.RuneCount - 1;
  2017. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2018. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2019. Assert.True (list.Count == 1);
  2020. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2021. // no clip
  2022. maxWidth = text.RuneCount + 0;
  2023. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2024. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2025. Assert.True (list.Count == 1);
  2026. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2027. maxWidth = text.RuneCount + 1;
  2028. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2029. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2030. Assert.True (list.Count == 1);
  2031. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2032. }
  2033. [Fact]
  2034. public void Reformat_NoWordrap_NewLines ()
  2035. {
  2036. var text = ustring.Empty;
  2037. var list = new List<ustring> ();
  2038. var maxWidth = 0;
  2039. var expectedClippedWidth = 0;
  2040. var wrap = false;
  2041. text = "A sentence has words.\nLine 2.";
  2042. maxWidth = 0;
  2043. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2044. Assert.True (list.Count == 1);
  2045. Assert.Equal (ustring.Empty, list [0]);
  2046. maxWidth = 1;
  2047. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2048. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2049. Assert.True (list.Count == 1);
  2050. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2051. maxWidth = 5;
  2052. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2053. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2054. Assert.True (list.Count == 1);
  2055. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2056. maxWidth = text.RuneCount - 1;
  2057. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2058. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2059. Assert.True (list.Count == 1);
  2060. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2061. // no clip
  2062. maxWidth = text.RuneCount + 0;
  2063. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2064. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2065. Assert.True (list.Count == 1);
  2066. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2067. maxWidth = text.RuneCount + 1;
  2068. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2069. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2070. Assert.True (list.Count == 1);
  2071. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\n", " "), list [0]);
  2072. text = "A sentence has words.\r\nLine 2.";
  2073. maxWidth = 0;
  2074. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2075. Assert.True (list.Count == 1);
  2076. Assert.Equal (ustring.Empty, list [0]);
  2077. maxWidth = 1;
  2078. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2079. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2080. Assert.True (list.Count == 1);
  2081. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2082. maxWidth = 5;
  2083. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2084. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2085. Assert.True (list.Count == 1);
  2086. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2087. maxWidth = text.RuneCount - 1;
  2088. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth) + 1;
  2089. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2090. Assert.True (list.Count == 1);
  2091. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " ").ToString (), list [0].ToString ());
  2092. // no clip
  2093. maxWidth = text.RuneCount + 0;
  2094. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2095. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2096. Assert.True (list.Count == 1);
  2097. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  2098. maxWidth = text.RuneCount + 1;
  2099. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2100. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2101. Assert.True (list.Count == 1);
  2102. Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]).Replace ("\r\n", " "), list [0]);
  2103. }
  2104. [Fact]
  2105. public void Reformat_Wrap_Spaces_No_NewLines ()
  2106. {
  2107. var text = ustring.Empty;
  2108. var list = new List<ustring> ();
  2109. var maxWidth = 0;
  2110. var expectedClippedWidth = 0;
  2111. var wrap = true;
  2112. // Even # of spaces
  2113. // 0123456789
  2114. text = "012 456 89";
  2115. // See WordWrap BUGBUGs above.
  2116. //maxWidth = 0;
  2117. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  2118. //Assert.True (list.Count == 1);
  2119. //Assert.Equal (ustring.Empty, list [0]);
  2120. //maxWidth = 1;
  2121. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2122. //// remove 3 whitespace chars
  2123. //expectedLines = text.RuneCount;
  2124. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  2125. //Assert.Equal (expectedLines, list.Count);
  2126. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2127. ////width = (int)Math.Ceiling ((double)(text.RuneCount / 2F));
  2128. //maxWidth = 5;
  2129. //expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2130. //expectedLines = (int)Math.Ceiling ((double)(text.RuneCount / maxWidth));
  2131. //list = TextFormatter.Reformat (text, maxWidth, TextAlignment.Left, wrap);
  2132. //Assert.Equal (expectedLines, list.Count);
  2133. //Assert.Equal (ustring.Make (text.ToRunes () [0..expectedClippedWidth]), list [0]);
  2134. maxWidth = text.RuneCount - 1;
  2135. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2136. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2137. Assert.True (list.Count == 2);
  2138. Assert.Equal ("012 456", list [0]);
  2139. Assert.Equal ("89", list [1]);
  2140. // no clip
  2141. maxWidth = text.RuneCount + 0;
  2142. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2143. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2144. Assert.True (list.Count == 1);
  2145. Assert.Equal ("012 456 89", list [0]);
  2146. maxWidth = text.RuneCount + 1;
  2147. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2148. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2149. Assert.True (list.Count == 1);
  2150. Assert.Equal ("012 456 89", list [0]);
  2151. // Odd # of spaces
  2152. // 0123456789
  2153. text = "012 456 89 end";
  2154. maxWidth = text.RuneCount - 1;
  2155. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2156. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2157. Assert.True (list.Count == 2);
  2158. Assert.Equal ("012 456 89", list [0]);
  2159. Assert.Equal ("end", list [1]);
  2160. // no clip
  2161. maxWidth = text.RuneCount + 0;
  2162. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2163. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2164. Assert.True (list.Count == 1);
  2165. Assert.Equal ("012 456 89 end", list [0]);
  2166. maxWidth = text.RuneCount + 1;
  2167. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2168. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2169. Assert.True (list.Count == 1);
  2170. Assert.Equal ("012 456 89 end", list [0]);
  2171. }
  2172. [Fact]
  2173. public void Reformat_Unicode_Wrap_Spaces_No_NewLines ()
  2174. {
  2175. var text = ustring.Empty;
  2176. var list = new List<ustring> ();
  2177. var maxWidth = 0;
  2178. var expectedClippedWidth = 0;
  2179. var wrap = true;
  2180. // Unicode
  2181. // Even # of chars
  2182. // 0123456789
  2183. text = "\u2660пÑРвРÑ";
  2184. maxWidth = text.RuneCount - 1;
  2185. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2186. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2187. Assert.True (list.Count == 2);
  2188. Assert.Equal ("\u2660пÑРвÐ", list [0]);
  2189. Assert.Equal ("Ñ", list [1]);
  2190. // no clip
  2191. maxWidth = text.RuneCount + 0;
  2192. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2193. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2194. Assert.True (list.Count == 1);
  2195. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  2196. maxWidth = text.RuneCount + 1;
  2197. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2198. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2199. Assert.True (list.Count == 1);
  2200. Assert.Equal ("\u2660пÑРвРÑ", list [0]);
  2201. // Unicode
  2202. // Odd # of chars
  2203. // 0123456789
  2204. text = "\u2660 ÑРвРÑ";
  2205. maxWidth = text.RuneCount - 1;
  2206. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2207. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2208. Assert.True (list.Count == 2);
  2209. Assert.Equal ("\u2660 ÑРвÐ", list [0]);
  2210. Assert.Equal ("Ñ", list [1]);
  2211. // no clip
  2212. maxWidth = text.RuneCount + 0;
  2213. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2214. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2215. Assert.True (list.Count == 1);
  2216. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  2217. maxWidth = text.RuneCount + 1;
  2218. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2219. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2220. Assert.True (list.Count == 1);
  2221. Assert.Equal ("\u2660 ÑРвРÑ", list [0]);
  2222. }
  2223. [Fact]
  2224. public void Reformat_Unicode_Wrap_Spaces_NewLines ()
  2225. {
  2226. var text = ustring.Empty;
  2227. var list = new List<ustring> ();
  2228. var maxWidth = 0;
  2229. var expectedClippedWidth = 0;
  2230. var wrap = true;
  2231. // Unicode
  2232. text = "\u2460\u2461\u2462\n\u2460\u2461\u2462\u2463\u2464";
  2233. maxWidth = text.RuneCount - 1;
  2234. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2235. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2236. Assert.Equal (2, list.Count);
  2237. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2238. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2239. // no clip
  2240. maxWidth = text.RuneCount + 0;
  2241. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2242. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2243. Assert.Equal (2, list.Count);
  2244. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2245. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2246. maxWidth = text.RuneCount + 1;
  2247. expectedClippedWidth = Math.Min (text.RuneCount, maxWidth);
  2248. list = TextFormatter.Format (text, maxWidth, TextAlignment.Left, wrap);
  2249. Assert.Equal (2, list.Count);
  2250. Assert.Equal ("\u2460\u2461\u2462", list [0]);
  2251. Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
  2252. }
  2253. [Fact]
  2254. public void System_Rune_ColumnWidth ()
  2255. {
  2256. var c = new System.Rune ('a');
  2257. Assert.Equal (1, Rune.ColumnWidth (c));
  2258. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2259. Assert.Equal (1, ustring.Make (c).Length);
  2260. c = new System.Rune ('b');
  2261. Assert.Equal (1, Rune.ColumnWidth (c));
  2262. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2263. Assert.Equal (1, ustring.Make (c).Length);
  2264. c = new System.Rune (123);
  2265. Assert.Equal (1, Rune.ColumnWidth (c));
  2266. Assert.Equal (1, ustring.Make (c).ConsoleWidth);
  2267. Assert.Equal (1, ustring.Make (c).Length);
  2268. c = new System.Rune ('\u1150');
  2269. Assert.Equal (2, Rune.ColumnWidth (c)); // 0x1150 ᅐ Unicode Technical Report #11
  2270. Assert.Equal (2, ustring.Make (c).ConsoleWidth);
  2271. Assert.Equal (3, ustring.Make (c).Length);
  2272. c = new System.Rune ('\u1161');
  2273. Assert.Equal (0, Rune.ColumnWidth (c)); // 0x1161 ᅡ column width of 0
  2274. Assert.Equal (0, ustring.Make (c).ConsoleWidth);
  2275. Assert.Equal (3, ustring.Make (c).Length);
  2276. c = new System.Rune (31);
  2277. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2278. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2279. Assert.Equal (1, ustring.Make (c).Length);
  2280. c = new System.Rune (127);
  2281. Assert.Equal (-1, Rune.ColumnWidth (c)); // non printable character
  2282. Assert.Equal (-1, ustring.Make (c).ConsoleWidth);
  2283. Assert.Equal (1, ustring.Make (c).Length);
  2284. }
  2285. [Fact]
  2286. public void System_Text_Rune ()
  2287. {
  2288. var c = new System.Text.Rune ('a');
  2289. Assert.Equal (1, c.Utf8SequenceLength);
  2290. c = new System.Text.Rune ('b');
  2291. Assert.Equal (1, c.Utf8SequenceLength);
  2292. c = new System.Text.Rune (123);
  2293. Assert.Equal (1, c.Utf8SequenceLength);
  2294. c = new System.Text.Rune ('\u1150');
  2295. Assert.Equal (3, c.Utf8SequenceLength); // 0x1150 ᅐ Unicode Technical Report #11
  2296. c = new System.Text.Rune ('\u1161');
  2297. Assert.Equal (3, c.Utf8SequenceLength); // 0x1161 ᅡ column width of 0
  2298. c = new System.Text.Rune (31);
  2299. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2300. c = new System.Text.Rune (127);
  2301. Assert.Equal (1, c.Utf8SequenceLength); // non printable character
  2302. }
  2303. [Fact]
  2304. public void Format_WordWrap_preserveTrailingSpaces ()
  2305. {
  2306. ustring text = " A sentence has words. \n This is the second Line - 2. ";
  2307. // With preserveTrailingSpaces = false by default.
  2308. var list1 = TextFormatter.Format (text, 4, TextAlignment.Left, true);
  2309. ustring wrappedText1 = ustring.Empty;
  2310. Assert.Equal (" A", list1 [0].ToString ());
  2311. Assert.Equal ("sent", list1 [1].ToString ());
  2312. Assert.Equal ("ence", list1 [2].ToString ());
  2313. Assert.Equal ("has", list1 [3].ToString ());
  2314. Assert.Equal ("word", list1 [4].ToString ());
  2315. Assert.Equal ("s. ", list1 [5].ToString ());
  2316. Assert.Equal (" Thi", list1 [6].ToString ());
  2317. Assert.Equal ("s is", list1 [7].ToString ());
  2318. Assert.Equal ("the", list1 [8].ToString ());
  2319. Assert.Equal ("seco", list1 [9].ToString ());
  2320. Assert.Equal ("nd", list1 [10].ToString ());
  2321. Assert.Equal ("Line", list1 [11].ToString ());
  2322. Assert.Equal ("- 2.", list1 [^1].ToString ());
  2323. foreach (var txt in list1) {
  2324. wrappedText1 += txt;
  2325. }
  2326. Assert.Equal (" Asentencehaswords. This isthesecondLine- 2.", wrappedText1);
  2327. // With preserveTrailingSpaces = true.
  2328. var list2 = TextFormatter.Format (text, 4, TextAlignment.Left, true, true);
  2329. ustring wrappedText2 = ustring.Empty;
  2330. Assert.Equal (" A ", list2 [0].ToString ());
  2331. Assert.Equal ("sent", list2 [1].ToString ());
  2332. Assert.Equal ("ence", list2 [2].ToString ());
  2333. Assert.Equal (" has", list2 [3].ToString ());
  2334. Assert.Equal (" ", list2 [4].ToString ());
  2335. Assert.Equal ("word", list2 [5].ToString ());
  2336. Assert.Equal ("s. ", list2 [6].ToString ());
  2337. Assert.Equal (" ", list2 [7].ToString ());
  2338. Assert.Equal ("This", list2 [8].ToString ());
  2339. Assert.Equal (" is ", list2 [9].ToString ());
  2340. Assert.Equal ("the ", list2 [10].ToString ());
  2341. Assert.Equal ("seco", list2 [11].ToString ());
  2342. Assert.Equal ("nd ", list2 [12].ToString ());
  2343. Assert.Equal ("Line", list2 [13].ToString ());
  2344. Assert.Equal (" - ", list2 [14].ToString ());
  2345. Assert.Equal ("2. ", list2 [^1].ToString ());
  2346. foreach (var txt in list2) {
  2347. wrappedText2 += txt;
  2348. }
  2349. Assert.Equal (" A sentence has words. This is the second Line - 2. ", wrappedText2);
  2350. }
  2351. [Fact]
  2352. public void Format_Throw_ArgumentException_With_WordWrap_As_False_And_Keep_End_Spaces_As_True ()
  2353. {
  2354. Assert.Throws<ArgumentException> (() => TextFormatter.Format ("Some text", 4, TextAlignment.Left, false, true));
  2355. }
  2356. [Fact]
  2357. public void Draw_Horizontal_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2358. {
  2359. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2360. var top = Application.Top;
  2361. var view = new View ("view") { X = -2 };
  2362. top.Add (view);
  2363. Application.Iteration += () => {
  2364. Assert.Equal (-2, view.X);
  2365. Application.RequestStop ();
  2366. };
  2367. try {
  2368. Application.Run ();
  2369. } catch (IndexOutOfRangeException ex) {
  2370. // After the fix this exception will not be caught.
  2371. Assert.IsType<IndexOutOfRangeException> (ex);
  2372. }
  2373. // Shutdown must be called to safely clean up Application if Init has been called
  2374. Application.Shutdown ();
  2375. }
  2376. [Fact]
  2377. public void TestClipOrPad_ShortWord ()
  2378. {
  2379. // word is short but we want it to fill 6 so it should be padded
  2380. Assert.Equal ("fff ", TextFormatter.ClipOrPad ("fff", 6));
  2381. }
  2382. [Fact]
  2383. public void TestClipOrPad_LongWord ()
  2384. {
  2385. // word is long but we want it to fill 3 space only
  2386. Assert.Equal ("123", TextFormatter.ClipOrPad ("123456789", 3));
  2387. }
  2388. [Fact]
  2389. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  2390. {
  2391. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  2392. var top = Application.Top;
  2393. var view = new View ("view") {
  2394. Y = -2,
  2395. Height = 10,
  2396. TextDirection = TextDirection.TopBottom_LeftRight
  2397. };
  2398. top.Add (view);
  2399. Application.Iteration += () => {
  2400. Assert.Equal (-2, view.Y);
  2401. Application.RequestStop ();
  2402. };
  2403. try {
  2404. Application.Run ();
  2405. } catch (IndexOutOfRangeException ex) {
  2406. // After the fix this exception will not be caught.
  2407. Assert.IsType<IndexOutOfRangeException> (ex);
  2408. }
  2409. // Shutdown must be called to safely clean up Application if Init has been called
  2410. Application.Shutdown ();
  2411. }
  2412. [Fact]
  2413. public void Internal_Tests ()
  2414. {
  2415. var tf = new TextFormatter ();
  2416. Assert.Equal (Key.Null, tf.HotKey);
  2417. tf.HotKey = Key.CtrlMask | Key.Q;
  2418. Assert.Equal (Key.CtrlMask | Key.Q, tf.HotKey);
  2419. }
  2420. [Fact, AutoInitShutdown]
  2421. public void Draw_Horizontal_Simple_Runes ()
  2422. {
  2423. var label = new Label ("Demo Simple Rune");
  2424. Application.Top.Add (label);
  2425. Application.Begin (Application.Top);
  2426. Assert.True (label.AutoSize);
  2427. Assert.Equal (new Rect (0, 0, 16, 1), label.Frame);
  2428. var expected = @"
  2429. Demo Simple Rune
  2430. ";
  2431. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2432. Assert.Equal (new Rect (0, 0, 16, 1), pos);
  2433. }
  2434. [Fact, AutoInitShutdown]
  2435. public void Draw_Vertical_Simple_Runes ()
  2436. {
  2437. var label = new Label ("Demo Simple Rune") {
  2438. TextDirection = TextDirection.TopBottom_LeftRight
  2439. };
  2440. Application.Top.Add (label);
  2441. Application.Begin (Application.Top);
  2442. Assert.True (label.AutoSize);
  2443. Assert.Equal (new Rect (0, 0, 1, 16), label.Frame);
  2444. var expected = @"
  2445. D
  2446. e
  2447. m
  2448. o
  2449. S
  2450. i
  2451. m
  2452. p
  2453. l
  2454. e
  2455. R
  2456. u
  2457. n
  2458. e
  2459. ";
  2460. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2461. Assert.Equal (new Rect (0, 0, 1, 16), pos);
  2462. }
  2463. [Fact, AutoInitShutdown]
  2464. public void Draw_Horizontal_Wide_Runes ()
  2465. {
  2466. var label = new Label ("デモエムポンズ");
  2467. Application.Top.Add (label);
  2468. Application.Begin (Application.Top);
  2469. Assert.True (label.AutoSize);
  2470. Assert.Equal (new Rect (0, 0, 14, 1), label.Frame);
  2471. var expected = @"
  2472. デモエムポンズ
  2473. ";
  2474. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2475. Assert.Equal (new Rect (0, 0, 14, 1), pos);
  2476. }
  2477. [Fact, AutoInitShutdown]
  2478. public void Draw_Vertical_Wide_Runes ()
  2479. {
  2480. var label = new Label ("デモエムポンズ") {
  2481. TextDirection = TextDirection.TopBottom_LeftRight
  2482. };
  2483. Application.Top.Add (label);
  2484. Application.Begin (Application.Top);
  2485. Assert.True (label.AutoSize);
  2486. Assert.Equal (new Rect (0, 0, 2, 7), label.Frame);
  2487. var expected = @"
  2488. ";
  2489. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2490. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  2491. }
  2492. [Fact, AutoInitShutdown]
  2493. public void Draw_Horizontal_Simple_TextAlignments ()
  2494. {
  2495. var text = "Hello World";
  2496. var width = 20;
  2497. var lblLeft = new Label (text) { Width = width };
  2498. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2499. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2500. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2501. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2502. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2503. Application.Top.Add (frame);
  2504. Application.Begin (Application.Top);
  2505. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2506. Assert.False (lblLeft.AutoSize);
  2507. Assert.False (lblCenter.AutoSize);
  2508. Assert.False (lblRight.AutoSize);
  2509. Assert.False (lblJust.AutoSize);
  2510. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2511. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2512. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2513. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2514. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2515. var expected = @"
  2516. ┌────────────────────┐
  2517. │Hello World │
  2518. │ Hello World │
  2519. │ Hello World│
  2520. │Hello World│
  2521. └────────────────────┘
  2522. ";
  2523. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2524. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2525. }
  2526. [Fact, AutoInitShutdown]
  2527. public void Draw_Vertical_Simple_TextAlignments ()
  2528. {
  2529. var text = "Hello World";
  2530. var height = 20;
  2531. var lblLeft = new Label (text, direction: TextDirection.TopBottom_LeftRight) { Height = height };
  2532. var lblCenter = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 2, Height = height, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2533. var lblRight = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 4, Height = height, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2534. var lblJust = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 6, Height = height, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2535. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2536. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2537. Application.Top.Add (frame);
  2538. Application.Begin (Application.Top);
  2539. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  2540. Assert.False (lblLeft.AutoSize);
  2541. Assert.False (lblCenter.AutoSize);
  2542. Assert.False (lblRight.AutoSize);
  2543. Assert.False (lblJust.AutoSize);
  2544. Assert.Equal (new Rect (0, 0, 1, height), lblLeft.Frame);
  2545. Assert.Equal (new Rect (2, 0, 1, height), lblCenter.Frame);
  2546. Assert.Equal (new Rect (4, 0, 1, height), lblRight.Frame);
  2547. Assert.Equal (new Rect (6, 0, 1, height), lblJust.Frame);
  2548. Assert.Equal (new Rect (0, 0, 9, height + 2), frame.Frame);
  2549. var expected = @"
  2550. ┌───────┐
  2551. │H H│
  2552. │e e│
  2553. │l l│
  2554. │l l│
  2555. │o H o│
  2556. │ e │
  2557. │W l │
  2558. │o l │
  2559. │r o │
  2560. │l H │
  2561. │d W e │
  2562. │ o l │
  2563. │ r l │
  2564. │ l o │
  2565. │ d │
  2566. │ W W│
  2567. │ o o│
  2568. │ r r│
  2569. │ l l│
  2570. │ d d│
  2571. └───────┘
  2572. ";
  2573. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2574. Assert.Equal (new Rect (0, 0, 9, height + 2), pos);
  2575. }
  2576. [Fact, AutoInitShutdown]
  2577. public void Draw_Horizontal_Wide_TextAlignments ()
  2578. {
  2579. var text = "こんにちは 世界";
  2580. var width = 25;
  2581. var lblLeft = new Label (text) { Width = width };
  2582. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  2583. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  2584. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  2585. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2586. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2587. Application.Top.Add (frame);
  2588. Application.Begin (Application.Top);
  2589. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2590. Assert.False (lblLeft.AutoSize);
  2591. Assert.False (lblCenter.AutoSize);
  2592. Assert.False (lblRight.AutoSize);
  2593. Assert.False (lblJust.AutoSize);
  2594. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  2595. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  2596. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  2597. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  2598. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  2599. var expected = @"
  2600. ┌─────────────────────────┐
  2601. │こんにちは 世界 │
  2602. │ こんにちは 世界 │
  2603. │ こんにちは 世界│
  2604. │こんにちは 世界│
  2605. └─────────────────────────┘
  2606. ";
  2607. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2608. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  2609. }
  2610. [Fact, AutoInitShutdown]
  2611. public void Draw_Vertical_Wide_TextAlignments ()
  2612. {
  2613. var text = "こんにちは 世界";
  2614. var height = 23;
  2615. var lblLeft = new Label (text) { Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight };
  2616. var lblCenter = new Label (text) { X = 3, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Middle };
  2617. var lblRight = new Label (text) { X = 6, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  2618. var lblJust = new Label (text) { X = 9, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Justified };
  2619. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  2620. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2621. Application.Top.Add (frame);
  2622. Application.Begin (Application.Top);
  2623. ((FakeDriver)Application.Driver).SetBufferSize (13, height + 2);
  2624. Assert.False (lblLeft.AutoSize);
  2625. Assert.False (lblCenter.AutoSize);
  2626. Assert.False (lblRight.AutoSize);
  2627. Assert.False (lblJust.AutoSize);
  2628. Assert.Equal (new Rect (0, 0, 2, height), lblLeft.Frame);
  2629. Assert.Equal (new Rect (3, 0, 2, height), lblCenter.Frame);
  2630. Assert.Equal (new Rect (6, 0, 2, height), lblRight.Frame);
  2631. Assert.Equal (new Rect (9, 0, 2, height), lblJust.Frame);
  2632. Assert.Equal (new Rect (0, 0, 13, height + 2), frame.Frame);
  2633. var expected = @"
  2634. ┌───────────┐
  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. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2661. Assert.Equal (new Rect (0, 0, 13, height + 2), pos);
  2662. }
  2663. [Fact]
  2664. public void GetTextWidth_Simple_And_Wide_Runes ()
  2665. {
  2666. ustring text = "Hello World";
  2667. Assert.Equal (11, TextFormatter.GetTextWidth (text));
  2668. text = "こんにちは世界";
  2669. Assert.Equal (14, TextFormatter.GetTextWidth (text));
  2670. }
  2671. [Fact]
  2672. public void GetSumMaxCharWidth_Simple_And_Wide_Runes ()
  2673. {
  2674. ustring text = "Hello World";
  2675. Assert.Equal (11, TextFormatter.GetSumMaxCharWidth (text));
  2676. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2677. text = "こんにちは 世界";
  2678. Assert.Equal (15, TextFormatter.GetSumMaxCharWidth (text));
  2679. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 6, 1));
  2680. }
  2681. [Fact]
  2682. public void GetSumMaxCharWidth_List_Simple_And_Wide_Runes ()
  2683. {
  2684. List<ustring> text =new List<ustring>() { "Hello", "World" };
  2685. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text));
  2686. Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2687. text = new List<ustring> () { "こんにちは", "世界" };
  2688. Assert.Equal (4, TextFormatter.GetSumMaxCharWidth (text));
  2689. Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text, 1, 1));
  2690. }
  2691. [Fact]
  2692. public void GetMaxLengthForWidth_Simple_And_Wide_Runes ()
  2693. {
  2694. ustring text = "Hello World";
  2695. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (text, 6));
  2696. text = "こんにちは 世界";
  2697. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (text, 6));
  2698. }
  2699. [Fact]
  2700. public void GetMaxLengthForWidth_List_Simple_And_Wide_Runes ()
  2701. {
  2702. var runes = ustring.Make ("Hello World").ToRuneList ();
  2703. Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2704. runes = ustring.Make ("こんにちは 世界").ToRuneList ();
  2705. Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (runes, 6));
  2706. }
  2707. [Fact]
  2708. public void Format_Truncate_Simple_And_Wide_Runes ()
  2709. {
  2710. var text = "Truncate";
  2711. var list = TextFormatter.Format (text, 3, false, false);
  2712. Assert.Equal ("Tru", list [^1].ToString ());
  2713. text = "デモエムポンズ";
  2714. list = TextFormatter.Format (text, 3, false, false);
  2715. Assert.Equal ("デ", list [^1].ToString ());
  2716. }
  2717. }
  2718. }