Skip to content

Commit

Permalink
负数转为Boolean时,作为true
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Sep 28, 2024
1 parent e24a72e commit d12c882
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion NewLife.Core/Common/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public virtual Boolean ToBoolean(Object? value, Boolean defaultValue)
if (String.Equals(str, Boolean.TrueString, StringComparison.OrdinalIgnoreCase)) return true;
if (String.Equals(str, Boolean.FalseString, StringComparison.OrdinalIgnoreCase)) return false;

return Int32.TryParse(str, out var n) ? n > 0 : defaultValue;
return Int32.TryParse(str, out var n) ? n != 0 : defaultValue;
}

try
Expand Down
10 changes: 10 additions & 0 deletions XUnitTest.Core/Reflection/ReflectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,21 @@ public void IsNumber(Type type, Boolean result)

[Theory]
[InlineData("true", typeof(Boolean), true)]
[InlineData("False", typeof(Boolean), false)]
[InlineData("1", typeof(Boolean), true)]
[InlineData("0", typeof(Boolean), false)]
[InlineData("2", typeof(Boolean), true)]
[InlineData("-1", typeof(Boolean), true)]
[InlineData(1, typeof(Boolean), true)]
[InlineData(0, typeof(Boolean), false)]
[InlineData(-1, typeof(Boolean), true)]
[InlineData("1234", typeof(Int16), (Int16)1234)]
[InlineData("1234", typeof(Int32), 1234)]
[InlineData("-1234", typeof(Int32), -1234)]
[InlineData("0", typeof(Int32), 0)]
[InlineData("-1", typeof(Int32), -1)]
[InlineData("12.34", typeof(Double), 12.34)]
[InlineData("-12.34", typeof(Double), -12.34)]
[InlineData("byte[]", typeof(Type), typeof(Byte[]))]
public void ChangeTypeTest(Object value, Type targetType, Object target)
{
Expand Down

0 comments on commit d12c882

Please sign in to comment.