Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminAbt committed Apr 28, 2024
1 parent 34783cd commit a27d9bc
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ dotnet_naming_style.pascal_case.capitalization = pascal_case
# IDE0290: Use primary constructor
dotnet_diagnostic.IDE0290.severity = none

# MA0016: Prefer using collection abstraction instead of implementation
dotnet_diagnostic.MA0016.severity = none

# MA0017: Abstract types should not have public or internal constructors
dotnet_diagnostic.MA0017.severity = none

Expand Down
7 changes: 0 additions & 7 deletions src/StrongOf/StrongGuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ public abstract partial class StrongGuid<TStrong>(Guid Value)
return null;
}

/// <summary>
/// Initializes a new instance of the StrongGuid class using the specified string representation of a Guid.
/// </summary>
/// <param name="value">A string containing a Guid to use for initialization.</param>
/// <remarks>Uses <see cref="Guid.Parse(string)"/></remarks>
public StrongGuid(string value) : this(Guid.Parse(value)) { }

/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/StrongOf/StrongOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static TStrong From(TTarget value)
/// <param name="source">The source to create the list of strong types from.</param>
/// <returns>A list of strong types.</returns>
[return: NotNullIfNotNull(nameof(source))]
public static ICollection<TStrong>? From(IEnumerable<TTarget>? source)
public static List<TStrong>? From(IEnumerable<TTarget>? source)
=> source?.Select(e => From(e)).ToList();


Expand Down
4 changes: 2 additions & 2 deletions src/StrongOf/StrongString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract partial class StrongString<TStrong>(string Value)
[return: NotNullIfNotNull(nameof(value))]
public static TStrong? FromNullable(string? value)
{
if (value != null)
if (value is not null)
{
TStrong strong = From(value);
return strong;
Expand Down Expand Up @@ -74,7 +74,7 @@ public static TStrong Empty()
/// </summary>
/// <returns>True if the current instance is empty; otherwise, false.</returns>
public bool IsEmpty()
=> Value == "";
=> Value is "";

// Equals

Expand Down
2 changes: 1 addition & 1 deletion tests/StrongOf.UnitTests/StrongDateTimeOffset_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void CompareTo_ShouldReturnCorrectOrder()

Assert.True(first.CompareTo(second) < 0);
Assert.True(second.CompareTo(first) > 0);
Assert.True(first.CompareTo(first) == 0);
Assert.Equal(0, first.CompareTo(first));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion tests/StrongOf.UnitTests/StrongDecimalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void OperatorEquals_ShouldReturnTrueForEqualValues()
public void OperatorEquals_Null()
{
TestDecimalOf strongDecimal = new(1.23m);
Assert.True(strongDecimal != null);
Assert.NotNull(strongDecimal);
Assert.False(strongDecimal == null);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/StrongOf.UnitTests/StrongGuidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace StrongOf.UnitTests;

public class StrongGuidTests
{
private sealed class TestGuidOf(Guid Value) : StrongGuid<TestGuidOf>(Value) { }
private sealed class TestGuidOf(Guid Value) : StrongGuid<TestGuidOf>(Value);

private sealed class OtherTestGuidOf(Guid Value) : StrongGuid<OtherTestGuidOf>(Value) { }

[Fact]
Expand Down Expand Up @@ -147,7 +148,7 @@ public void ToStringWithoutDashes_ShouldReturnCorrectFormat()
public void OperatorEquals_Null()
{
TestGuidOf strongGuid = new(Guid.Parse("00000000-0000-0000-0000-000000000001"));
Assert.True(strongGuid != null);
Assert.False(strongGuid == null);
Assert.NotNull(strongGuid);
Assert.NotNull(strongGuid);
}
}
39 changes: 39 additions & 0 deletions tests/StrongOf.UnitTests/StrongInt32_As_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,43 @@ public void AsInt32_ReturnsCorrectResult()
// Assert
Assert.Equal(strong.Value, strong.AsInt32());
}

[Fact]
public void FromNullable_WithValue_ReturnsNonNull()
{
// Arrange
int value = 7;

// Act
TestInt32Of result = TestInt32Of.FromNullable(value);

// Assert
Assert.NotNull(result);
}

[Fact]
public void FromNullable_WithNull_ReturnsNull()
{
// Arrange
int? value = null;

// Act
TestInt32Of? result = TestInt32Of.FromNullable(value);

// Assert
Assert.Null(result);
}

[Fact]
public void FromNullable_WithNotNull_ReturnsCorrectValue()
{
// Arrange
int value = 7;

// Act
TestInt32Of result = TestInt32Of.FromNullable(value);

// Assert
Assert.Equal(value, result.Value);
}
}
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongInt32_Operator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void OperatorNotEquals_ShouldReturnTrueForDifferentValues()
public void OperatorEquals_Null()
{
TestInt32Of strongInt = new(123);
Assert.True(strongInt != null);
Assert.False(strongInt == null);
Assert.NotNull(strongInt);
Assert.NotNull(strongInt);
}

[Theory]
Expand Down
39 changes: 39 additions & 0 deletions tests/StrongOf.UnitTests/StrongInt64_As_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,43 @@ public void AsInt64_ReturnsCorrectResult()
// Assert
Assert.Equal(strong.Value, strong.AsInt64());
}

[Fact]
public void FromNullable_WithValue_ReturnsNonNull()
{
// Arrange
long value = 7;

// Act
TestInt64Of result = TestInt64Of.FromNullable(value);

// Assert
Assert.NotNull(result);
}

[Fact]
public void FromNullable_WithNull_ReturnsNull()
{
// Arrange
long? value = null;

// Act
TestInt64Of? result = TestInt64Of.FromNullable(value);

// Assert
Assert.Null(result);
}

[Fact]
public void FromNullable_WithNotNull_ReturnsCorrectValue()
{
// Arrange
long value = 7;

// Act
TestInt64Of result = TestInt64Of.FromNullable(value);

// Assert
Assert.Equal(value, result.Value);
}
}
39 changes: 39 additions & 0 deletions tests/StrongOf.UnitTests/StrongString_As_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,43 @@ public void AsString_ReturnsCorrectResult()
// Assert
Assert.Equal(strong.Value, strong.AsString());
}

[Fact]
public void FromNullable_WithValue_ReturnsNonNull()
{
// Arrange
string value = "A";

// Act
TestStringOf result = TestStringOf.FromNullable(value);

// Assert
Assert.NotNull(result);
}

[Fact]
public void FromNullable_WithNull_ReturnsNull()
{
// Arrange
string? value = null;

// Act
TestStringOf? result = TestStringOf.FromNullable(value);

// Assert
Assert.Null(result);
}

[Fact]
public void FromNullable_WithNotNull_ReturnsCorrectValue()
{
// Arrange
string value = "A";

// Act
TestStringOf result = TestStringOf.FromNullable(value);

// Assert
Assert.Equal(value, result.Value);
}
}
26 changes: 26 additions & 0 deletions tests/StrongOf.UnitTests/StrongString_Methods_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public void Equals_ShouldReturnTrueWhenValuesAreEqual()
Assert.False(strongString.Equals("test", StringComparison.Ordinal));
}

[Fact]
public void EqualsStrong_ShouldReturnTrueWhenValuesAreEqual()
{
TestStringOf strongString1 = new("Test");
TestStringOf strongString2 = new("Test");

Assert.True(strongString1.Equals("Test", StringComparison.Ordinal));
Assert.True(strongString2.Equals("Test", StringComparison.Ordinal));
}

[Fact]
public void ToLower_ShouldReturnLowerCaseString()
{
Expand All @@ -48,6 +58,14 @@ public void ToLower_ShouldReturnLowerCaseString()
Assert.Equal("test", result.Value);
}

[Fact]
public void ToLowerInvariant_ShouldReturnLowerCaseString()
{
TestStringOf strongString = new("TEST");
TestStringOf result = strongString.ToLowerInvariant();
Assert.Equal("test", result.Value);
}

[Fact]
public void ToUpper_ShouldReturnUpperCaseString()
{
Expand All @@ -56,6 +74,14 @@ public void ToUpper_ShouldReturnUpperCaseString()
Assert.Equal("TEST", result.Value);
}

[Fact]
public void ToUpperInvariant_ShouldReturnUpperCaseString()
{
TestStringOf strongString = new("test");
TestStringOf result = strongString.ToUpperInvariant();
Assert.Equal("TEST", result.Value);
}

[Fact]
public void FirstChar_ShouldReturnFirstCharacterOfString()
{
Expand Down

0 comments on commit a27d9bc

Please sign in to comment.