LineCanvasTests.cs 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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. LineCanvas canvas = new ();
  18. Assert.Equal (string.Empty, canvas.ToString ());
  19. }
  20. [Fact]
  21. public void Clear_Removes_All_Lines ()
  22. {
  23. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  118. Assert.Equal (Rectangle.Empty, canvas.Bounds);
  119. }
  120. [Fact]
  121. public void Bounds_Single_Point_Zero_Length ()
  122. {
  123. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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. LineCanvas canvas = new ();
  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]
  657. public void Length_n_Is_n_Long (int x, int y, int length, Orientation orientation, string expected)
  658. {
  659. LineCanvas canvas = new ();
  660. canvas.AddLine (new (x, y), length, orientation, LineStyle.Single);
  661. var result = canvas.ToString ();
  662. OutputAssert.AssertEqual (output, expected, result);
  663. }
  664. [Fact]
  665. public void Length_Negative ()
  666. {
  667. var offset = new Point (5, 5);
  668. LineCanvas canvas = new ();
  669. canvas.AddLine (offset, -3, Orientation.Horizontal, LineStyle.Single);
  670. var looksLike = "───";
  671. Assert.Equal (looksLike, $"{canvas}");
  672. }
  673. [InlineData (Orientation.Horizontal, "─")]
  674. [InlineData (Orientation.Vertical, "│")]
  675. [Theory]
  676. public void Length_Zero_Alone_Is_Line (Orientation orientation, string expected)
  677. {
  678. var lc = new LineCanvas ();
  679. // Add a line at 0, 0 that's has length of 0
  680. lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
  681. OutputAssert.AssertEqual (output, expected, $"{lc}");
  682. }
  683. [InlineData (Orientation.Horizontal, "┼")]
  684. [InlineData (Orientation.Vertical, "┼")]
  685. [Theory]
  686. public void Length_Zero_Cross_Is_Cross (Orientation orientation, string expected)
  687. {
  688. var lc = new LineCanvas ();
  689. // Add point at opposite orientation
  690. lc.AddLine (
  691. Point.Empty,
  692. 0,
  693. orientation == Orientation.Horizontal ? Orientation.Vertical : Orientation.Horizontal,
  694. LineStyle.Single
  695. );
  696. // Add a line at 0, 0 that's has length of 0
  697. lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
  698. OutputAssert.AssertEqual (output, expected, $"{lc}");
  699. }
  700. [InlineData (Orientation.Horizontal, "╥")]
  701. [InlineData (Orientation.Vertical, "╞")]
  702. [Theory]
  703. public void Length_Zero_NextTo_Opposite_Is_T (Orientation orientation, string expected)
  704. {
  705. var lc = new LineCanvas ();
  706. // Add line with length of 1 in opposite orientation starting at same location
  707. if (orientation == Orientation.Horizontal)
  708. {
  709. lc.AddLine (Point.Empty, 1, Orientation.Vertical, LineStyle.Double);
  710. }
  711. else
  712. {
  713. lc.AddLine (Point.Empty, 1, Orientation.Horizontal, LineStyle.Double);
  714. }
  715. // Add a line at 0, 0 that's has length of 0
  716. lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
  717. OutputAssert.AssertEqual (output, expected, $"{lc}");
  718. }
  719. [Fact]
  720. public void TestLineCanvas_LeaveMargin_Top1_Left1 ()
  721. {
  722. LineCanvas canvas = new ();
  723. // Upper box
  724. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
  725. canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Single);
  726. var looksLike =
  727. @"
  728. ┌─
  729. │ ";
  730. OutputAssert.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}");
  731. }
  732. [Fact]
  733. public void TestLineCanvas_Window_Heavy ()
  734. {
  735. var driver = CreateFakeDriver ();
  736. View v = GetCanvas (driver, out LineCanvas canvas);
  737. // outer box
  738. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Heavy);
  739. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Heavy);
  740. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Heavy);
  741. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Heavy);
  742. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Heavy);
  743. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Heavy);
  744. v.Draw ();
  745. var looksLike =
  746. @"
  747. ┏━━━━┳━━━┓
  748. ┃ ┃ ┃
  749. ┣━━━━╋━━━┫
  750. ┃ ┃ ┃
  751. ┗━━━━┻━━━┛";
  752. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  753. v.Dispose ();
  754. }
  755. [Theory]
  756. [InlineData (LineStyle.Single)]
  757. [InlineData (LineStyle.Rounded)]
  758. public void TestLineCanvas_Window_HeavyTop_ThinSides (LineStyle thinStyle)
  759. {
  760. var driver = CreateFakeDriver ();
  761. View v = GetCanvas (driver, out LineCanvas canvas);
  762. // outer box
  763. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Heavy);
  764. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, thinStyle);
  765. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Heavy);
  766. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, thinStyle);
  767. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, thinStyle);
  768. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Heavy);
  769. v.Draw ();
  770. var looksLike =
  771. @"
  772. ┍━━━━┯━━━┑
  773. │ │ │
  774. ┝━━━━┿━━━┥
  775. │ │ │
  776. ┕━━━━┷━━━┙
  777. ";
  778. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  779. v.Dispose ();
  780. }
  781. [Theory]
  782. [InlineData (LineStyle.Single)]
  783. [InlineData (LineStyle.Rounded)]
  784. public void TestLineCanvas_Window_ThinTop_HeavySides (LineStyle thinStyle)
  785. {
  786. var driver = CreateFakeDriver ();
  787. View v = GetCanvas (driver, out LineCanvas canvas);
  788. // outer box
  789. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, thinStyle);
  790. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Heavy);
  791. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, thinStyle);
  792. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Heavy);
  793. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Heavy);
  794. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, thinStyle);
  795. v.Draw ();
  796. var looksLike =
  797. @"
  798. ┎────┰───┒
  799. ┃ ┃ ┃
  800. ┠────╂───┨
  801. ┃ ┃ ┃
  802. ┖────┸───┚
  803. ";
  804. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  805. v.Dispose ();
  806. }
  807. [Fact]
  808. public void Top_Left_From_TopRight_LeftUp ()
  809. {
  810. LineCanvas canvas = new ();
  811. // Upper box
  812. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
  813. canvas.AddLine (new (0, 1), -2, Orientation.Vertical, LineStyle.Single);
  814. var looksLike =
  815. @"
  816. ┌─
  817. │ ";
  818. OutputAssert.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}");
  819. }
  820. [Fact]
  821. public void Top_With_1Down ()
  822. {
  823. LineCanvas canvas = new ();
  824. // Top ─
  825. canvas.AddLine (Point.Empty, 1, Orientation.Horizontal, LineStyle.Single);
  826. // Bottom ─
  827. canvas.AddLine (new (1, 1), -1, Orientation.Horizontal, LineStyle.Single);
  828. //// Right down
  829. //canvas.AddLine (new Point (9, 0), 3, Orientation.Vertical, LineStyle.Single);
  830. //// Bottom
  831. //canvas.AddLine (new Point (9, 3), -10, Orientation.Horizontal, LineStyle.Single);
  832. //// Left Up
  833. //canvas.AddLine (new Point (0, 3), -3, Orientation.Vertical, LineStyle.Single);
  834. Assert.Equal (new (0, 0, 2, 2), canvas.Bounds);
  835. Dictionary<Point, Rune> map = canvas.GetMap ();
  836. Assert.Equal (2, map.Count);
  837. OutputAssert.AssertEqual (
  838. output,
  839. @"
  840. ─",
  841. $"{Environment.NewLine}{canvas}"
  842. );
  843. }
  844. [Fact]
  845. public void ToString_Empty ()
  846. {
  847. var lc = new LineCanvas ();
  848. OutputAssert.AssertEqual (output, string.Empty, lc.ToString ());
  849. }
  850. // 012
  851. [InlineData (0, 0, "═══")]
  852. [InlineData (1, 0, "═══")]
  853. [InlineData (0, 1, "═══")]
  854. [InlineData (1, 1, "═══")]
  855. [InlineData (2, 2, "═══")]
  856. [InlineData (-1, 0, "═══")]
  857. [InlineData (0, -1, "═══")]
  858. [InlineData (-1, -1, "═══")]
  859. [InlineData (-2, -2, "═══")]
  860. [Theory]
  861. public void ToString_Positive_Horizontal_1Line_Offset (int x, int y, string expected)
  862. {
  863. var lc = new LineCanvas ();
  864. lc.AddLine (new (x, y), 3, Orientation.Horizontal, LineStyle.Double);
  865. OutputAssert.AssertEqual (output, expected, $"{lc}");
  866. }
  867. [InlineData (0, 0, 0, 0, "═══")]
  868. [InlineData (1, 0, 1, 0, "═══")]
  869. [InlineData (-1, 0, -1, 0, "═══")]
  870. [InlineData (0, 0, 1, 0, "════")]
  871. [InlineData (1, 0, 3, 0, "═════")]
  872. [InlineData (1, 0, 4, 0, "══════")]
  873. [InlineData (1, 0, 5, 0, "═══ ═══")]
  874. [InlineData (0, 0, 0, 1, "\u2550\u2550\u2550\r\n\u2550\u2550\u2550")]
  875. [InlineData (0, 0, 1, 1, "═══ \r\n ═══")]
  876. [InlineData (0, 0, 2, 1, "═══ \r\n ═══")]
  877. [InlineData (1, 0, 0, 1, " ═══\r\n═══ ")]
  878. [InlineData (0, 1, 0, 1, "═══")]
  879. [InlineData (1, 1, 0, 1, "════")]
  880. [InlineData (2, 2, 0, 1, "═══ \r\n ═══")]
  881. [Theory]
  882. public void ToString_Positive_Horizontal_2Line_Offset (int x1, int y1, int x2, int y2, string expected)
  883. {
  884. var lc = new LineCanvas ();
  885. lc.AddLine (new (x1, y1), 3, Orientation.Horizontal, LineStyle.Double);
  886. lc.AddLine (new (x2, y2), 3, Orientation.Horizontal, LineStyle.Double);
  887. OutputAssert.AssertEqual (output, expected, $"{lc}");
  888. }
  889. // [Fact, SetupFakeDriver]
  890. // public void LeaveMargin_Top1_Left1 ()
  891. // {
  892. // var canvas = new LineCanvas ();
  893. // // Upper box
  894. // canvas.AddLine (Point.Empty, 9, Orientation.Horizontal, LineStyle.Single);
  895. // canvas.AddLine (new Point (8, 0), 3, Orientation.Vertical, LineStyle.Single);
  896. // canvas.AddLine (new Point (8, 3), -9, Orientation.Horizontal, LineStyle.Single);
  897. // canvas.AddLine (new Point (0, 2), -3, Orientation.Vertical, LineStyle.Single);
  898. // // Lower Box
  899. // canvas.AddLine (new Point (5, 0), 2, Orientation.Vertical, LineStyle.Single);
  900. // canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, LineStyle.Single);
  901. // string looksLike =
  902. //@"
  903. //┌────┬──┐
  904. //│ │ │
  905. //├────┼──┤
  906. //└────┴──┘
  907. //";
  908. // Assert.Equal (looksLike, $"{Environment.NewLine}{canvas}");
  909. // }
  910. [InlineData (0, 0, 0, Orientation.Horizontal, LineStyle.Double, "═")]
  911. [InlineData (0, 0, 0, Orientation.Vertical, LineStyle.Double, "║")]
  912. [InlineData (0, 0, 0, Orientation.Horizontal, LineStyle.Single, "─")]
  913. [InlineData (0, 0, 0, Orientation.Vertical, LineStyle.Single, "│")]
  914. [InlineData (0, 0, 1, Orientation.Horizontal, LineStyle.Double, "═")]
  915. [InlineData (0, 0, 1, Orientation.Vertical, LineStyle.Double, "║")]
  916. [InlineData (0, 0, 1, Orientation.Horizontal, LineStyle.Single, "─")]
  917. [InlineData (0, 0, 1, Orientation.Vertical, LineStyle.Single, "│")]
  918. [InlineData (0, 0, 2, Orientation.Horizontal, LineStyle.Double, "══")]
  919. [InlineData (0, 0, 2, Orientation.Vertical, LineStyle.Double, "║\n║")]
  920. [InlineData (0, 0, 2, Orientation.Horizontal, LineStyle.Single, "──")]
  921. [InlineData (0, 0, 2, Orientation.Vertical, LineStyle.Single, "│\n│")]
  922. [Theory]
  923. public void View_Draws_1LineTests (
  924. int x1,
  925. int y1,
  926. int length,
  927. Orientation o1,
  928. LineStyle s1,
  929. string expected
  930. )
  931. {
  932. var driver = CreateFakeDriver ();
  933. View v = GetCanvas (driver, out LineCanvas lc);
  934. v.Width = 10;
  935. v.Height = 10;
  936. v.Viewport = new (0, 0, 10, 10);
  937. lc.AddLine (new (x1, y1), length, o1, s1);
  938. v.Draw ();
  939. DriverAssert.AssertDriverContentsAre (expected, output, driver);
  940. v.Dispose ();
  941. }
  942. /// <summary>This test demonstrates how to correctly trigger a corner. By overlapping the lines in the same cell</summary>
  943. [Fact]
  944. public void View_Draws_Corner_Correct ()
  945. {
  946. var driver = CreateFakeDriver ();
  947. View v = GetCanvas (driver, out LineCanvas canvas);
  948. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
  949. canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Single);
  950. v.Draw ();
  951. var looksLike =
  952. @"
  953. ┌─
  954. │";
  955. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  956. v.Dispose ();
  957. }
  958. /// <summary>
  959. /// This test demonstrates that corners are only drawn when lines overlap. Not when they terminate adjacent to one
  960. /// another.
  961. /// </summary>
  962. [Fact]
  963. public void View_Draws_Corner_NoOverlap ()
  964. {
  965. var driver = CreateFakeDriver ();
  966. View v = GetCanvas (driver, out LineCanvas canvas);
  967. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
  968. canvas.AddLine (new (0, 1), 2, Orientation.Vertical, LineStyle.Single);
  969. v.Draw ();
  970. var looksLike =
  971. @"
  972. ──
  973. │";
  974. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  975. v.Dispose ();
  976. }
  977. [InlineData (LineStyle.Single)]
  978. [InlineData (LineStyle.Rounded)]
  979. [Theory]
  980. public void View_Draws_Horizontal (LineStyle style)
  981. {
  982. var driver = CreateFakeDriver ();
  983. View v = GetCanvas (driver, out LineCanvas canvas);
  984. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, style);
  985. v.Draw ();
  986. var looksLike =
  987. @"
  988. ──";
  989. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  990. v.Dispose ();
  991. }
  992. [Fact]
  993. public void View_Draws_Horizontal_Double ()
  994. {
  995. var driver = CreateFakeDriver ();
  996. View v = GetCanvas (driver, out LineCanvas canvas);
  997. canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Double);
  998. v.Draw ();
  999. var looksLike =
  1000. @"
  1001. ══";
  1002. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  1003. v.Dispose ();
  1004. }
  1005. [InlineData (LineStyle.Single)]
  1006. [InlineData (LineStyle.Rounded)]
  1007. [Theory]
  1008. public void View_Draws_Vertical (LineStyle style)
  1009. {
  1010. var driver = CreateFakeDriver ();
  1011. View v = GetCanvas (driver, out LineCanvas canvas);
  1012. canvas.AddLine (Point.Empty, 2, Orientation.Vertical, style);
  1013. v.Draw ();
  1014. var looksLike =
  1015. @"
  1016. │";
  1017. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  1018. v.Dispose ();
  1019. }
  1020. [Fact]
  1021. public void View_Draws_Vertical_Double ()
  1022. {
  1023. var driver = CreateFakeDriver ();
  1024. View v = GetCanvas (driver, out LineCanvas canvas);
  1025. canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Double);
  1026. v.Draw ();
  1027. var looksLike =
  1028. @"
  1029. ║";
  1030. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  1031. v.Dispose ();
  1032. }
  1033. [Fact]
  1034. public void View_Draws_Window_Double ()
  1035. {
  1036. var driver = CreateFakeDriver ();
  1037. View v = GetCanvas (driver, out LineCanvas canvas);
  1038. // outer box
  1039. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Double);
  1040. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Double);
  1041. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Double);
  1042. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Double);
  1043. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Double);
  1044. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Double);
  1045. v.Draw ();
  1046. var looksLike =
  1047. @"
  1048. ╔════╦═══╗
  1049. ║ ║ ║
  1050. ╠════╬═══╣
  1051. ║ ║ ║
  1052. ╚════╩═══╝";
  1053. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  1054. v.Dispose ();
  1055. }
  1056. [Theory]
  1057. [InlineData (LineStyle.Single)]
  1058. [InlineData (LineStyle.Rounded)]
  1059. public void View_Draws_Window_DoubleTop_SingleSides (LineStyle thinStyle)
  1060. {
  1061. var driver = CreateFakeDriver ();
  1062. View v = GetCanvas (driver, out LineCanvas canvas);
  1063. // outer box
  1064. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Double);
  1065. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, thinStyle);
  1066. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Double);
  1067. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, thinStyle);
  1068. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, thinStyle);
  1069. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Double);
  1070. v.Draw ();
  1071. var looksLike =
  1072. @"
  1073. ╒════╤═══╕
  1074. │ │ │
  1075. ╞════╪═══╡
  1076. │ │ │
  1077. ╘════╧═══╛
  1078. ";
  1079. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  1080. v.Dispose ();
  1081. }
  1082. /// <summary>
  1083. /// Demonstrates when <see cref="LineStyle.Rounded"/> corners are used. Notice how not all lines declare rounded.
  1084. /// If there are 1+ lines intersecting and a corner is to be used then if any of them are rounded a rounded corner is
  1085. /// used.
  1086. /// </summary>
  1087. [Fact]
  1088. public void View_Draws_Window_Rounded ()
  1089. {
  1090. var driver = CreateFakeDriver ();
  1091. View v = GetCanvas (driver, out LineCanvas canvas);
  1092. // outer box
  1093. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Rounded);
  1094. // LineStyle.Single is ignored because corner overlaps with the above line which is Rounded
  1095. // this results in a rounded corner being used.
  1096. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Single);
  1097. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Rounded);
  1098. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Single);
  1099. // These lines say rounded but they will result in the T sections which are never rounded.
  1100. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Rounded);
  1101. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Rounded);
  1102. v.Draw ();
  1103. var looksLike =
  1104. @"
  1105. ╭────┬───╮
  1106. │ │ │
  1107. ├────┼───┤
  1108. │ │ │
  1109. ╰────┴───╯";
  1110. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  1111. v.Dispose ();
  1112. }
  1113. [Theory]
  1114. [InlineData (LineStyle.Single)]
  1115. [InlineData (LineStyle.Rounded)]
  1116. public void View_Draws_Window_SingleTop_DoubleSides (LineStyle thinStyle)
  1117. {
  1118. var driver = CreateFakeDriver ();
  1119. View v = GetCanvas (driver, out LineCanvas canvas);
  1120. // outer box
  1121. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, thinStyle);
  1122. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Double);
  1123. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, thinStyle);
  1124. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Double);
  1125. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Double);
  1126. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, thinStyle);
  1127. v.Draw ();
  1128. var looksLike =
  1129. @"
  1130. ╓────╥───╖
  1131. ║ ║ ║
  1132. ╟────╫───╢
  1133. ║ ║ ║
  1134. ╙────╨───╜
  1135. ";
  1136. DriverAssert.AssertDriverContentsAre (looksLike, output, driver);
  1137. v.Dispose ();
  1138. }
  1139. [Fact]
  1140. public void Window ()
  1141. {
  1142. LineCanvas canvas = new ();
  1143. // Frame
  1144. canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Single);
  1145. canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Single);
  1146. canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Single);
  1147. canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Single);
  1148. // Cross
  1149. canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Single);
  1150. canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Single);
  1151. var looksLike =
  1152. @"
  1153. ┌────┬───┐
  1154. │ │ │
  1155. ├────┼───┤
  1156. │ │ │
  1157. └────┴───┘";
  1158. OutputAssert.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}");
  1159. }
  1160. [Fact]
  1161. public void Zero_Length_Intersections ()
  1162. {
  1163. // Draw at 1,2 within client area of View (i.e. leave a top and left margin of 1)
  1164. // This proves we aren't drawing excess above
  1165. var x = 1;
  1166. var y = 2;
  1167. var width = 5;
  1168. var height = 2;
  1169. var lc = new LineCanvas ();
  1170. // ╔╡╞═════╗
  1171. // Add a short horiz line for ╔╡
  1172. lc.AddLine (new (x, y), 2, Orientation.Horizontal, LineStyle.Double);
  1173. //LHS line down
  1174. lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
  1175. //Vertical line before Title, results in a ╡
  1176. lc.AddLine (new (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
  1177. //Vertical line after Title, results in a ╞
  1178. lc.AddLine (new (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
  1179. // remainder of top line
  1180. lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
  1181. //RHS line down
  1182. lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
  1183. var looksLike = @"
  1184. ╔╡╞══╗
  1185. ║ ║";
  1186. OutputAssert.AssertEqual (output, looksLike, $"{Environment.NewLine}{lc}");
  1187. }
  1188. [Fact]
  1189. public void LineCanvas_UsesFillCorrectly ()
  1190. {
  1191. // Arrange
  1192. var foregroundColor = new Color (255, 0); // Red
  1193. var backgroundColor = new Color (0, 0); // Black
  1194. var foregroundFill = new SolidFill (foregroundColor);
  1195. var backgroundFill = new SolidFill (backgroundColor);
  1196. var fillPair = new FillPair (foregroundFill, backgroundFill);
  1197. var lineCanvas = new LineCanvas
  1198. {
  1199. Fill = fillPair
  1200. };
  1201. // Act
  1202. lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  1203. Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
  1204. // Assert
  1205. foreach (Cell? cell in cellMap.Values)
  1206. {
  1207. Assert.NotNull (cell);
  1208. Assert.Equal (foregroundColor, cell.Value.Attribute!.Value.Foreground);
  1209. Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
  1210. }
  1211. }
  1212. [Fact]
  1213. public void LineCanvas_LineColorIgnoredBecauseOfFill ()
  1214. {
  1215. // Arrange
  1216. var foregroundColor = new Color (255, 0); // Red
  1217. var backgroundColor = new Color (0, 0); // Black
  1218. var lineColor = new Attribute (new Color (0, 255), new Color (255, 255, 255)); // Green on White
  1219. var foregroundFill = new SolidFill (foregroundColor);
  1220. var backgroundFill = new SolidFill (backgroundColor);
  1221. var fillPair = new FillPair (foregroundFill, backgroundFill);
  1222. var lineCanvas = new LineCanvas
  1223. {
  1224. Fill = fillPair
  1225. };
  1226. // Act
  1227. lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single, lineColor);
  1228. Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
  1229. // Assert
  1230. foreach (Cell? cell in cellMap.Values)
  1231. {
  1232. Assert.NotNull (cell);
  1233. Assert.Equal (foregroundColor, cell.Value.Attribute!.Value.Foreground);
  1234. Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
  1235. }
  1236. }
  1237. [Fact]
  1238. public void LineCanvas_IntersectingLinesUseFillCorrectly ()
  1239. {
  1240. // Arrange
  1241. var foregroundColor = new Color (255, 0); // Red
  1242. var backgroundColor = new Color (0, 0); // Black
  1243. var foregroundFill = new SolidFill (foregroundColor);
  1244. var backgroundFill = new SolidFill (backgroundColor);
  1245. var fillPair = new FillPair (foregroundFill, backgroundFill);
  1246. var lineCanvas = new LineCanvas
  1247. {
  1248. Fill = fillPair
  1249. };
  1250. // Act
  1251. lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  1252. lineCanvas.AddLine (new (2, -2), 5, Orientation.Vertical, LineStyle.Single);
  1253. Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
  1254. // Assert
  1255. foreach (Cell? cell in cellMap.Values)
  1256. {
  1257. Assert.NotNull (cell);
  1258. Assert.Equal (foregroundColor, cell.Value.Attribute!.Value.Foreground);
  1259. Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
  1260. }
  1261. }
  1262. // TODO: Remove this and make all LineCanvas tests independent of View
  1263. /// <summary>
  1264. /// Creates a new <see cref="View"/> into which a <see cref="LineCanvas"/> is rendered at
  1265. /// <see cref="View.DrawComplete"/> time.
  1266. /// </summary>
  1267. /// <param name="canvas">The <see cref="LineCanvas"/> you can draw into.</param>
  1268. /// <param name="offsetX">How far to offset drawing in X</param>
  1269. /// <param name="offsetY">How far to offset drawing in Y</param>
  1270. /// <returns></returns>
  1271. private View GetCanvas (IDriver driver, out LineCanvas canvas, int offsetX = 0, int offsetY = 0)
  1272. {
  1273. var v = new View { Width = 10, Height = 5, Viewport = new (0, 0, 10, 5) };
  1274. v.Driver = driver;
  1275. LineCanvas canvasCopy = canvas = new ();
  1276. v.DrawComplete += (s, e) =>
  1277. {
  1278. v.FillRect (v.Viewport);
  1279. foreach (KeyValuePair<Point, Rune> p in canvasCopy.GetMap ())
  1280. {
  1281. v.AddRune (
  1282. offsetX + p.Key.X,
  1283. offsetY + p.Key.Y,
  1284. p.Value
  1285. );
  1286. }
  1287. canvasCopy.Clear ();
  1288. };
  1289. return v;
  1290. }
  1291. #region GetRegion Tests
  1292. [Fact]
  1293. public void GetRegion_EmptyCellMap_ReturnsEmptyRegion ()
  1294. {
  1295. Dictionary<Point, Cell?> cellMap = new ();
  1296. Region region = LineCanvas.GetRegion (cellMap);
  1297. Assert.NotNull (region);
  1298. Assert.True (region.IsEmpty ());
  1299. }
  1300. [Fact]
  1301. public void GetRegion_SingleCell_ReturnsSingleRectangle ()
  1302. {
  1303. Dictionary<Point, Cell?> cellMap = new ()
  1304. {
  1305. { new Point (5, 10), new Cell { Grapheme = "X" } }
  1306. };
  1307. Region region = LineCanvas.GetRegion (cellMap);
  1308. Assert.NotNull (region);
  1309. Assert.False (region.IsEmpty ());
  1310. Assert.True (region.Contains (5, 10));
  1311. }
  1312. [Fact]
  1313. public void GetRegion_HorizontalLine_CreatesHorizontalSpan ()
  1314. {
  1315. Dictionary<Point, Cell?> cellMap = new ();
  1316. // Horizontal line from (5, 10) to (9, 10)
  1317. for (int x = 5; x <= 9; x++)
  1318. {
  1319. cellMap.Add (new Point (x, 10), new Cell { Grapheme = "─" });
  1320. }
  1321. Region region = LineCanvas.GetRegion (cellMap);
  1322. Assert.NotNull (region);
  1323. // All cells in the horizontal span should be in the region
  1324. for (int x = 5; x <= 9; x++)
  1325. {
  1326. Assert.True (region.Contains (x, 10), $"Expected ({x}, 10) to be in region");
  1327. }
  1328. // Cells outside the span should not be in the region
  1329. Assert.False (region.Contains (4, 10));
  1330. Assert.False (region.Contains (10, 10));
  1331. Assert.False (region.Contains (7, 9));
  1332. Assert.False (region.Contains (7, 11));
  1333. }
  1334. [Fact]
  1335. public void GetRegion_VerticalLine_CreatesMultipleHorizontalSpans ()
  1336. {
  1337. Dictionary<Point, Cell?> cellMap = new ();
  1338. // Vertical line from (5, 10) to (5, 14)
  1339. for (int y = 10; y <= 14; y++)
  1340. {
  1341. cellMap.Add (new Point (5, y), new Cell { Grapheme = "│" });
  1342. }
  1343. Region region = LineCanvas.GetRegion (cellMap);
  1344. Assert.NotNull (region);
  1345. // All cells in the vertical line should be in the region
  1346. for (int y = 10; y <= 14; y++)
  1347. {
  1348. Assert.True (region.Contains (5, y), $"Expected (5, {y}) to be in region");
  1349. }
  1350. // Cells outside should not be in the region
  1351. Assert.False (region.Contains (4, 12));
  1352. Assert.False (region.Contains (6, 12));
  1353. }
  1354. [Fact]
  1355. public void GetRegion_LShape_CreatesCorrectSpans ()
  1356. {
  1357. Dictionary<Point, Cell?> cellMap = new ();
  1358. // L-shape: horizontal line from (0, 0) to (5, 0), then vertical to (5, 3)
  1359. for (int x = 0; x <= 5; x++)
  1360. {
  1361. cellMap.Add (new Point (x, 0), new Cell { Grapheme = "─" });
  1362. }
  1363. for (int y = 1; y <= 3; y++)
  1364. {
  1365. cellMap.Add (new Point (5, y), new Cell { Grapheme = "│" });
  1366. }
  1367. Region region = LineCanvas.GetRegion (cellMap);
  1368. // Horizontal part
  1369. for (int x = 0; x <= 5; x++)
  1370. {
  1371. Assert.True (region.Contains (x, 0), $"Expected ({x}, 0) to be in region");
  1372. }
  1373. // Vertical part
  1374. for (int y = 1; y <= 3; y++)
  1375. {
  1376. Assert.True (region.Contains (5, y), $"Expected (5, {y}) to be in region");
  1377. }
  1378. // Empty cells should not be in region
  1379. Assert.False (region.Contains (1, 1));
  1380. Assert.False (region.Contains (4, 2));
  1381. }
  1382. [Fact]
  1383. public void GetRegion_DiscontiguousHorizontalCells_CreatesSeparateSpans ()
  1384. {
  1385. Dictionary<Point, Cell?> cellMap = new ()
  1386. {
  1387. { new Point (0, 5), new Cell { Grapheme = "X" } },
  1388. { new Point (1, 5), new Cell { Grapheme = "X" } },
  1389. // Gap at (2, 5)
  1390. { new Point (3, 5), new Cell { Grapheme = "X" } },
  1391. { new Point (4, 5), new Cell { Grapheme = "X" } }
  1392. };
  1393. Region region = LineCanvas.GetRegion (cellMap);
  1394. Assert.True (region.Contains (0, 5));
  1395. Assert.True (region.Contains (1, 5));
  1396. Assert.False (region.Contains (2, 5)); // Gap
  1397. Assert.True (region.Contains (3, 5));
  1398. Assert.True (region.Contains (4, 5));
  1399. }
  1400. [Fact]
  1401. public void GetRegion_IntersectingLines_CreatesCorrectRegion ()
  1402. {
  1403. Dictionary<Point, Cell?> cellMap = new ();
  1404. // Horizontal line
  1405. for (int x = 0; x <= 4; x++)
  1406. {
  1407. cellMap.Add (new Point (x, 2), new Cell { Grapheme = "─" });
  1408. }
  1409. // Vertical line intersecting at (2, 2)
  1410. for (int y = 0; y <= 4; y++)
  1411. {
  1412. cellMap [new Point (2, y)] = new Cell { Grapheme = "┼" };
  1413. }
  1414. Region region = LineCanvas.GetRegion (cellMap);
  1415. // Horizontal line
  1416. for (int x = 0; x <= 4; x++)
  1417. {
  1418. Assert.True (region.Contains (x, 2), $"Expected ({x}, 2) to be in region");
  1419. }
  1420. // Vertical line
  1421. for (int y = 0; y <= 4; y++)
  1422. {
  1423. Assert.True (region.Contains (2, y), $"Expected (2, {y}) to be in region");
  1424. }
  1425. }
  1426. #endregion
  1427. #region GetCellMapWithRegion Tests
  1428. [Fact]
  1429. public void GetCellMapWithRegion_EmptyCanvas_ReturnsEmptyMapAndRegion ()
  1430. {
  1431. LineCanvas canvas = new ();
  1432. (Dictionary<Point, Cell?> cellMap, Region region) = canvas.GetCellMapWithRegion ();
  1433. Assert.NotNull (cellMap);
  1434. Assert.Empty (cellMap);
  1435. Assert.NotNull (region);
  1436. Assert.True (region.IsEmpty ());
  1437. }
  1438. [Fact]
  1439. public void GetCellMapWithRegion_SingleHorizontalLine_ReturnsCellMapAndRegion ()
  1440. {
  1441. LineCanvas canvas = new ();
  1442. canvas.AddLine (new Point (5, 10), 5, Orientation.Horizontal, LineStyle.Single);
  1443. (Dictionary<Point, Cell?> cellMap, Region region) = canvas.GetCellMapWithRegion ();
  1444. Assert.NotNull (cellMap);
  1445. Assert.NotEmpty (cellMap);
  1446. Assert.NotNull (region);
  1447. Assert.False (region.IsEmpty ());
  1448. // Both cellMap and region should contain the same cells
  1449. foreach (Point p in cellMap.Keys)
  1450. {
  1451. Assert.True (region.Contains (p.X, p.Y), $"Expected ({p.X}, {p.Y}) to be in region");
  1452. }
  1453. }
  1454. [Fact]
  1455. public void GetCellMapWithRegion_SingleVerticalLine_ReturnsCellMapAndRegion ()
  1456. {
  1457. LineCanvas canvas = new ();
  1458. canvas.AddLine (new Point (5, 10), 5, Orientation.Vertical, LineStyle.Single);
  1459. (Dictionary<Point, Cell?> cellMap, Region region) = canvas.GetCellMapWithRegion ();
  1460. Assert.NotNull (cellMap);
  1461. Assert.NotEmpty (cellMap);
  1462. Assert.NotNull (region);
  1463. Assert.False (region.IsEmpty ());
  1464. // Both cellMap and region should contain the same cells
  1465. foreach (Point p in cellMap.Keys)
  1466. {
  1467. Assert.True (region.Contains (p.X, p.Y), $"Expected ({p.X}, {p.Y}) to be in region");
  1468. }
  1469. }
  1470. [Fact]
  1471. public void GetCellMapWithRegion_IntersectingLines_CorrectlyHandlesIntersection ()
  1472. {
  1473. LineCanvas canvas = new ();
  1474. // Create a cross pattern
  1475. canvas.AddLine (new Point (0, 2), 5, Orientation.Horizontal, LineStyle.Single);
  1476. canvas.AddLine (new Point (2, 0), 5, Orientation.Vertical, LineStyle.Single);
  1477. (Dictionary<Point, Cell?> cellMap, Region region) = canvas.GetCellMapWithRegion ();
  1478. Assert.NotNull (cellMap);
  1479. Assert.NotEmpty (cellMap);
  1480. Assert.NotNull (region);
  1481. // Verify intersection point is in both
  1482. Assert.True (cellMap.ContainsKey (new Point (2, 2)), "Intersection should be in cellMap");
  1483. Assert.True (region.Contains (2, 2), "Intersection should be in region");
  1484. // All cells should be in both structures
  1485. foreach (Point p in cellMap.Keys)
  1486. {
  1487. Assert.True (region.Contains (p.X, p.Y), $"Expected ({p.X}, {p.Y}) to be in region");
  1488. }
  1489. }
  1490. [Fact]
  1491. public void GetCellMapWithRegion_ComplexShape_RegionMatchesCellMap ()
  1492. {
  1493. LineCanvas canvas = new ();
  1494. // Create a box
  1495. canvas.AddLine (new Point (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  1496. canvas.AddLine (new Point (0, 3), 5, Orientation.Horizontal, LineStyle.Single);
  1497. canvas.AddLine (new Point (0, 0), 4, Orientation.Vertical, LineStyle.Single);
  1498. canvas.AddLine (new Point (4, 0), 4, Orientation.Vertical, LineStyle.Single);
  1499. (Dictionary<Point, Cell?> cellMap, Region region) = canvas.GetCellMapWithRegion ();
  1500. Assert.NotNull (cellMap);
  1501. Assert.NotEmpty (cellMap);
  1502. Assert.NotNull (region);
  1503. // Every cell in the map should be in the region
  1504. foreach (Point p in cellMap.Keys)
  1505. {
  1506. Assert.True (region.Contains (p.X, p.Y), $"Expected ({p.X}, {p.Y}) to be in region");
  1507. }
  1508. // Cells not in the map should not be in the region (interior of box)
  1509. Assert.False (cellMap.ContainsKey (new Point (2, 1)));
  1510. // Note: Region might contain interior if it's filled, so we just verify consistency
  1511. }
  1512. [Fact]
  1513. public void GetCellMapWithRegion_ResultsMatchSeparateCalls ()
  1514. {
  1515. LineCanvas canvas = new ();
  1516. // Create a complex pattern
  1517. canvas.AddLine (new Point (0, 0), 10, Orientation.Horizontal, LineStyle.Single);
  1518. canvas.AddLine (new Point (5, 0), 10, Orientation.Vertical, LineStyle.Single);
  1519. canvas.AddLine (new Point (0, 5), 10, Orientation.Horizontal, LineStyle.Double);
  1520. // Get results from combined method
  1521. (Dictionary<Point, Cell?> combinedCellMap, Region combinedRegion) = canvas.GetCellMapWithRegion ();
  1522. // Get results from separate calls
  1523. Dictionary<Point, Cell?> separateCellMap = canvas.GetCellMap ();
  1524. Region separateRegion = LineCanvas.GetRegion (separateCellMap);
  1525. // Cell maps should be identical
  1526. Assert.Equal (separateCellMap.Count, combinedCellMap.Count);
  1527. foreach (KeyValuePair<Point, Cell?> kvp in separateCellMap)
  1528. {
  1529. Assert.True (combinedCellMap.ContainsKey (kvp.Key), $"Combined map missing key {kvp.Key}");
  1530. }
  1531. // Regions should contain the same points
  1532. foreach (Point p in combinedCellMap.Keys)
  1533. {
  1534. Assert.True (combinedRegion.Contains (p.X, p.Y), $"Combined region missing ({p.X}, {p.Y})");
  1535. Assert.True (separateRegion.Contains (p.X, p.Y), $"Separate region missing ({p.X}, {p.Y})");
  1536. }
  1537. }
  1538. [Fact]
  1539. public void GetCellMapWithRegion_NegativeCoordinates_HandlesCorrectly ()
  1540. {
  1541. LineCanvas canvas = new ();
  1542. canvas.AddLine (new Point (-5, -5), 10, Orientation.Horizontal, LineStyle.Single);
  1543. canvas.AddLine (new Point (0, -5), 10, Orientation.Vertical, LineStyle.Single);
  1544. (Dictionary<Point, Cell?> cellMap, Region region) = canvas.GetCellMapWithRegion ();
  1545. Assert.NotNull (cellMap);
  1546. Assert.NotEmpty (cellMap);
  1547. Assert.NotNull (region);
  1548. // Verify negative coordinates are handled
  1549. Assert.True (cellMap.Keys.Any (p => p.X < 0 || p.Y < 0), "Should have negative coordinates");
  1550. // All cells should be in region
  1551. foreach (Point p in cellMap.Keys)
  1552. {
  1553. Assert.True (region.Contains (p.X, p.Y), $"Expected ({p.X}, {p.Y}) to be in region");
  1554. }
  1555. }
  1556. [Fact]
  1557. public void GetCellMapWithRegion_WithExclusion_RegionExcludesExcludedCells ()
  1558. {
  1559. LineCanvas canvas = new ();
  1560. canvas.AddLine (new Point (0, 0), 10, Orientation.Horizontal, LineStyle.Single);
  1561. // Exclude middle section
  1562. Region exclusionRegion = new ();
  1563. exclusionRegion.Combine (new Rectangle (3, 0, 4, 1), RegionOp.Union);
  1564. canvas.Exclude (exclusionRegion);
  1565. (Dictionary<Point, Cell?> cellMap, Region region) = canvas.GetCellMapWithRegion ();
  1566. Assert.NotNull (cellMap);
  1567. Assert.NotEmpty (cellMap);
  1568. // Excluded cells should not be in cellMap
  1569. for (int x = 3; x < 7; x++)
  1570. {
  1571. Assert.False (cellMap.ContainsKey (new Point (x, 0)), $"({x}, 0) should be excluded from cellMap");
  1572. }
  1573. // Region should match cellMap
  1574. foreach (Point p in cellMap.Keys)
  1575. {
  1576. Assert.True (region.Contains (p.X, p.Y), $"Expected ({p.X}, {p.Y}) to be in region");
  1577. }
  1578. // Excluded points should not be in region
  1579. for (int x = 3; x < 7; x++)
  1580. {
  1581. Assert.False (region.Contains (x, 0), $"({x}, 0) should be excluded from region");
  1582. }
  1583. }
  1584. #endregion
  1585. }