fix: Adds ISpanParsable and fixes command conditionals (#1241)

* Adds `ISpanParsable<T>`
* Removes `[Parsable]`
* Fixes querying by serial, body, and a few others.
* Adds `Parse` to `Rectangle3D`
* Fixes AutoArchive NPE


Closes #1209
This commit is contained in:
Kamron Batman 2022-11-12 00:26:02 -08:00 • committed by GitHub
parent d494a9c78c
commit 03fb36c869
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 1119 additions and 271 deletions

View file

@ -65,4 +65,44 @@ public class Point2DTests
Assert.False(p4.TryFormat(array, out var cp4, null, null));
Assert.Equal(0, cp4);
}
[Fact]
public void TestPoint2DTryParse()
{
// Happy Path
Assert.True(Point2D.TryParse("(101, 23)", null, out var p));
Assert.Equal(new Point2D(101, 23), p);
Assert.Equal(new Point2D(101, 23), Point2D.Parse("(101, 23)", null));
// Trimming
Assert.True(Point2D.TryParse(" (101,23) ", null, out p));
Assert.Equal(new Point2D(101, 23), p);
Assert.Equal(new Point2D(101, 23), Point2D.Parse(" (101,23) ", null));
// No parenthesis
Assert.False(Point2D.TryParse("101, 23)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point2D.Parse("101, 23)", null));
Assert.False(Point2D.TryParse("(101, 23", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point2D.Parse("(101, 23", null));
// No numbers
Assert.False(Point2D.TryParse("()", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point2D.Parse("()", null));
Assert.False(Point2D.TryParse("(101)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point2D.Parse("(101)", null));
Assert.False(Point2D.TryParse("(101,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point2D.Parse("(101,)", null));
Assert.False(Point2D.TryParse("(,23)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point2D.Parse("(,23)", null));
}
}

View file

@ -69,4 +69,60 @@ public class Point3DTests
Assert.False(p4.TryFormat(array, out var cp4, null, null));
Assert.Equal(0, cp4);
}
[Fact]
public void TestPoint3DTryParse()
{
// Happy Path
Assert.True(Point3D.TryParse("(101, 23, 55)", null, out var p));
Assert.Equal(new Point3D(101, 23, 55), p);
Assert.Equal(new Point3D(101, 23, 55), Point3D.Parse("(101, 23, 55)", null));
// Trimming
Assert.True(Point3D.TryParse(" (101,23 ,55) ", null, out p));
Assert.Equal(new Point3D(101, 23, 55), p);
Assert.Equal(new Point3D(101, 23, 55), Point3D.Parse(" (101,23 ,55) ", null));
// No parenthesis
Assert.False(Point3D.TryParse("101, 23 55)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("101, 23, 55)", null));
Assert.False(Point3D.TryParse("(101, 23, 55", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("(101, 23, 55", null));
// No numbers
Assert.False(Point3D.TryParse("()", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("()", null));
Assert.False(Point3D.TryParse("(,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("(,)", null));
Assert.False(Point3D.TryParse("(|)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("(|)", null));
Assert.False(Point3D.TryParse("(101)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("(101)", null));
Assert.False(Point3D.TryParse("(101,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("(101,)", null));
Assert.False(Point3D.TryParse("(,23)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("(,23)", null));
Assert.False(Point3D.TryParse("(,23,55)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("(,23,55)", null));
Assert.False(Point3D.TryParse("(,23,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Point3D.Parse("(,23,)", null));
}
}

View file

@ -69,4 +69,64 @@ public class Rectangle2DTests
Assert.False(p4.TryFormat(array, out var cp4, null, null));
Assert.Equal(0, cp4);
}
[Fact]
public void TestRectangle2DTryParse()
{
// Happy Path
Assert.True(Rectangle2D.TryParse("(101, 23)+(55, 89)", null, out var p));
Assert.Equal(new Rectangle2D(new Point2D(101, 23), new Point2D(55, 89)), p);
Assert.Equal(new Rectangle2D(new Point2D(101, 23), new Point2D(55, 89)), Rectangle2D.Parse("(101, 23)+(55, 89)", null));
// Trimming
Assert.True(Rectangle2D.TryParse(" (101,23)+ (55, 89) ", null, out p));
Assert.Equal(new Rectangle2D(new Point2D(101, 23), new Point2D(55, 89)), p);
Assert.Equal(new Rectangle2D(new Point2D(101, 23), new Point2D(55, 89)), Rectangle2D.Parse(" (101,23)+ (55, 89) ", null));
// No parenthesis
Assert.False(Rectangle2D.TryParse("101, 23)+( 55, 89)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("101, 23)+( 55, 89)", null));
Assert.False(Rectangle2D.TryParse("(101, 23)+(55, 89", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(101, 23)+(55, 89", null));
// No numbers
Assert.False(Rectangle2D.TryParse("()", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("()", null));
Assert.False(Rectangle2D.TryParse("(,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(,)", null));
Assert.False(Rectangle2D.TryParse("(|)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(|)", null));
Assert.False(Rectangle2D.TryParse("(101)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(101)", null));
Assert.False(Rectangle2D.TryParse("(101,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(101,)", null));
Assert.False(Rectangle2D.TryParse("(,23)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(,23)", null));
Assert.False(Rectangle2D.TryParse("(,23)+(,55)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(,23)+(,55)", null));
Assert.False(Rectangle2D.TryParse("(,23,)+(,89)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(,23,)+(,89)", null));
Assert.False(Rectangle2D.TryParse("(,,)+(55,89)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle2D.Parse("(,,)+(55,89)", null));
}
}

View file

@ -69,4 +69,72 @@ public class Rectangle3DTests
Assert.False(p4.TryFormat(array, out var cp4, null, null));
Assert.Equal(0, cp4);
}
[Fact]
public void TestRectangle3DTryParse()
{
// Happy Path
Assert.True(Rectangle3D.TryParse("(101, 23, 10)+(55, 89, 1)", null, out var p));
Assert.Equal(new Rectangle3D(new Point3D(101, 23, 10), new Point3D(55, 89, 1)), p);
Assert.Equal(new Rectangle3D(new Point3D(101, 23, 10), new Point3D(55, 89, 1)), Rectangle3D.Parse("(101, 23, 10)+(55, 89, 1)", null));
// Trimming
Assert.True(Rectangle3D.TryParse(" (101,23,10)+ (55, 89,1) ", null, out p));
Assert.Equal(new Rectangle3D(new Point3D(101, 23, 10), new Point3D(55, 89, 1)), p);
Assert.Equal(new Rectangle3D(new Point3D(101, 23, 10), new Point3D(55, 89, 1)), Rectangle3D.Parse(" (101,23,10)+ (55, 89,1) ", null));
// No parenthesis
Assert.False(Rectangle3D.TryParse("101, 23, 10)+( 55, 89, 1)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("101, 23, 10)+( 55, 89, 1)", null));
Assert.False(Rectangle3D.TryParse("(101, 23, 10)+(55, 89, 1", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(101, 23, 10)+(55, 89, 1", null));
// No numbers
Assert.False(Rectangle3D.TryParse("()", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("()", null));
Assert.False(Rectangle3D.TryParse("(,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(,)", null));
Assert.False(Rectangle3D.TryParse("(|)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(|)", null));
Assert.False(Rectangle3D.TryParse("(101)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(101)", null));
Assert.False(Rectangle3D.TryParse("(101,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(101,)", null));
Assert.False(Rectangle3D.TryParse("(101,23)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(101,23)", null));
Assert.False(Rectangle3D.TryParse("(,23)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(,23)", null));
Assert.False(Rectangle3D.TryParse("(,23)+(,55)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(,23)+(,55)", null));
Assert.False(Rectangle3D.TryParse("(,23,)+(,89)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(,23,)+(,89)", null));
Assert.False(Rectangle3D.TryParse("(101,,10)+(55, 89 1)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(,23,)+(,89)", null));
Assert.False(Rectangle3D.TryParse("(,,)+(55,89, 1)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => Rectangle3D.Parse("(,,)+(55,89, 1)", null));
}
}

View file

@ -3,7 +3,7 @@ using Xunit;
namespace Server.Tests;
public sealed class WorldLocationTests
public sealed class WorldLocationTests : IClassFixture<ServerFixture>
{
private static Map CreateMap(string name) => new(0, 0, 0, 1, 1, 0, name, MapRules.Internal);
@ -102,4 +102,84 @@ public sealed class WorldLocationTests
Assert.False(p4.TryFormat(array, out var cp4, null, null));
Assert.Equal(0, cp4);
}
[Fact]
public void TestWorldLocationTryParse()
{
var validMap = Map.Felucca;
// Happy Path
Assert.True(WorldLocation.TryParse("(101, 23, 55) [Felucca]", null, out var p));
Assert.Equal(new WorldLocation(101, 23, 55, validMap), p);
Assert.Equal(new WorldLocation(101, 23, 55, validMap), WorldLocation.Parse("(101, 23, 55) [Felucca]", null));
// Trimming
Assert.True(WorldLocation.TryParse(" (101,23 ,55) [Felucca] ", null, out p));
Assert.Equal(new WorldLocation(101, 23, 55, validMap), p);
Assert.Equal(new WorldLocation(101, 23, 55, validMap), WorldLocation.Parse(" (101,23 ,55) [Felucca] ", null));
// Null Map
Assert.True(WorldLocation.TryParse(" (101,23 ,55) [(-null-)]", null, out p));
Assert.Equal(new WorldLocation(101, 23, 55, null), p);
Assert.Equal(new WorldLocation(101, 23, 55, null), WorldLocation.Parse(" (101,23 ,55) [(-null-)]", null));
// No Brackets
Assert.False(WorldLocation.TryParse("(101, 23 55) Felucca]", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(101, 23, 55) Felucca]", null));
Assert.False(WorldLocation.TryParse("(101, 23 55) [Felucca", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(101, 23, 55) [Felucca", null));
// No parenthesis
Assert.False(WorldLocation.TryParse("101, 23 55) [Felucca]", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("101, 23, 55) [Felucca]", null));
Assert.False(WorldLocation.TryParse("(101, 23, 55 [Felucca]", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(101, 23, 55 [Felucca]", null));
// No Map - We don't support maps with no name, sorry.
Assert.False(WorldLocation.TryParse("(101, 23, 55) []", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(101, 23, 55) []", null));
// No numbers
Assert.False(WorldLocation.TryParse("()", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("()", null));
Assert.False(WorldLocation.TryParse("(,)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(,)", null));
Assert.False(WorldLocation.TryParse("(|)", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(|)", null));
Assert.False(WorldLocation.TryParse("() []", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("() []", null));
Assert.False(WorldLocation.TryParse("(101) [Felucca]", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(101) [Felucca]", null));
Assert.False(WorldLocation.TryParse("(101,) [Felucca]", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(101,) [Felucca]", null));
Assert.False(WorldLocation.TryParse("(,23) [Felucca]", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(,23) [Felucca]", null));
Assert.False(WorldLocation.TryParse("(,23,55) [Felucca]", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(,23,55) [Felucca]", null));
Assert.False(WorldLocation.TryParse("(,23,) [Felucca]", null, out p));
Assert.Equal(default, p);
Assert.Throws<FormatException>(() => WorldLocation.Parse("(,23,) [Felucca]", null));
}
}