mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
TextEdit control & a bunch of other stuff (#3436)
This commit is contained in:
committed by
GitHub
parent
db95c6284b
commit
e34935c9e2
@@ -1,5 +1,7 @@
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Graphics.Clyde;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.Input;
|
||||
@@ -15,11 +17,13 @@ namespace Robust.UnitTesting.Client.UserInterface.Controls
|
||||
public void Setup()
|
||||
{
|
||||
var uiMgr = new Mock<IUserInterfaceManagerInternal>();
|
||||
var clyde = new ClydeHeadless();
|
||||
|
||||
IoCManager.InitThread();
|
||||
IoCManager.Clear();
|
||||
IoCManager.RegisterInstance<IUserInterfaceManagerInternal>(uiMgr.Object);
|
||||
IoCManager.RegisterInstance<IUserInterfaceManager>(uiMgr.Object);
|
||||
IoCManager.RegisterInstance<IClyde>(clyde);
|
||||
IoCManager.BuildGraph();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
using NUnit.Framework;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
|
||||
namespace Robust.UnitTesting.Client.UserInterface.Controls;
|
||||
|
||||
[TestFixture]
|
||||
[TestOf(typeof(TextEditShared))]
|
||||
[Parallelizable]
|
||||
internal sealed class TextEditSharedTest
|
||||
{
|
||||
// @formatter:off
|
||||
[Test]
|
||||
[TestCase("foo bar baz", 0, ExpectedResult = 0)]
|
||||
[TestCase("foo bar baz", 1, ExpectedResult = 0)]
|
||||
[TestCase("foo bar baz", 4, ExpectedResult = 0)]
|
||||
[TestCase("foo bar baz", 5, ExpectedResult = 4)]
|
||||
[TestCase("foo bar baz", 8, ExpectedResult = 4)]
|
||||
[TestCase("foo bar baz", 9, ExpectedResult = 8)]
|
||||
[TestCase("foo +bar baz", 5, ExpectedResult = 4)]
|
||||
[TestCase("foo +bar baz", 4, ExpectedResult = 0)]
|
||||
[TestCase("foo +bar baz", 6, ExpectedResult = 5)]
|
||||
[TestCase("foo +bar baz", 7, ExpectedResult = 5)]
|
||||
[TestCase("Foo Bar Baz", 4, ExpectedResult = 0)]
|
||||
[TestCase("Foo Bar Baz", 11, ExpectedResult = 8)]
|
||||
[TestCase("Foo[Bar[Baz", 3, ExpectedResult = 0)]
|
||||
[TestCase("Foo[Bar[Baz", 4, ExpectedResult = 3)]
|
||||
[TestCase("Foo^Bar^Baz", 3, ExpectedResult = 0)]
|
||||
[TestCase("Foo^Bar^Baz", 5, ExpectedResult = 3)]
|
||||
[TestCase("Foo^^^Bar^Baz", 9, ExpectedResult = 3)]
|
||||
[TestCase("^^^ ^^^", 7, ExpectedResult = 0)]
|
||||
[TestCase("^^^ ^^^", 13, ExpectedResult = 7)]
|
||||
// @formatter:on
|
||||
public int TestPrevWordPosition(string str, int cursor)
|
||||
{
|
||||
// For my sanity.
|
||||
str = str.Replace("^", "👏");
|
||||
|
||||
return TextEditShared.PrevWordPosition(str, cursor);
|
||||
}
|
||||
|
||||
[Test]
|
||||
// @formatter:off
|
||||
[TestCase("foo bar baz", 11, ExpectedResult = 11)]
|
||||
[TestCase("foo bar baz", 0, ExpectedResult = 4 )]
|
||||
[TestCase("foo bar baz", 1, ExpectedResult = 4 )]
|
||||
[TestCase("foo bar baz", 3, ExpectedResult = 4 )]
|
||||
[TestCase("foo bar baz", 4, ExpectedResult = 8 )]
|
||||
[TestCase("foo bar baz", 5, ExpectedResult = 8 )]
|
||||
[TestCase("Foo Bar Baz", 0, ExpectedResult = 4 )]
|
||||
[TestCase("Foo Bar Baz", 8, ExpectedResult = 11)]
|
||||
[TestCase("foo +bar baz", 0, ExpectedResult = 4 )]
|
||||
[TestCase("foo +bar baz", 4, ExpectedResult = 5 )]
|
||||
[TestCase("Foo[Bar[Baz", 0, ExpectedResult = 3 )]
|
||||
[TestCase("Foo[Bar[Baz", 3, ExpectedResult = 4 )]
|
||||
[TestCase("Foo^Bar^Baz", 0, ExpectedResult = 3 )]
|
||||
[TestCase("Foo^Bar^Baz", 3, ExpectedResult = 5 )]
|
||||
[TestCase("Foo^^^Bar^Baz", 3, ExpectedResult = 9 )]
|
||||
[TestCase("^^^ ^^^", 0, ExpectedResult = 7 )]
|
||||
[TestCase("^^^ ^^^", 7, ExpectedResult = 13)]
|
||||
// @formatter:on
|
||||
public int TestNextWordPosition(string str, int cursor)
|
||||
{
|
||||
// For my sanity.
|
||||
str = str.Replace("^", "👏");
|
||||
|
||||
return TextEditShared.NextWordPosition(str, cursor);
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ namespace Robust.UnitTesting
|
||||
{
|
||||
}
|
||||
|
||||
public void TextEntered(TextEventArgs textEvent)
|
||||
public void TextEntered(TextEnteredEventArgs textEnteredEvent)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -71,5 +71,19 @@ namespace Robust.UnitTesting.Shared.Utility
|
||||
var message = FormattedMessage.FromMarkup(text);
|
||||
Assert.That(message.ToMarkup(), NUnit.Framework.Is.EqualTo(text));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCase("Foo")]
|
||||
[TestCase("[color=#FF000000]Foo[/color]")]
|
||||
[TestCase("[color=#00FF00FF]Foo[/color]bar")]
|
||||
[TestCase("honk honk [color=#00FF00FF]Foo[/color]bar")]
|
||||
public static void TestEnumerateRunes(string text)
|
||||
{
|
||||
var message = FormattedMessage.FromMarkup(text);
|
||||
|
||||
Assert.That(
|
||||
message.EnumerateRunes(),
|
||||
Is.EquivalentTo(message.ToString().EnumerateRunes()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
147
Robust.UnitTesting/Shared/Utility/TextRope_Test.cs
Normal file
147
Robust.UnitTesting/Shared/Utility/TextRope_Test.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Robust.UnitTesting.Shared.Utility;
|
||||
|
||||
[TestFixture]
|
||||
[TestOf(typeof(Rope))]
|
||||
[Parallelizable(ParallelScope.All)]
|
||||
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
|
||||
public static class TextRope_Test
|
||||
{
|
||||
[Test]
|
||||
public static void TestCalcWeight()
|
||||
{
|
||||
// Just using the example from Wikipedia:
|
||||
// https://commons.wikimedia.org/wiki/File:Vector_Rope_example.svg
|
||||
|
||||
BuildExample(out var tree);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(tree.NodeN.Weight, Is.EqualTo(6));
|
||||
Assert.That(tree.NodeM.Weight, Is.EqualTo(1));
|
||||
Assert.That(tree.NodeK.Weight, Is.EqualTo(4));
|
||||
Assert.That(tree.NodeJ.Weight, Is.EqualTo(2));
|
||||
Assert.That(tree.NodeF.Weight, Is.EqualTo(3));
|
||||
Assert.That(tree.NodeE.Weight, Is.EqualTo(6));
|
||||
|
||||
Assert.That(tree.NodeH.Weight, Is.EqualTo(1));
|
||||
Assert.That(tree.NodeG.Weight, Is.EqualTo(2));
|
||||
|
||||
Assert.That(tree.NodeC.Weight, Is.EqualTo(6));
|
||||
Assert.That(tree.NodeD.Weight, Is.EqualTo(6));
|
||||
|
||||
Assert.That(tree.NodeB.Weight, Is.EqualTo(9));
|
||||
|
||||
Assert.That(tree.NodeA.Weight, Is.EqualTo(22));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void TestCollect()
|
||||
{
|
||||
var tree = BuildExample(out _);
|
||||
var leaves = Rope.CollectLeaves(tree).Select(x => x.Text).ToArray();
|
||||
|
||||
Assert.That(leaves, Is.EquivalentTo(new[]
|
||||
{
|
||||
"Hello ", "my ", "na", "me i", "s", " Simon"
|
||||
}));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void TestCollectReverse()
|
||||
{
|
||||
var tree = BuildExample(out _);
|
||||
var leaves = Rope.CollectLeavesReverse(tree).Select(x => x.Text).ToArray();
|
||||
|
||||
Assert.That(leaves, Is.EquivalentTo(new[]
|
||||
{
|
||||
"Hello ", "my ", "na", "me i", "s", " Simon"
|
||||
}.Reverse()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void TestCollapse()
|
||||
{
|
||||
var tree = BuildExample(out _);
|
||||
|
||||
Assert.That(Rope.Collapse(tree), Is.EqualTo("Hello my name is Simon"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void TestSplit()
|
||||
{
|
||||
var tree = BuildExample(out _);
|
||||
var (left, right) = Rope.Split(tree, 7);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(Rope.Collapse(left), Is.EqualTo("Hello m"));
|
||||
Assert.That(Rope.Collapse(right), Is.EqualTo("y name is Simon"));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void TestDelete()
|
||||
{
|
||||
var tree = BuildExample(out _);
|
||||
|
||||
tree = Rope.Delete(tree, 2, 11);
|
||||
Assert.That(Rope.Collapse(tree), Is.EqualTo("He is Simon"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void TestEnumerateRunesReverseSub()
|
||||
{
|
||||
var tree = BuildExample(out _);
|
||||
|
||||
var runes = Rope.EnumerateRunesReverse(tree, 10);
|
||||
Assert.That(
|
||||
runes,
|
||||
Is.EquivalentTo("Hello my n".EnumerateRunes().Reverse()));
|
||||
}
|
||||
|
||||
private static Rope.Node BuildExample(out ExampleTree tree)
|
||||
{
|
||||
tree = default;
|
||||
|
||||
tree.NodeN = new Rope.Leaf(" Simon");
|
||||
tree.NodeM = new Rope.Leaf("s");
|
||||
tree.NodeK = new Rope.Leaf("me i");
|
||||
tree.NodeJ = new Rope.Leaf("na");
|
||||
tree.NodeF = new Rope.Leaf("my ");
|
||||
tree.NodeE = new Rope.Leaf("Hello ");
|
||||
|
||||
tree.NodeH = new Rope.Branch(tree.NodeM, tree.NodeN);
|
||||
tree.NodeG = new Rope.Branch(tree.NodeJ, tree.NodeK);
|
||||
|
||||
tree.NodeC = new Rope.Branch(tree.NodeE, tree.NodeF);
|
||||
tree.NodeD = new Rope.Branch(tree.NodeG, tree.NodeH);
|
||||
|
||||
tree.NodeB = new Rope.Branch(tree.NodeC, tree.NodeD);
|
||||
|
||||
tree.NodeA = new Rope.Branch(tree.NodeB, null);
|
||||
|
||||
return tree.NodeA;
|
||||
}
|
||||
|
||||
public struct ExampleTree
|
||||
{
|
||||
public Rope.Node NodeN;
|
||||
public Rope.Node NodeM;
|
||||
public Rope.Node NodeK;
|
||||
public Rope.Node NodeJ;
|
||||
public Rope.Node NodeF;
|
||||
public Rope.Node NodeE;
|
||||
public Rope.Node NodeH;
|
||||
public Rope.Node NodeG;
|
||||
public Rope.Node NodeC;
|
||||
public Rope.Node NodeD;
|
||||
public Rope.Node NodeB;
|
||||
public Rope.Node NodeA;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user