Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions PowerKit.Tests/BoolExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public void ParseOrNull_Test()
bool.ParseOrNull("yes").Should().BeNull();
bool.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
bool.ParseOrDefault("true").Should().BeTrue();
bool.ParseOrDefault("false").Should().BeFalse();
bool.ParseOrDefault("yes").Should().BeFalse();
bool.ParseOrDefault("yes", true).Should().BeTrue();
bool.ParseOrDefault(null).Should().BeFalse();
}
}
13 changes: 13 additions & 0 deletions PowerKit.Tests/DateTimeExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,17 @@ public void ParseOrNull_Test()
DateTime.ParseOrNull("not a date").Should().BeNull();
DateTime.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
DateTime.ParseOrDefault("2024-06-15").Should().Be(new DateTime(2024, 6, 15));
DateTime.ParseOrDefault("not a date").Should().Be(default(DateTime));
DateTime
.ParseOrDefault("not a date", new DateTime(2000, 1, 1))
.Should()
.Be(new DateTime(2000, 1, 1));
DateTime.ParseOrDefault(null).Should().Be(default(DateTime));
}
}
16 changes: 16 additions & 0 deletions PowerKit.Tests/DateTimeOffsetExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ public void ParseOrNull_Test()
DateTimeOffset.ParseOrNull("not a date").Should().BeNull();
DateTimeOffset.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
DateTimeOffset
.ParseOrDefault("2024-06-15T00:00:00+00:00")
.Should()
.Be(new DateTimeOffset(2024, 6, 15, 0, 0, 0, TimeSpan.Zero));
DateTimeOffset.ParseOrDefault("not a date").Should().Be(default(DateTimeOffset));
DateTimeOffset
.ParseOrDefault("not a date", new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero))
.Should()
.Be(new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero));
DateTimeOffset.ParseOrDefault(null).Should().Be(default(DateTimeOffset));
}
}
11 changes: 11 additions & 0 deletions PowerKit.Tests/DecimalExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public void ParseOrNull_Test()
decimal.ParseOrNull("abc").Should().BeNull();
decimal.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
decimal.ParseOrDefault("3.14").Should().Be(3.14m);
decimal.ParseOrDefault("-1.5").Should().Be(-1.5m);
decimal.ParseOrDefault("abc").Should().Be(0m);
decimal.ParseOrDefault("abc", -1m).Should().Be(-1m);
decimal.ParseOrDefault(null).Should().Be(0m);
}
}
11 changes: 11 additions & 0 deletions PowerKit.Tests/DoubleExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public void ParseOrNull_Test()
double.ParseOrNull("abc").Should().BeNull();
double.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
double.ParseOrDefault("3.14").Should().Be(3.14);
double.ParseOrDefault("-1.5").Should().Be(-1.5);
double.ParseOrDefault("abc").Should().Be(0.0);
double.ParseOrDefault("abc", -1.0).Should().Be(-1.0);
double.ParseOrDefault(null).Should().Be(0.0);
}
}
14 changes: 14 additions & 0 deletions PowerKit.Tests/GuidExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ public void ParseOrNull_Test()
Guid.ParseOrNull("not-a-guid").Should().BeNull();
Guid.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
Guid.ParseOrDefault("12345678-1234-1234-1234-123456789abc")
.Should()
.Be(new Guid("12345678-1234-1234-1234-123456789abc"));
Guid.ParseOrDefault("not-a-guid").Should().Be(Guid.Empty);
Guid.ParseOrDefault("not-a-guid", new Guid("ffffffff-ffff-ffff-ffff-ffffffffffff"))
.Should()
.Be(new Guid("ffffffff-ffff-ffff-ffff-ffffffffffff"));
Guid.ParseOrDefault(null).Should().Be(Guid.Empty);
}
}
11 changes: 11 additions & 0 deletions PowerKit.Tests/IntExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public void ParseOrNull_Test()
int.ParseOrNull("abc").Should().BeNull();
int.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
int.ParseOrDefault("42").Should().Be(42);
int.ParseOrDefault("-7").Should().Be(-7);
int.ParseOrDefault("abc").Should().Be(0);
int.ParseOrDefault("abc", -1).Should().Be(-1);
int.ParseOrDefault(null).Should().Be(0);
}
}
11 changes: 11 additions & 0 deletions PowerKit.Tests/LongExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public void ParseOrNull_Test()
long.ParseOrNull("abc").Should().BeNull();
long.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
long.ParseOrDefault("9876543210").Should().Be(9876543210L);
long.ParseOrDefault("-1").Should().Be(-1L);
long.ParseOrDefault("abc").Should().Be(0L);
long.ParseOrDefault("abc", -1L).Should().Be(-1L);
long.ParseOrDefault(null).Should().Be(0L);
}
}
13 changes: 13 additions & 0 deletions PowerKit.Tests/TimeSpanExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,17 @@ public void ParseOrNull_Test()
TimeSpan.ParseOrNull("not a timespan").Should().BeNull();
TimeSpan.ParseOrNull(null).Should().BeNull();
}

[Fact]
public void ParseOrDefault_Test()
{
// Act & assert
TimeSpan.ParseOrDefault("1:30:00").Should().Be(new TimeSpan(1, 30, 0));
TimeSpan.ParseOrDefault("not a timespan").Should().Be(TimeSpan.Zero);
TimeSpan
.ParseOrDefault("not a timespan", TimeSpan.FromSeconds(5))
.Should()
.Be(TimeSpan.FromSeconds(5));
TimeSpan.ParseOrDefault(null).Should().Be(TimeSpan.Zero);
}
}
6 changes: 6 additions & 0 deletions PowerKit/Extensions/BoolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ internal static class BoolExtensions
/// </summary>
public static bool? ParseOrNull(string? str) =>
bool.TryParse(str, out var result) ? result : null;

/// <summary>
/// Parses the string as a <see cref="bool" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static bool ParseOrDefault(string? str, bool defaultValue = default) =>
bool.ParseOrNull(str) ?? defaultValue;
}
}
17 changes: 17 additions & 0 deletions PowerKit/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,22 @@ DateTimeStyles styles
/// </summary>
public static DateTime? ParseOrNull(string? str) =>
DateTime.ParseOrNull(str, CultureInfo.CurrentCulture, DateTimeStyles.None);

/// <summary>
/// Parses the string as a <see cref="DateTime" /> using the specified format provider and styles,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static DateTime ParseOrDefault(
string? str,
IFormatProvider? formatProvider,
DateTimeStyles styles,
DateTime defaultValue = default
) => DateTime.ParseOrNull(str, formatProvider, styles) ?? defaultValue;

/// <summary>
/// Parses the string as a <see cref="DateTime" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static DateTime ParseOrDefault(string? str, DateTime defaultValue = default) =>
DateTime.ParseOrNull(str) ?? defaultValue;
}
}
19 changes: 19 additions & 0 deletions PowerKit/Extensions/DateTimeOffsetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,24 @@ DateTimeStyles styles
/// </summary>
public static DateTimeOffset? ParseOrNull(string? str) =>
DateTimeOffset.ParseOrNull(str, CultureInfo.CurrentCulture, DateTimeStyles.None);

/// <summary>
/// Parses the string as a <see cref="DateTimeOffset" /> using the specified format provider and styles,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static DateTimeOffset ParseOrDefault(
string? str,
IFormatProvider? formatProvider,
DateTimeStyles styles,
DateTimeOffset defaultValue = default
) => DateTimeOffset.ParseOrNull(str, formatProvider, styles) ?? defaultValue;

/// <summary>
/// Parses the string as a <see cref="DateTimeOffset" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static DateTimeOffset ParseOrDefault(
string? str,
DateTimeOffset defaultValue = default
) => DateTimeOffset.ParseOrNull(str) ?? defaultValue;
}
}
17 changes: 17 additions & 0 deletions PowerKit/Extensions/DecimalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,22 @@ internal static class DecimalExtensions
/// </summary>
public static decimal? ParseOrNull(string? str) =>
decimal.ParseOrNull(str, NumberStyles.Number, CultureInfo.CurrentCulture);

/// <summary>
/// Parses the string as a <see cref="decimal" /> using the specified styles and format provider,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static decimal ParseOrDefault(
string? str,
NumberStyles styles,
IFormatProvider? formatProvider,
decimal defaultValue = default
) => decimal.ParseOrNull(str, styles, formatProvider) ?? defaultValue;

/// <summary>
/// Parses the string as a <see cref="decimal" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static decimal ParseOrDefault(string? str, decimal defaultValue = default) =>
decimal.ParseOrNull(str) ?? defaultValue;
}
}
17 changes: 17 additions & 0 deletions PowerKit/Extensions/DoubleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,22 @@ internal static class DoubleExtensions
NumberStyles.Float | NumberStyles.AllowThousands,
CultureInfo.CurrentCulture
);

/// <summary>
/// Parses the string as a <see cref="double" /> using the specified styles and format provider,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static double ParseOrDefault(
string? str,
NumberStyles styles,
IFormatProvider? formatProvider,
double defaultValue = default
) => double.ParseOrNull(str, styles, formatProvider) ?? defaultValue;

/// <summary>
/// Parses the string as a <see cref="double" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static double ParseOrDefault(string? str, double defaultValue = default) =>
double.ParseOrNull(str) ?? defaultValue;
}
}
6 changes: 6 additions & 0 deletions PowerKit/Extensions/GuidExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ internal static class GuidExtensions
/// </summary>
public static Guid? ParseOrNull(string? str) =>
Guid.TryParse(str, out var result) ? result : null;

/// <summary>
/// Parses the string as a <see cref="Guid" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static Guid ParseOrDefault(string? str, Guid defaultValue = default) =>
Guid.ParseOrNull(str) ?? defaultValue;
}
}
17 changes: 17 additions & 0 deletions PowerKit/Extensions/IntExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,22 @@ internal static class IntExtensions
/// </summary>
public static int? ParseOrNull(string? str) =>
int.ParseOrNull(str, NumberStyles.Integer, CultureInfo.CurrentCulture);

/// <summary>
/// Parses the string as an <see cref="int" /> using the specified styles and format provider,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static int ParseOrDefault(
string? str,
NumberStyles styles,
IFormatProvider? formatProvider,
int defaultValue = default
) => int.ParseOrNull(str, styles, formatProvider) ?? defaultValue;

/// <summary>
/// Parses the string as an <see cref="int" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static int ParseOrDefault(string? str, int defaultValue = default) =>
int.ParseOrNull(str) ?? defaultValue;
}
}
17 changes: 17 additions & 0 deletions PowerKit/Extensions/LongExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,22 @@ internal static class LongExtensions
/// </summary>
public static long? ParseOrNull(string? str) =>
long.ParseOrNull(str, NumberStyles.Integer, CultureInfo.CurrentCulture);

/// <summary>
/// Parses the string as a <see cref="long" /> using the specified styles and format provider,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static long ParseOrDefault(
string? str,
NumberStyles styles,
IFormatProvider? formatProvider,
long defaultValue = default
) => long.ParseOrNull(str, styles, formatProvider) ?? defaultValue;

/// <summary>
/// Parses the string as a <see cref="long" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static long ParseOrDefault(string? str, long defaultValue = default) =>
long.ParseOrNull(str) ?? defaultValue;
}
}
16 changes: 16 additions & 0 deletions PowerKit/Extensions/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,21 @@ internal static class TimeSpanExtensions
/// </summary>
public static TimeSpan? ParseOrNull(string? str) =>
TimeSpan.ParseOrNull(str, CultureInfo.CurrentCulture);

/// <summary>
/// Parses the string as a <see cref="TimeSpan" /> using the specified format provider,
/// returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static TimeSpan ParseOrDefault(
string? str,
IFormatProvider? formatProvider,
TimeSpan defaultValue = default
) => TimeSpan.ParseOrNull(str, formatProvider) ?? defaultValue;

/// <summary>
/// Parses the string as a <see cref="TimeSpan" />, returning <paramref name="defaultValue" /> if parsing fails.
/// </summary>
public static TimeSpan ParseOrDefault(string? str, TimeSpan defaultValue = default) =>
TimeSpan.ParseOrNull(str) ?? defaultValue;
}
}