fix: Adds boolean support for Server.TryParse (#1605)
This commit is contained in:
parent
03f850fe03
commit
e680bdb4b5
2 changed files with 34 additions and 0 deletions
23
Projects/UOContent.Tests/Tests/Utilities/TryParseTests.cs
Normal file
23
Projects/UOContent.Tests/Tests/Utilities/TryParseTests.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using Server;
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Utility;
|
||||
|
||||
public class TryParseTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("True", null, true)]
|
||||
[InlineData("False", null, false)]
|
||||
[InlineData("Alakazam", "Not a valid boolean string.", true)]
|
||||
public void TestTryParseBool(string value, string returned, bool parsedAs)
|
||||
{
|
||||
string actualReturned = Server.Types.TryParse(typeof(bool), value, out object constructed);
|
||||
Assert.Equal(returned, actualReturned);
|
||||
|
||||
if (returned == null)
|
||||
{
|
||||
Assert.Equal(parsedAs, constructed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -187,6 +187,17 @@ namespace Server
|
|||
return null;
|
||||
}
|
||||
|
||||
if (IsType(type, OfBool))
|
||||
{
|
||||
if (bool.TryParse(value, out bool parsed))
|
||||
{
|
||||
constructed = parsed;
|
||||
return null;
|
||||
}
|
||||
|
||||
return "Not a valid boolean string.";
|
||||
}
|
||||
|
||||
if (value.StartsWithOrdinal("0x") && IsNumeric(type))
|
||||
{
|
||||
try
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue