123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425 |
- using System.Text;
- 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);
- TestHelpers.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.Viewport);
- }
- [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.Viewport);
- }
- [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.Viewport);
- //LHS line down
- lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new (x, y, 2, 2), lc.Viewport);
- //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.Viewport);
- //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.Viewport);
- // remainder of top line
- lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new (x, y, 4, 2), lc.Viewport);
- //RHS line down
- lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new (x, y, 4, 2), lc.Viewport);
- TestHelpers.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.Viewport);
- //LHS line down
- lc.AddLine (new (x, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new (x, y, 2, 2), lc.Viewport);
- //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.Viewport);
- //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.Viewport);
- // remainder of top line
- lc.AddLine (new (x + 2, y), width - 1, Orientation.Horizontal, LineStyle.Double);
- Assert.Equal (new (x, y, 4, 2), lc.Viewport);
- //RHS line down
- lc.AddLine (new (x + width, y), height, Orientation.Vertical, LineStyle.Double);
- Assert.Equal (new (x, y, 4, 2), lc.Viewport);
- TestHelpers.AssertEqual (
- _output,
- @"
- ╔╡╞╗
- ║ ║",
- $"{Environment.NewLine}{lc}"
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void Canvas_Updates_On_Changes ()
- {
- var lc = new LineCanvas ();
- Assert.Equal (Rectangle.Empty, lc.Viewport);
- lc.AddLine (Point.Empty, 2, Orientation.Horizontal, LineStyle.Double);
- Assert.NotEqual (Rectangle.Empty, lc.Viewport);
- lc.Clear ();
- Assert.Equal (Rectangle.Empty, lc.Viewport);
- }
- [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);
- 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 (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]
- 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]
- [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 =
- @"
- ┏━━━━┳━━━┓
- ┃ ┃ ┃
- ┣━━━━╋━━━┫
- ┃ ┃ ┃
- ┗━━━━┻━━━┛";
- TestHelpers.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 =
- @"
- ┍━━━━┯━━━┑
- │ │ │
- ┝━━━━┿━━━┥
- │ │ │
- ┕━━━━┷━━━┙
- ";
- TestHelpers.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 =
- @"
- ┎────┰───┒
- ┃ ┃ ┃
- ┠────╂───┨
- ┃ ┃ ┃
- ┖────┸───┚
- ";
- TestHelpers.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 =
- @"
- ┌─
- │ ";
- 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 (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.Viewport);
- 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 (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 (x1, y1), 3, Orientation.Horizontal, LineStyle.Double);
- lc.AddLine (new (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│")]
- [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 ();
- TestHelpers.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 =
- @"
- ┌─
- │";
- TestHelpers.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 =
- @"
- ──
- │
- │";
- TestHelpers.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 =
- @"
- ──";
- TestHelpers.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 =
- @"
- ══";
- TestHelpers.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 =
- @"
- │
- │";
- TestHelpers.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 =
- @"
- ║
- ║";
- TestHelpers.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 =
- @"
- ╔════╦═══╗
- ║ ║ ║
- ╠════╬═══╣
- ║ ║ ║
- ╚════╩═══╝";
- TestHelpers.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 =
- @"
- ╒════╤═══╕
- │ │ │
- ╞════╪═══╡
- │ │ │
- ╘════╧═══╛
- ";
- TestHelpers.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 =
- @"
- ╭────┬───╮
- │ │ │
- ├────┼───┤
- │ │ │
- ╰────┴───╯";
- TestHelpers.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 =
- @"
- ╓────╥───╖
- ║ ║ ║
- ╟────╫───╢
- ║ ║ ║
- ╙────╨───╜
- ";
- TestHelpers.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 =
- @"
- ┌────┬───┐
- │ │ │
- ├────┼───┤
- │ │ │
- └────┴───┘";
- 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 (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 = @"
- ╔╡╞══╗
- ║ ║";
- TestHelpers.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.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 (0, 0, 10, 5) };
- LineCanvas canvasCopy = canvas = new ();
- v.DrawContentComplete += (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;
- }
- }
|