Skip to content

Commit

Permalink
fix: Adds boolean support for Server.TryParse (#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdodkins authored Nov 21, 2023
1 parent 03f850f commit e680bdb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Projects/UOContent.Tests/Tests/Utilities/TryParseTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
11 changes: 11 additions & 0 deletions Projects/UOContent/Utilities/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ public static string TryParse(Type type, string value, out object constructed)
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
Expand Down

0 comments on commit e680bdb

Please sign in to comment.