1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426 |
- using System.Text;
- using UnitTests;
- using Xunit.Abstractions;
- namespace Terminal.Gui.DrawingTests;
- public class LineCanvasTests (ITestOutputHelper output)
- {
- [Theory]
- // Horizontal lines with a vertical zero-length
- [InlineData (
- 0,
- 0,
- 1,
- Orientation.Horizontal,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Single,
- "╞"
- )]
- [InlineData (
- 0,
- 0,
- -1,
- Orientation.Horizontal,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Single,
- "╡"
- )]
- [InlineData (
- 0,
- 0,
- 1,
- Orientation.Horizontal,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Double,
- "╟"
- )]
- [InlineData (
- 0,
- 0,
- -1,
- Orientation.Horizontal,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Double,
- "╢"
- )]
- [InlineData (
- 0,
- 0,
- 1,
- Orientation.Horizontal,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Single,
- "├"
- )]
- [InlineData (
- 0,
- 0,
- -1,
- Orientation.Horizontal,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Single,
- "┤"
- )]
- [InlineData (
- 0,
- 0,
- 1,
- Orientation.Horizontal,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Double,
- "╠"
- )]
- [InlineData (
- 0,
- 0,
- -1,
- Orientation.Horizontal,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Double,
- "╣"
- )]
- // Vertical lines with a horizontal zero-length
- [InlineData (
- 0,
- 0,
- 1,
- Orientation.Vertical,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Single,
- "╥"
- )]
- [InlineData (
- 0,
- 0,
- -1,
- Orientation.Vertical,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Single,
- "╨"
- )]
- [InlineData (
- 0,
- 0,
- 1,
- Orientation.Vertical,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Double,
- "╤"
- )]
- [InlineData (
- 0,
- 0,
- -1,
- Orientation.Vertical,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Double,
- "╧"
- )]
- [InlineData (
- 0,
- 0,
- 1,
- Orientation.Vertical,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Single,
- "┬"
- )]
- [InlineData (
- 0,
- 0,
- -1,
- Orientation.Vertical,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Single,
- "┴"
- )]
- [InlineData (
- 0,
- 0,
- 1,
- Orientation.Vertical,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Double,
- "╦"
- )]
- [InlineData (
- 0,
- 0,
- -1,
- Orientation.Vertical,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Double,
- "╩"
- )]
- // Crosses (two zero-length)
- [InlineData (
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Single,
- "╫"
- )]
- [InlineData (
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Double,
- "╪"
- )]
- [InlineData (
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Single,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Single,
- "┼"
- )]
- [InlineData (
- 0,
- 0,
- 0,
- Orientation.Vertical,
- LineStyle.Double,
- 0,
- 0,
- 0,
- Orientation.Horizontal,
- LineStyle.Double,
- "╬"
- )]
- public void Add_2_Lines (
- int x1,
- int y1,
- int len1,
- Orientation o1,
- LineStyle s1,
- int x2,
- int y2,
- int len2,
- Orientation o2,
- LineStyle s2,
- string expected
- )
- {
- View v = GetCanvas (out LineCanvas lc);
- v.Width = 10;
- v.Height = 10;
- v.Viewport = new (0, 0, 10, 10);
- lc.AddLine (new (x1, y1), len1, o1, s1);
- lc.AddLine (new (x2, y2), len2, o2, s2);
- OutputAssert.AssertEqual (output, expected, lc.ToString ());
- v.Dispose ();
- }
- [InlineData (
- 0,
- 0,
- 0,
- 0,
- 0,
- 1,
- 1
- )]
- [InlineData (
- 0,
- 0,
- 1,
- 0,
- 0,
- 1,
- 1
- )]
- [InlineData (
- 0,
- 0,
- 2,
- 0,
- 0,
- 2,
- 2
- )]
- [InlineData (
- 0,
- 0,
- 3,
- 0,
- 0,
- 3,
- 3
- )]
- [InlineData (
- 0,
- 0,
- -1,
- 0,
- 0,
- 1,
- 1
- )]
- [InlineData (
- 0,
- 0,
- -2,
- -1,
- -1,
- 2,
- 2
- )]
- [InlineData (
- 0,
- 0,
- -3,
- -2,
- -2,
- 3,
- 3
- )]
- [Theory]
- [SetupFakeDriver]
- public void Viewport_H_And_V_Lines_Both_Positive (
- int x,
- int y,
- int length,
- int expectedX,
- int expectedY,
- int expectedWidth,
- int expectedHeight
- )
- {
- var canvas = new LineCanvas ();
- canvas.AddLine (new (x, y), length, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new (x, y), length, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Bounds);
- }
- [InlineData (
- 0,
- 0,
- 0,
- 0,
- 0,
- 1,
- 1
- )]
- [InlineData (
- 0,
- 0,
- 1,
- 0,
- 0,
- 1,
- 1
- )]
- [InlineData (
- 0,
- 0,
- 2,
- 0,
- 0,
- 2,
- 1
- )]
- [InlineData (
- 0,
- 0,
- 3,
- 0,
- 0,
- 3,
- 1
- )]
- [InlineData (
- 0,
- 0,
- -1,
- 0,
- 0,
- 1,
- 1
- )]
- [InlineData (
- 0,
- 0,
- -2,
- -1,
- 0,
- 2,
- 1
- )]
- [InlineData (
- 0,
- 0,
- -3,
- -2,
- 0,
- 3,
- 1
- )]
- [Theory]
- [SetupFakeDriver]
- public void Viewport_H_Line (
- int x,
- int y,
- int length,
- int expectedX,
- int expectedY,
- int expectedWidth,
- int expectedHeight
- )
- {
- var canvas = new LineCanvas ();
- canvas.AddLine (new (x, y), length, Orientation.Horizontal, LineStyle.Single);
- Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Bounds);
- }
- [Fact]
- [SetupFakeDriver]
- public void Viewport_Specific ()
- {
- // Draw at 1,1 within client area of View (i.e. leave a top and left margin of 1)
- // This proves we aren't drawing excess above
- var x = 1;
- var y = 2;
- var width = 3;
- var height = 2;
- var lc = new LineCanvas ();
- // 01230
- // ╔╡╞╗1
- // ║ ║2
- // Add a short horiz line for ╔╡
- lc.AddLine (new (x, y), 2, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new (x, y, 2, 1), lc.Bounds);
- //LHS line down
- lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new (x, y, 2, 2), lc.Bounds);
- //Vertical line before Title, results in a ╡
- lc.AddLine (new (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new (x, y, 2, 2), lc.Bounds);
- //Vertical line after Title, results in a ╞
- lc.AddLine (new (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new (x, y, 3, 2), lc.Bounds);
- // remainder of top line
- lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new (x, y, 4, 2), lc.Bounds);
- //RHS line down
- lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new (x, y, 4, 2), lc.Bounds);
- OutputAssert.AssertEqual (
- output,
- @"
- ╔╡╞╗
- ║ ║",
- $"{Environment.NewLine}{lc}"
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void Viewport_Specific_With_Ustring ()
- {
- // Draw at 1,1 within client area of View (i.e. leave a top and left margin of 1)
- // This proves we aren't drawing excess above
- var x = 1;
- var y = 2;
- var width = 3;
- var height = 2;
- var lc = new LineCanvas ();
- // 01230
- // ╔╡╞╗1
- // ║ ║2
- // Add a short horiz line for ╔╡
- lc.AddLine (new (x, y), 2, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new (x, y, 2, 1), lc.Bounds);
- //LHS line down
- lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new (x, y, 2, 2), lc.Bounds);
- //Vertical line before Title, results in a ╡
- lc.AddLine (new (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new (x, y, 2, 2), lc.Bounds);
- //Vertical line after Title, results in a ╞
- lc.AddLine (new (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new (x, y, 3, 2), lc.Bounds);
- // remainder of top line
- lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new (x, y, 4, 2), lc.Bounds);
- //RHS line down
- lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new (x, y, 4, 2), lc.Bounds);
- OutputAssert.AssertEqual (
- output,
- @"
- ╔╡╞╗
- ║ ║",
- $"{Environment.NewLine}{lc}"
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void Canvas_Updates_On_Changes ()
- {
- var lc = new LineCanvas ();
- Assert.Equal (Rectangle.Empty, lc.Bounds);
- lc.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Double);
- Assert.NotEqual (Rectangle.Empty, lc.Bounds);
- lc.Clear ();
- Assert.Equal (Rectangle.Empty, lc.Bounds);
- }
- [InlineData (0, 0, Orientation.Horizontal, "─")]
- [InlineData (1, 0, Orientation.Horizontal, "─")]
- [InlineData (0, 1, Orientation.Horizontal, "─")]
- [InlineData (-1, 0, Orientation.Horizontal, "─")]
- [InlineData (0, -1, Orientation.Horizontal, "─")]
- [InlineData (-1, -1, Orientation.Horizontal, "─")]
- [InlineData (0, 0, Orientation.Vertical, "│")]
- [InlineData (1, 0, Orientation.Vertical, "│")]
- [InlineData (0, 1, Orientation.Vertical, "│")]
- [InlineData (0, -1, Orientation.Vertical, "│")]
- [InlineData (-1, 0, Orientation.Vertical, "│")]
- [InlineData (-1, -1, Orientation.Vertical, "│")]
- [Theory]
- [SetupFakeDriver]
- public void Length_0_Is_1_Long (int x, int y, Orientation orientation, string expected)
- {
- var canvas = new LineCanvas ();
- // Add a line at 5, 5 that's has length of 1
- canvas.AddLine (new (x, y), 1, orientation, LineStyle.Single);
- OutputAssert.AssertEqual (output, $"{expected}", $"{canvas}");
- }
- // X is offset by 2
- [InlineData (0, 0, 1, Orientation.Horizontal, "─")]
- [InlineData (1, 0, 1, Orientation.Horizontal, "─")]
- [InlineData (0, 1, 1, Orientation.Horizontal, "─")]
- [InlineData (0, 0, 1, Orientation.Vertical, "│")]
- [InlineData (1, 0, 1, Orientation.Vertical, "│")]
- [InlineData (0, 1, 1, Orientation.Vertical, "│")]
- [InlineData (-1, 0, 1, Orientation.Horizontal, "─")]
- [InlineData (0, -1, 1, Orientation.Horizontal, "─")]
- [InlineData (-1, 0, 1, Orientation.Vertical, "│")]
- [InlineData (0, -1, 1, Orientation.Vertical, "│")]
- [InlineData (0, 0, -1, Orientation.Horizontal, "─")]
- [InlineData (1, 0, -1, Orientation.Horizontal, "─")]
- [InlineData (0, 1, -1, Orientation.Horizontal, "─")]
- [InlineData (0, 0, -1, Orientation.Vertical, "│")]
- [InlineData (1, 0, -1, Orientation.Vertical, "│")]
- [InlineData (0, 1, -1, Orientation.Vertical, "│")]
- [InlineData (-1, 0, -1, Orientation.Horizontal, "─")]
- [InlineData (0, -1, -1, Orientation.Horizontal, "─")]
- [InlineData (-1, 0, -1, Orientation.Vertical, "│")]
- [InlineData (0, -1, -1, Orientation.Vertical, "│")]
- [InlineData (0, 0, 2, Orientation.Horizontal, "──")]
- [InlineData (1, 0, 2, Orientation.Horizontal, "──")]
- [InlineData (0, 1, 2, Orientation.Horizontal, "──")]
- [InlineData (1, 1, 2, Orientation.Horizontal, "──")]
- [InlineData (0, 0, 2, Orientation.Vertical, "│\r\n│")]
- [InlineData (1, 0, 2, Orientation.Vertical, "│\r\n│")]
- [InlineData (0, 1, 2, Orientation.Vertical, "│\r\n│")]
- [InlineData (1, 1, 2, Orientation.Vertical, "│\r\n│")]
- [InlineData (-1, 0, 2, Orientation.Horizontal, "──")]
- [InlineData (0, -1, 2, Orientation.Horizontal, "──")]
- [InlineData (-1, 0, 2, Orientation.Vertical, "│\r\n│")]
- [InlineData (0, -1, 2, Orientation.Vertical, "│\r\n│")]
- [InlineData (-1, -1, 2, Orientation.Vertical, "│\r\n│")]
- [InlineData (0, 0, -2, Orientation.Horizontal, "──")]
- [InlineData (1, 0, -2, Orientation.Horizontal, "──")]
- [InlineData (0, 1, -2, Orientation.Horizontal, "──")]
- [InlineData (0, 0, -2, Orientation.Vertical, "│\r\n│")]
- [InlineData (1, 0, -2, Orientation.Vertical, "│\r\n│")]
- [InlineData (0, 1, -2, Orientation.Vertical, "│\r\n│")]
- [InlineData (1, 1, -2, Orientation.Vertical, "│\r\n│")]
- [InlineData (-1, 0, -2, Orientation.Horizontal, "──")]
- [InlineData (0, -1, -2, Orientation.Horizontal, "──")]
- [InlineData (-1, 0, -2, Orientation.Vertical, "│\r\n│")]
- [InlineData (0, -1, -2, Orientation.Vertical, "│\r\n│")]
- [InlineData (-1, -1, -2, Orientation.Vertical, "│\r\n│")]
- [Theory]
- [SetupFakeDriver]
- public void Length_n_Is_n_Long (int x, int y, int length, Orientation orientation, string expected)
- {
- var canvas = new LineCanvas ();
- canvas.AddLine (new (x, y), length, orientation, LineStyle.Single);
- var result = canvas.ToString ();
- OutputAssert.AssertEqual (output, expected, result);
- }
- [Fact]
- [SetupFakeDriver]
- public void Length_Negative ()
- {
- var offset = new Point (5, 5);
- var canvas = new LineCanvas ();
- canvas.AddLine (offset, -3, Orientation.Horizontal, LineStyle.Single);
- var looksLike = "───";
- Assert.Equal (looksLike, $"{canvas}");
- }
- [InlineData (Orientation.Horizontal, "─")]
- [InlineData (Orientation.Vertical, "│")]
- [Theory]
- [SetupFakeDriver]
- public void Length_Zero_Alone_Is_Line (Orientation orientation, string expected)
- {
- var lc = new LineCanvas ();
- // Add a line at 0, 0 that's has length of 0
- lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
- OutputAssert.AssertEqual (output, expected, $"{lc}");
- }
- [InlineData (Orientation.Horizontal, "┼")]
- [InlineData (Orientation.Vertical, "┼")]
- [Theory]
- [SetupFakeDriver]
- public void Length_Zero_Cross_Is_Cross (Orientation orientation, string expected)
- {
- var lc = new LineCanvas ();
- // Add point at opposite orientation
- lc.AddLine (
- Point.Empty,
- 0,
- orientation == Orientation.Horizontal ? Orientation.Vertical : Orientation.Horizontal,
- LineStyle.Single
- );
- // Add a line at 0, 0 that's has length of 0
- lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
- OutputAssert.AssertEqual (output, expected, $"{lc}");
- }
- [InlineData (Orientation.Horizontal, "╥")]
- [InlineData (Orientation.Vertical, "╞")]
- [Theory]
- [SetupFakeDriver]
- public void Length_Zero_NextTo_Opposite_Is_T (Orientation orientation, string expected)
- {
- var lc = new LineCanvas ();
- // Add line with length of 1 in opposite orientation starting at same location
- if (orientation == Orientation.Horizontal)
- {
- lc.AddLine (Point.Empty, 1, Orientation.Vertical, LineStyle.Double);
- }
- else
- {
- lc.AddLine (Point.Empty, 1, Orientation.Horizontal, LineStyle.Double);
- }
- // Add a line at 0, 0 that's has length of 0
- lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single);
- OutputAssert.AssertEqual (output, expected, $"{lc}");
- }
- [Fact]
- public void TestLineCanvas_LeaveMargin_Top1_Left1 ()
- {
- var canvas = new LineCanvas ();
- // Upper box
- canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Single);
- var looksLike =
- @"
- ┌─
- │ ";
- OutputAssert.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}");
- }
- [Fact]
- [SetupFakeDriver]
- public void TestLineCanvas_Window_Heavy ()
- {
- View v = GetCanvas (out LineCanvas canvas);
- // outer box
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Heavy);
- canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Heavy);
- canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Heavy);
- v.Draw ();
- var looksLike =
- @"
- ┏━━━━┳━━━┓
- ┃ ┃ ┃
- ┣━━━━╋━━━┫
- ┃ ┃ ┃
- ┗━━━━┻━━━┛";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Theory]
- [SetupFakeDriver]
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- public void TestLineCanvas_Window_HeavyTop_ThinSides (LineStyle thinStyle)
- {
- View v = GetCanvas (out LineCanvas canvas);
- // outer box
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Heavy);
- canvas.AddLine (new (9, 0), 5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Heavy);
- canvas.AddLine (new (0, 4), -5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new (5, 0), 5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Heavy);
- v.Draw ();
- var looksLike =
- @"
- ┍━━━━┯━━━┑
- │ │ │
- ┝━━━━┿━━━┥
- │ │ │
- ┕━━━━┷━━━┙
- ";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Theory]
- [SetupFakeDriver]
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- public void TestLineCanvas_Window_ThinTop_HeavySides (LineStyle thinStyle)
- {
- View v = GetCanvas (out LineCanvas canvas);
- // outer box
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, thinStyle);
- canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, thinStyle);
- canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, thinStyle);
- v.Draw ();
- var looksLike =
- @"
- ┎────┰───┒
- ┃ ┃ ┃
- ┠────╂───┨
- ┃ ┃ ┃
- ┖────┸───┚
- ";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void Top_Left_From_TopRight_LeftUp ()
- {
- var canvas = new LineCanvas ();
- // Upper box
- canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new (0, 1), -2, Orientation.Vertical, LineStyle.Single);
- var looksLike =
- @"
- ┌─
- │ ";
- OutputAssert.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}");
- }
- [Fact]
- [SetupFakeDriver]
- public void Top_With_1Down ()
- {
- var canvas = new LineCanvas ();
- // Top ─
- canvas.AddLine (Point.Empty, 1, Orientation.Horizontal, LineStyle.Single);
- // Bottom ─
- canvas.AddLine (new (1, 1), -1, Orientation.Horizontal, LineStyle.Single);
- //// Right down
- //canvas.AddLine (new Point (9, 0), 3, Orientation.Vertical, LineStyle.Single);
- //// Bottom
- //canvas.AddLine (new Point (9, 3), -10, Orientation.Horizontal, LineStyle.Single);
- //// Left Up
- //canvas.AddLine (new Point (0, 3), -3, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new (0, 0, 2, 2), canvas.Bounds);
- Dictionary<Point, Rune> map = canvas.GetMap ();
- Assert.Equal (2, map.Count);
- OutputAssert.AssertEqual (
- output,
- @"
- ─
- ─",
- $"{Environment.NewLine}{canvas}"
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ToString_Empty ()
- {
- var lc = new LineCanvas ();
- OutputAssert.AssertEqual (output, string.Empty, lc.ToString ());
- }
- // 012
- [InlineData (0, 0, "═══")]
- [InlineData (1, 0, "═══")]
- [InlineData (0, 1, "═══")]
- [InlineData (1, 1, "═══")]
- [InlineData (2, 2, "═══")]
- [InlineData (-1, 0, "═══")]
- [InlineData (0, -1, "═══")]
- [InlineData (-1, -1, "═══")]
- [InlineData (-2, -2, "═══")]
- [Theory]
- [SetupFakeDriver]
- public void ToString_Positive_Horizontal_1Line_Offset (int x, int y, string expected)
- {
- var lc = new LineCanvas ();
- lc.AddLine (new (x, y), 3, Orientation.Horizontal, LineStyle.Double);
- OutputAssert.AssertEqual (output, expected, $"{lc}");
- }
- [InlineData (0, 0, 0, 0, "═══")]
- [InlineData (1, 0, 1, 0, "═══")]
- [InlineData (-1, 0, -1, 0, "═══")]
- [InlineData (0, 0, 1, 0, "════")]
- [InlineData (1, 0, 3, 0, "═════")]
- [InlineData (1, 0, 4, 0, "══════")]
- [InlineData (1, 0, 5, 0, "═══ ═══")]
- [InlineData (0, 0, 0, 1, "\u2550\u2550\u2550\r\n\u2550\u2550\u2550")]
- [InlineData (0, 0, 1, 1, "═══ \r\n ═══")]
- [InlineData (0, 0, 2, 1, "═══ \r\n ═══")]
- [InlineData (1, 0, 0, 1, " ═══\r\n═══ ")]
- [InlineData (0, 1, 0, 1, "═══")]
- [InlineData (1, 1, 0, 1, "════")]
- [InlineData (2, 2, 0, 1, "═══ \r\n ═══")]
- [Theory]
- [SetupFakeDriver]
- public void ToString_Positive_Horizontal_2Line_Offset (int x1, int y1, int x2, int y2, string expected)
- {
- var lc = new LineCanvas ();
- lc.AddLine (new (x1, y1), 3, Orientation.Horizontal, LineStyle.Double);
- lc.AddLine (new (x2, y2), 3, Orientation.Horizontal, LineStyle.Double);
- OutputAssert.AssertEqual (output, expected, $"{lc}");
- }
- // [Fact, SetupFakeDriver]
- // public void LeaveMargin_Top1_Left1 ()
- // {
- // var canvas = new LineCanvas ();
- // // Upper box
- // canvas.AddLine (Point.Empty, 9, Orientation.Horizontal, LineStyle.Single);
- // canvas.AddLine (new Point (8, 0), 3, Orientation.Vertical, LineStyle.Single);
- // canvas.AddLine (new Point (8, 3), -9, Orientation.Horizontal, LineStyle.Single);
- // canvas.AddLine (new Point (0, 2), -3, Orientation.Vertical, LineStyle.Single);
- // // Lower Box
- // canvas.AddLine (new Point (5, 0), 2, Orientation.Vertical, LineStyle.Single);
- // canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, LineStyle.Single);
- // string looksLike =
- //@"
- //┌────┬──┐
- //│ │ │
- //├────┼──┤
- //└────┴──┘
- //";
- // Assert.Equal (looksLike, $"{Environment.NewLine}{canvas}");
- // }
- [InlineData (0, 0, 0, Orientation.Horizontal, LineStyle.Double, "═")]
- [InlineData (0, 0, 0, Orientation.Vertical, LineStyle.Double, "║")]
- [InlineData (0, 0, 0, Orientation.Horizontal, LineStyle.Single, "─")]
- [InlineData (0, 0, 0, Orientation.Vertical, LineStyle.Single, "│")]
- [InlineData (0, 0, 1, Orientation.Horizontal, LineStyle.Double, "═")]
- [InlineData (0, 0, 1, Orientation.Vertical, LineStyle.Double, "║")]
- [InlineData (0, 0, 1, Orientation.Horizontal, LineStyle.Single, "─")]
- [InlineData (0, 0, 1, Orientation.Vertical, LineStyle.Single, "│")]
- [InlineData (0, 0, 2, Orientation.Horizontal, LineStyle.Double, "══")]
- [InlineData (0, 0, 2, Orientation.Vertical, LineStyle.Double, "║\n║")]
- [InlineData (0, 0, 2, Orientation.Horizontal, LineStyle.Single, "──")]
- [InlineData (0, 0, 2, Orientation.Vertical, LineStyle.Single, "│\n│")]
- [SetupFakeDriver]
- [Theory]
- public void View_Draws_1LineTests (
- int x1,
- int y1,
- int length,
- Orientation o1,
- LineStyle s1,
- string expected
- )
- {
- View v = GetCanvas (out LineCanvas lc);
- v.Width = 10;
- v.Height = 10;
- v.Viewport = new (0, 0, 10, 10);
- lc.AddLine (new (x1, y1), length, o1, s1);
- v.Draw ();
- DriverAssert.AssertDriverContentsAre (expected, output);
- v.Dispose ();
- }
- /// <summary>This test demonstrates how to correctly trigger a corner. By overlapping the lines in the same cell</summary>
- [Fact]
- [SetupFakeDriver]
- public void View_Draws_Corner_Correct ()
- {
- View v = GetCanvas (out LineCanvas canvas);
- canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Single);
- v.Draw ();
- var looksLike =
- @"
- ┌─
- │";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- /// <summary>
- /// This test demonstrates that corners are only drawn when lines overlap. Not when they terminate adjacent to one
- /// another.
- /// </summary>
- [Fact]
- [SetupFakeDriver]
- public void View_Draws_Corner_NoOverlap ()
- {
- View v = GetCanvas (out LineCanvas canvas);
- canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new (0, 1), 2, Orientation.Vertical, LineStyle.Single);
- v.Draw ();
- var looksLike =
- @"
- ──
- │
- │";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- [Theory]
- [SetupFakeDriver]
- public void View_Draws_Horizontal (LineStyle style)
- {
- View v = GetCanvas (out LineCanvas canvas);
- canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, style);
- v.Draw ();
- var looksLike =
- @"
- ──";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void View_Draws_Horizontal_Double ()
- {
- View v = GetCanvas (out LineCanvas canvas);
- canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Double);
- v.Draw ();
- var looksLike =
- @"
- ══";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- [Theory]
- [SetupFakeDriver]
- public void View_Draws_Vertical (LineStyle style)
- {
- View v = GetCanvas (out LineCanvas canvas);
- canvas.AddLine (Point.Empty, 2, Orientation.Vertical, style);
- v.Draw ();
- var looksLike =
- @"
- │
- │";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void View_Draws_Vertical_Double ()
- {
- View v = GetCanvas (out LineCanvas canvas);
- canvas.AddLine (Point.Empty, 2, Orientation.Vertical, LineStyle.Double);
- v.Draw ();
- var looksLike =
- @"
- ║
- ║";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void View_Draws_Window_Double ()
- {
- View v = GetCanvas (out LineCanvas canvas);
- // outer box
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Double);
- canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Double);
- canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Double);
- v.Draw ();
- var looksLike =
- @"
- ╔════╦═══╗
- ║ ║ ║
- ╠════╬═══╣
- ║ ║ ║
- ╚════╩═══╝";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Theory]
- [SetupFakeDriver]
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- public void View_Draws_Window_DoubleTop_SingleSides (LineStyle thinStyle)
- {
- View v = GetCanvas (out LineCanvas canvas);
- // outer box
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Double);
- canvas.AddLine (new (9, 0), 5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Double);
- canvas.AddLine (new (0, 4), -5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new (5, 0), 5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Double);
- v.Draw ();
- var looksLike =
- @"
- ╒════╤═══╕
- │ │ │
- ╞════╪═══╡
- │ │ │
- ╘════╧═══╛
- ";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- /// <summary>
- /// Demonstrates when <see cref="LineStyle.Rounded"/> corners are used. Notice how not all lines declare rounded.
- /// If there are 1+ lines intersecting and a corner is to be used then if any of them are rounded a rounded corner is
- /// used.
- /// </summary>
- [Fact]
- [SetupFakeDriver]
- public void View_Draws_Window_Rounded ()
- {
- View v = GetCanvas (out LineCanvas canvas);
- // outer box
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Rounded);
- // LineStyle.Single is ignored because corner overlaps with the above line which is Rounded
- // this results in a rounded corner being used.
- canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Single);
- canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Rounded);
- canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Single);
- // These lines say rounded but they will result in the T sections which are never rounded.
- canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Rounded);
- canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Rounded);
- v.Draw ();
- var looksLike =
- @"
- ╭────┬───╮
- │ │ │
- ├────┼───┤
- │ │ │
- ╰────┴───╯";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Theory]
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- [SetupFakeDriver]
- public void View_Draws_Window_SingleTop_DoubleSides (LineStyle thinStyle)
- {
- View v = GetCanvas (out LineCanvas canvas);
- // outer box
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, thinStyle);
- canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, thinStyle);
- canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, thinStyle);
- v.Draw ();
- var looksLike =
- @"
- ╓────╥───╖
- ║ ║ ║
- ╟────╫───╢
- ║ ║ ║
- ╙────╨───╜
- ";
- DriverAssert.AssertDriverContentsAre (looksLike, output);
- v.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void Window ()
- {
- var canvas = new LineCanvas ();
- // Frame
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Single);
- canvas.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Single);
- // Cross
- canvas.AddLine (new (5, 0), 5, Orientation.Vertical, LineStyle.Single);
- canvas.AddLine (new (0, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var looksLike =
- @"
- ┌────┬───┐
- │ │ │
- ├────┼───┤
- │ │ │
- └────┴───┘";
- OutputAssert.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}");
- }
- [Fact]
- [SetupFakeDriver]
- public void Zero_Length_Intersections ()
- {
- // Draw at 1,2 within client area of View (i.e. leave a top and left margin of 1)
- // This proves we aren't drawing excess above
- var x = 1;
- var y = 2;
- var width = 5;
- var height = 2;
- var lc = new LineCanvas ();
- // ╔╡╞═════╗
- // Add a short horiz line for ╔╡
- lc.AddLine (new (x, y), 2, Orientation.Horizontal, LineStyle.Double);
- //LHS line down
- lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
- //Vertical line before Title, results in a ╡
- lc.AddLine (new (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
- //Vertical line after Title, results in a ╞
- lc.AddLine (new (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
- // remainder of top line
- lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
- //RHS line down
- lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
- var looksLike = @"
- ╔╡╞══╗
- ║ ║";
- OutputAssert.AssertEqual (output, looksLike, $"{Environment.NewLine}{lc}");
- }
- [Fact]
- public void LineCanvas_UsesFillCorrectly ()
- {
- // Arrange
- var foregroundColor = new Color (255, 0); // Red
- var backgroundColor = new Color (0, 0); // Black
- var foregroundFill = new SolidFill (foregroundColor);
- var backgroundFill = new SolidFill (backgroundColor);
- var fillPair = new FillPair (foregroundFill, backgroundFill);
- var lineCanvas = new LineCanvas
- {
- Fill = fillPair
- };
- // Act
- lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
- Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
- // Assert
- foreach (Cell? cell in cellMap.Values)
- {
- Assert.NotNull (cell);
- Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground);
- Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
- }
- }
- [Fact]
- public void LineCanvas_LineColorIgnoredBecauseOfFill ()
- {
- // Arrange
- var foregroundColor = new Color (255, 0); // Red
- var backgroundColor = new Color (0, 0); // Black
- var lineColor = new Attribute (new Color (0, 255), new Color (255, 255, 255)); // Green on White
- var foregroundFill = new SolidFill (foregroundColor);
- var backgroundFill = new SolidFill (backgroundColor);
- var fillPair = new FillPair (foregroundFill, backgroundFill);
- var lineCanvas = new LineCanvas
- {
- Fill = fillPair
- };
- // Act
- lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single, lineColor);
- Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
- // Assert
- foreach (Cell? cell in cellMap.Values)
- {
- Assert.NotNull (cell);
- Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground);
- Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
- }
- }
- [Fact]
- public void LineCanvas_IntersectingLinesUseFillCorrectly ()
- {
- // Arrange
- var foregroundColor = new Color (255, 0); // Red
- var backgroundColor = new Color (0, 0); // Black
- var foregroundFill = new SolidFill (foregroundColor);
- var backgroundFill = new SolidFill (backgroundColor);
- var fillPair = new FillPair (foregroundFill, backgroundFill);
- var lineCanvas = new LineCanvas
- {
- Fill = fillPair
- };
- // Act
- lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
- lineCanvas.AddLine (new (2, -2), 5, Orientation.Vertical, LineStyle.Single);
- Dictionary<Point, Cell?> cellMap = lineCanvas.GetCellMap ();
- // Assert
- foreach (Cell? cell in cellMap.Values)
- {
- Assert.NotNull (cell);
- Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground);
- Assert.Equal (backgroundColor, cell.Value.Attribute.Value.Background);
- }
- }
- // TODO: Remove this and make all LineCanvas tests independent of View
- /// <summary>
- /// Creates a new <see cref="View"/> into which a <see cref="LineCanvas"/> is rendered at
- /// <see cref="View.DrawComplete"/> time.
- /// </summary>
- /// <param name="canvas">The <see cref="LineCanvas"/> you can draw into.</param>
- /// <param name="offsetX">How far to offset drawing in X</param>
- /// <param name="offsetY">How far to offset drawing in Y</param>
- /// <returns></returns>
- private View GetCanvas (out LineCanvas canvas, int offsetX = 0, int offsetY = 0)
- {
- var v = new View { Width = 10, Height = 5, Viewport = new (0, 0, 10, 5) };
- LineCanvas canvasCopy = canvas = new ();
- v.DrawComplete += (s, e) =>
- {
- v.FillRect (v.Viewport);
- foreach (KeyValuePair<Point, Rune> p in canvasCopy.GetMap ())
- {
- v.AddRune (
- offsetX + p.Key.X,
- offsetY + p.Key.Y,
- p.Value
- );
- }
- canvasCopy.Clear ();
- };
- return v;
- }
- }
|