LineCanvasTests.cs 49 KB

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