fix: Adds boolean support for Server.TryParse (#1605)

This commit is contained in:
mdodkins 2023-11-21 20:21:59 +00:00 committed by GitHub
parent 03f850fe03
commit e680bdb4b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View 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);
}
}
}

View file

@ -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