Skip to content

Commit

Permalink
Add default param to try parse and support net 6
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoFeiDu committed Nov 9, 2021
1 parent 5d6a94d commit 4632c5a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 64 deletions.
65 changes: 33 additions & 32 deletions src/Zaabee.Extensions/Zaabee.Extensions.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,59 +112,60 @@ public static DateTime ParseDateTime(this string s) =>
public static DateTimeOffset ParseDateTimeOffset(this string s) =>
DateTimeOffset.Parse(s);

public static object ParseEnum(this string value, Type enumType) =>
public static object ParseEnum(this string value, Type enumType, bool ignoreCase = false) =>
enumType is null
? throw new ArgumentNullException(nameof(enumType))
: Enum.Parse(enumType, value);
: Enum.Parse(enumType, value, ignoreCase);

#endregion

#region TryParse

public static sbyte TryParseSbyte(this string s) =>
sbyte.TryParse(s, out var result) ? result : default;
public static sbyte TryParseSbyte(this string s, sbyte @default = default) =>
sbyte.TryParse(s, out var result) ? result : @default;

public static byte TryParseByte(this string s) =>
byte.TryParse(s, out var result) ? result : default;
public static byte TryParseByte(this string s, byte @default = default) =>
byte.TryParse(s, out var result) ? result : @default;

public static short TryParseShort(this string s) =>
short.TryParse(s, out var result) ? result : default;
public static short TryParseShort(this string s, short @default = default) =>
short.TryParse(s, out var result) ? result : @default;

public static ushort TryParseUshort(this string s) =>
ushort.TryParse(s, out var result) ? result : default;
public static ushort TryParseUshort(this string s, ushort @default = default) =>
ushort.TryParse(s, out var result) ? result : @default;

public static int TryParseInt(this string s) =>
int.TryParse(s, out var result) ? result : default;
public static int TryParseInt(this string s, int @default = default) =>
int.TryParse(s, out var result) ? result : @default;

public static uint TryParseUint(this string s) =>
uint.TryParse(s, out var result) ? result : default;
public static uint TryParseUint(this string s, uint @default = default) =>
uint.TryParse(s, out var result) ? result : @default;

public static long TryParseLong(this string s) =>
long.TryParse(s, out var result) ? result : default;
public static long TryParseLong(this string s, long @default = default) =>
long.TryParse(s, out var result) ? result : @default;

public static ulong TryParseUlong(this string s) =>
ulong.TryParse(s, out var result) ? result : default;
public static ulong TryParseUlong(this string s, ulong @default = default) =>
ulong.TryParse(s, out var result) ? result : @default;

public static float TryParseFloat(this string s) =>
float.TryParse(s, out var result) ? result : default;
public static float TryParseFloat(this string s, float @default = default) =>
float.TryParse(s, out var result) ? result : @default;

public static double TryParseDouble(this string s) =>
double.TryParse(s, out var result) ? result : default;
public static double TryParseDouble(this string s, double @default = default) =>
double.TryParse(s, out var result) ? result : @default;

public static decimal TryParseDecimal(this string s) =>
decimal.TryParse(s, out var result) ? result : default;
public static decimal TryParseDecimal(this string s, decimal @default = default) =>
decimal.TryParse(s, out var result) ? result : @default;

public static bool TryParseBool(this string s) =>
bool.TryParse(s, out var result) ? result : default;
public static bool TryParseBool(this string s, bool @default = default) =>
bool.TryParse(s, out var result) ? result : @default;

public static DateTime TryParseDateTime(this string s) =>
DateTime.TryParse(s, out var result) ? result : default;
public static DateTime TryParseDateTime(this string s, DateTime @default = default) =>
DateTime.TryParse(s, out var result) ? result : @default;

public static DateTimeOffset TryParseDateTimeOffset(this string s) =>
DateTimeOffset.TryParse(s, out var result) ? result : default;
public static DateTimeOffset TryParseDateTimeOffset(this string s, DateTimeOffset @default = default) =>
DateTimeOffset.TryParse(s, out var result) ? result : @default;

public static TEnum TryParseEnum<TEnum>(this string value) where TEnum : struct, Enum =>
Enum.TryParse(value, out TEnum result) ? result : default;
public static TEnum TryParseEnum<TEnum>(this string value, TEnum @default = default)
where TEnum : struct, Enum =>
Enum.TryParse(value, out TEnum result) ? result : @default;

#endregion

Expand Down
6 changes: 3 additions & 3 deletions src/Zaabee.Extensions/Zaabee.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2021.5.0</PackageVersion>
<Version>2021.5.0</Version>
<PackageVersion>2021.6.0</PackageVersion>
<Version>2021.6.0</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageTags>Zaabee;Extensions</PackageTags>
Expand Down
89 changes: 62 additions & 27 deletions tests/Zaabee.Extensions.UnitTest/StringExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void TrimEnd()
{
const string target = "apple,banana,pear";
const string trimString = "pear";
Assert.Equal("apple,banana,",target.TrimEnd(trimString));
Assert.Equal("apple,banana,", target.TrimEnd(trimString));
Assert.Equal(target, target.TrimEnd(string.Empty));
}

Expand All @@ -49,7 +49,7 @@ public void IsNullOrWhiteSpaceTest(string str) =>
[InlineData(" or ")]
public void StringJoinTest(string separator)
{
var stringList = new List<string> {"Alice", "Bob", "Carol", "Dave", "Eve"};
var stringList = new List<string> { "Alice", "Bob", "Carol", "Dave", "Eve" };
Assert.Equal(stringList.StringJoin(separator), string.Join(separator, stringList));
}

Expand Down Expand Up @@ -165,22 +165,34 @@ public void ToUnicodeBytesTest(string str) =>
[InlineData(float.MaxValue)]
[InlineData(float.MinValue)]
[InlineData(0)]
public void ParseFloatTest(float value) =>
public void ParseFloatTest(float value)
{
#if NET48
Assert.Equal(value, value.ToString("R").ParseFloat());
#else
Assert.Equal(value, value.ToString(CultureInfo.InvariantCulture).ParseFloat());
#endif
}

[Theory]
[InlineData(double.MaxValue)]
[InlineData(double.MinValue)]
[InlineData(0)]
public void ParseDoubleTest(double value) =>
public void ParseDoubleTest(double value)
{
#if NET48
Assert.Equal(value, value.ToString("R").ParseDouble());
#else
Assert.Equal(value, value.ToString(CultureInfo.InvariantCulture).ParseDouble());
#endif
}

[Fact]
public void ParseDecimalTest()
{
Assert.Equal(decimal.MaxValue, decimal.MaxValue.ToString(CultureInfo.InvariantCulture).ParseDecimal());
Assert.Equal(decimal.MinValue, decimal.MinValue.ToString(CultureInfo.InvariantCulture).ParseDecimal());
Assert.Equal(0, 0.ToString().ParseDecimal());
Assert.Equal(0M, 0M.ToString(CultureInfo.InvariantCulture).ParseDecimal());
}

[Theory]
Expand All @@ -207,8 +219,12 @@ public void ParseDateTimeOffsetTest()
}

[Fact]
public void ParseEnumTest() =>
public void ParseEnumTest()
{
Assert.Equal(TestEnum.Create, TestEnum.Create.ToString().ParseEnum(typeof(TestEnum)));
Assert.Equal(TestEnum.Create, TestEnum.Create.ToString().ToLower().ParseEnum(typeof(TestEnum), true));
Assert.Throws<ArgumentException>(() => TestEnum.Create.ToString().ToLower().ParseEnum(typeof(TestEnum)));
}

#endregion

Expand All @@ -220,8 +236,8 @@ public void TryParseSbyteTest()
Assert.Equal(sbyte.MaxValue, sbyte.MaxValue.ToString().TryParseSbyte());
Assert.Equal(sbyte.MinValue, sbyte.MinValue.ToString().TryParseSbyte());
Assert.Equal(0, 0.ToString().TryParseSbyte());
Assert.Equal(0, (sbyte.MaxValue + 1).ToString().TryParseSbyte());
Assert.Equal(0, (sbyte.MinValue - 1).ToString().TryParseSbyte());
Assert.Equal(default, string.Empty.TryParseSbyte());
Assert.Equal(1, string.Empty.TryParseSbyte(1));
}

[Fact]
Expand All @@ -230,8 +246,8 @@ public void TryParseByteTest()
Assert.Equal(byte.MaxValue, byte.MaxValue.ToString().TryParseByte());
Assert.Equal(byte.MinValue, byte.MinValue.ToString().TryParseByte());
Assert.Equal(0, 0.ToString().TryParseByte());
Assert.Equal(0, (byte.MaxValue + 1).ToString().TryParseByte());
Assert.Equal(0, (byte.MinValue - 1).ToString().TryParseByte());
Assert.Equal(default, string.Empty.TryParseByte());
Assert.Equal(1, string.Empty.TryParseByte(1));
}

[Fact]
Expand All @@ -240,8 +256,8 @@ public void TryParseShortTest()
Assert.Equal(short.MaxValue, short.MaxValue.ToString().TryParseShort());
Assert.Equal(short.MinValue, short.MinValue.ToString().TryParseShort());
Assert.Equal(0, 0.ToString().TryParseShort());
Assert.Equal(0, (short.MaxValue + 1).ToString().TryParseShort());
Assert.Equal(0, (short.MinValue - 1).ToString().TryParseShort());
Assert.Equal(default, string.Empty.TryParseShort());
Assert.Equal(1, string.Empty.TryParseShort(1));
}

[Fact]
Expand All @@ -250,8 +266,8 @@ public void TryParseUshortTest()
Assert.Equal(ushort.MaxValue, ushort.MaxValue.ToString().TryParseUshort());
Assert.Equal(ushort.MinValue, ushort.MinValue.ToString().TryParseUshort());
Assert.Equal(0, 0.ToString().TryParseUshort());
Assert.Equal(0, (ushort.MaxValue + 1).ToString().TryParseUshort());
Assert.Equal(0, (ushort.MinValue - 1).ToString().TryParseUshort());
Assert.Equal(default, string.Empty.TryParseUshort());
Assert.Equal(1, string.Empty.TryParseUshort(1));
}

[Fact]
Expand All @@ -260,8 +276,8 @@ public void TryParseIntTest()
Assert.Equal(int.MaxValue, int.MaxValue.ToString().TryParseInt());
Assert.Equal(int.MinValue, int.MinValue.ToString().TryParseInt());
Assert.Equal(0, 0.ToString().TryParseInt());
Assert.Equal(0, (int.MaxValue + 1L).ToString().TryParseInt());
Assert.Equal(0, (int.MinValue - 1L).ToString().TryParseInt());
Assert.Equal(default, string.Empty.TryParseInt());
Assert.Equal(1, string.Empty.TryParseInt(1));
}

[Fact]
Expand All @@ -270,8 +286,8 @@ public void TryParseUintTest()
Assert.Equal(uint.MaxValue, uint.MaxValue.ToString().TryParseUint());
Assert.Equal(uint.MinValue, uint.MinValue.ToString().TryParseUint());
Assert.Equal(0U, 0.ToString().TryParseUint());
Assert.Equal(0U, (uint.MaxValue + 1L).ToString().TryParseUint());
Assert.Equal(0U, (uint.MinValue - 1L).ToString().TryParseUint());
Assert.Equal(default, string.Empty.TryParseUint());
Assert.Equal(1U, string.Empty.TryParseUint(1));
}

[Fact]
Expand All @@ -280,36 +296,48 @@ public void TryParseLongTest()
Assert.Equal(long.MaxValue, long.MaxValue.ToString().TryParseLong());
Assert.Equal(long.MinValue, long.MinValue.ToString().TryParseLong());
Assert.Equal(0L, 0.ToString().TryParseLong());
Assert.Equal(0L, (long.MaxValue + 1M).ToString(CultureInfo.InvariantCulture).TryParseLong());
Assert.Equal(0L, (long.MinValue - 1M).ToString(CultureInfo.InvariantCulture).TryParseLong());
Assert.Equal(default, string.Empty.TryParseLong());
Assert.Equal(1L, string.Empty.TryParseLong(1));
}

[Fact]
public void TryParseUlongTest()
{
Assert.Equal(ulong.MaxValue, ulong.MaxValue.ToString().TryParseUlong());
Assert.Equal(ulong.MinValue, ulong.MinValue.ToString().TryParseUlong());
Assert.Equal(0M, 0.ToString().TryParseUlong());
Assert.Equal(0M, (ulong.MaxValue + 1M).ToString(CultureInfo.InvariantCulture).TryParseUlong());
Assert.Equal(0M, (ulong.MinValue - 1M).ToString(CultureInfo.InvariantCulture).TryParseUlong());
Assert.Equal(0UL, 0.ToString().TryParseUlong());
Assert.Equal(default, string.Empty.TryParseUlong());
Assert.Equal(1UL, string.Empty.TryParseUlong(1));
}

[Fact]
public void TryParseFloatTest()
{
#if NET48
Assert.Equal(float.MaxValue, float.MaxValue.ToString("R").TryParseFloat());
Assert.Equal(float.MinValue, float.MinValue.ToString("R").TryParseFloat());
#else
Assert.Equal(float.MaxValue, float.MaxValue.ToString(CultureInfo.InvariantCulture).TryParseFloat());
Assert.Equal(float.MinValue, float.MinValue.ToString(CultureInfo.InvariantCulture).TryParseFloat());
#endif
Assert.Equal(0F, 0.ToString().TryParseFloat());
Assert.Equal(0F, string.Empty.ToString(CultureInfo.InvariantCulture).TryParseFloat());
Assert.Equal(default, string.Empty.TryParseFloat());
Assert.Equal(1F, string.Empty.TryParseFloat(1));
}

[Fact]
public void TryParseDoubleTest()
{
#if NET48
Assert.Equal(double.MaxValue, double.MaxValue.ToString("R").TryParseDouble());
Assert.Equal(double.MinValue, double.MinValue.ToString("R").TryParseDouble());
#else
Assert.Equal(double.MaxValue, double.MaxValue.ToString(CultureInfo.InvariantCulture).TryParseDouble());
Assert.Equal(double.MinValue, double.MinValue.ToString(CultureInfo.InvariantCulture).TryParseDouble());
#endif
Assert.Equal(0D, 0.ToString().TryParseDouble());
Assert.Equal(0D, string.Empty.ToString(CultureInfo.InvariantCulture).TryParseDouble());
Assert.Equal(default, string.Empty.TryParseDouble());
Assert.Equal(1D, string.Empty.TryParseDouble(1));
}

[Fact]
Expand All @@ -318,7 +346,8 @@ public void TryParseDecimalTest()
Assert.Equal(decimal.MaxValue, decimal.MaxValue.ToString(CultureInfo.InvariantCulture).TryParseDecimal());
Assert.Equal(decimal.MinValue, decimal.MinValue.ToString(CultureInfo.InvariantCulture).TryParseDecimal());
Assert.Equal(0M, 0.ToString().TryParseDecimal());
Assert.Equal(0M, string.Empty.TryParseDecimal());
Assert.Equal(default, string.Empty.TryParseDecimal());
Assert.Equal(1M, string.Empty.TryParseDecimal(1));
}

[Fact]
Expand All @@ -327,6 +356,8 @@ public void TryParseBoolTest()
Assert.True(true.ToString().TryParseBool());
Assert.False(false.ToString().TryParseBool());
Assert.False(0.ToString().TryParseBool());
Assert.False(string.Empty.TryParseBool());
Assert.True(string.Empty.TryParseBool(true));
}

[Fact]
Expand All @@ -337,6 +368,8 @@ public void TryParseDateTimeTest()
Assert.Equal(maxDateTime, maxDateTime.ToString(CultureInfo.InvariantCulture).TryParseDateTime());
Assert.Equal(minDateTime, minDateTime.ToString(CultureInfo.InvariantCulture).TryParseDateTime());
Assert.Equal(default, string.Empty.TryParseDateTime());
Assert.Equal(maxDateTime, string.Empty.TryParseDateTime(maxDateTime));
Assert.Equal(minDateTime, string.Empty.TryParseDateTime(minDateTime));
}

[Fact]
Expand All @@ -347,6 +380,8 @@ public void TryParseDateTimeOffsetTest()
Assert.Equal(maxDateTimeOffset, maxDateTimeOffset.ToString().TryParseDateTimeOffset());
Assert.Equal(minDateTimeOffset, minDateTimeOffset.ToString().TryParseDateTimeOffset());
Assert.Equal(default, string.Empty.TryParseDateTimeOffset());
Assert.Equal(maxDateTimeOffset, string.Empty.TryParseDateTimeOffset(maxDateTimeOffset));
Assert.Equal(minDateTimeOffset, string.Empty.TryParseDateTimeOffset(minDateTimeOffset));
}

[Fact]
Expand Down Expand Up @@ -457,7 +492,7 @@ public void ToLongTest(long value, NumerationSystem numerationSystem)

private static bool BytesEqual(byte[] first, byte[] second)
{
if (first == null || second == null) return false;
if (first is null || second is null) return false;
if (first.Length != second.Length) return false;
return !first.Where((t, i) => t != second[i]).Any();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks>net48;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand All @@ -11,7 +11,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>
Expand Down

0 comments on commit 4632c5a

Please sign in to comment.