Skip to content

Commit

Permalink
Merge pull request #9 from BenjaminAbt/feature/add-from-methods
Browse files Browse the repository at this point in the history
add from methods
  • Loading branch information
BenjaminAbt authored Jan 11, 2024
2 parents 1a6649e + 9924d52 commit 0d6be33
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public class MySubmitModelValidator : AbstractValidator<MySubmitModel>

[![StrongOf](https://img.shields.io/nuget/v/StrongOf.svg?logo=nuget&label=StrongOf)](https://www.nuget.org/packages/StrongOf)\
[![StrongOf.AspNetCore](https://img.shields.io/nuget/v/StrongOf.AspNetCore.svg?logo=nuget&label=StrongOf.AspNetCore)](https://www.nuget.org/packages/StrongOf.AspNetCore)\
[![StrongOf.Json](https://img.shields.io/nuget/v/StrongOf.Json.svg?logo=nuget&label=StrongOf.Json)](https://www.nuget.org/packages/StrongOf.Json)
[![StrongOf.Json](https://img.shields.io/nuget/v/StrongOf.Json.svg?logo=nuget&label=StrongOf.Json)](https://www.nuget.org/packages/StrongOf.Json)\
[![StrongOf.FluentValidation](https://img.shields.io/nuget/v/StrongOf.FluentValidation.svg?logo=nuget&label=StrongOf.FluentValidation)](https://www.nuget.org/packages/StrongOf.FluentValidation)
See [StrongOf on NuGet.org](https://www.nuget.org/packages/StrongOf)
Expand Down
31 changes: 24 additions & 7 deletions src/StrongOf/StrongGuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public abstract class StrongGuid<TStrong>(Guid Value) : StrongOf<Guid, TStrong>(
public Guid AsGuid() => Value;

/// <summary>
/// Creates a new instance of StrongGuid from a nullable Guid value.
/// Converts a nullable Guid value to a strong type of Guid.
/// </summary>
/// <param name="value">The nullable char value.</param>
/// <returns>A new instance of StrongGuid if the value has a value, null otherwise.</returns>
/// <param name="value">The nullable Guid value to convert.</param>
/// <returns>A strong type of Guid if the value has a Guid; otherwise, null.</returns>
[return: NotNullIfNotNull(nameof(value))]
public static TStrong? FromNullable(Guid? value)
public static TStrong? FromGuid(Guid? value)
{
if (value.HasValue)
{
Expand All @@ -31,6 +31,23 @@ public abstract class StrongGuid<TStrong>(Guid Value) : StrongOf<Guid, TStrong>(
return null;
}

/// <summary>
/// Converts a string to a strong type of Guid.
/// </summary>
/// <param name="value">The string to convert.</param>
/// <returns>A strong type of Guid if the string can be parsed to a Guid; otherwise, null.</returns>
[return: NotNullIfNotNull(nameof(value))]
public static TStrong? FromString(string? value)
{
if (value is not null && Guid.TryParse(value, out Guid result))
{
TStrong strong = From(result);
return strong;
}

return null;
}

/// <summary>
/// Initializes a new instance of the StrongGuid class using the specified string representation of a Guid.
/// </summary>
Expand Down Expand Up @@ -63,7 +80,7 @@ public int CompareTo(object? other)
/// </summary>
/// <returns>An empty Guid of the strong type.</returns>
public static TStrong Empty()
=> From(Guid.Empty);
=> StrongOf<Guid, TStrong>.From(Guid.Empty);

/// <summary>
/// Checks if the Guid is empty.
Expand All @@ -77,7 +94,7 @@ public bool IsEmpty()
/// </summary>
/// <returns>A new Guid of the strong type.</returns>
public static TStrong New()
=> From(Guid.NewGuid());
=> StrongOf<Guid, TStrong>.From(Guid.NewGuid());

/// <summary>
/// Tries to parse a Guid from a string and returns a value that indicates whether the operation succeeded.
Expand All @@ -89,7 +106,7 @@ public static bool TryParse(ReadOnlySpan<char> content, [NotNullWhen(true)] out
{
if (Guid.TryParse(content, out Guid value))
{
strong = From(value);
strong = StrongOf<Guid, TStrong>.From(value);
return true;
}

Expand Down
38 changes: 38 additions & 0 deletions tests/StrongOf.UnitTests/StrongGuidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@ public class StrongGuidTests
private sealed class TestGuidOf(Guid Value) : StrongGuid<TestGuidOf>(Value) { }
private sealed class OtherTestGuidOf(Guid Value) : StrongGuid<OtherTestGuidOf>(Value) { }

[Fact]
public void FromGuid_NullValue_ReturnsNull()
{
Assert.Null(TestGuidOf.FromGuid(null));
}

[Fact]
public void FromGuid_ValidValue_ReturnsStrongGuid()
{
Guid guid = Guid.NewGuid();
TestGuidOf strongGuid = TestGuidOf.FromGuid(guid);

Assert.NotNull(strongGuid);
Assert.Equal(guid, strongGuid.AsGuid());
}

[Fact]
public void FromString_NullValue_ReturnsNull()
{
Assert.Null(TestGuidOf.FromString(null));
}

[Fact]
public void FromString_InvalidValue_ReturnsNull()
{
Assert.Null(TestGuidOf.FromString("invalid"));
}

[Fact]
public void FromString_ValidValue_ReturnsStrongGuid()
{
Guid guid = Guid.NewGuid();
TestGuidOf strongGuid = TestGuidOf.FromString(guid.ToString());

Assert.NotNull(strongGuid);
Assert.Equal(guid, strongGuid.AsGuid());
}

[Fact]
public void Equals_WithDifferentType_ReturnsFalse()
{
Expand Down

0 comments on commit 0d6be33

Please sign in to comment.