12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331 |
- using System.Text;
- using Xunit.Abstractions;
- namespace Terminal.Gui.DrawingTests;
- public class LineCanvasTests
- {
- private readonly ITestOutputHelper output;
- public LineCanvasTests (ITestOutputHelper output) { this.output = output; }
- [Theory]
- [AutoInitShutdown]
- // 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 Rectangle (0, 0, 10, 10);
- lc.AddLine (new Point (x1, y1), len1, o1, s1);
- lc.AddLine (new Point (x2, y2), len2, o2, s2);
- TestHelpers.AssertEqual (output, expected, lc.ToString ());
- }
- [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 Bounds_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 Point (x, y), length, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new Point (x, y), length, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new Rectangle (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 Bounds_H_Line (
- int x,
- int y,
- int length,
- int expectedX,
- int expectedY,
- int expectedWidth,
- int expectedHeight
- )
- {
- var canvas = new LineCanvas ();
- canvas.AddLine (new Point (x, y), length, Orientation.Horizontal, LineStyle.Single);
- Assert.Equal (new Rectangle (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Bounds);
- }
- [Fact]
- [SetupFakeDriver]
- public void Bounds_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 Point (x, y), 2, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new Rectangle (x, y, 2, 1), lc.Bounds);
- //LHS line down
- lc.AddLine (new Point (x, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new Rectangle (x, y, 2, 2), lc.Bounds);
- //Vertical line before Title, results in a ╡
- lc.AddLine (new Point (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new Rectangle (x, y, 2, 2), lc.Bounds);
- //Vertical line after Title, results in a ╞
- lc.AddLine (new Point (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new Rectangle (x, y, 3, 2), lc.Bounds);
- // remainder of top line
- lc.AddLine (new Point (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new Rectangle (x, y, 4, 2), lc.Bounds);
- //RHS line down
- lc.AddLine (new Point (x + width, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new Rectangle (x, y, 4, 2), lc.Bounds);
- TestHelpers.AssertEqual (
- output,
- @"
- ╔╡╞╗
- ║ ║",
- $"{Environment.NewLine}{lc}"
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void Bounds_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 Point (x, y), 2, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new Rectangle (x, y, 2, 1), lc.Bounds);
- //LHS line down
- lc.AddLine (new Point (x, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new Rectangle (x, y, 2, 2), lc.Bounds);
- //Vertical line before Title, results in a ╡
- lc.AddLine (new Point (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new Rectangle (x, y, 2, 2), lc.Bounds);
- //Vertical line after Title, results in a ╞
- lc.AddLine (new Point (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
- Assert.Equal (new Rectangle (x, y, 3, 2), lc.Bounds);
- // remainder of top line
- lc.AddLine (new Point (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new Rectangle (x, y, 4, 2), lc.Bounds);
- //RHS line down
- lc.AddLine (new Point (x + width, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new Rectangle (x, y, 4, 2), lc.Bounds);
- TestHelpers.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 Point (x, y), 1, orientation, LineStyle.Single);
- TestHelpers.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 Point (x, y), length, orientation, LineStyle.Single);
- var result = canvas.ToString ();
- TestHelpers.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);
- TestHelpers.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);
- TestHelpers.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);
- TestHelpers.AssertEqual (output, expected, $"{lc}");
- }
- [Fact]
- [AutoInitShutdown]
- 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 =
- @"
- ┌─
- │ ";
- TestHelpers.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}");
- }
- [Fact]
- [AutoInitShutdown]
- 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 Point (9, 0), 5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new Point (9, 4), -10, Orientation.Horizontal, LineStyle.Heavy);
- canvas.AddLine (new Point (0, 4), -5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new Point (5, 0), 5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new Point (0, 2), 10, Orientation.Horizontal, LineStyle.Heavy);
- v.Draw ();
- var looksLike =
- @"
- ┏━━━━┳━━━┓
- ┃ ┃ ┃
- ┣━━━━╋━━━┫
- ┃ ┃ ┃
- ┗━━━━┻━━━┛";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Theory]
- [AutoInitShutdown]
- [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 Point (9, 0), 5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new Point (9, 4), -10, Orientation.Horizontal, LineStyle.Heavy);
- canvas.AddLine (new Point (0, 4), -5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new Point (5, 0), 5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new Point (0, 2), 10, Orientation.Horizontal, LineStyle.Heavy);
- v.Draw ();
- var looksLike =
- @"
- ┍━━━━┯━━━┑
- │ │ │
- ┝━━━━┿━━━┥
- │ │ │
- ┕━━━━┷━━━┙
- ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Theory]
- [AutoInitShutdown]
- [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 Point (9, 0), 5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new Point (9, 4), -10, Orientation.Horizontal, thinStyle);
- canvas.AddLine (new Point (0, 4), -5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new Point (5, 0), 5, Orientation.Vertical, LineStyle.Heavy);
- canvas.AddLine (new Point (0, 2), 10, Orientation.Horizontal, thinStyle);
- v.Draw ();
- var looksLike =
- @"
- ┎────┰───┒
- ┃ ┃ ┃
- ┠────╂───┨
- ┃ ┃ ┃
- ┖────┸───┚
- ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [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 Point (0, 1), -2, Orientation.Vertical, LineStyle.Single);
- var looksLike =
- @"
- ┌─
- │ ";
- TestHelpers.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 Point (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 Rectangle (0, 0, 2, 2), canvas.Bounds);
- Dictionary<Point, Rune> map = canvas.GetMap ();
- Assert.Equal (2, map.Count);
- TestHelpers.AssertEqual (
- output,
- @"
- ─
- ─",
- $"{Environment.NewLine}{canvas}"
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ToString_Empty ()
- {
- var lc = new LineCanvas ();
- TestHelpers.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 Point (x, y), 3, Orientation.Horizontal, LineStyle.Double);
- TestHelpers.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 Point (x1, y1), 3, Orientation.Horizontal, LineStyle.Double);
- lc.AddLine (new Point (x2, y2), 3, Orientation.Horizontal, LineStyle.Double);
- TestHelpers.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│")]
- [AutoInitShutdown]
- [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 Rectangle (0, 0, 10, 10);
- lc.AddLine (new Point (x1, y1), length, o1, s1);
- v.Draw ();
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- /// <summary>This test demonstrates how to correctly trigger a corner. By overlapping the lines in the same cell</summary>
- [Fact]
- [AutoInitShutdown]
- 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 =
- @"
- ┌─
- │";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- /// <summary>
- /// This test demonstrates that corners are only drawn when lines overlap. Not when they terminate adjacent to one
- /// another.
- /// </summary>
- [Fact]
- [AutoInitShutdown]
- public void View_Draws_Corner_NoOverlap ()
- {
- View v = GetCanvas (out LineCanvas canvas);
- canvas.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new Point (0, 1), 2, Orientation.Vertical, LineStyle.Single);
- v.Draw ();
- var looksLike =
- @"
- ──
- │
- │";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- [Theory]
- [AutoInitShutdown]
- 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 =
- @"
- ──";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact]
- [AutoInitShutdown]
- 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 =
- @"
- ══";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- [Theory]
- [AutoInitShutdown]
- 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 =
- @"
- │
- │";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact]
- [AutoInitShutdown]
- 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 =
- @"
- ║
- ║";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact]
- [AutoInitShutdown]
- 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 Point (9, 0), 5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new Point (9, 4), -10, Orientation.Horizontal, LineStyle.Double);
- canvas.AddLine (new Point (0, 4), -5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new Point (5, 0), 5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new Point (0, 2), 10, Orientation.Horizontal, LineStyle.Double);
- v.Draw ();
- var looksLike =
- @"
- ╔════╦═══╗
- ║ ║ ║
- ╠════╬═══╣
- ║ ║ ║
- ╚════╩═══╝";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Theory]
- [AutoInitShutdown]
- [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 Point (9, 0), 5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new Point (9, 4), -10, Orientation.Horizontal, LineStyle.Double);
- canvas.AddLine (new Point (0, 4), -5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new Point (5, 0), 5, Orientation.Vertical, thinStyle);
- canvas.AddLine (new Point (0, 2), 10, Orientation.Horizontal, LineStyle.Double);
- v.Draw ();
- var looksLike =
- @"
- ╒════╤═══╕
- │ │ │
- ╞════╪═══╡
- │ │ │
- ╘════╧═══╛
- ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- /// <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]
- [AutoInitShutdown]
- 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 Point (9, 0), 5, Orientation.Vertical, LineStyle.Single);
- canvas.AddLine (new Point (9, 4), -10, Orientation.Horizontal, LineStyle.Rounded);
- canvas.AddLine (new Point (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 Point (5, 0), 5, Orientation.Vertical, LineStyle.Rounded);
- canvas.AddLine (new Point (0, 2), 10, Orientation.Horizontal, LineStyle.Rounded);
- v.Draw ();
- var looksLike =
- @"
- ╭────┬───╮
- │ │ │
- ├────┼───┤
- │ │ │
- ╰────┴───╯";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (LineStyle.Single)]
- [InlineData (LineStyle.Rounded)]
- 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 Point (9, 0), 5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new Point (9, 4), -10, Orientation.Horizontal, thinStyle);
- canvas.AddLine (new Point (0, 4), -5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new Point (5, 0), 5, Orientation.Vertical, LineStyle.Double);
- canvas.AddLine (new Point (0, 2), 10, Orientation.Horizontal, thinStyle);
- v.Draw ();
- var looksLike =
- @"
- ╓────╥───╖
- ║ ║ ║
- ╟────╫───╢
- ║ ║ ║
- ╙────╨───╜
- ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact]
- [SetupFakeDriver]
- public void Window ()
- {
- var canvas = new LineCanvas ();
- // Frame
- canvas.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new Point (9, 0), 5, Orientation.Vertical, LineStyle.Single);
- canvas.AddLine (new Point (9, 4), -10, Orientation.Horizontal, LineStyle.Single);
- canvas.AddLine (new Point (0, 4), -5, Orientation.Vertical, LineStyle.Single);
- // Cross
- canvas.AddLine (new Point (5, 0), 5, Orientation.Vertical, LineStyle.Single);
- canvas.AddLine (new Point (0, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var looksLike =
- @"
- ┌────┬───┐
- │ │ │
- ├────┼───┤
- │ │ │
- └────┴───┘";
- TestHelpers.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 Point (x, y), 2, Orientation.Horizontal, LineStyle.Double);
- //LHS line down
- lc.AddLine (new Point (x, y), height, Orientation.Vertical, LineStyle.Double);
- //Vertical line before Title, results in a ╡
- lc.AddLine (new Point (x + 1, y), 0, Orientation.Vertical, LineStyle.Single);
- //Vertical line after Title, results in a ╞
- lc.AddLine (new Point (x + 2, y), 0, Orientation.Vertical, LineStyle.Single);
- // remainder of top line
- lc.AddLine (new Point (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
- //RHS line down
- lc.AddLine (new Point (x + width, y), height, Orientation.Vertical, LineStyle.Double);
- var looksLike = @"
- ╔╡╞══╗
- ║ ║";
- TestHelpers.AssertEqual (output, looksLike, $"{Environment.NewLine}{lc}");
- }
- // 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.DrawContentComplete"/> 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 Rectangle (0, 0, 10, 5) };
- Application.Top.Add (v);
- Application.Begin (Application.Top);
- LineCanvas canvasCopy = canvas = new LineCanvas ();
- v.DrawContentComplete += (s, e) =>
- {
- v.Clear (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;
- }
- }
|