Skip to content

Commit

Permalink
Merge pull request #6 from BenjaminAbt/feature/more-functions
Browse files Browse the repository at this point in the history
add more functionality
  • Loading branch information
BenjaminAbt authored Dec 27, 2023
2 parents 890199e + 7ad14b5 commit 1883273
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 21 deletions.
55 changes: 55 additions & 0 deletions src/StrongOf/Strong.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace StrongOf;

/// <summary>
/// Provides utility methods for strong types.
/// </summary>
public static class Strong
{
/// <summary>
/// Checks if the given strong type is null.
/// </summary>
/// <typeparam name="TStrong">The type of the strong object.</typeparam>
/// <param name="strong">The strong object to check.</param>
/// <returns>true if the strong object is null; otherwise, false.</returns>
public static bool IsNull<TStrong>(TStrong? strong)
where TStrong : IStrongOf
{
return strong is null;
}

/// <summary>
/// Checks if the given strong type is not null.
/// </summary>
/// <typeparam name="TStrong">The type of the strong object.</typeparam>
/// <param name="strong">The strong object to check.</param>
/// <returns>true if the strong object is not null; otherwise, false.</returns>
public static bool IsNotNull<TStrong>(TStrong? strong)
where TStrong : IStrongOf
{
return strong is not null;
}

/// <summary>
/// Checks if the given strong string is null or empty.
/// </summary>
/// <typeparam name="TStrong">The type of the strong string.</typeparam>
/// <param name="strong">The strong string to check.</param>
/// <returns>true if the strong string is null or empty; otherwise, false.</returns>
public static bool IsNullOrEmpty<TStrong>(TStrong? strong)
where TStrong : StrongString<TStrong>
{
return strong is null || strong.IsEmpty();
}

/// <summary>
/// Checks if the given strong string is not null or empty.
/// </summary>
/// <typeparam name="TStrong">The type of the strong string.</typeparam>
/// <param name="strong">The strong string to check.</param>
/// <returns>true if the strong string is not null or empty; otherwise, false.</returns>
public static bool IsNotNullOrEmpty<TStrong>(TStrong? strong)
where TStrong : StrongString<TStrong>
{
return strong is not null && strong.IsEmpty() is false;
}
}
89 changes: 89 additions & 0 deletions src/StrongOf/StrongString.Methods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace StrongOf;

/// <summary>
/// Represents a strong string of a specific type.
/// </summary>
/// <typeparam name="TStrong">The type of the strong string.</typeparam>
public abstract partial class StrongString<TStrong>
{
/// <summary>
/// Trims all leading and trailing white-space characters from the current string.
/// </summary>
/// <returns>A new strong string equivalent to the current string but without leading and trailing white-space characters.</returns>
public TStrong Trim()
=> From(Value.Trim());

/// <summary>
/// Trims all leading white-space characters from the current string.
/// </summary>
/// <returns>A new strong string equivalent to the current string but without leading white-space characters.</returns>
public TStrong TrimStart()
=> From(Value.TrimStart());

/// <summary>
/// Trims all trailing white-space characters from the current string.
/// </summary>
/// <returns>A new strong string equivalent to the current string but without trailing white-space characters.</returns>
public TStrong TrimEnd()
=> From(Value.TrimEnd());

/// <summary>
/// Determines whether this string and a specified string have the same value.
/// </summary>
/// <param name="value">The string to compare to this instance.</param>
/// <param name="stringComparison">One of the enumeration values that specifies the rules for the comparison.</param>
/// <returns>true if the value of the value parameter is the same as this string; otherwise, false.</returns>
public bool Equals(string value, StringComparison stringComparison)
=> Value.Equals(value, stringComparison);

/// <summary>
/// Determines whether this string and a specified StrongString object have the same value.
/// </summary>
/// <param name="other">The StrongString object to compare to this instance.</param>
/// <param name="stringComparison">One of the enumeration values that specifies the rules for the comparison.</param>
/// <returns>true if the value of the other parameter is the same as this string; otherwise, false.</returns>
public bool Equals<T>(T other, StringComparison stringComparison) where T : TStrong
=> Value.Equals(other.Value, stringComparison);

/// <summary>
/// Returns a copy of this string converted to lowercase.
/// </summary>
/// <returns>A string in lowercase.</returns>
public TStrong ToLower()
=> From(Value.ToLower());

/// <summary>
/// Returns a copy of this string converted to lowercase, using the casing rules of the invariant culture.
/// </summary>
/// <returns>A string in lowercase.</returns>
public TStrong ToLowerInvariant()
=> From(Value.ToLowerInvariant());

/// <summary>
/// Returns a copy of this string converted to uppercase.
/// </summary>
/// <returns>A string in uppercase.</returns>
public TStrong ToUpper()
=> From(Value.ToUpper());

/// <summary>
/// Returns a copy of this string converted to uppercase, using the casing rules of the invariant culture.
/// </summary>
/// <returns>A string in uppercase.</returns>
public TStrong ToUpperInvariant()
=> From(Value.ToUpperInvariant());

/// <summary>
/// Returns the first character of the current string.
/// </summary>
/// <returns>The first character of the current string.</returns>
public char FirstChar()
=> Value.First();

/// <summary>
/// Returns the first character of the current string converted to uppercase, using the casing rules of the invariant culture.
/// </summary>
/// <returns>The first character of the current string in uppercase.</returns>
public char FirstCharUpperInvariant()
=> char.ToUpperInvariant(FirstChar());
}
2 changes: 1 addition & 1 deletion src/StrongOf/StrongString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace StrongOf;
/// Represents a strong type of string.
/// </summary>
/// <typeparam name="TStrong">The type of the strong string.</typeparam>
public abstract class StrongString<TStrong>(string Value) : StrongOf<string, TStrong>(Value), IComparable, IStrongString
public abstract partial class StrongString<TStrong>(string Value) : StrongOf<string, TStrong>(Value), IComparable, IStrongString
where TStrong : StrongString<TStrong>
{
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongCharTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace StrongOf.Tests;

public class StrongCharTests
{
private sealed class TestCharOf(char value) : StrongChar<TestCharOf>(value) { }
private sealed class OtherTestCharOf(char value) : StrongChar<OtherTestCharOf>(value) { }
private sealed class TestCharOf(char Value) : StrongChar<TestCharOf>(Value) { }
private sealed class OtherTestCharOf(char Value) : StrongChar<OtherTestCharOf>(Value) { }

[Fact]
public void Equals_WithDifferentType_ReturnsFalse()
Expand Down
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongDateTimeOffsetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace StrongOf.Tests;

public class StrongDateTimeOffsetTests
{
private sealed class TestDateTimeOffsetOf(DateTimeOffset value) : StrongDateTimeOffset<TestDateTimeOffsetOf>(value) { }
private sealed class OtherTestDateTimeOffsetOf(DateTimeOffset value) : StrongDateTimeOffset<OtherTestDateTimeOffsetOf>(value) { }
private sealed class TestDateTimeOffsetOf(DateTimeOffset Value) : StrongDateTimeOffset<TestDateTimeOffsetOf>(Value) { }
private sealed class OtherTestDateTimeOffsetOf(DateTimeOffset Value) : StrongDateTimeOffset<OtherTestDateTimeOffsetOf>(Value) { }

[Fact]
public void Equals_WithDifferentType_ReturnsFalse()
Expand Down
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongDateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace StrongOf.Tests;

public class StrongDateTimeTests
{
private sealed class TestDateTimeOf(DateTime value) : StrongDateTime<TestDateTimeOf>(value) { }
private sealed class OtherTestDateTimeOf(DateTime value) : StrongDateTime<OtherTestDateTimeOf>(value) { }
private sealed class TestDateTimeOf(DateTime Value) : StrongDateTime<TestDateTimeOf>(Value) { }
private sealed class OtherTestDateTimeOf(DateTime Value) : StrongDateTime<OtherTestDateTimeOf>(Value) { }

[Fact]
public void Equals_WithDifferentType_ReturnsFalse()
Expand Down
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongDecimalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace StrongOf.Tests;

public class StrongDecimalTests
{
private sealed class TestDecimalOf(decimal value) : StrongDecimal<TestDecimalOf>(value) { }
private sealed class OtherTestDecimalOf(decimal value) : StrongDecimal<OtherTestDecimalOf>(value) { }
private sealed class TestDecimalOf(decimal Value) : StrongDecimal<TestDecimalOf>(Value) { }
private sealed class OtherTestDecimalOf(decimal Value) : StrongDecimal<OtherTestDecimalOf>(Value) { }

[Fact]
public void Equals_WithDifferentType_ReturnsFalse()
Expand Down
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongGuidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests;

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

[Fact]
public void Equals_WithDifferentType_ReturnsFalse()
Expand Down
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongInt32Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests;

public class StrongInt32Tests
{
private sealed class TestInt32Of(int value) : StrongInt32<TestInt32Of>(value) { }
private sealed class OtherTestInt32Of(int value) : StrongInt32<OtherTestInt32Of>(value) { }
private sealed class TestInt32Of(int Value) : StrongInt32<TestInt32Of>(Value) { }
private sealed class OtherTestInt32Of(int Value) : StrongInt32<OtherTestInt32Of>(Value) { }

[Fact]
public void NewFrom_ShouldBeTheSame()
Expand Down
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongInt64Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests;

public class StrongInt64Tests
{
private sealed class TestInt64Of(long value) : StrongInt64<TestInt64Of>(value) { }
private sealed class OtherTestInt64Of(long value) : StrongInt64<OtherTestInt64Of>(value) { }
private sealed class TestInt64Of(long Value) : StrongInt64<TestInt64Of>(Value) { }
private sealed class OtherTestInt64Of(long Value) : StrongInt64<OtherTestInt64Of>(Value) { }

[Fact]
public void NewFrom_ShouldBeTheSame()
Expand Down
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongOfTests_Equals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests;

public class StrongOfTests_Equals
{
private sealed class TestOf(int value) : StrongOf<int, TestOf>(value) { }
private sealed class OtherTestOf(int value) : StrongOf<int, OtherTestOf>(value) { }
private sealed class TestOf(int Value) : StrongOf<int, TestOf>(Value) { }
private sealed class OtherTestOf(int Value) : StrongOf<int, OtherTestOf>(Value) { }

[Fact]
public void Equals_WithSameReference_ReturnsTrue()
Expand Down
2 changes: 1 addition & 1 deletion tests/StrongOf.UnitTests/StrongOfTests_Obj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace StrongOf.UnitTests;

public class StrongOfTests_EqualsTests
{
private sealed class TestOf(int value) : StrongOf<int, TestOf>(value) { }
private sealed class TestOf(int Value) : StrongOf<int, TestOf>(Value) { }

[Fact]
public void GetHashCode_ReturnsExpectedHashCode()
Expand Down
2 changes: 1 addition & 1 deletion tests/StrongOf.UnitTests/StrongOfTests_Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace StrongOf.UnitTests;

public class StrongOfTests_Operators
{
private sealed class TestOf(int value) : StrongOf<int, TestOf>(value) { }
private sealed class TestOf(int Value) : StrongOf<int, TestOf>(Value) { }

[Fact]
public void OperatorEquals_WithNullObjects_ReturnsTrue()
Expand Down
74 changes: 74 additions & 0 deletions tests/StrongOf.UnitTests/StrongString.Methods.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Xunit;

namespace StrongOf.UnitTests;

public class StrongStringMethodsTests
{
private sealed class TestStringOf(string Value) : StrongString<TestStringOf>(Value) { }


[Fact]
public void Trim_ShouldRemoveLeadingAndTrailingWhiteSpace()
{
TestStringOf strongString = new(" Test ");
TestStringOf result = strongString.Trim();
Assert.Equal("Test", result.Value);
}

[Fact]
public void TrimStart_ShouldRemoveLeadingWhiteSpace()
{
TestStringOf strongString = new(" Test");
StrongString<TestStringOf> result = strongString.TrimStart();
Assert.Equal("Test", result.Value);
}

[Fact]
public void TrimEnd_ShouldRemoveTrailingWhiteSpace()
{
TestStringOf strongString = new("Test ");
StrongString<TestStringOf> result = strongString.TrimEnd();
Assert.Equal("Test", result.Value);
}

[Fact]
public void Equals_ShouldReturnTrueWhenValuesAreEqual()
{
TestStringOf strongString = new("Test");

Assert.True(strongString.Equals("Test", StringComparison.Ordinal));
Assert.False(strongString.Equals("test", StringComparison.Ordinal));
}

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

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

[Fact]
public void FirstChar_ShouldReturnFirstCharacterOfString()
{
TestStringOf strongString = new("Test");
char result = strongString.FirstChar();
Assert.Equal('T', result);
}

[Fact]
public void FirstCharUpperInvariant_ShouldReturnFirstCharacterOfStringInUpperCase()
{
TestStringOf strongString = new("test");
char result = strongString.FirstCharUpperInvariant();
Assert.Equal('T', result);
}
}
4 changes: 2 additions & 2 deletions tests/StrongOf.UnitTests/StrongStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests;

public class StrongStringTests
{
private sealed class TestStringOf(string value) : StrongString<TestStringOf>(value) { }
private sealed class OtherTestStringOf(string value) : StrongString<OtherTestStringOf>(value) { }
private sealed class TestStringOf(string Value) : StrongString<TestStringOf>(Value) { }
private sealed class OtherTestStringOf(string Value) : StrongString<OtherTestStringOf>(Value) { }

[Fact]
public void Equals_WithDifferentType_ReturnsFalse()
Expand Down
Loading

0 comments on commit 1883273

Please sign in to comment.