From e15e42dbe8df18bb0edbf6e112e3237a13c22fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=9A=D0=BE=D0=BB=D0=BE=D1=81=D0=BE=D0=B2?= Date: Sun, 10 Mar 2024 01:09:28 +0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Change=20tests.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Controls/HeaderContainer.cs | 4 + src/TUI/Engine/Helper.cs | 2 +- .../Nodes/Attributes/Paddings/IPaddingable.cs | 2 +- .../Nodes/Attributes/Resizing/IResizable.cs | 2 +- .../Nodes/Attributes/Resizing/Resizing.cs | 3 +- .../Engine/Nodes/Components/ComponentBase.cs | 2 +- src/TUI/Engine/Nodes/Components/IComponent.cs | 2 +- src/TUI/Engine/Nodes/Components/Sketch.cs | 2 +- .../Engine/Nodes/Containers/ContainerBase.cs | 6 ++ .../Nodes/Containers/ContainerExtension.cs | 93 ++++++------------- src/TUI/Engine/Nodes/Containers/IContainer.cs | 7 +- src/TUI/Engine/Nodes/NodeBase.cs | 20 +--- .../Engine/Rendering/ComponentCraftsman.cs | 24 ++--- .../Engine/Rendering/ContainerCraftsman.cs | 41 +++++--- src/TUI/Engine/Rendering/IDrawable.cs | 2 +- src/TUI/Engine/Rendering/IntegerExtension.cs | 2 +- src/TUI/Engine/Rendering/NodeCraftsman.cs | 9 +- .../WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs | 61 ++++++------ tests/WIdgets/TUI.Tests/Controls/LogoTests.cs | 2 +- tests/WIdgets/TUI.Tests/IntegerTests.cs | 22 +++++ tests/WIdgets/TUI.Tests/TestComponent.cs | 18 ++++ 21 files changed, 167 insertions(+), 159 deletions(-) create mode 100644 tests/WIdgets/TUI.Tests/IntegerTests.cs create mode 100644 tests/WIdgets/TUI.Tests/TestComponent.cs diff --git a/src/TUI/Components/Controls/HeaderContainer.cs b/src/TUI/Components/Controls/HeaderContainer.cs index 912ce23..0a01794 100644 --- a/src/TUI/Components/Controls/HeaderContainer.cs +++ b/src/TUI/Components/Controls/HeaderContainer.cs @@ -13,15 +13,19 @@ public class HeaderContainer : ContainerBase, IContainer { var versionHints = new VersionHints(); versionHints.SetPadding(Indentation.Default); + versionHints.SetAlignment(Horizontal.Left); var tagsHints = new TagHints(); tagsHints.SetPadding(Indentation.Default); + tagsHints.SetAlignment(Horizontal.Left); var appTypeHints = new AppTypeHints(); appTypeHints.SetPadding(Indentation.Default); + appTypeHints.SetAlignment(Horizontal.Left); var hotkeysHints = new HotkeysHint(); hotkeysHints.SetPadding(Indentation.Default); + hotkeysHints.SetAlignment(Horizontal.Left); var logo = new Logo(); logo.SetAlignment(Horizontal.Right); diff --git a/src/TUI/Engine/Helper.cs b/src/TUI/Engine/Helper.cs index 2ea4241..ab871c0 100644 --- a/src/TUI/Engine/Helper.cs +++ b/src/TUI/Engine/Helper.cs @@ -22,7 +22,7 @@ public static class Helper public static void ShowBackground(Position position, Size size) { - return; + return; var color = Colors.Dequeue(); var top = position.Top; diff --git a/src/TUI/Engine/Nodes/Attributes/Paddings/IPaddingable.cs b/src/TUI/Engine/Nodes/Attributes/Paddings/IPaddingable.cs index 6932bcb..b100181 100644 --- a/src/TUI/Engine/Nodes/Attributes/Paddings/IPaddingable.cs +++ b/src/TUI/Engine/Nodes/Attributes/Paddings/IPaddingable.cs @@ -4,7 +4,7 @@ namespace TUI.Engine.Nodes.Attributes.Paddings; public interface IPaddingable { - Padding? Padding { get; } + Padding Padding { get; } void SetPadding(Level level); void SetPaddingLeft(Level level); diff --git a/src/TUI/Engine/Nodes/Attributes/Resizing/IResizable.cs b/src/TUI/Engine/Nodes/Attributes/Resizing/IResizable.cs index 6c6f2f6..2773ce8 100644 --- a/src/TUI/Engine/Nodes/Attributes/Resizing/IResizable.cs +++ b/src/TUI/Engine/Nodes/Attributes/Resizing/IResizable.cs @@ -9,6 +9,6 @@ public interface IResizable Resizing ResizingVertical { get; } void SetAdaptive(Orientation orientation); - void SetHug(Orientation orientation); void SetFixed(Orientation orientation, int value); + Size GetFixedSize(); } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Attributes/Resizing/Resizing.cs b/src/TUI/Engine/Nodes/Attributes/Resizing/Resizing.cs index db3984d..a213eb1 100644 --- a/src/TUI/Engine/Nodes/Attributes/Resizing/Resizing.cs +++ b/src/TUI/Engine/Nodes/Attributes/Resizing/Resizing.cs @@ -3,6 +3,5 @@ namespace TUI.Engine.Nodes.Attributes.Resizing; public enum Resizing { Adaptive, - Fixed, - Hug, + Fixed } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Components/ComponentBase.cs b/src/TUI/Engine/Nodes/Components/ComponentBase.cs index 7027dbb..862547e 100644 --- a/src/TUI/Engine/Nodes/Components/ComponentBase.cs +++ b/src/TUI/Engine/Nodes/Components/ComponentBase.cs @@ -14,7 +14,7 @@ public abstract class ComponentBase : NodeBase, IComponent public abstract Sketch DrawComponent(); - public Sketch Draw() + public Sketch MakeSketch() { var sketch = DrawComponent(); _sketchSize = sketch.GetSize(); diff --git a/src/TUI/Engine/Nodes/Components/IComponent.cs b/src/TUI/Engine/Nodes/Components/IComponent.cs index 4883d76..a1bf772 100644 --- a/src/TUI/Engine/Nodes/Components/IComponent.cs +++ b/src/TUI/Engine/Nodes/Components/IComponent.cs @@ -5,5 +5,5 @@ namespace TUI.Engine.Nodes.Components; public interface IComponent : INode, IAlignable, IPaddingable { - Sketch Draw(); + Sketch MakeSketch(); } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Components/Sketch.cs b/src/TUI/Engine/Nodes/Components/Sketch.cs index f5c8768..aba0983 100644 --- a/src/TUI/Engine/Nodes/Components/Sketch.cs +++ b/src/TUI/Engine/Nodes/Components/Sketch.cs @@ -10,7 +10,7 @@ public sealed class Sketch : IEnumerable public IEnumerator GetEnumerator() => ContentRows.GetEnumerator(); - public IEnumerable Rows(Size maxSize) => + public IEnumerable Crop(Size maxSize) => ContentRows.Where(row => maxSize.Width >= row.GetWidth()).Take(maxSize.Height).ToArray(); public Size GetSize() diff --git a/src/TUI/Engine/Nodes/Containers/ContainerBase.cs b/src/TUI/Engine/Nodes/Containers/ContainerBase.cs index 60c499f..416a5e9 100644 --- a/src/TUI/Engine/Nodes/Containers/ContainerBase.cs +++ b/src/TUI/Engine/Nodes/Containers/ContainerBase.cs @@ -1,3 +1,4 @@ +using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Orientations; namespace TUI.Engine.Nodes.Containers; @@ -6,5 +7,10 @@ public abstract class ContainerBase : NodeBase, IContainer { public Orientation Orientation => Orientation.Horizontal; + public Size GetSketchSize() + { + throw new NotImplementedException(); + } + public abstract Nodes GetNodes(); } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs b/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs index 513d289..b1ebcb6 100644 --- a/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs +++ b/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs @@ -8,55 +8,17 @@ namespace TUI.Engine.Nodes.Containers; public static class ContainerExtension { - public static Size GetSize(this IContainer container, Resizing resizing) - { - int width = resizing switch - { - // Resizing.Fixed => _fixedWeight, - // Resizing.Hug => GetAllowableSize().Width, - // Resizing.Adaptive => GetAllowableSize().Width, - _ => 0 - }; - - int height = resizing switch - { - // Resizing.Fixed => _fixedHeight, - // Resizing.Hug => GetAllowableSize().Height, - // Resizing.Adaptive => GetAllowableSize().Height, - _ => 0 - }; - - return new Size(width, height); - } - - public static Position GetNextNodePosition(this IContainer container, Orientation orientation, Size allowableSize, - Position containerPosition) - { - var nodeSize = container.GetSize(allowableSize); - - return GetNextNodePosition(orientation, nodeSize, containerPosition); - } - - private static Position GetNextNodePosition(Orientation orientation, Size nodeSize, Position nodePosition) => - orientation switch - { - Orientation.Horizontal => nodePosition with - { - Left = nodePosition.Left + nodeSize.Width - }, - Orientation.Vertical => nodePosition with - { - Top = nodePosition.Top + nodeSize.Height - }, - _ => throw new ArgumentOutOfRangeException() - }; - public static Size GetSize(this IContainer container, Size allowableSize) { var nodeCount = container.GetNodes().Count; - var width = container.Orientation == Orientation.Horizontal - ? allowableSize.Width / nodeCount - : allowableSize.Width; + var width = container.ResizingHorizontal switch + { + Resizing.Adaptive => container.Orientation == Orientation.Horizontal + ? allowableSize.Width / nodeCount + : allowableSize.Width, + Resizing.Fixed => container.GetFixedSize().Width, + _ => throw new ArgumentOutOfRangeException() + }; var height = container.Orientation == Orientation.Vertical ? allowableSize.Height / nodeCount : allowableSize.Height; @@ -67,33 +29,32 @@ public static class ContainerExtension public static class ComponentExtensions { - public static Position GetPosition(this IComponent component, Position sketchPosition, Size allowableSize, - Size actualSize) + public static Position CorrectPosition(this IComponent component, Position pencil, Size maxSize, Size sketchSize) { - var left = sketchPosition.Left + (int)(component.Padding?.Left ?? 0) + - CompensationLeft(component.Alignment.Horizontal, allowableSize, actualSize); - var top = sketchPosition.Top + (int)(component.Padding?.Top ?? 0) + - CompensationTop(component.Alignment.Vertical, allowableSize, actualSize); + var padding = component.Padding; + var alignment = component.Alignment; + var alignmentCompensationLeft = GetAlignmentCompensationLeft(alignment.Horizontal, maxSize, sketchSize); + var alignmentCompensationTop = GetAlignmentCompensationTop(alignment.Vertical, maxSize, sketchSize); + var left = pencil.Left + (int)padding.Left + alignmentCompensationLeft; + var top = pencil.Top + (int)padding.Top + alignmentCompensationTop; return new Position(left, top); } - private static int CompensationLeft(Horizontal componentHorizontal, Size defaultSize, - Size realSize) => - componentHorizontal switch + private static int GetAlignmentCompensationLeft(Horizontal alignment, Size maxSize, Size sketchSize) => + alignment switch { Horizontal.Left => 0, - Horizontal.Center => (defaultSize.Width - realSize.Width) / 2, - Horizontal.Right => defaultSize.Width - realSize.Width, + Horizontal.Center => (maxSize.Width - sketchSize.Width) / 2, + Horizontal.Right => maxSize.Width - sketchSize.Width, _ => 0 }; - private static int CompensationTop(Vertical componentVertical, Size defaultSize, Size realSize) - => - componentVertical switch - { - Vertical.Top => 0, - Vertical.Center => (defaultSize.Height - realSize.Height) / 2, - Vertical.Bottom => defaultSize.Height - realSize.Height, - _ => 0 - }; + private static int GetAlignmentCompensationTop(Vertical alignment, Size maxSize, Size sketchSize) => + alignment switch + { + Vertical.Top => 0, + Vertical.Center => (maxSize.Height - sketchSize.Height) / 2, + Vertical.Bottom => maxSize.Height - sketchSize.Height, + _ => 0 + }; } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Containers/IContainer.cs b/src/TUI/Engine/Nodes/Containers/IContainer.cs index 4c37659..85c2500 100644 --- a/src/TUI/Engine/Nodes/Containers/IContainer.cs +++ b/src/TUI/Engine/Nodes/Containers/IContainer.cs @@ -1,9 +1,10 @@ +using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Orientations; namespace TUI.Engine.Nodes.Containers; -public interface IContainer : INode, - IWithOrientation +public interface IContainer : INode, IWithOrientation { - public Nodes GetNodes(); + Size GetSketchSize(); + Nodes GetNodes(); } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/NodeBase.cs b/src/TUI/Engine/Nodes/NodeBase.cs index 021c06a..ec121ef 100644 --- a/src/TUI/Engine/Nodes/NodeBase.cs +++ b/src/TUI/Engine/Nodes/NodeBase.cs @@ -1,3 +1,4 @@ +using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Orientations; using TUI.Engine.Nodes.Attributes.Resizing; @@ -8,10 +9,12 @@ public abstract class NodeBase : INode private int _fixedWidth; private int _fixedHeight; + public Size GetFixedSize() => new(_fixedWidth, _fixedHeight); + #region Resizing public Resizing ResizingHorizontal { get; private set; } = Resizing.Adaptive; - public Resizing ResizingVertical { get; private set; } = Resizing.Hug; + public Resizing ResizingVertical { get; private set; } = Resizing.Adaptive; public void SetAdaptive(Orientation orientation) { @@ -28,21 +31,6 @@ public abstract class NodeBase : INode } } - public void SetHug(Orientation orientation) - { - switch (orientation) - { - case Orientation.Horizontal: - ResizingHorizontal = Resizing.Hug; - break; - case Orientation.Vertical: - ResizingVertical = Resizing.Hug; - break; - default: - throw new ArgumentOutOfRangeException(nameof(orientation), orientation, null); - } - } - public void SetFixed(Orientation orientation, int value) { switch (orientation) diff --git a/src/TUI/Engine/Rendering/ComponentCraftsman.cs b/src/TUI/Engine/Rendering/ComponentCraftsman.cs index 577eed5..8182efe 100644 --- a/src/TUI/Engine/Rendering/ComponentCraftsman.cs +++ b/src/TUI/Engine/Rendering/ComponentCraftsman.cs @@ -14,23 +14,23 @@ public sealed class ComponentCraftsman : CraftsmanBase, IDrawable _canvas = canvas; } - public Size Draw(IComponent component, Position sketchPosition, Size allowableSize) + public Size Draw(IComponent component, Position pencil, Size maxSize) { - var sketch = component.Draw(); - var actualSize = sketch.GetSize(); - var maxSize = _canvas.GetSize() - sketchPosition; - var pencilPosition = component.GetPosition(sketchPosition, allowableSize, actualSize); + var sketch = component.MakeSketch(); + var sketchSize = sketch.GetSize(); - foreach (var row in sketch.Rows(maxSize)) + var correctedPencil = component.CorrectPosition(pencil, maxSize, sketchSize); + + Debug(correctedPencil, pencil, maxSize); + + foreach (var line in sketch.Crop(maxSize)) { - _canvas.SetPencil(pencilPosition.Left, pencilPosition.Top); - _canvas.Paint(row); + _canvas.SetPencil(correctedPencil.Left, correctedPencil.Top); + _canvas.Paint(line); - pencilPosition = pencilPosition with { Top = pencilPosition.Top + 1 }; + correctedPencil = correctedPencil with { Top = correctedPencil.Top + 1 }; } - Debug(pencilPosition, sketchPosition, allowableSize); - - return actualSize; + return sketchSize; } } \ No newline at end of file diff --git a/src/TUI/Engine/Rendering/ContainerCraftsman.cs b/src/TUI/Engine/Rendering/ContainerCraftsman.cs index 2a70802..cad67f9 100644 --- a/src/TUI/Engine/Rendering/ContainerCraftsman.cs +++ b/src/TUI/Engine/Rendering/ContainerCraftsman.cs @@ -1,6 +1,7 @@ using TUI.Engine.Nodes; using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Orientations; +using TUI.Engine.Nodes.Attributes.Resizing; using TUI.Engine.Nodes.Components; using TUI.Engine.Nodes.Containers; @@ -15,52 +16,64 @@ public sealed class ContainerCraftsman : CraftsmanBase, IDrawable _componentCraftsman = componentCraftsman; } - - public Size Draw(IContainer container, Position sketchPosition, Size allowableSize) + public Size Draw(IContainer container, Position pencil, Size maxSize) { - var sketchSize = container.GetSize(allowableSize); var controlNumber = 0; + var nextNodePosition = pencil; var nodes = container.GetNodes(); + var sketchSize = container.GetSize(maxSize); + Debug(nextNodePosition, nextNodePosition, maxSize); while (controlNumber < nodes.Count) { var node = nodes[controlNumber]; - sketchPosition = DrawNode(node, container.Orientation, sketchPosition, sketchSize); + nextNodePosition = DrawNode(node, container, nextNodePosition, sketchSize); controlNumber++; } - Debug(sketchPosition, sketchPosition, allowableSize); return sketchSize; } - private Position DrawNode(INode node, Orientation orientation, Position sketchPosition, Size sketchSize) + private Position DrawNode(INode node, IContainer container, Position nodePosition, Size maxSize) { switch (node) { case IContainer childContainer: - Draw(childContainer, sketchPosition, sketchSize); - return GetNextNodePosition(orientation, sketchSize, sketchPosition); + var containerSize = Draw(childContainer, nodePosition, maxSize); + return GetNextNodePosition(container, containerSize, nodePosition); case IComponent childComponent: - var componentSize = _componentCraftsman.Draw(childComponent, sketchPosition, sketchSize); - return GetNextNodePosition(orientation, sketchSize, sketchPosition, componentSize); + var componentSize = _componentCraftsman.Draw(childComponent, nodePosition, maxSize); + return GetNextNodePosition(container, maxSize, nodePosition, componentSize); default: throw new InvalidCastException(); } } private static Position GetNextNodePosition( - Orientation orientation, + IContainer container, Size defaultSize, Position position, Size? componentSize = null) { - switch (orientation) + switch (container.Orientation) { case Orientation.Horizontal: - var componentWidth = componentSize?.Width.Max(defaultSize.Width) ?? defaultSize.Width; + var componentWidth = container.ResizingHorizontal switch + { + Resizing.Adaptive => defaultSize.Width, + Resizing.Fixed => componentSize?.Width.Max(container.GetFixedSize().Width) ?? + container.GetFixedSize().Width, + _ => 0 + }; return position with { Left = position.Left + componentWidth }; case Orientation.Vertical: - var componentHeight = componentSize?.Height.Max(defaultSize.Height) ?? defaultSize.Height; + var componentHeight = container.ResizingVertical switch + { + Resizing.Adaptive => defaultSize.Height, + Resizing.Fixed => componentSize?.Height.Max(container.GetFixedSize().Height) ?? + container.GetFixedSize().Height, + _ => 0 + }; return position with { Top = position.Top + componentHeight }; default: throw new InvalidCastException(); diff --git a/src/TUI/Engine/Rendering/IDrawable.cs b/src/TUI/Engine/Rendering/IDrawable.cs index a196386..05ae2bc 100644 --- a/src/TUI/Engine/Rendering/IDrawable.cs +++ b/src/TUI/Engine/Rendering/IDrawable.cs @@ -5,5 +5,5 @@ namespace TUI.Engine.Rendering; public interface IDrawable where TItem : INode { - Size Draw(TItem item, Position sketchPosition, Size allowableSize); + Size Draw(TItem item, Position pencil, Size maxSize); } \ No newline at end of file diff --git a/src/TUI/Engine/Rendering/IntegerExtension.cs b/src/TUI/Engine/Rendering/IntegerExtension.cs index af53b8b..d31959d 100644 --- a/src/TUI/Engine/Rendering/IntegerExtension.cs +++ b/src/TUI/Engine/Rendering/IntegerExtension.cs @@ -2,5 +2,5 @@ namespace TUI.Engine.Rendering; public static class IntegerExtension { - public static int Max(this int value, int maxValue) => maxValue <= value ? value : maxValue; + public static int Max(this int value, int maxValue) => value <= maxValue ? value : maxValue; } \ No newline at end of file diff --git a/src/TUI/Engine/Rendering/NodeCraftsman.cs b/src/TUI/Engine/Rendering/NodeCraftsman.cs index a73c939..ec62349 100644 --- a/src/TUI/Engine/Rendering/NodeCraftsman.cs +++ b/src/TUI/Engine/Rendering/NodeCraftsman.cs @@ -5,6 +5,9 @@ using TUI.Engine.Nodes.Containers; namespace TUI.Engine.Rendering; +/// +/// 🍀 +/// public sealed class NodeCraftsman : IDrawable { private readonly IDrawable _componentCraftsman; @@ -18,11 +21,11 @@ public sealed class NodeCraftsman : IDrawable _containerCraftsman = containerCraftsman; } - public Size Draw(INode node, Position sketchPosition, Size allowableSize) => + public Size Draw(INode node, Position pencil, Size maxSize) => node switch { - IContainer container => _containerCraftsman.Draw(container, sketchPosition, allowableSize), - IComponent component => _componentCraftsman.Draw(component, sketchPosition, allowableSize), + IContainer container => _containerCraftsman.Draw(container, pencil, maxSize), + IComponent component => _componentCraftsman.Draw(component, pencil, maxSize), _ => throw new InvalidCastException("Unknown node type.") }; } \ No newline at end of file diff --git a/tests/WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs b/tests/WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs index 127665c..e5a2bfc 100644 --- a/tests/WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs +++ b/tests/WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs @@ -1,9 +1,9 @@ using Moq; using TUI.Engine.Nodes; +using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Alignments; using TUI.Engine.Nodes.Attributes.Orientations; using TUI.Engine.Nodes.Attributes.Resizing; -using TUI.Engine.Nodes.Components; using TUI.Engine.Nodes.Containers; using TUI.Engine.Rendering; @@ -11,25 +11,22 @@ namespace Widgets.Tests; public class NodeCraftsmanTests { - private readonly IComponent _component; + private readonly TestComponent _component; public NodeCraftsmanTests() { - _component = Mock.Of(c => - c.Draw() == new Sketch("Lorem") && - c.Alignment == new Alignment(Horizontal.Left, Vertical.Top)); + _component = new TestComponent(); + _component.SetAlignment(Horizontal.Left); + _component.SetAlignment(Vertical.Top); } [Fact] public void DrawSimple() { var canvas = Mock.Of(w => w.Width == 9 && w.Height == 1); - var nodes = new Nodes { _component }; - var root = Mock.Of(r => r.GetNodes() == nodes); var componentCraftsman = new ComponentCraftsman(canvas); - var containerCraftsman = new ContainerCraftsman(componentCraftsman); - new NodeCraftsman(componentCraftsman, containerCraftsman).Draw(root, Position.Default, canvas.GetSize()); + componentCraftsman.Draw(_component, Position.Default, canvas.GetSize()); Mock.Get(canvas).Verify(w => w.SetPencil(0, 0), Times.Once()); Mock.Get(canvas).Verify(w => w.Paint("Lorem"), Times.Once()); @@ -47,14 +44,12 @@ public class NodeCraftsmanTests int expectedPosition) { var canvas = Mock.Of(w => w.Width == canvasSize && w.Height == canvasSize); - var component = Mock.Of(c => c.Draw() == new Sketch(content) && - c.Alignment == new Alignment(alignment, Vertical.Top)); - var nodes = new Nodes { component }; - var root = Mock.Of(r => r.GetNodes() == nodes); + _component.SetContent(content); + _component.SetAlignment(Vertical.Top); + _component.SetAlignment(alignment); var componentCraftsman = new ComponentCraftsman(canvas); - var containerCraftsman = new ContainerCraftsman(componentCraftsman); - new NodeCraftsman(componentCraftsman, containerCraftsman).Draw(root, Position.Default, canvas.GetSize()); + componentCraftsman.Draw(_component, Position.Default, canvas.GetSize()); Mock.Get(canvas).Verify(w => w.Paint(content), Times.Once()); Mock.Get(canvas).Verify(w => w.SetPencil(expectedPosition, 0), Times.Once()); @@ -78,14 +73,12 @@ public class NodeCraftsmanTests public void DrawWithVerticalAlignment(Vertical alignment, string content, int canvasSize, int[] expectedPositions) { var canvas = Mock.Of(w => w.Width == canvasSize && w.Height == canvasSize); - var component = Mock.Of(c => c.Draw() == new Sketch(content) && - c.Alignment == new Alignment(Horizontal.Left, alignment)); - var nodes = new Nodes { component }; - var root = Mock.Of(r => r.GetNodes() == nodes); + _component.SetContent(content); + _component.SetAlignment(Horizontal.Left); + _component.SetAlignment(alignment); var componentCraftsman = new ComponentCraftsman(canvas); - var containerCraftsman = new ContainerCraftsman(componentCraftsman); - new NodeCraftsman(componentCraftsman, containerCraftsman).Draw(root, Position.Default, canvas.GetSize()); + componentCraftsman.Draw(_component, Position.Default, canvas.GetSize()); foreach (var expectedCursorPosition in expectedPositions) { @@ -107,14 +100,12 @@ public class NodeCraftsmanTests int expectedTop) { var canvas = Mock.Of(w => w.Width == 6 && w.Height == 5); - var component = Mock.Of(c => c.Draw() == new Sketch("VV") && - c.Alignment == new Alignment(horizontal, vertical)); - var nodes = new Nodes { component }; - var root = Mock.Of(r => r.GetNodes() == nodes); + _component.SetContent("VV"); + _component.SetAlignment(horizontal); + _component.SetAlignment(vertical); var componentCraftsman = new ComponentCraftsman(canvas); - var containerCraftsman = new ContainerCraftsman(componentCraftsman); - new NodeCraftsman(componentCraftsman, containerCraftsman).Draw(root, Position.Default, canvas.GetSize()); + componentCraftsman.Draw(_component, Position.Default, canvas.GetSize()); Mock.Get(canvas).Verify(w => w.SetPencil(expectedLeft, expectedTop), Times.Once()); } @@ -130,7 +121,7 @@ public class NodeCraftsmanTests var componentCraftsman = new ComponentCraftsman(canvas); var containerCraftsman = new ContainerCraftsman(componentCraftsman); - new NodeCraftsman(componentCraftsman, containerCraftsman).Draw(root, Position.Default, canvas.GetSize()); + containerCraftsman.Draw(root, Position.Default, canvas.GetSize()); Mock.Get(canvas).Verify(w => w.SetPencil(0, 0), Times.Once()); Mock.Get(canvas).Verify(w => w.Paint("Lorem"), Times.Once()); @@ -177,7 +168,7 @@ public class NodeCraftsmanTests var componentCraftsman = new ComponentCraftsman(canvas); var containerCraftsman = new ContainerCraftsman(componentCraftsman); - new NodeCraftsman(componentCraftsman, containerCraftsman).Draw(root, Position.Default, canvas.GetSize()); + containerCraftsman.Draw(root, Position.Default, canvas.GetSize()); Mock.Get(canvas).Verify(w => w.SetPencil(0, 0), Times.Exactly(1)); Mock.Get(canvas).Verify(w => w.SetPencil(6, 0), Times.Exactly(1)); @@ -196,7 +187,7 @@ public class NodeCraftsmanTests var componentCraftsman = new ComponentCraftsman(canvas); var containerCraftsman = new ContainerCraftsman(componentCraftsman); - new NodeCraftsman(componentCraftsman, containerCraftsman).Draw(root, Position.Default, canvas.GetSize()); + containerCraftsman.Draw(root, Position.Default, canvas.GetSize()); Mock.Get(canvas).Verify(w => w.SetPencil(0, 0), Times.Exactly(1)); Mock.Get(canvas).Verify(w => w.SetPencil(0, 1), Times.Exactly(1)); @@ -204,20 +195,22 @@ public class NodeCraftsmanTests } [Theory] - [InlineData(Resizing.Hug, 5)] + [InlineData(Resizing.Fixed, 6)] [InlineData(Resizing.Fixed, 3)] [InlineData(Resizing.Adaptive, 10)] - public void DrawWithResizeContainer(Resizing resizing, int expectedCursorPosition) + public void DrawResizingContainer(Resizing resizing, int expectedCursorPosition) { var canvas = Mock.Of(w => w.Width == 20 && w.Height == 2); var container = - Mock.Of(c => c.GetNodes() == new Nodes { _component } && c.ResizingHorizontal == resizing); + Mock.Of(c => + c.GetNodes() == new Nodes { _component } && c.ResizingHorizontal == resizing && + c.GetFixedSize() == new Size(6, 2)); var nodes = new Nodes { container, _component }; var root = Mock.Of(r => r.GetNodes() == nodes && r.Orientation == Orientation.Horizontal); var componentCraftsman = new ComponentCraftsman(canvas); var containerCraftsman = new ContainerCraftsman(componentCraftsman); - new NodeCraftsman(componentCraftsman, containerCraftsman).Draw(root, Position.Default, canvas.GetSize()); + containerCraftsman.Draw(root, Position.Default, canvas.GetSize()); Mock.Get(canvas).Verify(w => w.SetPencil(0, 0), Times.Exactly(1)); Mock.Get(canvas).Verify(w => w.SetPencil(expectedCursorPosition, 0), Times.Exactly(1)); diff --git a/tests/WIdgets/TUI.Tests/Controls/LogoTests.cs b/tests/WIdgets/TUI.Tests/Controls/LogoTests.cs index 306b9ac..9455f0f 100644 --- a/tests/WIdgets/TUI.Tests/Controls/LogoTests.cs +++ b/tests/WIdgets/TUI.Tests/Controls/LogoTests.cs @@ -9,7 +9,7 @@ public class LogoTests { var logo = new Logo(); - var render = logo.Draw().ToString(); + var render = logo.MakeSketch().ToString(); Assert.Equal( " \u001b[38;2;132;186;100m\u256d\u2501\u2501\u2501\u2501\u2533\u256e\u001b[0m\u001b[38;2;113;121;126m\u2571\u2571\u001b[0m\u001b[38;2;132;186;100m\u256d\u2501\u2501\u2501\u256e\u001b[0m\n \u001b[38;2;132;186;100m\u2503\u256d\u256e\u256d\u256e\u2503\u2503\u001b[0m\u001b[38;2;113;121;126m\u2571\u2571\u001b[0m\u001b[38;2;132;186;100m\u2570\u256e\u256d\u256e\u2503\u001b[0m\n \u001b[38;2;132;186;100m\u2570\u256f\u2503\u2503\u2570\u252b\u2503\u001b[0m\u001b[38;2;113;121;126m\u2571\u2571\u2571\u001b[0m\u001b[38;2;132;186;100m\u2503\u2503\u2503\u2503\u001b[0m\n \u001b[38;2;113;121;126m\u2571\u2571\u001b[0m\u001b[38;2;132;186;100m\u2503\u2503\u001b[0m\u001b[38;2;113;121;126m\u2571\u001b[0m\u001b[38;2;132;186;100m\u2503\u2503\u001b[0m\u001b[38;2;113;121;126m\u2571\u001b[0m\u001b[38;2;132;186;100m\u256d\u256e\u2503\u2503\u2503\u2503\u001b[0m\n \u001b[38;2;113;121;126m\u2571\u2571\u2571\u001b[0m\u001b[38;2;132;186;100m\u2503\u2503\u001b[0m\u001b[38;2;113;121;126m\u2571\u001b[0m\u001b[38;2;132;186;100m\u2503\u2570\u2501\u256f\u2523\u256f\u2570\u256f\u2503\u001b[0m\n\u001b[38;2;113;121;126m\u2571\u2571\u2571\u2571\u001b[0m\u001b[38;2;132;186;100m\u2570\u256f\u001b[0m\u001b[38;2;113;121;126m\u2571\u001b[0m\u001b[38;2;132;186;100m\u2570\u2501\u2501\u2501\u253b\u2501\u2501\u2501\u256f\u001b[0m\n", diff --git a/tests/WIdgets/TUI.Tests/IntegerTests.cs b/tests/WIdgets/TUI.Tests/IntegerTests.cs new file mode 100644 index 0000000..71e9e7e --- /dev/null +++ b/tests/WIdgets/TUI.Tests/IntegerTests.cs @@ -0,0 +1,22 @@ +using FluentAssertions; +using TUI.Engine.Rendering; + +namespace Widgets.Tests; + +public class IntegerTests +{ + [Fact] + public void IntegerGreaterMax() + { + var result = 5.Max(10); + + result.Should().Be(5); + } + [Fact] + public void IntegerLessMax() + { + var result = 5.Max(3); + + result.Should().Be(3); + } +} \ No newline at end of file diff --git a/tests/WIdgets/TUI.Tests/TestComponent.cs b/tests/WIdgets/TUI.Tests/TestComponent.cs new file mode 100644 index 0000000..d018319 --- /dev/null +++ b/tests/WIdgets/TUI.Tests/TestComponent.cs @@ -0,0 +1,18 @@ +using TUI.Engine.Nodes.Components; + +namespace Widgets.Tests; + +internal class TestComponent : ComponentBase +{ + private string _content = "Lorem"; + + public void SetContent(string content) + { + _content = content; + } + + public override Sketch DrawComponent() + { + return new Sketch(_content); + } +} \ No newline at end of file