LineCanvasTests.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.DrawingTests;
  4. public class LineCanvasTests (ITestOutputHelper _output)
  5. {
  6. [Theory]
  7. // Horizontal lines with a vertical zero-length
  8. [InlineData (
  9. 0,
  10. 0,
  11. 1,
  12. Orientation.Horizontal,
  13. LineStyle.Double,
  14. 0,
  15. 0,
  16. 0,
  17. Orientation.Vertical,
  18. LineStyle.Single,
  19. "╞"
  20. )]
  21. [InlineData (
  22. 0,
  23. 0,
  24. -1,
  25. Orientation.Horizontal,
  26. LineStyle.Double,
  27. 0,
  28. 0,
  29. 0,
  30. Orientation.Vertical,
  31. LineStyle.Single,
  32. "╡"
  33. )]
  34. [InlineData (
  35. 0,
  36. 0,
  37. 1,
  38. Orientation.Horizontal,
  39. LineStyle.Single,
  40. 0,
  41. 0,
  42. 0,
  43. Orientation.Vertical,
  44. LineStyle.Double,
  45. "╟"
  46. )]
  47. [InlineData (
  48. 0,
  49. 0,
  50. -1,
  51. Orientation.Horizontal,
  52. LineStyle.Single,
  53. 0,
  54. 0,
  55. 0,
  56. Orientation.Vertical,
  57. LineStyle.Double,
  58. "╢"
  59. )]
  60. [InlineData (
  61. 0,
  62. 0,
  63. 1,
  64. Orientation.Horizontal,
  65. LineStyle.Single,
  66. 0,
  67. 0,
  68. 0,
  69. Orientation.Vertical,
  70. LineStyle.Single,
  71. "├"
  72. )]
  73. [InlineData (
  74. 0,
  75. 0,
  76. -1,
  77. Orientation.Horizontal,
  78. LineStyle.Single,
  79. 0,
  80. 0,
  81. 0,
  82. Orientation.Vertical,
  83. LineStyle.Single,
  84. "┤"
  85. )]
  86. [InlineData (
  87. 0,
  88. 0,
  89. 1,
  90. Orientation.Horizontal,
  91. LineStyle.Double,
  92. 0,
  93. 0,
  94. 0,
  95. Orientation.Vertical,
  96. LineStyle.Double,
  97. "╠"
  98. )]
  99. [InlineData (
  100. 0,
  101. 0,
  102. -1,
  103. Orientation.Horizontal,
  104. LineStyle.Double,
  105. 0,
  106. 0,
  107. 0,
  108. Orientation.Vertical,
  109. LineStyle.Double,
  110. "╣"
  111. )]
  112. // Vertical lines with a horizontal zero-length
  113. [InlineData (
  114. 0,
  115. 0,
  116. 1,
  117. Orientation.Vertical,
  118. LineStyle.Double,
  119. 0,
  120. 0,
  121. 0,
  122. Orientation.Horizontal,
  123. LineStyle.Single,
  124. "╥"
  125. )]
  126. [InlineData (
  127. 0,
  128. 0,
  129. -1,
  130. Orientation.Vertical,
  131. LineStyle.Double,
  132. 0,
  133. 0,
  134. 0,
  135. Orientation.Horizontal,
  136. LineStyle.Single,
  137. "╨"
  138. )]
  139. [InlineData (
  140. 0,
  141. 0,
  142. 1,
  143. Orientation.Vertical,
  144. LineStyle.Single,
  145. 0,
  146. 0,
  147. 0,
  148. Orientation.Horizontal,
  149. LineStyle.Double,
  150. "╤"
  151. )]
  152. [InlineData (
  153. 0,
  154. 0,
  155. -1,
  156. Orientation.Vertical,
  157. LineStyle.Single,
  158. 0,
  159. 0,
  160. 0,
  161. Orientation.Horizontal,
  162. LineStyle.Double,
  163. "╧"
  164. )]
  165. [InlineData (
  166. 0,
  167. 0,
  168. 1,
  169. Orientation.Vertical,
  170. LineStyle.Single,
  171. 0,
  172. 0,
  173. 0,
  174. Orientation.Horizontal,
  175. LineStyle.Single,
  176. "┬"
  177. )]
  178. [InlineData (
  179. 0,
  180. 0,
  181. -1,
  182. Orientation.Vertical,
  183. LineStyle.Single,
  184. 0,
  185. 0,
  186. 0,
  187. Orientation.Horizontal,
  188. LineStyle.Single,
  189. "┴"
  190. )]
  191. [InlineData (
  192. 0,
  193. 0,
  194. 1,
  195. Orientation.Vertical,
  196. LineStyle.Double,
  197. 0,
  198. 0,
  199. 0,
  200. Orientation.Horizontal,
  201. LineStyle.Double,
  202. "╦"
  203. )]
  204. [InlineData (
  205. 0,
  206. 0,
  207. -1,
  208. Orientation.Vertical,
  209. LineStyle.Double,
  210. 0,
  211. 0,
  212. 0,
  213. Orientation.Horizontal,
  214. LineStyle.Double,
  215. "╩"
  216. )]
  217. // Crosses (two zero-length)
  218. [InlineData (
  219. 0,
  220. 0,
  221. 0,
  222. Orientation.Vertical,
  223. LineStyle.Double,
  224. 0,
  225. 0,
  226. 0,
  227. Orientation.Horizontal,
  228. LineStyle.Single,
  229. "╫"
  230. )]
  231. [InlineData (
  232. 0,
  233. 0,
  234. 0,
  235. Orientation.Vertical,
  236. LineStyle.Single,
  237. 0,
  238. 0,
  239. 0,
  240. Orientation.Horizontal,
  241. LineStyle.Double,
  242. "╪"
  243. )]
  244. [InlineData (
  245. 0,
  246. 0,
  247. 0,
  248. Orientation.Vertical,
  249. LineStyle.Single,
  250. 0,
  251. 0,
  252. 0,
  253. Orientation.Horizontal,
  254. LineStyle.Single,
  255. "┼"
  256. )]
  257. [InlineData (
  258. 0,
  259. 0,
  260. 0,
  261. Orientation.Vertical,
  262. LineStyle.Double,
  263. 0,
  264. 0,
  265. 0,
  266. Orientation.Horizontal,
  267. LineStyle.Double,
  268. "╬"
  269. )]
  270. public void Add_2_Lines (
  271. int x1,
  272. int y1,
  273. int len1,
  274. Orientation o1,
  275. LineStyle s1,
  276. int x2,
  277. int y2,
  278. int len2,
  279. Orientation o2,
  280. LineStyle s2,
  281. string expected
  282. )
  283. {
  284. View v = GetCanvas (out LineCanvas lc);
  285. v.Width = 10;
  286. v.Height = 10;
  287. v.Viewport = new (0, 0, 10, 10);
  288. lc.AddLine (new (x1, y1), len1, o1, s1);
  289. lc.AddLine (new (x2, y2), len2, o2, s2);
  290. TestHelpers.AssertEqual (_output, expected, lc.ToString ());
  291. v.Dispose ();
  292. }
  293. [InlineData (
  294. 0,
  295. 0,
  296. 0,
  297. 0,
  298. 0,
  299. 1,
  300. 1
  301. )]
  302. [InlineData (
  303. 0,
  304. 0,
  305. 1,
  306. 0,
  307. 0,
  308. 1,
  309. 1
  310. )]
  311. [InlineData (
  312. 0,
  313. 0,
  314. 2,
  315. 0,
  316. 0,
  317. 2,
  318. 2
  319. )]
  320. [InlineData (
  321. 0,
  322. 0,
  323. 3,
  324. 0,
  325. 0,
  326. 3,
  327. 3
  328. )]
  329. [InlineData (
  330. 0,
  331. 0,
  332. -1,
  333. 0,
  334. 0,
  335. 1,
  336. 1
  337. )]
  338. [InlineData (
  339. 0,
  340. 0,
  341. -2,
  342. -1,
  343. -1,
  344. 2,
  345. 2
  346. )]
  347. [InlineData (
  348. 0,
  349. 0,
  350. -3,
  351. -2,
  352. -2,
  353. 3,
  354. 3
  355. )]
  356. [Theory]
  357. [SetupFakeDriver]
  358. public void Viewport_H_And_V_Lines_Both_Positive (
  359. int x,
  360. int y,
  361. int length,
  362. int expectedX,
  363. int expectedY,
  364. int expectedWidth,
  365. int expectedHeight
  366. )
  367. {
  368. var canvas = new LineCanvas ();
  369. canvas.AddLine (new (x, y), length, Orientation.Horizontal, LineStyle.Single);
  370. canvas.AddLine (new (x, y), length, Orientation.Vertical, LineStyle.Single);
  371. Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Viewport);
  372. }
  373. [InlineData (
  374. 0,
  375. 0,
  376. 0,
  377. 0,
  378. 0,
  379. 1,
  380. 1
  381. )]
  382. [InlineData (
  383. 0,
  384. 0,
  385. 1,
  386. 0,
  387. 0,
  388. 1,
  389. 1
  390. )]
  391. [InlineData (
  392. 0,
  393. 0,
  394. 2,
  395. 0,
  396. 0,
  397. 2,
  398. 1
  399. )]
  400. [InlineData (
  401. 0,
  402. 0,
  403. 3,
  404. 0,
  405. 0,
  406. 3,
  407. 1
  408. )]
  409. [InlineData (
  410. 0,
  411. 0,
  412. -1,
  413. 0,
  414. 0,
  415. 1,
  416. 1
  417. )]
  418. [InlineData (
  419. 0,
  420. 0,
  421. -2,
  422. -1,
  423. 0,
  424. 2,
  425. 1
  426. )]
  427. [InlineData (
  428. 0,
  429. 0,
  430. -3,
  431. -2,
  432. 0,
  433. 3,
  434. 1
  435. )]
  436. [Theory]
  437. [SetupFakeDriver]
  438. public void Viewport_H_Line (
  439. int x,
  440. int y,
  441. int length,
  442. int expectedX,
  443. int expectedY,
  444. int expectedWidth,
  445. int expectedHeight
  446. )
  447. {
  448. var canvas = new LineCanvas ();
  449. canvas.AddLine (new (x, y), length, Orientation.Horizontal, LineStyle.Single);
  450. Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Viewport);
  451. }
  452. [Fact]
  453. [SetupFakeDriver]
  454. public void Viewport_Specific ()
  455. {
  456. // Draw at 1,1 within client area of View (i.e. leave a top and left margin of 1)
  457. // This proves we aren't drawing excess above
  458. var x = 1;
  459. var y = 2;
  460. var width = 3;
  461. var height = 2;
  462. var lc = new LineCanvas ();
  463. // 01230
  464. // ╔╡╞╗1
  465. // ║ ║2
  466. // Add a short horiz line for ╔╡
  467. lc.AddLine (new (x, y), 2, Orientation.Horizontal, LineStyle.Double);
  468. Assert.Equal (new (x, y, 2, 1), lc.Viewport);
  469. //LHS line down
  470. lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
  471. Assert.Equal (new (x, y, 2, 2), lc.Viewport);
  472. //Vertical line before Title, results in a ╡
  473. lc.AddLine (new (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
  474. Assert.Equal (new (x, y, 2, 2), lc.Viewport);
  475. //Vertical line after Title, results in a ╞
  476. lc.AddLine (new (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
  477. Assert.Equal (new (x, y, 3, 2), lc.Viewport);
  478. // remainder of top line
  479. lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
  480. Assert.Equal (new (x, y, 4, 2), lc.Viewport);
  481. //RHS line down
  482. lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
  483. Assert.Equal (new (x, y, 4, 2), lc.Viewport);
  484. TestHelpers.AssertEqual (
  485. _output,
  486. @"
  487. ╔╡╞╗
  488. ║ ║",
  489. $"{Environment.NewLine}{lc}"
  490. );
  491. }
  492. [Fact]
  493. [SetupFakeDriver]
  494. public void Viewport_Specific_With_Ustring ()
  495. {
  496. // Draw at 1,1 within client area of View (i.e. leave a top and left margin of 1)
  497. // This proves we aren't drawing excess above
  498. var x = 1;
  499. var y = 2;
  500. var width = 3;
  501. var height = 2;
  502. var lc = new LineCanvas ();
  503. // 01230
  504. // ╔╡╞╗1
  505. // ║ ║2
  506. // Add a short horiz line for ╔╡
  507. lc.AddLine (new (x, y), 2, Orientation.Horizontal, LineStyle.Double);
  508. Assert.Equal (new (x, y, 2, 1), lc.Viewport);
  509. //LHS line down
  510. lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
  511. Assert.Equal (new (x, y, 2, 2), lc.Viewport);
  512. //Vertical line before Title, results in a ╡
  513. lc.AddLine (new (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
  514. Assert.Equal (new (x, y, 2, 2), lc.Viewport);
  515. //Vertical line after Title, results in a ╞
  516. lc.AddLine (new (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
  517. Assert.Equal (new (x, y, 3, 2), lc.Viewport);
  518. // remainder of top line
  519. lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
  520. Assert.Equal (new (x, y, 4, 2), lc.Viewport);
  521. //RHS line down
  522. lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
  523. Assert.Equal (new (x, y, 4, 2), lc.Viewport);
  524. TestHelpers.AssertEqual (
  525. _output,
  526. @"
  527. ╔╡╞╗
  528. ║ ║",
  529. $"{Environment.NewLine}{lc}"
  530. );
  531. }
  532. [Fact]
  533. [SetupFakeDriver]
  534. public void Canvas_Updates_On_Changes ()
  535. {
  536. var lc = new LineCanvas ();
  537. Assert.Equal (Rectangle.Empty, lc.Viewport);
  538. lc.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Double);
  539. Assert.NotEqual (Rectangle.Empty, lc.Viewport);
  540. lc.Clear ();
  541. Assert.Equal (Rectangle.Empty, lc.Viewport);
  542. }
  543. [InlineData (0, 0, Orientation.Horizontal, "─")]
  544. [InlineData (1, 0, Orientation.Horizontal, "─")]
  545. [InlineData (0, 1, Orientation.Horizontal, "─")]
  546. [InlineData (-1, 0, Orientation.Horizontal, "─")]
  547. [InlineData (0, -1, Orientation.Horizontal, "─")]
  548. [InlineData (-1, -1, Orientation.Horizontal, "─")]
  549. [InlineData (0, 0, Orientation.Vertical, "│")]
  550. [InlineData (1, 0, Orientation.Vertical, "│")]
  551. [InlineData (0, 1, Orientation.Vertical, "│")]
  552. [InlineData (0, -1, Orientation.Vertical, "│")]
  553. [InlineData (-1, 0, Orientation.Vertical, "│")]
  554. [InlineData (-1, -1, Orientation.Vertical, "│")]
  555. [Theory]
  556. [SetupFakeDriver]
  557. public void Length_0_Is_1_Long (int x, int y, Orientation orientation, string expected)
  558. {
  559. var canvas = new LineCanvas ();
  560. // Add a line at 5, 5 that's has length of 1
  561. canvas.AddLine (new (x, y), 1, orientation, LineStyle.Single);
  562. TestHelpers.AssertEqual (_output, $"{expected}", $"{canvas}");
  563. }
  564. // X is offset by 2
  565. [InlineData (0, 0, 1, Orientation.Horizontal, "─")]
  566. [InlineData (1, 0, 1, Orientation.Horizontal, "─")]
  567. [InlineData (0, 1, 1, Orientation.Horizontal, "─")]
  568. [InlineData (0, 0, 1, Orientation.Vertical, "│")]
  569. [InlineData (1, 0, 1, Orientation.Vertical, "│")]
  570. [InlineData (0, 1, 1, Orientation.Vertical, "│")]
  571. [InlineData (-1, 0, 1, Orientation.Horizontal, "─")]
  572. [InlineData (0, -1, 1, Orientation.Horizontal, "─")]
  573. [InlineData (-1, 0, 1, Orientation.Vertical, "│")]
  574. [InlineData (0, -1, 1, Orientation.Vertical, "│")]
  575. [InlineData (0, 0, -1, Orientation.Horizontal, "─")]
  576. [InlineData (1, 0, -1, Orientation.Horizontal, "─")]
  577. [InlineData (0, 1, -1, Orientation.Horizontal, "─")]
  578. [InlineData (0, 0, -1, Orientation.Vertical, "│")]
  579. [InlineData (1, 0, -1, Orientation.Vertical, "│")]
  580. [InlineData (0, 1, -1, Orientation.Vertical, "│")]
  581. [InlineData (-1, 0, -1, Orientation.Horizontal, "─")]
  582. [InlineData (0, -1, -1, Orientation.Horizontal, "─")]
  583. [InlineData (-1, 0, -1, Orientation.Vertical, "│")]
  584. [InlineData (0, -1, -1, Orientation.Vertical, "│")]
  585. [InlineData (0, 0, 2, Orientation.Horizontal, "──")]
  586. [InlineData (1, 0, 2, Orientation.Horizontal, "──")]
  587. [InlineData (0, 1, 2, Orientation.Horizontal, "──")]
  588. [InlineData (1, 1, 2, Orientation.Horizontal, "──")]
  589. [InlineData (0, 0, 2, Orientation.Vertical, "│\r\n│")]
  590. [InlineData (1, 0, 2, Orientation.Vertical, "│\r\n│")]
  591. [InlineData (0, 1, 2, Orientation.Vertical, "│\r\n│")]
  592. [InlineData (1, 1, 2, Orientation.Vertical, "│\r\n│")]
  593. [InlineData (-1, 0, 2, Orientation.Horizontal, "──")]
  594. [InlineData (0, -1, 2, Orientation.Horizontal, "──")]
  595. [InlineData (-1, 0, 2, Orientation.Vertical, "│\r\n│")]
  596. [InlineData (0, -1, 2, Orientation.Vertical, "│\r\n│")]
  597. [InlineData (-1, -1, 2, Orientation.Vertical, "│\r\n│")]
  598. [InlineData (0, 0, -2, Orientation.Horizontal, "──")]
  599. [InlineData (1, 0, -2, Orientation.Horizontal, "──")]
  600. [InlineData (0, 1, -2, Orientation.Horizontal, "──")]
  601. [InlineData (0, 0, -2, Orientation.Vertical, "│\r\n│")]
  602. [InlineData (1, 0, -2, Orientation.Vertical, "│\r\n│")]
  603. [InlineData (0, 1, -2, Orientation.Vertical, "│\r\n│")]
  604. [InlineData (1, 1, -2, Orientation.Vertical, "│\r\n│")]
  605. [InlineData (-1, 0, -2, Orientation.Horizontal, "──")]
  606. [InlineData (0, -1, -2, Orientation.Horizontal, "──")]
  607. [InlineData (-1, 0, -2, Orientation.Vertical, "│\r\n│")]
  608. [InlineData (0, -1, -2, Orientation.Vertical, "│\r\n│")]
  609. [InlineData (-1, -1, -2, Orientation.Vertical, "│\r\n│")]
  610. [Theory]
  611. [SetupFakeDriver]
  612. public void Length_n_Is_n_Long (int x, int y, int length, Orientation orientation, string expected)
  613. {
  614. var canvas = new LineCanvas ();
  615. canvas.AddLine (new (x, y), length, orientation, LineStyle.Single);
  616. var result = canvas.ToString ();
  617. TestHelpers.AssertEqual (_output, expected, result);
  618. }
  619. [Fact]
  620. [SetupFakeDriver]
  621. public void Length_Negative ()
  622. {
  623. var offset = new Point (5, 5);
  624. var canvas = new LineCanvas ();
  625. canvas.AddLine (offset, -3, Orientation.Horizontal, LineStyle.Single);
  626. var looksLike = "───";
  627. Assert.Equal (looksLike, $"{canvas}");
  628. }
  629. [InlineData (Orientation.Horizontal, "─")]
  630. [InlineData (Orientation.Vertical, "│")]
  631. [Theory]
  632. [SetupFakeDriver]
  633. public void Length_Zero_Alone_Is_Line (Orientation orientation, string expected)
  634. {
  635. var lc = new LineCanvas ();
  636. // Add a line at 0, 0 that's has length of 0
  637. lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
  638. TestHelpers.AssertEqual (_output, expected, $"{lc}");
  639. }
  640. [InlineData (Orientation.Horizontal, "┼")]
  641. [InlineData (Orientation.Vertical, "┼")]
  642. [Theory]
  643. [SetupFakeDriver]
  644. public void Length_Zero_Cross_Is_Cross (Orientation orientation, string expected)
  645. {
  646. var lc = new LineCanvas ();
  647. // Add point at opposite orientation
  648. lc.AddLine (
  649. Point.Empty,
  650. 0,
  651. orientation == Orientation.Horizontal ? Orientation.Vertical : Orientation.Horizontal,
  652. LineStyle.Single
  653. );
  654. // Add a line at 0, 0 that's has length of 0
  655. lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
  656. TestHelpers.AssertEqual (_output, expected, $"{lc}");
  657. }
  658. [InlineData (Orientation.Horizontal, "╥")]
  659. [InlineData (Orientation.Vertical, "╞")]
  660. [Theory]
  661. [SetupFakeDriver]
  662. public void Length_Zero_NextTo_Opposite_Is_T (Orientation orientation, string expected)
  663. {
  664. var lc = new LineCanvas ();
  665. // Add line with length of 1 in opposite orientation starting at same location
  666. if (orientation == Orientation.Horizontal)
  667. {
  668. lc.AddLine (Point.Empty, 1, Orientation.Vertical, LineStyle.Double);
  669. }
  670. else
  671. {
  672. lc.AddLine (Point.Empty, 1, Orientation.Horizontal, LineStyle.Double);
  673. }
  674. // Add a line at 0, 0 that's has length of 0
  675. lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
  676. TestHelpers.AssertEqual (_output, expected, $"{lc}");
  677. }
  678. [Fact]
  679. public void TestLineCanvas_LeaveMargin_Top1_Left1 ()
  680. {
  681. var canvas = new LineCanvas ();
  682. // Upper box
  683. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
  684. canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Single);
  685. var looksLike =
  686. @"
  687. ┌─
  688. │ ";
  689. TestHelpers.AssertEqual (_output, looksLike, $"{Environment.NewLine}{canvas}");
  690. }
  691. [Fact]
  692. [SetupFakeDriver]
  693. public void TestLineCanvas_Window_Heavy ()
  694. {
  695. View v = GetCanvas (out LineCanvas canvas);
  696. // outer box
  697. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Heavy);
  698. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Heavy);
  699. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Heavy);
  700. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Heavy);
  701. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Heavy);
  702. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Heavy);
  703. v.Draw ();
  704. var looksLike =
  705. @"
  706. ┏━━━━┳━━━┓
  707. ┃ ┃ ┃
  708. ┣━━━━╋━━━┫
  709. ┃ ┃ ┃
  710. ┗━━━━┻━━━┛";
  711. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  712. v.Dispose ();
  713. }
  714. [Theory]
  715. [SetupFakeDriver]
  716. [InlineData (LineStyle.Single)]
  717. [InlineData (LineStyle.Rounded)]
  718. public void TestLineCanvas_Window_HeavyTop_ThinSides (LineStyle thinStyle)
  719. {
  720. View v = GetCanvas (out LineCanvas canvas);
  721. // outer box
  722. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Heavy);
  723. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, thinStyle);
  724. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Heavy);
  725. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, thinStyle);
  726. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, thinStyle);
  727. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Heavy);
  728. v.Draw ();
  729. var looksLike =
  730. @"
  731. ┍━━━━┯━━━┑
  732. │ │ │
  733. ┝━━━━┿━━━┥
  734. │ │ │
  735. ┕━━━━┷━━━┙
  736. ";
  737. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  738. v.Dispose ();
  739. }
  740. [Theory]
  741. [SetupFakeDriver]
  742. [InlineData (LineStyle.Single)]
  743. [InlineData (LineStyle.Rounded)]
  744. public void TestLineCanvas_Window_ThinTop_HeavySides (LineStyle thinStyle)
  745. {
  746. View v = GetCanvas (out LineCanvas canvas);
  747. // outer box
  748. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, thinStyle);
  749. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Heavy);
  750. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, thinStyle);
  751. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Heavy);
  752. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Heavy);
  753. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, thinStyle);
  754. v.Draw ();
  755. var looksLike =
  756. @"
  757. ┎────┰───┒
  758. ┃ ┃ ┃
  759. ┠────╂───┨
  760. ┃ ┃ ┃
  761. ┖────┸───┚
  762. ";
  763. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  764. v.Dispose ();
  765. }
  766. [Fact]
  767. [SetupFakeDriver]
  768. public void Top_Left_From_TopRight_LeftUp ()
  769. {
  770. var canvas = new LineCanvas ();
  771. // Upper box
  772. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
  773. canvas.AddLine (new (0, 1), -2, Orientation.Vertical, LineStyle.Single);
  774. var looksLike =
  775. @"
  776. ┌─
  777. │ ";
  778. TestHelpers.AssertEqual (_output, looksLike, $"{Environment.NewLine}{canvas}");
  779. }
  780. [Fact]
  781. [SetupFakeDriver]
  782. public void Top_With_1Down ()
  783. {
  784. var canvas = new LineCanvas ();
  785. // Top ─
  786. canvas.AddLine (Point.Empty, 1, Orientation.Horizontal, LineStyle.Single);
  787. // Bottom ─
  788. canvas.AddLine (new (1, 1), -1, Orientation.Horizontal, LineStyle.Single);
  789. //// Right down
  790. //canvas.AddLine (new Point (9, 0), 3, Orientation.Vertical, LineStyle.Single);
  791. //// Bottom
  792. //canvas.AddLine (new Point (9, 3), -10, Orientation.Horizontal, LineStyle.Single);
  793. //// Left Up
  794. //canvas.AddLine (new Point (0, 3), -3, Orientation.Vertical, LineStyle.Single);
  795. Assert.Equal (new (0, 0, 2, 2), canvas.Viewport);
  796. Dictionary<Point, Rune> map = canvas.GetMap ();
  797. Assert.Equal (2, map.Count);
  798. TestHelpers.AssertEqual (
  799. _output,
  800. @"
  801. ─",
  802. $"{Environment.NewLine}{canvas}"
  803. );
  804. }
  805. [Fact]
  806. [SetupFakeDriver]
  807. public void ToString_Empty ()
  808. {
  809. var lc = new LineCanvas ();
  810. TestHelpers.AssertEqual (_output, string.Empty, lc.ToString ());
  811. }
  812. // 012
  813. [InlineData (0, 0, "═══")]
  814. [InlineData (1, 0, "═══")]
  815. [InlineData (0, 1, "═══")]
  816. [InlineData (1, 1, "═══")]
  817. [InlineData (2, 2, "═══")]
  818. [InlineData (-1, 0, "═══")]
  819. [InlineData (0, -1, "═══")]
  820. [InlineData (-1, -1, "═══")]
  821. [InlineData (-2, -2, "═══")]
  822. [Theory]
  823. [SetupFakeDriver]
  824. public void ToString_Positive_Horizontal_1Line_Offset (int x, int y, string expected)
  825. {
  826. var lc = new LineCanvas ();
  827. lc.AddLine (new (x, y), 3, Orientation.Horizontal, LineStyle.Double);
  828. TestHelpers.AssertEqual (_output, expected, $"{lc}");
  829. }
  830. [InlineData (0, 0, 0, 0, "═══")]
  831. [InlineData (1, 0, 1, 0, "═══")]
  832. [InlineData (-1, 0, -1, 0, "═══")]
  833. [InlineData (0, 0, 1, 0, "════")]
  834. [InlineData (1, 0, 3, 0, "═════")]
  835. [InlineData (1, 0, 4, 0, "══════")]
  836. [InlineData (1, 0, 5, 0, "═══ ═══")]
  837. [InlineData (0, 0, 0, 1, "\u2550\u2550\u2550\r\n\u2550\u2550\u2550")]
  838. [InlineData (0, 0, 1, 1, "═══ \r\n ═══")]
  839. [InlineData (0, 0, 2, 1, "═══ \r\n ═══")]
  840. [InlineData (1, 0, 0, 1, " ═══\r\n═══ ")]
  841. [InlineData (0, 1, 0, 1, "═══")]
  842. [InlineData (1, 1, 0, 1, "════")]
  843. [InlineData (2, 2, 0, 1, "═══ \r\n ═══")]
  844. [Theory]
  845. [SetupFakeDriver]
  846. public void ToString_Positive_Horizontal_2Line_Offset (int x1, int y1, int x2, int y2, string expected)
  847. {
  848. var lc = new LineCanvas ();
  849. lc.AddLine (new (x1, y1), 3, Orientation.Horizontal, LineStyle.Double);
  850. lc.AddLine (new (x2, y2), 3, Orientation.Horizontal, LineStyle.Double);
  851. TestHelpers.AssertEqual (_output, expected, $"{lc}");
  852. }
  853. // [Fact, SetupFakeDriver]
  854. // public void LeaveMargin_Top1_Left1 ()
  855. // {
  856. // var canvas = new LineCanvas ();
  857. // // Upper box
  858. // canvas.AddLine (Point.Empty, 9, Orientation.Horizontal, LineStyle.Single);
  859. // canvas.AddLine (new Point (8, 0), 3, Orientation.Vertical, LineStyle.Single);
  860. // canvas.AddLine (new Point (8, 3), -9, Orientation.Horizontal, LineStyle.Single);
  861. // canvas.AddLine (new Point (0, 2), -3, Orientation.Vertical, LineStyle.Single);
  862. // // Lower Box
  863. // canvas.AddLine (new Point (5, 0), 2, Orientation.Vertical, LineStyle.Single);
  864. // canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, LineStyle.Single);
  865. // string looksLike =
  866. //@"
  867. //┌────┬──┐
  868. //│ │ │
  869. //├────┼──┤
  870. //└────┴──┘
  871. //";
  872. // Assert.Equal (looksLike, $"{Environment.NewLine}{canvas}");
  873. // }
  874. [InlineData (0, 0, 0, Orientation.Horizontal, LineStyle.Double, "═")]
  875. [InlineData (0, 0, 0, Orientation.Vertical, LineStyle.Double, "║")]
  876. [InlineData (0, 0, 0, Orientation.Horizontal, LineStyle.Single, "─")]
  877. [InlineData (0, 0, 0, Orientation.Vertical, LineStyle.Single, "│")]
  878. [InlineData (0, 0, 1, Orientation.Horizontal, LineStyle.Double, "═")]
  879. [InlineData (0, 0, 1, Orientation.Vertical, LineStyle.Double, "║")]
  880. [InlineData (0, 0, 1, Orientation.Horizontal, LineStyle.Single, "─")]
  881. [InlineData (0, 0, 1, Orientation.Vertical, LineStyle.Single, "│")]
  882. [InlineData (0, 0, 2, Orientation.Horizontal, LineStyle.Double, "══")]
  883. [InlineData (0, 0, 2, Orientation.Vertical, LineStyle.Double, "║\n║")]
  884. [InlineData (0, 0, 2, Orientation.Horizontal, LineStyle.Single, "──")]
  885. [InlineData (0, 0, 2, Orientation.Vertical, LineStyle.Single, "│\n│")]
  886. [SetupFakeDriver]
  887. [Theory]
  888. public void View_Draws_1LineTests (
  889. int x1,
  890. int y1,
  891. int length,
  892. Orientation o1,
  893. LineStyle s1,
  894. string expected
  895. )
  896. {
  897. View v = GetCanvas (out LineCanvas lc);
  898. v.Width = 10;
  899. v.Height = 10;
  900. v.Viewport = new (0, 0, 10, 10);
  901. lc.AddLine (new (x1, y1), length, o1, s1);
  902. v.Draw ();
  903. TestHelpers.AssertDriverContentsAre (expected, _output);
  904. v.Dispose ();
  905. }
  906. /// <summary>This test demonstrates how to correctly trigger a corner. By overlapping the lines in the same cell</summary>
  907. [Fact]
  908. [SetupFakeDriver]
  909. public void View_Draws_Corner_Correct ()
  910. {
  911. View v = GetCanvas (out LineCanvas canvas);
  912. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
  913. canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Single);
  914. v.Draw ();
  915. var looksLike =
  916. @"
  917. ┌─
  918. │";
  919. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  920. v.Dispose ();
  921. }
  922. /// <summary>
  923. /// This test demonstrates that corners are only drawn when lines overlap. Not when they terminate adjacent to one
  924. /// another.
  925. /// </summary>
  926. [Fact]
  927. [SetupFakeDriver]
  928. public void View_Draws_Corner_NoOverlap ()
  929. {
  930. View v = GetCanvas (out LineCanvas canvas);
  931. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
  932. canvas.AddLine (new (0, 1), 2, Orientation.Vertical, LineStyle.Single);
  933. v.Draw ();
  934. var looksLike =
  935. @"
  936. ──
  937. │";
  938. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  939. v.Dispose ();
  940. }
  941. [InlineData (LineStyle.Single)]
  942. [InlineData (LineStyle.Rounded)]
  943. [Theory]
  944. [SetupFakeDriver]
  945. public void View_Draws_Horizontal (LineStyle style)
  946. {
  947. View v = GetCanvas (out LineCanvas canvas);
  948. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, style);
  949. v.Draw ();
  950. var looksLike =
  951. @"
  952. ──";
  953. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  954. v.Dispose ();
  955. }
  956. [Fact]
  957. [SetupFakeDriver]
  958. public void View_Draws_Horizontal_Double ()
  959. {
  960. View v = GetCanvas (out LineCanvas canvas);
  961. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Double);
  962. v.Draw ();
  963. var looksLike =
  964. @"
  965. ══";
  966. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  967. v.Dispose ();
  968. }
  969. [InlineData (LineStyle.Single)]
  970. [InlineData (LineStyle.Rounded)]
  971. [Theory]
  972. [SetupFakeDriver]
  973. public void View_Draws_Vertical (LineStyle style)
  974. {
  975. View v = GetCanvas (out LineCanvas canvas);
  976. canvas.AddLine (Point.Empty, 2, Orientation.Vertical, style);
  977. v.Draw ();
  978. var looksLike =
  979. @"
  980. │";
  981. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  982. v.Dispose ();
  983. }
  984. [Fact]
  985. [SetupFakeDriver]
  986. public void View_Draws_Vertical_Double ()
  987. {
  988. View v = GetCanvas (out LineCanvas canvas);
  989. canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Double);
  990. v.Draw ();
  991. var looksLike =
  992. @"
  993. ║";
  994. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  995. v.Dispose ();
  996. }
  997. [Fact]
  998. [SetupFakeDriver]
  999. public void View_Draws_Window_Double ()
  1000. {
  1001. View v = GetCanvas (out LineCanvas canvas);
  1002. // outer box
  1003. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Double);
  1004. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Double);
  1005. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Double);
  1006. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Double);
  1007. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Double);
  1008. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Double);
  1009. v.Draw ();
  1010. var looksLike =
  1011. @"
  1012. ╔════╦═══╗
  1013. ║ ║ ║
  1014. ╠════╬═══╣
  1015. ║ ║ ║
  1016. ╚════╩═══╝";
  1017. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  1018. v.Dispose ();
  1019. }
  1020. [Theory]
  1021. [SetupFakeDriver]
  1022. [InlineData (LineStyle.Single)]
  1023. [InlineData (LineStyle.Rounded)]
  1024. public void View_Draws_Window_DoubleTop_SingleSides (LineStyle thinStyle)
  1025. {
  1026. View v = GetCanvas (out LineCanvas canvas);
  1027. // outer box
  1028. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Double);
  1029. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, thinStyle);
  1030. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Double);
  1031. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, thinStyle);
  1032. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, thinStyle);
  1033. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Double);
  1034. v.Draw ();
  1035. var looksLike =
  1036. @"
  1037. ╒════╤═══╕
  1038. │ │ │
  1039. ╞════╪═══╡
  1040. │ │ │
  1041. ╘════╧═══╛
  1042. ";
  1043. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  1044. v.Dispose ();
  1045. }
  1046. /// <summary>
  1047. /// Demonstrates when <see cref="LineStyle.Rounded"/> corners are used. Notice how not all lines declare rounded.
  1048. /// If there are 1+ lines intersecting and a corner is to be used then if any of them are rounded a rounded corner is
  1049. /// used.
  1050. /// </summary>
  1051. [Fact]
  1052. [SetupFakeDriver]
  1053. public void View_Draws_Window_Rounded ()
  1054. {
  1055. View v = GetCanvas (out LineCanvas canvas);
  1056. // outer box
  1057. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Rounded);
  1058. // LineStyle.Single is ignored because corner overlaps with the above line which is Rounded
  1059. // this results in a rounded corner being used.
  1060. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Single);
  1061. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Rounded);
  1062. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Single);
  1063. // These lines say rounded but they will result in the T sections which are never rounded.
  1064. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Rounded);
  1065. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Rounded);
  1066. v.Draw ();
  1067. var looksLike =
  1068. @"
  1069. ╭────┬───╮
  1070. │ │ │
  1071. ├────┼───┤
  1072. │ │ │
  1073. ╰────┴───╯";
  1074. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  1075. v.Dispose ();
  1076. }
  1077. [Theory]
  1078. [InlineData (LineStyle.Single)]
  1079. [InlineData (LineStyle.Rounded)]
  1080. [SetupFakeDriver]
  1081. public void View_Draws_Window_SingleTop_DoubleSides (LineStyle thinStyle)
  1082. {
  1083. View v = GetCanvas (out LineCanvas canvas);
  1084. // outer box
  1085. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, thinStyle);
  1086. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Double);
  1087. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, thinStyle);
  1088. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Double);
  1089. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Double);
  1090. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, thinStyle);
  1091. v.Draw ();
  1092. var looksLike =
  1093. @"
  1094. ╓────╥───╖
  1095. ║ ║ ║
  1096. ╟────╫───╢
  1097. ║ ║ ║
  1098. ╙────╨───╜
  1099. ";
  1100. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  1101. v.Dispose ();
  1102. }
  1103. [Fact]
  1104. [SetupFakeDriver]
  1105. public void Window ()
  1106. {
  1107. var canvas = new LineCanvas ();
  1108. // Frame
  1109. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Single);
  1110. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Single);
  1111. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Single);
  1112. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Single);
  1113. // Cross
  1114. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Single);
  1115. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Single);
  1116. var looksLike =
  1117. @"
  1118. ┌────┬───┐
  1119. │ │ │
  1120. ├────┼───┤
  1121. │ │ │
  1122. └────┴───┘";
  1123. TestHelpers.AssertEqual (_output, looksLike, $"{Environment.NewLine}{canvas}");
  1124. }
  1125. [Fact]
  1126. [SetupFakeDriver]
  1127. public void Zero_Length_Intersections ()
  1128. {
  1129. // Draw at 1,2 within client area of View (i.e. leave a top and left margin of 1)
  1130. // This proves we aren't drawing excess above
  1131. var x = 1;
  1132. var y = 2;
  1133. var width = 5;
  1134. var height = 2;
  1135. var lc = new LineCanvas ();
  1136. // ╔╡╞═════╗
  1137. // Add a short horiz line for ╔╡
  1138. lc.AddLine (new (x, y), 2, Orientation.Horizontal, LineStyle.Double);
  1139. //LHS line down
  1140. lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
  1141. //Vertical line before Title, results in a ╡
  1142. lc.AddLine (new (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
  1143. //Vertical line after Title, results in a ╞
  1144. lc.AddLine (new (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
  1145. // remainder of top line
  1146. lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
  1147. //RHS line down
  1148. lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
  1149. var looksLike = @"
  1150. ╔╡╞══╗
  1151. ║ ║";
  1152. TestHelpers.AssertEqual (_output, looksLike, $"{Environment.NewLine}{lc}");
  1153. }
  1154. [Fact]
  1155. public void LineCanvas_UsesFillCorrectly ()
  1156. {
  1157. // Arrange
  1158. var foregroundColor = new Color (255, 0); // Red
  1159. var backgroundColor = new Color (0, 0); // Black
  1160. var foregroundFill = new SolidFill (foregroundColor);
  1161. var backgroundFill = new SolidFill (backgroundColor);
  1162. var fillPair = new FillPair (foregroundFill, backgroundFill);
  1163. var lineCanvas = new LineCanvas
  1164. {
  1165. Fill = fillPair
  1166. };
  1167. // Act
  1168. lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  1169. Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
  1170. // Assert
  1171. foreach (Cell? cell in cellMap.Values)
  1172. {
  1173. Assert.NotNull (cell);
  1174. Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground);
  1175. Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
  1176. }
  1177. }
  1178. [Fact]
  1179. public void LineCanvas_LineColorIgnoredBecauseOfFill ()
  1180. {
  1181. // Arrange
  1182. var foregroundColor = new Color (255, 0); // Red
  1183. var backgroundColor = new Color (0, 0); // Black
  1184. var lineColor = new Attribute (new Color (0, 255), new Color (255, 255, 255)); // Green on White
  1185. var foregroundFill = new SolidFill (foregroundColor);
  1186. var backgroundFill = new SolidFill (backgroundColor);
  1187. var fillPair = new FillPair (foregroundFill, backgroundFill);
  1188. var lineCanvas = new LineCanvas
  1189. {
  1190. Fill = fillPair
  1191. };
  1192. // Act
  1193. lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single, lineColor);
  1194. Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
  1195. // Assert
  1196. foreach (Cell? cell in cellMap.Values)
  1197. {
  1198. Assert.NotNull (cell);
  1199. Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground);
  1200. Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
  1201. }
  1202. }
  1203. [Fact]
  1204. public void LineCanvas_IntersectingLinesUseFillCorrectly ()
  1205. {
  1206. // Arrange
  1207. var foregroundColor = new Color (255, 0); // Red
  1208. var backgroundColor = new Color (0, 0); // Black
  1209. var foregroundFill = new SolidFill (foregroundColor);
  1210. var backgroundFill = new SolidFill (backgroundColor);
  1211. var fillPair = new FillPair (foregroundFill, backgroundFill);
  1212. var lineCanvas = new LineCanvas
  1213. {
  1214. Fill = fillPair
  1215. };
  1216. // Act
  1217. lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  1218. lineCanvas.AddLine (new (2, -2), 5, Orientation.Vertical, LineStyle.Single);
  1219. Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
  1220. // Assert
  1221. foreach (Cell? cell in cellMap.Values)
  1222. {
  1223. Assert.NotNull (cell);
  1224. Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground);
  1225. Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
  1226. }
  1227. }
  1228. // TODO: Remove this and make all LineCanvas tests independent of View
  1229. /// <summary>
  1230. /// Creates a new <see cref="View"/> into which a <see cref="LineCanvas"/> is rendered at
  1231. /// <see cref="View.DrawContentComplete"/> time.
  1232. /// </summary>
  1233. /// <param name="canvas">The <see cref="LineCanvas"/> you can draw into.</param>
  1234. /// <param name="offsetX">How far to offset drawing in X</param>
  1235. /// <param name="offsetY">How far to offset drawing in Y</param>
  1236. /// <returns></returns>
  1237. private View GetCanvas (out LineCanvas canvas, int offsetX = 0, int offsetY = 0)
  1238. {
  1239. var v = new View { Width = 10, Height = 5, Viewport = new (0, 0, 10, 5) };
  1240. LineCanvas canvasCopy = canvas = new ();
  1241. v.DrawContentComplete += (s, e) =>
  1242. {
  1243. v.FillRect (v.Viewport);
  1244. foreach (KeyValuePair<Point, Rune> p in canvasCopy.GetMap ())
  1245. {
  1246. v.AddRune (
  1247. offsetX + p.Key.X,
  1248. offsetY + p.Key.Y,
  1249. p.Value
  1250. );
  1251. }
  1252. canvasCopy.Clear ();
  1253. };
  1254. return v;
  1255. }
  1256. }