diff --git a/src/StrongOf/Strong.cs b/src/StrongOf/Strong.cs new file mode 100644 index 0000000..b80af68 --- /dev/null +++ b/src/StrongOf/Strong.cs @@ -0,0 +1,55 @@ +namespace StrongOf; + +/// +/// Provides utility methods for strong types. +/// +public static class Strong +{ + /// + /// Checks if the given strong type is null. + /// + /// The type of the strong object. + /// The strong object to check. + /// true if the strong object is null; otherwise, false. + public static bool IsNull(TStrong? strong) + where TStrong : IStrongOf + { + return strong is null; + } + + /// + /// Checks if the given strong type is not null. + /// + /// The type of the strong object. + /// The strong object to check. + /// true if the strong object is not null; otherwise, false. + public static bool IsNotNull(TStrong? strong) + where TStrong : IStrongOf + { + return strong is not null; + } + + /// + /// Checks if the given strong string is null or empty. + /// + /// The type of the strong string. + /// The strong string to check. + /// true if the strong string is null or empty; otherwise, false. + public static bool IsNullOrEmpty(TStrong? strong) + where TStrong : StrongString + { + return strong is null || strong.IsEmpty(); + } + + /// + /// Checks if the given strong string is not null or empty. + /// + /// The type of the strong string. + /// The strong string to check. + /// true if the strong string is not null or empty; otherwise, false. + public static bool IsNotNullOrEmpty(TStrong? strong) + where TStrong : StrongString + { + return strong is not null && strong.IsEmpty() is false; + } +} diff --git a/src/StrongOf/StrongString.Methods.cs b/src/StrongOf/StrongString.Methods.cs new file mode 100644 index 0000000..ec73b97 --- /dev/null +++ b/src/StrongOf/StrongString.Methods.cs @@ -0,0 +1,89 @@ +namespace StrongOf; + +/// +/// Represents a strong string of a specific type. +/// +/// The type of the strong string. +public abstract partial class StrongString +{ + /// + /// Trims all leading and trailing white-space characters from the current string. + /// + /// A new strong string equivalent to the current string but without leading and trailing white-space characters. + public TStrong Trim() + => From(Value.Trim()); + + /// + /// Trims all leading white-space characters from the current string. + /// + /// A new strong string equivalent to the current string but without leading white-space characters. + public TStrong TrimStart() + => From(Value.TrimStart()); + + /// + /// Trims all trailing white-space characters from the current string. + /// + /// A new strong string equivalent to the current string but without trailing white-space characters. + public TStrong TrimEnd() + => From(Value.TrimEnd()); + + /// + /// Determines whether this string and a specified string have the same value. + /// + /// The string to compare to this instance. + /// One of the enumeration values that specifies the rules for the comparison. + /// true if the value of the value parameter is the same as this string; otherwise, false. + public bool Equals(string value, StringComparison stringComparison) + => Value.Equals(value, stringComparison); + + /// + /// Determines whether this string and a specified StrongString object have the same value. + /// + /// The StrongString object to compare to this instance. + /// One of the enumeration values that specifies the rules for the comparison. + /// true if the value of the other parameter is the same as this string; otherwise, false. + public bool Equals(T other, StringComparison stringComparison) where T : TStrong + => Value.Equals(other.Value, stringComparison); + + /// + /// Returns a copy of this string converted to lowercase. + /// + /// A string in lowercase. + public TStrong ToLower() + => From(Value.ToLower()); + + /// + /// Returns a copy of this string converted to lowercase, using the casing rules of the invariant culture. + /// + /// A string in lowercase. + public TStrong ToLowerInvariant() + => From(Value.ToLowerInvariant()); + + /// + /// Returns a copy of this string converted to uppercase. + /// + /// A string in uppercase. + public TStrong ToUpper() + => From(Value.ToUpper()); + + /// + /// Returns a copy of this string converted to uppercase, using the casing rules of the invariant culture. + /// + /// A string in uppercase. + public TStrong ToUpperInvariant() + => From(Value.ToUpperInvariant()); + + /// + /// Returns the first character of the current string. + /// + /// The first character of the current string. + public char FirstChar() + => Value.First(); + + /// + /// Returns the first character of the current string converted to uppercase, using the casing rules of the invariant culture. + /// + /// The first character of the current string in uppercase. + public char FirstCharUpperInvariant() + => char.ToUpperInvariant(FirstChar()); +} diff --git a/src/StrongOf/StrongString.cs b/src/StrongOf/StrongString.cs index 349fa54..7a2d6dc 100644 --- a/src/StrongOf/StrongString.cs +++ b/src/StrongOf/StrongString.cs @@ -6,7 +6,7 @@ namespace StrongOf; /// Represents a strong type of string. /// /// The type of the strong string. -public abstract class StrongString(string Value) : StrongOf(Value), IComparable, IStrongString +public abstract partial class StrongString(string Value) : StrongOf(Value), IComparable, IStrongString where TStrong : StrongString { /// diff --git a/tests/StrongOf.UnitTests/StrongCharTests.cs b/tests/StrongOf.UnitTests/StrongCharTests.cs index 53c8df4..af0acfa 100644 --- a/tests/StrongOf.UnitTests/StrongCharTests.cs +++ b/tests/StrongOf.UnitTests/StrongCharTests.cs @@ -4,8 +4,8 @@ namespace StrongOf.Tests; public class StrongCharTests { - private sealed class TestCharOf(char value) : StrongChar(value) { } - private sealed class OtherTestCharOf(char value) : StrongChar(value) { } + private sealed class TestCharOf(char Value) : StrongChar(Value) { } + private sealed class OtherTestCharOf(char Value) : StrongChar(Value) { } [Fact] public void Equals_WithDifferentType_ReturnsFalse() diff --git a/tests/StrongOf.UnitTests/StrongDateTimeOffsetTests.cs b/tests/StrongOf.UnitTests/StrongDateTimeOffsetTests.cs index 79b230a..91adccd 100644 --- a/tests/StrongOf.UnitTests/StrongDateTimeOffsetTests.cs +++ b/tests/StrongOf.UnitTests/StrongDateTimeOffsetTests.cs @@ -5,8 +5,8 @@ namespace StrongOf.Tests; public class StrongDateTimeOffsetTests { - private sealed class TestDateTimeOffsetOf(DateTimeOffset value) : StrongDateTimeOffset(value) { } - private sealed class OtherTestDateTimeOffsetOf(DateTimeOffset value) : StrongDateTimeOffset(value) { } + private sealed class TestDateTimeOffsetOf(DateTimeOffset Value) : StrongDateTimeOffset(Value) { } + private sealed class OtherTestDateTimeOffsetOf(DateTimeOffset Value) : StrongDateTimeOffset(Value) { } [Fact] public void Equals_WithDifferentType_ReturnsFalse() diff --git a/tests/StrongOf.UnitTests/StrongDateTimeTests.cs b/tests/StrongOf.UnitTests/StrongDateTimeTests.cs index aae4c7c..422b608 100644 --- a/tests/StrongOf.UnitTests/StrongDateTimeTests.cs +++ b/tests/StrongOf.UnitTests/StrongDateTimeTests.cs @@ -5,8 +5,8 @@ namespace StrongOf.Tests; public class StrongDateTimeTests { - private sealed class TestDateTimeOf(DateTime value) : StrongDateTime(value) { } - private sealed class OtherTestDateTimeOf(DateTime value) : StrongDateTime(value) { } + private sealed class TestDateTimeOf(DateTime Value) : StrongDateTime(Value) { } + private sealed class OtherTestDateTimeOf(DateTime Value) : StrongDateTime(Value) { } [Fact] public void Equals_WithDifferentType_ReturnsFalse() diff --git a/tests/StrongOf.UnitTests/StrongDecimalTests.cs b/tests/StrongOf.UnitTests/StrongDecimalTests.cs index 13f51b3..3446017 100644 --- a/tests/StrongOf.UnitTests/StrongDecimalTests.cs +++ b/tests/StrongOf.UnitTests/StrongDecimalTests.cs @@ -5,8 +5,8 @@ namespace StrongOf.Tests; public class StrongDecimalTests { - private sealed class TestDecimalOf(decimal value) : StrongDecimal(value) { } - private sealed class OtherTestDecimalOf(decimal value) : StrongDecimal(value) { } + private sealed class TestDecimalOf(decimal Value) : StrongDecimal(Value) { } + private sealed class OtherTestDecimalOf(decimal Value) : StrongDecimal(Value) { } [Fact] public void Equals_WithDifferentType_ReturnsFalse() diff --git a/tests/StrongOf.UnitTests/StrongGuidTests.cs b/tests/StrongOf.UnitTests/StrongGuidTests.cs index 0b7895d..d410e89 100644 --- a/tests/StrongOf.UnitTests/StrongGuidTests.cs +++ b/tests/StrongOf.UnitTests/StrongGuidTests.cs @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests; public class StrongGuidTests { - private sealed class TestGuidOf(Guid value) : StrongGuid(value) { } - private sealed class OtherTestGuidOf(Guid value) : StrongGuid(value) { } + private sealed class TestGuidOf(Guid Value) : StrongGuid(Value) { } + private sealed class OtherTestGuidOf(Guid Value) : StrongGuid(Value) { } [Fact] public void Equals_WithDifferentType_ReturnsFalse() diff --git a/tests/StrongOf.UnitTests/StrongInt32Tests.cs b/tests/StrongOf.UnitTests/StrongInt32Tests.cs index 1dbc4e4..48769e0 100644 --- a/tests/StrongOf.UnitTests/StrongInt32Tests.cs +++ b/tests/StrongOf.UnitTests/StrongInt32Tests.cs @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests; public class StrongInt32Tests { - private sealed class TestInt32Of(int value) : StrongInt32(value) { } - private sealed class OtherTestInt32Of(int value) : StrongInt32(value) { } + private sealed class TestInt32Of(int Value) : StrongInt32(Value) { } + private sealed class OtherTestInt32Of(int Value) : StrongInt32(Value) { } [Fact] public void NewFrom_ShouldBeTheSame() diff --git a/tests/StrongOf.UnitTests/StrongInt64Tests.cs b/tests/StrongOf.UnitTests/StrongInt64Tests.cs index 66bb057..c2f3273 100644 --- a/tests/StrongOf.UnitTests/StrongInt64Tests.cs +++ b/tests/StrongOf.UnitTests/StrongInt64Tests.cs @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests; public class StrongInt64Tests { - private sealed class TestInt64Of(long value) : StrongInt64(value) { } - private sealed class OtherTestInt64Of(long value) : StrongInt64(value) { } + private sealed class TestInt64Of(long Value) : StrongInt64(Value) { } + private sealed class OtherTestInt64Of(long Value) : StrongInt64(Value) { } [Fact] public void NewFrom_ShouldBeTheSame() diff --git a/tests/StrongOf.UnitTests/StrongOfTests_Equals.cs b/tests/StrongOf.UnitTests/StrongOfTests_Equals.cs index a78d857..e3b7fa1 100644 --- a/tests/StrongOf.UnitTests/StrongOfTests_Equals.cs +++ b/tests/StrongOf.UnitTests/StrongOfTests_Equals.cs @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests; public class StrongOfTests_Equals { - private sealed class TestOf(int value) : StrongOf(value) { } - private sealed class OtherTestOf(int value) : StrongOf(value) { } + private sealed class TestOf(int Value) : StrongOf(Value) { } + private sealed class OtherTestOf(int Value) : StrongOf(Value) { } [Fact] public void Equals_WithSameReference_ReturnsTrue() diff --git a/tests/StrongOf.UnitTests/StrongOfTests_Obj.cs b/tests/StrongOf.UnitTests/StrongOfTests_Obj.cs index e01ab45..450f87f 100644 --- a/tests/StrongOf.UnitTests/StrongOfTests_Obj.cs +++ b/tests/StrongOf.UnitTests/StrongOfTests_Obj.cs @@ -4,7 +4,7 @@ namespace StrongOf.UnitTests; public class StrongOfTests_EqualsTests { - private sealed class TestOf(int value) : StrongOf(value) { } + private sealed class TestOf(int Value) : StrongOf(Value) { } [Fact] public void GetHashCode_ReturnsExpectedHashCode() diff --git a/tests/StrongOf.UnitTests/StrongOfTests_Operators.cs b/tests/StrongOf.UnitTests/StrongOfTests_Operators.cs index 651d4ad..b75fbe1 100644 --- a/tests/StrongOf.UnitTests/StrongOfTests_Operators.cs +++ b/tests/StrongOf.UnitTests/StrongOfTests_Operators.cs @@ -4,7 +4,7 @@ namespace StrongOf.UnitTests; public class StrongOfTests_Operators { - private sealed class TestOf(int value) : StrongOf(value) { } + private sealed class TestOf(int Value) : StrongOf(Value) { } [Fact] public void OperatorEquals_WithNullObjects_ReturnsTrue() diff --git a/tests/StrongOf.UnitTests/StrongString.Methods.Tests.cs b/tests/StrongOf.UnitTests/StrongString.Methods.Tests.cs new file mode 100644 index 0000000..147e946 --- /dev/null +++ b/tests/StrongOf.UnitTests/StrongString.Methods.Tests.cs @@ -0,0 +1,74 @@ +using Xunit; + +namespace StrongOf.UnitTests; + +public class StrongStringMethodsTests +{ + private sealed class TestStringOf(string Value) : StrongString(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 result = strongString.TrimStart(); + Assert.Equal("Test", result.Value); + } + + [Fact] + public void TrimEnd_ShouldRemoveTrailingWhiteSpace() + { + TestStringOf strongString = new("Test "); + StrongString 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); + } +} diff --git a/tests/StrongOf.UnitTests/StrongStringTests.cs b/tests/StrongOf.UnitTests/StrongStringTests.cs index c35ae28..b570d65 100644 --- a/tests/StrongOf.UnitTests/StrongStringTests.cs +++ b/tests/StrongOf.UnitTests/StrongStringTests.cs @@ -4,8 +4,8 @@ namespace StrongOf.UnitTests; public class StrongStringTests { - private sealed class TestStringOf(string value) : StrongString(value) { } - private sealed class OtherTestStringOf(string value) : StrongString(value) { } + private sealed class TestStringOf(string Value) : StrongString(Value) { } + private sealed class OtherTestStringOf(string Value) : StrongString(Value) { } [Fact] public void Equals_WithDifferentType_ReturnsFalse() diff --git a/tests/StrongOf.UnitTests/StrongTests.cs b/tests/StrongOf.UnitTests/StrongTests.cs new file mode 100644 index 0000000..bfd8748 --- /dev/null +++ b/tests/StrongOf.UnitTests/StrongTests.cs @@ -0,0 +1,78 @@ +using Xunit; + +namespace StrongOf.Tests; + +public class StrongTests +{ + private sealed class TestStringOf(string Value) : StrongString(Value) { } + + [Fact] + public void IsNull_ShouldReturnTrue_WhenStrongIsNull() + { + TestStringOf? strong = null; + Assert.True(Strong.IsNull(strong)); + } + + [Fact] + public void IsNull_ShouldReturnFalse_WhenStrongIsNotNull() + { + TestStringOf strong = new(""); + Assert.False(Strong.IsNull(strong)); + } + + [Fact] + public void IsNotNull_ShouldReturnTrue_WhenStrongIsNotNull() + { + TestStringOf strong = new(""); + Assert.True(Strong.IsNotNull(strong)); + } + + [Fact] + public void IsNotNull_ShouldReturnFalse_WhenStrongIsNull() + { + TestStringOf? strong = null; + Assert.False(Strong.IsNotNull(strong)); + } + + [Fact] + public void IsNullOrEmpty_ShouldReturnTrue_WhenStrongStringIsNull() + { + TestStringOf? strongString = null; + Assert.True(Strong.IsNullOrEmpty(strongString)); + } + + [Fact] + public void IsNullOrEmpty_ShouldReturnTrue_WhenStrongStringIsEmpty() + { + TestStringOf strongString = new(""); + Assert.True(Strong.IsNullOrEmpty(strongString)); + } + + [Fact] + public void IsNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsNotNullOrEmpty() + { + TestStringOf strongString = new("test"); + Assert.False(Strong.IsNullOrEmpty(strongString)); + } + + [Fact] + public void IsNotNullOrEmpty_ShouldReturnTrue_WhenStrongStringIsNotNullOrEmpty() + { + TestStringOf strongString = new("test"); + Assert.True(Strong.IsNotNullOrEmpty(strongString)); + } + + [Fact] + public void IsNotNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsNull() + { + TestStringOf? strongString = null; + Assert.False(Strong.IsNotNullOrEmpty(strongString)); + } + + [Fact] + public void IsNotNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsEmpty() + { + TestStringOf strongString = new(""); + Assert.False(Strong.IsNotNullOrEmpty(strongString)); + } +}