Skip to content

Commit e680bdb

Browse files
authored
fix: Adds boolean support for Server.TryParse (#1605)
1 parent 03f850f commit e680bdb

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Server;
2+
using System;
3+
using Xunit;
4+
5+
namespace Server.Tests.Utility;
6+
7+
public class TryParseTests
8+
{
9+
[Theory]
10+
[InlineData("True", null, true)]
11+
[InlineData("False", null, false)]
12+
[InlineData("Alakazam", "Not a valid boolean string.", true)]
13+
public void TestTryParseBool(string value, string returned, bool parsedAs)
14+
{
15+
string actualReturned = Server.Types.TryParse(typeof(bool), value, out object constructed);
16+
Assert.Equal(returned, actualReturned);
17+
18+
if (returned == null)
19+
{
20+
Assert.Equal(parsedAs, constructed);
21+
}
22+
}
23+
}

Projects/UOContent/Utilities/Types.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ public static string TryParse(Type type, string value, out object constructed)
187187
return null;
188188
}
189189

190+
if (IsType(type, OfBool))
191+
{
192+
if (bool.TryParse(value, out bool parsed))
193+
{
194+
constructed = parsed;
195+
return null;
196+
}
197+
198+
return "Not a valid boolean string.";
199+
}
200+
190201
if (value.StartsWithOrdinal("0x") && IsNumeric(type))
191202
{
192203
try

0 commit comments

Comments
 (0)